Submission #218740

#TimeUsernameProblemLanguageResultExecution timeMemory
218740someone_aaK blocks (IZhO14_blocks)C++17
100 / 100
397 ms88700 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int maxk = 110;
ll n, k;
ll dp[maxn][maxk];
ll arr[maxn];

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

	cin>>n>>k;
	ll prefmax = 0LL;

	for(int i=0;i<=n;i++) {
		for(int j=0;j<=k;j++) {
			dp[i][j] = (INT_MAX);
		}
	}

	for(int i=1;i<=n;i++) {
		cin>>arr[i];
		prefmax = max(prefmax, arr[i]);
		dp[i][1] = prefmax;
	}


	for(int d=2;d<=k;d++) {
		stack<ll>dps, arrs;
		for(int i=1;i<=n;i++) {
			ll curr = dp[i-1][d-1];

			while(!arrs.empty() && arrs.top() < arr[i]) {
				curr = min(curr, dps.top());
				arrs.pop();
				dps.pop();
			}

			if(arrs.empty() || arrs.top() + dps.top() >= curr + arr[i]) {
				arrs.push(arr[i]);
				dps.push(curr);
			}

			if(i >= d) dp[i][d] = arrs.top() + dps.top();
		}
	}
	cout<<dp[n][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...