#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define inf 1e18
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
vector <ll> v(n);
for (auto &i : v) cin >> i;
vector < vector <ll> > dp(n + 1, vector <ll> (k + 1));
for (int i = 1; i <= n; ++i) dp[i][0] = inf;
stack < pair <ll, ll> > st;
for (int i = 1; i <= k; ++i) {
while (!st.empty()) st.pop();
dp[i][i] = dp[i - 1][i - 1] + v[i - 1]; st.push({v[i - 1], dp[i - 1][i - 1]});
for (int j = i + 1; j <= n; ++j) {
ll c = dp[j - 1][i - 1];
while (!st.empty() && st.top().fi <= v[j - 1]) {
c = min(c, st.top().se);
st.pop();
}
if (st.empty() || st.top().fi + st.top().se > v[j - 1] + c) st.push({v[j - 1], c});
dp[j][i] = st.top().fi + st.top().se;
}
}
cout << dp[n][k];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// ifstream cin("input.txt");
// ofstream cout("output.txt");
int q = 1; // cin >> q;
while (q--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2944 KB |
Output is correct |
2 |
Correct |
10 ms |
3456 KB |
Output is correct |
3 |
Correct |
14 ms |
5596 KB |
Output is correct |
4 |
Correct |
78 ms |
29184 KB |
Output is correct |
5 |
Correct |
352 ms |
84088 KB |
Output is correct |
6 |
Correct |
23 ms |
8832 KB |
Output is correct |
7 |
Correct |
134 ms |
41720 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
23 ms |
8704 KB |
Output is correct |
15 |
Correct |
3 ms |
1024 KB |
Output is correct |
16 |
Correct |
9 ms |
2944 KB |
Output is correct |
17 |
Correct |
9 ms |
3456 KB |
Output is correct |
18 |
Correct |
13 ms |
5504 KB |
Output is correct |
19 |
Correct |
72 ms |
29224 KB |
Output is correct |
20 |
Correct |
284 ms |
84088 KB |
Output is correct |
21 |
Correct |
23 ms |
8832 KB |
Output is correct |
22 |
Correct |
161 ms |
41720 KB |
Output is correct |
23 |
Correct |
20 ms |
7296 KB |
Output is correct |
24 |
Correct |
33 ms |
13560 KB |
Output is correct |
25 |
Correct |
320 ms |
84088 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
31 ms |
8704 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
9 ms |
2944 KB |
Output is correct |
31 |
Correct |
9 ms |
3456 KB |
Output is correct |
32 |
Correct |
17 ms |
5760 KB |
Output is correct |
33 |
Correct |
118 ms |
29440 KB |
Output is correct |
34 |
Correct |
372 ms |
84600 KB |
Output is correct |
35 |
Correct |
24 ms |
9472 KB |
Output is correct |
36 |
Correct |
175 ms |
42360 KB |
Output is correct |
37 |
Correct |
4 ms |
1024 KB |
Output is correct |
38 |
Correct |
20 ms |
8704 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |