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;
void dfs(int now, int cursum, int tgt, int &curmask, int &ind){
//cout << "running dfs(" << now << ", " <<cursum << ", " << tgt << ", " << curmask << ", " << ind << ")\n";
if(cursum == tgt){
dp[ind][curmask] = 1;
return;
}
if(now >= head) return;
if(tgt - cursum < b[has[now]]) return;
curmask ^= (1 << has[now]);
dfs(now + 1, cursum + b[has[now]], tgt, curmask, ind);
curmask ^= (1 << has[now]);
dfs(now + 1, cursum, tgt, curmask, ind);
}
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);
for(int m = 0; m < (1 << M); m++) dp[0][m] = 1;
for(int i = 1; i <= N; i++){
for(int m = 0; m < (1 << M); m++){
if(!dp[i - 1][m]) continue;
//cout << "Running " << i << " " << m << endl;
head = 0;
for(int j = 0; j < M; j++) if(!((m >> j) & 1)) has[head++] = j;
dfs(0, 0, a[i], m, i);
//cout << "done" << endl;
}
}
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... |