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>
using namespace std;
#define endl "\n"
#define int long long int
#define NUMMAX 200050
const int mod = 1e9 + 7;
int n, k;
int brr[201][100001];
int prefarr[100001], dp[2][100001], q[100001], l = 1, r = 1;
bool func1(int x, int y, int i) {
return (dp[0][y] - dp[0][x] >= (prefarr[y] - prefarr[x]) * (prefarr[n] - prefarr[i]));
}
bool func2(int x, int y, int i) {
return ((dp[0][y] - dp[0][x]) * (prefarr[i] - prefarr[y]) <=
(dp[0][i] - dp[0][y]) * (prefarr[y] - prefarr[x]));
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++){
int x;cin >> x;
prefarr[i] = prefarr[i - 1] + x;
}
for (int i = 1; i < k+1; i++){
q[r++] = 0;
for (int j = 1; j < n+1; j++){
while (r - l > 1 && func1(q[l], q[l + 1], j)) l++;
int x = q[l];
dp[1][j] = dp[0][x] + (prefarr[j] - prefarr[x]) * (prefarr[n] - prefarr[j]);
brr[i][j] = x;
while (r - l > 1 && func2(q[r - 2], q[r - 1], j)) r--;
q[r++] = j;
}
l = r = 1;
for (int j = 1; j < n+1; j++){
dp[0][j] = dp[1][j];
}
}
int ans = -1, res = -1;
for (int i = 1; i < n+1; i++){
if (dp[0][i] > ans) ans = dp[0][i], res = i;
}
cout << ans << '\n';
for (int i = 0; i < k; i++){
cout << res << ' ';
res = brr[k - i][res];
}
}
signed main()
{
// freopen("sequence.in","r", stdin);
// freopen("sequence.out","w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
}
# | 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... |