Submission #1161796

#TimeUsernameProblemLanguageResultExecution timeMemory
1161796aram.hosnaK blocks (IZhO14_blocks)C++20
0 / 100
41 ms81128 KiB
/*in the name of god*/
#include <bits/stdc++.h>


using namespace std;

typedef long long ll ;
typedef long double ld ;
typedef pair<ll , ll > pii ;
typedef pair<ll , pii > pip ;

#define S   second
#define F   first
#define pb   push_back
#define ms(x , y)   memset(x , y , sizeof(x))
#define all(x)   x.begin() , x.end()
#define set_dec(x)	  cout << fixed << setprecision(x);
#define Mp(x , y)   make_pair(x , y)

const ll inf = 1e18 ;
const int N = 1e5+100 ;

ll n , k , a[N] , dp[N][102] , lf[N] , b[N] ;
stack <int> s ;

void solve(){
    cin >> n>> k ;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
    }
    for(int i = 1 ;i <= n ; i++){
        while(!s.empty() && a[s.top()] < a[i])s.pop() ;
        if(s.empty())lf[i] = -1 ;
        else lf[i] = s.top();
        s.push(i);
    }
    ms(dp , 63);
    ms(b , 63);
    ll mx= 0 ;
    for(int i = 1;  i <= n ;i++){
        mx = max(mx, a[i]);
        dp[i][1] = mx ;
        b[1] = min(b[1] , dp[i][1]);
    }
    for(int i =1 ; i <=n ; i++){
        for(int j = 2 ; j <= k ; j++){
            if(lf[i] == -1){
                    dp[i][j] = b[j-1]+ a[i];

            }
            else dp[i][j] = min(dp[lf[i]][j-1] + a[i] , dp[lf[i]-1][j]);
            b[j] = min(b[j] , dp[i][j]);
        }
    }
    cout << dp[n][k] << endl;


}


int main(){
    cin.tie(0)->sync_with_stdio(0) ;


    int tt=1 ;
    //cin >> tt ;
    while(tt--)solve();

    return 0 ;
}

// never stop believing:)
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...