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>
using namespace std;
typedef long long ll;
ll pre[100005];
ll dp[100005];
ll dpp[100005];
int prosli[205][100005];
struct line{
ll a, b;
int ind;
ll eval(ll x){
return a*x+b;
}
};
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int n, k;
cin >> n >> k;
for(int i=1; i<=n; i++){
int x;
cin >> x;
pre[i] = pre[i-1] + x;
}
line dummy_line;
dummy_line.a = 0;
dummy_line.b = -2000000000000000000LL;
dummy_line.ind = 0;
for(int j=1; j<=k; j++){
deque <line> lines;
lines.push_back(dummy_line);
for(int i=1; i<=n; i++){
while(lines.size() >= 2){
line line1 = lines.front();
lines.pop_front();
line line2 = lines.front();
if(line2.eval(pre[i]) < line1.eval(pre[i])){
lines.push_front(line1);
break;
}
}
dp[i] = lines.front().eval(pre[i]);
prosli[j][i] = lines.front().ind;
line nline;
nline.a = pre[i];
nline.b = -pre[i]*pre[i] + dpp[i];
nline.ind = i;
if(pre[i]-pre[i-1] || nline.b > lines.back().b) lines.push_back(nline);
}
for(int i=1; i<=n; i++){
dpp[i] = dp[i];
}
}
cout << dp[n] << "\n";
int t = n;
for(int i=k; i>=1; i--){
cout << prosli[i][t] << " ";
t = prosli[i][t];
}
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |