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;
const int N = 3005, oo = 1e9;
int n, k, a[N], dp[N][N];
int main() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
dp[i][j] = oo;
}
}
stack < int > st;
dp[0][0] = 0;
for(int i = 1; i <= n; ++i) {
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
int x = 0;
if(!st.empty()) x = st.top();
for(int j = 1; j <= min(i, k); ++j) {
dp[i][j] = dp[x][j];
for(int p = x; p < i; ++p) {
dp[i][j] = min(dp[i][j], dp[p][j - 1] + a[i]);
}
}
st.push(i);
}
cout << dp[n][k] << '\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... |