Submission #723157

#TimeUsernameProblemLanguageResultExecution timeMemory
723157buihuynhtayK blocks (IZhO14_blocks)C++14
100 / 100
159 ms40644 KiB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 55;
const int inf = 1e9 + 7;
int n, k, a[N];
int dp[101][N];

void solve(){
    cin >> n >> k;
    for(int i = 1 ;i <= n; i++) cin >> a[i];
    int mx = 0;
    for(int g = 0; g <= k; g++)
        for(int i = 0; i <= n; i++)
            dp[g][i] = inf;
    for(int i = 1; i <= n; i++){
        mx = max(mx, a[i]);
        dp[1][i] = mx;
    }
    for(int g = 2; g <= k; g++){
        stack<pair<int, int>> st;
        for(int i = 1; i <= n; i++){
            int mi = dp[g-1][i-1];
            while(!st.empty() && a[st.top().second] <= a[i]){
                mi = min(mi, st.top().first);
                st.pop();
            }
            dp[g][i] = min(dp[g][st.empty() ? 0 : st.top().second], mi + a[i]);
            st.push(make_pair(mi, i));
        }
    }
    cout << dp[k][n] << '\n';
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    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...