# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
32216 | chonka | K blocks (IZhO14_blocks) | C++98 | 139 ms | 87572 KiB |
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>
#include<stdio.h>
#include<stack>
using namespace std ;
#define inf 1000000007
#define MAXN 100007
#define MAXK 107
int n , k ;
int a[ MAXN ] ;
long long dp[ MAXN ][ MAXK ] ;
long long vals[ MAXN ] ;
long long pref[ MAXN ] ;
void input ( ) {
scanf ( "%d%d" , &n , &k ) ;
int i ;
for ( i = 1 ; i <= n ; i ++ ) {
scanf ( "%d" , &a[ i ] ) ;
}
}
void solve ( ) {
stack < int > s ;
int sz ;
int i , j ;
for ( i = 1 ; i <= n ; i ++ ) {
dp[ i ][ 1 ] = max ( dp[ i - 1 ][ 1 ] , 1LL * a[ i ] ) ;
}
pref[ 0 ] = inf ;
for ( j = 2 ; j <= k ; j ++ ) {
while ( s.empty ( ) == false ) { s.pop ( ) ; }
a[ j - 1 ] = inf ;
s.push ( j - 1 ) ;
sz = 1 ;
for ( i = j ; i <= n ; i ++ ) {
while ( s.empty ( ) == false && a[ s.top ( ) ] <= a[ i ] ) {
sz -- ;
s.pop ( ) ;
}
vals[ sz ] = dp[ s.top ( ) ][ j - 1 ] + a[ i ] ;
pref[ sz ] = min ( pref[ sz - 1 ] , vals[ sz ] ) ;
dp[ i ][ j ] = pref[ sz ] ;
s.push ( i ) ;
sz ++ ;
}
}
printf ( "%lld\n" , dp[ n ][ k ] ) ;
}
int main ( ) {
input ( ) ;
solve ( ) ;
return 0 ;
}
Compilation message (stderr)
# | 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... |