#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 = 2e5 + 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];
unordered_map<int,int> trace[202];
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;
if (cur.fs < 1e16)
trace[k][i] = cur.se;
// cout << i <<" "<<k<<" "<<cur.fs<<" "<<cur.se <<"\n";
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 |
2396 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
2396 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 |
2396 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
0 ms |
2392 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
2 ms |
3164 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
2 ms |
3304 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
3 ms |
3164 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
2 ms |
3164 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
2908 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
16 ms |
9664 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
22 ms |
9900 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
14 ms |
8796 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
18 ms |
9820 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
16 ms |
9676 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
3932 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
7 ms |
5468 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3492 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
3488 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
215 ms |
80508 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
3756 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
128 ms |
49392 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
161 ms |
57224 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
160 ms |
61208 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
136 ms |
50404 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
147 ms |
57304 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
189 ms |
72876 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
16556 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
29 ms |
18540 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Runtime error |
327 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |