# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
241059 | Nightlight | Split the sequence (APIO14_sequence) | C++14 | 1331 ms | 80632 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.
#include <bits/stdc++.h>
using namespace std;
int N, K;
long long pre[100001];
long long dp[2][100001];
int opt[201][100001];
long long cost(int l, int r) {
return pre[l - 1] * (pre[r] - pre[l - 1]);
}
void dnc(int k, int l, int r, int kl, int kr) {
if(l > r) return;
long long best = -1e18, bestid = 0;
int mid = (l + r) >> 1;
int skg = k & 1;
for(int i = kl; i <= mid && i <= kr; i++) {
long long now = dp[!skg][i - 1] + cost(i, mid);
if(now > best) {
bestid = i;
best = now;
}
}
opt[k][mid] = bestid - 1;
dp[skg][mid] = best;
dnc(k, l, mid - 1, kl, bestid);
dnc(k, mid + 1, r, bestid, kr);
}
int main() {
scanf("%d %d", &N, &K);
K++;
for(int i = 1; i <= N; i++) {
scanf("%lld", &pre[i]);
pre[i] += pre[i - 1];
}
for(int i = 2; i <= K; i++) {
dnc(i, i, N, i, N);
}
printf("%lld\n", dp[K & 1][N]);
int now = N;
vector<int> ans;
for(int i = K; i > 1; i--) {
now = opt[i][now];
ans.push_back(now);
}
for(int i = ans.size() - 1; i >= 0; i--) printf("%d ", ans[i]);
puts("");
cin >> N;
}
Compilation message (stderr)
# | 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... |