Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <map>
- #include <cstring>
- #include <queue>
- #include <algorithm>
- //#include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1005 * 1005;
- vector<int> masks[maxn];
- int salary[maxn];
- int notes[maxn];
- int dp[20][1 << 20];
- int rec(int at, int bitmask) {
- if(at == -1) {
- return 1;
- }
- if(dp[at][bitmask] != -1) {
- return dp[at][bitmask];
- }
- int result = 0;
- for(int i = 0; i < masks[salary[at]].size(); i++) {
- int tmp_mask = masks[salary[at]][i];
- if((tmp_mask & bitmask) == tmp_mask) {
- result |= rec(at - 1, bitmask ^ tmp_mask);
- }
- }
- return dp[at][bitmask] = result;
- }
- int main() {
- ios::sync_with_stdio(0);
- int n, m;
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- cin >> salary[i];
- }
- for(int i = 0; i < m; i++) {
- cin >> notes[i];
- }
- for(int bitmask = 0; bitmask < (1 << m); bitmask++) {
- int sum = 0;
- for(int i = 0; i < m; i++) {
- if(bitmask & (1 << i)) {
- sum += notes[i];
- }
- }
- masks[sum].push_back(bitmask);
- }
- memset(dp, -1, sizeof dp);
- cout << (rec(n - 1, (1 << m) - 1) == 1 ? "YES" : "NO") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement