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>
#define debug printf
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define ll long long
#define sz size()
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define tiii tuple<int,int,int>
#define mkt make_tuple
const int MAXN = 1e5+10 , MAXK = 210 ;
using namespace std ;
int n , k ;
int a[MAXN] ;
int myOpt[MAXN][MAXK] ;
ll sufSum[MAXN] ;
ll dp[MAXN][2] ;
void calcDp(int l , int r , int optmin , int optmax, int am )
{
if( r < l ) return ;
int mid = (l+r)>>1 ;
ll opt , val=0 , cur ;
lp(i,max(optmin,mid),optmax+1)
{
cur = dp[i][!(am%2)] + (sufSum[mid] - sufSum[i])*sufSum[i] ;
if( cur > val ) val = cur, opt = i ;
}
dp[mid][am%2] = val ;
myOpt[mid][am] = opt ;
calcDp(l,mid-1, optmin, opt , am ) ;
calcDp(mid+1, r, opt, optmax, am ) ;
}
int main()
{
scanf("%d%d", &n, &k) ;
lp(i,1,n+1) scanf("%d", &a[i] ) ;
for(int i = n ; i > 0 ; i-- ) sufSum[i] = sufSum[i+1] + a[i] ;
lp(i,1,k+1) calcDp(1,n - i,1,n-i+1,i) ;
printf("%lld\n", dp[1][k%2] ) ;
int cur = 1 ;
for(int i = k ; i > 0 ; i-- )
{
cur= myOpt[cur][i] ;
printf("%d " , cur-1 ) ;
}
printf("\n") ;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k) ;
~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:51:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
lp(i,1,n+1) scanf("%d", &a[i] ) ;
~~~~~^~~~~~~~~~~~~~
sequence.cpp: In function 'void calcDp(int, int, int, int, int)':
sequence.cpp:41:17: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
myOpt[mid][am] = opt ;
~~~~~~~~~~~~~~~^~~~~
# | 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... |