fork(2) download
  1. const maxn = 1000;
  2. oo = 1000000000;
  3. var n,i,j: longint;
  4. f,p: array[0..maxn] of longint;
  5. dp: array[0..maxn,0..maxn] of longint;
  6. min: longint;
  7. begin
  8. readln(n);
  9. f[0]:=0;
  10. for i:=1 to n do
  11. begin
  12. readln(p[i]);
  13. if p[i]>100 then f[i]:=f[i-1]+1
  14. else f[i]:=f[i-1];
  15. end;
  16. dp[0,0]:=0;
  17. for i:=1 to n do
  18. for j:=0 to f[i] do
  19. begin
  20. dp[i,j]:=oo;
  21. if j<=f[i-1] then dp[i,j]:=dp[i-1,j]+p[i];
  22. if (dp[i-1,j-1]+p[i]<dp[i,j]) and (p[i]>100) and (j>0) then dp[i,j]:=dp[i-1,j-1]+p[i];
  23. if (dp[i-1,j+1]<dp[i,j]) and (j<f[i-1]) then dp[i,j]:=dp[i-1,j+1];
  24. end;
  25. min:=oo;
  26. for i:=0 to f[n] do
  27. if dp[n,i]<min then min:=dp[n,i];
  28. writeln(min);
  29. end.
Success #stdin #stdout 0s 4196KB
stdin
5
35
40
101
59
63
stdout
1 0 35
1 0 35
1 0 35
2 0 75
2 0 75
2 0 75
3 0 176
3 0 176
3 0 176
3 1 1000000000
3 1 176
3 1 176
4 0 235
4 0 235
4 0 176
4 1 235
4 1 235
4 1 235
5 0 239
5 0 239
5 0 235
5 1 298
5 1 298
5 1 298
235