#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
//#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define pii pair<int, int>
#define fi first
#define se second
#define sz(x) (int)(x).size()
constexpr long double EPS = 1e-12;
constexpr i64 INF = 1E18;
struct Line{
i64 m, c, i;
Line(i64 _m, i64 _c, int _i) : m(_m), c(_c), i(_i) {}
inline i64 calc(int x){
return m * x + c;
}
inline long double x_int(Line &other){
return (long double)(c - other.c) / (other.m - m + EPS);
}
};
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, k;
cin >> n >> k;
vi a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i - 1];
}
vector<vector<i64>> dp(2, vector<i64>(n + 1, -INF));
vvi from(n + 1, vi(k + 2));
for(int j = 1; j < k + 1; j++){
deque<Line> dq;
dq.emplace_front(0, 0, 0);
for(int i = 1; i <= n; i++){
int ps = a[n] - a[i];
while(sz(dq) >= 2 && dq.back().calc(ps) <= dq[sz(dq) - 2].calc(ps)){
dq.pop_back();
}
dp[1][i] = dq.back().calc(ps) + a[i] * ps;
from[i][j] = dq.back().i;
Line cur = {-a[i], dp[0][i], i};
while(sz(dq) >= 2 && cur.x_int(dq[0]) > dq[1].x_int(dq[0])){
dq.pop_front();
}
dq.emplace_front(cur);
}
dp[0] = dp[1];
}
std::pair<i64, int> ans{-1, 0};
for(int i = 1; i < n; ++i) {
ans = std::max(ans, {dp[0][i], i});
}
cout << ans.first << "\n";
vi ind;
for(int i = k, idx = ans.second; i >= 1; i--){
ind.emplace_back(idx);
idx = from[idx][i];
}
for(int i = sz(ind) - 1; i >= 0; i--){
cout << ind[i] << " ";
assert(ind[i] != 0);
}
cout << "\n";
return 0;
}
// dp[i][j] = max(dp[z - 1][j - 1] + (a[i] - a[z - 1]) * (a[n] - a[i]))
// dp[i][j] = max(-a[z - 1] * (a[n] - a[i]) + dp[z - 1][j - 1]) + a[i] * (a[n] - a[i])
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
452 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Runtime error |
2 ms |
884 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
344 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Runtime error |
6 ms |
2136 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
2 ms |
1372 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Runtime error |
68 ms |
17748 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
8268 KB |
declared answer doesn't correspond to the split scheme: declared = 3999126103, real = 8294093399 |
2 |
Halted |
0 ms |
0 KB |
- |