This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=1e5+9;
int a[maxn],lep[maxn];
long long f[maxn][109];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("MAXK.INP","r",stdin);
//freopen("MAXK.OUT","w",stdout);
int n,k;
cin>>n>>k;
a[0]=1e9+1;
for1(i,1,n)cin>>a[i];
stack<int>run;
run.push(0);
for1(i,1,n){
while (!run.empty()&&a[run.top()]<a[i])run.pop();
lep[i]=run.top();
run.push(i);
}
int mx=0;
for1(i,1,n){
mx=max(mx,a[i]);
f[i][1]=mx;
}
for1(j,2,k){
stack<int>id;
stack<long long>best;
stack<long long>hjhj;
for1(i,j,n){
long long nw=f[i-1][j-1];
while (!id.empty()&&id.top()>lep[i]){
best.pop();
nw=min(nw,hjhj.top());
hjhj.pop();
id.pop();
}
id.push(i);
hjhj.push(nw);
if (best.empty())best.push(nw+a[i]);
else best.push(min(best.top(),nw+a[i]));
f[i][j]=best.top();
}
}
cout<<f[n][k];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |