#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 131072;
const int K = 110;
const ll inf = 1e15;
ll dp[K][N];
ll a[N];
int n;
#pragma GCC optimize("O3,unroll-loops")
#define MIN(x, y) ((x)<(y)?(x):(y))
ll seg[K][2*N];
ll get(int k, int l, int r)
{
l += N; r += N;
ll ans = inf;
while (l < r) {
if (l&1) { ans = MIN(ans, seg[k][l]); ++l; }
if (r&1) { --r; ans = MIN(ans, seg[k][r]); }
l /= 2; r /= 2;
}
return ans;
}
void up(int k, int i, ll x)
{
i += N;
Loop (_,0,18) {
seg[k][i] = MIN(seg[k][i], x);
i /= 2;
}
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int k;
cin >> n >> k;
Loop (i,0,n)
cin >> a[i];
memset(seg, 32 /* inf */, sizeof(seg));
dp[1][0] = a[0];
up(1, 0, dp[1][0]);
Loop (i,2,k+1)
dp[i][0] = inf;
vector<int> vec;
vec.push_back(0);
Loop (i,1,n) {
while (vec.size() && a[vec.back()] < a[i])
vec.pop_back();
dp[1][i] = max(a[i], dp[1][i-1]);
up(1, i, dp[1][i]);
Loop (j,2,k+1) {
dp[j][i] = get(j-1, vec.size()?
vec.back():
0, i) + a[i];
if (vec.size()) {
dp[j][i] = min(dp[j][i],
dp[j][vec.back()]);
}
up(j, i, dp[j][i]);
}
vec.push_back(i);
}
cout << dp[k][n-1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
226020 KB |
Output is correct |
2 |
Correct |
97 ms |
225956 KB |
Output is correct |
3 |
Correct |
82 ms |
226016 KB |
Output is correct |
4 |
Correct |
81 ms |
225960 KB |
Output is correct |
5 |
Correct |
83 ms |
226028 KB |
Output is correct |
6 |
Correct |
81 ms |
225968 KB |
Output is correct |
7 |
Correct |
95 ms |
225924 KB |
Output is correct |
8 |
Correct |
81 ms |
226000 KB |
Output is correct |
9 |
Correct |
90 ms |
225996 KB |
Output is correct |
10 |
Correct |
81 ms |
225984 KB |
Output is correct |
11 |
Correct |
79 ms |
225988 KB |
Output is correct |
12 |
Correct |
89 ms |
225996 KB |
Output is correct |
13 |
Correct |
81 ms |
226016 KB |
Output is correct |
14 |
Correct |
81 ms |
226060 KB |
Output is correct |
15 |
Correct |
85 ms |
226000 KB |
Output is correct |
16 |
Correct |
80 ms |
226052 KB |
Output is correct |
17 |
Correct |
85 ms |
225992 KB |
Output is correct |
18 |
Correct |
82 ms |
225952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
226016 KB |
Output is correct |
2 |
Correct |
81 ms |
226036 KB |
Output is correct |
3 |
Correct |
83 ms |
225980 KB |
Output is correct |
4 |
Correct |
81 ms |
225992 KB |
Output is correct |
5 |
Correct |
82 ms |
226144 KB |
Output is correct |
6 |
Correct |
81 ms |
225976 KB |
Output is correct |
7 |
Correct |
80 ms |
225928 KB |
Output is correct |
8 |
Correct |
96 ms |
225984 KB |
Output is correct |
9 |
Correct |
82 ms |
225960 KB |
Output is correct |
10 |
Correct |
81 ms |
225996 KB |
Output is correct |
11 |
Correct |
83 ms |
226000 KB |
Output is correct |
12 |
Correct |
91 ms |
226072 KB |
Output is correct |
13 |
Correct |
85 ms |
225972 KB |
Output is correct |
14 |
Correct |
79 ms |
226140 KB |
Output is correct |
15 |
Correct |
85 ms |
226076 KB |
Output is correct |
16 |
Correct |
82 ms |
226076 KB |
Output is correct |
17 |
Correct |
88 ms |
226068 KB |
Output is correct |
18 |
Correct |
82 ms |
226064 KB |
Output is correct |
19 |
Correct |
82 ms |
226000 KB |
Output is correct |
20 |
Correct |
80 ms |
226124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
226020 KB |
Output is correct |
2 |
Correct |
97 ms |
225956 KB |
Output is correct |
3 |
Correct |
82 ms |
226016 KB |
Output is correct |
4 |
Correct |
81 ms |
225960 KB |
Output is correct |
5 |
Correct |
83 ms |
226028 KB |
Output is correct |
6 |
Correct |
81 ms |
225968 KB |
Output is correct |
7 |
Correct |
95 ms |
225924 KB |
Output is correct |
8 |
Correct |
81 ms |
226000 KB |
Output is correct |
9 |
Correct |
90 ms |
225996 KB |
Output is correct |
10 |
Correct |
81 ms |
225984 KB |
Output is correct |
11 |
Correct |
79 ms |
225988 KB |
Output is correct |
12 |
Correct |
89 ms |
225996 KB |
Output is correct |
13 |
Correct |
81 ms |
226016 KB |
Output is correct |
14 |
Correct |
81 ms |
226060 KB |
Output is correct |
15 |
Correct |
85 ms |
226000 KB |
Output is correct |
16 |
Correct |
80 ms |
226052 KB |
Output is correct |
17 |
Correct |
85 ms |
225992 KB |
Output is correct |
18 |
Correct |
82 ms |
225952 KB |
Output is correct |
19 |
Correct |
81 ms |
226016 KB |
Output is correct |
20 |
Correct |
81 ms |
226036 KB |
Output is correct |
21 |
Correct |
83 ms |
225980 KB |
Output is correct |
22 |
Correct |
81 ms |
225992 KB |
Output is correct |
23 |
Correct |
82 ms |
226144 KB |
Output is correct |
24 |
Correct |
81 ms |
225976 KB |
Output is correct |
25 |
Correct |
80 ms |
225928 KB |
Output is correct |
26 |
Correct |
96 ms |
225984 KB |
Output is correct |
27 |
Correct |
82 ms |
225960 KB |
Output is correct |
28 |
Correct |
81 ms |
225996 KB |
Output is correct |
29 |
Correct |
83 ms |
226000 KB |
Output is correct |
30 |
Correct |
91 ms |
226072 KB |
Output is correct |
31 |
Correct |
85 ms |
225972 KB |
Output is correct |
32 |
Correct |
79 ms |
226140 KB |
Output is correct |
33 |
Correct |
85 ms |
226076 KB |
Output is correct |
34 |
Correct |
82 ms |
226076 KB |
Output is correct |
35 |
Correct |
88 ms |
226068 KB |
Output is correct |
36 |
Correct |
82 ms |
226064 KB |
Output is correct |
37 |
Correct |
82 ms |
226000 KB |
Output is correct |
38 |
Correct |
80 ms |
226124 KB |
Output is correct |
39 |
Correct |
87 ms |
226280 KB |
Output is correct |
40 |
Correct |
84 ms |
225972 KB |
Output is correct |
41 |
Correct |
81 ms |
225996 KB |
Output is correct |
42 |
Correct |
81 ms |
226020 KB |
Output is correct |
43 |
Correct |
82 ms |
225936 KB |
Output is correct |
44 |
Correct |
82 ms |
225956 KB |
Output is correct |
45 |
Correct |
80 ms |
225992 KB |
Output is correct |
46 |
Correct |
82 ms |
226000 KB |
Output is correct |
47 |
Correct |
81 ms |
225996 KB |
Output is correct |
48 |
Correct |
83 ms |
225924 KB |
Output is correct |
49 |
Correct |
82 ms |
225956 KB |
Output is correct |
50 |
Correct |
84 ms |
225984 KB |
Output is correct |
51 |
Correct |
81 ms |
226172 KB |
Output is correct |
52 |
Correct |
86 ms |
226124 KB |
Output is correct |
53 |
Correct |
84 ms |
226620 KB |
Output is correct |
54 |
Correct |
81 ms |
226592 KB |
Output is correct |
55 |
Correct |
82 ms |
226432 KB |
Output is correct |
56 |
Correct |
90 ms |
226608 KB |
Output is correct |
57 |
Correct |
85 ms |
226612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
226020 KB |
Output is correct |
2 |
Correct |
97 ms |
225956 KB |
Output is correct |
3 |
Correct |
82 ms |
226016 KB |
Output is correct |
4 |
Correct |
81 ms |
225960 KB |
Output is correct |
5 |
Correct |
83 ms |
226028 KB |
Output is correct |
6 |
Correct |
81 ms |
225968 KB |
Output is correct |
7 |
Correct |
95 ms |
225924 KB |
Output is correct |
8 |
Correct |
81 ms |
226000 KB |
Output is correct |
9 |
Correct |
90 ms |
225996 KB |
Output is correct |
10 |
Correct |
81 ms |
225984 KB |
Output is correct |
11 |
Correct |
79 ms |
225988 KB |
Output is correct |
12 |
Correct |
89 ms |
225996 KB |
Output is correct |
13 |
Correct |
81 ms |
226016 KB |
Output is correct |
14 |
Correct |
81 ms |
226060 KB |
Output is correct |
15 |
Correct |
85 ms |
226000 KB |
Output is correct |
16 |
Correct |
80 ms |
226052 KB |
Output is correct |
17 |
Correct |
85 ms |
225992 KB |
Output is correct |
18 |
Correct |
82 ms |
225952 KB |
Output is correct |
19 |
Correct |
81 ms |
226016 KB |
Output is correct |
20 |
Correct |
81 ms |
226036 KB |
Output is correct |
21 |
Correct |
83 ms |
225980 KB |
Output is correct |
22 |
Correct |
81 ms |
225992 KB |
Output is correct |
23 |
Correct |
82 ms |
226144 KB |
Output is correct |
24 |
Correct |
81 ms |
225976 KB |
Output is correct |
25 |
Correct |
80 ms |
225928 KB |
Output is correct |
26 |
Correct |
96 ms |
225984 KB |
Output is correct |
27 |
Correct |
82 ms |
225960 KB |
Output is correct |
28 |
Correct |
81 ms |
225996 KB |
Output is correct |
29 |
Correct |
83 ms |
226000 KB |
Output is correct |
30 |
Correct |
91 ms |
226072 KB |
Output is correct |
31 |
Correct |
85 ms |
225972 KB |
Output is correct |
32 |
Correct |
79 ms |
226140 KB |
Output is correct |
33 |
Correct |
85 ms |
226076 KB |
Output is correct |
34 |
Correct |
82 ms |
226076 KB |
Output is correct |
35 |
Correct |
88 ms |
226068 KB |
Output is correct |
36 |
Correct |
82 ms |
226064 KB |
Output is correct |
37 |
Correct |
82 ms |
226000 KB |
Output is correct |
38 |
Correct |
80 ms |
226124 KB |
Output is correct |
39 |
Correct |
87 ms |
226280 KB |
Output is correct |
40 |
Correct |
84 ms |
225972 KB |
Output is correct |
41 |
Correct |
81 ms |
225996 KB |
Output is correct |
42 |
Correct |
81 ms |
226020 KB |
Output is correct |
43 |
Correct |
82 ms |
225936 KB |
Output is correct |
44 |
Correct |
82 ms |
225956 KB |
Output is correct |
45 |
Correct |
80 ms |
225992 KB |
Output is correct |
46 |
Correct |
82 ms |
226000 KB |
Output is correct |
47 |
Correct |
81 ms |
225996 KB |
Output is correct |
48 |
Correct |
83 ms |
225924 KB |
Output is correct |
49 |
Correct |
82 ms |
225956 KB |
Output is correct |
50 |
Correct |
84 ms |
225984 KB |
Output is correct |
51 |
Correct |
81 ms |
226172 KB |
Output is correct |
52 |
Correct |
86 ms |
226124 KB |
Output is correct |
53 |
Correct |
84 ms |
226620 KB |
Output is correct |
54 |
Correct |
81 ms |
226592 KB |
Output is correct |
55 |
Correct |
82 ms |
226432 KB |
Output is correct |
56 |
Correct |
90 ms |
226608 KB |
Output is correct |
57 |
Correct |
85 ms |
226612 KB |
Output is correct |
58 |
Correct |
105 ms |
228204 KB |
Output is correct |
59 |
Correct |
95 ms |
226956 KB |
Output is correct |
60 |
Correct |
112 ms |
229144 KB |
Output is correct |
61 |
Correct |
481 ms |
253188 KB |
Output is correct |
62 |
Runtime error |
631 ms |
262144 KB |
Execution killed with signal 9 |
63 |
Halted |
0 ms |
0 KB |
- |