# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
856772 | Amirreza_Fakhri | Split the sequence (APIO14_sequence) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name oF the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define halt(x) cout << x << '\n', exit(0)
using namespace std;
const int inf = 1e9 + 7;
const int mod = 998244353;
const int maxN = 1e5 + 5, maxK = 2e2 + 5;
int n, k, ps[maxN], dp[maxN];
int32_t opt[maxK][maxN];
int cost(int l, int r) {
return ps[l] * (ps[r] - ps[l]);
}
void f(int pos, int q) {
if (!q) return;
cout << pos << ' ';
f(opt[q][pos], q - 1);
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k; k++;
for (int i = 1; i <= n; i++) {
cin >> ps[i]; ps[i] += ps[i-1];
}
for (int i = 1; i <= n; i++) dp[i] = -inf * inf;
dp[0] = 0;
for (int i = 1; i <= k; i++) {
opt[i][n+1] = n-1;
for (int j = n; j >= i; j--) {
dp[j] = -inf * inf;
for (int o = opt[i-1][j]; o <= min(j - 1, 1ll * opt[i][j+1]); o++) {
if (dp[j] < dp[o] + cost(o, j)) {
dp[j] = dp[o] + cost(o, j);
opt[ ][j] = o;
}
}
}
}
cout << dp[n] << '\n';
f(opt[k][n], k - 1);
cout << '\n';
return 0;
}