85072024-01-19 17:05:12URAQT420Tulipán csokrokcpp17Time limit exceeded 62/1001.595s406876 KiB
// Source: https://usaco.guide/general/io

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

struct Tata{
	int w;
	long val;
};
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));

	for(int i = 1; i <= n; ++i){
		cin>>v[i];
	}

	stack<int> s;
	for(int i = 1; i <= n; ++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();
	}

	//for(int x: w)cout<<x<<' ';

	dp[0][1] = 1e9 + 1;
	for(int i = 1; i <= n; ++i){
		dp[i][1] = min(dp[i-1][1], v[i]);
	}
	dp[0][1] = 0;

	for(int j = 2; j <= k; ++j){
		stack<Tata> uperr_max;
		vector<long> lowerr_max(n+1);
		for(int i = 1; i <= n; ++i){
			if(uperr_max.empty() || uperr_max.top().w < w[i]){
				Tata temp;
				temp.w = w[i], temp.val = dp[i-1][j-1];
				uperr_max.push(temp);
			}else if(uperr_max.top().w == w[i])uperr_max.top().val = max(uperr_max.top().val, dp[i-1][j-1]);
			else if(uperr_max.top().w > w[i]){
				long temp = dp[i-1][j-1];
				while(uperr_max.top().w > w[i]){
					temp = max(temp, uperr_max.top().val);
					uperr_max.pop();
				}
				uperr_max.top().val = max(uperr_max.top().val, temp);
			}

			dp[i][j] = max(dp[w[i]-1][j], uperr_max.top().val + v[i]);

		}

	}

	cout<<dp[n][k]<<endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Accepted3ms2064 KiB
subtask212/12
3Accepted3ms2364 KiB
4Accepted3ms2388 KiB
5Accepted63ms17196 KiB
subtask325/25
6Accepted3ms2840 KiB
7Accepted3ms2796 KiB
8Accepted3ms3000 KiB
9Accepted3ms3084 KiB
10Accepted3ms3200 KiB
11Accepted3ms3100 KiB
subtask425/25
12Accepted3ms3252 KiB
13Accepted3ms3652 KiB
14Accepted3ms3620 KiB
15Accepted3ms3940 KiB
16Accepted3ms4308 KiB
17Accepted3ms4672 KiB
18Accepted4ms5108 KiB
19Accepted4ms6216 KiB
20Accepted6ms7788 KiB
subtask50/38
21Time limit exceeded1.593s395548 KiB
22Time limit exceeded1.582s396248 KiB
23Time limit exceeded1.575s397804 KiB
24Time limit exceeded1.585s400744 KiB
25Accepted1.125s406876 KiB
26Accepted453ms172428 KiB
27Accepted85ms31700 KiB
28Accepted228ms79112 KiB
29Accepted1.042s329128 KiB
30Time limit exceeded1.595s323308 KiB
31Time limit exceeded1.592s397472 KiB
32Time limit exceeded1.593s397084 KiB