Submission #578109

#TimeUsernameProblemLanguageResultExecution timeMemory
578109balbitUplifting Excursion (BOI22_vault)C++14
100 / 100
1357 ms1748 KiB
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second

#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define RREP(i,n)  for (int i = n-1; i>=0;--i)
#define REP(i,n)  FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)

#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T,typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT

ll amt[606];
ll taken[606];

const ll inf = 0x3f3f3f3f3f3f3f3f;

const int rng = 300 * 300 ;
ll bst[rng*2+4];

vector<ll> bd(ll x) {
    //  maybe i can min x ith rng or smth
    MN(x, rng*2);
    vector<ll> re;
    ll scale = 1;
    while(x) {
        if (x & 1)  {
            re.pb(scale); x-=1;
        }else{
            re.pb(scale); re.pb(scale); x-=2;
        }
        x/=2; scale *= 2;
    }
    return re;
}

void ASS(bool x) {
    if (!x) while(1);
}

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

    bug(1,2);

//    vector<int> hi = bd(23);
//    for (int x : hi) cout<<x<<' ';
//    return 0;

    REP(i, rng*2+4) {
        bst[i] = -inf;
    }

    int m; ll L;
    cin>>m>>L;

    ll tot = 0;
    ll things= 0;

    FOR(i,-m,m+1) {
        cin>>amt[i+m];
        tot += i * (amt[i+m]);
        things += amt[i+m];
    }
    bug(tot, L);
    if (tot < L) {
        // flip  everything
        FOR(i,0,m+1) {
            swap(amt[i+m], amt[-i+m]);
        }
        tot = -tot; L = -L;
    }

    FOR(i,-m,m+1) {
        taken[i+m] = amt[i+m];
    }

    bug(tot,L);

    for (int i = m; i>=1; --i) {
        if (tot > L + rng) {
            int need = (tot - (L+rng)-1) / i + 1; // ceiling
            need = min(need, taken[i+m]);
            taken[i+m] -= need; tot -= i * need; things -= need;
        }
        if (tot <= L+rng && tot >= L-rng) {
//            MX(bst[tot-L+rng], things);
            bug(tot, things);
            while (taken[i+m] && (tot-i >= L-rng)) {
                --taken[i+m]; tot -= i; --things;
                bug(tot, things);
//                MX(bst[tot-L+rng], things);
            }
        }
    }

    bug(tot, L, things);


    if (tot > L) {
        // sad
        cout<<"impossible"<<endl; return 0;
    }

    MX(bst[tot-L+rng], things);
//    FOR(j,-2*m,2*m+1) {
//        bug(j, bst[j+rng]);
//    }

    FOR(i,1,m+1) {
        if (amt[i+m] - taken[i+m]) {
            // try adding stuff back
            for (ll d : bd(amt[i+m] - taken[i+m])) {
                if (i < 0) {

                    ASS(0);
                }else{
                    RREP(j, rng*2+1 - d*i) {
                        if (j + d*i < 0) ASS(0);
                        if (j + d*i > rng*2) ASS(0);
                        MX(bst[j+d*i], bst[j]+d);
                    }
                }
            }
        }
        #ifdef BALBIT
//        bug(i, taken[i+m], amt[i+m]);
//        FOR(j,-2*m,2*m+1) {
//            bug(j, bst[j+rng]);
//        }
        #endif
    }

    FOR(i,-m,m+1) {
        if (i == 0) continue;
        if (taken[i+m]) {
            // try removing stuff
            for (ll d : bd(taken[i+m])) {
                if (i < 0) {
                    RREP(j,  rng*2+1+d*i) {
                        if (j - d*i < 0) ASS(0);
                        if (j - d*i > rng*2) ASS(0);
                        MX(bst[j-d*i], bst[j]-d);
                    }
                }else{
                    FOR(j, d*i, rng*2+1) {
                        if (j - d*i < 0) ASS(0);
                        if (j - d*i > rng*2) ASS(0);
                        MX(bst[j-d*i], bst[j]-d);
                    }
                }
            }
        }
        #ifdef BALBIT
//        bug(i, taken[i+m], amt[i+m]);
//        FOR(j,-m,m+1) {
//            bug(j, bst[j+rng]);
//        }
        #endif
    }
    if (bst[rng] <= 0) cout<<"impossible"<<endl;
    else cout<<bst[rng]<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...