#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define sz(x) int(x.size())
const ll INF = 1'000'000'000'000'000LL;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
vll A(1+N, INF);
for(int i = 1; i <= N; i++) cin >> A[i];
ll* DP[1+K];
DP[0] = new ll[1+N];
DP[1] = new ll[1+N];
for(int k = 2; k <= K; k++)
{
DP[k] = DP[k-2];
}
for(int i = 1; i <= N; i++)
{
DP[0][i] = DP[1][i] = +INF;
}
DP[0][0] = 0;
for(int k = 1; k <= K; k++)
{
// cerr << "\n\n\n\n\n k = " << k << '\n';
vector<ll> Aval;
vector<ll> DPprev;
vector<int> mxp;
vector<ll> mndp{INF};
// DP[k][0] = 0;
DP[k][0] = INF;
for(int i = 1; i <= N; i++)
{
// cerr << "\n\n\n";
// cerr << "i = " << i << '\n';
// cerr << "push value " << k-1 << ' ' << i-1 << " : " << DP[k-1][i-1] << '\n';
mxp.push_back(i-1);
// if(mndp.empty())
// mndp.push_back(DP[k-1][max(i-1, k-1)] + A[i]);
// else
// mndp.push_back(min(mndp.back(), DP[k-1][max(i-1, k-1)] + A[i]));
Aval.push_back(A[i]);
DPprev.push_back(DP[k-1][i-1]);
// mxp
mndp.push_back(min(mndp.back(), Aval.back() + DPprev.back()));
// cerr << "push P " << mndp.back() << '\n';
ll mnright = +INF;
while(A[mxp.back()] <= A[i])
{
mnright = min(mnright, DPprev.back());
mxp.pop_back();
mndp.pop_back();
Aval.pop_back();
DPprev.pop_back();
}
// cerr << "pop B " << mndp.back() << '\n';
// DPprev.back() = min()
Aval[sz(Aval)-1] = A[i];
DPprev[sz(DPprev)-1] = min(DPprev.back(), mnright);
// DPpre
mndp.pop_back();
mndp.push_back(min(mndp.back(), Aval.back() + DPprev.back()));
// cerr << "push Q " << mndp.back() << '\n';
DP[k][i] = mndp.back();
if(i < k) DP[k][i] = INF;
// cerr << k << ' ' << i << " : " << DP[k][i] << '\n';
}
}
cout << DP[K][N] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
7 ms |
468 KB |
Output is correct |
59 |
Correct |
6 ms |
1564 KB |
Output is correct |
60 |
Correct |
12 ms |
1560 KB |
Output is correct |
61 |
Correct |
88 ms |
1672 KB |
Output is correct |
62 |
Correct |
309 ms |
3284 KB |
Output is correct |
63 |
Correct |
17 ms |
3284 KB |
Output is correct |
64 |
Correct |
130 ms |
3352 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
324 KB |
Output is correct |
71 |
Correct |
34 ms |
592 KB |
Output is correct |
72 |
Correct |
2 ms |
596 KB |
Output is correct |
73 |
Correct |
10 ms |
608 KB |
Output is correct |
74 |
Correct |
6 ms |
1604 KB |
Output is correct |
75 |
Correct |
14 ms |
1612 KB |
Output is correct |
76 |
Correct |
111 ms |
1656 KB |
Output is correct |
77 |
Correct |
337 ms |
3404 KB |
Output is correct |
78 |
Correct |
18 ms |
3280 KB |
Output is correct |
79 |
Correct |
153 ms |
3348 KB |
Output is correct |
80 |
Correct |
13 ms |
3284 KB |
Output is correct |
81 |
Correct |
42 ms |
3288 KB |
Output is correct |
82 |
Correct |
317 ms |
3356 KB |
Output is correct |
83 |
Correct |
1 ms |
324 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
29 ms |
816 KB |
Output is correct |
86 |
Correct |
2 ms |
800 KB |
Output is correct |
87 |
Correct |
9 ms |
800 KB |
Output is correct |
88 |
Correct |
7 ms |
2252 KB |
Output is correct |
89 |
Correct |
16 ms |
2352 KB |
Output is correct |
90 |
Correct |
114 ms |
2436 KB |
Output is correct |
91 |
Correct |
333 ms |
5640 KB |
Output is correct |
92 |
Correct |
19 ms |
5340 KB |
Output is correct |
93 |
Correct |
156 ms |
5492 KB |
Output is correct |
94 |
Correct |
2 ms |
596 KB |
Output is correct |
95 |
Correct |
26 ms |
596 KB |
Output is correct |
96 |
Correct |
1 ms |
340 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
320 KB |
Output is correct |
99 |
Correct |
1 ms |
320 KB |
Output is correct |