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>
#include<fstream>
using namespace std;
ifstream fin("FOOD.INP");
ofstream fout("FOOD.OUT");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define pf push_front
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 5e3, mxm = 1e5;
const ll inf = 1e18;
struct Line{
ll m, c; int id; // use for tracing
Line(){
m = c = id = 0;
}
Line(ll _m, ll _c, int _id){
m = _m; c = _c; id = _id;
}
ll val(ll x){return(x * m + c);}
ld intersect(const Line &other){return((ld)(c - other.c) / (other.m - m));}
};
int n, k;
int trace[100001][201];
ll dp[100001][2], a[100005], p[100005];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
forr(i, 1, n + 1)cin >> a[i];
forr(i, 1, n + 1){
p[i] = p[i - 1] + a[i];
}
memset(dp, -1, sizeof(dp));
ll ans = -1, id = -1;
dp[0][0] = 0;
forr(j, 1, k + 1){
deque<Line>dq;
if(dp[0][0] != -1)dq.pb(Line(0, 0, 0));
forr(i, 1, n + 1){
while(dq.size() >= 2 && dq.back().val(p[i]) <= dq[dq.size() - 2].val(p[i]))dq.pop_back();
dp[i][1] = dq.back().val(p[i]) - p[i] * p[i] + p[i] * p[n];
trace[i][j] = dq.back().id;
if(j == k){
if(dp[i][1] > ans){
ans = dp[i][1]; id = i;
}
}
if(dp[i][0] == -1)continue;
Line curr = Line(p[i], -p[i] * p[n] + dp[i][0], i);
while(dq.size() >= 2 && (curr.c - dq[0].c) * (dq[1].m - dq[0].m) <= (dq[0].m - curr.m) * (dq[0].c - dq[1].c)){
dq.pop_front();
}
dq.pf(curr);
}
forr(i, 1, n + 1){
dp[i][0] = dp[i][1];
}
}
cout << ans << "\n";
vt<int>cut;
for(int i = k; i >= 1; i--){
cut.pb(id);
id = trace[id][i];
}
for(auto i: cut)cout << i << " ";
return(0);
}
# | 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... |