Submission #500824

#TimeUsernameProblemLanguageResultExecution timeMemory
500824keta_tsimakuridzeK blocks (IZhO14_blocks)C++14
100 / 100
296 ms84644 KiB
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, K = 105, inf = 1e18; // !
int t, n, k, dp[N][K], a[N];
stack<pii> st;
main(){
	cin >> n >> k;
	for(int i = 1; i <= n; i++) cin >> a[i], dp[i][0] = max(a[i], dp[i - 1][0]);
	st.push({dp[n][0], 0});
	for(int i = 1; i < k; i++) {
		while(st.size()) st.pop();
		for(int j = i + 1; j <= n; j++) {
			int last = inf;
			while(st.size() && st.top().f <= a[j]) {
				last = min(last, st.top().s);
				// x1 < x2
				// x1 + y1 < x2 + y2
				st.pop();
			}
			last = min(last, dp[j - 1][i - 1]);
			if(!st.size() || st.top().f + st.top().s > last + a[j]) st.push({a[j], last});
			dp[j][i] = st.top().f + st.top().s;
		}
	}
	cout << st.top().f + st.top().s;
}

Compilation message (stderr)

blocks.cpp:10:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   10 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...