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;
stack<int> s;
int main() {
int n, k;
cin >> n >> k;
int arr[n + 1];
for (int i = 1; i <= n; ++i) cin >> arr[i];
int dp[n + 1][k + 1];
int chosen[n + 1][k + 1];
dp[0][1] = 0;
for (int i = 1; i <= n; ++i)
dp[i][1] = max(arr[i], dp[i - 1][1]), chosen[i][1] = dp[i][1];
for (int j = 2; j <= k; ++j) {
while (s.size() > 0) s.pop();
for (int i = j; i <= n; ++i) {
dp[i][j] = dp[i - 1][j - 1] + arr[i];
chosen[i][j] = arr[i];
while (s.size() > 0) {
int idx = s.top();
if (arr[idx] <= arr[i]) {
if (dp[idx][j] - chosen[idx][j] +
max(chosen[idx][j], arr[i]) <
dp[i][j]) {
dp[i][j] = dp[idx][j] - chosen[idx][j] +
max(chosen[idx][j], arr[i]);
}
s.pop();
} else
break;
}
if (s.size() > 0) {
int idx = s.top();
if (dp[idx][j] < dp[i][j]) {
dp[i][j] = dp[idx][j];
chosen[i][j] = chosen[idx][j];
}
}
s.push(i);
}
}
cout << dp[n][k] << '\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... |