w:array[0..1000] of longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure dfs(k,tot:longint);
var i:longint;
begin
if tot>m then exit;
if k>n then
begin
if tot>sum then sum:=tot;
exit;
end;
dfs(k+1,tot+w[k]);
dfs(k+1,tot);
end;
begin
readln(m,n);
for i:=1 to n do read(w[i]);
sum:=0;
dfs(1,0);
writeln(sum);
end.
var
a:array[1..100000]of longint;
i,max,n,c:longint;
procedure try(sum,n:longint);
begin
if n=0 then begin
if sum>max then max:=sum;
exit;
end;
try(sum,n-1);
if sum+a[n]<=c then try(sum+a[n],n-1);
end;
begin
read(c,n);
for i:=1 to n do read(a[i]);
max:=0;
try(0,n);
write(max);
end.
状态: Accepted
测评机: Xeond[6]
得分: 100分 [我要评价一下题目~]
提交日期: 2011-12-20 15:16:00
有效耗时: 593毫秒
测试结果1: 通过本测试点|有效耗时171ms
测试结果2: 通过本测试点|有效耗时47ms
测试结果3: 通过本测试点|有效耗时47ms
测试结果4: 通过本测试点|有效耗时47ms
测试结果5: 通过本测试点|有效耗时47ms
测试结果6: 通过本测试点|有效耗时47ms
测试结果7: 通过本测试点|有效耗时47ms
测试结果8: 通过本测试点|有效耗时47ms
测试结果9: 通过本测试点|有效耗时46ms
测试结果10: 通过本测试点|有效耗时47ms
提交代码: view sourceprint?01.program dp;
02.var
03.
a:array[1..100000]of longint;
04.
i,max,n,c:longint;
05.procedure try(sum,n:longint);
06.begin
07.
if n=0 then begin
08.
if sum>max then max:=sum;
09.
exit;
10.
end;
11.
try(sum,n-1);
12.
if sum+a[n]<=c then try(sum+a[n],n-1);
13.end;
14.begin
15.
read(c,n);
16.
for i:=1 to n do read(a[i]);
17.
max:=0;
18.
try(0,n);
19.
write(max);
20.end