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 LL long long
#define pb push_back
int A[100005], n, k;
LL P[100005];
LL dp[205][100005];
int pa[205][100005];
LL sub( int l, int r ){
return P[r] - P[l-1];
}
void dnc( int k, int l, int r, int optl, int optr ){
if ( l > r ) return;
int m = ( l + r ) / 2;
LL &ans = dp[k][m];
int opt = -1;
for ( int i = optl; i <= min(m, optr); i++ ){
LL temp = dp[k-1][i-1] + sub(i,m) * 1LL * sub(m+1,n);
if (opt == -1 || temp > ans ){
ans = temp;
opt = i;
}
}
pa[k][m] = opt;
if ( opt == -1 ) assert(1==2);
dnc(k,l,m-1,optl,opt);
dnc(k,m+1,r,opt,optr);
}
int main(){
scanf("%d%d",&n,&k);
for ( int i = 1; i <= n; i++ ){
scanf("%d",&A[i]);
P[i] = P[i-1] + A[i];
}
for ( int i = 1; i <= k; i++ ){
for ( int j = 1; j < n; j++ ) dp[i][j] = LLONG_MIN;
dnc(i, 1, n-1, 1, n-1);
}
LL ans = n-1;
for ( int i = 1; i < n; i++ ){
if ( dp[k][i] > dp[k][ans] ){
ans = i;
}
}
printf("%lld\n",dp[k][ans]);
int x = ans;
vector < int > V;
for ( int i = k; i >= 1; i-- ){
V.pb(x);
x = pa[i][x] - 1;
}
for ( int i = k-1; i >= 0; i-- ){
printf("%d",V[i]);
if ( i == 0 ) printf("\n");
else printf(" ");
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
sequence.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d",&A[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... |