#include <bits/stdc++.h>
#define N 100010
#define K 110
#define inf 200000000000000000LL
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
ll n, k, v[N], dp[N][K], maior;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i++) cin>>v[i];
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++)
dp[i][j] = inf;
for(int i = 1; i <= n; i++)
{
maior = max(maior, v[i]);
dp[i][1] = maior;
}
for(int q = 2; q <= k; q++)
{
stack < pii > W;
for(int i = 1; i <= n; i++)
{
ll opt = dp[i - 1][q - 1];
while(!W.empty() and W.top().f < v[i])
{
opt = min(opt, W.top().s);
W.pop();
}
if(W.empty() or opt + v[i] < W.top().f + W.top().s) W.push({v[i], opt});
if(i >= q) dp[i][q] = W.top().f + W.top().s;
}
}
cout<<dp[n][k]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
86392 KB |
Output is correct |
2 |
Correct |
66 ms |
86632 KB |
Output is correct |
3 |
Correct |
62 ms |
86696 KB |
Output is correct |
4 |
Correct |
61 ms |
86696 KB |
Output is correct |
5 |
Correct |
63 ms |
86696 KB |
Output is correct |
6 |
Correct |
67 ms |
86696 KB |
Output is correct |
7 |
Correct |
62 ms |
86712 KB |
Output is correct |
8 |
Correct |
63 ms |
86712 KB |
Output is correct |
9 |
Correct |
70 ms |
86728 KB |
Output is correct |
10 |
Correct |
70 ms |
86756 KB |
Output is correct |
11 |
Correct |
61 ms |
86756 KB |
Output is correct |
12 |
Correct |
63 ms |
86764 KB |
Output is correct |
13 |
Correct |
68 ms |
86780 KB |
Output is correct |
14 |
Correct |
71 ms |
86780 KB |
Output is correct |
15 |
Correct |
68 ms |
86780 KB |
Output is correct |
16 |
Correct |
63 ms |
86780 KB |
Output is correct |
17 |
Correct |
62 ms |
86780 KB |
Output is correct |
18 |
Correct |
63 ms |
86780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
86780 KB |
Output is correct |
2 |
Correct |
70 ms |
86780 KB |
Output is correct |
3 |
Correct |
67 ms |
86780 KB |
Output is correct |
4 |
Correct |
63 ms |
86780 KB |
Output is correct |
5 |
Correct |
73 ms |
86780 KB |
Output is correct |
6 |
Correct |
67 ms |
86780 KB |
Output is correct |
7 |
Correct |
72 ms |
86780 KB |
Output is correct |
8 |
Correct |
68 ms |
86780 KB |
Output is correct |
9 |
Correct |
63 ms |
86780 KB |
Output is correct |
10 |
Correct |
71 ms |
86780 KB |
Output is correct |
11 |
Correct |
63 ms |
86780 KB |
Output is correct |
12 |
Correct |
61 ms |
86780 KB |
Output is correct |
13 |
Correct |
66 ms |
86780 KB |
Output is correct |
14 |
Correct |
61 ms |
86780 KB |
Output is correct |
15 |
Correct |
63 ms |
86780 KB |
Output is correct |
16 |
Correct |
63 ms |
86780 KB |
Output is correct |
17 |
Correct |
63 ms |
86780 KB |
Output is correct |
18 |
Correct |
63 ms |
86780 KB |
Output is correct |
19 |
Correct |
63 ms |
86780 KB |
Output is correct |
20 |
Correct |
62 ms |
86780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
86780 KB |
Output is correct |
2 |
Correct |
72 ms |
86780 KB |
Output is correct |
3 |
Correct |
68 ms |
86780 KB |
Output is correct |
4 |
Correct |
61 ms |
86780 KB |
Output is correct |
5 |
Correct |
61 ms |
86780 KB |
Output is correct |
6 |
Correct |
72 ms |
86780 KB |
Output is correct |
7 |
Correct |
63 ms |
86780 KB |
Output is correct |
8 |
Correct |
66 ms |
86780 KB |
Output is correct |
9 |
Correct |
62 ms |
86780 KB |
Output is correct |
10 |
Correct |
77 ms |
86780 KB |
Output is correct |
11 |
Correct |
66 ms |
86780 KB |
Output is correct |
12 |
Correct |
71 ms |
86780 KB |
Output is correct |
13 |
Correct |
70 ms |
86780 KB |
Output is correct |
14 |
Correct |
63 ms |
86780 KB |
Output is correct |
15 |
Correct |
66 ms |
86784 KB |
Output is correct |
16 |
Correct |
68 ms |
86784 KB |
Output is correct |
17 |
Correct |
63 ms |
86784 KB |
Output is correct |
18 |
Correct |
65 ms |
86784 KB |
Output is correct |
19 |
Correct |
61 ms |
86784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
86784 KB |
Output is correct |
2 |
Correct |
71 ms |
87424 KB |
Output is correct |
3 |
Correct |
77 ms |
87684 KB |
Output is correct |
4 |
Correct |
196 ms |
88136 KB |
Output is correct |
5 |
Correct |
398 ms |
89280 KB |
Output is correct |
6 |
Correct |
86 ms |
89828 KB |
Output is correct |
7 |
Correct |
293 ms |
90716 KB |
Output is correct |
8 |
Correct |
70 ms |
90716 KB |
Output is correct |
9 |
Correct |
65 ms |
90716 KB |
Output is correct |
10 |
Correct |
61 ms |
90716 KB |
Output is correct |
11 |
Correct |
64 ms |
90716 KB |
Output is correct |
12 |
Correct |
61 ms |
90716 KB |
Output is correct |
13 |
Correct |
62 ms |
90716 KB |
Output is correct |
14 |
Correct |
88 ms |
90716 KB |
Output is correct |
15 |
Correct |
64 ms |
90716 KB |
Output is correct |
16 |
Correct |
69 ms |
90716 KB |
Output is correct |
17 |
Correct |
68 ms |
90816 KB |
Output is correct |
18 |
Correct |
76 ms |
91048 KB |
Output is correct |
19 |
Correct |
156 ms |
91284 KB |
Output is correct |
20 |
Correct |
397 ms |
92484 KB |
Output is correct |
21 |
Correct |
85 ms |
93080 KB |
Output is correct |
22 |
Correct |
262 ms |
93756 KB |
Output is correct |
23 |
Correct |
79 ms |
94448 KB |
Output is correct |
24 |
Correct |
110 ms |
95096 KB |
Output is correct |
25 |
Correct |
412 ms |
95860 KB |
Output is correct |
26 |
Correct |
62 ms |
95860 KB |
Output is correct |
27 |
Correct |
62 ms |
95860 KB |
Output is correct |
28 |
Correct |
95 ms |
95860 KB |
Output is correct |
29 |
Correct |
64 ms |
95860 KB |
Output is correct |
30 |
Correct |
72 ms |
95860 KB |
Output is correct |
31 |
Correct |
82 ms |
95996 KB |
Output is correct |
32 |
Correct |
98 ms |
96624 KB |
Output is correct |
33 |
Correct |
231 ms |
97060 KB |
Output is correct |
34 |
Correct |
531 ms |
98644 KB |
Output is correct |
35 |
Correct |
86 ms |
98888 KB |
Output is correct |
36 |
Correct |
277 ms |
99940 KB |
Output is correct |
37 |
Correct |
67 ms |
99940 KB |
Output is correct |
38 |
Correct |
102 ms |
99940 KB |
Output is correct |
39 |
Correct |
64 ms |
99940 KB |
Output is correct |
40 |
Correct |
63 ms |
99940 KB |
Output is correct |
41 |
Correct |
65 ms |
99940 KB |
Output is correct |
42 |
Correct |
61 ms |
99940 KB |
Output is correct |