제출 #1370179

#제출 시각아이디문제언어결과실행 시간메모리
1370179c12은행 (IZhO14_bank)C++20
100 / 100
89 ms12752 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;
    dp[bit] = {-1,0};
    for(int i = 0;i < m;i++){
        if((1 << i) & bit){
            int nbit = bit xor (1 << i);

            auto npair = dp[nbit];
            if(npair.first == -1) continue;

            if(npair.first == n){
              dp[bit] = {n,0};
              return;
            }
            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);
            }

        }
    }
} 

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

    for(int i = 1;i < (1 << m);i++){
        recursive(i);
        if(dp[i].first == n){
            cout << "YES";
            return 0;        
        }
    }
    
    cout << "NO";
    
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…