Submission #1370159

#TimeUsernameProblemLanguageResultExecution timeMemory
1370159c12Bank (IZhO14_bank)C++20
71 / 100
292 ms25064 KiB
#include <bits/stdc++.h>

#define ll long long

using namespace std;

vector<int>people,money;

vector<pair<int,int>>dp;
vector<int>vis;

int n,m;

void printByte(int bit){
    for(int i = m;i >= 0;i--){
        cout << (((bit & (1 << i)) > 0) ? 1 : 0);
    }
    cout << '\n';
}

void recursive(int bit){
    if(vis[bit]) return;
    vis[bit] = 1;

    for(int i = 0;i <= m;i++){
        if((1 << i) & bit){
            int nbit = bit xor (1 << i);

            recursive(nbit);
            auto npair = dp[nbit];
            if(npair.first < n && npair.second - money[i] >= 0){
                npair.second -= money[i];
                if(npair.second == 0){
                    npair.first++;
                    if(npair.first < n){
                        npair.second = people[npair.first];
                    }
                }
            }

            dp[bit] = max(dp[bit],npair);
        }
    }

    // cout << bit << '\n';
    // printByte(bit);
    // cout << dp[bit].first << ' '  << dp[bit].second << '\n';
    // cout << '\n';
} 

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m;

    people = vector<int>(n);
    money = vector<int>(m);

    dp = vector<pair<int,int>>(1 << (m+1));
    vis = vector<int>(1 << (m+1));

    for(int i = 0;i < n;i++) cin >> people[i];
    for(int i = 0;i < m;i++) cin >> money[i];

    dp[0] = {0,people[0]};

    // cout << dp[0].first << ' ' << dp[0].second << '\n';

    recursive((1 << (m+1)) - 1);

    if(dp[(1 << (m+1)) - 1].first == n){
        cout << "YES";
    }
    else{
        cout << "NO";
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...