#include <iostream>
#include <stack>
using namespace std;
const int MAXN = 1e5+5;
int arr[MAXN];
int dp[110][MAXN];
int main(){
int n,k;
cin>>n>>k;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++){
dp[j][i] = 1e9;
}
}
dp[1][0] = 0;
for(int i=1;i<=n;i++){
cin>>arr[i];
dp[1][i] = 0;
dp[1][i] =max(dp[1][i-1],arr[i]);
}
dp[1][0] = 1e9;
for(int i=2;i<=k;i++){
stack<int> s1,s2;
for(int j=1;j<=n;j++){
int tempdp = dp[i-1][j-1];
while(s1.size() && s1.top()<arr[j]){
tempdp = min(tempdp,s2.top());
s1.pop();
s2.pop();
}
if(s1.empty()||tempdp+arr[j]<s1.top()+s2.top()){
s1.push(arr[j]);
s2.push(tempdp);
// cout<<arr[j]<<" "<<tempdp<<endl;
}
if(j>=i){
dp[i][j] = s1.top()+s2.top();
//cout<<dp[i][j]<<endl;
}
}
}
cout<<dp[k][n]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
3 ms |
768 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
2 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1536 KB |
Output is correct |
2 |
Correct |
20 ms |
1280 KB |
Output is correct |
3 |
Correct |
28 ms |
2176 KB |
Output is correct |
4 |
Correct |
81 ms |
14584 KB |
Output is correct |
5 |
Correct |
193 ms |
40952 KB |
Output is correct |
6 |
Correct |
46 ms |
2936 KB |
Output is correct |
7 |
Correct |
102 ms |
19324 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
23 ms |
4864 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
9 ms |
1536 KB |
Output is correct |
17 |
Correct |
19 ms |
1152 KB |
Output is correct |
18 |
Correct |
22 ms |
2176 KB |
Output is correct |
19 |
Correct |
68 ms |
14460 KB |
Output is correct |
20 |
Correct |
183 ms |
41008 KB |
Output is correct |
21 |
Correct |
39 ms |
2936 KB |
Output is correct |
22 |
Correct |
95 ms |
19424 KB |
Output is correct |
23 |
Correct |
44 ms |
2172 KB |
Output is correct |
24 |
Correct |
57 ms |
5772 KB |
Output is correct |
25 |
Correct |
180 ms |
41000 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
29 ms |
4864 KB |
Output is correct |
29 |
Correct |
7 ms |
588 KB |
Output is correct |
30 |
Correct |
12 ms |
1536 KB |
Output is correct |
31 |
Correct |
19 ms |
1152 KB |
Output is correct |
32 |
Correct |
24 ms |
2400 KB |
Output is correct |
33 |
Correct |
96 ms |
14876 KB |
Output is correct |
34 |
Correct |
266 ms |
41460 KB |
Output is correct |
35 |
Correct |
57 ms |
3320 KB |
Output is correct |
36 |
Correct |
169 ms |
19928 KB |
Output is correct |
37 |
Correct |
7 ms |
640 KB |
Output is correct |
38 |
Correct |
24 ms |
4864 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |