85092024-01-19 18:19:22URAQT420Tulipán csokrokcpp17Time limit exceeded 62/1001.593s790708 KiB
// Source: https://usaco.guide/general/io

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

	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
1Accepted3ms1828 KiB
2Accepted3ms2048 KiB
subtask212/12
3Accepted3ms2284 KiB
4Accepted3ms2516 KiB
5Accepted28ms15576 KiB
subtask325/25
6Accepted3ms2740 KiB
7Accepted3ms2952 KiB
8Accepted3ms3052 KiB
9Accepted3ms3164 KiB
10Accepted3ms3176 KiB
11Accepted3ms3164 KiB
subtask425/25
12Accepted3ms3200 KiB
13Accepted3ms3256 KiB
14Accepted3ms3400 KiB
15Accepted3ms3440 KiB
16Accepted3ms3772 KiB
17Accepted3ms3984 KiB
18Accepted3ms4184 KiB
19Accepted4ms5228 KiB
20Accepted4ms6916 KiB
subtask50/38
21Accepted953ms786560 KiB
22Accepted1.151s787584 KiB
23Accepted1.47s790708 KiB
24Time limit exceeded1.592s399336 KiB
25Accepted995ms404724 KiB
26Accepted395ms170100 KiB
27Accepted50ms29440 KiB
28Accepted180ms76356 KiB
29Accepted769ms326580 KiB
30Time limit exceeded1.593s321280 KiB
31Accepted1.266s789196 KiB
32Accepted1.177s788348 KiB