#include <bits/stdc++.h>
/// kitsune
using namespace std;
#define fi first
#define se second
#define mp make_pair
//#define int long long
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = (int)(l); i <= (int)(r); i++)
#define per(i, r, l) for (int i = (int)(r); i >= (int)(l); i--)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename _Tp> bool minimize(_Tp& __a, const _Tp& __b) { if (__a > __b) { __a = __b; return true; } return false; }
template<typename _Tp> bool maximize(_Tp& __a, const _Tp& __b) { if (__a < __b) { __a = __b; return true; } return false; }
const int siz = 1e5 + 2;
const int SIZ = 1e6 + 2;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const ll MAXX = 1e18;
const string file = "1";
int n, m;
int a[20], b[20];
int s[1 << 20];
bool f[20][1 << 20]; bool g[20][1 << 20];
bool dp(int pos, int mask) {
if (pos == n) {
return true;
}
bool& res = f[pos][mask];
bool& cal = g[pos][mask];
if (cal) {
return res;
}
res = false;
rep (i, 0, m - 1) {
if (mask & (1 << i)) {
continue;
}
int new_mask = mask ^ (1 << i);
int sum = s[new_mask];
if (sum <= a[pos]) {
res |= dp(pos + (sum == a[pos]), new_mask);
}
}
cal = true;
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// if (fopen((file + ".inp").c_str(), "r")) {
// freopen((file + ".inp").c_str(), "r", stdin);
// freopen((file + ".out").c_str(), "w", stdout);
// }
cin >> n >> m;
rep (i, 0, n - 1) {
cin >> a[i];
a[i] += a[i - 1];
}
rep (i, 0, m - 1) {
cin >> b[i];
}
rep (mask, 0, (1 << m) - 1) {
rep (i, 0, m - 1) {
if (mask & (1 << i)) {
s[mask] += b[i];
}
}
}
cout << (dp(0, 0) ? "YES" : "NO") << "\n";
// cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
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... |