# include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, a[N], k, dp[102][N];
int stk[N], pt, pf[N];
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n; i ++){
dp[1][i] = max(dp[1][i - 1], a[i]);
}
for(int i = 2; i <= k; i ++){
pt = 0;
deque < pair <int, int> > dq;
for(int j = i; j <= n; j ++){
while(pt > 0 && a[ stk[pt] ] < a[j])
pt --;
stk[++ pt] = j;
while(!dq.empty() && dq.back().first > dp[i - 1][j - 1])
dq.pop_back();
dq.push_back({dp[i - 1][j - 1], j - 1});
int lo = 0, hi = (int)dq.size() - 1;
while(lo <= hi){
int md = (lo + hi) >> 1;
if(dq[md].second >= max(stk[pt - 1], i - 1))
hi = md - 1;
else
lo = md + 1;
}
pf[pt] = dq[hi + 1].first + a[j];
if(pt > 1)
pf[pt] = min(pf[pt - 1], pf[pt]);
dp[i][j] = pf[pt];
}
}
cout << dp[k][n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
888 KB |
Output is correct |
16 |
Correct |
2 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
2 ms |
760 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1528 KB |
Output is correct |
2 |
Correct |
27 ms |
760 KB |
Output is correct |
3 |
Correct |
45 ms |
2100 KB |
Output is correct |
4 |
Correct |
249 ms |
14556 KB |
Output is correct |
5 |
Correct |
753 ms |
41444 KB |
Output is correct |
6 |
Correct |
74 ms |
3392 KB |
Output is correct |
7 |
Correct |
369 ms |
19912 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
57 ms |
4856 KB |
Output is correct |
15 |
Correct |
8 ms |
632 KB |
Output is correct |
16 |
Correct |
21 ms |
1656 KB |
Output is correct |
17 |
Correct |
27 ms |
1016 KB |
Output is correct |
18 |
Correct |
42 ms |
2180 KB |
Output is correct |
19 |
Correct |
195 ms |
14520 KB |
Output is correct |
20 |
Correct |
559 ms |
40652 KB |
Output is correct |
21 |
Correct |
72 ms |
2936 KB |
Output is correct |
22 |
Correct |
282 ms |
19260 KB |
Output is correct |
23 |
Correct |
59 ms |
1920 KB |
Output is correct |
24 |
Correct |
108 ms |
5436 KB |
Output is correct |
25 |
Correct |
579 ms |
40896 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
32 ms |
4856 KB |
Output is correct |
29 |
Correct |
9 ms |
632 KB |
Output is correct |
30 |
Correct |
15 ms |
1656 KB |
Output is correct |
31 |
Correct |
27 ms |
956 KB |
Output is correct |
32 |
Correct |
38 ms |
2436 KB |
Output is correct |
33 |
Correct |
110 ms |
14596 KB |
Output is correct |
34 |
Correct |
309 ms |
41160 KB |
Output is correct |
35 |
Correct |
70 ms |
3320 KB |
Output is correct |
36 |
Correct |
172 ms |
19520 KB |
Output is correct |
37 |
Correct |
8 ms |
636 KB |
Output is correct |
38 |
Correct |
65 ms |
4828 KB |
Output is correct |
39 |
Correct |
3 ms |
504 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |