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;
#define f first
#define s second
#define int long long
const int maxn = 1e5 + 69;
const int inf = 1e18;
int n, k, a[maxn], dp[maxn][110];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1;i <= n;i++)
cin >> a[i];
for (int i = 0;i <= n;i++) {
for (int j = 0;j <= k;j++)
dp[i][j] = inf;
}
int mx = 0;
for (int i = 1;i <= n;i++) {
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for (int j = 2;j <= k;j++) {
vector<pair<int, int>> st;
for (int i = 1;i <= n;i++) {
int mn = dp[i - 1][j - 1];
while (!st.empty() && a[st.back().f] < a[i]) {
mn = min(mn, st.back().s);
st.pop_back();
}
if (st.empty() || a[i] + mn < a[st.back().f] + st.back().s)
st.push_back({i, mn});
if (i >= j)
dp[i][j] = a[st.back().f] + st.back().s;
}
}
cout << dp[n][k];
}
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... |