제출 #753210

#제출 시각아이디문제언어결과실행 시간메모리
753210SLin25은행 (IZhO14_bank)C++14
100 / 100
147 ms8624 KiB
#include <bits/stdc++.h> #include <iostream> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int people[n]; int banknotes[m]; for (int i = 0; i < n; i++) { cin >> people[i]; } for (int i = 0; i < m; i++) { cin >> banknotes[i]; } int sums[(1<<m)]; sums[0] = 0; int pref[n]; for (int i = 0; i < n; i++) { pref[i] = people[i]; } for (int i = 1; i < n; i++) { pref[i] += pref[i - 1]; } for (int i = 1; i < (1 << m); i++) { int sb = 0; for (int j = 0; j < m; j++) { if (i&(1<<j)) sb++; } if (sb==1) { for (int j = 0; j < m; j++) { if (i&(1<<j)) sums[i] = banknotes[j]; } } else { for (int j = 0; j < m; j++) { if (i&(1<<j)) { sums[i] = sums[(1<<j)] + sums[i^(1<<j)]; break; } } } } int dp[(1<<m)]; int np = 1e9; for (int i = 0; i < (1<<m); i++) dp[i] = np; dp[0] = 0; for (int i = 1; i < (1<<m); i++) { for (int j = 0; j < m; j++) { if (i&(1<<j) && dp[i^(1<<j)] != np && dp[i^(1<<j)] != n) { int targ = pref[dp[i^(1<<j)]] - sums[i^(1<<j)]; if (targ == banknotes[j]) dp[i] = dp[i^(1<<j)]+1; else if (targ > banknotes[j]) dp[i] = dp[i^(1<<j)]; } } } bool pass = false; for (int i = 0; i < (1<<m); i++) { if (dp[i] == n) { pass = true; break; } } cout << (pass?"YES":"NO") << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...