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 fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 5, K = 201;
const long long inf = 1e18;
int A[N], s[N];
int32_t bf[K][N];
int dp[2][N];
int m[N], c[N];
deque<int> cht;
int f(int id, int v){
return m[id] * v + c[id];
}
bool cmp(int x, int y, int z){
return (c[x] - c[z]) * (m[y] - m[x]) <= (c[x] - c[y]) * (m[z] - m[x]);
}
void add(int sl, int co, int id){
m[id] = sl, c[id] = co;
while(cht.size() >= 2 && cmp(cht[cht.size() - 2], cht[cht.size() - 1], id)){
cht.pop_back();
}
cht.push_back(id);
}
pii qry(int v){
while(cht.size() >= 2 && f(cht[1], v) >= f(cht[0], v)) cht.pop_front();
return {cht[0], f(cht[0], v)};
}
int32_t main(){
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> A[i];
}
for(int i = 0; i < n; i++) s[i + 1] = s[i] + A[i];
for(int i = 0; i < k; i++){
cht.clear();
for(int j = 0; j < n; j++){
add(s[j], dp[i % 2][j] - s[j] * s[j], j);
pii val = qry(s[j + 1]);
bf[i + 1][j + 1] = val.fi;
dp[(i + 1) % 2][j + 1] = val.se;
}
}
int ans = dp[k % 2][n];
vector<int> cut;
int v = n;
for(int j = k; j >= 1; j--){
v = bf[j][v];
cut.push_back(v);
}
cout << ans << endl;
reverse(cut.begin(), cut.end());
for(auto i : cut){
cout << i << ' ';
}
cout << endl;
}
# | 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... |