Submission #1338059

#TimeUsernameProblemLanguageResultExecution timeMemory
1338059OwstinBank (IZhO14_bank)C++20
52 / 100
220 ms327680 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define umap unordered_map
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n, m; cin >> n >> m;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }
    vector<int> vect(m);
    for (int i = 0; i < m; i++) {
        cin >> vect[i];
    }
    vector<vector<int>> dp(1 << m, vector<int>(1001, -1));
    dp[0][0] = 0;
    for (int i = 0; i < 1 << m; i++) {
        for (int j = 0; j < m; j++) {
            if (i & (1 << j)) {
                for (int k = 0; k + vect[j] < 1000; k++) {
                    if (dp[i ^ (1 << j)][k] != -1) {
                        dp[i][k + vect[j]] = max(dp[i][k + vect[j]], dp[i ^ (1 << j)][k]);
                        if (k + vect[j] == vec[dp[i][k + vect[j]]]) {
                            dp[i][0] = max(dp[i][0], dp[i][k + vect[j]] + 1);
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < 1000; i++) {
        if (dp.back()[i] == n) {
            cout << "YES"; return;
        }
    }
    cout << "NO";
}

int main() {
    FAST_IO;
    //freopen("guard.in", "r", stdin);
    //freopen("guard.out", "w", stdout);
    //TEST_CASES;
    solve(); cout << endl;
    /*int a; cin >> a;
    for (int i = 1; i <= a; i++){
        cout << "Case #" << i << ": ";
        solve();
        cout << 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...