This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 4000;
const int LOG = 50;
const int block = 700;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
struct Line{
	ll m, c;
	int idx;
	ll eval(ll x) {return 1LL * m * x + c;}
	long double intersectX(Line l)
	{
		return (long double) (l.c - c) / (long double) (m - l.m);
	}
};
int n;
int k;
int a[mxN];
int pre[mxN];
int trace[207][mxN];
ll dp[2][mxN];
deque<Line> dq;
vector<int> ans;
// ax + y
void solve()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
    memset(dp, -0x3f, sizeof(dp));
    ll oo = dp[0][0];
    for(int i = 1; i <= n; i++) dp[0][i] = 0;
    for(int i = 1; i <= k; i++) {
        dq.clear();
        for(int j = 0; j <= n; j++) dp[i & 1][j] = oo;
        for(int j = 1; j <= n; j++) {
            Line newline = {pre[j - 1], dp[(i - 1) & 1][j - 1] - 1LL * pre[j - 1] * pre[j - 1], j - 1};
            while(sz(dq) >= 2 && dq.back().intersectX(newline) <= dq[sz(dq) - 2].intersectX(newline)) dq.pop_back();
            dq.push_back(newline);
            //lc.add(pre[j - 1], dp[(i - 1) & 1][j - 1] - 1LL * pre[j - 1] * pre[j - 1], j - 1);
            while(sz(dq) >= 2 && dq[0].eval(pre[j]) <= dq[1].eval(pre[j])) dq.pop_front();
            pair<ll, int> tmp = {dq[0].eval(pre[j]), dq[0].idx};
            if(maximize(dp[i & 1][j], tmp.fi) == true) {
                trace[i][j] = tmp.se;
            }
        }
    }
    cout << dp[k & 1][n] << endl;
    int x = k;
    int y = n;
    while(x > 0) {
        int tmp = trace[x][y];
        if(y != n) ans.pb(y);
        --x;
        y = tmp;
    }
    ans.pb(y);
    reverse(all(ans));
    for(auto it : ans) cout << it << ' ';
}
// dp[i][k] = dp[j][k - 1] + (pre[i] - pre[j]) * pre[j]
// dp[i][k] = dp[j][k - 1] - pre[j] ^ 2 + pre[i] * pre[j]
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("task.inp", "r", stdin);
    //freopen("task.out", "w", stdout);
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    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... |