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;
typedef long long ll;
int a[100005];
int dp[100005][102];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
memset(dp,0x3f,sizeof(dp));
int n, k;
cin >> n >> k;
int maxx = 0;
for (int i=1; i<=n; i++) {
cin >> a[i];
dp[i][1] = maxx = max(a[i], maxx);
}
for (int i=2; i<=k; i++) {
stack<pair<int, int>> s;
for (int j=1; j<=n; j++) {
int best = dp[j-1][i-1];
while (!s.empty() && a[s.top().second] <= a[j]) {
best = min(best, s.top().first);
s.pop();
}
if (s.empty()) dp[j][i] = best + a[j];
else dp[j][i] = min(best + a[j], dp[s.top().second][i-1]);
s.push({best, j});
}
}
cout << dp[n][k];
}
# | 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... |