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;
int main(){
int n,m; cin >> n>>m;
bool dp[1<<m][n+1];
for(int i=0; i<1<<m; i++)for(int j=0; j<=n; j++) dp[i][j]=false;
int a[n+1];
for(int i=0; i<n; i++) cin >> a[i+1];
int psum[n+1];psum[0]=0; for(int i=1; i<=n; i++) psum[i] = psum[i-1]+a[i];
int bank[m];
for(int i=0; i<m; i++)cin >> bank[i];
for(int i=0; i<m; i++) dp[1<<i][0]=true;
for(int mask =1; mask<1<<m; mask++){
dp[mask][0]=true;
int tot=0;
for(int i=0; i<m; i++) if(mask & (1<<i))tot+=bank[i];
for(int i=1; i<=n; i++) if(tot==psum[i] && dp[mask][i-1])dp[mask][i] =1;
for(int i=0; i<m; i++) if(!(mask & (1<<i))){
for(int j=0; j<=n; j++) dp[mask^(1<<i)][j] = (dp[mask^(1<<i)][j]) || (dp[mask][j]);
}
}
if(dp[(1<<m)-1][n]) cout <<"YES";
else 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... |