8508 | 2024-01-19 18:15:43 | URAQT420 | Tulipán csokrok | cpp17 | Time limit exceeded 62/100 | 1.588s | 790804 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1964 KiB | ||||
2 | Accepted | 3ms | 2108 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 2ms | 2096 KiB | ||||
4 | Accepted | 3ms | 2100 KiB | ||||
5 | Accepted | 59ms | 15276 KiB | ||||
subtask3 | 25/25 | ||||||
6 | Accepted | 3ms | 2336 KiB | ||||
7 | Accepted | 3ms | 2468 KiB | ||||
8 | Accepted | 2ms | 2480 KiB | ||||
9 | Accepted | 3ms | 2612 KiB | ||||
10 | Accepted | 2ms | 2696 KiB | ||||
11 | Accepted | 3ms | 2828 KiB | ||||
subtask4 | 25/25 | ||||||
12 | Accepted | 3ms | 3080 KiB | ||||
13 | Accepted | 3ms | 3428 KiB | ||||
14 | Accepted | 3ms | 3648 KiB | ||||
15 | Accepted | 3ms | 3844 KiB | ||||
16 | Accepted | 3ms | 3872 KiB | ||||
17 | Accepted | 3ms | 4100 KiB | ||||
18 | Accepted | 4ms | 4516 KiB | ||||
19 | Accepted | 4ms | 5960 KiB | ||||
20 | Accepted | 4ms | 7456 KiB | ||||
subtask5 | 0/38 | ||||||
21 | Accepted | 864ms | 786848 KiB | ||||
22 | Accepted | 1.187s | 788036 KiB | ||||
23 | Accepted | 1.485s | 790804 KiB | ||||
24 | Time limit exceeded | 1.588s | 399672 KiB | ||||
25 | Accepted | 783ms | 404904 KiB | ||||
26 | Accepted | 333ms | 170312 KiB | ||||
27 | Accepted | 82ms | 29816 KiB | ||||
28 | Accepted | 216ms | 77116 KiB | ||||
29 | Accepted | 800ms | 327156 KiB | ||||
30 | Accepted | 1.361s | 640116 KiB | ||||
31 | Accepted | 1.156s | 789712 KiB | ||||
32 | Accepted | 1.256s | 788932 KiB |