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>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb emplace_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=1e5+3;
const ll INF=1e17;
int n,k,a[N],l[N],r[N];
ll f[N][104];
stack<int> skull;
vector<ll> in[N],out[N];
multiset<ll> st;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
For(i,1,n) cin >> a[i];
fill(l,l+n+1,-1);
fill(r,r+n+1,n+1);
For(i,1,n) {
while(sz(skull) && a[skull.top()]<a[i]) {
r[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
while(sz(skull)) skull.pop();
ForD(i,n,1) {
while(sz(skull) && a[skull.top()]<a[i]) {
l[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
For(i,1,n) l[i]++,r[i]--;
For(i,0,n)
For(j,1,k) f[i][j]=INF;
f[1][1]=a[1];
For(i,2,n) f[i][1]=max(1LL*a[i],f[i-1][1]);
For(j,2,k) {
For(i,1,n) in[i].clear(),out[i].clear();
For(i,j,n) {
for(auto el: in[i]) st.insert(el);
for(auto el: out[i]) st.erase(st.find(el));
ll cur=f[max(l[i]-1,j-1)][j-1]+a[i];
f[i][j]=min(f[i][j],cur);
if (sz(st)) f[i][j]=min(f[i][j],*st.begin());
if (r[i]>i) {
in[i+1].pb(cur);
out[r[i]+1].pb(cur);
}
// For(k,j,i) f[i][j]=min(f[i][j],f[k-1][j-1]+get(k,i));
}
}
cout << f[n][k];
return 0;
}
# | 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... |