// gpt code
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF_NEG = LLONG_MIN / 4;
struct Line {
mutable int k, m, p, idx;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(int x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
static const long long INF = LLONG_MAX;
// safe division using __int128 to avoid overflow
long long div(long long a, long long b) {
if (b == 0) return (a >= 0 ? LLONG_MAX : LLONG_MIN);
__int128 A = a, B = b;
__int128 q = A / B;
if ((A ^ B) < 0 && (A % B)) q -= 1; // floor for negatives
if (q > LLONG_MAX) return LLONG_MAX;
if (q < LLONG_MIN) return LLONG_MIN;
return (long long) q;
}
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = INF; return false; }
if (x->k == y->k) x->p = (x->m > y->m ? INF : -INF);
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(int k, int m, int idx) {
auto z = insert({k, m, 0, idx}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
pair<int,int> query(int x) {
assert(!empty());
auto it = lower_bound(x); // finds line with p >= x
Line l = *it;
return { l.k * x + l.m, l.idx };
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, K;
cin >> n >> K;
vector<int> ar(n+1), pref(n+1,0);
for (int i = 1; i <= n; ++i) {
cin >> ar[i];
pref[i] = pref[i-1] + ar[i];
}
auto qsum = [&](int l, int r)->long long{
if (r < l) return 0;
return pref[r] - pref[l-1];
};
// dp[j] = pair{value, chosen_j} for the previous layer (k-1)
vector<pair<int,int>> dp(n+2, {INF_NEG, -1});
// base layer (k = 0)
for (int i = 1; i <= n+1; ++i) dp[i] = {0, -1};
LineContainer cht;
for (int k = 1; k <= K; ++k) {
cht.clear();
vector<pair<int,int>> ndp(n+2, {INF_NEG, -1}); // new layer (k)
// iterate j from n down to 1
for (int j = n; j >= 1; --j) {
// slope = pref[j]
long long slope = pref[j];
// intercept uses dp[j+1] from previous layer
long long intercept = dp[j+1].first - pref[j]*pref[j];
cht.add((int)slope, (int)intercept, j);
long long x = pref[n] + pref[j-1];
auto pr = cht.query((int)x);
long long val = -pref[j-1] * pref[n] + pr.first;
ndp[j] = {(int)val, pr.second};
}
dp.swap(ndp); // dp now holds current layer k
}
cout << dp[1].first << "\n";
// reconstruct splits from final dp (each dp[j].second is the chosen j)
int i = 1, k = K;
while (k > 0 && i <= n) {
int j = dp[i].second;
if (j <= 0) break;
cout << j << " ";
i = j + 1;
--k;
}
cout << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |