fork download
  1. program rollercoaster3;
  2. var N,K,i,countH, speed :Longint;
  3. S : Ansistring;
  4. roller : array[1..105] of Longint;
  5.  
  6. begin
  7. {
  8.   uncomment the two following lines if you want to read/write from files
  9.   assign(input, 'input.txt'); reset(input);
  10.   assign(output, 'output.txt'); rewrite(output);
  11. }
  12.  
  13. ReadLn(N);
  14.  
  15. ReadLn(S);
  16. K := 0; countH:=0; speed:=0;
  17.  
  18. { INSERT YOUR CODE HERE }
  19. if S[1]='U' then begin K:=-1; WriteLn(K); exit; end;
  20. if S[1]='H' then begin S[1]:='B'; K:=K+1; end;
  21. for i:=1 to N do
  22. Case S[i] of
  23. 'D' : roller[i]:=9;
  24. 'U' : roller[i]:=-11;
  25. 'H' : roller[i]:=0;
  26. 'B' : roller[i]:=10;
  27. end;
  28. for i:=1 to N do
  29. begin
  30. if S[i]='H' then countH:=countH+1;
  31. speed:=speed+roller[i];
  32. if speed<=0 then
  33. begin
  34. if countH>0 then
  35. begin
  36. countH:=countH-1;
  37. speed:=speed + 10;
  38. k:=k+1;
  39. end
  40. else
  41. begin
  42. k:=-1;
  43. break;
  44. end;
  45. end;
  46.  
  47. end;
  48. WriteLn(K);
  49. end.
  50.  
Success #stdin #stdout 0s 5288KB
stdin
10
HHHHHHHHHU

stdout
2