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 MAX = 21;
const int MAXM = 2005;
const int INF = 0x3f3f3f3f;
int n, m, a[MAX], b[MAX], sums[1 << MAX];
bool dp[MAX][1 << MAX], dp2[MAXM];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
if(n == 1){
dp2[0] = true;
for(int i = 1; i <= n; i++)
for(int j = MAXM - 1; j >= 0; j--)
if(j >= b[i]) dp2[j] |= dp2[j - b[i]];
cout << (dp2[a[1]] ? "YES" : "NO") << '\n';
exit(0);
}
for(int mask = 0; mask < (1 << m); mask++){
int cur = 0;
for(int i = 0; i < m; i++)
if(mask & (1 << i)) cur += b[i];
sums[mask] = cur;
}
dp[0][0] = true;
for(int i = 1; i <= n; i++){
for(int mask = 0; mask < (1 << m); mask++){
for(int sub = mask; sub > 0; sub = (sub - 1) & mask)
dp[i][mask] |= (dp[i - 1][mask ^ sub] && sums[sub] == a[i]);
}
}
bool pos = false;
for(int mask = 0; mask < (1 << m); mask++)
pos |= dp[n][mask];
cout << (pos ? "YES" : "NO") << '\n';
exit(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... |