Submission #375651

#TimeUsernameProblemLanguageResultExecution timeMemory
375651R3KTBank (IZhO14_bank)C++17
71 / 100
1081 ms35820 KiB
#include <iostream> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <deque> #include <queue> #include <stack> #include <set> #include <string> #include <string.h> #include <array> #include <unordered_map> #include <unordered_set> #include <iomanip> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back const int N = 21, MaxVal = 20001; int n, m, a[N], b[N]; bool dp[N][(1 << N)]; int allsum[(1 << N)]; vector<pair<int, int>> all[MaxVal]; // https://oj.uz/problem/view/IZhO14_bank // TLE int main() { ios::sync_with_stdio(0); cin.tie(0); 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 i=1; i<(1 << m); i++) { int cur1=0; int cur2 = 0; int sum=0; for (int j=0; j<m; j++) { if (((i >> j)&1)) { sum += b[j]; cur2 += (1 << j); } else { cur1 += (1 << j); } } // cout << i << " " << sum << "\n"; all[sum].pb({cur2, cur1}); allsum[i] = cur2; } bool work=false; for (int i=0; i<n; i++) { work = false; for (auto one : all[a[i]]) { int sum = one.first; if (i == 0) { dp[0][sum] = true; work=true; } else { for (int j = one.second; j>0; j = (j-1) & one.second) { // for (int j=0; j<(1<<m); j++) { // int cursum = 0; int cursum = allsum[j]; // for (int k=0; k < m; k++) { // if ((j >> k) & 1) { // cursum += (1 << k); // } // } if (!dp[i][sum + cursum]) { dp[i][sum + cursum] = dp[i-1][cursum]; } if (dp[i][sum + cursum]) work = true; } } } } // for (int i=0; i<n; i++) { // for (int j=0; j<(1 << m); j++) { // cout << dp[i][j] << " "; // } // cout << "\n"; // } // for (int j=(1 << m); j>=1; j--) { // if (dp[n-1][j]) { // cout << "YES"; // return 0; // } // } if (work) cout << "YES"; else cout << "NO"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...