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 int long long
using namespace std;
const int N = 1e5 + 42, K = 110, INF = 1e15 + 42;
int n, k, a[N], dp[K][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
n++;
for(int i = 1; i < n; i++)
cin >> a[i];
for(int i = 1; i < N; i++)
dp[0][i] = INF;
for(int i = 1; i <= k; i++) {
for(int j = 0; j < n; j++)
dp[i][j] = INF;
vector<pair<int, int>> imax;
a[i-1] = INF;
imax.push_back({i-1, INF});
for(int j = i; j < n; j++) {
int mini = dp[i-1][j-1];
while(a[imax.back().first] <= a[j]) {
mini = min(mini, imax.back().second);
imax.pop_back();
}
dp[i][j] = min(a[j] + mini, dp[i][max(imax.back().first, i-1)]);
imax.push_back({j, mini});
}
}
cout << dp[k][n-1];
}
# | 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... |