#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)
typedef long long ll;
typedef pair < int, int > ii;
const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;
const int N = 1e5 + 5;
const int K = 100 + 5;
int n, k, a[N], dp[N][K];
ii p[N];
int main () {
ios :: sync_with_stdio(0);
cin >> n >> k;
int mx = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for(int j = 2; j <= k; j++) {
int sz = 0;
for(int i = 1; i <= n; i++) {
int ans = j <= i ? dp[i - 1][j - 1] : inf;
while(sz and a[i] >= p[sz - 1].first)
ans = min(ans, p[--sz].second);
if(!sz or a[i] + ans < p[sz - 1].first + p[sz - 1].second)
p[sz++] = ii(a[i], ans);
dp[i][j] = p[sz - 1].first + p[sz - 1].second;
}
}
cout << dp[n][k] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
43904 KB |
Output is correct |
2 |
Correct |
0 ms |
43904 KB |
Output is correct |
3 |
Correct |
0 ms |
43904 KB |
Output is correct |
4 |
Correct |
0 ms |
43904 KB |
Output is correct |
5 |
Correct |
0 ms |
43904 KB |
Output is correct |
6 |
Correct |
0 ms |
43904 KB |
Output is correct |
7 |
Correct |
0 ms |
43904 KB |
Output is correct |
8 |
Correct |
0 ms |
43904 KB |
Output is correct |
9 |
Correct |
0 ms |
43904 KB |
Output is correct |
10 |
Correct |
0 ms |
43904 KB |
Output is correct |
11 |
Correct |
0 ms |
43904 KB |
Output is correct |
12 |
Correct |
0 ms |
43904 KB |
Output is correct |
13 |
Correct |
1 ms |
43904 KB |
Output is correct |
14 |
Correct |
0 ms |
43904 KB |
Output is correct |
15 |
Correct |
0 ms |
43904 KB |
Output is correct |
16 |
Correct |
1 ms |
43904 KB |
Output is correct |
17 |
Correct |
0 ms |
43904 KB |
Output is correct |
18 |
Correct |
0 ms |
43904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
43904 KB |
Output is correct |
2 |
Correct |
0 ms |
43904 KB |
Output is correct |
3 |
Correct |
0 ms |
43904 KB |
Output is correct |
4 |
Correct |
0 ms |
43904 KB |
Output is correct |
5 |
Correct |
0 ms |
43904 KB |
Output is correct |
6 |
Correct |
0 ms |
43904 KB |
Output is correct |
7 |
Correct |
0 ms |
43904 KB |
Output is correct |
8 |
Correct |
0 ms |
43904 KB |
Output is correct |
9 |
Correct |
0 ms |
43904 KB |
Output is correct |
10 |
Correct |
0 ms |
43904 KB |
Output is correct |
11 |
Correct |
0 ms |
43904 KB |
Output is correct |
12 |
Correct |
0 ms |
43904 KB |
Output is correct |
13 |
Correct |
0 ms |
43904 KB |
Output is correct |
14 |
Correct |
0 ms |
43904 KB |
Output is correct |
15 |
Correct |
0 ms |
43904 KB |
Output is correct |
16 |
Correct |
0 ms |
43904 KB |
Output is correct |
17 |
Correct |
0 ms |
43904 KB |
Output is correct |
18 |
Correct |
0 ms |
43904 KB |
Output is correct |
19 |
Correct |
0 ms |
43904 KB |
Output is correct |
20 |
Correct |
0 ms |
43904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
43904 KB |
Output is correct |
2 |
Correct |
1 ms |
43904 KB |
Output is correct |
3 |
Correct |
0 ms |
43904 KB |
Output is correct |
4 |
Correct |
0 ms |
43904 KB |
Output is correct |
5 |
Correct |
0 ms |
43904 KB |
Output is correct |
6 |
Correct |
0 ms |
43904 KB |
Output is correct |
7 |
Correct |
0 ms |
43904 KB |
Output is correct |
8 |
Correct |
0 ms |
43904 KB |
Output is correct |
9 |
Correct |
0 ms |
43904 KB |
Output is correct |
10 |
Correct |
0 ms |
43904 KB |
Output is correct |
11 |
Correct |
0 ms |
43904 KB |
Output is correct |
12 |
Correct |
0 ms |
43904 KB |
Output is correct |
13 |
Correct |
0 ms |
43904 KB |
Output is correct |
14 |
Correct |
0 ms |
43904 KB |
Output is correct |
15 |
Correct |
0 ms |
43904 KB |
Output is correct |
16 |
Correct |
0 ms |
43904 KB |
Output is correct |
17 |
Correct |
0 ms |
43904 KB |
Output is correct |
18 |
Correct |
0 ms |
43904 KB |
Output is correct |
19 |
Correct |
0 ms |
43904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
43904 KB |
Output is correct |
2 |
Correct |
7 ms |
43904 KB |
Output is correct |
3 |
Correct |
9 ms |
43904 KB |
Output is correct |
4 |
Correct |
38 ms |
43904 KB |
Output is correct |
5 |
Correct |
147 ms |
43904 KB |
Output is correct |
6 |
Correct |
10 ms |
43904 KB |
Output is correct |
7 |
Correct |
77 ms |
43904 KB |
Output is correct |
8 |
Correct |
0 ms |
43904 KB |
Output is correct |
9 |
Correct |
0 ms |
43904 KB |
Output is correct |
10 |
Correct |
0 ms |
43904 KB |
Output is correct |
11 |
Correct |
1 ms |
43904 KB |
Output is correct |
12 |
Correct |
0 ms |
43904 KB |
Output is correct |
13 |
Correct |
0 ms |
43904 KB |
Output is correct |
14 |
Correct |
14 ms |
43904 KB |
Output is correct |
15 |
Correct |
0 ms |
43904 KB |
Output is correct |
16 |
Correct |
0 ms |
43904 KB |
Output is correct |
17 |
Correct |
5 ms |
43904 KB |
Output is correct |
18 |
Correct |
3 ms |
43904 KB |
Output is correct |
19 |
Correct |
38 ms |
43904 KB |
Output is correct |
20 |
Correct |
160 ms |
43904 KB |
Output is correct |
21 |
Correct |
17 ms |
43904 KB |
Output is correct |
22 |
Correct |
85 ms |
43904 KB |
Output is correct |
23 |
Correct |
6 ms |
43904 KB |
Output is correct |
24 |
Correct |
17 ms |
43904 KB |
Output is correct |
25 |
Correct |
195 ms |
43904 KB |
Output is correct |
26 |
Correct |
0 ms |
43904 KB |
Output is correct |
27 |
Correct |
0 ms |
43904 KB |
Output is correct |
28 |
Correct |
15 ms |
43904 KB |
Output is correct |
29 |
Correct |
0 ms |
43904 KB |
Output is correct |
30 |
Correct |
6 ms |
43904 KB |
Output is correct |
31 |
Correct |
0 ms |
43904 KB |
Output is correct |
32 |
Correct |
7 ms |
43904 KB |
Output is correct |
33 |
Correct |
46 ms |
43904 KB |
Output is correct |
34 |
Correct |
191 ms |
43904 KB |
Output is correct |
35 |
Correct |
12 ms |
43904 KB |
Output is correct |
36 |
Correct |
86 ms |
43904 KB |
Output is correct |
37 |
Correct |
0 ms |
43904 KB |
Output is correct |
38 |
Correct |
8 ms |
43904 KB |
Output is correct |
39 |
Correct |
1 ms |
43904 KB |
Output is correct |
40 |
Correct |
0 ms |
43904 KB |
Output is correct |
41 |
Correct |
0 ms |
43904 KB |
Output is correct |
42 |
Correct |
0 ms |
43904 KB |
Output is correct |