#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;
ll n, k;
pair<ll, ll> dp[201][100001];
ll pref[100001];
bool case1(ll k, ll l, ll j, ll i) {
return (dp[i][k].first - dp[i][l].first >
(pref[k] - pref[l]) * (pref[n] - pref[j]));
}
bool case2(ll k, ll l, ll j, ll i) {
return ((dp[i][l].first - dp[i][k].first) * (pref[j] - pref[k]) >
(dp[i][j].first - dp[i][k].first) * (pref[k] - pref[l]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
FOR(i, 1, n + 1) {
ll x;
cin >> x;
pref[i] = pref[i - 1] + x;
}
fill(dp[0], dp[0] + n + 1, make_pair(0, 0));
FOR(i, 1, k + 1) {
deque<ll> q;
q.push_back(0);
dp[i][0] = {0, 0};
FOR(j, 1, n + 1) {
while (q.size() > 1 && case1(q[1], q[0], j, i - 1)) q.pop_front();
ll x = q[0];
dp[i][j] = {dp[i - 1][x].first + (pref[j] - pref[x]) * (pref[n] - pref[j]), x};
// cout << i << ' ' << j << ' ' << x << ' ' << dp[i][j].first << '\n';
while (q.size() > 1 && case2(q[q.size() - 2], q[q.size() - 1], j, i - 1)) q.pop_back();
q.push_back(j);
}
}
ll mx = 0, indx;
FOR(i, 1, n + 1) {
if (dp[k][i].first > mx) mx = dp[k][i].first, indx = i;
}
cout << mx << '\n';
FOR(i, 0, k) {
cout << indx << ' ';
indx = dp[k - i][indx].second;
}
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Incorrect |
2 ms |
384 KB |
contestant didn't find the optimal answer: 1542522 < 1542524 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
contestant didn't find the optimal answer: 1092528 < 1093956 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
384 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
3 ms |
2304 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Incorrect |
2 ms |
324 KB |
contestant didn't find the optimal answer: 1066891648920 < 1499437552673 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
contestant didn't find the optimal answer: 20166072 < 21503404 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1196 KB |
contestant didn't find the optimal answer: 1794250000 < 1818678304 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
7488 KB |
contestant didn't find the optimal answer: 13198103640 < 19795776960 |
2 |
Halted |
0 ms |
0 KB |
- |