6898 | 2023-12-19 15:06:14 | 111 | Tulipán csokrok | cpp17 | Accepted 100/100 | 1.046s | 793308 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pii pair<int, int>
int random(int a, int b) {
static random_device rd;
return uniform_int_distribution<int>(a, b)(rd);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef CB
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int N = 10000, K = 5000;
cin >> N >> K;
int v[N + 1];
for (int i = 1; i <= N; i++) {
v[i] = random(1, 1000000000);
cin >> v[i];
}
int dp[K + 1][N + 1];
for (int i = 1; i <= N; i++) {
dp[0][i] = -1e9;
}
pii a[N];
int b[N + 1];
for (int i = 1; i <= K; i++) {
dp[i - 1][0] = 0;
b[0] = 0;
int c = 0;
for (int j = 1; j <= N; j++) {
int m = dp[i - 1][j - 1] + v[j];
while (c > 0 && a[c - 1].first > v[j]) {
m = max(m, a[c - 1].second - a[c - 1].first + v[j]);
c--;
}
a[c++] = {v[j], m};
dp[i][j] = b[c] = max(b[c - 1], m);
}
}
cout << dp[K][N] << endl;
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 3ms | 2060 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 3ms | 2384 KiB | ||||
4 | Accepted | 3ms | 2460 KiB | ||||
5 | Accepted | 79ms | 13440 KiB | ||||
subtask3 | 25/25 | ||||||
6 | Accepted | 3ms | 2716 KiB | ||||
7 | Accepted | 3ms | 2688 KiB | ||||
8 | Accepted | 3ms | 2716 KiB | ||||
9 | Accepted | 3ms | 2796 KiB | ||||
10 | Accepted | 3ms | 2976 KiB | ||||
11 | Accepted | 3ms | 3184 KiB | ||||
subtask4 | 25/25 | ||||||
12 | Accepted | 3ms | 3312 KiB | ||||
13 | Accepted | 3ms | 3408 KiB | ||||
14 | Accepted | 3ms | 3496 KiB | ||||
15 | Accepted | 3ms | 3536 KiB | ||||
16 | Accepted | 3ms | 3608 KiB | ||||
17 | Accepted | 3ms | 3976 KiB | ||||
18 | Accepted | 3ms | 4268 KiB | ||||
19 | Accepted | 4ms | 5500 KiB | ||||
20 | Accepted | 4ms | 6924 KiB | ||||
subtask5 | 38/38 | ||||||
21 | Accepted | 917ms | 786448 KiB | ||||
22 | Accepted | 962ms | 787096 KiB | ||||
23 | Accepted | 1.001s | 789560 KiB | ||||
24 | Accepted | 1.046s | 793308 KiB | ||||
25 | Accepted | 561ms | 402464 KiB | ||||
26 | Accepted | 270ms | 167772 KiB | ||||
27 | Accepted | 93ms | 26924 KiB | ||||
28 | Accepted | 153ms | 73916 KiB | ||||
29 | Accepted | 465ms | 324184 KiB | ||||
30 | Accepted | 792ms | 637252 KiB | ||||
31 | Accepted | 990ms | 788564 KiB | ||||
32 | Accepted | 977ms | 787800 KiB |