#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define multiTest unsigned long long numTest; for (cin >> numTest; numTest--;)
#define db(val) "[" #val " = " << (val) << "] "
#define el cout << '\n'
#define taskName "kblocks"
using namespace std;
typedef long long ll;
typedef pair <ll, ll> ii;
const ll si = 1e5 + 9;
const ll mod = 1e9 + 7;
int n, k, dp[109][si], a[si], lr[si], mn[si];
/*
testing someone's solution
*/
inline void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
memset(dp, 0x3f, sizeof(dp));
dp[1][0] = 0;
for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]);
for (int i = 1; i <= n; ++i) for (lr[i] = i - 1; lr[i] && a[lr[i]] <= a[i]; ) lr[i] = lr[lr[i]];
for (int i = 2; i <= k; ++i)
{
mn[i - 1] = 1e9;
for (int j = i; j <= n; ++j)
{
mn[j] = dp[i - 1][j - 1];
for (int t = j - 1; t > lr[j]; t = lr[t]) mn[j] = min(mn[j], mn[t]);
dp[i][j] = min(dp[i][lr[j]], mn[j] + a[j]);
}
}
cout << dp[k][n], el;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// multiTest
{
solve();
el;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42964 KB |
Output is correct |
2 |
Correct |
18 ms |
42992 KB |
Output is correct |
3 |
Correct |
16 ms |
42996 KB |
Output is correct |
4 |
Correct |
17 ms |
42876 KB |
Output is correct |
5 |
Correct |
17 ms |
42964 KB |
Output is correct |
6 |
Correct |
16 ms |
42920 KB |
Output is correct |
7 |
Correct |
18 ms |
42888 KB |
Output is correct |
8 |
Correct |
19 ms |
42892 KB |
Output is correct |
9 |
Correct |
18 ms |
42964 KB |
Output is correct |
10 |
Correct |
18 ms |
42900 KB |
Output is correct |
11 |
Correct |
18 ms |
42964 KB |
Output is correct |
12 |
Correct |
17 ms |
42972 KB |
Output is correct |
13 |
Correct |
18 ms |
42964 KB |
Output is correct |
14 |
Correct |
19 ms |
42964 KB |
Output is correct |
15 |
Correct |
17 ms |
42964 KB |
Output is correct |
16 |
Correct |
17 ms |
42964 KB |
Output is correct |
17 |
Correct |
18 ms |
42944 KB |
Output is correct |
18 |
Correct |
17 ms |
42880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
42964 KB |
Output is correct |
2 |
Correct |
18 ms |
42940 KB |
Output is correct |
3 |
Correct |
18 ms |
42992 KB |
Output is correct |
4 |
Correct |
17 ms |
42996 KB |
Output is correct |
5 |
Correct |
20 ms |
42960 KB |
Output is correct |
6 |
Correct |
17 ms |
42964 KB |
Output is correct |
7 |
Correct |
17 ms |
42964 KB |
Output is correct |
8 |
Correct |
17 ms |
42900 KB |
Output is correct |
9 |
Correct |
17 ms |
42920 KB |
Output is correct |
10 |
Correct |
17 ms |
42900 KB |
Output is correct |
11 |
Correct |
17 ms |
42964 KB |
Output is correct |
12 |
Correct |
16 ms |
42920 KB |
Output is correct |
13 |
Correct |
18 ms |
42964 KB |
Output is correct |
14 |
Correct |
20 ms |
42952 KB |
Output is correct |
15 |
Correct |
17 ms |
42988 KB |
Output is correct |
16 |
Correct |
17 ms |
42976 KB |
Output is correct |
17 |
Correct |
17 ms |
42928 KB |
Output is correct |
18 |
Correct |
17 ms |
42992 KB |
Output is correct |
19 |
Correct |
17 ms |
42920 KB |
Output is correct |
20 |
Correct |
17 ms |
42964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42964 KB |
Output is correct |
2 |
Correct |
18 ms |
42992 KB |
Output is correct |
3 |
Correct |
16 ms |
42996 KB |
Output is correct |
4 |
Correct |
17 ms |
42876 KB |
Output is correct |
5 |
Correct |
17 ms |
42964 KB |
Output is correct |
6 |
Correct |
16 ms |
42920 KB |
Output is correct |
7 |
Correct |
18 ms |
42888 KB |
Output is correct |
8 |
Correct |
19 ms |
42892 KB |
Output is correct |
9 |
Correct |
18 ms |
42964 KB |
Output is correct |
10 |
Correct |
18 ms |
42900 KB |
Output is correct |
11 |
Correct |
18 ms |
42964 KB |
Output is correct |
12 |
Correct |
17 ms |
42972 KB |
Output is correct |
13 |
Correct |
18 ms |
42964 KB |
Output is correct |
14 |
Correct |
19 ms |
42964 KB |
Output is correct |
15 |
Correct |
17 ms |
42964 KB |
Output is correct |
16 |
Correct |
17 ms |
42964 KB |
Output is correct |
17 |
Correct |
18 ms |
42944 KB |
Output is correct |
18 |
Correct |
17 ms |
42880 KB |
Output is correct |
19 |
Correct |
18 ms |
42964 KB |
Output is correct |
20 |
Correct |
18 ms |
42940 KB |
Output is correct |
21 |
Correct |
18 ms |
42992 KB |
Output is correct |
22 |
Correct |
17 ms |
42996 KB |
Output is correct |
23 |
Correct |
20 ms |
42960 KB |
Output is correct |
24 |
Correct |
17 ms |
42964 KB |
Output is correct |
25 |
Correct |
17 ms |
42964 KB |
Output is correct |
26 |
Correct |
17 ms |
42900 KB |
Output is correct |
27 |
Correct |
17 ms |
42920 KB |
Output is correct |
28 |
Correct |
17 ms |
42900 KB |
Output is correct |
29 |
Correct |
17 ms |
42964 KB |
Output is correct |
30 |
Correct |
16 ms |
42920 KB |
Output is correct |
31 |
Correct |
18 ms |
42964 KB |
Output is correct |
32 |
Correct |
20 ms |
42952 KB |
Output is correct |
33 |
Correct |
17 ms |
42988 KB |
Output is correct |
34 |
Correct |
17 ms |
42976 KB |
Output is correct |
35 |
Correct |
17 ms |
42928 KB |
Output is correct |
36 |
Correct |
17 ms |
42992 KB |
Output is correct |
37 |
Correct |
17 ms |
42920 KB |
Output is correct |
38 |
Correct |
17 ms |
42964 KB |
Output is correct |
39 |
Correct |
18 ms |
43028 KB |
Output is correct |
40 |
Correct |
17 ms |
42972 KB |
Output is correct |
41 |
Correct |
17 ms |
42964 KB |
Output is correct |
42 |
Correct |
18 ms |
42952 KB |
Output is correct |
43 |
Correct |
18 ms |
42996 KB |
Output is correct |
44 |
Correct |
17 ms |
42964 KB |
Output is correct |
45 |
Correct |
18 ms |
42952 KB |
Output is correct |
46 |
Correct |
17 ms |
42884 KB |
Output is correct |
47 |
Correct |
20 ms |
42900 KB |
Output is correct |
48 |
Correct |
18 ms |
42924 KB |
Output is correct |
49 |
Correct |
17 ms |
42964 KB |
Output is correct |
50 |
Correct |
18 ms |
42940 KB |
Output is correct |
51 |
Correct |
17 ms |
42972 KB |
Output is correct |
52 |
Correct |
17 ms |
42888 KB |
Output is correct |
53 |
Correct |
17 ms |
42964 KB |
Output is correct |
54 |
Correct |
17 ms |
42988 KB |
Output is correct |
55 |
Correct |
18 ms |
42964 KB |
Output is correct |
56 |
Correct |
17 ms |
42964 KB |
Output is correct |
57 |
Correct |
15 ms |
42876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42964 KB |
Output is correct |
2 |
Correct |
18 ms |
42992 KB |
Output is correct |
3 |
Correct |
16 ms |
42996 KB |
Output is correct |
4 |
Correct |
17 ms |
42876 KB |
Output is correct |
5 |
Correct |
17 ms |
42964 KB |
Output is correct |
6 |
Correct |
16 ms |
42920 KB |
Output is correct |
7 |
Correct |
18 ms |
42888 KB |
Output is correct |
8 |
Correct |
19 ms |
42892 KB |
Output is correct |
9 |
Correct |
18 ms |
42964 KB |
Output is correct |
10 |
Correct |
18 ms |
42900 KB |
Output is correct |
11 |
Correct |
18 ms |
42964 KB |
Output is correct |
12 |
Correct |
17 ms |
42972 KB |
Output is correct |
13 |
Correct |
18 ms |
42964 KB |
Output is correct |
14 |
Correct |
19 ms |
42964 KB |
Output is correct |
15 |
Correct |
17 ms |
42964 KB |
Output is correct |
16 |
Correct |
17 ms |
42964 KB |
Output is correct |
17 |
Correct |
18 ms |
42944 KB |
Output is correct |
18 |
Correct |
17 ms |
42880 KB |
Output is correct |
19 |
Correct |
18 ms |
42964 KB |
Output is correct |
20 |
Correct |
18 ms |
42940 KB |
Output is correct |
21 |
Correct |
18 ms |
42992 KB |
Output is correct |
22 |
Correct |
17 ms |
42996 KB |
Output is correct |
23 |
Correct |
20 ms |
42960 KB |
Output is correct |
24 |
Correct |
17 ms |
42964 KB |
Output is correct |
25 |
Correct |
17 ms |
42964 KB |
Output is correct |
26 |
Correct |
17 ms |
42900 KB |
Output is correct |
27 |
Correct |
17 ms |
42920 KB |
Output is correct |
28 |
Correct |
17 ms |
42900 KB |
Output is correct |
29 |
Correct |
17 ms |
42964 KB |
Output is correct |
30 |
Correct |
16 ms |
42920 KB |
Output is correct |
31 |
Correct |
18 ms |
42964 KB |
Output is correct |
32 |
Correct |
20 ms |
42952 KB |
Output is correct |
33 |
Correct |
17 ms |
42988 KB |
Output is correct |
34 |
Correct |
17 ms |
42976 KB |
Output is correct |
35 |
Correct |
17 ms |
42928 KB |
Output is correct |
36 |
Correct |
17 ms |
42992 KB |
Output is correct |
37 |
Correct |
17 ms |
42920 KB |
Output is correct |
38 |
Correct |
17 ms |
42964 KB |
Output is correct |
39 |
Correct |
18 ms |
43028 KB |
Output is correct |
40 |
Correct |
17 ms |
42972 KB |
Output is correct |
41 |
Correct |
17 ms |
42964 KB |
Output is correct |
42 |
Correct |
18 ms |
42952 KB |
Output is correct |
43 |
Correct |
18 ms |
42996 KB |
Output is correct |
44 |
Correct |
17 ms |
42964 KB |
Output is correct |
45 |
Correct |
18 ms |
42952 KB |
Output is correct |
46 |
Correct |
17 ms |
42884 KB |
Output is correct |
47 |
Correct |
20 ms |
42900 KB |
Output is correct |
48 |
Correct |
18 ms |
42924 KB |
Output is correct |
49 |
Correct |
17 ms |
42964 KB |
Output is correct |
50 |
Correct |
18 ms |
42940 KB |
Output is correct |
51 |
Correct |
17 ms |
42972 KB |
Output is correct |
52 |
Correct |
17 ms |
42888 KB |
Output is correct |
53 |
Correct |
17 ms |
42964 KB |
Output is correct |
54 |
Correct |
17 ms |
42988 KB |
Output is correct |
55 |
Correct |
18 ms |
42964 KB |
Output is correct |
56 |
Correct |
17 ms |
42964 KB |
Output is correct |
57 |
Correct |
15 ms |
42876 KB |
Output is correct |
58 |
Correct |
19 ms |
43092 KB |
Output is correct |
59 |
Correct |
20 ms |
43332 KB |
Output is correct |
60 |
Correct |
22 ms |
43408 KB |
Output is correct |
61 |
Correct |
47 ms |
43520 KB |
Output is correct |
62 |
Correct |
98 ms |
44112 KB |
Output is correct |
63 |
Correct |
30 ms |
44116 KB |
Output is correct |
64 |
Correct |
61 ms |
44152 KB |
Output is correct |
65 |
Correct |
20 ms |
42936 KB |
Output is correct |
66 |
Correct |
20 ms |
42968 KB |
Output is correct |
67 |
Correct |
19 ms |
42964 KB |
Output is correct |
68 |
Correct |
19 ms |
42964 KB |
Output is correct |
69 |
Correct |
20 ms |
42980 KB |
Output is correct |
70 |
Correct |
23 ms |
42964 KB |
Output is correct |
71 |
Correct |
27 ms |
43092 KB |
Output is correct |
72 |
Correct |
21 ms |
43092 KB |
Output is correct |
73 |
Correct |
25 ms |
43212 KB |
Output is correct |
74 |
Correct |
24 ms |
43348 KB |
Output is correct |
75 |
Correct |
27 ms |
43444 KB |
Output is correct |
76 |
Correct |
52 ms |
43512 KB |
Output is correct |
77 |
Correct |
119 ms |
44152 KB |
Output is correct |
78 |
Correct |
31 ms |
44148 KB |
Output is correct |
79 |
Correct |
68 ms |
44112 KB |
Output is correct |
80 |
Correct |
29 ms |
43760 KB |
Output is correct |
81 |
Correct |
35 ms |
44108 KB |
Output is correct |
82 |
Correct |
110 ms |
44140 KB |
Output is correct |
83 |
Correct |
17 ms |
42908 KB |
Output is correct |
84 |
Correct |
19 ms |
42972 KB |
Output is correct |
85 |
Correct |
22 ms |
43044 KB |
Output is correct |
86 |
Correct |
19 ms |
43092 KB |
Output is correct |
87 |
Correct |
20 ms |
43080 KB |
Output is correct |
88 |
Correct |
21 ms |
43368 KB |
Output is correct |
89 |
Correct |
23 ms |
43444 KB |
Output is correct |
90 |
Correct |
40 ms |
43476 KB |
Output is correct |
91 |
Correct |
88 ms |
44116 KB |
Output is correct |
92 |
Correct |
29 ms |
44100 KB |
Output is correct |
93 |
Correct |
56 ms |
44140 KB |
Output is correct |
94 |
Correct |
18 ms |
43012 KB |
Output is correct |
95 |
Correct |
23 ms |
43092 KB |
Output is correct |
96 |
Correct |
18 ms |
42952 KB |
Output is correct |
97 |
Correct |
18 ms |
42952 KB |
Output is correct |
98 |
Correct |
18 ms |
42964 KB |
Output is correct |
99 |
Correct |
18 ms |
42976 KB |
Output is correct |