85082024-01-19 18:15:43URAQT420Tulipán csokrokcpp17Time limit exceeded 62/1001.588s790804 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, k; cin>>n>>k;
	vector<long> v(n+1);
	vector<int> w(n+1);
	vector<vector<long> > dp(n+1, vector<long>(k+1));

	dp[0][1] = 1e9 + 1;
	stack<int> s;
	for(int i = 1; i <= n; ++i){
		cin>>v[i];
		if(s.empty())s.push(i);
		else{
			while(v[i-1] > v[i] && v[s.top()-1] > v[i]){
				s.pop();
			}
			if(v[i-1] < v[i])s.push(i);
		}
		w[i] = s.top();
		dp[i][1] = min(dp[i-1][1], v[i]);
	}

	dp[0][1] = 0;

	for(int j = 2; j <= k; ++j){
		for(int i = j; i <= n; ++i){
			if(w[i] == w[i-1]){
				dp[i-1][j-1] = max(dp[i-1][j-1], dp[i-2][j-1]);
			}else if(w[i] < w[i-1]){
				int temp = i-1;
				while(temp && w[temp] != w[i]){
					dp[i-1][j-1] = max(dp[i-1][j-1], dp[temp-1][j-1]);
					temp = w[temp] - 1;
				}
				dp[i-1][j-1] = max(dp[i-1][j-1], dp[temp-1][j-1]);
			}
			dp[i][j] = max(dp[w[i]-1][j], dp[i-1][j-1] + v[i]);

		}

	}

	cout<<dp[n][k]<<endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1964 KiB
2Accepted3ms2108 KiB
subtask212/12
3Accepted2ms2096 KiB
4Accepted3ms2100 KiB
5Accepted59ms15276 KiB
subtask325/25
6Accepted3ms2336 KiB
7Accepted3ms2468 KiB
8Accepted2ms2480 KiB
9Accepted3ms2612 KiB
10Accepted2ms2696 KiB
11Accepted3ms2828 KiB
subtask425/25
12Accepted3ms3080 KiB
13Accepted3ms3428 KiB
14Accepted3ms3648 KiB
15Accepted3ms3844 KiB
16Accepted3ms3872 KiB
17Accepted3ms4100 KiB
18Accepted4ms4516 KiB
19Accepted4ms5960 KiB
20Accepted4ms7456 KiB
subtask50/38
21Accepted864ms786848 KiB
22Accepted1.187s788036 KiB
23Accepted1.485s790804 KiB
24Time limit exceeded1.588s399672 KiB
25Accepted783ms404904 KiB
26Accepted333ms170312 KiB
27Accepted82ms29816 KiB
28Accepted216ms77116 KiB
29Accepted800ms327156 KiB
30Accepted1.361s640116 KiB
31Accepted1.156s789712 KiB
32Accepted1.256s788932 KiB