This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 547
//#define mp make_pair
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll inf=1e18;
const ll mod=1e9+7;
const ld pai=acos(-1);
ll N , K ;
ll A [100009] , Last [100009] , Dp [100009][109] ;
ll Tree [400009] ;
void upd ( int node , int l , int r , int id , ll val ) {
if ( l == r ) {
Tree [node] = val ;
return ;
}
if ( id <= mid ) upd ( node * 2 , l , mid , id , val ) ;
else upd ( node * 2 + 1 , mid + 1 , r , id , val ) ;
Tree [node] = min ( Tree [node*2] , Tree [node*2+1] ) ;
}
ll query ( int node , int l , int r , int s , int e ) {
if ( l > r || s > r || e < l ) return inf ;
if ( s <= l && e >= r ) return Tree [node] ;
return min ( query ( node * 2 , l , mid , s , e ) , query ( node * 2 + 1 , mid + 1 , r , s , e ) ) ;
}
int main () {
cin >> N >> K ;
for ( int i = 1 ; i <= N ; i ++ ) cin >> A [i] ;
for ( int i = 1 ; i <= N ; i ++ ) {
if ( A [i-1] > A [i] ) Last [i] = i - 1 ;
else Last [i] = Last [i-1] ;
}
for ( int i = 0 ; i <= N ; i ++ ) {
for ( int j = 1 ; j <= K ; j ++ ) {
Dp [i][j] = inf ;
}
}
for ( int j = 1 ; j <= K ; j ++ ) {
for ( int i = j ; i <= N ; i ++ ) {
ll Val1 = query ( 1 , 0 , N , Last [i] + 1 , i ) + A [i] ;
ll Val2 = Dp [ Last [i] ][j] ;
Dp [i][j] = min ( Val1 , Val2 ) ;
}
for ( int i = 1 ; i <= N ; i ++ ) {
upd ( 1 , 0 , N , i , Dp [i][j] ) ;
}
}
cout << Dp [N][K] << endl ;
}
# | 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... |