#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) {
return (ln3.b - ln1.b) * (ln1.a - ln2.a) <= (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 = 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 |
0 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 |
0 ms |
4572 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 |
6608 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
14684 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 |
4444 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 |
80420 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
4444 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
6 ms |
57864 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
6 ms |
74196 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
8 ms |
80476 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
2 ms |
20996 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
14684 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
2 ms |
24924 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
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 |
80476 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
4440 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
13 ms |
80456 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
11 ms |
70236 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 |
80476 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
3 ms |
19000 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
6 ms |
35416 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4960 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
4956 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
129 ms |
81160 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
5228 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
75 ms |
52192 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
82 ms |
60312 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
100 ms |
62796 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
83 ms |
52528 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
93 ms |
60648 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
119 ms |
75228 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
14200 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
21 ms |
14712 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1532 ms |
89384 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
21 ms |
14612 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Incorrect |
1520 ms |
88240 KB |
contestant didn't find the optimal answer: 36183245049 < 36183271951 |
6 |
Halted |
0 ms |
0 KB |
- |