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 = 1e5 + 10, K = 1e2 + 10;
int n, k;
int a[N];
int dp[N][K];
int pref[N];
int lst[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
stack<int> stk;
for(int i = 1; i <= n; i++) {
while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
if(stk.empty()) lst[i] = 0;
else lst[i] = stk.top();
stk.push(i);
}
int mx = 0;
for(int i = 1; i <= n; i++) {
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for(int i = 0; i <= n; i++) for(int j = 2; j <= k; j++) dp[i][j] = 1e9;
pref[0] = 1e9;
for(int j = 2; j <= k; j++) {
for(int i = j; i <= n; i++) {
pref[i] = min(pref[lst[i]], dp[lst[i] + 1][j - 1] + a[i]);
dp[i][j] = pref[i];
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= k; j++) cout << dp[i][j] << " ";
// cout << "\n";
// }
cout << dp[n][k];
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... |