#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 21, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 1 << 20;
int n, m;
int a[N], b[N];
vector <int> adj[N];
int f[N][lim + 1];
void preprocess() {
for (int x = 0; x < n; x++) {
for (int mask = 0; mask < (1 << m); mask++) {
int sum = 0;
for (int j = 0; j < m; j++) {
if ((1 << j) & mask) sum += b[j];
}
if (sum == a[x]) adj[x].push_back(mask);
}
}
}
int dp(int i, int mask) {
if (i >= n) return 1;
if (f[i][mask] != -1) return f[i][mask];
int ans = 0;
for (auto v : adj[i]) {
if (!(mask & v)) {
ans = max(ans, dp(i + 1, mask | v));
if (ans == 1) break;
}
}
return f[i][mask] = ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
preprocess();
memset(f, -1, sizeof(f));
cout << (dp(0, 0) == 1 ? "YES" : "NO");
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... |