#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 1e5 + 100, M = 500 + 10, len = 315, inf = 1e18;
const ll mod = 998244353;
ll um(ll a, ll b){
return (1LL * a * b) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll bp(ll x, ll step){
ll res = 1;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
ll inv(ll x){
return bp(x, mod - 2);
}
ll a[N], dp[N], nw[N];
int main() {
//ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
ll n, k;
cin >> n >> k;
for(ll i = 1; i <= n; i++){
cin >> a[i];
}
for(ll l = 1; l <= k; l++){
vector <pll> vec;
//cout << l << endl;
for(ll i = l; i <= n; i++){
ll mx = dp[i - 1];
while((ll)vec.size() != 0){
pll cur = vec.back();
if(cur.F <= a[i]){
vec.pop_back();
mx = min(mx, cur.S - cur.F);
} else break;
}
if((ll)vec.size() == 0) vec.pb({a[i], mx + a[i]});
else vec.pb({a[i], min(mx + a[i], vec.back().S)});
nw[i] = vec.back().S;
//cout << nw[i] << " ";
// for(auto u : vec){
// cout << u.F << " " << u.S << endl;
// }
}
//cout << endl;
for(ll i = l; i <= n; i++){
dp[i] = nw[i];
}
}
cout << dp[n];
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... |