| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1247293 | quannnguyen2009 | Bitaro's travel (JOI23_travel) | C11 | 0 ms | 0 KiB | 
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;
const int N=2e5+5, mod = 1e9+7, inf = 1e18;
 
int n, q;
int a[N];
 
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i=1; i<=n; i++) cin >> a[i];
    cin >> q;
    while (q--) {
        int s; cin >> s;
        int it = lower_bound(a+1, a+n+1, s)-a;
        int l, r;
        if (it==n+1) l=r=n;
        else if (a[it]==s || it==1) l=r=it;
        else if (a[it]-s<s-a[it - 1]) l=r=it;
        else l=r=it-1;
        int res = abs(s-a[l]);
        s = a[l];
        while(1) {
            if(l==1) {
                res += abs(s - a[n]);
                break;
            }
            if (r==n) {
                res += abs(s - a[1]);
                break;
            }
            int L=a[l-1], R=a[r+1];
            if (s-L<=R-s) {
                int nxt = lower_bound(a+1, a+n+1, 2*s-R)-a;
                res += abs(s-a[nxt]);
                s = a[nxt];
                l = nxt;
            } else {
                int nxt = upper_bound(a+1, a+n+1, 2*s-L)-a-1;
                res += abs(s-a[nxt]);
                s = a[nxt];
                r = nxt;
            }
        }
        cout << res << "\n";
    }
}
