Submission #1370184

#TimeUsernameProblemLanguageResultExecution timeMemory
1370184c12Bank (IZhO14_bank)C++20
0 / 100
1 ms580 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];
                    }
                }
            }

            if(dp[bit].first > npair.first){
                dp[bit] = npair;
            }
            else if(dp[bit].first == npair.first && dp[bit].second < npair.second){
                dp[bit] = npair;
            }
        }
    }

} 

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);
    vis = vector<int>(1 << m);

    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]};

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

    if(dp[(1 << m) - 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...