fork download
  1. program rollercoaster3;
  2. var N,K,i,countH, speed :int64;
  3. S : Ansistring;
  4. roller : array[1..100] of int64;
  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.  
  16. { INSERT YOUR CODE HERE }
  17. if S[1]='U' then begin K:=-1; WriteLn(K); exit; end;
  18. for i:=1 to 4 do section[i]:=0;
  19. for i:=1 to N do
  20. Case S[i] of
  21. 'D' : begin roller[i]:=9; section[1]:=section[1]+1; end;
  22. 'U' : begin roller[i]:=-11; section[2]:=section[2]+1; end;
  23. 'H' : begin roller[i]:=0; section[3]:=section[3]+1; end;
  24. 'B' : begin roller[i]:=10; section[4]:=section[4]+1; end;
  25. end;
  26. for i:=1 to N do
  27. begin
  28. if S[i]='H' then countH:=countH+1;
  29. speed:=speed+roller[i];
  30. if speed<=0 then
  31. begin
  32. if countH>0 then
  33. begin
  34. countH:=countH-1;
  35. speed:=speed + 10;
  36. if ((speed<=0) and ((countH<=0) and (i=N))) then begin K:=-1; WriteLn(K); exit; end;
  37. K:=K+1;
  38. end
  39. else begin K:=-1; WriteLn(K); exit; end;
  40. end;
  41. end;
  42. WriteLn(K);
  43. end.
Success #stdin #stdout 0s 5308KB
stdin
22
BBBBBBBBBBHUUUUUUUUUUB  
stdout
1