This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/IZhO14_bank
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> A(n);
for (int& a : A) {
cin >> a;
}
vector<int> B(m);
for (int& b : B) {
cin >> b;
}
vector<int> dp_prefix(1 << m);
vector<int> dp_remaining(1 << m);
for (int bitmask = 1; bitmask < (1 << m); bitmask++) {
for (int last = 0; last < m; last++) {
if (!(bitmask & (1 << last))) {
continue;
}
int previous = bitmask ^ (1 << last);
int sum = dp_remaining[previous] + B[last];
if (sum == A[dp_prefix[previous]]) {
if (dp_prefix[previous] + 1 > dp_prefix[bitmask]) {
dp_prefix[bitmask] = dp_prefix[previous] + 1;
dp_remaining[bitmask] = 0;
}
}
else {
if (dp_prefix[previous] >= dp_prefix[bitmask]) {
dp_prefix[bitmask] = dp_prefix[previous];
dp_remaining[bitmask] = sum;
}
}
}
if (dp_prefix[bitmask] == n) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
# | 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... |