#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define sp " "
#define debug(x) cout << #x << " => " << x << "\n"
const int mod = 1e9 + 7;
const ld err = 1e-6;
int n, k;
ll dp[100005], ne[100005], arr[100005], p[100005];
int backtrack[205][100005];
int aqua;
vector <int> ans;
struct line{
ll m, c, idx;
int f(int x){
return m * x + c;
}
ld intersect(line l){
return (ld) (c - l.c) / (l.m - m);
}
};
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> arr[i];
p[i] = p[i - 1] + arr[i];
}
fill(dp, dp + n + 1, 0);
for(int i = 0; i <= k; i++){
deque <line> dq;
dq.push_front({-p[i], dp[i], i});
for(int j = i + 1; j <= n; j++){
while(dq.size() > 1 && dq.back().f(p[n] - p[j]) <= dq[dq.size() - 2].f(p[n] - p[j]))
dq.pop_back();
ne[j] = p[j] * (p[n] - p[j]) + dq.back().f(p[n] - p[j]);
backtrack[i][j] = dq.back().idx;
line cur = {-p[j], dp[j], j};
while(dq.size() > 1 && cur.intersect(dq[1]) >= dq[0].intersect(dq[1]))
dq.pop_front();
dq.push_front(cur);
}
for(int j = i + 1; j <= n; j++){
dp[j] = ne[j];
ne[j] = 0;
}
}
cout << dp[n] << "\n";
aqua = n;
for(int i = k; i >= 1; i--){
aqua = backtrack[i][aqua];
ans.push_back(aqua);
}
for(int i = ans.size() - 1; i >= 0; i--)
cout << ans[i] << " ";
cout << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
332 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Incorrect |
0 ms |
332 KB |
declared answer doesn't correspond to the split scheme: declared = 205032704, real = 4500000000 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Incorrect |
0 ms |
460 KB |
declared answer doesn't correspond to the split scheme: declared = -2100597223, real = 122453454361 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
332 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Incorrect |
2 ms |
1356 KB |
declared answer doesn't correspond to the split scheme: declared = 646158965, real = 1989216017013 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
332 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Incorrect |
8 ms |
1972 KB |
declared answer doesn't correspond to the split scheme: declared = 1135786834, real = 48529971264338 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
844 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
2 ms |
972 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Incorrect |
83 ms |
9316 KB |
declared answer doesn't correspond to the split scheme: declared = -1385059858, real = 4916765506251246 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
5192 KB |
declared answer doesn't correspond to the split scheme: declared = 337910107, real = 19665368619 |
2 |
Halted |
0 ms |
0 KB |
- |