This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1;
int big = 1e9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int dp[n + 1][k + 1], a[n], greater[n];
for (int &x : a)
cin >> x;
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()] < a[i])
greater[st.top()] = i, st.pop();
st.push(i);
}
while (!st.empty())
greater[st.top()] = n, st.pop();
for (int i = 0; i < k; i++)
dp[n][i] = big;
for (int i = 0; i < n; i++)
dp[i][k] = big;
dp[n][k] = 0;
for (int i = n - 1; i >= 0; i--)
for (int j = k - 1; j >= 0; j--)
dp[i][j] = min(dp[greater[i]][j], a[i] + dp[i + 1][j + 1]);
cout << dp[0][0] << '\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... |