#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 = 2e18;
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) {
return (long double)(ln3.b - ln1.b) / (ln1.a - ln3.a) <= (long double)(ln2.b - ln1.b) / (ln1.a - ln2.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 = 0;
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
sequence.cpp: In function 'void solution()':
sequence.cpp:87:45: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
87 | cht.add(LINE(-pre[i], -(dp[i][k & 1 ^ 1] - pre[i] * pre[i]), i));
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
0 ms |
2396 KB |
Integer 0 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
2 ms |
22876 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
4440 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
6488 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
2 ms |
14680 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
6492 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
7 ms |
80476 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
5 ms |
57692 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
8 ms |
74332 KB |
Integer 0 violates the range [1, 199] |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
13 ms |
80568 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
14 ms |
80504 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
12 ms |
70232 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
13 ms |
80476 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
13 ms |
80420 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
3 ms |
18780 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
6 ms |
35420 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4960 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
2 ms |
4956 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
133 ms |
81236 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
5228 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
77 ms |
52132 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
84 ms |
60312 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
100 ms |
62736 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
82 ms |
52340 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
96 ms |
60584 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
121 ms |
75056 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
14200 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
21 ms |
15012 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1487 ms |
88140 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
25 ms |
13984 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1661 ms |
88460 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
957 ms |
65064 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1201 ms |
71304 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
984 ms |
59836 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
1115 ms |
67096 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1391 ms |
82156 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |