#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define ld long double
#define ar array
const int LOG = 20;
const int MOD = 1e9 + 7;
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][202];
int prv[N][202];
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] = -c[0];
prv[j][i] = c[1];
CHT.add(LINE{-pref[i], -(dp[i][j - 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] + (pref[n - 1] - pref[i]) * pref[i]){
res = dp[i][k - 1] + (pref[n - 1] - pref[i]) * pref[i];
opt = i;
}
}
cout<<res<<'\n';
vector<int> ans;
for(int j = k - 1;j >= 0;j--){
ans.push_back(opt);
opt = prv[j][opt];
}
for(auto u:ans)cout<<u + 1<<" ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
860 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
0 ms |
604 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
860 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
1 ms |
860 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
2 ms |
920 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1884 KB |
declared answer doesn't correspond to the split scheme: declared = 21503404, real = 21501998 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
16736 KB |
declared answer doesn't correspond to the split scheme: declared = 1818678304, real = 1814947614 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |