| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 
 | program budget;
 {20:03 }
 
 type node=record
 
 u:integer;
 
 v,p:array[0..2]of integer;
 
 end;
 
 var w:array[1..60]of node;
 
 f:array[0..60,0..2000]of longint;
 
 vx,px,qx,g:array[1..60]of integer;
 
 m,n,i,k,j:integer;
 
 begin
 
 assign(input,'budget.in');reset(input);
 
 assign(output,'budget.out');rewrite(output);
 
 readln(n,m);
 
 k:=0;
 
 for i:=1 to m do begin
 
 readln(px[i],vx[i],qx[i]);
 
 if qx[i]=0 then begin
 
 inc(k);g[i]:=k;
 
 with w[k] do begin
 
 u:=0;
 
 v[0]:=vx[i];p[0]:=px[i];
 
 for j:=1 to 2 do begin
 
 v[j]:=0;p[j]:=0;
 
 end;
 
 end;
 
 end;
 
 end;
 
 for i:=1 to m do begin
 
 if qx[i]<>0 then
 
 with w[g[qx[i]]] do begin
 
 inc(u);v[u]:=vx[i];p[u]:=px[i];
 
 end;
 
 end;
 
 for i:=1 to k do
 
 with w[i] do begin
 
 write('w[',i,']:');
 
 for j:=0 to 2 do write('(',v[j],',',p[j],') ');
 
 writeln;
 
 end;
 
 
 
 fillchar(f,sizeof(f),0);
 
 
 
 for i:=1 to k do
 
 for j:=1 to n do
 
 with w[i] do begin
 
 f[i,j]:=f[i-1,j];
 
 
 
 if (f[i,j]< (f[i-1,j-p[0]]+p[0]*v[0]))and(j>=p[0]) then
 
 f[i,j]:=f[i-1,j-p[0]]+p[0]*v[0];
 
 
 
 if (f[i,j]< (f[i-1,j-p[0]-p[1]]+p[0]*v[0]+v[1]*p[1]))and(j>=p[0]+p[1]) then
 
 f[i,j]:=f[i-1,j-p[0]-p[1]]+p[0]*v[0]+v[1]*p[1];
 
 
 
 if (f[i,j]< (f[i-1,j-p[0]-p[2]]+p[0]*v[0]+p[2]*v[2]))and(j>=p[0]+p[2]) then
 
 f[i,j]:=f[i-1,j-p[0]-p[2]]+p[0]*v[0]+p[2]*v[2];
 
 
 
 if (f[i,j]< (f[i-1,j-p[0]-p[1]-p[2]]+p[0]*v[0]+v[1]*p[1]+p[2]*v[2]))and(j>=p[0]+p[1]+p[2]) then
 
 f[i,j]:=f[i-1,j-p[0]-p[1]-p[2]]+p[0]*v[0]+v[1]*p[1]+p[2]*v[2];
 
 writeln('f[',i,',',j,']:',f[i,j]);
 
 end;
 
 writeln(f[k,n]);
 
 close(input);close(output);
 
 end.
 
 
 |