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>
using namespace std;
//#define int long long
#define ll long long
#define ld long double
#define ar array
const ll INF = 1e18;
const int N = 1e5 + 20;
struct LINE {
ll a, b, id ;
LINE (ll a, ll b, ll id ) : a(a), b(b), id(id) {}
ll operator() (const ll &x) {
return a * x + b;
}
};
struct convex_hull_trick {
vector<LINE> op;
void init () {
vector<LINE> ().swap(op);
}
bool bad (LINE ln1, LINE ln2, LINE ln3) {
__int128 hi = 1;
return hi * (ln3.b - ln1.b) * (ln1.a - ln2.a) <= hi * (ln2.b - ln1.b) * (ln1.a - ln3.a);
}
void add (LINE ln) {
while (op.size() >= 2 && bad(op[op.size() - 2], op[op.size() - 1], ln)) {
op.pop_back();
}
op.push_back(ln);
}
ar<ll,2> get (int x) {
if (op.empty()) return ar<ll,2>{INF, -1l};
int lf = 0, rt = op.size() - 1;
while (lf < rt) {
int mid = (lf + rt) >> 1;
if (op[mid](x) <= op[mid + 1](x)) rt = mid;
else lf = mid + 1;
}
return ar<ll,2>{op[lf](x), op[lf].id};
}
}CHT;
ll pref[N];
ll dp[N][2];
int prv[202][N];
ll A[N];
signed main() {
int n, k;
cin>>n>>k;
// ll A[n];
for(int i = 0;i < n;i++)cin>>A[i];
//ll pref[n];
//int prv[k][n];
pref[0] = A[0];
for(int i= 1;i < n;i++)pref[i] = pref[i - 1] + A[i];
//ll dp[n][k + 1];
for(int i = 0;i < n;i++)dp[i][0] = 0;
for(int j = 1;j < k;j++){
CHT.init();
for(int i = 0;i < n;i++){
ar<ll, 2> c = CHT.get(pref[i]);
dp[i][j & 1] = -c[0];
prv[j][i] = c[1];
CHT.add(LINE{-pref[i], -(dp[i][j & 1 ^ 1] - pref[i] * pref[i]), i});
}
}
for(int j = 1;j < k;j++){
for(int i = 1;i < n;i++){
//cout<<prv[j][i]<<" ";
}
//cout<<'\n';
}
ll res = -1;
int opt = -1;
for(int i = 0;i < n;i++){
if(res < dp[i][(k - 1) & 1] + (pref[n - 1] - pref[i]) * pref[i]){
res = dp[i][(k - 1) & 1] + (pref[n - 1] - pref[i]) * pref[i];
opt = i;
}
}
cout<<res<<'\n';
for(int j = k - 1;j >= 0;j--){
cout<<opt + 1<<" ";
opt = prv[j][opt];
}
//for(auto u:ans)cout<<u + 1<<" ";
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:75:46: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
75 | CHT.add(LINE{-pref[i], -(dp[i][j & 1 ^ 1] - pref[i] * pref[i]), i});
| ~~^~~
# | 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... |