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
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18;
const ll BASE = 137;
const int szBL = 320;
int n, K;
ll a[N];
ll dp[N][2];
int trace[202][N];
ll pre[N];
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 (SZ(op) >= 2 && bad(op[SZ(op) - 2], op[SZ(op) - 1], ln)) {
op.pop_back();
}
op.push_back(ln);
}
pll get (ll x) {
if (op.empty()) return MP(INF, 0);
ll lf = 0, rt = SZ(op) - 1;
while (lf < rt) {
ll mid = (lf + rt) >> 1;
if (op[mid](x) <= op[mid + 1](x)) rt = mid;
else lf = mid + 1;
}
return MP(op[lf](x), op[lf].id);
}
}cht;
void solution () {
cin >> n >> K;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n) pre[i] = pre[i - 1] + a[i];
rep (i, 1, n) dp[i][1] = 0;
rep (k, 2, K) {
cht.init();
rep (i, 1, n) {
pll cur = cht.get(pre[i]);
dp[i][k & 1] = -cur.fs;
trace[k][i] = cur.se;
// cout << i <<" "<<k<<" "<<cur.fs<<" "<<cur.se <<"\n";
// if (dp[i][k & 1] != INF)
cht.add(LINE(-pre[i], -(dp[i][k & 1 ^ 1] - pre[i] * pre[i]), i));
}
}
ll res = -1;
int opt = 0;
rep (i, 1, n) {
if (res < dp[i][K & 1] + (pre[n] - pre[i]) * pre[i]) {
res = dp[i][K & 1] + (pre[n] - pre[i]) * pre[i];
opt = i;
}
}
vector<int> Ans;
reb (k, K, 1) {
Ans.push_back(opt);
opt = trace[k][opt];
}
cout << res <<"\n";
iter (&id, Ans) cout << id<<" ";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("TREEV");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp: In function 'void solution()':
sequence.cpp:88:45: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
88 | cht.add(LINE(-pre[i], -(dp[i][k & 1 ^ 1] - pre[i] * pre[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... |