#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10, MAX_K = 210;
int hist[MAX_N][MAX_K], dp[MAX_N][2];
ll pref[MAX_N], arr[MAX_N];
int cnt, curr;
void output(int n, int k) {
if(k == 0) {
return;
}
output(hist[n][k], k - 1);
cout << hist[n][k] << " ";
}
void div(int l, int r, int optl, int optr) {
if(l > r) {return;}
int m = (l + r) / 2ll;
int best = -1;
for(int tr = optl; tr <= min(m - 1, optr); tr ++) {
ll now = dp[tr][curr ^ 1] + pref[tr] * (pref[m] - pref[tr]);
if(chkmax(dp[m][curr], now)) {
dp[m][curr] = now;
hist[m][cnt] = tr;
best = tr;
}
}
div(l, m - 1, optl, best);
div(m + 1, r, best, optr);
}
signed main() {
//ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i ++) {
cin >> arr[i];
pref[i] = pref[i - 1] + arr[i];
}
for(int i = 1; i <= k; i ++) {
curr = i & 1;
cnt = i;
for(int j = 0; j <= n; j ++) {
dp[j][curr] = -mod;
}
div(1, n, 1, n);
}
cout << dp[n][k & 1] << endl;
output(n, k);
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
1 ms |
384 KB |
Integer -1 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Incorrect |
1 ms |
384 KB |
declared answer doesn't correspond to the split scheme: declared = -2100597223, real = 122453454361 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Incorrect |
2 ms |
512 KB |
declared answer doesn't correspond to the split scheme: declared = 646158965, real = 1989216017013 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1152 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
1152 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Incorrect |
6 ms |
1152 KB |
declared answer doesn't correspond to the split scheme: declared = 1103519186, real = 17902527208914 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8832 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
9 ms |
8832 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Incorrect |
70 ms |
8880 KB |
declared answer doesn't correspond to the split scheme: declared = -1694967322, real = 197910398032358 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
692 ms |
85100 KB |
declared answer doesn't correspond to the split scheme: declared = 2147433776, real = 6442401072 |
2 |
Halted |
0 ms |
0 KB |
- |