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 <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <string.h>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int N = 21, MaxVal = 20001;
int n, m, a[N], b[N];
bool dp[N][(1 << N)];
vector<vector<int>> all[MaxVal];
vector<int> go[(1 << N)];
// https://oj.uz/problem/view/IZhO14_bank
int main() {
ios::sync_with_stdio(0);
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];
for (int i=1; i<(1 << m); i++) {
vector<int> cur;
int sum=0;
for (int j=0; j<m; j++) {
if (((i >> j)&1)) {
sum += b[j];
cur.pb(j);
go[i].pb(j);
}
}
// cout << i << " " << sum << "\n";
all[sum].pb(cur);
}
bool work=false;
for (int i=0; i<n; i++) {
work = false;
for (auto one : all[a[i]]) {
int sum=0;
vector<int> left;
int p=0;
for (int j=0; j<(int)one.size(); j++) {
while (p < one[j]) {
left.pb(p++);
}
p++;
sum += (1 << one[j]);
}
while (p < m ) {
left.pb(p++);
}
if (i == 0) {
dp[0][sum] = true;
work=true;
}
else {
for (int j=1; j<(1 << left.size()); j++) {
int cursum = 0;
for (auto k : go[j]) {
cursum += (1 << left[k]);
}
// for (int k=0; k<(int)left.size(); k++) {
// if ((j >> k) & 1) {
// cursum += (1 << left[k]);
// }
// }
if (!dp[i][sum + cursum]) {
dp[i][sum + cursum] = dp[i-1][cursum];
}
if (dp[i][sum + cursum]) work = true;
}
}
}
}
// for (int i=0; i<n; i++) {
// for (int j=0; j<(1 << m); j++) {
// cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
// for (int j=(1 << m); j>=1; j--) {
// if (dp[n-1][j]) {
// cout << "YES";
// return 0;
// }
// }
if (work) cout << "YES";
else cout << "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... |