# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1123430 | VinhLuu | K blocks (IZhO14_blocks) | C++20 | 121 ms | 4504 KiB |
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define fi first
#define se second
#define pot(x, y) lower_bound(x.begin(), x.end(), y) - x.begin() + 1
using namespace std;
typedef pair<int,int> pii;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, kw;
ll a[N], pf[N], mx[N], f[2][N];
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> kw;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
}
int pre = 0, nx = 1;
ll _tmp = 0;
for(int i = 1; i <= n; i ++) {
_tmp = max(_tmp, a[i]);
f[pre][i] = _tmp;
}
pf[0] = 1e16;
for(int k = 2; k <= kw; k ++) {
stack<int> st;
for(int i = k; i <= n; i ++) {
mx[i] = f[pre][i - 1];
while(!st.empty() && a[st.top()] <= a[i]) {
mx[i] = min(mx[i], mx[st.top()]);
st.pop();
}
int last = (!st.empty() ? st.top() : 0);
pf[i] = min(mx[i] + a[i], pf[last]);
f[nx][i] = pf[i];
st.push(i);
}
swap(pre, nx);
}
cout << f[pre][n];
}
Compilation message (stderr)
# | 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... |