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 fr first
#define sc second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k, opt[100002][202], ps[100069];
long long dp[100069][2];
void dnc(int l, int r, int c, int optl, int optr)
{
if (l>r) return;
int m = (l+r)/2, i;
long long val;
dp[m][c%2] = -1e18;
for (i=optl; i<=min(m-1, optr); i++)
{
val = dp[i][(c+1)%2]+1ll*(ps[m]-ps[i])*ps[i];
if (val>dp[m][c%2])
{
dp[m][c%2] = val;
opt[m][c] = i;
}
}
dnc(l, m-1, c, optl, opt[m][c]);
dnc(m+1, r, c, opt[m][c], optr);
}
int main()
{
int i, j, c;
scanf("%d%d", &n, &k);
k++;
for (i=1; i<=n; i++)
{
scanf("%d", ps+i);
ps[i] += ps[i-1];
dp[i][0] = -1e18;
}
for (c=1; c<=k; c++)
{
dp[0][c%2] = -1e18;
dnc(1, n, c, 0, n-1);
}
printf("%lld\n", dp[n][k%2]);
for (i=n, c=k; c>=2; i=opt[i][c], c--)
{
printf("%d ", opt[i][c]);
}
printf("\n");
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:31:12: warning: unused variable 'j' [-Wunused-variable]
31 | int i, j, c;
| ^
sequence.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d", ps+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... |