#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod = (ll)1e9 + 7;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
void solve() {
int n, k;
cin >> n >> k;
vector<int> arr(n);
cin >> arr;
vector<vector<pair<int, int>>> dp(n + 1, vector<pair<int, int>>(k + 1, {1000000000, -1000000000}));
dp[0][0] = {0, 0};
for (int i = 0; i < n; i++) {
for (int j = 0; j <= min(k, i); j++) {
if (dp[i][j].second >= arr[i]) {
if (dp[i + 1][j].first > dp[i][j].first) {
dp[i + 1][j] = dp[i][j];
} else if (dp[i + 1][j].first == dp[i][j].first and dp[i + 1][j].second < dp[i][j].second) {
dp[i + 1][j] = dp[i][j];
}
} else {
ll d = arr[i] - dp[i][j].second;
if (dp[i + 1][j].first > dp[i][j].first + d) {
dp[i + 1][j].first = dp[i][j].first + d;
dp[i + 1][j].second = arr[i];
} else if (dp[i + 1][j].first == dp[i][j].first + d and dp[i + 1][j].second < arr[i]) {
dp[i + 1][j].second = arr[i];
}
}
if (i + 1 == j or j == k) {
continue;
}
if (dp[i + 1][j + 1].first > dp[i][j].first + arr[i] or (dp[i + 1][j + 1].first == dp[i][j].first + arr[i] and dp[i + 1][j + 1].second < arr[i])) {
dp[i + 1][j + 1] = {dp[i][j].first + arr[i], arr[i]};
}
}
}
cout << dp[n][k].first;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
304 KB |
Output is correct |
11 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
2816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |