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>
using namespace std;
int n, m, a[22], b[22], sum[(1 << 20) + 3];
int dp[22][(1 << 20) + 2];
int solve(int j, int mask) {
if (j == -1) {
return 1;
}
if (dp[j][mask] != -1) {
return dp[j][mask];
}
dp[j][mask] = 0;
for (int submask = mask; submask > 0; submask = (submask - 1)&mask) {
if (sum[submask] == a[j] && solve(j - 1, mask^submask) == 1) {
dp[j][mask] = 1;
}
}
return dp[j][mask];
}
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];
}
for (int mask = 0; mask < (1 << m); mask++) {
sum[mask] = 0;
for (int i = 0; i < m; i++) {
if (mask & (1 << i)) {
sum[mask] += b[i];
}
}
}
memset(dp, -1, sizeof(dp));
cout << (solve(n - 1, (1 << m) - 1) == 1 ? "YES" : "NO") << endl;
}
# | 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... |