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 <iostream>
using namespace std;
const int inf = 1e9 + 5;
const int N = 1e5 + 5;
int n, k, a[N], dp[N], bef[N];
pair < int, int > p[N];
int main () {
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
bef[i] = max(bef[i - 1], a[i]);
}
for(int j = 2; j <= k; j++) {
int sz = 0;
for(int i = 1; i <= n; i++) {
int ans = j <= i ? bef[i - 1] : inf;
bef[i - 1] = dp[i - 1];
while(sz and a[i] >= p[sz - 1].first)
ans = min(ans, p[--sz].second);
if(!sz or a[i] + ans < p[sz - 1].first + p[sz - 1].second)
p[sz++] = make_pair(a[i], ans);
dp[i] = p[sz - 1].first + p[sz - 1].second;
}
bef[n] = dp[n];
}
cout << bef[n] << endl;
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... |