Submission #171460

#TimeUsernameProblemLanguageResultExecution timeMemory
171460ne4eHbKa은행 (IZhO14_bank)C++17
100 / 100
842 ms17228 KiB
//{ <defines>
#ifndef _LOCAL
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#endif

#include <bits/stdc++.h>
using namespace std;

#define fr(i, n) for(int i = 0; i < (n); ++i)
#define fo(n) fr(i, (n))

#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _  << ' ' <<

#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back

#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;

const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;

inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());

template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 20;
const int M = 1 << N;

int n, m, a[N], b[N], s[M];
vi f[(int)2e4 + 5];

void solve() {
    srand(time());
    cin >> n >> m;
    int sua = 0;
    fo(n) cin >> a[i], sua += a[i];
//    sort(a, a + n);
//    reverse(a, a + n);
    random_shuffle(a, a + n);
    int sub = 0;
    fo(m) cin >> b[i], sub += b[i];
    if(sua > sub) re void(cout << "NO\n");

#ifdef _LOCAL
    for(auto &p : f) p.clear();
#endif

    fo(1 << m) {
        fr(j, m) ifn(i & pw(j)) s[i ^ pw(j)] = s[i] + b[j];
        bool ok = true;
        bool on = false;
        bool nl = false;
        fr(j, m) {
            if(!on && ((i >> j) & 1)) on = true;
            ef(on && !nl && !((i >> j) & 1)) nl = true;
            ef(nl && ((i >> j) & 1)) ok = false;
            if(j == m - 1 || b[j + 1] != b[j])
                on = nl = false;
        }
        if(ok) f[s[i]].pb(i);
    }

    unordered_set<int> G, H; unordered_set<int> *g = &G, *h = &H;
    g -> insert(0);
    fo(n) {
        if(g -> empty() || f[a[i]].empty()) re void(cout << "NO\n");
        h -> clear();
        for(int t : *g) for(int j : f[a[i]]) ifn(j & t) h -> insert(j ^ t);
        swap(g, h);
    }
    cout << (g -> empty() ? "NO\n" : "YES\n");
}

int main() {
#ifdef _LOCAL
    freopen("in.txt", "r", stdin);
    int tests; cin >> tests;
    fo(tests) {
		cerr << "case #" << i+1 << endl;
		solve();
	}
#else
    ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
#endif
	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...