Submission #1197394

#TimeUsernameProblemLanguageResultExecution timeMemory
1197394nai0610K blocks (IZhO14_blocks)C++20
100 / 100
96 ms80452 KiB
#include <bits/stdc++.h>
#define ll int64_t
#define ld long double
using namespace std;
// 
const int maxn =1e5+5;
const int maxm=1e2+5;
const int mod = 1e9+7; // 998244353,1610612741
const ll inf = 1e18;
const ld pi = atan(1.0L)*4;
int n,k,a[maxn];
ll f[maxm][maxn],s[maxn][2];
int main() {
    // freopen("../input.inp","r",stdin);
    // freopen("output.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    clock_t start,end;
	start=clock();
	cin >>n>>k;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
		f[1][i]=a[i];
		f[1][i]=max(f[1][i],f[1][i-1]);
	}
	int tp=0;
	for (int i=1;i<=k;i++) {
		tp=0;
		for (int j=i+1;j<=n;j++){
			ll mn=f[i][j-1];
			while (tp&&s[tp][0]<a[j]) mn=min(mn,s[tp--][1]);
			if (tp==0||mn+a[j]<s[tp][0]+s[tp][1]) {
				s[++tp][0]=a[j];
				s[tp][1]=mn;
			}
			f[i+1][j]=s[tp][0]+s[tp][1];
		}
	}
	cout <<f[k][n];
	end=clock();
	ld dur=(ld)(end-start)/(ld)(CLOCKS_PER_SEC)*(1000.0L);
	cerr<<dur<<'\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...