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>
using namespace std;
int dp[100'002][102], a[100'002], pref[100'002];
int mx[4 * 100'002];
void Build(int v, int vl, int vr)
{
if (vl == vr)
{
mx[v] = a[vl];
return;
}
int m = (vl + vr) / 2;
Build(v * 2, vl, m);
Build(v * 2 + 1, m + 1, vr);
mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
}
int Mx(int v, int vl, int vr, int l, int r)
{
if (vl == vr)
return mx[v];
int res = 0;
int m = (vl + vr) / 2;
if (l <= m)
res = max(res, Mx(v * 2, vl, m, l, min(r, m)));
if (r > m)res = max(res, Mx(v * 2 + 1, m + 1, vr, max(l, m + 1), r));
return res;
}
int main()
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
Build(1, 1, n);
for (int i = 1; i <= n; i++)
for (int curk = 1; curk <= k; curk++)
dp[i][curk] = 1'000'000'000;
if (k != 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(1, 1, n, i - sz + 1, i));
}*/
dp[i][curk] = min(dp[i][curk], dp[i - sz][curk - 1] + Mx(1, 1, n, i - sz + 1, i));
}
/*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... |