제출 #1136309

#제출 시각아이디문제언어결과실행 시간메모리
1136309abel2008Bank (IZhO14_bank)C++17
100 / 100
98 ms8620 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n, m, a[N], b[N];
pair<int, int> dp[(1 << N)];
// dp[s] -> 
// 1) max prefix a[0, i) that can be formed using the banknotes in set s
// 2) leftover (fixed given prefix)
// notice we can greedily maximize prefix without considering leftover
// proof is left to reader
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < m; i++) cin >> b[i];
	dp[0] = {0, 0};
	bool pos = false;
	for (int s = 0; s < (1 << m); s++) {
		if (dp[s].first == n) {
			pos = true;
			break;
		}
		for (int j = 0; j < m; j++) {
			if (s & (1 << j)) continue;
			pair<int, int> new_state;
			if (a[dp[s].first] == dp[s].second + b[j]) {
				new_state = {dp[s].first + 1, 0};
			} else new_state = {dp[s].first, dp[s].second + b[j]};
			dp[s | (1 << j)] = max(dp[s | (1 << j)], new_state);
		}
	}
	cout << (pos ? "YES" : "NO");
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...