제출 #331079

#제출 시각아이디문제언어결과실행 시간메모리
331079GurbanK개의 묶음 (IZhO14_blocks)C++17
0 / 100
1 ms364 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e2+5;
const int maxn=1e5+5;
int n,k,a[maxn];
ll dp[N][maxn];
stack<pair<int,ll>>s;

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

	cin >> n >> k;
	for(int i = 1;i <= n;i++) cin >> a[i];
	
	for(int i = 1;i <= n;i++) dp[1][i] = max(dp[1][i-1],1ll*a[i]);

	for(int i = 2;i <= k;i++){
		dp[i][i] = dp[i-1][i-1]+a[i];
		s.push({i,dp[i][i]});
		for(int j = i+1;j <= n;j++){
			while(!s.empty() and a[s.top().first] <= a[j]) s.pop();
			if(!s.empty()){
				ll nw = a[j] + dp[i-1][s.top().first];
				s.push({j,min(nw,s.top().second)});
			}
			else s.push({j,dp[i-1][i-1]+a[j]});
			dp[i][j] = s.top().second;
		}
	}
	cout<<dp[k][n];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...