제출 #1102231

#제출 시각아이디문제언어결과실행 시간메모리
1102231A_M_Namdar은행 (IZhO14_bank)C++17
100 / 100
846 ms14920 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int N = 25, M = (1 << 21);;
int n, m, a[N], b[N], sum[M], dp[2][M];

void input() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) 
		cin >> a[i];
	for (int i = 0; i < m; i++) 
		cin >> b[i];
}

void solve() {
	int p = 0;
	for (int i = 1; i < (1 << m); i++) 
		for (int j = 0; j < m; j++) 
			if ((i >> j) & 1) 
				sum[i] += b[j];
	int s = 0, maxi = 0;
	for (int i = 0; i < n; i++) {
		s += a[i];
		for (int j = 0; j < (1 << m); j++) {
			if (m - __builtin_popcount(j) < n - 1 - i) continue;
			for (int k = 0; k < m; k++, p++) {
				if ((j >> k) & 1) {
					dp[i & 1][j] = max(dp[i & 1][j], dp[i & 1][j - (1 << k)]);
					if (dp[i & 1][j] == i + 1) break;
				}
			}
			dp[i & 1][j] = max(dp[(i + 1) & 1][j], dp[i & 1][j]);
			if (sum[j] == s) dp[i & 1][j]++;

			maxi = max(maxi, dp[i & 1][j]);
			if (maxi >= n) {
				cout << "YES";
				return;
			}
		}
	}
	if (maxi >= n) 
		cout << "YES";
	else 
		cout << "NO";
}

int main() {
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	input();
	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...