#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int maxk = 110;
int n, k;
int dp[maxn][maxk];
int a[maxn];
int tree[maxk][4*maxn];
void build(int tree_ind, int li=0, int ri=n, int index=1) {
if(li == ri) tree[tree_ind][index] = INT_MAX;
else {
int mid = (li + ri) / 2;
build(tree_ind, li, mid, 2*index);
build(tree_ind, mid+1, ri, 2*index+1);
tree[tree_ind][index] = min(tree[tree_ind][2*index], tree[tree_ind][2*index+1]);
}
}
void update(int tree_ind, int ind, int val, int li=0, int ri=n, int index=1) {
if(li==ri) {
tree[tree_ind][index] = val;
}
else {
int mid = (li + ri) / 2;
if(ind <= mid) update(tree_ind, ind, val, li, mid, 2*index);
else update(tree_ind, ind, val, mid+1, ri, 2*index+1);
tree[tree_ind][index] = min(tree[tree_ind][2*index], tree[tree_ind][2*index+1]);
//cout<<"For the range: "<<li<<" "<<ri<<" -> "<<tree[tree_ind][index]<<"\n";
}
}
int query(int tree_ind, int ql, int qr, int li=0, int ri=n, int index=1) {
//cout<<"Discovering "<<li<<" "<<ri<<" with val "<<tree[tree_ind][index]<<" at "<<index<<"\n";
if(li > qr || ri < ql) return INT_MAX;
else if(li >= ql && ri <= qr){
//cout<<"Ulaza "<<li<<" "<<ri<<" - "<<tree[tree_ind][index]<<"\n";
return tree[tree_ind][index];
}
else {
int mid = (li + ri) / 2;
return min(query(tree_ind, ql, qr, li, mid, 2*index), query(tree_ind, ql, qr, mid+1, ri, 2*index+1));
}
}
int prev_ind[maxn];
void preprocess() {
deque<pair<int,int> > dq;
dq.push_back(mp(a[1], 1));
prev_ind[1] = -1;
for(int i=2;i<=n;i++) {
while(dq.size() > 0 && dq.back().first <= a[i]) {
dq.pop_back();
}
if(dq.size() == 0) prev_ind[i] = -1;
else prev_ind[i] = dq.back().second;
dq.push_back(mp(a[i], i));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
int prefmax = 0;
for(int i=1;i<=k;i++){
build(i);
}
for(int i=1;i<=n;i++) {
cin>>a[i];
prefmax = max(prefmax, a[i]);
dp[i][1] = prefmax;
update(1, i, dp[i][1]);
}
preprocess();
for(int i=2;i<=n;i++) {
for(int d=2;d<=k;d++) {
if(i < d) continue;
if(prev_ind[i] == -1) {
dp[i][d] = query(d-1, d-1, i-1) + a[i];
//cout<<i<<": "<<query(d-1, d-1, i-1)<<" "<<dp[i][d]<<"\n";
}
else {
dp[i][d] = INT_MAX;
if(prev_ind[i] >= d) dp[i][d] = dp[prev_ind[i]][d];
dp[i][d] = min(dp[i][d], query(d-1, max(prev_ind[i], d-1), i-1) + a[i]);
}
update(d, i, dp[i][d]);
}
}
cout<<dp[n][k]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
1152 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
768 KB |
Output is correct |
18 |
Correct |
6 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
8064 KB |
Output is correct |
2 |
Correct |
26 ms |
20352 KB |
Output is correct |
3 |
Correct |
118 ms |
23548 KB |
Output is correct |
4 |
Correct |
955 ms |
59256 KB |
Output is correct |
5 |
Execution timed out |
1105 ms |
147320 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |