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;
int main() {
//freopen("guard.in", "r", stdin);
//freopen("guard.out", "w", stdout);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<int> b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
vector<int> dp((1 << m));
dp[0] = 1;
for (int i = 0; i < (1 << m); i++) {
if (dp[i] == 1) {
int sum = 0;
for (int j = 0; j < m; j++) {
if (i & (1 << j)) {
sum += b[j];
}
}
int ind = 0;
while (ind < n && sum >= a[ind]) {
sum -= a[ind];
ind++;
}
if (ind == n) {
continue;
}
int r = a[ind] - sum;
for (int j = 0; j < m; j++) {
if (!(i & (1 << j)) && r >= b[j]) {
dp[i | (1 << j)] = 1;
}
}
}
}
int a_s = 0;
for (int i = 0; i < n; i++) {
a_s += a[i];
}
bool con = false;
for (int i = 0; i < (1 << m); i++) {
if (dp[i] == 1) {
int sum = 0;
for (int j = 0; j < m; j++) {
if (i & (1 << j)) {
sum += b[j];
}
}
if (a_s == sum) {
con = true;
}
}
}
if (con) {
cout << "YES\n";
} else {
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... |