Submission #1085136

#TimeUsernameProblemLanguageResultExecution timeMemory
1085136pacmanBank (IZhO14_bank)C++14
100 / 100
57 ms16984 KiB
#include <bits/stdc++.h>
#define int long long int

using namespace std;

const int maxn = 1 << 21, inf = 1e18;

int n, m, sallary[21],money[21], cover[maxn], left_money[maxn];

int32_t main(){
	
//	freopen("bank.in","r",stdin);
//	freopen("bank.out","w",stdout);
	
	ios::sync_with_stdio(0) ,cout.tie(0) ,cin.tie(0);
	
	cin >> n >> m;
	for(int i = 0 ;i < n; i++){
		cin >> sallary[i];
	}
	for(int i = 0 ;i < m;i++){
		cin >> money[i];
	}
	
	for(int i = 0 ; i <= (1 << m); i++){
		cover[i] = -inf;
		left_money[i] = -inf;
	}
	
	cover[0] = 0;
	left_money[0] = 0;
	for(int i = 0 ;i < (1 << m); i++){
		int mask = i;
		
		while(mask > 0){
			int j = __builtin_ctz(mask);
			int ghabli = i ^ (1 << j);
			
			if(cover[ghabli] == -inf){
				mask ^= (1 << j);
				continue;
			}
			int now_money = left_money[ghabli] + money[j];
			int want_money = sallary[cover[ghabli]];
			
			if(now_money < want_money){
				cover[i] = cover[ghabli];
				left_money[i] = now_money;
			}
			else if(now_money == want_money){
				cover[i] = cover[ghabli] + 1;
				left_money[i] = 0;
			}
			
			mask ^= (1 << j);
		}
		if(cover[i] == n){
			cout << "YES" << endl;
			return 0;
		}
	}
	
	cout << "NO" << endl;
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...