#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define f0r(i, n) for (auto i = 0; i < (n); ++i)
#define fnr(i, n, k) for (auto i = (n); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define F first
#define S second
#define ctn(x) cout << x << '\n'
#define forl(a, l) for (auto a : l)
#define ctl(l) for (auto &a : (l)) cout << a << ' '; cout << endl;
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define pq priority_queue
template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using Adj = V<vi>;
using vvi = V<vi>;
const int N = 20;
bool dp[1<<N];
vi make[1005];
int sal[N+5], notes[N+5];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
f0r(i, n) cin >> sal[i];
sort(sal, sal + n);
f0r(i, m) cin >> notes[i];
fnr(s, 1, 1 << m) {
int sm = 0;
f0r(j, m) if (s & (1 << j)) sm += notes[j];
if (binary_search(sal, sal + n, sm)) make[sm].pb(s);
}
if (n == 1) {
if (make[sal[0]].size()) ctn("YES");
else ctn("NO");
return 0;
}
dp[0] = 1;
fnr(i, 1, n+1) {
for (int s = (1 << m)-1; s; --s) {
dp[s] = 0;
forl(x, make[sal[i-1]]) {
if ((s & x) == x) dp[s] |= dp[s ^ x];
if (dp[s]) break;
}
}
dp[0] = 0;
}
fnr(s, 1, 1 << m) {
if (dp[s]) {
cout << s << endl;
ctn("YES");
return 0;
}
}
ctn("NO");
}
# | 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... |