This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 1000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 100005
#define maxk 105
#define lg 25
ll n,k;
ll a[maxn];
ll b[maxn];
ll dp[2][maxn];
ll dpmin[2][maxn];
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> k;
for(ll i = 1;i<=n;i++) cin >> a[i];
stack<pll> st;
stack<ll> mini;
fill(dp[0],dp[0]+n+1,llinf);
dp[0][0] = 0;
for(ll j = 1;j<=k;j++){
while(sz(st)) st.pop();
while(sz(mini)) mini.pop();
ll e = (j&1);
dp[e][0] = llinf;
for(ll i = 1;i<=n;i++){
dp[e][i] = dp[e^1][i-1];
while(sz(st)&&a[i]>=st.top().fi){
dp[e][i] = min(dp[e][i],st.top().sc);
st.pop();
mini.pop();
}
st.push({a[i],dp[e][i]});
dp[e][i]+=a[i];
if(sz(mini)) dp[e][i] = min(dp[e][i],mini.top());
mini.push(dp[e][i]);
}
for(ll i = 0;i<=n;i++) dp[(j&1)^1][i] = dp[j&1][i];
}
cout<<dp[k&1][n]<<endl;
return 0;
}
/*
5 1
1 2 3 4 5
out: 5
5 2
1 2 3 4 5
out: 6
5 4
3 1 3 5 1
out: 10
7 4
10 7 4 1 8 4 1
10 5
5 9 2 9 4 6 9 3 7 8
*/
# | 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... |