#include <bits/stdc++.h>
using namespace std;
#define uwu "blocks"
typedef long long ll;
#define pb push_back
#define endl '\n'
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
const int N = 1e5 + 3;
const int INF = 1e9 + 7;
int n, k, a[N];
int dp[N][104];
void mini(int &cur, int val){
if (cur > val) cur = val;
}
void solve(void){
memset(dp, 0x3f, sizeof(dp)); dp[0][1] = 0;
FOR(i, 1, n) dp[i][1] = max(dp[i - 1][1], a[i]);
FOR(j, 2, k){
stack <pair<int, int>> st;
FOR(i, j, n){
int val = dp[i - 1][j - 1];
while(!st.empty() && a[st.top().first] <= a[i]){
mini(val, st.top().second);
st.pop();
}
int L = st.empty() ? 0 : st.top().first;
dp[i][j] = min(dp[L][j], val + a[i]);
st.push(make_pair(i, val));
}
}
cout << dp[n][k];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(uwu".inp","r")){
freopen(uwu".inp","r",stdin);
freopen(uwu".out","w",stdout);
}
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
solve();
cerr << "\nTime used: " << clock() << "ms\n";
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:44:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(uwu".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
blocks.cpp:45:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(uwu".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
41052 KB |
Output is correct |
2 |
Correct |
13 ms |
40944 KB |
Output is correct |
3 |
Correct |
10 ms |
41016 KB |
Output is correct |
4 |
Correct |
10 ms |
41048 KB |
Output is correct |
5 |
Correct |
10 ms |
41128 KB |
Output is correct |
6 |
Correct |
10 ms |
41052 KB |
Output is correct |
7 |
Correct |
10 ms |
41052 KB |
Output is correct |
8 |
Correct |
10 ms |
41048 KB |
Output is correct |
9 |
Correct |
11 ms |
41052 KB |
Output is correct |
10 |
Correct |
12 ms |
41108 KB |
Output is correct |
11 |
Correct |
10 ms |
41052 KB |
Output is correct |
12 |
Correct |
10 ms |
41108 KB |
Output is correct |
13 |
Correct |
10 ms |
40952 KB |
Output is correct |
14 |
Correct |
11 ms |
41052 KB |
Output is correct |
15 |
Correct |
9 ms |
41052 KB |
Output is correct |
16 |
Correct |
10 ms |
40980 KB |
Output is correct |
17 |
Correct |
16 ms |
41052 KB |
Output is correct |
18 |
Correct |
10 ms |
41052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
41052 KB |
Output is correct |
2 |
Correct |
10 ms |
41052 KB |
Output is correct |
3 |
Correct |
16 ms |
41116 KB |
Output is correct |
4 |
Correct |
12 ms |
41052 KB |
Output is correct |
5 |
Correct |
10 ms |
41120 KB |
Output is correct |
6 |
Correct |
10 ms |
41052 KB |
Output is correct |
7 |
Correct |
9 ms |
41048 KB |
Output is correct |
8 |
Correct |
10 ms |
40992 KB |
Output is correct |
9 |
Correct |
11 ms |
41048 KB |
Output is correct |
10 |
Correct |
10 ms |
41052 KB |
Output is correct |
11 |
Correct |
10 ms |
41164 KB |
Output is correct |
12 |
Correct |
10 ms |
41052 KB |
Output is correct |
13 |
Correct |
11 ms |
41048 KB |
Output is correct |
14 |
Correct |
10 ms |
41080 KB |
Output is correct |
15 |
Correct |
10 ms |
41052 KB |
Output is correct |
16 |
Correct |
11 ms |
41112 KB |
Output is correct |
17 |
Correct |
10 ms |
41048 KB |
Output is correct |
18 |
Correct |
10 ms |
41052 KB |
Output is correct |
19 |
Correct |
11 ms |
41164 KB |
Output is correct |
20 |
Correct |
9 ms |
41052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
41052 KB |
Output is correct |
2 |
Correct |
13 ms |
40944 KB |
Output is correct |
3 |
Correct |
10 ms |
41016 KB |
Output is correct |
4 |
Correct |
10 ms |
41048 KB |
Output is correct |
5 |
Correct |
10 ms |
41128 KB |
Output is correct |
6 |
Correct |
10 ms |
41052 KB |
Output is correct |
7 |
Correct |
10 ms |
41052 KB |
Output is correct |
8 |
Correct |
10 ms |
41048 KB |
Output is correct |
9 |
Correct |
11 ms |
41052 KB |
Output is correct |
10 |
Correct |
12 ms |
41108 KB |
Output is correct |
11 |
Correct |
10 ms |
41052 KB |
Output is correct |
12 |
Correct |
10 ms |
41108 KB |
Output is correct |
13 |
Correct |
10 ms |
40952 KB |
Output is correct |
14 |
Correct |
11 ms |
41052 KB |
Output is correct |
15 |
Correct |
9 ms |
41052 KB |
Output is correct |
16 |
Correct |
10 ms |
40980 KB |
Output is correct |
17 |
Correct |
16 ms |
41052 KB |
Output is correct |
18 |
Correct |
10 ms |
41052 KB |
Output is correct |
19 |
Correct |
10 ms |
41052 KB |
Output is correct |
20 |
Correct |
10 ms |
41052 KB |
Output is correct |
21 |
Correct |
16 ms |
41116 KB |
Output is correct |
22 |
Correct |
12 ms |
41052 KB |
Output is correct |
23 |
Correct |
10 ms |
41120 KB |
Output is correct |
24 |
Correct |
10 ms |
41052 KB |
Output is correct |
25 |
Correct |
9 ms |
41048 KB |
Output is correct |
26 |
Correct |
10 ms |
40992 KB |
Output is correct |
27 |
Correct |
11 ms |
41048 KB |
Output is correct |
28 |
Correct |
10 ms |
41052 KB |
Output is correct |
29 |
Correct |
10 ms |
41164 KB |
Output is correct |
30 |
Correct |
10 ms |
41052 KB |
Output is correct |
31 |
Correct |
11 ms |
41048 KB |
Output is correct |
32 |
Correct |
10 ms |
41080 KB |
Output is correct |
33 |
Correct |
10 ms |
41052 KB |
Output is correct |
34 |
Correct |
11 ms |
41112 KB |
Output is correct |
35 |
Correct |
10 ms |
41048 KB |
Output is correct |
36 |
Correct |
10 ms |
41052 KB |
Output is correct |
37 |
Correct |
11 ms |
41164 KB |
Output is correct |
38 |
Correct |
9 ms |
41052 KB |
Output is correct |
39 |
Correct |
12 ms |
41052 KB |
Output is correct |
40 |
Correct |
12 ms |
41052 KB |
Output is correct |
41 |
Correct |
10 ms |
41052 KB |
Output is correct |
42 |
Correct |
16 ms |
41120 KB |
Output is correct |
43 |
Correct |
10 ms |
41048 KB |
Output is correct |
44 |
Correct |
10 ms |
41052 KB |
Output is correct |
45 |
Correct |
9 ms |
40964 KB |
Output is correct |
46 |
Correct |
10 ms |
41052 KB |
Output is correct |
47 |
Correct |
10 ms |
41052 KB |
Output is correct |
48 |
Correct |
9 ms |
41052 KB |
Output is correct |
49 |
Correct |
10 ms |
41052 KB |
Output is correct |
50 |
Correct |
10 ms |
41052 KB |
Output is correct |
51 |
Correct |
10 ms |
41168 KB |
Output is correct |
52 |
Correct |
11 ms |
41052 KB |
Output is correct |
53 |
Correct |
10 ms |
41052 KB |
Output is correct |
54 |
Correct |
9 ms |
41052 KB |
Output is correct |
55 |
Correct |
9 ms |
41144 KB |
Output is correct |
56 |
Correct |
10 ms |
41052 KB |
Output is correct |
57 |
Correct |
10 ms |
41052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
41052 KB |
Output is correct |
2 |
Correct |
13 ms |
40944 KB |
Output is correct |
3 |
Correct |
10 ms |
41016 KB |
Output is correct |
4 |
Correct |
10 ms |
41048 KB |
Output is correct |
5 |
Correct |
10 ms |
41128 KB |
Output is correct |
6 |
Correct |
10 ms |
41052 KB |
Output is correct |
7 |
Correct |
10 ms |
41052 KB |
Output is correct |
8 |
Correct |
10 ms |
41048 KB |
Output is correct |
9 |
Correct |
11 ms |
41052 KB |
Output is correct |
10 |
Correct |
12 ms |
41108 KB |
Output is correct |
11 |
Correct |
10 ms |
41052 KB |
Output is correct |
12 |
Correct |
10 ms |
41108 KB |
Output is correct |
13 |
Correct |
10 ms |
40952 KB |
Output is correct |
14 |
Correct |
11 ms |
41052 KB |
Output is correct |
15 |
Correct |
9 ms |
41052 KB |
Output is correct |
16 |
Correct |
10 ms |
40980 KB |
Output is correct |
17 |
Correct |
16 ms |
41052 KB |
Output is correct |
18 |
Correct |
10 ms |
41052 KB |
Output is correct |
19 |
Correct |
10 ms |
41052 KB |
Output is correct |
20 |
Correct |
10 ms |
41052 KB |
Output is correct |
21 |
Correct |
16 ms |
41116 KB |
Output is correct |
22 |
Correct |
12 ms |
41052 KB |
Output is correct |
23 |
Correct |
10 ms |
41120 KB |
Output is correct |
24 |
Correct |
10 ms |
41052 KB |
Output is correct |
25 |
Correct |
9 ms |
41048 KB |
Output is correct |
26 |
Correct |
10 ms |
40992 KB |
Output is correct |
27 |
Correct |
11 ms |
41048 KB |
Output is correct |
28 |
Correct |
10 ms |
41052 KB |
Output is correct |
29 |
Correct |
10 ms |
41164 KB |
Output is correct |
30 |
Correct |
10 ms |
41052 KB |
Output is correct |
31 |
Correct |
11 ms |
41048 KB |
Output is correct |
32 |
Correct |
10 ms |
41080 KB |
Output is correct |
33 |
Correct |
10 ms |
41052 KB |
Output is correct |
34 |
Correct |
11 ms |
41112 KB |
Output is correct |
35 |
Correct |
10 ms |
41048 KB |
Output is correct |
36 |
Correct |
10 ms |
41052 KB |
Output is correct |
37 |
Correct |
11 ms |
41164 KB |
Output is correct |
38 |
Correct |
9 ms |
41052 KB |
Output is correct |
39 |
Correct |
12 ms |
41052 KB |
Output is correct |
40 |
Correct |
12 ms |
41052 KB |
Output is correct |
41 |
Correct |
10 ms |
41052 KB |
Output is correct |
42 |
Correct |
16 ms |
41120 KB |
Output is correct |
43 |
Correct |
10 ms |
41048 KB |
Output is correct |
44 |
Correct |
10 ms |
41052 KB |
Output is correct |
45 |
Correct |
9 ms |
40964 KB |
Output is correct |
46 |
Correct |
10 ms |
41052 KB |
Output is correct |
47 |
Correct |
10 ms |
41052 KB |
Output is correct |
48 |
Correct |
9 ms |
41052 KB |
Output is correct |
49 |
Correct |
10 ms |
41052 KB |
Output is correct |
50 |
Correct |
10 ms |
41052 KB |
Output is correct |
51 |
Correct |
10 ms |
41168 KB |
Output is correct |
52 |
Correct |
11 ms |
41052 KB |
Output is correct |
53 |
Correct |
10 ms |
41052 KB |
Output is correct |
54 |
Correct |
9 ms |
41052 KB |
Output is correct |
55 |
Correct |
9 ms |
41144 KB |
Output is correct |
56 |
Correct |
10 ms |
41052 KB |
Output is correct |
57 |
Correct |
10 ms |
41052 KB |
Output is correct |
58 |
Correct |
11 ms |
41052 KB |
Output is correct |
59 |
Correct |
13 ms |
41564 KB |
Output is correct |
60 |
Correct |
14 ms |
41564 KB |
Output is correct |
61 |
Correct |
40 ms |
41560 KB |
Output is correct |
62 |
Correct |
106 ms |
42084 KB |
Output is correct |
63 |
Correct |
17 ms |
42072 KB |
Output is correct |
64 |
Correct |
55 ms |
42076 KB |
Output is correct |
65 |
Correct |
10 ms |
41048 KB |
Output is correct |
66 |
Correct |
12 ms |
41168 KB |
Output is correct |
67 |
Correct |
11 ms |
41048 KB |
Output is correct |
68 |
Correct |
10 ms |
41128 KB |
Output is correct |
69 |
Correct |
12 ms |
41048 KB |
Output is correct |
70 |
Correct |
10 ms |
41052 KB |
Output is correct |
71 |
Correct |
22 ms |
41276 KB |
Output is correct |
72 |
Correct |
12 ms |
41052 KB |
Output is correct |
73 |
Correct |
16 ms |
41224 KB |
Output is correct |
74 |
Correct |
13 ms |
41564 KB |
Output is correct |
75 |
Correct |
16 ms |
41640 KB |
Output is correct |
76 |
Correct |
50 ms |
41564 KB |
Output is correct |
77 |
Correct |
128 ms |
42160 KB |
Output is correct |
78 |
Correct |
18 ms |
42076 KB |
Output is correct |
79 |
Correct |
66 ms |
42224 KB |
Output is correct |
80 |
Correct |
16 ms |
42072 KB |
Output is correct |
81 |
Correct |
27 ms |
42040 KB |
Output is correct |
82 |
Correct |
127 ms |
42020 KB |
Output is correct |
83 |
Correct |
10 ms |
41048 KB |
Output is correct |
84 |
Correct |
10 ms |
41052 KB |
Output is correct |
85 |
Correct |
19 ms |
41312 KB |
Output is correct |
86 |
Correct |
12 ms |
41308 KB |
Output is correct |
87 |
Correct |
16 ms |
41096 KB |
Output is correct |
88 |
Correct |
13 ms |
41564 KB |
Output is correct |
89 |
Correct |
21 ms |
41696 KB |
Output is correct |
90 |
Correct |
47 ms |
41564 KB |
Output is correct |
91 |
Correct |
121 ms |
42508 KB |
Output is correct |
92 |
Correct |
18 ms |
42384 KB |
Output is correct |
93 |
Correct |
62 ms |
42584 KB |
Output is correct |
94 |
Correct |
11 ms |
41052 KB |
Output is correct |
95 |
Correct |
17 ms |
41276 KB |
Output is correct |
96 |
Correct |
10 ms |
41160 KB |
Output is correct |
97 |
Correct |
9 ms |
41048 KB |
Output is correct |
98 |
Correct |
11 ms |
41052 KB |
Output is correct |
99 |
Correct |
9 ms |
41052 KB |
Output is correct |