#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const ll inf = 1e18;
struct ST{
    vector<ll> t;
    int n;
    ST(int ns){
        n = ns;
        t.resize(4 * n);
    }
    void upd(int v, int tl, int tr, int p, ll x){
        if (tl == tr){
            t[v] = x;
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        if (p <= tm){
            upd(vv, tl, tm, p, x);
        }
        else {
            upd(vv + 1, tm + 1, tr, p, x);
        }
        t[v] = min(t[vv], t[vv + 1]);
    }
    void upd(int p, ll x){
        upd(1, 1, n, p, x);
    }
    ll get(int v, int tl, int tr, int l, int r){
        if (l > tr || r < tl) return inf;
        if (l <= tl && tr <= r) return t[v];
        int tm = (tl + tr) / 2, vv = 2 * v;
        return min(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
    }
    ll get(int l, int r){
        if (l > r) return inf;
        return get(1, 1, n, l, r);
    }
};
ll delivery(int n, int k, int L, int X[]){
    vector<int> x(n + 1);
    for (int i = 1; i <= n; i++) x[i] = X[i - 1];
    sort(x.begin() + 1, x.end());
    vector<ll> dp(n + 1, inf); dp[0] = 0;
    ST T(n);
    for (int i = 1; i <= n; i++){
        int l = max(1, i - k + 1);
        dp[i] = min({dp[l - 1] + L, dp[l - 1] + 2 * x[i], T.get(l, i - 1)});
        if (i != n){
            T.upd(i, dp[i] + 2 * (L - x[i + 1]));
        }
    }
    return dp[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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |