Submission #91540

#TimeUsernameProblemLanguageResultExecution timeMemory
91540NordwayK blocks (IZhO14_blocks)C++14
100 / 100
365 ms48352 KiB
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const int INF = INT_MAX;
const int mod = 998244353;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 2e5+5;
const int M = 1e2+1;

int a[N],dp[N][M];

int main(){
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][1]=max(dp[i-1][1],a[i]);
	}
	for(int j=2;j<=k;j++){
		stack<pii>s;
		for(int i=j;i<=n;i++){
			int mn=dp[i-1][j-1];
			while(!s.empty()){
				if(a[s.top().x]>a[i])break;
				mn=min(mn,s.top().y);
				s.pop();
			}
			dp[i][j]=min(mn+a[i],!s.empty()?dp[s.top().x][j]:INF);
			s.push(mp(i,mn));
		}
	}
	cout<<dp[n][k];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...