| 比赛 | 暑假培训B班二测 | 评测结果 | AWTTTTTTTTT | 
    | 题目名称 | 待售干草 | 最终得分 | 9 | 
    | 用户昵称 | 日光。 | 运行时间 | 9.001 s | 
    | 代码语言 | C++ | 内存使用 | 0.30 MiB | 
    | 提交时间 | 2012-07-22 10:41:00 | 
显示代码纯文本
#include<iostream>
#include<fstream>
using namespace std;
int N,M;
int a[5001]={0};
int ans;
int search(int temp,int step)
{
	if(step==M)
	{
		if(ans<temp) ans=temp;
	}
	else
	{
		for(int i=step+1;i<=M;i++)
		{
			if(temp+a[i]<=N)
			{
				search(temp+a[i],step+1);
				break;
			}
			else
			{
				search(temp,step+1);
			}
		}
	}
	return ans;
}
int main()
{
	ifstream fin("hay4sale.in");
	ofstream fout("hay4sale.out");
	fin>>N>>M;
	ans=0;
	for(int i=1;i<=M;i++) fin>>a[i];
	search(0,0);
	fout<<ans<<endl;
	return 0;
}