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, it, sz;
long long a[N], f[N], fz[N], s[N], ans;
struct line
{
long long a, b;
int c;
} l, cv[N];
bool bad (line l1, line l2, line l3)
{
return 1.0 * (l1.b - l3.b) / (l3.a - l1.a) >= 1.0 * (l1.b - l2.b) / (l2.a - l1.a);
}
long long calc (long long x, int i, int j)
{
while (it < sz)
{
if (cv[it].a * x + cv[it].b >= cv[it + 1].a * x + cv[it + 1].b)
{
break;
}
++it;
}
cut[j][i] = cv[it].c;
return cv[it].a * x + cv[it].b;
}
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 j = 2; j <= k + 1; ++j)
{
for (int i = 1; i < n; ++i)
{
f[i] = calc (s[n] - s[i], i, j - 1) + s[i] * (s[n] - s[i]);
l = {-s[i], fz[i], i};
fz[i] = f[i];
while (sz > it)
{
if (bad (cv[sz - 1], cv[sz], l))
{
--sz;
continue;
}
break;
}
cv[++sz] = l;
if (f[i] >= ans)
{
ans = f[i];
z = i;
}
}
sz = it = 1;
}
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] << " ";
}
}
# | 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... |