This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<int64_t> f(n + 1, 0);
for (int i = 1; i <= n; i++) f[i] = max<int64_t>(f[i - 1], a[i]);
vector<int64_t> g(n + 1);
f[0] = 0;
for (int i = 1; i < k; i++) {
vector<int64_t> nf(n + 1, 1e18);
stack<int> st;
for (int j = 1; j <= n; j++) {
int64_t mn = 1e18;
while (st.size() && a[st.top()] <= a[j]) {
mn = min(mn, g[st.top()]);
st.pop();
}
if (st.size()) nf[j] = min(nf[st.top()], f[st.top()] + a[j]);
nf[j] = min(nf[j], mn + a[j]);
g[j] = min(f[j], mn);
st.emplace(j);
}
f = nf;
}
cout << f[n];
}
| # | 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... |