#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define meta int tm = (tl + tr) / 2, x = i * 2 + 1, y = x + 1
const int N = 2e5 + 7;
const int oo = 1e9;
const int mod = 1e9 + 7;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
int n, k, a[N], mn[N][107], dp[N][107], s;
int p[N];
stack <int> st;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
while (!st.empty ()) {
if (a[i] > st.top ()) st.pop ();
}
// cout << i << " : ";
for (int j = 1; j <= min(i, k); j++) {
if (st.empty ()) dp[i][j] = mn[i - 1][j - 1] + a[i];
else dp[i][j] = min (a[i] + dp[s][j - 1], dp[s][j]);
if (i == 1) mn[i][j] = dp[i][j];
else mn[i][j] = min (mn[i - 1][j], dp[i][j]);
// cout << dp[i][j] << ' ';
}
// cout << '\n';
st.push (a[i]);
}
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... |