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;
int main () {
int n, k; cin >> n >> k;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<pair<long long, long long>>> dp(n, vector<pair<long long, long long>>(k, {1e18, 0}));
dp[0][0] = {a[0], a[0]};
for (int j = 0; j < k; j++) {
for (int i = 0; i < n-1; i++) {
long long temp = dp[i][j].first + max((long long)(0), a[i + 1] - a[i]);
if (temp < dp[i + 1][j].first) {
dp[i + 1][j].first = temp;
dp[i + 1][j].second = max(a[i + 1], dp[i][j].second);
}
else if (temp == dp[i + 1][j].first) {
dp[i + 1][j].second = max({a[i + 1], dp[i][j].second, dp[i + 1][j].second});
}
if (j < k - 1) {
if (dp[i][j].first + a[i + 1] < dp[i + 1][j + 1].first) {
dp[i + 1][j + 1].first = dp[i][j].first + a[i + 1];
dp[i + 1][j + 1].second = a[i + 1];
}
else if (dp[i][j].first + a[i + 1] == dp[i + 1][j + 1].first) {
dp[i + 1][j + 1].second = max(a[i + 1], dp[i + 1][j + 1].second);
}
}
}
}
cout << dp[n-1][k-1].first << '\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... |