Submission #703983

#TimeUsernameProblemLanguageResultExecution timeMemory
703983vjudge1K blocks (IZhO14_blocks)C++17
100 / 100
145 ms40780 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
const int mod=998244353, maxn=1e5+5;
int n, k, a[maxn], mindp, val, dp[105][maxn], maxi;
stack<pii> s;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> k;
	for(int i=1; i<=n; i++) {
		cin >> a[i];
		maxi=max(maxi, a[i]);
		dp[1][i]=maxi;
	}
	for(int kel=2; kel<=k; kel++) {
		for(int i=kel; i<=n; i++) {
			mindp=dp[kel-1][i-1];
			while(!s.empty()&&a[s.top().fi]<=a[i]) {
				mindp=min(mindp, s.top().se);
				s.pop();
			}
			val=1e9;
			if(!s.empty()) val=dp[kel][s.top().fi];
			dp[kel][i]=min(val, mindp+a[i]);
			s.push({i, mindp});
		}
		while(!s.empty()) s.pop();
	}
	cout << dp[k][n] << '\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...