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;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
int32_t main() {
#ifdef LOCAL
freopen("/tmp/input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n, k;
cin >> n >> k;
vector<int> a(n);
cin >> a;
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, infll));
dp[0][0] = 0;
for (int j = 1; j <= k; ++j) {
vector<int> st;
vector<ll> mn;
vector<ll> pref;
for (int i = 1; i <= n; ++i) {
ll cur = dp[j - 1][i - 1];
while (!st.empty() && a[st.back()] <= a[i - 1]) {
cur = min(cur, mn.back());
st.pop_back();
pref.pop_back();
}
st.push_back(i - 1);
mn.push_back(cur);
if (pref.empty()) {
pref.push_back(cur + a[i - 1]);
} else {
pref.push_back(min(pref.back(), cur + a[i - 1]));
}
dp[j][i] = pref.back();
}
}
cout << dp[k][n] << "\n";
return 0;
}
# | 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... |