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 <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
#define task ""
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
const int N = 1e6 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
int n, m;
int a[20], b[20], d[20];
bool dp[20][MASK(20)];
void solve(int test = -1) {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
sort(a, a + n), sort(b, b + m);
for (int mask = 0; mask < MASK(m); mask++) {
int sum = 0;
for (int msk = mask; msk > 0; msk -= msk & -msk) {
int j = __builtin_ctz(msk);
sum += b[j];
}
if (sum == a[0]) dp[0][mask] = 1;
}
for (int i = 1; i < n; i++) {
for (int mask = 0; mask < MASK(m); mask++) {
d[0] = 0;
for (int msk = mask; msk > 0; msk -= msk & -msk) {
int j = __builtin_ctz(msk);
d[++d[0]] = j;
}
for (int msk = 0; msk < MASK(d[0]); msk++) {
int sum = 0;
int msk2 = 0;
for (int msk1 = msk; msk1 > 0; msk1 -= msk1 & - msk1) {
int j = __builtin_ctz(msk1);
sum += b[d[j + 1]];
msk2 += MASK(d[j + 1]);
}
dp[i][mask] |= dp[i - 1][mask ^ msk2] & (sum == a[i]);
}
}
}
for (int mask = 0; mask < MASK(m); mask++) if (dp[n - 1][mask]) return void (cout << "YES");
cout << "NO";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
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... |