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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canp(int n, int m, vector<int> a, vector<int> b) {
sort(a.rbegin(), a.rend());
sort(b.rbegin(), b.rend());
for (int i = 0; i < n; i++) {
int ca = a[i];
vector<bool> dp(ca + 1, false);
dp[0] = true;
for (int ba = 0; ba < m; ba++) {
for (int j = ca; j >= b[ba]; j--) {
if (dp[j - b[ba]]) {
dp[j] = true;
}
}
}
auto it = upper_bound(b.begin(), b.end(), ca);
if (it != b.begin() && *(--it) == ca) {
b.erase(it);
continue;
}
if (!dp[ca]) {
return false;
}
int ra = ca;
for (int ba = m - 1; ba >= 0 && ra > 0; ba--) {
if (ra >= b[ba] && dp[ra - b[ba]]) {
ra -= b[ba];
b[ba] = -1;
}
}
b.erase(remove(b.begin(), b.end(), -1), b.end());
m = b.size();
}
return true;
}
int main() {
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];
}
if (canp(n, m, a, b)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
# | 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... |