# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
369943 | EIMONIM | Split the sequence (APIO14_sequence) | C++14 | 0 ms | 0 KiB |
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 pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
#define int int64_t
#define ii pair<int,int>
#define x first
#define y second
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<ii>
#define vvii vector<vii>
#define vb vector<bool>
#define vvb vector<vb>
#define loop(i,s,e) for(int i=(s);i<(e);i++)
#define loopr(i,s,e) for(int i=(e)-1;i>=(s);i--)
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a=min(a,b)
const int INF = 1e18, MOD = 1e9 + 7;
/*mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto unidist = uniform_int_distribution<int>(0, MAXN);*/
const int N = 2e5;
typedef int64_t ll;
typedef pair<ll, ll> pll;
int n, k, a[N];
ll pre[N], dp[N], cnt[N];
struct CHT {
struct Line {
ll m, b, c;
ll operator()(ll x) { return m * x + b; }
} dq[N * 2];
int l, r;
void init() {
dq[0] = {0, 0, 0};
l = 0, r = 1;
}
bool better_ins(Line& L, Line& L1, Line& L2) {
ll b1 = (L.b - L2.b) * (L2.m - L1.m), b2 = (L2.m - L.m) * (L1.b - L2.b);
return b1 < b2 || (b1 == b2 && L.c <= L1.c);
}
bool better_qry(Line& L1, Line& L2, ll x) {
ll b1 = L1(x), b2 = L2(x);
return b1 > b2 || (b1 == b2 && L1.c >= L2.c);
}
void insert(Line L) {
while(r - l >= 2 && better_ins(L, dq[r - 1], dq[r - 2])) --r;
dq[r++] = L;
}
pll query(ll x) {
while(r - l >= 2 && better_qry(dq[l], dq[l + 1], x)) ++l;
return {dq[l](x), dq[l].c};
}
} cht;
pll calc(ll x) {
dp[0] = cnt[0] = 0;
cht.init();
for(int i = 1 ; i <= n ; ++i) {
pll qry = cht.query(pre[i]);
dp[i] = pre[i] * pre[i] + x + qry.F;
cnt[i] = qry.S + 1;
cht.insert({-2LL * pre[i], dp[i] + pre[i] * pre[i], cnt[i]});
}
return {cnt[n], dp[n]};
}
void init() {
cin >> n >> k; k++;
for(int i = 1 ; i <= n ; ++i) cin >> a[i], pre[i] = pre[i - 1] + a[i];
}
ll solve() {
ll l = 0, r = pre[n] * pre[n], ans = -1;
while(l <= r) {
ll mid = l + (r - l) / 2;
pll res = calc(mid);
if(res.F > k) l = mid + 1;
else r = mid - 1, ans = res.S - k * mid;
}
return ans;
}
int32_t main(){
init();
cout << int(pre[n]*pre[n] - solve())/2 << endl;
Orig::n = n, Orig::k = k-1;
loop(i,0,n) Orig::a[i+1] = a[i+1];
Orig::calc();
return 0;
}
/*
color a
cls
g++ seq.cpp -o a & a
7 3
4 1 3 4 0 2 3
for /l %x in (1,1,16) do a < out\big%x.in > out\big%x.out
*/