This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n, k;
ll dp[201][100001];
int pref[100001]{0};
bool case1(int x, int y, int i, int indx) {
return (dp[indx][y] - dp[indx][x] >=
(pref[y] - pref[x]) * (pref[n] - pref[i]));
}
bool case2(int x, int y, int i, int indx) {
return ((dp[indx][y] - dp[indx][x]) * (pref[i] - pref[y]) <=
(dp[indx][i] - dp[indx][y]) * (pref[y] - pref[x]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
FOR(i, 1, n + 1) {
int x;
cin >> x;
pref[i] = pref[i - 1] + x;
}
fill(dp[0], dp[0] + n + 1, 0);
FOR(i, 1, k + 1) {
deque<int> q;
q.push_back(0);
dp[i][0] = 0;
FOR(j, 1, n + 1) {
while (q.size() > 1 && case1(q[0], q[1], j, i - 1)) q.pop_front();
ll x = q[0];
dp[i][j] = dp[i - 1][x] + (ll)(pref[j] - pref[x]) * (ll)(pref[n] - pref[j]);
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 = -1;
int indx = -1;
FOR(i, 1, n + 1) {
if (dp[k][i] > mx) mx = dp[k][i], indx = i;
}
cout << mx << '\n';
FOR(i, 1, k + 1) {
cout << indx << ' ';
FOR(j, 0, indx) {
if (dp[k - i][j] + (pref[indx] - pref[j]) * (pref[n] - pref[indx]) == mx) {
mx = dp[k - i][j];
indx = j;
if (j != 0) break;
}
}
}
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |