//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++)
{
int minF;
stack< ar > s;
for (int j = i; j <= n; j++)
{
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 |
32 ms |
87148 KB |
Output is correct |
2 |
Correct |
32 ms |
87108 KB |
Output is correct |
3 |
Correct |
32 ms |
87116 KB |
Output is correct |
4 |
Correct |
32 ms |
87108 KB |
Output is correct |
5 |
Correct |
33 ms |
87144 KB |
Output is correct |
6 |
Correct |
31 ms |
87188 KB |
Output is correct |
7 |
Correct |
32 ms |
87144 KB |
Output is correct |
8 |
Correct |
32 ms |
87112 KB |
Output is correct |
9 |
Correct |
33 ms |
87120 KB |
Output is correct |
10 |
Correct |
36 ms |
87340 KB |
Output is correct |
11 |
Correct |
33 ms |
87140 KB |
Output is correct |
12 |
Correct |
33 ms |
87084 KB |
Output is correct |
13 |
Correct |
33 ms |
87116 KB |
Output is correct |
14 |
Correct |
32 ms |
87192 KB |
Output is correct |
15 |
Correct |
32 ms |
87124 KB |
Output is correct |
16 |
Correct |
32 ms |
87116 KB |
Output is correct |
17 |
Correct |
32 ms |
87124 KB |
Output is correct |
18 |
Correct |
32 ms |
87140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
87124 KB |
Output is correct |
2 |
Correct |
32 ms |
87148 KB |
Output is correct |
3 |
Correct |
32 ms |
87144 KB |
Output is correct |
4 |
Correct |
35 ms |
87084 KB |
Output is correct |
5 |
Correct |
32 ms |
87224 KB |
Output is correct |
6 |
Correct |
32 ms |
87168 KB |
Output is correct |
7 |
Correct |
33 ms |
87116 KB |
Output is correct |
8 |
Correct |
32 ms |
87108 KB |
Output is correct |
9 |
Correct |
37 ms |
87188 KB |
Output is correct |
10 |
Correct |
36 ms |
87232 KB |
Output is correct |
11 |
Correct |
32 ms |
87160 KB |
Output is correct |
12 |
Correct |
43 ms |
87176 KB |
Output is correct |
13 |
Correct |
32 ms |
87168 KB |
Output is correct |
14 |
Correct |
33 ms |
87116 KB |
Output is correct |
15 |
Correct |
31 ms |
87176 KB |
Output is correct |
16 |
Correct |
33 ms |
87176 KB |
Output is correct |
17 |
Correct |
34 ms |
87156 KB |
Output is correct |
18 |
Correct |
33 ms |
87116 KB |
Output is correct |
19 |
Correct |
32 ms |
87116 KB |
Output is correct |
20 |
Correct |
33 ms |
87084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87148 KB |
Output is correct |
2 |
Correct |
32 ms |
87108 KB |
Output is correct |
3 |
Correct |
32 ms |
87116 KB |
Output is correct |
4 |
Correct |
32 ms |
87108 KB |
Output is correct |
5 |
Correct |
33 ms |
87144 KB |
Output is correct |
6 |
Correct |
31 ms |
87188 KB |
Output is correct |
7 |
Correct |
32 ms |
87144 KB |
Output is correct |
8 |
Correct |
32 ms |
87112 KB |
Output is correct |
9 |
Correct |
33 ms |
87120 KB |
Output is correct |
10 |
Correct |
36 ms |
87340 KB |
Output is correct |
11 |
Correct |
33 ms |
87140 KB |
Output is correct |
12 |
Correct |
33 ms |
87084 KB |
Output is correct |
13 |
Correct |
33 ms |
87116 KB |
Output is correct |
14 |
Correct |
32 ms |
87192 KB |
Output is correct |
15 |
Correct |
32 ms |
87124 KB |
Output is correct |
16 |
Correct |
32 ms |
87116 KB |
Output is correct |
17 |
Correct |
32 ms |
87124 KB |
Output is correct |
18 |
Correct |
32 ms |
87140 KB |
Output is correct |
19 |
Correct |
34 ms |
87124 KB |
Output is correct |
20 |
Correct |
32 ms |
87148 KB |
Output is correct |
21 |
Correct |
32 ms |
87144 KB |
Output is correct |
22 |
Correct |
35 ms |
87084 KB |
Output is correct |
23 |
Correct |
32 ms |
87224 KB |
Output is correct |
24 |
Correct |
32 ms |
87168 KB |
Output is correct |
25 |
Correct |
33 ms |
87116 KB |
Output is correct |
26 |
Correct |
32 ms |
87108 KB |
Output is correct |
27 |
Correct |
37 ms |
87188 KB |
Output is correct |
28 |
Correct |
36 ms |
87232 KB |
Output is correct |
29 |
Correct |
32 ms |
87160 KB |
Output is correct |
30 |
Correct |
43 ms |
87176 KB |
Output is correct |
31 |
Correct |
32 ms |
87168 KB |
Output is correct |
32 |
Correct |
33 ms |
87116 KB |
Output is correct |
33 |
Correct |
31 ms |
87176 KB |
Output is correct |
34 |
Correct |
33 ms |
87176 KB |
Output is correct |
35 |
Correct |
34 ms |
87156 KB |
Output is correct |
36 |
Correct |
33 ms |
87116 KB |
Output is correct |
37 |
Correct |
32 ms |
87116 KB |
Output is correct |
38 |
Correct |
33 ms |
87084 KB |
Output is correct |
39 |
Correct |
32 ms |
87104 KB |
Output is correct |
40 |
Correct |
33 ms |
87180 KB |
Output is correct |
41 |
Correct |
32 ms |
87104 KB |
Output is correct |
42 |
Correct |
31 ms |
87132 KB |
Output is correct |
43 |
Correct |
33 ms |
87148 KB |
Output is correct |
44 |
Correct |
32 ms |
87140 KB |
Output is correct |
45 |
Correct |
32 ms |
87112 KB |
Output is correct |
46 |
Correct |
34 ms |
87200 KB |
Output is correct |
47 |
Correct |
32 ms |
87148 KB |
Output is correct |
48 |
Correct |
32 ms |
87116 KB |
Output is correct |
49 |
Correct |
33 ms |
87128 KB |
Output is correct |
50 |
Correct |
32 ms |
87208 KB |
Output is correct |
51 |
Correct |
31 ms |
87136 KB |
Output is correct |
52 |
Correct |
35 ms |
87180 KB |
Output is correct |
53 |
Correct |
32 ms |
87176 KB |
Output is correct |
54 |
Correct |
32 ms |
87116 KB |
Output is correct |
55 |
Correct |
36 ms |
87316 KB |
Output is correct |
56 |
Correct |
32 ms |
87116 KB |
Output is correct |
57 |
Correct |
32 ms |
87156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87148 KB |
Output is correct |
2 |
Correct |
32 ms |
87108 KB |
Output is correct |
3 |
Correct |
32 ms |
87116 KB |
Output is correct |
4 |
Correct |
32 ms |
87108 KB |
Output is correct |
5 |
Correct |
33 ms |
87144 KB |
Output is correct |
6 |
Correct |
31 ms |
87188 KB |
Output is correct |
7 |
Correct |
32 ms |
87144 KB |
Output is correct |
8 |
Correct |
32 ms |
87112 KB |
Output is correct |
9 |
Correct |
33 ms |
87120 KB |
Output is correct |
10 |
Correct |
36 ms |
87340 KB |
Output is correct |
11 |
Correct |
33 ms |
87140 KB |
Output is correct |
12 |
Correct |
33 ms |
87084 KB |
Output is correct |
13 |
Correct |
33 ms |
87116 KB |
Output is correct |
14 |
Correct |
32 ms |
87192 KB |
Output is correct |
15 |
Correct |
32 ms |
87124 KB |
Output is correct |
16 |
Correct |
32 ms |
87116 KB |
Output is correct |
17 |
Correct |
32 ms |
87124 KB |
Output is correct |
18 |
Correct |
32 ms |
87140 KB |
Output is correct |
19 |
Correct |
34 ms |
87124 KB |
Output is correct |
20 |
Correct |
32 ms |
87148 KB |
Output is correct |
21 |
Correct |
32 ms |
87144 KB |
Output is correct |
22 |
Correct |
35 ms |
87084 KB |
Output is correct |
23 |
Correct |
32 ms |
87224 KB |
Output is correct |
24 |
Correct |
32 ms |
87168 KB |
Output is correct |
25 |
Correct |
33 ms |
87116 KB |
Output is correct |
26 |
Correct |
32 ms |
87108 KB |
Output is correct |
27 |
Correct |
37 ms |
87188 KB |
Output is correct |
28 |
Correct |
36 ms |
87232 KB |
Output is correct |
29 |
Correct |
32 ms |
87160 KB |
Output is correct |
30 |
Correct |
43 ms |
87176 KB |
Output is correct |
31 |
Correct |
32 ms |
87168 KB |
Output is correct |
32 |
Correct |
33 ms |
87116 KB |
Output is correct |
33 |
Correct |
31 ms |
87176 KB |
Output is correct |
34 |
Correct |
33 ms |
87176 KB |
Output is correct |
35 |
Correct |
34 ms |
87156 KB |
Output is correct |
36 |
Correct |
33 ms |
87116 KB |
Output is correct |
37 |
Correct |
32 ms |
87116 KB |
Output is correct |
38 |
Correct |
33 ms |
87084 KB |
Output is correct |
39 |
Correct |
32 ms |
87104 KB |
Output is correct |
40 |
Correct |
33 ms |
87180 KB |
Output is correct |
41 |
Correct |
32 ms |
87104 KB |
Output is correct |
42 |
Correct |
31 ms |
87132 KB |
Output is correct |
43 |
Correct |
33 ms |
87148 KB |
Output is correct |
44 |
Correct |
32 ms |
87140 KB |
Output is correct |
45 |
Correct |
32 ms |
87112 KB |
Output is correct |
46 |
Correct |
34 ms |
87200 KB |
Output is correct |
47 |
Correct |
32 ms |
87148 KB |
Output is correct |
48 |
Correct |
32 ms |
87116 KB |
Output is correct |
49 |
Correct |
33 ms |
87128 KB |
Output is correct |
50 |
Correct |
32 ms |
87208 KB |
Output is correct |
51 |
Correct |
31 ms |
87136 KB |
Output is correct |
52 |
Correct |
35 ms |
87180 KB |
Output is correct |
53 |
Correct |
32 ms |
87176 KB |
Output is correct |
54 |
Correct |
32 ms |
87116 KB |
Output is correct |
55 |
Correct |
36 ms |
87316 KB |
Output is correct |
56 |
Correct |
32 ms |
87116 KB |
Output is correct |
57 |
Correct |
32 ms |
87156 KB |
Output is correct |
58 |
Incorrect |
35 ms |
87356 KB |
Output isn't correct |
59 |
Halted |
0 ms |
0 KB |
- |