55592023-07-26 14:18:14szilMajomházcpp17Accepted 100/1001.889s23600 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 200002;
const ll INF = 1e18;

int n, k;
ll a[MAXN], pref[MAXN], dp[MAXN], used[MAXN];

struct Point {
    ll a = -1;

    Point() {}

    Point(int x) {
        a = x;
    }

    ll f(ll b) {
        if (a == -1) return INF;
        return (pref[b] - pref[a]) * (b - a) + dp[a];
    }

    bool operator<(const Point &x) const {
        return true;
    }
};

Point tree[4*MAXN];

void add(Point p, int v = 1, int tl = 0, int tr = n+1) {
    int tm = (tl + tr) / 2;
    bool lef = p.f(tl) < tree[v].f(tl);
    bool mid = p.f(tm) < tree[v].f(tm);
    if (mid) swap(p, tree[v]);

    if (tl + 1 != tr) {
        if (lef != mid) {
            add(p, v*2, tl, tm);
        } else {
            add(p, v*2+1, tm, tr);
        }
    }
}

pair<ll, Point> get(int b, int v = 1, int tl = 0, int tr = n+1) {
    int tm = (tl + tr) / 2;
    pair<ll, Point> x = {tree[v].f(b), tree[v]};
    if (tl + 1 == tr) {
        return x;
    } else {
        if (b < tm) {
            return min(x, get(b, v*2, tl, tm));
        } else {
            return min(x, get(b, v*2+1, tm, tr));
        }
    }
}

pair<ll, ll> solve(ll lambda) {
    fill(tree, tree+4*MAXN, Point(-1));
    add(Point(0));
    for (int i = 1; i <= n; i++) {
        auto opt = get(i);
        dp[i] = opt.first + lambda;
        used[i] = used[opt.second.a] + 1;
        add(Point(i));
    }
    return {dp[n], used[n]};
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k; k++;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pref[i] = a[i] + pref[i-1];
    }
    ll lo = 0, hi = 1e17;
    while (lo < hi) {
        ll lambda = (lo + hi) / 2;
        auto x = solve(lambda);
        if (x.second <= k) hi = lambda;
        else lo = lambda + 1;
    }
    auto ans = solve(lo);
    cout << (ans.first - lo * ans.second) << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted27ms14424 KiB
2Accepted87ms15052 KiB
subtask210/10
3Accepted30ms14868 KiB
4Accepted29ms15204 KiB
5Accepted28ms15164 KiB
6Accepted28ms15500 KiB
7Accepted28ms15560 KiB
subtask310/10
8Accepted35ms15928 KiB
9Accepted32ms16108 KiB
10Accepted35ms16384 KiB
11Accepted32ms16532 KiB
12Accepted32ms16592 KiB
subtask420/20
13Accepted96ms16948 KiB
14Accepted94ms16920 KiB
15Accepted100ms16980 KiB
16Accepted97ms16860 KiB
17Accepted100ms16860 KiB
18Accepted96ms17048 KiB
subtask529/29
19Accepted842ms20008 KiB
20Accepted876ms19912 KiB
21Accepted866ms19960 KiB
22Accepted884ms20008 KiB
23Accepted882ms19960 KiB
subtask631/31
24Accepted1.715s23008 KiB
25Accepted1.71s23032 KiB
26Accepted1.763s23032 KiB
27Accepted1.804s23032 KiB
28Accepted1.819s23056 KiB
29Accepted1.812s23440 KiB
30Accepted1.86s23288 KiB
31Accepted1.889s23168 KiB
32Accepted1.85s23168 KiB
33Accepted1.886s23468 KiB
34Accepted1.851s23296 KiB
35Accepted1.884s23320 KiB
36Accepted1.85s23600 KiB
37Accepted1.871s23352 KiB
38Accepted1.87s23384 KiB
39Accepted1.868s23356 KiB
40Accepted1.873s23404 KiB