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<int> all[N];
// https://oj.uz/problem/view/IZhO14_bank
// TLE
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++) {
int sum=0;
for (int j=0; j<m; j++) {
if (((i >> j)&1)) {
sum += b[j];
}
}
for (int j=0; j<n; j++) {
if (a[j] == sum) {
all[j].pb(i);
}
}
// cout << i << " " << sum << "\n";
}
bool work=false;
for (int i=0; i<n; i++) {
work = false;
for (auto one : all[i]) {
int sum = one;
if (i == 0) {
dp[0][sum] = true;
work=true;
}
else {
int complement = ((1 << m) - 1) ^ sum;
for (int j = complement; j>0; j = (j-1) & complement) {
int cursum = j;
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... |