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>
using namespace std;
int n, k, a[100002], lg2[100002], Left[100002];
int dp[102][200002];
int rmq[20][100002];
void calc_rmq (int ind) {
for (int i = 0; i <= n; i++)
rmq[0][i] = dp[ind][i];
for (int i = 1; (1 << i) <= n; i++)
for (int j = 0; j <= n - (1 << i) + 1; j++)
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
int query (int x, int y) {
int l = lg2[y - x + 1];
return min(rmq[l][x], rmq[l][y - (1 << l) + 1]);
}
int main()
{
cin >> n>> k;
for (int i = 2; i <= n; i++)
lg2[i] = lg2[i / 2] + 1;
for (int i = 1; i <= n; i++)
cin >> a[i];
stack<int>st;
st.push(0);
a[0] = 1e9;
for (int i = 1; i <= n; i++) {
while (a[st.top()] <= a[i])
st.pop();
Left[i] = st.top();
st.push(i);
}
for (int i = 0; i <= k; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = 1e9;
dp[0][0] = 0;
calc_rmq(0);
for (int i = 1; i <= k; i++) {
for (int j = i; j <= n; j++)
dp[i][j] = min(query(Left[j], j - 1) + a[j], dp[i][Left[j]]);
calc_rmq(i);
}
cout << dp[k][n] << "\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... |