This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DIM 100010
#define DIMK 110
#define INF 2000000000000000000LL
using namespace std;
long long dp[DIMK][DIM],aint[DIM*4],v[DIM];
int rmq[20][DIM],p[DIM];
deque <int> d;
int n,k,i,j,t;
int get_min (int x, int y){
int nr = p[y-x+1];
return min (rmq[nr][x],rmq[nr][y-(1<<nr)+1]);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>k;
for (i=1;i<=k;i++)
for (j=1;j<=n;j++)
dp[i][j] = INF;
for (i=1;i<=n;i++){
cin>>v[i];
dp[1][i] = max (dp[1][i-1],v[i]);
}
for (i=2;i<=n;i++)
p[i] = p[i/2] + 1;
for (j=2;j<=k;j++){
for (i=1;i<=n;i++)
rmq[0][i] = dp[j-1][i];
for (i=1;(1<<i)<=n;i++)
for (t=1;t<=n;t++){
rmq[i][t] = rmq[i-1][t];
if (t + (1<<(i-1)) <= n)
rmq[i][t] = min (rmq[i][t],rmq[i-1][t + (1<<(i-1))]);
}
d.clear();
for (i=j;i<=n;i++){
while (!d.empty() && v[i] >= v[d.back()])
d.pop_back();
if (d.empty())
dp[j][i] = v[i] + get_min(1,i-1);
else dp[j][i] = min (dp[j][d.back()],v[i] + get_min(d.back(),i-1));
d.push_back(i);
}
}
cout<<dp[k][n];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |