Submission #537934

#TimeUsernameProblemLanguageResultExecution timeMemory
537934nicolexxuuBank (IZhO14_bank)C++14
100 / 100
92 ms8508 KiB
#include <bits/stdc++.h>
#define MAXN 21

using namespace std;
int N, M;
int a[MAXN], b[MAXN];
int num_people[1 << MAXN], leftover[1 << MAXN];

int main() {
	cin >> N >> M;
	for(int i = 0; i < N; i++) cin >> a[i];
	for(int i = 0; i < M; i++) cin >> b[i];
	
	bool ok = false;
	for(int mask = 0; mask < (1 << M); mask++) {
		int p = num_people[mask];
//		cout << "mask: " << mask << " p: " << p << "leftover: " << leftover[mask] << endl;
		ok |= (p == N);
		if(mask > 0 && (p == 0 && leftover[mask] == 0)) continue;
		for(int note = 0; note < M; note++) {
			if((mask & (1 << note)) == 0) {
				int new_mask = mask + (1 << note);
				if(leftover[mask] + b[note] < a[p]) {
					num_people[new_mask] = p;
					leftover[new_mask] = leftover[mask] + b[note];
//					cout << "new mask: " << new_mask << " p: " << p << "leftover: " << leftover[new_mask] << endl;
				} else if(leftover[mask] + b[note] == a[p]) {
					num_people[new_mask] = p+1;
					leftover[new_mask] = 0;
//					cout << "new mask: " << new_mask << " p: " << p+1 << endl;
				}
			}
		}
	}
	
	if(ok) cout << "YES" << endl;
	else cout << "NO" << endl;
}

// alternate solution ..
//#include <bits/stdc++.h>
//#define MAXN 21
//using namespace std;
//
//int a[MAXN], b[MAXN];
//bool possible[MAXN][1 << MAXN];
//vector<int> combos[2005];
//int N, M;
//
//int main() {
//	ios_base::sync_with_stdio(0); 
//	cin.tie(NULL);
//	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 mask = 0; mask < (1 << M); mask++) {
//		int sum = 0;
//		for(int i = 0; i < M; i++)
//			if(mask & (1 << i)) sum += b[i];
//		if(sum <= 1000) combos[sum].push_back(mask);
//	}
//	
//	possible[0][0] = true;
//	for(int i = 0; i < N; i++) {
//		for(int mask = 0; mask < (1 << M); mask++) {
//			if(!possible[i][mask]) continue; // how much does this reduce complexity by?? lol
//			for(int combo : combos[a[i]])
//				if((combo & mask) == 0) possible[i+1][combo + mask] |= possible[i][mask];
//		}
//	}
//	
//	bool res = false;
//	for(int i = 0; i < (1 << M); i++) res |= possible[N][i];
//	if(res) cout << "YES\n";
//	else cout << "NO\n";
//}


//#include <iostream>
//#include <vector>
//
//using std::cout;
//using std::endl;
//using std::vector;
//
//int main() {
//	int people_num;
//	int note_num;
//	std::cin >> people_num >> note_num;
//	vector<int> people(people_num);
//	vector<int> banknotes(note_num);
//	for (int& p : people) {
//		std::cin >> p;
//	}
//	for (int& b : banknotes) {
//		std::cin >> b;
//	}
//
//	vector<int> leftover(1 << note_num, -1);
//	vector<int> people_covered(1 << note_num, -1);
//	leftover[0] = 0;
//	people_covered[0] = 0;
//	for (int s = 0; s < (1 << note_num); s++) {
//		for (int last = 0; last < note_num; last++) {
//			if ((s & (1 << last)) == 0) {
//				continue;
//			}
//			int prev = s & ~(1 << last);
//			if (people_covered[prev] == -1) {
//				continue;
//			}
//
//			int new_amt = leftover[prev] + banknotes[last];
//			// the salary of the current person we're going to try to pay
//			int curr_target = people[people_covered[prev]];
//			// if it's still not enough, just increment the leftover pile
//			if (new_amt < curr_target) {
//				people_covered[s] = people_covered[prev];
//				leftover[s] = new_amt;
//			}
//			/*
//			 * or it's exactly right, in which case reset the leftover count
//			 * and increment the covered amount
//			 */
//			else if (new_amt == curr_target) {
//				people_covered[s] = people_covered[prev] + 1;
//				leftover[s] = 0;
//			}
//		}
//		// we've covered all the people!
//		if (people_covered[s] == people_num) {
//			cout << "YES" << endl;
//			return 0;
//		}
//	}
//	cout << "NO" << endl;
//}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...