This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q, X[200005], bruh[200005], P[200005];
int ans[200005], rgt[200005];
struct node{
	int s, e, m, val, val2;
	node *l, *r;
	node (int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
		val = val2 = 0;
	}
	
	void upd(int p, int v){
		if(s == e)val = v;
		else{
			if(p <= m)l->upd(p, v);
			else r->upd(p, v);
			val = min(l->val, r->val);
		}
	}
	
	void upd2(int p, int v){
		if(s == e)val2 = v;
		else{
			if(p <= m)l->upd2(p, v);
			else r->upd2(p, v);
			val2 = max(l->val2, r->val2);
		}
	}
	
	int qry(int a, int b){
		if(s == a && e == b)return val;
		else if(b <= m)return l->qry(a, b);
		else if(a > m)return r->qry(a, b);
		else return min(l->qry(a, m), r->qry(m+1, b));
	}
	
	int qry2(int a, int b){
		if(s == a && e == b)return val2;
		else if(b <= m)return l->qry2(a, b);
		else if(a > m)return r->qry2(a, b);
		else return max(l->qry2(a, m), r->qry2(m+1, b));
	}
}*root;
void solve(){
	cin >> n;
	for(int i=1;i<=n;i++)cin >> X[i];
	for(int i=1;i<n;i++)bruh[i] = 2 * X[i] - X[i+1], P[i+1] = 2 * X[i+1] - X[i];
	P[1] = 1e18;
	root = new node(1, n);
	for(int i=1;i<=n;i++)root->upd(i, bruh[i]), root->upd2(i, P[i]);
	for(int i=2;i<=n;i++)ans[1] += X[i] - X[i-1];
	stack <int> st;
	st.push(1);
	rgt[1] = n;
	for(int i=2;i<=n;i++){
		int lo = i, hi = n, tmp = n;
		while(lo <= hi){
			int mid = (lo + hi) >> 1;
			if(root->qry(i, mid) <= X[i-1])tmp = mid, hi = mid - 1;
			else lo = mid + 1;
		}
		rgt[i] = tmp;
		ans[i] += X[tmp] - X[i] + X[tmp] - X[i-1];
		int res = X[tmp + 1];
		if(tmp == n)ans[i] += X[i-1] - X[1];
		else{
			lo = 1, hi = i - 1, tmp = i;
			while(lo <= hi){
				int mid = (lo + hi) >> 1;
				if(root->qry2(mid, i - 1) > res)tmp = mid, lo = mid + 1;
				else hi = mid - 1;
			}
			ans[i] += X[i-1] - X[tmp] + ans[tmp];
		}
	}
	cin >> q;
	while(q--){
		int x; cin >> x;
		int lo = 1, hi = n, tmp = 0;
		while(lo <= hi){
			int mid = (lo + hi) >> 1;
			if(X[mid] <= x)tmp = mid, lo = mid + 1;
			else hi = mid - 1;
		}
		int res;
		if(!tmp)res = 1;
		else if(tmp == n)res = n;
		else res = (X[tmp+1] - x < x - X[tmp] ? tmp + 1 : tmp);
		cout << ans[res] + abs(X[res] - x) << '\n';
	}
}
main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}
Compilation message (stderr)
travel.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  108 | main(){
      | ^~~~| # | 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... |