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;
const int C = 1010;
vector<int> good[C][21];
bool dp[21][(1 << 21)];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n, m;
cin >> n >> m;
int a[n], b[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++) {
int sum = 0, bits = 0;
for (int j = 0; j < m; j++)
sum += ((mask >> j) & 1) * b[j], bits += ((mask >> j) & 1);
if (sum < C)
good[sum][bits].emplace_back(mask);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << m); j++)
dp[i][j] = false;
}
for (int bit = 0; bit <= m; bit++)
for (int mask: good[a[0]][bit])
dp[0][mask] = true;
for (int j = 0; j < (1 << m); j++)
for (int bit = 0; bit < m; bit++) {
if (((j >> bit) & 1) == 1 && dp[0][j - (1 << bit)]) {
dp[0][j] = true;
break;
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < (1 << m); j++) {
for (int bit = 0; bit < m; bit++) {
if (((j >> bit) & 1) == 1 && dp[i][j - (1 << bit)]) {
dp[i][j] = true;
break;
}
}
int bits = 0;
for (int bit = 0; bit < m; bit++) bits += ((j >> bit) & 1);
if (!dp[i][j]) {
for (int bit = bits; bit >= 0; bit--) {
for (int mask: good[a[i]][bit]) {
if ((j & mask) == mask && dp[i - 1][j - mask]) {
dp[i][j] = true;
break;
}
}
}
}
}
}
cout << (dp[n - 1][(1 << m) - 1] ? "YES" : "NO");
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... |