fork download
  1. program rollercoaster3;
  2. var N,K,i,countH, speed :Longint;
  3. S : Ansistring;
  4. roller : array[1..100] of Longint;
  5. section : array [1..4] of Longint;
  6.  
  7. begin
  8. (*assign(input, 'input.txt'); reset(input);
  9.   assign(output, 'output.txt'); rewrite(output);*)
  10.  
  11. ReadLn(N);
  12.  
  13. ReadLn(S);
  14. K := 0; countH:=0; speed:=0;
  15. for i:=1 to 4 do section[i]:=0;
  16. { INSERT YOUR CODE HERE }
  17. if S[1]='U' then begin K:=-1; WriteLn(K); exit; end;
  18. for i:=1 to N do
  19. Case S[i] of
  20. 'D' : begin roller[i]:=9; section[1]:=section[1]+1; end;
  21. 'U' : begin roller[i]:=-11; section[2]:=section[2]+1; end;
  22. 'H' : begin roller[i]:=0; section[3]:=section[3]+1; end;
  23. 'B' : begin roller[i]:=10; section[4]:=section[4]+1; end;
  24. end;
  25. for i:=1 to N do
  26. begin
  27. if S[i]='H' then countH:=countH+1;
  28. speed:=speed+roller[i];
  29. if speed<=0 then
  30. begin
  31. if countH>0 then
  32. begin
  33. countH:=countH-1;
  34. speed:=speed + 10;
  35. if ((speed<=0) and ((countH<=0) or (i=N))) then begin K:=-1; WriteLn(K); exit; end;
  36. K:=K+1;
  37. end
  38. else begin K:=-1; WriteLn(K); exit; end;
  39. end;
  40. end;
  41. if ((section[4] =0) and (section[2]= N div 2) and (section[3]+section[1]=N div 2)) then K:=section[3];
  42. WriteLn(K);
  43. end.
Success #stdin #stdout 0.01s 5288KB
stdin
8
DDDHUUUU
stdout
-1