using namespace std;
#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>
#include <queue>
#define MAXN 100005
#define MAXM 1000005
long long st[262144];
void update(int x,long long v,int l,int r,int p) {
if(l > x || r < x) return;
if(l == r) {
st[p] = v;
return;
}
int m = (l + r) / 2;
update(x,v,l,m,2 * p + 1);
update(x,v,m + 1,r,2 * p + 2);
st[p] = max(st[2 * p + 1],st[2 * p + 2]);
return;
}
void update(int x,long long v) {
update(x,v,0,MAXN,0);
}
long long getMax(int a,int b,int l,int r,int p) {
if(l > b || r < a) return -99999999999;
if(l >= a && r <= b) return st[p];
int m = (l + r) / 2;
return max(getMax(a,b,l,m,2 * p + 1),getMax(a,b,m + 1,r,2 * p + 2));
}
long long getMax(int a,int b) {
return getMax(a,b,0,MAXN,0);
}
int N,K;
long long arr[MAXN],dp[105][MAXN],sum;
int main() {
cin >> N >> K;
for(int i = 0;i < N;++i) {
cin >> arr[i];
update(i,arr[i]);
sum += arr[i];
dp[0][i] = arr[i];
if(i != 0) dp[0][i] = max(dp[0][i],dp[0][i - 1]);
}
for(int i = 1;i < K;++i) {
priority_queue<pair<long long,int>> pq;
for(int j = i;j < N;++j) {
dp[i][j] = dp[i - 1][i - 1] + getMax(i,j);
while(!pq.empty() && pq.top().second <= arr[j]) {
pq.pop();
}
if(!pq.empty()) {
dp[i][j] = min(dp[i][j],pq.top().first);
}
pq.push({dp[i - 1][j - 1] + j,j});
}
}
cout << dp[K - 1][N - 1] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |