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>
#define ll long long
#define ld long double
#define rep(i,m,n) for(ll i=m; i<=n; i++)
#define reb(i,m,n) for(ll i=m; i>=n; i--)
#define ii pair<ll,ll>
#define fs first
#define se second
#define pb push_back
#define sz(v) (ll)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
using namespace std;
//#ifndef ONLINE_JUDGE
// #include "debug.h"
//#else
// #define deb(...) 23
// #define __
// #define n____
//#endif
mt19937 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand (ll l, ll r) {
return l + rd()%(r - l + 1);
}
const ll N = 1e5 + 7;
const ll Mod = 1e9 + 7;
const ll bse = 137;
const ll INF = 1e18;
ll n, K;
ll a[N];
ll dp[105][N];
void chloe_solution () {
cin >> n >> K;
rep (i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
rep (i, 1, K) {
stack<pair<ll,int>> op;
rep (j, 1, n) {
ll mnV = dp[i - 1][j - 1];
while (!op.empty() && a[op.top().se] <= a[j]) {
mnV = min(mnV, op.top().fs);
op.pop();
}
dp[i][j] = min(dp[i][(op.empty() == 1) ? 0 : op.top().se], mnV + a[j]);
op.push({mnV, j});
}
}
cout << dp[K][n] <<"\n";
}
int main() {
//file("c");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll num_test = 1;
// cin >> num_test;
while (num_test--)
chloe_solution();
}
/*
hbjcyibtycdi!ttcotodf
*/
# | 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... |