#include <bits/stdc++.h>
#pragma GCC optimize "Ofast"
using namespace std;
using ld = long double;
#define ll long long
#define pf push_front
#define pof pop_front
#define pb push_back
#define pob pop_back
#define fi first
#define se second
////////////////// fresh air //////////////////
vector<ll> calculate_costs(vector<int> w, vector<int> costa, vector<int> costb, vector<int> q) {
    ll n = w.size();
    vector<ll> otv;
    vector<pair<ll, ll>> a;
    for (ll i = 0; i < n; i++) {
        a.pb({w[i], i});
    }
    a.pb({1e18, n});
    sort(a.begin(), a.end());
    for (auto d : q) {
        ll ans = 0;
        ll l = -1, mn = 1e18;
        for (ll i = 0; i <= n; i++) {
            ll pos = a[i].se;
            if (i && a[i].fi - a[i - 1].fi <= d) {
                if (l == -1) {
                    l = i - 1;
                    ans += costb[a[i - 1].se] - costa[a[i - 1].se];
                    if (i < n - 1 && a[i + 1].fi - a[i].fi <= d) {
                        mn = costa[pos] - costb[pos];
                    }
                }
                if (!(a[i + 1].fi - a[i].fi <= d && a[i + 1].fi - a[i - 1].fi > d) || (i < n - 2 && a[i + 2].fi - a[i + 1].fi <= d)) {
                    ll s = costa[pos] - costb[pos];
                    mn = min(mn, s);
                }
                ans += costb[pos];
            }
            else {
                if (i != n) ans += costa[pos];
                ans += ((i - l) % 2) * (l != -1) * mn;
                l = -1;
                mn = 1e18;
            }
            // cout << a[i].fi << ' ' << a[i].se << ' ' << ans << "\n";
        }
        otv.pb(ans);
        // cout << otv.back() << "\n";
    }
    return otv;
}
// signed main() {
//     ios_base::sync_with_stdio(0);
//     cin.tie(NULL); cout.tie(NULL);
//     int T = 1;
//     //cin >> T;
//     //cout << setprecision(8) << fixed;
//     while (T--) {
//         calculate_costs({15, 12, 2, 10, 21}, {5, 4, 5, 6, 3}, {1, 2, 2, 3, 2}, {5, 9, 1});
//         calculate_costs({2, 3, 4, 6, 7, 8}, {5, 4, 5, 6, 5, 6}, {2, 0, 1, 2, 1, 3}, {1});
//         calculate_costs({1, 2, 3, 4, 5}, {5, 4, 1, 7, 8}, {1, 2, 0, 5, 3}, {1});
//     }
// }
| # | 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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |