제출 #1025514

#제출 시각아이디문제언어결과실행 시간메모리
1025514ef10은행 (IZhO14_bank)C++17
100 / 100
74 ms8792 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int N,M;
int A[21];
int B[21];
pair<int,int> dp[1<<20];

void setv(int i, pair<int,int> v) {
	if (dp[i].first < 0) {
		dp[i] = v;
		return;
	}
	if (dp[i].first < v.first) {
		dp[i]=v;
	}
}

int main() {
	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		cin >> A[i];
	}
	sort(A+1,A+N+1);
	for (int i = 0; i < M; i++) {
		cin >> B[i];
	}
	sort(B,B+M);
	for (int i = 0; i < (1<<M); i++) {
			dp[i] = make_pair(-1,-1);
	}
	dp[0] = make_pair(0,0);
	//for (int i = 1; i <= N; i++) {
		for (int j = 0; j < (1 << M); j++) {
			for (int k = 0; k < M; k++) {
				if (!(j&(1<<k))) continue;
				if (dp[j&~(1<<k)].first >= 0 && dp[j&~(1<<k)].first < N) {
					int i = dp[j&~(1<<k)].first;
					int r = dp[j&~(1<<k)].second + B[k];
					if (r==A[i+1]) {
						setv(j,make_pair(i+1,0));
					} else {
						setv(j,make_pair(i,r));
					}
				}
			}
		}
	//}
	for (int i = 0; i < (1<<M); i++) {
		if (dp[i].first == N) {
			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...