#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("O3")
#define vi vector<int>
#define vb vector<bool>
#define F(i, s, e) for(int i = s; i < e; i++)
#define sp <<' '<<
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define vvi vector<vi>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define endl '\n'
const int mod = 1e9 + 7;
using namespace std;
const int N = 1e5+5;
const int inf = 1e15;
int dp[N][201];
int origin[N][201];
struct Line {
int a, b, from;
};
long double intersection(Line &a, Line &b) {
return ((long double) b.b - a.b) / ((long double) a.a - b.a);
}
void solve() {
int n, k;
cin >> n >> k;
vi a(n+1, 0), p(n+1, 0);
F(i, 0, n) cin >> a[i+1];
F(i, 1, n+1) p[i] = p[i-1] + a[i];
F(i, 0, n+1) F(j, 0, k+1) dp[i][j] = -inf;
dp[0][0] = 0;
F(j, 1, k+1) {
deque<Line> q;
F(i, 0, n+1) {
if(q.size()) {
int x = p[i];
while(q.size() >= 2 && q[0].a * x + q[0].b < q[1].a * x + q[1].b) q.pop_front();
dp[i][j] = q[0].a * x + q[0].b + p[i]*p[n] - p[i]*p[i];
origin[i][j] = q[0].from;
}
Line nw = {p[i], -p[i]*p[n] + dp[i][j-1], i};
while(q.size() >= 2 && intersection(q[q.size() - 2], nw) < intersection(q.back(), nw)) q.pop_back();
q.push_back(nw);
}
}
int ans = 0;
F(i, 1, n+1) ans = max(ans, dp[i][k]);
F(i, 1, n+1) {
if(dp[i][k] == ans) {
int cur = i;
vi res;
for(int j = k; j >= 1; j--) {
res.pb(cur);
cur = origin[cur][j];
}
reverse(res.begin(), res.end());
cout << ans << endl;
for(int e : res) cout << e << " ";
cout << endl;
return;
}
}
}
int32_t main() {
cin.tie(0); ios_base::sync_with_stdio(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#endif
int t = 1;
//cin >> t;
while(t--) solve();
}
# | 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... |