#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
struct Element {
int maxv, cur, minp;
Element() {}
Element(int maxv, ll cur, ll minp) : maxv(maxv), cur(cur), minp(minp) {}
};
const int oo = 1e9 + 5;
const int N = 1e5 + 1;
const int K = 101;
int n, k, a[N], d[N][K];
deque<Element> d1[K];
void debugs(deque<Element> &s) {
EACH(i, s) {
cerr << i.maxv << ' ' << i.cur << ' ' << i.minp << '\n';
}
cerr << '\n';
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
FOR(i, 0, k) d[1][i] = oo;
d[1][1] = a[1];
FOR(i, 0, k) d1[i].pb(Element(a[1], d[1][i], d[1][i]));
FOR(i, 2, n) {
FOR(j, 0, k) {
int curv = (j? d[i - 1][j - 1] + a[i] : oo);
d1[j].pb(Element(a[i], curv, min(curv, (d1[j].empty()? oo : d1[j].bk().minp))));
int minv = oo;
WHILE(!d1[j].empty() && d1[j].bk().maxv <= a[i]) {
minv = min(minv, d1[j].bk().cur + a[i] - d1[j].bk().maxv);
d1[j]._pb();
}
d1[j].pb(Element(a[i], minv, min(minv, d1[j].empty()? oo : d1[j].bk().minp)));
}
d[i][0] = oo;
FOR(j, 1, k) d[i][j] = d1[j].bk().minp;
}
cout << d[n][k];
return 0;
}
/*
2 2
1 2
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message
blocks.cpp: In function 'void debugs(std::deque<Element>&)':
blocks.cpp:36:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
blocks.cpp:62:2: note: in expansion of macro 'EACH'
62 | EACH(i, s) {
| ^~~~
blocks.cpp: In function 'int main()':
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:72:5: note: in expansion of macro 'FOR'
72 | FOR(i, 1, n) cin >> a[i];
| ^~~
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:73:5: note: in expansion of macro 'FOR'
73 | FOR(i, 0, k) d[1][i] = oo;
| ^~~
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:75:5: note: in expansion of macro 'FOR'
75 | FOR(i, 0, k) d1[i].pb(Element(a[1], d[1][i], d[1][i]));
| ^~~
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:76:5: note: in expansion of macro 'FOR'
76 | FOR(i, 2, n) {
| ^~~
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:77:3: note: in expansion of macro 'FOR'
77 | FOR(j, 0, k) {
| ^~~
blocks.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
blocks.cpp:88:3: note: in expansion of macro 'FOR'
88 | FOR(j, 1, k) d[i][j] = d1[j].bk().minp;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
400 KB |
Output is correct |
3 |
Correct |
1 ms |
396 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
400 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
400 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
400 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
396 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
404 KB |
Output is correct |
11 |
Correct |
1 ms |
396 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
400 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
400 KB |
Output is correct |
3 |
Correct |
1 ms |
396 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
400 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
400 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
400 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
396 KB |
Output is correct |
28 |
Correct |
1 ms |
404 KB |
Output is correct |
29 |
Correct |
1 ms |
396 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
400 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
400 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
404 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
400 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
400 KB |
Output is correct |
3 |
Correct |
1 ms |
396 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
400 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
400 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
400 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
396 KB |
Output is correct |
28 |
Correct |
1 ms |
404 KB |
Output is correct |
29 |
Correct |
1 ms |
396 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
400 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
400 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
404 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
400 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
9 ms |
4376 KB |
Output is correct |
59 |
Correct |
12 ms |
18388 KB |
Output is correct |
60 |
Correct |
22 ms |
18316 KB |
Output is correct |
61 |
Correct |
92 ms |
18368 KB |
Output is correct |
62 |
Correct |
300 ms |
40968 KB |
Output is correct |
63 |
Correct |
34 ms |
40904 KB |
Output is correct |
64 |
Correct |
132 ms |
40904 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
404 KB |
Output is correct |
69 |
Correct |
1 ms |
788 KB |
Output is correct |
70 |
Correct |
1 ms |
724 KB |
Output is correct |
71 |
Correct |
27 ms |
4372 KB |
Output is correct |
72 |
Correct |
4 ms |
4368 KB |
Output is correct |
73 |
Correct |
9 ms |
4436 KB |
Output is correct |
74 |
Correct |
15 ms |
18324 KB |
Output is correct |
75 |
Correct |
22 ms |
18408 KB |
Output is correct |
76 |
Correct |
102 ms |
18392 KB |
Output is correct |
77 |
Correct |
281 ms |
41036 KB |
Output is correct |
78 |
Correct |
33 ms |
40928 KB |
Output is correct |
79 |
Correct |
143 ms |
40848 KB |
Output is correct |
80 |
Correct |
30 ms |
41024 KB |
Output is correct |
81 |
Correct |
64 ms |
40940 KB |
Output is correct |
82 |
Correct |
301 ms |
40944 KB |
Output is correct |
83 |
Correct |
1 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
852 KB |
Output is correct |
85 |
Correct |
34 ms |
8704 KB |
Output is correct |
86 |
Correct |
4 ms |
4580 KB |
Output is correct |
87 |
Correct |
13 ms |
5460 KB |
Output is correct |
88 |
Correct |
14 ms |
18796 KB |
Output is correct |
89 |
Correct |
21 ms |
19952 KB |
Output is correct |
90 |
Correct |
117 ms |
33008 KB |
Output is correct |
91 |
Correct |
324 ms |
84172 KB |
Output is correct |
92 |
Correct |
35 ms |
42572 KB |
Output is correct |
93 |
Correct |
155 ms |
60592 KB |
Output is correct |
94 |
Correct |
5 ms |
4452 KB |
Output is correct |
95 |
Correct |
29 ms |
4388 KB |
Output is correct |
96 |
Correct |
1 ms |
724 KB |
Output is correct |
97 |
Correct |
1 ms |
820 KB |
Output is correct |
98 |
Correct |
1 ms |
340 KB |
Output is correct |
99 |
Correct |
1 ms |
340 KB |
Output is correct |