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>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
int n, m;
vector<int> s, v;
int dp[21][1 << 20];
int solve(int p, int mask) {
if (p == n) return dp[p][mask] = 1;
if (mask == (1 << v.size()) - 1) return dp[p][mask] = -1;
if (dp[p][mask] != 0) return dp[p][mask];
vector<int> c;
for (int i = 0; i < m && v[i] <= s[p]; i++) if (!(mask & (1 << i))) c.push_back(i);
dp[p][mask] = -1;
for (int i = 0; i < (1 << c.size()); i++) {
int sum = 0, newMask = mask;
for (int j = 0; j < c.size(); j++) {
if (i & (1 << j)) {
sum += v[c[j]];
newMask |= (1 << c[j]);
if (sum > s[p]) break;
}
}
if (sum == s[p]) {
dp[p][mask] = max(dp[p][mask], solve(p + 1, newMask));
if (dp[p][mask] == 1) return 1;
}
}
return dp[p][mask];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
s.resize(n);
v.resize(m);
int mx = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
mx = max(mx, s[i]);
}
for (int i = 0; i < m; i++) cin >> v[i];
sort(all(s));
sort(all(v));
while (v[v.size() - 1] > mx) v.pop_back();
solve(0, 0);
cout << (dp[0][0] == 1 ? "YES" : "NO");
}
Compilation message (stderr)
bank.cpp: In function 'int solve(int, int)':
bank.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int j = 0; j < c.size(); j++) {
| ~~^~~~~~~~~~
# | 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... |