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
using namespace std;
const int N=1e5+3;
int n,a[N],k;
ll f[N][103];
stack<ll> st,st1;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
For(i,1,n) cin >> a[i];
For(i,1,n) f[i][1]=max(f[i-1][1],1LL*a[i]);
For(j,2,k) {
while(sz(st)) st.pop();
while(sz(st1)) st1.pop();
For(i,j,n) {
ll tmp=f[i-1][j-1];
f[i][j]=f[i-1][j-1]+a[i];
while(sz(st) && a[st.top()]<=a[i]) {
tmp=min(tmp,st1.top());
st1.pop();
st.pop();
}
if (st1.empty() || (st1.top()+a[st.top()]>=tmp+a[i])) {
st1.push(tmp);
st.push(i);
}
f[i][j]=min(f[i][j],st1.top()+a[st.top()]);
}
}
cout << f[n][k] << endl;
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... |