#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ins insert
#define pb push_back
#define int long long int
#define pii pair<int, int>
#define endl '\n'
#define drop(x) cout<<(x)<<endl; return;
#define all(x) x.begin(),x.end()
#define hash z0hashp
#define div z0dvp
const int mod = 1e9 +7, sze = 1e6+23, inf = INT_MAX, P = 1453;
int dp[105][100005];
void opt1z(){ // https://codeforces.com/blog/entry/18866?#comment-238114
int n,k;
cin>>n>>k;
vector<int> arr(n+1);
int mx =0;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++){
dp[j][i]=inf;
}
}
for(int i=1;i<=n;i++){
cin>>arr[i];
mx=max(mx,arr[i]);
dp[1][i]=mx;
}
for(int i=2;i<=k;i++){
stack<int> s1,s2;
for(int j=1;j<=n;j++){
int mdp = dp[i-1][j-1];
while(!s1.empty() && s1.top() < arr[j]){
mdp = min(mdp,s2.top());
s1.pop();
s2.pop();
}
if(s1.empty() || mdp + arr[j] < s1.top() + s2.top()){
s1.push(arr[j]);
s2.push(mdp);
}
if(j >= i){
dp[i][j] = s1.top() + s2.top();
}
}
}
drop(dp[k][n]);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
// cin>>tt;
while(tt--){
opt1z();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
1 ms |
12636 KB |
Output is correct |
35 |
Correct |
1 ms |
12636 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
12636 KB |
Output is correct |
38 |
Correct |
2 ms |
16732 KB |
Output is correct |
39 |
Correct |
4 ms |
39260 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
0 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
2396 KB |
Output is correct |
43 |
Correct |
0 ms |
2396 KB |
Output is correct |
44 |
Correct |
0 ms |
2396 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
0 ms |
2396 KB |
Output is correct |
47 |
Correct |
0 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4440 KB |
Output is correct |
51 |
Correct |
3 ms |
20828 KB |
Output is correct |
52 |
Correct |
2 ms |
20828 KB |
Output is correct |
53 |
Correct |
4 ms |
51804 KB |
Output is correct |
54 |
Correct |
4 ms |
49756 KB |
Output is correct |
55 |
Correct |
4 ms |
43356 KB |
Output is correct |
56 |
Correct |
4 ms |
49756 KB |
Output is correct |
57 |
Correct |
4 ms |
49752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
1 ms |
12636 KB |
Output is correct |
35 |
Correct |
1 ms |
12636 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
12636 KB |
Output is correct |
38 |
Correct |
2 ms |
16732 KB |
Output is correct |
39 |
Correct |
4 ms |
39260 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
0 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
2396 KB |
Output is correct |
43 |
Correct |
0 ms |
2396 KB |
Output is correct |
44 |
Correct |
0 ms |
2396 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
0 ms |
2396 KB |
Output is correct |
47 |
Correct |
0 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4440 KB |
Output is correct |
51 |
Correct |
3 ms |
20828 KB |
Output is correct |
52 |
Correct |
2 ms |
20828 KB |
Output is correct |
53 |
Correct |
4 ms |
51804 KB |
Output is correct |
54 |
Correct |
4 ms |
49756 KB |
Output is correct |
55 |
Correct |
4 ms |
43356 KB |
Output is correct |
56 |
Correct |
4 ms |
49756 KB |
Output is correct |
57 |
Correct |
4 ms |
49752 KB |
Output is correct |
58 |
Correct |
4 ms |
21080 KB |
Output is correct |
59 |
Correct |
3 ms |
2908 KB |
Output is correct |
60 |
Correct |
5 ms |
7004 KB |
Output is correct |
61 |
Correct |
28 ms |
50336 KB |
Output is correct |
62 |
Correct |
79 ms |
80220 KB |
Output is correct |
63 |
Correct |
7 ms |
5468 KB |
Output is correct |
64 |
Correct |
35 ms |
38236 KB |
Output is correct |
65 |
Correct |
3 ms |
20824 KB |
Output is correct |
66 |
Correct |
3 ms |
39260 KB |
Output is correct |
67 |
Correct |
3 ms |
39344 KB |
Output is correct |
68 |
Correct |
3 ms |
39260 KB |
Output is correct |
69 |
Correct |
0 ms |
2396 KB |
Output is correct |
70 |
Correct |
1 ms |
8540 KB |
Output is correct |
71 |
Correct |
17 ms |
49360 KB |
Output is correct |
72 |
Correct |
1 ms |
2648 KB |
Output is correct |
73 |
Correct |
4 ms |
21084 KB |
Output is correct |
74 |
Correct |
3 ms |
2908 KB |
Output is correct |
75 |
Correct |
5 ms |
7004 KB |
Output is correct |
76 |
Correct |
29 ms |
56156 KB |
Output is correct |
77 |
Correct |
77 ms |
80220 KB |
Output is correct |
78 |
Correct |
8 ms |
5464 KB |
Output is correct |
79 |
Correct |
35 ms |
38236 KB |
Output is correct |
80 |
Correct |
6 ms |
3420 KB |
Output is correct |
81 |
Correct |
12 ms |
11612 KB |
Output is correct |
82 |
Correct |
88 ms |
80220 KB |
Output is correct |
83 |
Correct |
1 ms |
2396 KB |
Output is correct |
84 |
Correct |
1 ms |
8540 KB |
Output is correct |
85 |
Correct |
14 ms |
49340 KB |
Output is correct |
86 |
Correct |
1 ms |
2648 KB |
Output is correct |
87 |
Correct |
5 ms |
21124 KB |
Output is correct |
88 |
Correct |
3 ms |
2908 KB |
Output is correct |
89 |
Correct |
6 ms |
7472 KB |
Output is correct |
90 |
Correct |
46 ms |
46380 KB |
Output is correct |
91 |
Correct |
132 ms |
81168 KB |
Output is correct |
92 |
Correct |
8 ms |
6052 KB |
Output is correct |
93 |
Correct |
58 ms |
39048 KB |
Output is correct |
94 |
Correct |
1 ms |
2652 KB |
Output is correct |
95 |
Correct |
10 ms |
47280 KB |
Output is correct |
96 |
Correct |
1 ms |
8540 KB |
Output is correct |
97 |
Correct |
0 ms |
2396 KB |
Output is correct |
98 |
Correct |
1 ms |
4444 KB |
Output is correct |
99 |
Correct |
1 ms |
4444 KB |
Output is correct |