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 = 1 << 21;
int n, m, a[30], b[30], p[30], sum;
pair <int, int> dp[N];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i], sum -= a[i];
for (int i = 0; i < m; i++)
cin >> b[i], sum += b[i];
if (sum < 0)
return cout << "NO\n", 0;
if (sum > 0)
a[n] = sum, n++;
p[0] = a[0];
for (int i = 1; i < n; i++)
p[i] = p[i - 1] + a[i];
cout << '\n';
for (int mask = 0; mask < (1 << m); mask++)
if (dp[mask].first || !mask) {
for (int j = 0; j < m; j++)
if (!((mask >> j) & 1)) {
if (dp[mask].first + b[j] == p[dp[mask].second])
dp[mask ^ (1 << j)] = {dp[mask].first + b[j], dp[mask].second + 1};
else if (dp[mask].first + b[j] < p[dp[mask].second])
dp[mask ^ (1 << j)] = {dp[mask].first + b[j], dp[mask].second};
if (dp[mask ^ (1 << j)].second == n)
return cout << "YES\n", 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... |