Submission #337579

#TimeUsernameProblemLanguageResultExecution timeMemory
337579BY_KUTBILIMK개의 묶음 (IZhO14_blocks)C++14
100 / 100
323 ms40772 KiB
/** @BY_KUTBILIM **/
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back  
#define ll long long

const int inf = (int)1e9+7;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie();

    int n, k;
    cin >> n >> k;
    int a[n+1];
    int dp[n+1][k+1];
    
    for(int i = 1; i <= n; i++)cin >> a[i];
    
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= k; j++){
            dp[i][j] = inf;
        }
    }
    dp[0][0] = 0;
    dp[1][1] = a[1];
    for(int i = 2; i <= n; i++){
        dp[i][1] = max(dp[i-1][1], a[i]);
    }
    
    for(int block = 2; block <= k; block++){
        stack<pair<int,int>> st;
        
        for(int i = 1; i <= n; i++){
            int mn = dp[i-1][block-1];
            while(!st.empty() and a[st.top().first] <= a[i]){
                mn = min(mn, st.top().second);
                st.pop();
            }
            if(!st.empty()){
                dp[i][block] = min(dp[i][block], dp[st.top().first][block]);
            }
            dp[i][block] = min(dp[i][block], mn + a[i]);
            st.push({i, mn});
        }
    }
    cout << dp[n][k];
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...