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>
/*
author: aykhn
*/
using namespace std;
typedef long long ll;
#define OPT ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pss pair<long,long>
#define all(v) v.begin(), v.end()
#define mpr make_pair
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount
#define print(v) for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout<<endl;
int n, m;
vector<int> a, b;
vector<vector<short>> dp(20, vector<short> ((1 << 20), -1));
int f(int i, int mask, int s)
{
if (i == n) return true;
if (s > a[i]) return false;
if (s == a[i]) return f(i + 1, mask, 0);
if (!bpc(mask)) return false;
if (dp[i][mask] != -1) return dp[i][mask];
bool res = 0;
for (int j = 0; j < m; j++)
{
if (mask & (1 << j)) res |= f(i, mask ^ (1 << j), s + b[j]);
}
return dp[i][mask] = res;
}
int main()
{
OPT;
cin >> n >> m;
a.assign(n, 0);
b.assign(m, 0);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
cout << (f(0, (1 << m) - 1, 0) ? "YES" : "NO") << endl;;
}
# | 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... |