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 <iostream>
using namespace std;
long long mx(long long X, long long Y)
{
return X > Y ? X : Y;
}
long long mn(long long X, long long Y)
{
return X < Y ? X : Y;
}
int main()
{
long long n, k;
cin >> n >> k;
long long a[n+1];
long long P = 0;
long long prev[n+1];
a[0] = 0;
for(long long i = 1; i <= n; i++)
{
cin >> a[i];
a[i] += a[i-1];
}
for(long long i = 1; i <= n; i++)
{
while((k+1)*(a[i] - a[P]) > a[n]) P++;
prev[i] = P;
}
long long dp[n+1][k+1]; //minimum s2 value
long long p_pos[n+1][k+1];
for(long long i = 1; i <= n; i++)
{
dp[i][0] = a[i] * a[i];
p_pos[i][0] = 0;
for(long long x = 1; x <= k; x++)
{
dp[i][x] = 4000000000000000000LL;
for(long long j = mx(1, prev[i] - 2); j < mn(i, prev[i] + 2); j++)
{
if(dp[j][x-1] + (a[i] - a[j])*(a[i] - a[j]) < dp[i][x])
{
dp[i][x] = dp[j][x-1] + (a[i] - a[j])*(a[i] - a[j]);
p_pos[i][x] = j;
}
}
}
}
cout << (a[n]*a[n] - dp[n][k])/2 << '\n';
long long y = n;
for(long long i = k; i >= 1; i--)
{
cout << p_pos[y][i] << ' ';
y = p_pos[y][i];
}
cout << '\n';
return 0;
}
# | 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... |