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
using namespace std;
vector<vector<int>> dp, cost;
vector<int> a, b;
int n, m;
int solve(int mask, int i){
if(i == n) return 1;
if(dp[mask][i] != -1) return dp[mask][i];
for(auto s : cost[a[i]]){
if((s & mask) == s){
if(solve(mask ^ s, i + 1)){
return dp[mask][i] = 1;
}
}
}
return dp[mask][i] = 0;
}
int main(){
cin>>n>>m;
a = vector<int>(n);
b = vector<int>(m);
dp = vector<vector<int>>((1<<m), vector<int>(n, -1));
for(auto& i : a) cin>>i;
for(auto& i : b) cin>>i;
cost.resize(30005);
for (int mask = 0; mask < (1<<m); mask++)
{
int total = 0;
for (int i = 0; i < m; i++)
{
if(!(mask & (1<<i))) continue;
total += b[i];
}
cost[total].push_back(mask);
}
cout<<(solve((1<<m) - 1, 0) ? "YES" : "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... |