# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
169134 | 2019-12-18T14:46:18 Z | LinusTorvaldsFan | Bank (IZhO14_bank) | C++14 | 0 ms | 0 KB |
#include <iostream> #include <vector> using namespace std; const int N = 20; const int M = 14; const int masks = 1 << M; bool dp[masks][N]; int main() { int n, m; cin >> n >> m; vector<int>a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int>b(m); for (int i = 0; i < m; i++) { cin >> b[i]; } if (n == 1) { for (int mask = 0; mask < (1 << m); mask++) { int cur = 0; for (int j = 0; j < m; j++) { if (mask & (1 << j)) { cur += b[j]; } } if (cur == a[0]) { cout << "YES"; return 0; } } cout << "NO"; return 0; } if (n <= 20 && m <= 14) { memset(dp, 0, sizeof(dp)); dp[0][0] = true; for (int mask = 0; mask < (1 << m); mask++) { for (int j = 0; j < n; j++) { if (!dp[mask][j])continue; int lost = (~mask + (1<<m)) % (1 << m); for (int submask = lost;; submask = (submask - 1) & lost) { int sum = 0; for (int t = 0; t < m; t++) { if (submask & (1 << t)) { sum += b[t]; } } if (sum == a[j]) { dp[mask + submask][j + 1] = true; } if (submask == 0)break; } } } for (int mask = 0; mask < (1 << m); mask++) { if (dp[mask][n]) { cout << "YES"; return 0; } } cout << "NO"; } return 0; }