比赛 20160420s 评测结果 AAAAAAAAAA
题目名称 买汽水 最终得分 100
用户昵称 铁策 运行时间 0.245 s
代码语言 C++ 内存使用 7.46 MiB
提交时间 2016-04-20 09:30:14
显示代码纯文本
#include <cstdio>
#include <cctype>
#include <algorithm>
#define Satoshi 1048600
using namespace std;
int a[Satoshi], b[Satoshi], ca, cb;
int n, m, s1, s2;
int p[30], q[30];
void dfs1(int x, int now)
{
	if (x == s1)
	{
		a[ca++] = now;
	}
	else
	{
		dfs1(x + 1, now + p[x]);
		dfs1(x + 1, now);
	}
}
void dfs2(int x, int now)
{
	if (now > m)
		return;
	if (x == s2)
	{
		b[cb++] = now;
	}
	else
	{
		dfs2(x + 1, now + q[x]);
		dfs2(x + 1, now);
	}
}
int main()
{
	freopen("drink.in", "r", stdin);
	freopen("drink.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n / 2; i++)
	{
		scanf("%d", &p[i]);
	}
	for (int i = n / 2; i < n; i++)
	{
		scanf("%d", &q[i - n / 2]);
	}
	s1 = n / 2; 
	s2 = n - s1;
	dfs1(0, 0);
	dfs2(0, 0);
	sort(a, a + ca);
	sort(b, b + cb);
	int l = 0, r = cb - 1;
	int ans = 0;
	for (; l < ca; l++)
	{
		while (a[l] + b[r] > m)
			r--;
		if (a[l] + b[r] > ans)
			ans = a[l] + b[r];
	}
	printf("%d\n", ans);
}