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 <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll Mod = 1e9 + 7;
const ll Inf = 2242545357980376863LL;
const ll Log = 20;
//const ll N = 1ll << Log;
const int Maxn = 1e5 + 10;
const int Base = 101;
typedef pair<ll, ll> Line;
typedef pair<pair<ll, Line>, int> Ty;
struct CHT{
set < pair < Line , ll > > S;
set < pair < ll , Line > > I;
map< Line, int> mp;
vector<Ty> L;
ll INF = 4e18;
inline void Add(Line X, ll idx){
while(!L.empty() && Intersection(L.back().F.S, X) <= L.back().F.F)
L.pop_back();
L.pb({{(L.empty() ? -INF : Intersection(L.back().F.S, X)), X}, idx});
return ;
X.F *= -1;
X.S *= -1;
mp[X] = idx;
ll t = -INF;
auto it = S.lower_bound({X, -INF});
while ((int)S.size())
{
if (it == S.begin())
{t = -INF; break;}
it --; ll r = Intersection(it->first, X);
if (r <= it->second)
I.erase({it->second, it->first}), it = S.erase(it);
else
{t = r; break;}
}
it = S.lower_bound({X, -INF});
while ((int)S.size())
{
if (it == S.end())
break;
ll r = Intersection(X, it->first);
Line Y = it->first;
I.erase({it->second, it->first});
it = S.erase(it);
if (r <= t)
{
r = -INF;
if (it != S.begin())
it --, r = Intersection(it->first, Y);
S.insert({Y, r}); I.insert({r, Y}); return ;
}
if (it != S.end() && it->second <= r)
continue;
S.insert({Y, r}); I.insert({r, Y}); break;
}
S.insert({X, t}); I.insert({t, X});
}
inline pll GetMin(ll X)
{
auto it = upper_bound(all(L), Ty({X, {INF, INF}}, INF)); it --;
return pll((X * it->F.S.F + it->F.S.S), it -> S);
}
inline ll Intersection(Line X, Line Y)
{
if (X.first == Y.first && X.second <= Y.second)
return (-INF);
if (X.first == Y.first)
return (INF);
return ((X.second - Y.second) / (Y.first - X.first)) + ((X.second - Y.second) % (Y.first - X.first) > 0);
}
};
CHT cht;
ll a[Maxn], mk[Maxn];
ll ps[Maxn], sf[Maxn];
ll dp[Maxn][203], par[Maxn][203];
vector<ll> A;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//debug((-3) / 2);
ll n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
ps[0] = 0;
for(int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i - 1];
for(int i = 0; i <= n; i++) dp[i][0] = ps[i] * ps[i];
pll res;
for(int j = 1; j <= k; j++){
cht.I.clear();
cht.S.clear();
cht.mp.clear();
dp[0][j] = 0;
for(int i = 1; i <= n; i++){
cht.Add( pll(-2LL * ps[i - 1], dp[i - 1][j - 1] + ps[i - 1]*ps[i - 1] ), i - 1);
res = cht.GetMin(ps[i]);
dp[i][j] = res.F + ps[i]*ps[i];
par[i][j] = res.S;
}
}
cout << ((ps[n] * ps[n]) - dp[n][k]) / 2LL << '\n';
ll nw = n;
for(int i = 0; i < k; i++){
nw = par[nw][k - i];
A.pb(nw);
}
reverse(all(A));
for(auto x : A) cout << x << ' ';
cout << '\n';
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... |