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 = 25, M = 1 << 20 + 5;
int n, m, a[N], b[N], ps[N], dp[M];
void read() {
cin >> n >> m;;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
cin >> b[i];
}
bool bit(int mask, int id) {
return (1 & (mask >> id));
}
void solve() {
for (int i = 1; i <= n; i++)
ps[i] = ps[i - 1] + a[i];
dp[0] = 0;
for (int mask = 1; mask < (1 << m); mask++) {
int sum = 0, bits = 0;
for (int i = 0; i < m; i++)
if (bit(mask, i)) {
sum += b[i];
bits++;
}
for (int i = 0; i < m; i++)
if (bit(mask, i)) {
int upd = dp[mask - (1 << i)];
upd += (sum == ps[upd + 1]);
//if (mask == 20) cout << i << ' ' << sum << ' ' << upd << ' ' << ps[upd + 1] << endl;
dp[mask] = max(dp[mask], upd);
//if (dp[mask] == ps[n]) cout << "YES\n";
}
}
}
void write() { cout << ((dp[(1 << m) - 1] == n)? "YES\n": "NO\n"); }
int main() {
ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
read(), solve(), write();
}
Compilation message (stderr)
bank.cpp:4:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
4 | const int N = 25, M = 1 << 20 + 5;
| ~~~^~~
# | 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... |