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;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
  tomorrow will worse
  but the day after tomorrow will be the sunshine..
 
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up  */
//The most CHALISHKANCHIK
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const long long N = 1e5+50, inf = 1e18, mod = 1e9+7;
void solve(){
	int n;
	cin >> n;
	vi v(n);
	for(int i = 0; i < n; i++){
		cin >> v[i];
	}
	int q;
	cin >> q;
	while(q--){
		int x, ans = 0;
		cin >> x;
		int l = lower_bound(all(v),x) - v.begin();
		int r = l;
		while(1){
			if(l == 0){
				ans += abs(v[n-1] - x);
				break;
			}
			if(r == n){
				ans += abs(x - v[0]);
				break;
			}
			if(x - v[l-1] <= v[r] - x){
			    int i = lower_bound(all(v), 2*x-v[r]) - v.begin();
				ans += x-v[i];
				x = v[i];
				l = i;
			}
			else{
				int i = lower_bound(all(v), 2*x - v[l-1]) - v.begin() - 1;
				ans += v[i]-x;
				x = v[i];
				r = i+1;
			}
		}
		cout << ans << '\n';
	}
}
main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int t = 1;
	//~ cin >> t;
	while(t--){
		solve();
	}
}
Compilation message (stderr)
travel.cpp:60:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   60 | 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... |