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 <algorithm>
using namespace std;
const int maxN = 21;
int N, M, a[maxN], b[maxN], dp[maxN][1 << maxN], has[maxN], head, cnt;
bool dfs(int now, int cursum, int tgt, int &curmask, int &ind){
//cout << "running dfs(" << now << ", " <<cursum << ", " << tgt << ", " << curmask << ", " << ind << ")\n";
if(cursum == tgt){
return dp[ind - 1][curmask];
}
if(now >= head) return false;
if(tgt - cursum < b[has[now]]) return false;
curmask ^= (1 << has[now]);
if(dfs(now + 1, cursum + b[has[now]], tgt, curmask, ind)){
curmask ^= (1 << has[now]);
return true;
}
curmask ^= (1 << has[now]);
if(dfs(now + 1, cursum, tgt, curmask, ind)){
return true;
}
return false;
}
int main(){
cin >> N >> M;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 0; i < M; i++) cin >> b[i];
sort(b, b + M);
sort(a, a + N);
for(int m = 0; m < (1 << M); m++) dp[0][m] = 1;
for(int i = 1; i <= N; i++){
bool f = 0;
for(int m = (1 << M) - 1; m >= 0; m--){
//cout << "Running " << i << " " << m << endl;
for(int j = 0; j < M; j++) if(!(((m >> j) & 1)) && (!dp[i][m ^ (1 << j)])) {
dp[i][m] = 0;
break;
}
head = 0;
cnt = 0;
for(int j = 0; j < M; j++) if(((m >> j) & 1)) has[head++] = j;
dp[i][m] = dfs(0, 0, a[i], m, i);
//cout << "done" << endl;
f |= dp[i][m];
}
if(!f){
cout << "NO\n";
return 0;
}
}
for(int m = 0; m < (1 << M); m++) if(dp[N][m]){
cout << "YES\n";
return 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... |