68982023-12-19 15:06:14111Tulipán csokrokcpp17Accepted 100/1001.046s793308 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2060 KiB
subtask212/12
3Accepted3ms2384 KiB
4Accepted3ms2460 KiB
5Accepted79ms13440 KiB
subtask325/25
6Accepted3ms2716 KiB
7Accepted3ms2688 KiB
8Accepted3ms2716 KiB
9Accepted3ms2796 KiB
10Accepted3ms2976 KiB
11Accepted3ms3184 KiB
subtask425/25
12Accepted3ms3312 KiB
13Accepted3ms3408 KiB
14Accepted3ms3496 KiB
15Accepted3ms3536 KiB
16Accepted3ms3608 KiB
17Accepted3ms3976 KiB
18Accepted3ms4268 KiB
19Accepted4ms5500 KiB
20Accepted4ms6924 KiB
subtask538/38
21Accepted917ms786448 KiB
22Accepted962ms787096 KiB
23Accepted1.001s789560 KiB
24Accepted1.046s793308 KiB
25Accepted561ms402464 KiB
26Accepted270ms167772 KiB
27Accepted93ms26924 KiB
28Accepted153ms73916 KiB
29Accepted465ms324184 KiB
30Accepted792ms637252 KiB
31Accepted990ms788564 KiB
32Accepted977ms787800 KiB