//Link :
//Author: Nguyen Dinh Manh
#include <bits/stdc++.h>
using namespace std;
#define timing "Time collapse : " << fixed << setprecision(3) << 1.000 * clock() / CLOCKS_PER_SEC
#define int long long
#define ar array< int , 2>
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define task ""
template<typename T1, typename T2> bool minN(T1 &a, T2 b) {if (a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxX(T1 &a, T2 b) {if (a < b) a = b; else return 0; return 1;}
const int MAX = 100011, INFL = 1e9 + 7, INFLL = 1e18 + 7;
int n, f[MAX][111], a[MAX], k;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen(task".inp","r", stdin); freopen(task".out","w",stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
memset(f, 0x3f, sizeof f);
f[1][0] = 0;
for (int i = 1; i <= n; i++)
minN(f[1][i], max(f[1][i - 1], a[i]));
for (int i = 2; i <= k; ++i)
{
stack< ar > s;
for (int j = i; j <= n; ++j)
{
int minF = f[i - 1][j - 1];
while (!s.empty() && a[s.top()[1]] <= a[j])
{
minN(minF, s.top()[0]);
s.pop();
}
f[i][j] = min(f[i][(s.empty() ? 0 : s.top()[1])], minF + a[j]);
s.push({minF, j});
}
//for (int j = 1; j <= n; j++) cout << f[i][j] << " \n"[j == n];
}
cout << f[k][n] ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
87188 KB |
Output is correct |
2 |
Correct |
31 ms |
87116 KB |
Output is correct |
3 |
Correct |
32 ms |
87164 KB |
Output is correct |
4 |
Correct |
33 ms |
87204 KB |
Output is correct |
5 |
Correct |
32 ms |
87108 KB |
Output is correct |
6 |
Correct |
33 ms |
87116 KB |
Output is correct |
7 |
Correct |
36 ms |
87116 KB |
Output is correct |
8 |
Correct |
40 ms |
87204 KB |
Output is correct |
9 |
Correct |
37 ms |
87228 KB |
Output is correct |
10 |
Correct |
39 ms |
87080 KB |
Output is correct |
11 |
Correct |
32 ms |
87124 KB |
Output is correct |
12 |
Correct |
32 ms |
87108 KB |
Output is correct |
13 |
Correct |
32 ms |
87224 KB |
Output is correct |
14 |
Correct |
33 ms |
87080 KB |
Output is correct |
15 |
Correct |
34 ms |
87092 KB |
Output is correct |
16 |
Correct |
34 ms |
87184 KB |
Output is correct |
17 |
Correct |
35 ms |
87224 KB |
Output is correct |
18 |
Correct |
39 ms |
87100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
87120 KB |
Output is correct |
2 |
Correct |
34 ms |
87104 KB |
Output is correct |
3 |
Correct |
35 ms |
87116 KB |
Output is correct |
4 |
Correct |
32 ms |
87220 KB |
Output is correct |
5 |
Correct |
34 ms |
87196 KB |
Output is correct |
6 |
Correct |
36 ms |
87144 KB |
Output is correct |
7 |
Correct |
39 ms |
87140 KB |
Output is correct |
8 |
Correct |
37 ms |
87120 KB |
Output is correct |
9 |
Correct |
33 ms |
87172 KB |
Output is correct |
10 |
Correct |
33 ms |
87116 KB |
Output is correct |
11 |
Correct |
32 ms |
87140 KB |
Output is correct |
12 |
Correct |
33 ms |
87108 KB |
Output is correct |
13 |
Correct |
33 ms |
87124 KB |
Output is correct |
14 |
Correct |
32 ms |
87152 KB |
Output is correct |
15 |
Correct |
33 ms |
87196 KB |
Output is correct |
16 |
Correct |
41 ms |
87192 KB |
Output is correct |
17 |
Correct |
36 ms |
87128 KB |
Output is correct |
18 |
Correct |
39 ms |
87220 KB |
Output is correct |
19 |
Correct |
31 ms |
87196 KB |
Output is correct |
20 |
Correct |
32 ms |
87116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
87188 KB |
Output is correct |
2 |
Correct |
31 ms |
87116 KB |
Output is correct |
3 |
Correct |
32 ms |
87164 KB |
Output is correct |
4 |
Correct |
33 ms |
87204 KB |
Output is correct |
5 |
Correct |
32 ms |
87108 KB |
Output is correct |
6 |
Correct |
33 ms |
87116 KB |
Output is correct |
7 |
Correct |
36 ms |
87116 KB |
Output is correct |
8 |
Correct |
40 ms |
87204 KB |
Output is correct |
9 |
Correct |
37 ms |
87228 KB |
Output is correct |
10 |
Correct |
39 ms |
87080 KB |
Output is correct |
11 |
Correct |
32 ms |
87124 KB |
Output is correct |
12 |
Correct |
32 ms |
87108 KB |
Output is correct |
13 |
Correct |
32 ms |
87224 KB |
Output is correct |
14 |
Correct |
33 ms |
87080 KB |
Output is correct |
15 |
Correct |
34 ms |
87092 KB |
Output is correct |
16 |
Correct |
34 ms |
87184 KB |
Output is correct |
17 |
Correct |
35 ms |
87224 KB |
Output is correct |
18 |
Correct |
39 ms |
87100 KB |
Output is correct |
19 |
Correct |
34 ms |
87120 KB |
Output is correct |
20 |
Correct |
34 ms |
87104 KB |
Output is correct |
21 |
Correct |
35 ms |
87116 KB |
Output is correct |
22 |
Correct |
32 ms |
87220 KB |
Output is correct |
23 |
Correct |
34 ms |
87196 KB |
Output is correct |
24 |
Correct |
36 ms |
87144 KB |
Output is correct |
25 |
Correct |
39 ms |
87140 KB |
Output is correct |
26 |
Correct |
37 ms |
87120 KB |
Output is correct |
27 |
Correct |
33 ms |
87172 KB |
Output is correct |
28 |
Correct |
33 ms |
87116 KB |
Output is correct |
29 |
Correct |
32 ms |
87140 KB |
Output is correct |
30 |
Correct |
33 ms |
87108 KB |
Output is correct |
31 |
Correct |
33 ms |
87124 KB |
Output is correct |
32 |
Correct |
32 ms |
87152 KB |
Output is correct |
33 |
Correct |
33 ms |
87196 KB |
Output is correct |
34 |
Correct |
41 ms |
87192 KB |
Output is correct |
35 |
Correct |
36 ms |
87128 KB |
Output is correct |
36 |
Correct |
39 ms |
87220 KB |
Output is correct |
37 |
Correct |
31 ms |
87196 KB |
Output is correct |
38 |
Correct |
32 ms |
87116 KB |
Output is correct |
39 |
Correct |
32 ms |
87120 KB |
Output is correct |
40 |
Correct |
37 ms |
87176 KB |
Output is correct |
41 |
Correct |
32 ms |
87120 KB |
Output is correct |
42 |
Correct |
36 ms |
87180 KB |
Output is correct |
43 |
Correct |
35 ms |
87124 KB |
Output is correct |
44 |
Correct |
40 ms |
87168 KB |
Output is correct |
45 |
Correct |
33 ms |
87116 KB |
Output is correct |
46 |
Correct |
35 ms |
87148 KB |
Output is correct |
47 |
Correct |
33 ms |
87176 KB |
Output is correct |
48 |
Correct |
33 ms |
87160 KB |
Output is correct |
49 |
Correct |
34 ms |
87116 KB |
Output is correct |
50 |
Correct |
34 ms |
87084 KB |
Output is correct |
51 |
Correct |
39 ms |
87188 KB |
Output is correct |
52 |
Correct |
35 ms |
87116 KB |
Output is correct |
53 |
Correct |
37 ms |
87140 KB |
Output is correct |
54 |
Correct |
33 ms |
87116 KB |
Output is correct |
55 |
Correct |
32 ms |
87156 KB |
Output is correct |
56 |
Correct |
32 ms |
87168 KB |
Output is correct |
57 |
Correct |
33 ms |
87124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
87188 KB |
Output is correct |
2 |
Correct |
31 ms |
87116 KB |
Output is correct |
3 |
Correct |
32 ms |
87164 KB |
Output is correct |
4 |
Correct |
33 ms |
87204 KB |
Output is correct |
5 |
Correct |
32 ms |
87108 KB |
Output is correct |
6 |
Correct |
33 ms |
87116 KB |
Output is correct |
7 |
Correct |
36 ms |
87116 KB |
Output is correct |
8 |
Correct |
40 ms |
87204 KB |
Output is correct |
9 |
Correct |
37 ms |
87228 KB |
Output is correct |
10 |
Correct |
39 ms |
87080 KB |
Output is correct |
11 |
Correct |
32 ms |
87124 KB |
Output is correct |
12 |
Correct |
32 ms |
87108 KB |
Output is correct |
13 |
Correct |
32 ms |
87224 KB |
Output is correct |
14 |
Correct |
33 ms |
87080 KB |
Output is correct |
15 |
Correct |
34 ms |
87092 KB |
Output is correct |
16 |
Correct |
34 ms |
87184 KB |
Output is correct |
17 |
Correct |
35 ms |
87224 KB |
Output is correct |
18 |
Correct |
39 ms |
87100 KB |
Output is correct |
19 |
Correct |
34 ms |
87120 KB |
Output is correct |
20 |
Correct |
34 ms |
87104 KB |
Output is correct |
21 |
Correct |
35 ms |
87116 KB |
Output is correct |
22 |
Correct |
32 ms |
87220 KB |
Output is correct |
23 |
Correct |
34 ms |
87196 KB |
Output is correct |
24 |
Correct |
36 ms |
87144 KB |
Output is correct |
25 |
Correct |
39 ms |
87140 KB |
Output is correct |
26 |
Correct |
37 ms |
87120 KB |
Output is correct |
27 |
Correct |
33 ms |
87172 KB |
Output is correct |
28 |
Correct |
33 ms |
87116 KB |
Output is correct |
29 |
Correct |
32 ms |
87140 KB |
Output is correct |
30 |
Correct |
33 ms |
87108 KB |
Output is correct |
31 |
Correct |
33 ms |
87124 KB |
Output is correct |
32 |
Correct |
32 ms |
87152 KB |
Output is correct |
33 |
Correct |
33 ms |
87196 KB |
Output is correct |
34 |
Correct |
41 ms |
87192 KB |
Output is correct |
35 |
Correct |
36 ms |
87128 KB |
Output is correct |
36 |
Correct |
39 ms |
87220 KB |
Output is correct |
37 |
Correct |
31 ms |
87196 KB |
Output is correct |
38 |
Correct |
32 ms |
87116 KB |
Output is correct |
39 |
Correct |
32 ms |
87120 KB |
Output is correct |
40 |
Correct |
37 ms |
87176 KB |
Output is correct |
41 |
Correct |
32 ms |
87120 KB |
Output is correct |
42 |
Correct |
36 ms |
87180 KB |
Output is correct |
43 |
Correct |
35 ms |
87124 KB |
Output is correct |
44 |
Correct |
40 ms |
87168 KB |
Output is correct |
45 |
Correct |
33 ms |
87116 KB |
Output is correct |
46 |
Correct |
35 ms |
87148 KB |
Output is correct |
47 |
Correct |
33 ms |
87176 KB |
Output is correct |
48 |
Correct |
33 ms |
87160 KB |
Output is correct |
49 |
Correct |
34 ms |
87116 KB |
Output is correct |
50 |
Correct |
34 ms |
87084 KB |
Output is correct |
51 |
Correct |
39 ms |
87188 KB |
Output is correct |
52 |
Correct |
35 ms |
87116 KB |
Output is correct |
53 |
Correct |
37 ms |
87140 KB |
Output is correct |
54 |
Correct |
33 ms |
87116 KB |
Output is correct |
55 |
Correct |
32 ms |
87156 KB |
Output is correct |
56 |
Correct |
32 ms |
87168 KB |
Output is correct |
57 |
Correct |
33 ms |
87124 KB |
Output is correct |
58 |
Incorrect |
41 ms |
87268 KB |
Output isn't correct |
59 |
Halted |
0 ms |
0 KB |
- |