#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define fi first
#define se second
#define inf 1e9
using namespace std;
typedef pair <int, int> ii;
const int N = 1e5 + 9;
int n, k, a[N], dp[N][109], st[N << 2], L[N];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("nhap.inp", "r", stdin);
// freopen("xuat.out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[1][1] = a[1];
FOR(i, 2, n) dp[i][1] = max(dp[i-1][1], a[i]);
for (int t = 2; t <= k; ++t){
stack <ii> st;
for (int i = t; i <= n; ++i) {
int minF = dp[i-1][t-1];
while (!st.empty() && a[st.top().se] <= a[i]){
minF = min(minF, st.top().fi);
st.pop();
}
dp[i][t] = (dp[st.empty() ? 0 : st.top().se][t], minF + a[i]);
st.push({minF, i});
}
}
cout << dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
44888 KB |
Output is correct |
2 |
Correct |
6 ms |
44772 KB |
Output is correct |
3 |
Correct |
6 ms |
44892 KB |
Output is correct |
4 |
Correct |
7 ms |
44888 KB |
Output is correct |
5 |
Correct |
5 ms |
44900 KB |
Output is correct |
6 |
Correct |
6 ms |
44888 KB |
Output is correct |
7 |
Correct |
6 ms |
44888 KB |
Output is correct |
8 |
Correct |
6 ms |
44888 KB |
Output is correct |
9 |
Correct |
6 ms |
44892 KB |
Output is correct |
10 |
Correct |
6 ms |
44888 KB |
Output is correct |
11 |
Correct |
6 ms |
44888 KB |
Output is correct |
12 |
Correct |
5 ms |
44892 KB |
Output is correct |
13 |
Incorrect |
6 ms |
44888 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
44888 KB |
Output is correct |
2 |
Correct |
5 ms |
44892 KB |
Output is correct |
3 |
Correct |
6 ms |
44892 KB |
Output is correct |
4 |
Correct |
5 ms |
44892 KB |
Output is correct |
5 |
Correct |
6 ms |
44888 KB |
Output is correct |
6 |
Correct |
6 ms |
45144 KB |
Output is correct |
7 |
Correct |
7 ms |
44892 KB |
Output is correct |
8 |
Correct |
5 ms |
44804 KB |
Output is correct |
9 |
Correct |
6 ms |
44892 KB |
Output is correct |
10 |
Correct |
6 ms |
44892 KB |
Output is correct |
11 |
Correct |
6 ms |
44892 KB |
Output is correct |
12 |
Correct |
6 ms |
44888 KB |
Output is correct |
13 |
Correct |
5 ms |
44888 KB |
Output is correct |
14 |
Incorrect |
6 ms |
44888 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
44888 KB |
Output is correct |
2 |
Correct |
6 ms |
44772 KB |
Output is correct |
3 |
Correct |
6 ms |
44892 KB |
Output is correct |
4 |
Correct |
7 ms |
44888 KB |
Output is correct |
5 |
Correct |
5 ms |
44900 KB |
Output is correct |
6 |
Correct |
6 ms |
44888 KB |
Output is correct |
7 |
Correct |
6 ms |
44888 KB |
Output is correct |
8 |
Correct |
6 ms |
44888 KB |
Output is correct |
9 |
Correct |
6 ms |
44892 KB |
Output is correct |
10 |
Correct |
6 ms |
44888 KB |
Output is correct |
11 |
Correct |
6 ms |
44888 KB |
Output is correct |
12 |
Correct |
5 ms |
44892 KB |
Output is correct |
13 |
Incorrect |
6 ms |
44888 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
44888 KB |
Output is correct |
2 |
Correct |
6 ms |
44772 KB |
Output is correct |
3 |
Correct |
6 ms |
44892 KB |
Output is correct |
4 |
Correct |
7 ms |
44888 KB |
Output is correct |
5 |
Correct |
5 ms |
44900 KB |
Output is correct |
6 |
Correct |
6 ms |
44888 KB |
Output is correct |
7 |
Correct |
6 ms |
44888 KB |
Output is correct |
8 |
Correct |
6 ms |
44888 KB |
Output is correct |
9 |
Correct |
6 ms |
44892 KB |
Output is correct |
10 |
Correct |
6 ms |
44888 KB |
Output is correct |
11 |
Correct |
6 ms |
44888 KB |
Output is correct |
12 |
Correct |
5 ms |
44892 KB |
Output is correct |
13 |
Incorrect |
6 ms |
44888 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |