#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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 27ms | 14424 KiB | ||||
2 | Accepted | 87ms | 15052 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 30ms | 14868 KiB | ||||
4 | Accepted | 29ms | 15204 KiB | ||||
5 | Accepted | 28ms | 15164 KiB | ||||
6 | Accepted | 28ms | 15500 KiB | ||||
7 | Accepted | 28ms | 15560 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 35ms | 15928 KiB | ||||
9 | Accepted | 32ms | 16108 KiB | ||||
10 | Accepted | 35ms | 16384 KiB | ||||
11 | Accepted | 32ms | 16532 KiB | ||||
12 | Accepted | 32ms | 16592 KiB | ||||
subtask4 | 20/20 | ||||||
13 | Accepted | 96ms | 16948 KiB | ||||
14 | Accepted | 94ms | 16920 KiB | ||||
15 | Accepted | 100ms | 16980 KiB | ||||
16 | Accepted | 97ms | 16860 KiB | ||||
17 | Accepted | 100ms | 16860 KiB | ||||
18 | Accepted | 96ms | 17048 KiB | ||||
subtask5 | 29/29 | ||||||
19 | Accepted | 842ms | 20008 KiB | ||||
20 | Accepted | 876ms | 19912 KiB | ||||
21 | Accepted | 866ms | 19960 KiB | ||||
22 | Accepted | 884ms | 20008 KiB | ||||
23 | Accepted | 882ms | 19960 KiB | ||||
subtask6 | 31/31 | ||||||
24 | Accepted | 1.715s | 23008 KiB | ||||
25 | Accepted | 1.71s | 23032 KiB | ||||
26 | Accepted | 1.763s | 23032 KiB | ||||
27 | Accepted | 1.804s | 23032 KiB | ||||
28 | Accepted | 1.819s | 23056 KiB | ||||
29 | Accepted | 1.812s | 23440 KiB | ||||
30 | Accepted | 1.86s | 23288 KiB | ||||
31 | Accepted | 1.889s | 23168 KiB | ||||
32 | Accepted | 1.85s | 23168 KiB | ||||
33 | Accepted | 1.886s | 23468 KiB | ||||
34 | Accepted | 1.851s | 23296 KiB | ||||
35 | Accepted | 1.884s | 23320 KiB | ||||
36 | Accepted | 1.85s | 23600 KiB | ||||
37 | Accepted | 1.871s | 23352 KiB | ||||
38 | Accepted | 1.87s | 23384 KiB | ||||
39 | Accepted | 1.868s | 23356 KiB | ||||
40 | Accepted | 1.873s | 23404 KiB |