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 ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1e5+5;
ll dp[205][MAX];
ll tab[MAX];
int pref[MAX];
ll prefix(int a,int b)
{
return pref[b]-pref[a-1];
}
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
vi ans;
int32_t main()
{
BOOST;
int n,k;
cin>>n>>k;
for (int i=1;i<=n;i++)cin>>tab[i];
for (int i=1;i<=n;i++)pref[i]=pref[i-1]+tab[i];
for (int i=1;i<=k;i++)
{
LineContainer L;
for (int j=2;j<=n;j++)
{
L.add(pref[j-1],-(ll)pref[j-1]*pref[j-1]+dp[i-1][j-1]);
dp[i][j]=L.query(pref[j]);
}
}
/*
for (int i=1;i<=k;i++)
for (int j=1;j<=n;j++)
for (int l=j;l>=2;l--)
dp[i][j]=max(dp[i][j],prefix(l,j)*prefix(1,l-1)+dp[i-1][l-1]);
*/
cout<<dp[k][n]<<"\n";
int wsk=n;
for (int i=k;i>=1;i--)
for (int j=wsk;j>=2;j--)
if (dp[i][wsk]==prefix(j,wsk)*prefix(1,j-1)+dp[i-1][j-1])
{
wsk=j-1;
ans.pb(j-1);
break;
}
for (auto it:ans)cout<<it<<" ";
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... |