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 F first
#define S second
using namespace std;
int n,m;
int a[25],b[25];
pair<int,int> dp[1<<20];
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];
for(int i=0;i<(1<<m);i++)
dp[i]={-1,-1};
dp[0]={0,0};
for(int st=1;st<(1<<m);st++){
for(int j=0;j<m;j++){
if(st&(1<<j)){
int prv=st^(1<<j);
if(dp[prv].F==-1)
continue;
//cout << st << " ";
int total=dp[prv].S+b[j];
if(total==a[dp[prv].F+1]){
if(dp[prv].F+1>dp[st].F){
dp[st]={dp[prv].F+1,0};
}
}
else{
if(dp[prv].F>dp[st].F){
dp[st]={dp[prv].F,total};
}
}
}
}
if(dp[st].F==n){
cout << "YES";
return 0;
}
//cout << dp[st].F << ' ';
}
cout << "NO";
}
# | 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... |