This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e5 + 10, K = 210;
ll PS[N], S[N], dp[2][N]; int opt[K][N], A[N], n, k;
inline ll get(int l, int r) {
return S[r] - S[max(0, l - 1)] - (PS[r] - PS[max(0, l - 1)]) * PS[max(0, l - 1)];
}
void DC(int j, int l = 1, int r = n, int optl = 1, int optr = n) {
if (r - l < 0) return;
int mid = (l + r) >> 1;
dp[j & 1][mid] = 1e18;
for (int i = max(2, optl); i <= min(mid, optr); i++) {
ll x = get(i, mid) + dp[1 - (j & 1)][i - 1];
if (x < dp[j & 1][mid]) {
dp[j & 1][mid] = x, opt[j][mid] = i;
}
}
DC(j, l, mid - 1, optl, opt[j][mid] == -1 ? n : opt[j][mid]);
DC(j, mid + 1, r, max(1, opt[j][mid]), optr);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]), PS[i] = PS[i - 1] + A[i];
for (int i = 1; i <= n; i++) {
S[i] = S[i - 1] + 1ll * A[i] * PS[i - 1];
}
memset(opt, -1, sizeof opt);
for (int i = 0; i <= n; i++) dp[1][i] = S[i];
for (int i = 2; i <= k + 1; i++) {
DC(i);
}
printf("%lld\n", S[n] - dp[1 - (k & 1)][n]);
int id = n; vector<int> vec;
for (int i = k + 1; i > 1; i--) {
if (opt[i][id] == -1) { cout << to_string(k); break; }
vec.push_back(id = opt[i][id] - 1);
}
reverse(vec.begin(), vec.end());
for (int &i : vec) printf("%d ", i);
printf("\n");
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
35 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:36:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
36 | for (int i = 1; i <= n; i++) scanf("%d", &A[i]), PS[i] = PS[i - 1] + A[i];
| ~~~~~^~~~~~~~~~~~~
# | 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... |