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;
pair<int, int> better(pair<int, int> p1, pair<int, int> p2) {
if (p1.first != p2.first) {
return p1.first > p2.first ? p1 : p2;
}
return p1.second >= p2.second ? p1 : p2;
}
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<pair<int, int>> dp(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);
auto [employee, remaining] = dp[previous];
int sum = remaining + B[last];
// Matches the current employee's salary, can go to the next one
pair<int, int> state;
if (sum == A[employee]) {
state = {employee + 1, 0};
}
// Push this banknote onto the leftover amount
else {
state = {employee, sum};
}
dp[bitmask] = better(dp[bitmask], state);
}
if (dp[bitmask].first == 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... |