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("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
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];
for (int i = 0; i < n;){
int j = find(b.begin(), b.end(), a[i]) - b.begin();
if (j != m){
a.erase(a.begin() + i);
b.erase(b.begin() + j);
n--;
m--;
} else {
i++;
}
}
if (m < n * 2){
cout << "NO" << endl;
return 0;
}
vector<pair<int, int>> dp(1 << m, make_pair(-1, -1));
dp[0] = make_pair(0, 0);
for (int mask = 0; mask < (1 << m); mask++){
if (dp[mask] == make_pair(-1, -1)) continue;
if (dp[mask] == make_pair(n, 0)){
cout << "YES" << endl;
return 0;
}
int j = dp[mask].first, has = dp[mask].second;
if (j >= n) continue;
for (int i = 0; i < m; i++){
if (mask & (1 << i)) continue;
if (has + b[i] < a[j]){
dp[mask | (1 << i)] = max(dp[mask | (1 << i)], make_pair(j, has + b[i]));
} else if (has + b[i] == a[j]){
dp[mask | (1 << i)] = max(dp[mask | (1 << i)], make_pair(j + 1, 0));
}
}
}
cout << "NO" << endl;
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... |