#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int INF = 1e9;
const int MAXN = (int) 1e5;
const int MAXK = 100;
int dp[MAXN + 1][MAXK + 1];
int arr[MAXN + 1];
int stk[MAXN + 1];
int best[MAXN + 1], mn_dp[MAXN + 1];
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, j, n, k;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for(i = 1; i <= n; i++) {
cin >> arr[i];
}
for(i = 1; i <= n; i++) {
dp[i][1] = max(dp[i - 1][1], arr[i]);
}
dp[0][1] = INF;
for(j = 2; j <= k; j++) {
int sz = 0;
dp[0][j] = best[0] = INF;
for(i = 1; i <= n; i++) {
int mn = INF;
while(sz > 0 && arr[i] >= arr[stk[sz]]) {
mn = min(mn, mn_dp[sz]);
sz--;
}
stk[++sz] = i;
best[sz] = min(best[sz - 1], min(mn, dp[stk[sz - 1]][j - 1]) + arr[i]);
mn_dp[sz] = min(mn, dp[i][j - 1]);
dp[i][j] = best[sz];
}
}
cout << dp[n][k];
//cin.close();
//cout.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
652 KB |
Output is correct |
13 |
Correct |
2 ms |
652 KB |
Output is correct |
14 |
Correct |
2 ms |
652 KB |
Output is correct |
15 |
Correct |
2 ms |
652 KB |
Output is correct |
16 |
Correct |
2 ms |
652 KB |
Output is correct |
17 |
Correct |
2 ms |
652 KB |
Output is correct |
18 |
Correct |
2 ms |
652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
652 KB |
Output is correct |
2 |
Correct |
2 ms |
652 KB |
Output is correct |
3 |
Correct |
2 ms |
652 KB |
Output is correct |
4 |
Correct |
2 ms |
652 KB |
Output is correct |
5 |
Correct |
2 ms |
780 KB |
Output is correct |
6 |
Correct |
2 ms |
780 KB |
Output is correct |
7 |
Correct |
2 ms |
780 KB |
Output is correct |
8 |
Correct |
2 ms |
780 KB |
Output is correct |
9 |
Correct |
2 ms |
780 KB |
Output is correct |
10 |
Correct |
2 ms |
780 KB |
Output is correct |
11 |
Correct |
2 ms |
780 KB |
Output is correct |
12 |
Correct |
2 ms |
780 KB |
Output is correct |
13 |
Correct |
2 ms |
780 KB |
Output is correct |
14 |
Correct |
2 ms |
780 KB |
Output is correct |
15 |
Correct |
2 ms |
780 KB |
Output is correct |
16 |
Correct |
2 ms |
780 KB |
Output is correct |
17 |
Correct |
2 ms |
780 KB |
Output is correct |
18 |
Correct |
2 ms |
780 KB |
Output is correct |
19 |
Correct |
2 ms |
780 KB |
Output is correct |
20 |
Correct |
2 ms |
780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
780 KB |
Output is correct |
2 |
Correct |
2 ms |
780 KB |
Output is correct |
3 |
Correct |
2 ms |
780 KB |
Output is correct |
4 |
Correct |
2 ms |
816 KB |
Output is correct |
5 |
Correct |
2 ms |
816 KB |
Output is correct |
6 |
Correct |
2 ms |
816 KB |
Output is correct |
7 |
Correct |
2 ms |
816 KB |
Output is correct |
8 |
Correct |
2 ms |
816 KB |
Output is correct |
9 |
Correct |
2 ms |
816 KB |
Output is correct |
10 |
Correct |
2 ms |
816 KB |
Output is correct |
11 |
Correct |
2 ms |
816 KB |
Output is correct |
12 |
Correct |
2 ms |
816 KB |
Output is correct |
13 |
Correct |
2 ms |
816 KB |
Output is correct |
14 |
Correct |
2 ms |
816 KB |
Output is correct |
15 |
Correct |
2 ms |
816 KB |
Output is correct |
16 |
Correct |
2 ms |
816 KB |
Output is correct |
17 |
Correct |
2 ms |
816 KB |
Output is correct |
18 |
Correct |
2 ms |
816 KB |
Output is correct |
19 |
Correct |
2 ms |
816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4748 KB |
Output is correct |
2 |
Correct |
18 ms |
18444 KB |
Output is correct |
3 |
Correct |
24 ms |
18456 KB |
Output is correct |
4 |
Correct |
70 ms |
18456 KB |
Output is correct |
5 |
Correct |
252 ms |
41228 KB |
Output is correct |
6 |
Correct |
44 ms |
41228 KB |
Output is correct |
7 |
Correct |
119 ms |
41228 KB |
Output is correct |
8 |
Correct |
2 ms |
41228 KB |
Output is correct |
9 |
Correct |
2 ms |
41228 KB |
Output is correct |
10 |
Correct |
2 ms |
41228 KB |
Output is correct |
11 |
Correct |
2 ms |
41228 KB |
Output is correct |
12 |
Correct |
2 ms |
41228 KB |
Output is correct |
13 |
Correct |
2 ms |
41228 KB |
Output is correct |
14 |
Correct |
17 ms |
41228 KB |
Output is correct |
15 |
Correct |
6 ms |
41228 KB |
Output is correct |
16 |
Correct |
8 ms |
41228 KB |
Output is correct |
17 |
Correct |
19 ms |
41228 KB |
Output is correct |
18 |
Correct |
24 ms |
41228 KB |
Output is correct |
19 |
Correct |
64 ms |
41228 KB |
Output is correct |
20 |
Correct |
343 ms |
41236 KB |
Output is correct |
21 |
Correct |
46 ms |
41236 KB |
Output is correct |
22 |
Correct |
142 ms |
41236 KB |
Output is correct |
23 |
Correct |
40 ms |
41236 KB |
Output is correct |
24 |
Correct |
66 ms |
41236 KB |
Output is correct |
25 |
Correct |
313 ms |
41236 KB |
Output is correct |
26 |
Correct |
2 ms |
41236 KB |
Output is correct |
27 |
Correct |
3 ms |
41236 KB |
Output is correct |
28 |
Correct |
15 ms |
41236 KB |
Output is correct |
29 |
Correct |
6 ms |
41236 KB |
Output is correct |
30 |
Correct |
8 ms |
41236 KB |
Output is correct |
31 |
Correct |
18 ms |
41236 KB |
Output is correct |
32 |
Correct |
24 ms |
41236 KB |
Output is correct |
33 |
Correct |
59 ms |
41236 KB |
Output is correct |
34 |
Correct |
192 ms |
41560 KB |
Output is correct |
35 |
Correct |
44 ms |
41560 KB |
Output is correct |
36 |
Correct |
98 ms |
41560 KB |
Output is correct |
37 |
Correct |
6 ms |
41560 KB |
Output is correct |
38 |
Correct |
14 ms |
41560 KB |
Output is correct |
39 |
Correct |
3 ms |
41560 KB |
Output is correct |
40 |
Correct |
2 ms |
41560 KB |
Output is correct |
41 |
Correct |
2 ms |
41560 KB |
Output is correct |
42 |
Correct |
2 ms |
41560 KB |
Output is correct |