Submission #556260

#TimeUsernameProblemLanguageResultExecution timeMemory
556260uroskK blocks (IZhO14_blocks)C++14
100 / 100
206 ms4376 KiB
// __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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...