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 maxN = 20;
const int maxA = 1e3 + 20;
int A[maxN];
int B[maxN];
vector<int> masks[maxA];
bool flag[maxN][1 << maxN];
int N, M;
void backtrack(int pos, int cur) {
if (flag[pos][cur]) {
return;
}
flag[pos][cur] = true;
if (pos == N) {
cout << "YES";
exit(0);
}
for (auto mask: masks[A[pos]]) {
if (!(cur & mask)) {
backtrack(pos + 1, cur | 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++) {
int sum = 0;
for (int i = 0; i < M; i++) {
if ((mask >> i) & 1) {
sum += B[i];
}
}
if (sum < maxA) {
masks[sum].push_back(mask);
}
}
backtrack(0, 0);
cout << "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... |