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;
const int N = 1e5 + 17;
int n, k, cut[217][N], z, as[N], c;
long long a[N], f[217][N], s[N], ans;
void dac (int l, int r, int x, int y, int zz)
{
if (l > r)
{
return;
}
int mid = l + r >> 1;
for (int i = max (x, mid - 2); i <= y; ++i)
{
if (f[mid][zz] < f[mid - 1][i] + (s[zz] - s[i]) * (s[n] - s[zz]))
{
f[mid][zz] = f[mid - 1][i] + (s[zz] - s[i]) * (s[n] - s[zz]);
cut[mid - 1][zz] = i;
if (f[mid][zz] >= ans)
{
ans = f[mid][zz];
z = zz;
}
}
}
dac (l, mid - 1, x, cut[mid - 1][zz], zz);
dac (mid + 1, r, cut[mid - 1][zz], y, zz);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
for (int i = 1; i <= n; ++i)
{
for (int j = 2; j <= k + 1; ++j)
{
f[j][i] = -1e18;
}
}
for (int i = 1; i < n; ++i)
{
dac (2, min (i + 1, k + 1), 0, i - 1, i);
}
cout << ans << "\n";
if (!z)
{
return 0;
}
as[++c] = z;
int x = cut[k][z];
while (k > 1)
{
as[++c] = x;
x = cut[--k][x];
}
while (c--)
{
cout << as[c + 1] << " ";
}
}
Compilation message (stderr)
sequence.cpp: In function 'void dac(int, int, int, int, int)':
sequence.cpp:12:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int mid = l + r >> 1;
| ~~^~~
# | 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... |