[백준] 2629 양팔 저울

0

백준

목록 보기
40/271
post-thumbnail

백준 2629 양팔 저울

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 30;
const int MAXW = 40000;

//추
int n;
int marble;
int w[MAXN + 1];
int cache[MAXN][MAXW + 1];

int possible(int index, int sum) {
	if (sum == marble) return 1;
	if (index == n) return 0;

	int& ret = cache[index][sum];
	if (ret != -1) return ret;

	ret = possible(index + 1, sum);
	ret = max(ret, possible(index + 1, sum + w[index]));
	ret = max(ret, possible(index + 1, sum - w[index]));
	return ret;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> w[i];
	}

	int k;
	cin >> k;
	while (k--) {
		memset(cache, -1, sizeof(cache));
		cin >> marble;
		
		if (possible(0, 0)) cout << "Y ";
		else cout << "N ";
	}
	return 0;
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글