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>
#define N 20
using namespace std;
int a[N];
int b[N];
bool ok[(1<<N)];
int n,m;
int dp[N+1][(1<<N)];
int sum[(1<<N)];
bool f(int x,int mask){
if(dp[x][mask] != -1)
return dp[x][mask];
dp[x][mask] = 0;
for(int mask2 = mask;mask2;mask2 = (mask2-1) & mask){
if(sum[mask2] == a[x-1]){
dp[x][mask] |= f(x-1,mask ^ mask2);
}
if(dp[x][mask])
return 1;
}
return 0;
}
void solve(){
cin >> n >> m;
for(int i = 0;i<n;i++){
cin >> a[i];
}
for(int i = 0;i<m;i++){
cin >> b[i];
}
while(n && m){
bool ok = 0;
for(int i = 0;i<n && !ok;i++){
for(int j = 0;j<m && !ok;j++){
if(a[i] == b[j]){
ok = 1;
n--,m--;
for(int c = i;c<n;c++){
a[c] = a[c+1];
}
for(int c = j;c<m;c++){
b[c] = b[c+1];
}
}
}
}
if(!ok)break;
}
if(n*2 > m){
cout << "NO" << endl;
return;
}
for(int i = 0;i<(1<<m);i++){
for(int j = 0;j<m;j++){
if((1<<j) & i){
sum[i] += b[j];
}
}
}
for(int j = 0;j<(1<<N);j++){
dp[0][j] = 1;
}
for(int i = 1;i<=N;i++){
for(int j = 0;j<(1<<N);j++){
dp[i][j] = -1;
}
}
if(f(n,(1<<m)-1)){
cout << "YES" << endl;
}
else cout << "NO" << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds.";
#endif
}
# | 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... |