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>
#define MAXN 100005
#define MAXK 205
using namespace std;
typedef long long int ll;
vector<ll> dpprev, dpnow;
int n, k, arr[MAXN], dprev[MAXN][MAXK], layer;
ll s[MAXN];
void DCT(int a, int b, int op1, int op2) {
if (a > b) return;
int mid = (a + b) >> 1;
int theop = op1;
for (int op = op1; op <= min(mid-1, op2); op++)
if (dpnow[mid-1] < dpprev[op-1] + s[op] * (s[mid] - s[op]))
theop = op,
dpnow[mid-1] = dpprev[op-1] + s[op] * (s[mid] - s[op]),
dprev[mid][layer] = theop;
DCT(a, mid-1, op1, theop), DCT(mid+1, b, theop, op2);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d ", &arr[i]);
s[0] = 0;
for (int i = 1; i <= n; i++)
s[i] = s[i-1] + arr[i];
for (int i = 1; i <= n; i++)
dpprev.push_back(0);
for (layer = 1; layer <= k; layer++) {
dpnow.clear();
for (int i = 1; i <= n; i++)
dpnow.push_back(0);
DCT(layer, n, layer, n-1);
dpprev = dpnow;
}
printf("%lld\n", dpnow.back());
bool ap = 0;
int x = n;
layer--;
while (dprev[x][layer]) {
if (ap) printf(" ");
printf("%d", dprev[x][layer]);
ap = 1;
x = dprev[x][layer];
layer--;
}
printf("\n");
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d ", &arr[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... |