이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |