#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 = LLONG_MAX, P = 1453;
int dp[105][100005];
int arr[sze];
void opt1z(){ // https://codeforces.com/blog/entry/18866?#comment-238114
int n,k;
cin>>n>>k;
int mx = 0;
memset(dp,0x3f,sizeof(dp));
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 |
18 ms |
84060 KB |
Output is correct |
2 |
Correct |
17 ms |
84056 KB |
Output is correct |
3 |
Correct |
18 ms |
83972 KB |
Output is correct |
4 |
Correct |
18 ms |
84060 KB |
Output is correct |
5 |
Correct |
18 ms |
84060 KB |
Output is correct |
6 |
Correct |
18 ms |
84060 KB |
Output is correct |
7 |
Correct |
20 ms |
84312 KB |
Output is correct |
8 |
Correct |
20 ms |
84056 KB |
Output is correct |
9 |
Correct |
17 ms |
84060 KB |
Output is correct |
10 |
Correct |
19 ms |
83952 KB |
Output is correct |
11 |
Correct |
18 ms |
84108 KB |
Output is correct |
12 |
Correct |
17 ms |
84060 KB |
Output is correct |
13 |
Correct |
19 ms |
83960 KB |
Output is correct |
14 |
Correct |
19 ms |
84060 KB |
Output is correct |
15 |
Correct |
19 ms |
84060 KB |
Output is correct |
16 |
Correct |
28 ms |
84052 KB |
Output is correct |
17 |
Correct |
18 ms |
84060 KB |
Output is correct |
18 |
Correct |
19 ms |
84060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
84060 KB |
Output is correct |
2 |
Correct |
21 ms |
83888 KB |
Output is correct |
3 |
Correct |
22 ms |
84008 KB |
Output is correct |
4 |
Correct |
22 ms |
84056 KB |
Output is correct |
5 |
Correct |
23 ms |
84060 KB |
Output is correct |
6 |
Correct |
23 ms |
84088 KB |
Output is correct |
7 |
Correct |
22 ms |
84060 KB |
Output is correct |
8 |
Correct |
22 ms |
84048 KB |
Output is correct |
9 |
Correct |
32 ms |
84056 KB |
Output is correct |
10 |
Correct |
22 ms |
84056 KB |
Output is correct |
11 |
Correct |
22 ms |
83928 KB |
Output is correct |
12 |
Correct |
23 ms |
84060 KB |
Output is correct |
13 |
Correct |
21 ms |
83968 KB |
Output is correct |
14 |
Correct |
32 ms |
84060 KB |
Output is correct |
15 |
Correct |
24 ms |
83912 KB |
Output is correct |
16 |
Correct |
25 ms |
84060 KB |
Output is correct |
17 |
Correct |
23 ms |
84060 KB |
Output is correct |
18 |
Correct |
23 ms |
84112 KB |
Output is correct |
19 |
Correct |
19 ms |
84112 KB |
Output is correct |
20 |
Correct |
18 ms |
84060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
84060 KB |
Output is correct |
2 |
Correct |
17 ms |
84056 KB |
Output is correct |
3 |
Correct |
18 ms |
83972 KB |
Output is correct |
4 |
Correct |
18 ms |
84060 KB |
Output is correct |
5 |
Correct |
18 ms |
84060 KB |
Output is correct |
6 |
Correct |
18 ms |
84060 KB |
Output is correct |
7 |
Correct |
20 ms |
84312 KB |
Output is correct |
8 |
Correct |
20 ms |
84056 KB |
Output is correct |
9 |
Correct |
17 ms |
84060 KB |
Output is correct |
10 |
Correct |
19 ms |
83952 KB |
Output is correct |
11 |
Correct |
18 ms |
84108 KB |
Output is correct |
12 |
Correct |
17 ms |
84060 KB |
Output is correct |
13 |
Correct |
19 ms |
83960 KB |
Output is correct |
14 |
Correct |
19 ms |
84060 KB |
Output is correct |
15 |
Correct |
19 ms |
84060 KB |
Output is correct |
16 |
Correct |
28 ms |
84052 KB |
Output is correct |
17 |
Correct |
18 ms |
84060 KB |
Output is correct |
18 |
Correct |
19 ms |
84060 KB |
Output is correct |
19 |
Correct |
22 ms |
84060 KB |
Output is correct |
20 |
Correct |
21 ms |
83888 KB |
Output is correct |
21 |
Correct |
22 ms |
84008 KB |
Output is correct |
22 |
Correct |
22 ms |
84056 KB |
Output is correct |
23 |
Correct |
23 ms |
84060 KB |
Output is correct |
24 |
Correct |
23 ms |
84088 KB |
Output is correct |
25 |
Correct |
22 ms |
84060 KB |
Output is correct |
26 |
Correct |
22 ms |
84048 KB |
Output is correct |
27 |
Correct |
32 ms |
84056 KB |
Output is correct |
28 |
Correct |
22 ms |
84056 KB |
Output is correct |
29 |
Correct |
22 ms |
83928 KB |
Output is correct |
30 |
Correct |
23 ms |
84060 KB |
Output is correct |
31 |
Correct |
21 ms |
83968 KB |
Output is correct |
32 |
Correct |
32 ms |
84060 KB |
Output is correct |
33 |
Correct |
24 ms |
83912 KB |
Output is correct |
34 |
Correct |
25 ms |
84060 KB |
Output is correct |
35 |
Correct |
23 ms |
84060 KB |
Output is correct |
36 |
Correct |
23 ms |
84112 KB |
Output is correct |
37 |
Correct |
19 ms |
84112 KB |
Output is correct |
38 |
Correct |
18 ms |
84060 KB |
Output is correct |
39 |
Correct |
18 ms |
84060 KB |
Output is correct |
40 |
Correct |
19 ms |
83908 KB |
Output is correct |
41 |
Correct |
17 ms |
84060 KB |
Output is correct |
42 |
Correct |
18 ms |
83980 KB |
Output is correct |
43 |
Correct |
19 ms |
84060 KB |
Output is correct |
44 |
Correct |
23 ms |
83912 KB |
Output is correct |
45 |
Correct |
24 ms |
84060 KB |
Output is correct |
46 |
Correct |
21 ms |
84096 KB |
Output is correct |
47 |
Correct |
18 ms |
84060 KB |
Output is correct |
48 |
Correct |
17 ms |
84152 KB |
Output is correct |
49 |
Correct |
20 ms |
84004 KB |
Output is correct |
50 |
Correct |
19 ms |
84048 KB |
Output is correct |
51 |
Correct |
18 ms |
84060 KB |
Output is correct |
52 |
Correct |
17 ms |
84060 KB |
Output is correct |
53 |
Correct |
19 ms |
83968 KB |
Output is correct |
54 |
Correct |
18 ms |
84108 KB |
Output is correct |
55 |
Correct |
19 ms |
84060 KB |
Output is correct |
56 |
Correct |
20 ms |
84060 KB |
Output is correct |
57 |
Correct |
22 ms |
84052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
84060 KB |
Output is correct |
2 |
Correct |
17 ms |
84056 KB |
Output is correct |
3 |
Correct |
18 ms |
83972 KB |
Output is correct |
4 |
Correct |
18 ms |
84060 KB |
Output is correct |
5 |
Correct |
18 ms |
84060 KB |
Output is correct |
6 |
Correct |
18 ms |
84060 KB |
Output is correct |
7 |
Correct |
20 ms |
84312 KB |
Output is correct |
8 |
Correct |
20 ms |
84056 KB |
Output is correct |
9 |
Correct |
17 ms |
84060 KB |
Output is correct |
10 |
Correct |
19 ms |
83952 KB |
Output is correct |
11 |
Correct |
18 ms |
84108 KB |
Output is correct |
12 |
Correct |
17 ms |
84060 KB |
Output is correct |
13 |
Correct |
19 ms |
83960 KB |
Output is correct |
14 |
Correct |
19 ms |
84060 KB |
Output is correct |
15 |
Correct |
19 ms |
84060 KB |
Output is correct |
16 |
Correct |
28 ms |
84052 KB |
Output is correct |
17 |
Correct |
18 ms |
84060 KB |
Output is correct |
18 |
Correct |
19 ms |
84060 KB |
Output is correct |
19 |
Correct |
22 ms |
84060 KB |
Output is correct |
20 |
Correct |
21 ms |
83888 KB |
Output is correct |
21 |
Correct |
22 ms |
84008 KB |
Output is correct |
22 |
Correct |
22 ms |
84056 KB |
Output is correct |
23 |
Correct |
23 ms |
84060 KB |
Output is correct |
24 |
Correct |
23 ms |
84088 KB |
Output is correct |
25 |
Correct |
22 ms |
84060 KB |
Output is correct |
26 |
Correct |
22 ms |
84048 KB |
Output is correct |
27 |
Correct |
32 ms |
84056 KB |
Output is correct |
28 |
Correct |
22 ms |
84056 KB |
Output is correct |
29 |
Correct |
22 ms |
83928 KB |
Output is correct |
30 |
Correct |
23 ms |
84060 KB |
Output is correct |
31 |
Correct |
21 ms |
83968 KB |
Output is correct |
32 |
Correct |
32 ms |
84060 KB |
Output is correct |
33 |
Correct |
24 ms |
83912 KB |
Output is correct |
34 |
Correct |
25 ms |
84060 KB |
Output is correct |
35 |
Correct |
23 ms |
84060 KB |
Output is correct |
36 |
Correct |
23 ms |
84112 KB |
Output is correct |
37 |
Correct |
19 ms |
84112 KB |
Output is correct |
38 |
Correct |
18 ms |
84060 KB |
Output is correct |
39 |
Correct |
18 ms |
84060 KB |
Output is correct |
40 |
Correct |
19 ms |
83908 KB |
Output is correct |
41 |
Correct |
17 ms |
84060 KB |
Output is correct |
42 |
Correct |
18 ms |
83980 KB |
Output is correct |
43 |
Correct |
19 ms |
84060 KB |
Output is correct |
44 |
Correct |
23 ms |
83912 KB |
Output is correct |
45 |
Correct |
24 ms |
84060 KB |
Output is correct |
46 |
Correct |
21 ms |
84096 KB |
Output is correct |
47 |
Correct |
18 ms |
84060 KB |
Output is correct |
48 |
Correct |
17 ms |
84152 KB |
Output is correct |
49 |
Correct |
20 ms |
84004 KB |
Output is correct |
50 |
Correct |
19 ms |
84048 KB |
Output is correct |
51 |
Correct |
18 ms |
84060 KB |
Output is correct |
52 |
Correct |
17 ms |
84060 KB |
Output is correct |
53 |
Correct |
19 ms |
83968 KB |
Output is correct |
54 |
Correct |
18 ms |
84108 KB |
Output is correct |
55 |
Correct |
19 ms |
84060 KB |
Output is correct |
56 |
Correct |
20 ms |
84060 KB |
Output is correct |
57 |
Correct |
22 ms |
84052 KB |
Output is correct |
58 |
Correct |
21 ms |
84052 KB |
Output is correct |
59 |
Correct |
20 ms |
84572 KB |
Output is correct |
60 |
Correct |
25 ms |
84572 KB |
Output is correct |
61 |
Correct |
43 ms |
84792 KB |
Output is correct |
62 |
Correct |
86 ms |
85596 KB |
Output is correct |
63 |
Correct |
24 ms |
85492 KB |
Output is correct |
64 |
Correct |
50 ms |
85588 KB |
Output is correct |
65 |
Correct |
17 ms |
84060 KB |
Output is correct |
66 |
Correct |
17 ms |
83900 KB |
Output is correct |
67 |
Correct |
24 ms |
84188 KB |
Output is correct |
68 |
Correct |
20 ms |
84060 KB |
Output is correct |
69 |
Correct |
25 ms |
84172 KB |
Output is correct |
70 |
Correct |
17 ms |
84060 KB |
Output is correct |
71 |
Correct |
29 ms |
84288 KB |
Output is correct |
72 |
Correct |
22 ms |
84060 KB |
Output is correct |
73 |
Correct |
20 ms |
84224 KB |
Output is correct |
74 |
Correct |
27 ms |
84732 KB |
Output is correct |
75 |
Correct |
23 ms |
84572 KB |
Output is correct |
76 |
Correct |
50 ms |
84776 KB |
Output is correct |
77 |
Correct |
80 ms |
85596 KB |
Output is correct |
78 |
Correct |
24 ms |
85560 KB |
Output is correct |
79 |
Correct |
71 ms |
85596 KB |
Output is correct |
80 |
Correct |
25 ms |
85596 KB |
Output is correct |
81 |
Correct |
30 ms |
85588 KB |
Output is correct |
82 |
Correct |
93 ms |
85588 KB |
Output is correct |
83 |
Correct |
18 ms |
84012 KB |
Output is correct |
84 |
Correct |
26 ms |
84028 KB |
Output is correct |
85 |
Correct |
27 ms |
84280 KB |
Output is correct |
86 |
Correct |
19 ms |
84316 KB |
Output is correct |
87 |
Correct |
20 ms |
84316 KB |
Output is correct |
88 |
Correct |
21 ms |
84572 KB |
Output is correct |
89 |
Correct |
26 ms |
85060 KB |
Output is correct |
90 |
Correct |
57 ms |
85388 KB |
Output is correct |
91 |
Correct |
135 ms |
86332 KB |
Output is correct |
92 |
Correct |
26 ms |
86056 KB |
Output is correct |
93 |
Correct |
73 ms |
86308 KB |
Output is correct |
94 |
Correct |
26 ms |
84048 KB |
Output is correct |
95 |
Correct |
33 ms |
84196 KB |
Output is correct |
96 |
Correct |
21 ms |
84220 KB |
Output is correct |
97 |
Correct |
25 ms |
84040 KB |
Output is correct |
98 |
Correct |
18 ms |
84020 KB |
Output is correct |
99 |
Correct |
17 ms |
84060 KB |
Output is correct |