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 N = 21, M = 1 << N, K = 1010;
int dp[N][M];
int n, m, mx, a[N], b[N];
vector<int> s[K];
bool check(int i, int msk) {
for (int j = 0; j < N; j++) {
if (i >> j & 1 && !(msk >> j & 1)) {
return false;
}
}
return true;
}
int g(int i, int msk) {
for (int j = 0; j < N; j++) {
if (i >> j & 1) {
msk = msk & ~(1 << j);
}
}
return msk;
}
bool f(int u, int msk) {
if (dp[u][msk] == 1 || u == 0) {
return true;
} if (dp[u][msk] == 0) {
return false;
}
for (int i : s[a[u]]) {
if (check(i, msk) && f(u - 1, g(i, msk))) {
dp[u][msk] = 1;
return true;
}
}
dp[u][msk] = 0;
return false;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
}
for (int j = 1; j <= m; j++) {
cin >> b[j];
}
for (int i = 0; i < 1 << m; i++) {
int sum = 0;
for (int j = 0; j < N; j++) {
if (i >> j & 1) {
sum += b[j + 1];
}
}
if (sum > mx) continue;
s[sum].push_back(i);
}
cout << (f(n, (1 << m) - 1) ? "YES\n" : "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... |