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. writeln(costo[i+j]);
  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 2 5 2
5 4 3 5 4
stdout
6
9
12
15
18
9
12
15
18
24
12
15
18
24
15
18
24
18
24
24
24