Submission #168067

#TimeUsernameProblemLanguageResultExecution timeMemory
168067GioChkhaidzeK blocks (IZhO14_blocks)C++14
100 / 100
246 ms42872 KiB
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N=1e5+5,K=105;
int n,k;
int a[N],dp[N][K];
stack < pair < int , int > > st ;
main ( ){
	scanf("%d%d",&n,&k);
	
	for (int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
		dp[i][1]=max(dp[i-1][1],a[i]);
	}
	
	for (int j=2; j<=k; j++) {
		while (st.size()) st.pop();
		for (int i=j; i<=n; i++) {
			int h=a[i],up=dp[i-1][j-1];
			while (st.size() && st.top().F<=h) { up=min(up,st.top().S); st.pop(); } 
			if (!st.size() || st.top().F+st.top().S>=up+h) st.push({h,up});	
			dp[i][j]=st.top().F+st.top().S;
		}
	}
	
	printf("%d\n",dp[n][k]);
}

Compilation message (stderr)

blocks.cpp:9:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main ( ){
        ^
blocks.cpp: In function 'int main()':
blocks.cpp:10:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&k);
  ~~~~~^~~~~~~~~~~~~~
blocks.cpp:13:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);
   ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...