/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author win11905
*/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define iii tuple<int, int, int>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 1e5+5;
const int K = 1e2+5;
class blocks {
private:
int n, k;
int dp[K][N];
int A[N];
public:
void solve(istream& cin, ostream& cout) {
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> A[i];
for(int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i-1], A[i]);
for(int i = 2; i <= k; ++i) {
stack<iii> stk;
for(int j = i; j <= n; ++j) {
int mn = dp[i-1][j-1];
while(!stk.empty() && get<0>(stk.top()) <= A[j]) mn = min(mn, get<1>(stk.top())), stk.pop();
stk.emplace(A[j], mn, min(mn + A[j], stk.empty() ? INF : get<2>(stk.top())));
dp[i][j] = get<2>(stk.top());
}
}
cout << dp[k][n] << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
blocks *obj = new blocks();
obj->solve(in, out);
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
41848 KB |
Output is correct |
2 |
Correct |
30 ms |
41848 KB |
Output is correct |
3 |
Correct |
29 ms |
41848 KB |
Output is correct |
4 |
Correct |
30 ms |
41848 KB |
Output is correct |
5 |
Correct |
29 ms |
41848 KB |
Output is correct |
6 |
Correct |
29 ms |
41848 KB |
Output is correct |
7 |
Correct |
29 ms |
41848 KB |
Output is correct |
8 |
Correct |
81 ms |
41848 KB |
Output is correct |
9 |
Correct |
32 ms |
41784 KB |
Output is correct |
10 |
Correct |
34 ms |
41848 KB |
Output is correct |
11 |
Correct |
34 ms |
41844 KB |
Output is correct |
12 |
Correct |
35 ms |
41848 KB |
Output is correct |
13 |
Correct |
35 ms |
41848 KB |
Output is correct |
14 |
Correct |
34 ms |
41848 KB |
Output is correct |
15 |
Correct |
35 ms |
41848 KB |
Output is correct |
16 |
Correct |
35 ms |
41848 KB |
Output is correct |
17 |
Correct |
34 ms |
41848 KB |
Output is correct |
18 |
Correct |
34 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41820 KB |
Output is correct |
3 |
Correct |
34 ms |
41848 KB |
Output is correct |
4 |
Correct |
34 ms |
41848 KB |
Output is correct |
5 |
Correct |
30 ms |
41848 KB |
Output is correct |
6 |
Correct |
35 ms |
41796 KB |
Output is correct |
7 |
Correct |
35 ms |
41848 KB |
Output is correct |
8 |
Correct |
36 ms |
41848 KB |
Output is correct |
9 |
Correct |
30 ms |
41848 KB |
Output is correct |
10 |
Correct |
29 ms |
41848 KB |
Output is correct |
11 |
Correct |
30 ms |
41848 KB |
Output is correct |
12 |
Correct |
29 ms |
41848 KB |
Output is correct |
13 |
Correct |
57 ms |
41976 KB |
Output is correct |
14 |
Correct |
34 ms |
41848 KB |
Output is correct |
15 |
Correct |
29 ms |
41848 KB |
Output is correct |
16 |
Correct |
35 ms |
41848 KB |
Output is correct |
17 |
Correct |
34 ms |
41848 KB |
Output is correct |
18 |
Correct |
34 ms |
41848 KB |
Output is correct |
19 |
Correct |
35 ms |
41848 KB |
Output is correct |
20 |
Correct |
35 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41848 KB |
Output is correct |
3 |
Correct |
34 ms |
41848 KB |
Output is correct |
4 |
Correct |
35 ms |
41976 KB |
Output is correct |
5 |
Correct |
35 ms |
41848 KB |
Output is correct |
6 |
Correct |
34 ms |
41848 KB |
Output is correct |
7 |
Correct |
34 ms |
41848 KB |
Output is correct |
8 |
Correct |
35 ms |
41848 KB |
Output is correct |
9 |
Correct |
35 ms |
41848 KB |
Output is correct |
10 |
Correct |
36 ms |
41848 KB |
Output is correct |
11 |
Correct |
34 ms |
41848 KB |
Output is correct |
12 |
Correct |
35 ms |
41848 KB |
Output is correct |
13 |
Correct |
35 ms |
41848 KB |
Output is correct |
14 |
Correct |
37 ms |
41848 KB |
Output is correct |
15 |
Correct |
35 ms |
41848 KB |
Output is correct |
16 |
Correct |
35 ms |
41848 KB |
Output is correct |
17 |
Correct |
35 ms |
41848 KB |
Output is correct |
18 |
Correct |
34 ms |
41848 KB |
Output is correct |
19 |
Correct |
35 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41848 KB |
Output is correct |
2 |
Correct |
39 ms |
41848 KB |
Output is correct |
3 |
Correct |
43 ms |
41848 KB |
Output is correct |
4 |
Correct |
73 ms |
41860 KB |
Output is correct |
5 |
Correct |
137 ms |
42616 KB |
Output is correct |
6 |
Correct |
46 ms |
42588 KB |
Output is correct |
7 |
Correct |
83 ms |
42616 KB |
Output is correct |
8 |
Correct |
35 ms |
41848 KB |
Output is correct |
9 |
Correct |
35 ms |
41848 KB |
Output is correct |
10 |
Correct |
35 ms |
41848 KB |
Output is correct |
11 |
Correct |
35 ms |
41976 KB |
Output is correct |
12 |
Correct |
35 ms |
41848 KB |
Output is correct |
13 |
Correct |
35 ms |
41848 KB |
Output is correct |
14 |
Correct |
47 ms |
41992 KB |
Output is correct |
15 |
Correct |
35 ms |
41848 KB |
Output is correct |
16 |
Correct |
40 ms |
41932 KB |
Output is correct |
17 |
Correct |
39 ms |
42104 KB |
Output is correct |
18 |
Correct |
42 ms |
42104 KB |
Output is correct |
19 |
Correct |
79 ms |
42104 KB |
Output is correct |
20 |
Correct |
162 ms |
42592 KB |
Output is correct |
21 |
Correct |
48 ms |
42532 KB |
Output is correct |
22 |
Correct |
98 ms |
42616 KB |
Output is correct |
23 |
Correct |
44 ms |
42616 KB |
Output is correct |
24 |
Correct |
55 ms |
42572 KB |
Output is correct |
25 |
Correct |
163 ms |
42668 KB |
Output is correct |
26 |
Correct |
34 ms |
41860 KB |
Output is correct |
27 |
Correct |
34 ms |
41976 KB |
Output is correct |
28 |
Correct |
47 ms |
41976 KB |
Output is correct |
29 |
Correct |
31 ms |
41980 KB |
Output is correct |
30 |
Correct |
39 ms |
41976 KB |
Output is correct |
31 |
Correct |
39 ms |
42232 KB |
Output is correct |
32 |
Correct |
43 ms |
42360 KB |
Output is correct |
33 |
Correct |
79 ms |
42424 KB |
Output is correct |
34 |
Correct |
168 ms |
43136 KB |
Output is correct |
35 |
Correct |
46 ms |
43000 KB |
Output is correct |
36 |
Correct |
101 ms |
43052 KB |
Output is correct |
37 |
Correct |
36 ms |
41976 KB |
Output is correct |
38 |
Correct |
45 ms |
41848 KB |
Output is correct |
39 |
Correct |
34 ms |
41848 KB |
Output is correct |
40 |
Correct |
81 ms |
41848 KB |
Output is correct |
41 |
Correct |
29 ms |
41848 KB |
Output is correct |
42 |
Correct |
29 ms |
41848 KB |
Output is correct |