This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define BUGO(x) cerr << #x << " = " << (x) << '\n';
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
using ll = long long;
const int N = 100'002;
const int K = 20;
int lg[N];
ll dp[N][102], a[N];
ll mx[N][K];
void Build(int n)
{
lg[1] = 0;
for (int i = 2; i <= n; i++)
lg[i] = lg[i / 2] + 1;
int k = lg[n];
for (int i = 0; i < n; i++)
mx[i][0] = a[i + 1];
for (int j = 1; j <= k; j++)
for (int i = 0; i + (1 << j) - 1 <= n; i++)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
}
ll Mx(int l, int r)
{
l--; r--;
int j = lg[r - l + 1];
return max(mx[l][j], mx[r - (1 << j) + 1][j]);
}
int main()
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
Build(n);
for (int i = 1; i <= n; i++)
for (int curk = 1; curk <= k; curk++)
dp[i][curk] = 1'000'000'000'000'000'000;
if (k != 1 || (k == 1 && n == 1))
dp[1][1] = a[1];
for (int i = 2; i <= n; i++)
for (int curk = max(1, k - n + i); curk <= min(i, k - ((n - i) ? 1 : 0)); curk++)
for (int sz = ((curk == 1) ? i : 1); sz <= i - curk + 1; sz++)
{
/*if (i == 5)
{
BUGO(curk);
BUGO(sz);
}*/
// BUGO(Mx(i - sz + 1, i));
dp[i][curk] = min(dp[i][curk], dp[i - sz][curk - 1] + Mx(i - sz + 1, i));
}
//for (int i = 0; i < n; i++)
//{
// for (int k = 0; k < lg[n]; k++)
// cout << mx[i][k] << ' ';
// cout << '\n';
//}
/*for (int i = 1; i <= n; i++)
{
for (int curk = 0; curk <= k; curk++)
cerr << dp[i][curk] << ' ';
cerr << '\n';
}*/
cout << dp[n][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... |