#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i<= b; ++i)
#define FORD(i, a, b) for (int i = a; i>=b; --i)
#define REP(i, a) for (int i = 0; i<a; ++i)
#define DEBUG(x) { cerr << #x << " = " << x << endl; }
#define Arr(A, l, r) { cerr << #A << " = "; FOR(_, l, r) cerr << A[_] << ' '; cerr << endl; }
#define N 100100
#define pp pair<int, int>
#define next __next
#define prev __prev
#define __builtin_popcount __builtin_popcountll
#define bit(S, i) (((S) >> i) & 1)
#define IO cin.tie(NULL);cout.tie(NULL);
#define taskname "Test"
using namespace std;
template<class X, class Y> inline void Max(X &x, Y y) { x = x > y ? x : y; }
template<class X, class Y> inline void Min(X &x, Y y) { x = x < y ? x : y; }
/*
dp[i][j] = min(dp[i - 1][k] + RMQ[k + 1][j]);
*/
int n, k, a[N];
int dp[111][N];
stack<int> st;
int main() {
IO;
scanf("%d %d", &n, &k);
FOR(i, 1, n) scanf("%d", &a[i]);
int mx = -1;
FOR(i, 1, n) Max(mx, a[i]), dp[1][i] = mx;
FOR(j, 2, k) {
while (!st.empty()) st.pop();
st.push(j - 1);
//dp[j][j] = dp[j - 1][j - 1] + a[j];
FOR(i, j, n) {
int best = 1e9;
while (!st.empty() && a[st.top()] <= a[i]) {
if (st.top() != j - 1) Min(best, dp[j][st.top()] + a[i] - a[st.top()]);
Min(best, dp[j - 1][st.top()] + a[i]);
st.pop();
}
if (!st.empty()) {
if (st.top() != j - 1) Min(best, dp[j][st.top()]);
Min(best, dp[j - 1][st.top()] + a[i]);
}
else Min(best, dp[j - 1][j - 1] + a[i]);
dp[j][i] = best;
// cerr << j << ' ' << i << ' ' << best << endl;
st.push(i);
}
}
printf("%d", dp[k][n]);
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:31:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
^
blocks.cpp:32:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
FOR(i, 1, n) scanf("%d", &a[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
45812 KB |
Output is correct |
2 |
Correct |
0 ms |
45812 KB |
Output is correct |
3 |
Correct |
0 ms |
45812 KB |
Output is correct |
4 |
Correct |
0 ms |
45812 KB |
Output is correct |
5 |
Correct |
0 ms |
45812 KB |
Output is correct |
6 |
Correct |
0 ms |
45812 KB |
Output is correct |
7 |
Correct |
0 ms |
45812 KB |
Output is correct |
8 |
Correct |
0 ms |
45812 KB |
Output is correct |
9 |
Correct |
0 ms |
45812 KB |
Output is correct |
10 |
Correct |
0 ms |
45812 KB |
Output is correct |
11 |
Correct |
0 ms |
45812 KB |
Output is correct |
12 |
Correct |
0 ms |
45812 KB |
Output is correct |
13 |
Correct |
0 ms |
45812 KB |
Output is correct |
14 |
Correct |
0 ms |
45812 KB |
Output is correct |
15 |
Correct |
0 ms |
45812 KB |
Output is correct |
16 |
Correct |
0 ms |
45812 KB |
Output is correct |
17 |
Correct |
0 ms |
45812 KB |
Output is correct |
18 |
Correct |
0 ms |
45812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
45812 KB |
Output is correct |
2 |
Correct |
0 ms |
45812 KB |
Output is correct |
3 |
Correct |
0 ms |
45812 KB |
Output is correct |
4 |
Correct |
0 ms |
45812 KB |
Output is correct |
5 |
Correct |
0 ms |
45812 KB |
Output is correct |
6 |
Correct |
0 ms |
45812 KB |
Output is correct |
7 |
Correct |
0 ms |
45812 KB |
Output is correct |
8 |
Correct |
0 ms |
45812 KB |
Output is correct |
9 |
Correct |
0 ms |
45812 KB |
Output is correct |
10 |
Correct |
0 ms |
45812 KB |
Output is correct |
11 |
Correct |
0 ms |
45812 KB |
Output is correct |
12 |
Correct |
0 ms |
45812 KB |
Output is correct |
13 |
Correct |
0 ms |
45812 KB |
Output is correct |
14 |
Correct |
0 ms |
45812 KB |
Output is correct |
15 |
Correct |
0 ms |
45812 KB |
Output is correct |
16 |
Correct |
0 ms |
45812 KB |
Output is correct |
17 |
Correct |
0 ms |
45812 KB |
Output is correct |
18 |
Correct |
0 ms |
45812 KB |
Output is correct |
19 |
Correct |
0 ms |
45812 KB |
Output is correct |
20 |
Correct |
0 ms |
45812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
45812 KB |
Output is correct |
2 |
Correct |
0 ms |
45812 KB |
Output is correct |
3 |
Correct |
0 ms |
45812 KB |
Output is correct |
4 |
Correct |
0 ms |
45812 KB |
Output is correct |
5 |
Correct |
0 ms |
45812 KB |
Output is correct |
6 |
Correct |
0 ms |
45812 KB |
Output is correct |
7 |
Correct |
0 ms |
45812 KB |
Output is correct |
8 |
Correct |
0 ms |
45812 KB |
Output is correct |
9 |
Correct |
0 ms |
45812 KB |
Output is correct |
10 |
Correct |
0 ms |
45812 KB |
Output is correct |
11 |
Correct |
0 ms |
45812 KB |
Output is correct |
12 |
Correct |
0 ms |
45812 KB |
Output is correct |
13 |
Correct |
0 ms |
45812 KB |
Output is correct |
14 |
Correct |
0 ms |
45812 KB |
Output is correct |
15 |
Correct |
0 ms |
45812 KB |
Output is correct |
16 |
Correct |
0 ms |
45812 KB |
Output is correct |
17 |
Correct |
0 ms |
45812 KB |
Output is correct |
18 |
Correct |
0 ms |
45812 KB |
Output is correct |
19 |
Correct |
0 ms |
45812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
45812 KB |
Output is correct |
2 |
Correct |
6 ms |
45812 KB |
Output is correct |
3 |
Correct |
9 ms |
45812 KB |
Output is correct |
4 |
Correct |
56 ms |
45812 KB |
Output is correct |
5 |
Correct |
149 ms |
45812 KB |
Output is correct |
6 |
Correct |
9 ms |
45812 KB |
Output is correct |
7 |
Correct |
79 ms |
45812 KB |
Output is correct |
8 |
Correct |
0 ms |
45812 KB |
Output is correct |
9 |
Correct |
0 ms |
45812 KB |
Output is correct |
10 |
Correct |
0 ms |
45812 KB |
Output is correct |
11 |
Correct |
0 ms |
45812 KB |
Output is correct |
12 |
Correct |
0 ms |
45812 KB |
Output is correct |
13 |
Correct |
0 ms |
45812 KB |
Output is correct |
14 |
Correct |
16 ms |
45812 KB |
Output is correct |
15 |
Correct |
0 ms |
45812 KB |
Output is correct |
16 |
Correct |
6 ms |
45812 KB |
Output is correct |
17 |
Correct |
9 ms |
45812 KB |
Output is correct |
18 |
Correct |
16 ms |
45812 KB |
Output is correct |
19 |
Correct |
69 ms |
45812 KB |
Output is correct |
20 |
Correct |
219 ms |
45812 KB |
Output is correct |
21 |
Correct |
33 ms |
45812 KB |
Output is correct |
22 |
Correct |
99 ms |
45812 KB |
Output is correct |
23 |
Correct |
13 ms |
45812 KB |
Output is correct |
24 |
Correct |
49 ms |
45812 KB |
Output is correct |
25 |
Correct |
199 ms |
45812 KB |
Output is correct |
26 |
Correct |
0 ms |
45812 KB |
Output is correct |
27 |
Correct |
0 ms |
45812 KB |
Output is correct |
28 |
Correct |
16 ms |
45812 KB |
Output is correct |
29 |
Correct |
0 ms |
45812 KB |
Output is correct |
30 |
Correct |
6 ms |
45812 KB |
Output is correct |
31 |
Correct |
9 ms |
45812 KB |
Output is correct |
32 |
Correct |
9 ms |
45944 KB |
Output is correct |
33 |
Correct |
73 ms |
45944 KB |
Output is correct |
34 |
Correct |
239 ms |
45944 KB |
Output is correct |
35 |
Correct |
16 ms |
45944 KB |
Output is correct |
36 |
Correct |
96 ms |
45944 KB |
Output is correct |
37 |
Correct |
0 ms |
45812 KB |
Output is correct |
38 |
Correct |
16 ms |
45812 KB |
Output is correct |
39 |
Correct |
0 ms |
45812 KB |
Output is correct |
40 |
Correct |
0 ms |
45812 KB |
Output is correct |
41 |
Correct |
0 ms |
45812 KB |
Output is correct |
42 |
Correct |
0 ms |
45812 KB |
Output is correct |