8509 | 2024-01-19 18:19:22 | URAQT420 | Tulipán csokrok | cpp17 | Time limit exceeded 62/100 | 1.593s | 790708 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 3ms | 2048 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 3ms | 2284 KiB | ||||
4 | Accepted | 3ms | 2516 KiB | ||||
5 | Accepted | 28ms | 15576 KiB | ||||
subtask3 | 25/25 | ||||||
6 | Accepted | 3ms | 2740 KiB | ||||
7 | Accepted | 3ms | 2952 KiB | ||||
8 | Accepted | 3ms | 3052 KiB | ||||
9 | Accepted | 3ms | 3164 KiB | ||||
10 | Accepted | 3ms | 3176 KiB | ||||
11 | Accepted | 3ms | 3164 KiB | ||||
subtask4 | 25/25 | ||||||
12 | Accepted | 3ms | 3200 KiB | ||||
13 | Accepted | 3ms | 3256 KiB | ||||
14 | Accepted | 3ms | 3400 KiB | ||||
15 | Accepted | 3ms | 3440 KiB | ||||
16 | Accepted | 3ms | 3772 KiB | ||||
17 | Accepted | 3ms | 3984 KiB | ||||
18 | Accepted | 3ms | 4184 KiB | ||||
19 | Accepted | 4ms | 5228 KiB | ||||
20 | Accepted | 4ms | 6916 KiB | ||||
subtask5 | 0/38 | ||||||
21 | Accepted | 953ms | 786560 KiB | ||||
22 | Accepted | 1.151s | 787584 KiB | ||||
23 | Accepted | 1.47s | 790708 KiB | ||||
24 | Time limit exceeded | 1.592s | 399336 KiB | ||||
25 | Accepted | 995ms | 404724 KiB | ||||
26 | Accepted | 395ms | 170100 KiB | ||||
27 | Accepted | 50ms | 29440 KiB | ||||
28 | Accepted | 180ms | 76356 KiB | ||||
29 | Accepted | 769ms | 326580 KiB | ||||
30 | Time limit exceeded | 1.593s | 321280 KiB | ||||
31 | Accepted | 1.266s | 789196 KiB | ||||
32 | Accepted | 1.177s | 788348 KiB |