제출 #336136

#제출 시각아이디문제언어결과실행 시간메모리
336136nafis_shifatK blocks (IZhO14_blocks)C++14
100 / 100
383 ms41356 KiB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
int main() {
	int n,K;
	cin>>n>>K;
	int a[n+1];
	for(int i = 1; i <= n; i++) cin>>a[i];


	int dp[n+1][K+1];


    
    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 j = 2; j <= K; j++) {
    	stack<pii> st;
    	for(int i = 1; i <= n; i++) {
    		int cur = dp[i - 1][j - 1];

    		while(!st.empty() && a[st.top().first] <= a[i]) {
    			cur = min(cur,st.top().second);
    			st.pop();
    		}    		

    		if(!st.empty()) dp[i][j] = dp[st.top().first][j];

    		dp[i][j] = min(dp[i][j],cur + a[i]);
    		st.push({i,cur});
    	}
    }
	

	cout<<dp[n][K]<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...