#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005, K = 205;
ll dp[K][N], sum[N], val[N], m[N], q[N], tot = 0;
int nxt[K][N];
ll get_val(int x, int l, int idx){
return m[x] * m[l] - m[l] * m[l] + dp[idx - 1][l];
}
double get_in(int a, int b, int idx){
return (double) 1.0 * (m[a] * m[a] - dp[idx - 1][a] - m[b] * m[b] + dp[idx - 1][b]) / (m[b] - m[a]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> val[i];
tot += val[i];
sum[i] += val[i];
sum[i + 1] += sum[i];
}
for(int i = 0; i < n; i++) m[i] = tot - (i == 0 ? 0 : sum[i - 1]);
for(int i = 1; i <= k; i++){
val[n - 1] = n - 1;
dp[i][n - 1] = 0;
int idx = n - 1, nn = n - 1;
for(int j = n - 2; j >= 0; j--){
while(idx > nn && get_val(j, val[idx], i) <= get_val(j, val[idx - 1], i)) idx--;
nxt[i][j] = val[idx];
dp[i][j] = get_val(j, val[idx], i);
while(idx > nn && get_in(val[nn], val[nn + 1], i) >= get_in(j, val[nn], i)) nn++;
val[--nn] = j;
}
}
cout << dp[k][0] << endl;
int curr = nxt[k][0];
for(int i = k - 1; i >= 0; i--){
cout << curr << " ";
curr = nxt[i][curr];
}
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 774 < 999 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
380 KB |
contestant didn't find the optimal answer: 1093924 < 1093956 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
4 ms |
2936 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 117554464392 < 1499437552673 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
504 KB |
contestant didn't find the optimal answer: 21503354 < 21503404 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1016 KB |
contestant didn't find the optimal answer: 1818678300 < 1818678304 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
6392 KB |
contestant didn't find the optimal answer: 19795776765 < 19795776960 |
2 |
Halted |
0 ms |
0 KB |
- |