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>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100005

int N,M;
int input[NUM],table[NUM],dp[NUM];


int main(){

	scanf("%d %d",&N,&M);

	for(int i = 0; i <= N; i++){

		table[i] = 0;
		dp[i] = 0;
	}

	//区間数の計上
	scanf("%d",&input[0]);
	int pre = input[0];

	for(int i = 1; i < M; i++){

		scanf("%d",&input[i]);
		table[(input[i]-pre)-1]++;
		pre = input[i];
	}
	table[N-pre]++;

	dp[N] = table[N];

	//累積和
	for(int i = 1; i <= N; i++){

		table[i] += table[i-1];
	}

	for(int len = N-1; len >= 1; len--){

		dp[len] = dp[len+1] + (table[N]-table[len-1]);
	}

	int Q,count;
	scanf("%d",&Q);

	int L,R,mid,ans;

	for(int loop = 0; loop < Q; loop++){

		scanf("%d",&count);

		count -= input[0]-1; //必ずinput[0]-1回の損失発生

		L = 1,R = N,mid = (L+R)/2,ans = -1;

		while(L <= R){

			if(dp[mid] <= count){

				ans = mid;
				R = mid-1;

			}else{

				L = mid+1;
			}

			mid = (L+R)/2;
		}

		printf("%d\n",ans);
	}

	return 0;
}

C++: 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

int N, M;
int imos[123456];

int main(void)
{
	cin >> N >> M;
	vector<int>m;
	for (int i = 0; i < M; ++i) {
		int tmp; cin >> tmp;
		m.push_back(tmp - 1);
	}
	for (int i = 0; i < N; ++i) {
		auto itr = upper_bound(m.begin(), m.end(), i);
		if (itr != m.begin()) {
			--itr;
			imos[i - *itr + 1]--;
		}
	}
	imos[0] = N;
	for (int i = 1; i <= N + 100; ++i) {
		imos[i] += imos[i - 1];
	}
	for (int i = 0; i <= N + 100; ++i) {
		imos[i] = -imos[i];
	}
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; ++i) {
		int l;
		cin >> l;
		auto itr = lower_bound(imos, imos + N + 100, -l);
		int ans = itr - imos;
		if (ans > N) {
			ans = -1;
		}
		if (ans == 0)ans = 1;
		cout << ans << endl;
	}
	return 0;
}

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

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

const pid_t pid = getpid();
// Problem Specific Parameter:


const int inf =  1 << 21;
const int limit = 100010;
int ary[limit];
bool reset[limit];


int main(void){
	int n, m;
	cin >> n >> m;
	
	rep(i,m){
		int p;
		cin >> p;
		reset[p - 1] = true;
	}

	int val = inf;
	
	rep(i, n) {
		if(reset[i]) val = 1;
		ary[i] = val;
		val++; 
	}

	sort(ary, ary + n);

	int q;
	cin >> q;
	rep(loop, q){
		int l;
		cin >> l;

		int ng = 0, ok = 2 * inf;
		while(ok - ng > 1){
			const int mid = (ok + ng) / 2;
			const int val = n - (upper_bound(ary, ary + n, mid) - ary);
			if(val <= l) 
				ok = mid;
			else 
				ng = mid;
		}

		if(n < ok)
			cout << -1 << endl;
		else 
			cout << ok << endl;
	}

	return 0;
}