# | TimeUTC-0 | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1149776 | notasingle... | Split the sequence (APIO14_sequence) | C++20 | 0 ms | 0 KiB |
#include<bits/stdc++.h>
using namespace std;
long long n, k, p;
long long lst[200005], slot[200005][201], trace[200005][201];
deque<pair<long long, long long>> D;
inline f(pair<long long, long long> P, long long t){
return P.first + (lst[t] - lst[P.second])*(lst[t] - lst[P.second]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
p = n;
for(long long i = 1; i <= n; ++i){
cin >> lst[i];
lst[i] += lst[i-1];
slot[i][0] = lst[i] * lst[i];
}
for(long long _ = 1; _ <= k; ++_){
D.push_front({slot[_][_-1], _});
for(long long i = _ + 1; i <= n; ++i){
while(D.size() > 1 && f(D.front(), i) > f(D[1], i)) D.pop_front();
D.push_back({slot[i][_-1], i});
slot[i][_] = f(D.front(), i);
trace[i][_] = D.front().second;
}
while(!D.empty()) D.pop_front();
}
cout << (lst[n] * lst[n] - slot[n][k]) / 2 << '\n';
while(k--){