This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// dp[i][mask] = true if the first i-th person is considered and the on bit in mask is taken
/*
* transition dp[i][mask] |= dp[i-1][mask|val];
*/
// base case dp[0][0] = true;
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
bool dp[30][300000];
vector<int>val[30];
void solve()
{
int n, m; cin >> n >> m;
vector<int>a(n + 1), b(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 sum = 0;
for (int i = 0; i < m; i++) {
if (mask & (1 << i))sum += b[i + 1];
}
for (int i = 1; i <= n; i++) {
if (a[i] == sum) val[i].push_back(mask);
}
}
dp[0][0] = true;
for (int i = 1; i <= n; i++) {
for (int mask = 0; mask < (1 << m); mask++) {
for (auto u : val[i]) {
if((mask & u) == u)dp[i][mask] |= dp[i - 1][mask ^ u];
}
}
}
for (int mask = 0; mask < (1 << m); mask++) {
if (dp[n][mask]) { cout << "YES" << "\n"; return; }
}
cout << "NO" << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int 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... |