This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 1e5 + 10;
int n, kk, arr[maxn], rmq[maxn][20], lg[maxn];
int dp[101][maxn];
int Max(int i, int j) {
int l = lg[j-i+1];
return max(rmq[i][l], rmq[j - (1 << l) + 1][l]);
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
cin>>n>>kk;
for(int i=1; i<=n; i++) {
cin>>arr[i];
rmq[i][0] = arr[i];
} lg[1] = 0;
for(int i = 2; i <= n; i++)
lg[i] = lg[i>>1] + 1;
for(int i = 1; i<=20; i++)
for(int j=1; j <= n; j++)
if(j + (1 << (i-1)) <= n)
rmq[j][i] = max(rmq[j][i-1], rmq[j + (1 << (i-1))][i-1]);
memset(dp, 0x3f, sizeof dp);
for(int i=1; i<=n; i++)
dp[1][i] = Max(1, i);
for(int k=2; k <= kk; k++) {
for(int i=1; i <= n; i++) {
for(int j = 1; j < i; j++)
dp[k][i] = min(dp[k][i], dp[k-1][j] + Max(j+1, i));
}
} cout<<dp[kk][n]<<endl;
}
# | 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... |