제출 #1141535

#제출 시각아이디문제언어결과실행 시간메모리
1141535AmirMakaM은행 (IZhO14_bank)C++20
100 / 100
101 ms9100 KiB
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second 
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pii pair<int,int> // 
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define pdd pair<double,double>
#define mp make_pair   
#define AmirMakaM ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
// solve it
const ull SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 mrand(SEED);
ull rnd(ull x = ~(0ull)) {return mrand() % x;} 
const ll MOD = 998244353;
const ll INF = 1e16+20;
const int inf = 1e9 + 7;
const ll N = 2e5+5;
const ll M = 2e3+1;
const double pi = 2*acos(0.0);
const int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};

void solve() {
    int n, m;
    cin >> n >> m;
    int a[n+1], b[m+1];
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=m; i++) cin >> b[i];
    
    pii dp[(1<<m)];
    for(int i=0; i<(1<<m); i++) dp[i] = {-1,-1};
    dp[0] = {0,0};
    
    for(int i=1; i<(1<<m); i++) {
        for(int j=0; j<m; j++) {
            if(!(i&(1<<j))) continue;
            int prev = (i^(1<<j));
            if(dp[prev].f == -1) continue;
            
            //cout << i << " " << prev << "  " << j+1 << '\n';
            if(dp[prev].s + b[j+1] == a[dp[prev].f+1]) {
                dp[i] = {dp[prev].f+1,0};
            } else if(dp[prev].s + b[j+1] < a[dp[prev].f+1]) {
                dp[i] = {dp[prev].f,dp[prev].s + b[j+1]};
            }
            //cout << dp[i].f << " " << dp[i].s << '\n';
        }
        
        if(dp[i].f == n) {
            cout << "YES";
            return;
        }
    }
    cout << "NO";
}

int main() {
    AmirMakaM;
    srand(SEED);
    //freopen("walk.in", "r", stdin);
    //freopen("walk.out", "w", stdout);
    int ttt = 1;
    //cin >> ttt;
    while(ttt--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...