This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int M = 20;
const int N = 1 << M;
const int V = 1001;
vector<int> g[V];
vector<int> dp[M];
int s[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int it1 = 0, it2 = 0;
while (it1 < a.size() && it2 < b.size()) {
if (a[it1] == b[it2]) {
a.erase(a.begin() + it1);
b.erase(b.begin() + it2);
continue;
}
if (a[it1] < b[it2]) {
it1++;
} else {
it2++;
}
}
if (a.empty()) {
cout << "YES";
return 0;
}
int topkek = 1 << m;
n = a.size();
for (int mask = 0; mask < topkek; mask++) {
for (int i = 0; i < m; i++) {
if (mask & (1 << i)) {
s[mask] += b[i];
}
}
if (s[mask] < V) {
g[s[mask]].push_back(mask);
}
}
dp[0] = g[a[0]];
for (int i = 0; i + 1 < n; i++) {
for (int mask : dp[i]) {
int cnt = 1 << (m - __builtin_popcount(mask));
if (cnt < g[a[i + 1]].size()) {
for (int kek = mask; kek < topkek; kek = (kek + 1) | mask) {
if (s[kek ^ mask] == a[i + 1]) {
dp[i + 1].push_back(kek);
}
}
} else {
for (int kek : g[a[i + 1]]) {
if (!(kek & mask)) {
dp[i + 1].push_back(mask | kek);
}
}
}
}
}
if (dp[n - 1].empty()) {
cout << "NO";
} else {
cout << "YES";
}
return 0;
}
Compilation message (stderr)
bank.cpp: In function 'int main()':
bank.cpp:34:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (it1 < a.size() && it2 < b.size()) {
~~~~^~~~~~~~~~
bank.cpp:34:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (it1 < a.size() && it2 < b.size()) {
~~~~^~~~~~~~~~
bank.cpp:66:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (cnt < g[a[i + 1]].size()) {
~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |