# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1125640 | baoheyhey | K blocks (IZhO14_blocks) | C++17 | 0 ms | 0 KiB |
#include<bits/stdc++.h>
#define ll long long
#define FOR(i, l, r) for(int i = (l), _r = r; i <= _r; ++i)
#define FOD(i, l, r) for(int i = (l), _r = r; i >= _r; --i)
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define el "\n"
#define bit(a, x) (a >> x & 1)
#define X first
#define Y second
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define cntbit(x) __builtin_popcountll(x)
#define uni(a) sort(ALL(a)), a.resize(unique(ALL(a)) - a.begin())
#define sz(a) a.size()
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const int maxn = 1e5 + 5;
ll dp[maxn][105];
ll n, a[maxn], k;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #define task "task"
// if(fopen(task".inp", "r")){
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
// }
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof(dp));
dp[0][1] = 0;
FOR(i, 1, n) dp[i][1] = max(dp[i - 1][1], a[i]);
FOR(i, 2, k){
stack<pll> st;
FOR(j, i, n){
int minn = dp[j - 1][i - 1];
while(sz(st) && a[st.top().Y] <= a[j]){
minn = min(minn, st.top().X);
st.pop();
}
dp[j][i] = min(dp[(sz(st) ? st.top().Y : 0)][i], minn + a[j]);
st.push({minn, j});
}
}
cout << dp[n][k];
cerr << "Time elapsed: " << TIME << " s" << el;
}