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 a[22], b[22];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
srand(time(NULL));
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
if (n == 1) {
for (int mask = 0; mask < (1 << m); mask++) {
int ans = 0;
for (int i = 0; i < m; i++)
if (mask & (1 << i))
ans += b[i];
if (ans == a[0])
return cout << "YES\n", 0;
}
cout << "NO\n";
return 0;
}
if (m <= 10) {
vector<int> s(1 << m, 0);
for (int mask = 0; mask < (1 << m); mask++) {
for (int i = 0; i < m; i++)
if (mask & (1 << i)) s[mask] += b[i];
}
vector<vector<int>> dp(n + 1, vector<int> (1 << m));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int mask = 0; mask < (1 << m); mask++) {
if (s[mask] != a[i]) continue;
for (int prev = 0; prev < (1 << m); prev++) {
if ((prev & mask) != 0) continue;
if (dp[i][prev] == 1)
dp[i + 1][mask] = 1;
}
}
}
int ok = 0;
for (int mask = 0; mask < (1 << m); mask++)
if (dp[n][mask]) ok = 1;
if (ok == 1)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
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... |