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 <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define sep ' '
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define kill(x) {cout << x << endl;return;}
const int N = 21;
const ll LLINF = 2e18;
const int INF = INT_MAX/2;
int n, m;
int a[N], b[N];
int dp_prev[1<<N], dp[1<<N];
// check MAXN
// diffrence between extract and erase in multi set
void set_io() {
fastIO;
// freopen("guard.in", "r", stdin);
// freopen("guard.out", "w", stdout);
}
void solve() {
cin >> n >> m;
for(int i = 0 ; i < n ; i ++) cin >> a[i];
for(int i = 0 ; i < m ; i ++) cin >> b[i];
dp_prev[0] = 0;
for (int mask = 1 ; mask < 1<<m ; mask++)
dp_prev[mask] = b[int(log2(mask))] + dp_prev[mask^(1<<int(log2(mask)))];
for(int i = 0 ; i < n ; i ++) {
memset(dp, -1, sizeof dp);
for (int mask = 1 ; mask < 1<<m ; mask++) {
for(int j = 0 ; j < m ; j ++) {
if ((mask>>j)&1) {
int x = mask ^ (1<<j);
if (dp[x] != -1) {
dp[mask] = dp[x] + b[j];
j = m;
continue;
}
if (dp_prev[x] >= 0 && dp_prev[x] + b[j] == a[i])
dp[mask] = 0;
}
}
}
swap(dp, dp_prev);
}
if (dp_prev[(1<<m)-1] != -1) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main() {
set_io();
solve();
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... |