Submission #791735

#TimeUsernameProblemLanguageResultExecution timeMemory
791735CDuongFeast (NOI19_feast)C++17
22 / 100
72 ms8512 KiB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("arch=skylake")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;

int n, k, a[mxN];
ll pref[mxN], ans;
pair<ll, int> dp[mxN];

ll calc(int l, int r) {
    if(l > r) return 0;
    return pref[r] - pref[l - 1];
}

void solve() {
    cin >> n >> k;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
    }
    ll l = -oo / n, r = 0;
    while(l < r) {
        ll mid = (l + r + 1) >> 1;
        pair<ll, int> maxx = {0, 0};
        for(int i = 1; i <= n; ++i) {
            ll tmp = maxx.ff + pref[i] + mid;

            if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
            else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});

            if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
            else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
        }
        if(maxx.ss <= k) l = mid;
        else r = mid - 1;
    }

    pair<ll, int> maxx = {0, 0}, doesntexist = {0, 0};
    for(int i = 1; i <= n; ++i) {
        ll tmp = maxx.ff + pref[i] + l;

        if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
        else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});

        if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
        else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});

        if(dp[i].ff == doesntexist.ff) doesntexist = min(doesntexist, dp[i]);
        else doesntexist = max(doesntexist, dp[i]);
    }

    cout << doesntexist.ff - doesntexist.ss * l << endl;
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
    auto end = chrono::high_resolution_clock::now();
    cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
    cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
#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...