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>
#pragma GCC target("popcnt")
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define pb push_back
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<int> a(n), b(m);
for(int i=0; i<n; i++) cin >> a[i];
for(int i=0; i<m; i++) cin >> b[i];
if(n==1){
int t = a[0];
int dp[t+1][m+1] = {};
for(int j=0; j<=m; j++) dp[0][j] = 1;
for(int v=1; v<=t; v++){
for(int i=1; i<=m; i++){
dp[v][i] = dp[v][i-1];
if(v-b[i-1]>=0) dp[v][i] |= dp[v-b[i-1]][i-1];
}
}
if(dp[t][m]) cout << "YES";
else cout << "NO";
return 0;
}
vector<int> arr[1001];
for(int s=0; s<(1<<m); s++){
int t = 0;
for(int p=0; p<m; p++){
if(s&(1<<p)) t+=b[p];
}
if(t<=1000) arr[t].pb(s);
}
vector<vector<int>> dp((1<<m), vector<int>(n+1, 0));
for(int s=0; s<(1<<m); s++){
dp[s][0] = 1;
}
for(int i=1; i<=n; i++){
int t = a[i-1];
for(int s=0; s<(1<<m); s++){
for(auto u: arr[t]){
if((s&u)==u){
dp[s][i] |= dp[s^u][i-1];
}
}
}
}
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... |