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;
//#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(X) debug_func(X, #X)
#define DEBUG_ENDL endl
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(X) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "")
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
void debug_func_preffix(const string &s) { if(s.size()) cout << s << " = "; }
template <typename T>
void debug_func(const T &t, const string &s = "") {
    debug_func_preffix(s);
    cout << t << DEBUG_ENDL_S(s);
}
template <typename T, typename U>
void debug_func(const P<T, U> &p, const string &s = "") {
    debug_func_preffix(s);
    cout << "(";
    debug_func(p.first);
    cout << ", ";
    debug_func(p.second);
    cout << ")" << DEBUG_ENDL_S(s);
}
template <typename T>
void debug_func(const V<T> &v, const string &s = "") {
    for(ll i = 0; i < v.size(); i++) {
        string t = s + "[" + to_string(i) + "]";
        debug_func(v[i], t);
    }
}

int main() {
    ll N, M;
    cin >> N >> M;
    V<ll> mv(M);
    for(ll &e : mv) cin >> e;
    ll initl = mv[0] - 1;
    mv.push_back(N + 1);
    V<ll> dist;
    for(ll i = 0; i + 1 < mv.size(); i++) dist.push_back(mv[i + 1] - mv[i] - 1);
    sort(ALL(dist));
    DEBUG(dist);
    V<ll> dsum(M + 1);
    for(ll i = 0; i + 1 < dsum.size(); i++) dsum[i + 1] = dsum[i] + dist[i];
    DEBUG(dsum);
    ll Q;
    cin >> Q;
    for(ll QQ = 0; QQ < Q; QQ++) {
        ll L;
        cin >> L;
        ll ok = 1e9, ng = 0;
        bool suc = 0;
        while(ok - ng > 1) {
            ll mid = (ok + ng) / 2;
            auto ite = lower_bound(ALL(dist), mid);
            ll idx = distance(dist.begin(), ite);
            ll cnt = distance(ite, dist.end());
            ll rdsum = dsum[M] - dsum[idx];
            ll loss = rdsum - cnt * (mid - 1);
            DEBUG(mid);
            DEBUG(idx);
            DEBUG(cnt);
            DEBUG(rdsum);
            DEBUG(loss);
            loss += initl;
            if(loss <= L) {
                ok = mid;
                suc = true;
            } else {
                ng = mid;
            }
        }
        cout << (suc ? ok : -1) << endl;
    }
    return 0;
}

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


int main(){
    int N, M, m[100001];
    cin >> N >> M;
    for(int i=0; i<M; i++) cin >> m[i];
    m[M] = N+1;

    vector<int> intervals;
    for(int i=0; i<M; i++) intervals.push_back(m[i+1] - m[i] - 1);
    sort(intervals.begin(), intervals.end());

    int loss[100001];
    int now = m[0] - 1;
    int num = 0;
    for(int l=N; l>0; l--){
        while(intervals.size() > 0 && intervals.back() >= l){
            intervals.pop_back();
            num++;
        }
        now += num;
        loss[l] = -now;
    }
    int Q;
    cin >> Q;
    while(Q--){
        int l;
        cin >> l;
        int ans = lower_bound(loss, loss+N+1, -l) - loss;
        if(ans == 0) ans = 1;
        if(ans == N+1) ans = -1;
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

#define int long long

int N,M;


bool sou[101010];

int cnt[202020];

int fff = 0;

int ss[202020];
int sum[202020];

int func(int x) {
  int res = ss[101010 - 1] - ss[x - 1] - (sum[101010 -1] - sum[x - 1]) * (x - 1) + fff;
  return res;
}



signed main() {
  cin >> N >> M;
  for(int i = 0;i < M;i++) {
    int m;
    cin >> m;
    m--;
    sou[m] = true;
  }

  sou[N] = true;

  int fi = 0;
  while(!sou[fi]) fi++;

  fff = fi;
  {
    int c = 0;
    for(int i = fi;i <= N;i++) {
      if(sou[i]) {
        cnt[c]++;
        c = 0;
      }
      else {
        c++;
      }
    }
  }

  cnt[0] = 0;

  for(int i = 1;i < 101010;i++) {
    sum[i] += sum[i - 1] + cnt[i];
  }

  for(int i = 1;i < 101010;i++) {
    ss[i] += ss[i - 1] + cnt[i] * i;
  }

  int Q;
  cin >> Q;
  vector<int> ans;
  for(int qq = 0;qq < Q;qq++) {
    int l;
    cin >> l;
    int ok = 101010;
    int ng = 0;
    while(ok - ng > 1) {
      int mid = (ok + ng) / 2;
      if(func(mid) <= l) ok = mid;
      else ng = mid;
    }
    ans.push_back(ok);
  }

  for(auto x : ans) {
    if(x == 101010) { cout << -1 << endl; }
    else { cout << x << endl;}
  }

}