Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

// Sort
type Val struct {
	id, num int
}
type ByNum []Val

func (a ByNum) Len() int           { return len(a) }
func (a ByNum) Less(i, j int) bool { return a[i].num < a[j].num }
func (a ByNum) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	m := sc.nextIntSlice(M)
	m = append(m, N)
	Q := sc.nextInt()
	l := sc.nextIntSlice(Q)
	base := m[0] - 1
	sums := make([]int, N+1)
	for i := 1; i <= M; i++ {
		var diff int
		if i != M {
			diff = m[i] - m[i-1] - 1
		} else {
			diff = m[i] - m[i-1]
		}
		for j := 0; j < diff; j++ {
			sums[j+1] += diff - j
		}
	}
	for _, allow := range l {
		ok := N + 1
		ng := 0
		for abs(ok-ng) > 1 {
			mid := (ok + ng) / 2
			if base+sums[mid] <= allow {
				ok = mid
			} else {
				ng = mid
			}
		}
		if ok == N+1 {
			fmt.Fprintln(wtr, -1)
		} else {
			fmt.Fprintln(wtr, ok)
		}
	}

	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX_SEG_SIZE = 100010;

int N, M, Q;
vector<int> m;
int sum = 0;
int dp[MAX_SEG_SIZE];
int dp2[MAX_SEG_SIZE];

int solve(int length){
  int loss = sum;
  // loss += m[0] - 1;
  loss -= dp[length-1];
  loss -= (length-1) * dp2[length];
  return loss;
}

int main(void){
  cin >> N >> M;
  m = vector<int>(M);
  for(auto& _m: m){
    cin >> _m;
    _m--;
  }


  sum = N - M;
  int num_of_seg = M+1;
  vector<int> seg_len(num_of_seg);
  for(int i = 0; i < (int)seg_len.size(); i++){
    int next = (i == num_of_seg-1 ? N : m[i]);
    int prev = (i == 0 ? 0 : m[i - 1]);
    seg_len[i] = next - prev - 1;
  }
  // dp[i] := 長さi以上の区間の数
  memset(dp, 0, sizeof(int) * MAX_SEG_SIZE);
  memset(dp2, 0, sizeof(int) * MAX_SEG_SIZE);
  for(int i = 1; i < num_of_seg; i++){
    dp[seg_len[i]] += seg_len[i];
    dp2[seg_len[i]] += 1;
    // dp[seg_len[i]] += seg_len[i];
  }
  for(int i = 1; i < MAX_SEG_SIZE; i++){
    dp[i] += dp[i-1];
  }
  for(int i = MAX_SEG_SIZE-2; i >= 0; i--){
    dp2[i] += dp2[i+1];
  }

  ////////////
  // for(int i = 0;  i< N; i++){
  //   cout << dp[i] << " ";
  // }
  // cout << endl;
  // cout << "-----" << endl;
  // for(int i = 0; i < N; i++){
  //   cout << dp2[i] << endl;
  // }
  // cout << endl;
  ////////////

  cin >> Q;
  const int RMAX = 100001;
  for(int loop = 0; loop < Q; loop++){
    int loss; cin >> loss;
    int l = 0, r = RMAX;
    while(l+1 < r){
      int med = (l + r) / 2;
      int v = solve(med);
      // cout << v << " " << med << endl;
      if(v <= loss){
        // shorter
        r = med;
      }else{
        // longer
        l = med;
      }
    }
    if(r == RMAX) cout << -1 << endl;
    else cout << r << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define AND(x,y,z) 
#define DUMP(xs) for (auto x:xs) cout<<x<<' ';cout<<endl
#define DUMPP(xs) for (auto x:xs) cout<<'('<<x.first<<','<<x.second<<')';cout<<endl
#define FOR(i,a,b) for (ll i=(ll)(a);i<(ll)(b);++i)
#define OUT(x) cout<<x<<endl
#define REP(i,n) FOR(i,0,n)
#define YES(cond) cond ? OUT("YES") : OUT("NO")
#define Yes(cond) cond ? OUT("Yes") : OUT("No")

template<class T> T gcd(const T x,const T y){if(!y){return x;}return gcd(y,x%y);}
template<class T> T lcm(const T x,const T y){return x/gcd(x,y)*y;};
template<class T> T modpow(const T x,const T n,const T mod) {
  T res=1;while(n>0){if(n%2)res=res*x%mod;x=x*x%mod;n/=2;};return res;
}
template<class T> bool alltrue(const T &a){return all_of(ALL(a),[](bool x){return x;});}
template<class T> bool anytrue(const T &a){return any_of(ALL(a),[](bool x){return x;});}
template<class T> bool contain(const T &a,const T &b){for(auto x:b){if (a.find(x)==a.end()){return false;}}return true;}
template<class T> bool mmax(T &m,const T q){ if (m < q) {m = q; return true;} else return false; }
template<class T> bool mmin(T &m,const T q){ if (m > q) {m = q; return true;} else return false; }
template<class T> int siz(const T &a) {return (int)a.size();}
template<class T> set<T> devisers(T n) {
  set<T> res;for(T i=1;i<(T)sqrt(n)+1;i++){if(n%i==0){res.insert(i);res.insert(n/i);}}return res;
}
template<class T> void concat(T &a, const T &b){a.insert(a.end(),b.begin(),b.end());}
template<class T> void getand(const T &a,const T &b,T &c) {
  set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end()));
}
template<class T> void getor(const T &a,const T &b,T &c) {
  set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end()));
}

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD=1e9+7;
const int MAX=1e5+100;

int main() {
  // 最短の矢の長さ（or-1）
  // 矢の長さをxとすると
  // l>=N-(x*M)を満たす最低のxが答え
  // N*l/M<=x
  // しかし当然ながら扇風機の範囲がかぶるところが存在する
  // xを順に伸ばして言ってDPに損失を保存していく
  int N,M;cin>>N>>M;
  vector<int> ms(M);
  REP(i,M) cin>>ms[i];

  // 損失にならない場はtrue
  bitset<100000> bs;
  REP(i,M) bs.set(N-1-(ms[i]-1));

  // 長さがiの時の損失の数をdp[i-1]とする
  vector<ll> dp(N,0);
  dp[0] = N-M;

  // 余計なzeroの数
  int red = 100000 - N;

  FOR(n,1,N) {
    bs |= bs>>1;
    dp[n] = 100000 - bs.count()-red;
  }
  // DUMP(dp);

  int Q;cin>>Q;

  // 逆順にして、l+1のlower_bound-begin-1=idxとしたとき、N-idxが答え
  reverse(ALL(dp));

  REP(i,Q) {
    int l;cin>>l;
    if (dp[0]>l) {
      OUT(-1);
    } else {
      ll idx = lower_bound(ALL(dp),l+1)-dp.begin()-1;
      OUT(N-idx);
    }
  }

  return 0;
}