This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod1 = (ll)1e9 + 7;
const ll mod2 = (ll)1e9 + 9;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
struct holder {
ll mx, val, st_min;
};
struct cool_stack {
vector<holder> st;
void push(ll val, ll dp) {
ll min_dp = dp;
while (!st.empty() and st.back().mx <= val) {
min_dp = min(min_dp, st.back().val);
st.pop_back();
}
ll minimal = inf;
if (!st.empty()) {
minimal = st.back().st_min;
}
st.pb({val, min_dp, min(minimal, min_dp + val)});
}
ll get() {
if (st.empty()) {
return inf;
}
return st.back().st_min;
}
void pop() {
if (!st.empty()) {
st.pop_back();
}
}
};
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, inf));
dp[0][0] = 0;
for (int b = 1; b <= k; b++) {
cool_stack helper;
for (int i = 1; i <= n; i++) {
helper.push(arr[i], dp[b - 1][i - 1]);
dp[b][i] = helper.get();
}
}
cout << dp[k][n];
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
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... |