#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
template <int N>
struct St {
    int st[N << 1]{};
    void upd(int i, int v) {
        for (st[i += N] = v; i >>= 1;) st[i] = max(st[i << 1], st[i << 1 | 1]);
    }
    int walk(int l, int x) {
        for (l += N;; l >>= 1) {
            if (l & 1) {
                if (st[l] > x) {
                    while (l < N) l ^= st[l <<= 1] <= x;
                    return l - N;
                }
                ++l;
            }
        }
    }
};
int main() {
    int n;
    cin >> n;
    int x[n + 1]{};
    St<1 << 18> L, R;
    for (int i = 1; i <= n; ++i) {
        cin >> x[i];
        if (i == 1) continue;
        L.upd(n - i, x[i] * 2 - x[i - 1]);
        R.upd(i - 1, x[i] - x[i - 1] * 2);
    }
    L.upd(n - 1, 2e9);
    ll ans[n + 1]{};
    for (int i = 1; i <= n; ++i) {
        int l = i, r = i;
        while (l > 1) {
            int r2 = R.walk(r, -x[l - 1] - 1);
            ans[i] += x[r2] - x[l];
            r = r2;
            if (r == n) break;
            int l2 = n - L.walk(n - l, x[r + 1]);
            ans[i] += x[r] - x[l2];
            l = l2;
        }
        ans[i] += x[n] - x[1];
    }
    int q;
    cin >> q;
    while (q--) {
        int s;
        cin >> s;
        if (s >= x[n]) cout << s - x[n] + ans[n] << endl;
        else {
            int i = upper_bound(x + 1, x + n + 1, s) - x;
            if (i > 1 and s - x[i - 1] <= x[i] - s) --i;
            cout << ans[i] + abs(s - x[i]) << endl;
        }
    }
}
| # | 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... |