fork download
  1. Program machine;
  2. Uses Math;
  3. { constraints }
  4. const
  5. MAXD = 1000;
  6. MAXY = 1000000;
  7. { input data }
  8. var
  9. C, D, Y, i,j,k,w: longint;
  10. // Warning! M and P are 1-based
  11. M, P : array[1..MAXD] of longint;
  12. bilancio : array[0..MAXD] of longint;
  13. costo, indice : array[0..MAXY] of longint;
  14. begin
  15.  
  16. (*assign(input, 'input.txt'); reset(input);
  17.   assign(output, 'output.txt'); rewrite(output);*)
  18.  
  19.  
  20. readln(C, D, Y);
  21. // Warning! M and P are 1-based
  22. for i:=1 to D do
  23. read(M[i]);
  24. readln();
  25. for i:=1 to D do
  26. read(P[i]);
  27. readln();
  28. bilancio[1]:=C+M[1]-P[1];
  29. { insert your code here }
  30. bilancio[0]:=0; costo[0]:=0; w:=0;
  31. for i:=2 to D do bilancio[i]:=bilancio[i-1]+M[i]-P[i]+ P[i-1];
  32. for i:=1 to Y do costo[i]:=2147483647;
  33. for i:= 0 to D do
  34. begin
  35. for j:=1 to D do
  36. begin
  37. if (i+j <= Y) then
  38. begin
  39. if (costo[i] + bilancio[j])<=costo[i+j] then
  40. costo[i+j] := costo[i] + bilancio[j];
  41. write(Costo[i+j],' ',i,' ',j,' ', costo[i],' ', bilancio[j]); writeln;
  42. end;
  43. end;
  44. if ((i <= Y) and (costo[i] = bilancio[i])) then
  45. begin
  46. indice[w] := i;
  47. w := w+1;
  48. end;
  49. end;
  50. for i:= D to Y do
  51. begin
  52. for k:=0 to w-1 do
  53. begin
  54. j := indice[k];
  55. if (i+j <= Y) then
  56. begin
  57. if (costo[i+j] >= costo[i] + bilancio[j]) then
  58. costo[i+j] := costo[i] + bilancio[j];
  59. end;
  60. end;
  61. end;
  62.  
  63. writeln(costo[Y]); { print result }
  64. end.
Success #stdin #stdout 0s 5320KB
stdin
10 5 6
1 2 12 5 2
5 4 3 5 4
stdout
6 0 1 0 6
9 0 2 0 9
22 0 3 0 22
25 0 4 0 25
28 0 5 0 28
9 1 1 6 6
15 1 2 6 9
25 1 3 6 22
28 1 4 6 25
34 1 5 6 28
15 2 1 9 6
18 2 2 9 9
28 2 3 9 22
34 2 4 9 25
18 3 1 15 6
24 3 2 15 9
34 3 3 15 22
24 4 1 18 6
27 4 2 18 9
27 5 1 24 6
27