This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
bool pay(int n, int m, vector<int>& a, vector<int>& b) {
if (m < n) return false;
vector<bool> ans(n, false);
vector<vector<int>> dp(n, vector<int>(1<<m, -1));
queue<pair<int, pair<int, int>>> q;
q.push(mp(0, mp(0, a[0])));
while (!q.empty()) {
int mask = q.front().first;
int index = q.front().second.first;
int sum = q.front().second.second;
q.pop();
if(dp[index][mask] != -1) continue;
for (int i = 0; i < m; ++i) {
if (!(mask & (1<<i))) {
if (b[i] < sum) {
q.push(mp(mask | (1<<i), mp(index, sum - b[i])));
} else if (b[i] == sum) {
ans[index] = true;
dp[index][mask] = 1;
if (index + 1 < n) q.push(mp(mask | (1<<i), mp(index+1, a[index+1])));
}
}
}
if (dp[index][mask] == -1) dp[index][mask] = 0;
}
return ans[n-1];
}
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int j = 0; j < m; ++j) cin >> b[j];
if (pay(n, m, a, b)) cout << "YES\n";
else cout << "NO\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |