#include <iostream>
using namespace std;
using ll = long long;
const int LOG = 20;
const int maxn = 1e5 + 5;
int log_dp[maxn+5];
int T[maxn+5][LOG];
ll dp[maxn+5][105];
int query(int l, int r) {
int k = log_dp[r-l+1];
return max(T[l][k], T[r-(1<<k)+1][k]);
}
void f(int l, int r, int tl, int tr, int j) {
if(l > r) return ;
int tm = (l + r) / 2, p = tl;
for(int i=tl; i<=min(tm, tr); i++) {
ll v = dp[i-1][j-1] + query(i, tm);
if(v < dp[tm][j]) {
dp[tm][j] = v;
p = i;
}
}
f(l, tm-1, tl, p, j);
f(tm+1, r, p, tr, j);
}
signed main() {
log_dp[1] = 0;
for(int i=2; i<=maxn; i++) log_dp[i] = log_dp[i/2] + 1;
int n, k;
cin >> n >> k;
int v[n+1];
for(int i=1; i<=n; i++) cin >> v[i], T[i][0] = v[i];
for(int j=1; j<LOG; j++)
for(int i=1; i+(1<<j)-1<=n; i++)
T[i][j] = max(T[i][j-1], T[i+(1<<(j-1))][j-1]);
for(int i=0; i<=n; i++)
for(int j=0; j<=k; j++) dp[i][j] = 1e18;
dp[0][0] = 0;
for(int j=1; j<=k; j++) f(1, n, 1, n, j);
cout << dp[n][k] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |