比赛 树立信心的模拟赛 评测结果 AWTTTTTTTT
题目名称 凯伦和超市 最终得分 10
用户昵称 胡嘉兴 运行时间 16.009 s
代码语言 C++ 内存使用 0.36 MiB
提交时间 2017-09-01 21:34:06
显示代码纯文本
#include <cstdio>

using namespace std;

#define N 5050

int c[N], d[N], x[N], book[N], n, ans = 0, maxans = 0;

int max(int a, int b)
{
	if(a > b)
	{
		return a;
	}
	return b;
}
void dfs(int b, int must)
{
	if(must != 0)
	{
		if(b >= c[must] - d[must]&&book[must])
		{
			ans++;
			book[must] = 0;
			dfs(b - c[must] + d[must], x[must]);
			ans--;
			book[must] = 1;
		}
		return;
	}
	for(int i = n; i >= 1; i--)
	{
		if(b >= c[i] - d[i]&&book[i])
		{
			if(b >= c[i])
			{
				ans++;
				book[i] = 0;
				maxans = max(maxans, ans);
				dfs(b - c[i], 0);
				ans--;
				book[i] = 1;
			}
			ans++;
			book[i] = 0;
			maxans = max(maxans, ans);
			dfs(b - c[i] + d[i], x[i]);
			ans--;
			book[i] = 1;
		}
	}
	return;
}
int main()
{	
	int b;
	freopen("market.in", "r", stdin);
	freopen("market.out", "w", stdout);
	
	scanf("%d%d", &n, &b);
	
	scanf("%d %d", &c[1], &d[1]);
	
	x[1] = 0;
	for(int i = 1; i <= n; i++)
	{
		book[i] = 1;
	}
	for(int i = 2; i <= n; i++)
	{
		
		scanf("%d%d%d", &c[i], &d[i], &x[i]);
		
	}
	dfs(b, 0);
	
	printf("%d\n", maxans + 1);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}