Submission #904305

#TimeUsernameProblemLanguageResultExecution timeMemory
90430512345678K blocks (IZhO14_blocks)C++17
100 / 100
252 ms200276 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
const int nx=5e5+5, kx=105;
int n, k, v[nx], dp[kx][nx], x, mx;
 
int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>k;
    v[0]=1e9;
    for (int i=1; i<=n; i++) cin>>v[i], mx=max(mx, v[i]), dp[1][i]=mx;
    for (int i=2; i<=k; i++)
    {
        stack<pair<int, int>> s;
        for (int j=i; j<=n; j++)
        {
            int vl=dp[i-1][j-1];
            while (!s.empty()&&s.top().first<=v[j]) vl=min(vl, s.top().second), s.pop();
            dp[i][j]=vl+v[j];
            if (!s.empty()) dp[i][j]=min(dp[i][j], s.top().first+s.top().second);
            if (s.empty()) s.push({v[j], vl});
            else if (s.top().first+s.top().second>v[j]+vl) s.push({v[j], vl});
            //cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
        }
    }
    cout<<dp[k][n];
}

/*
3 2
7 6 2
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...