#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod1 = (ll)1e9 + 7;
const ll mod2 = (ll)1e9 + 9;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
struct holder {
ll mx, val, st_min;
};
struct cool_stack {
vector<holder> st;
void push(ll val, ll dp) {
ll min_dp = dp;
while (!st.empty() and st.back().mx <= val) {
min_dp = min(min_dp, st.back().val);
st.pop_back();
}
ll minimal = inf;
if (!st.empty()) {
minimal = st.back().st_min;
}
st.pb({val, min_dp, min(minimal, min_dp + val)});
}
ll get() {
if (st.empty()) {
return inf;
}
return st.back().st_min;
}
void pop() {
if (!st.empty()) {
st.pop_back();
}
}
};
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, inf));
dp[0][0] = 0;
for (int b = 1; b <= k; b++) {
cool_stack helper;
for (int i = 1; i <= n; i++) {
helper.push(arr[i], dp[b - 1][i - 1]);
dp[b][i] = helper.get();
}
}
cout << dp[k][n];
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
428 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
256 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2660 KB |
Output is correct |
2 |
Correct |
10 ms |
2048 KB |
Output is correct |
3 |
Correct |
15 ms |
4096 KB |
Output is correct |
4 |
Correct |
77 ms |
28160 KB |
Output is correct |
5 |
Correct |
219 ms |
81912 KB |
Output is correct |
6 |
Correct |
22 ms |
5752 KB |
Output is correct |
7 |
Correct |
108 ms |
38776 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
29 ms |
8468 KB |
Output is correct |
15 |
Correct |
6 ms |
768 KB |
Output is correct |
16 |
Correct |
10 ms |
2560 KB |
Output is correct |
17 |
Correct |
8 ms |
2048 KB |
Output is correct |
18 |
Correct |
19 ms |
4096 KB |
Output is correct |
19 |
Correct |
94 ms |
28280 KB |
Output is correct |
20 |
Correct |
254 ms |
81912 KB |
Output is correct |
21 |
Correct |
21 ms |
5760 KB |
Output is correct |
22 |
Correct |
124 ms |
38672 KB |
Output is correct |
23 |
Correct |
17 ms |
4224 KB |
Output is correct |
24 |
Correct |
40 ms |
11264 KB |
Output is correct |
25 |
Correct |
250 ms |
81984 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
28 ms |
8656 KB |
Output is correct |
29 |
Correct |
4 ms |
896 KB |
Output is correct |
30 |
Correct |
11 ms |
2780 KB |
Output is correct |
31 |
Correct |
8 ms |
2468 KB |
Output is correct |
32 |
Correct |
16 ms |
4600 KB |
Output is correct |
33 |
Correct |
117 ms |
28740 KB |
Output is correct |
34 |
Correct |
375 ms |
83744 KB |
Output is correct |
35 |
Correct |
27 ms |
7416 KB |
Output is correct |
36 |
Correct |
188 ms |
40516 KB |
Output is correct |
37 |
Correct |
10 ms |
768 KB |
Output is correct |
38 |
Correct |
24 ms |
8448 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
256 KB |
Output is correct |