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 ll long long
#define pii pair<int, int>
#define piii pair<pii, int>
#define pllll pair<ll, ll>
#define pb push_back
#define all(v) v.begin(), v.end()
#define size(v) v.size()
#define INF 2e18
#define f first
#define s second
using namespace std;
int n, m;
vector<int>costMask[30005];
int dp[2000000][25];
vector<int>a, b;
int Bank(int mask, int i){
if(i == 0) return 1;
if(dp[mask][i] != -1) return dp[mask][i];
for(int Submask : costMask[a[i]]){
if((mask & Submask) == Submask){
if(Bank(mask ^ Submask, i - 1)){
return dp[mask][i] = 1;
}
}
}
return dp[mask][i] = 0;
}
void solve(){
cin >> n >> m;
a.resize(n + 1), b.resize(m + 1);
for(int i = 1; i <= n; ++i){
cin >> a[i];
}for(int i = 1; i <= m; ++i){
cin >> b[i];
}
for(int mask = 0; mask < (1 << m); ++mask){
int Total = 0;
for(int i = 0; i < m; ++i){
if(mask & (1 << i)){
Total += b[i + 1];
}
}
costMask[Total].pb(mask);
}
for(int mask = 0; mask < (1 << m); mask++){
for(int i = 1; i <= n; ++i){
dp[mask][i] = -1;
}
}
if(Bank((1 << m) - 1, n)) cout << "YES\n";
else cout << "NO\n";
}
int main(){
int t;
t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# | 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... |