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 fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int N = 16;
int n, k;
ll a[maxn];
vector<ll> f, g;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> k;
f.resize(n + 4), g.resize(n + 4);
for (int i = 1; i <= n; i++) {
cin >> a[i];
g[i] = max(g[i - 1], a[i]);
}
stack<pair<ll,int>> st;
for (int t = 2; t <= k; t++) {
stack<pair<ll,int>>().swap(st);
f[0] = g[0] = INF;
for (int i = 1; i <= n; i++) {
ll candi = g[i - 1];
while (!st.empty() && a[st.top().se] < a[i]) {
candi = min(candi, st.top().fi);
st.pop();
}
f[i] = min(candi + a[i], f[st.empty() ? 0 : st.top().se]);
st.push({candi, i});
}
swap(f, g);
}
cout << g[n];
}
# | 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... |