Submission #892591

#TimeUsernameProblemLanguageResultExecution timeMemory
892591vjudge1Bank (IZhO14_bank)C++17
100 / 100
100 ms17284 KiB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define int long long
 
const int dx[] = {0, 1, 0, -1, -1, 1, 1, -1};  
const int dy[] = {1, 0, -1, 0, -1, 1, -1, 1};
const int maxn = 21;
const int inf = 1e18;
const int mod = 1e9+7;
 
using namespace std;

int n, m;
int a[maxn], b[maxn];
pair<int, int> dp[(1ll << maxn)+3];

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];
	for(int mask = 1; mask <= (1 << m); mask++){
		dp[mask] = {-1, -1};
	}
	dp[0] = {0, 0};
	for(int mask = 0; mask < (1 << m); mask++){
		if(dp[mask].F == -1) continue;
		if(dp[mask].F == n){
			cout << "YES\n";
			return;
		}
		for(int i = 0; i < m; i++){
			if((mask & (1 << i)) == 0){
				int next = (mask ^ (1 << i));
				if(dp[mask].S + b[i] == a[dp[mask].F]){
					dp[next] = max(dp[next], {dp[mask].F + 1, 0});
				}else{
					dp[next] = max(dp[next], {dp[mask].F, dp[mask].S + b[i]});
				}
			}
		}
	}
	cout << "NO\n";
}

signed main(){
	speed
	int test = 1;
//	cin >> test;
	while(test--){
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...