背包DP
代码如下:
1
var
i,j,n,t:longint;fi,ti:
array
[
1
..
100
]
of
longint;
2 a: array [ 0 .. 1000 ] of longint;
3 function max(a,b:longint):longint;
4 begin
5 if a > b then max: = a else max: = b;
6 end ;
7 begin
8 readln(n);
9 readln(t);
10 fillchar(a,sizeof(a), 0 );
11 for i: = 1 to n do readln(fi[i],ti[i]);
12 for i: = 1 to n do
13 for j: = t downto ti[i] do
14 begin
15 a[j]: = max(a[j - ti[i]] + fi[i],a[j]);
16 end ;
17 writeln(a[t]);
18 end .
2 a: array [ 0 .. 1000 ] of longint;
3 function max(a,b:longint):longint;
4 begin
5 if a > b then max: = a else max: = b;
6 end ;
7 begin
8 readln(n);
9 readln(t);
10 fillchar(a,sizeof(a), 0 );
11 for i: = 1 to n do readln(fi[i],ti[i]);
12 for i: = 1 to n do
13 for j: = t downto ti[i] do
14 begin
15 a[j]: = max(a[j - ti[i]] + fi[i],a[j]);
16 end ;
17 writeln(a[t]);
18 end .