import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
    int n = scan!int, m = scan!int;
    int[] ls = scan!int(m);

    foreach(i; 0 .. m){
        if(i + ls[i] > n){
            "-1".writeln;
            return;
        }
    }

    long sum = ls.to!(long[]).sum;
    if(sum < n.to!long){
        "-1".writeln;
        return;
    }

    int[] ans;
    foreach(i; 0 .. m) ans ~= i + 1;

    ans[m - 1] = n - ls[m - 1] + 1;
    foreach_reverse(i; 0 .. m - 1){
        if(ans[i] + ls[i] < ans[i + 1]) ans[i] = ans[i + 1] - ls[i];
    }

    ans.unsplit.print;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n, m;
multitest_loop:
	while (readf !(" %s %s") (n, m) > 0)
	{
		readln;
		auto len = readln.splitter.map !(to !(int)).array;
		auto total = sum (len, 0L);
		if (total < n)
		{
			writeln (-1);
			continue multitest_loop;
		}
		foreach (i; 0..m)
		{
			if (len[i] > n - i)
			{
				writeln (-1);
				continue multitest_loop;
			}
		}

		long [] ans;
		ans.reserve (m);
		foreach (i; 0..m)
		{
			long pos = max (i, n - total);
			ans ~= pos + 1;
			total -= len[i];
		}
		writefln !("%(%s %)") (ans);
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		bool ok = true;
		auto ans = new int[](m);
		int pos = 1;
		long r;
		foreach (i; 0..m)
		{
			if (l[i] > n-i)
			{
				ok = false;
				break;
			}
			ans[i] = pos - cast(int)r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + cast(int)l[i];
			if (pos > n+1)
			{
				ok = false;
				break;
			}
		}
		debug writeln(ans);
		if (ok)
			ans.map!(to!string).join(" ").writeln;
		else
			writeln(-1);
	}

	stdout.flush;
	debug readln;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readLong();
      const M = readInt();
      auto L = new long[M];
      foreach (i; 0 .. M) {
        L[i] = readLong();
      }

      auto LSum = new long[M + 1];
      foreach (i; 0 .. M) {
        LSum[i + 1] = LSum[i] + L[i];
      }

      long[] ans;
      if (LSum[M] >= N) {
        ans = new long[M];
        long bef = -1;
        foreach (i; 0 .. M) {
          /*
            bef < p
            p + L[i] <= N
            p + L[i] + ... + L[M - 1] >= N
          */
          ans[i] = max(bef + 1, N - (LSum[M] - LSum[i]));
          if (!(ans[i] + L[i] <= N)) {
            ans = [];
            break;
          }
          bef = ans[i];
        }
      }

      if (!ans.empty) {
        foreach (i; 0 .. M) {
          if (i > 0) write(" ");
          write(ans[i] + 1);
        }
        writeln();
      } else {
        writeln(-1);
      }

      debug {
        long[] brt;
        foreach (h; 0 .. N^^M) {
          auto ps = iota(M).map!(i => (h / N^^i % N)).array;
          bool ok = true;
          auto col = new int[cast(int)(N)];
          col[] = -1;
          foreach (i; 0 .. M) {
            if (ps[i] + L[i] <= N) {
              col[cast(int)(ps[i]) .. cast(int)(ps[i] + L[i])] = i;
            } else {
              ok = false;
              break;
            }
          }
          ok = ok && (col.count(-1) == 0);
          foreach (i; 0 .. M) {
            ok = ok && (col.count(i) != 0);
          }
          if (ok) {
            brt = ps;
            break;
          }
        }
        writeln("brt = ", brt);
        assert((!ans.empty) == (!brt.empty));
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA!int;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		auto ans = new int[](m);
		int pos = 1;
		int r;
		foreach (i; 0..m)
		{
			ans[i] = pos - r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + l[i];
		}
		ans.map!(to!string).join(" ").writeln;
	}

	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA!int;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		bool ok = true;
		auto ans = new int[](m);
		int pos = 1;
		int r;
		foreach (i; 0..m)
		{
			ans[i] = pos - r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + l[i];
			if (pos > n+1)
				ok = false;
		}
		debug writeln(ans);
		if (ok)
			ans.map!(to!string).join(" ").writeln;
		else
			writeln(-1);
	}

	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA!int;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		bool ok = true;
		auto ans = new int[](m);
		int pos = 1;
		int r;
		auto index = l.MAKE_IDX!"a > b"();
		foreach (i; index)
		{
			ans[i] = pos - r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + l[i];
			if (pos > n+1)
				ok = false;
		}
		debug writeln(ans);
		if (ok)
			ans.map!(to!string).join(" ").writeln;
		else
			writeln(-1);
	}

	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA!int;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		bool ok = true;
		auto ans = new int[](m);
		int pos = 1;
		int r;
		foreach (i; 0..m)
		{
			ans[i] = pos - r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + l[i];
			if (pos > n)
				ok = false;
		}
		if (ok)
			ans.map!(to!string).join(" ").writeln;
		else
			writeln(-1);
	}

	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto l = RDA!int;

	auto len = l.sum;
	auto d = len - n;
	if (d < 0)
		writeln(-1);
	else
	{
		bool ok = true;
		auto ans = new int[](m);
		int pos = 1;
		int r;
		foreach (i; 0..m)
		{
			if (l[i] > n-i)
			{
				ok = false;
				break;
			}
			ans[i] = pos - r;
			r = min(l[i]-1, d);
			d -= r;
			pos = ans[i] + l[i];
			if (pos > n+1)
			{
				ok = false;
				break;
			}
		}
		debug writeln(ans);
		if (ok)
			ans.map!(to!string).join(" ").writeln;
		else
			writeln(-1);
	}

	stdout.flush;
	debug readln;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto L = new int[M];
      foreach (i; 0 .. M) {
        L[i] = readInt();
      }

      auto LSum = new int[M + 1];
      foreach (i; 0 .. M) {
        LSum[i + 1] = LSum[i] + L[i];
      }

      int[] ans;
      if (LSum[M] >= N) {
        ans = new int[M];
        int bef = -1;
        foreach (i; 0 .. M) {
          /*
            bef < p
            p + L[i] <= N
            p + L[i] + ... + L[M - 1] >= N
          */
          ans[i] = max(bef + 1, N - (LSum[M] - LSum[i]));
          if (!(ans[i] + L[i] <= N)) {
            ans = [];
            break;
          }
          bef = ans[i];
        }
      }

      if (!ans.empty) {
        foreach (i; 0 .. M) {
          if (i > 0) write(" ");
          write(ans[i] + 1);
        }
        writeln();
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.string;
import std.random;

void solve(int[] a)
{
    int[] one[40];
    int[] zero[40];
    foreach (int i; 0 .. 32)
    {
        foreach (int j; 0 .. a.length)
        {
            if ((a[j] & (1 << i)) == 0)
            {
                zero[i] ~= j;
            }
            else
            {
                one[i] ~= j;
            }
        }
    }
    int tar = -1;
    for (int pos = 30; pos >= 0; -- pos)
    {
        if (one[pos].length > 0)
        {
            int i;
            for (i = 0; i < pos; ++ i)
            {
                if (zero[i].length == 0)
                {
                    break;
                }
                int j;
                for (j = 0; j < zero[i].length; ++ j)
                {
                    if (a[zero[i][j]] & (1 << pos))
                    {
                        break;
                    }
                }
                if (j == zero[i].length)
                {
                    break;
                }
            }
            if (i == pos)
            {
                tar = pos;
                break;
            }
        }
    }
    if (tar == -1)
    {
        writefln("-1");
        return;
    }
    bool[] flag = new bool[a.length];
    int k = 0;
    foreach (int i; 0 .. one[tar].length)
    {
        ++ k;
        flag[one[tar][i]] = true;
    }
    foreach (int i; 0 .. tar)
    {
        foreach (int j; 0 .. zero[i].length)
        {
            if ((a[zero[i][j]] & (1 << tar)) && flag[zero[i][j]] == false)
            {
                ++ k;
                flag[zero[i][j]] = true;
            }
        }
    }
    writefln("%d", k);
    foreach (int i; 0 .. a.length)
    {
        if (flag[i] == true)
        {
            writef("%d ", a[i]);
        }
    }
    writefln("");
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int[] a = new int[n];
        foreach (int i; 0 .. n)
        {
            //a[i] = uniform(0, 1000000000) + 1;
            //a[i] = 1000000000 - i;
            scanf("%d", &a[i]);
        }
        solve(a);
    }
}

import std.stdio, std.string;
import std.random;

int getPosition(T)(T[] arr, int[] index, T tar)
{
    int l = 0, r = index.length - 1, idx = -1;
    while (l <= r)
    {
        int m = (l + r) >> 1;
        if (arr[index[m]] < tar)
        {
            l = m + 1;
        }
        else
        {
            idx = m;
            r = m - 1;
        }
    }
    if (idx == -1)
    {
        idx = index.length;
    }
    return idx;
}

void solve(int[] a)
{
    int[] one[40];
    int[] zero[40];
    foreach (int i; 0 .. 32)
    {
        foreach (int j; 0 .. a.length)
        {
            if ((a[j] & (1 << i)) == 0)
            {
                zero[i] ~= j;
            }
            else
            {
                one[i] ~= j;
            }
        }
    }
    int tar = -1;
    for (int pos = 30; pos >= 0; -- pos)
    {
        if (one[pos].length > 0)
        {
            int i;
            for (i = 0; i < pos; ++ i)
            {
                if (zero[i].length == 0)
                {
                    break;
                }
                int j;
                int idx = getPosition(a, zero[i], 1 << pos);
                for (j = idx; j < zero[i].length; ++ j)
                {
                    if (a[zero[i][j]] & (1 << pos))
                    {
                        break;
                    }
                }
                if (j == zero[i].length)
                {
                    break;
                }
            }
            if (i == pos)
            {
                tar = pos;
                break;
            }
        }
    }
    if (tar == -1)
    {
        writefln("-1");
        return;
    }
    bool[] flag = new bool[a.length];
    int k = 0;
    foreach (int i; 0 .. one[tar].length)
    {
        ++ k;
        flag[one[tar][i]] = true;
    }
    foreach (int i; 0 .. tar)
    {
        foreach (int j; 0 .. zero[i].length)
        {
            if ((a[zero[i][j]] & (1 << tar)) && flag[zero[i][j]] == false)
            {
                ++ k;
                flag[zero[i][j]] = true;
            }
        }
    }
    /*foreach (int i; 0 .. a.length)
    {
        if (flag[i] == false && (a[i] & (1 << tar)) != 0)
        {
            ++ k;
            flag[i] = true;
        }
    }*/
    writefln("%d", k);
    foreach (int i; 0 .. a.length)
    {
        if (flag[i] == true)
        {
            writef("%d ", a[i]);
        }
    }
    writefln("");
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int[] a = new int[n];
        foreach (int i; 0 .. n)
        {
            //a[i] = uniform(0, 1000000000) + 1;
            //a[i] = 1000000000 - i;
            scanf("%d", &a[i]);
        }
        solve(a);
    }
}

import std.stdio, std.string;
import std.random;

int getPosition(T)(T[] arr, int[] index, T tar)
{
    int l = 0, r = index.length - 1, idx = -1;
    while (l <= r)
    {
        int m = (l + r) >> 1;
        if (arr[index[m]] < tar)
        {
            l = m + 1;
        }
        else
        {
            idx = m;
            r = m - 1;
        }
    }
    if (idx == -1)
    {
        idx = index.length;
    }
    return idx;
}

void solve(int[] a)
{
    int[] one[40];
    int[] zero[40];
    foreach (int i; 0 .. 32)
    {
        foreach (int j; 0 .. a.length)
        {
            if ((a[j] & (1 << i)) == 0)
            {
                zero[i] ~= j;
            }
            else
            {
                one[i] ~= j;
            }
        }
    }
    int tar = -1;
    for (int pos = 30; pos >= 0; -- pos)
    {
        if (one[pos].length > 0)
        {
            int i;
            for (i = 0; i < pos; ++ i)
            {
                if (zero[i].length == 0)
                {
                    break;
                }
                int j;
                int idx = getPosition(a, zero[i], 1 << pos);
                for (j = idx; j < zero[i].length; ++ j)
                {
                    if (a[zero[i][j]] & (1 << pos))
                    {
                        break;
                    }
                }
                if (j == zero[i].length)
                {
                    break;
                }
            }
            if (i == pos)
            {
                tar = pos;
                break;
            }
        }
    }
    if (tar == -1)
    {
        writefln("-1");
        return;
    }
    bool[] flag = new bool[a.length];
    int k = 0;
    foreach (int i; 0 .. one[tar].length)
    {
        ++ k;
        flag[one[tar][i]] = true;
    }
    foreach (int i; 0 .. tar)
    {
        foreach (int j; 0 .. zero[i].length)
        {
            if ((a[zero[i][j]] & (1 << tar)) && flag[zero[i][j]] == false)
            {
                ++ k;
                flag[zero[i][j]] = true;
            }
        }
    }
    foreach (int i; 0 .. a.length)
    {
        if (flag[i] == false && (a[i] & (1 << tar)) != 0)
        {
            ++ k;
            flag[i] = true;
        }
    }
    writefln("%d", k);
    foreach (int i; 0 .. a.length)
    {
        if (flag[i] == true)
        {
            writef("%d ", a[i]);
        }
    }
    writefln("");
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int[] a = new int[n];
        foreach (int i; 0 .. n)
        {
            //a[i] = uniform(0, 1000000000) + 1;
            //a[i] = 1000000000 - i;
            scanf("%d", &a[i]);
        }
        solve(a);
    }
}

import std.stdio, std.string;
import std.random;

void solve(int[] a)
{
	int[] one[40];
	int[] zero[40];
	foreach (int i; 0 .. 32)
	{
		foreach (int j; 0 .. a.length)
		{
			if ((a[j] & (1 << i)) == 0)
			{
				zero[i] ~= j;
			}
			else
			{
				one[i] ~= j;
			}
		}
	}
	int tar = -1;
	for (int pos = 30; pos >= 0; -- pos)
	{
		if (one[pos].length > 0)
		{
			int i;
			for (i = 0; i < pos; ++ i)
			{
				if (zero[i].length == 0)
				{
					break;
				}
				int j;
				for (j = 0; j < zero[i].length; ++ j)
				{
					if (a[zero[i][j]] & (1 << pos))
					{
						break;
					}
				}
				if (j == zero[i].length)
				{
					break;
				}
			}
			if (i == pos)
			{
				tar = pos;
				break;
			}
		}
	}
	if (tar == -1)
	{
		writefln("-1");
		return;
	}
	bool[] flag = new bool[a.length];
	int k = 0;
	foreach (int i; 0 .. one[tar].length)
	{
		++ k;
		flag[one[tar][i]] = true;
	}
	foreach (int i; 0 .. tar)
	{
		foreach (int j; 0 .. zero[i].length)
		{
			if ((a[zero[i][j]] & (1 << tar)) && flag[zero[i][j]] == false)
			{
				++ k;
				flag[zero[i][j]] = true;
			}
		}
	}
	foreach (int i; 0 .. a.length)
	{
		if (flag[i] == false && (a[i] & (1 << tar)) != 0)
		{
			++ k;
			flag[i] = true;
		}
	}
	writefln("%d", k);
	foreach (int i; 0 .. a.length)
	{
		if (flag[i] == true)
		{
			writef("%d ", a[i]);
		}
	}
	writefln("");
}

void main(string[] args)
{
	int n;
	while (scanf("%d", &n) == 1)
	{
		int[] a = new int[n];
		foreach (int i; 0 .. n)
		{
			//a[i] = uniform(0, 1000000000) + 1;
			//a[i] = 1000000000 - i;
			scanf("%d", &a[i]);
		}
		solve(a);
	}
}


import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

alias Point = Tuple !(int, q{x}, int, q{y}, int, q{t});

void main ()
{
	int n, m;
	while (readf (" %s %s ", &n, &m) > 0)
	{
		readln ();
		auto p = new Point [n + m + 2];
		int xmax = int.min;
		int ymax = int.min;
		foreach (i; 0..n + m)
		{
			readf (" %s %s", &p[i].x, &p[i].y);
			p[i].t = (i >= n);
			if (i >= n)
			{
				xmax = max (xmax, p[i].x);
				ymax = max (ymax, p[i].y);
			}
		}
		p[n + m + 0] = Point (xmax, 0, 1);
		p[n + m + 1] = Point (0, ymax, 1);
		sort !((a, b) => (a.x * 1L * b.y < a.y * 1L * b.x) ||
		    ((a.x * 1L * b.y == a.y * 1L * b.x) &&
		    (a.x + a.y < b.x + b.y)),
		    SwapStrategy.stable) (p);

		Point [] q;
		foreach (c; p)
		{
			if (q.length > 0 &&
			    c.x == q[$ - 1].x &&
			    c.y == q[$ - 1].y)
			{
				q[$ - 1].t &= c.t;
			}
			else
			{
				q ~= c;
			}
		}
		p = q;
		debug {writeln (p.map !(to !(string)).join ("\n"));}
		debug {writeln ();}

		Point [] s;
		s.reserve (n + m + 4);
		foreach (c; p)
		{
			while (s.length > 1 &&
			    (s[$ - 1].x - s[$ - 2].x) * 1L *
			    (c.y - s[$ - 2].y) >
			    (s[$ - 1].y - s[$ - 2].y) * 1L *
			    (c.x - s[$ - 2].x))
			{
				s.length--;
			}
			s.assumeSafeAppend ();
			s ~= c;
		}
		debug {writeln (s.map !(to !(string)).join ("\n"));}

		writeln (all !(a => a.t == 1) (s) ? "Min" : "Max");
	}
}

import std.algorithm, std.range, std.stdio, std.typecons;
alias Point = Tuple !(long, q{x}, int, q{y}, int, q{t});
void main ()
{
    int n, m;
    readf (" %s %s ", &n, &m);
    readln ();
    auto p = new Point [n + m];
    foreach (i, ref c; p)
    {
        readf (" %s %s", &c.x, &c.y);
        c.t = (i >= n);
    }
    p ~= Point (p[n..$].minPos!q{a.x > b.x}.front.x, 0, 1);
    p ~= Point (0, p[n..$].minPos!q{a.y > b.y}.front.y, 1);
    sort !((a, b) => (a.x * b.y < a.y * b.x) ||
        ((a.x * b.y == a.y * b.x) && (a.x + a.y < b.x + b.y))) (p);
    Point [] q, s;
    foreach (c; p)
    {
        if (q.length > 0 && c.x == q[$ - 1].x && c.y == q[$ - 1].y)
            q[$ - 1].t &= c.t;
        else
            q ~= c;
    }
    foreach (c; q)
    {
        while (s.length > 1 &&
            (s[$ - 1].x - s[$ - 2].x) * (c.y - s[$ - 2].y) >
            (s[$ - 1].y - s[$ - 2].y) * (c.x - s[$ - 2].x))
            s.length--;
        s.assumeSafeAppend ();
        s ~= c;
    }
    writeln (s.all!q{a.t == 1} ? "Min" : "Max");
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		auto cnt = new long[](30);
		foreach (i; 0..n)
		{
			foreach (j; 0..30)
			{
				auto bit = 1L << j;
				if (a[i] & bit)
					++cnt[j];
			}
		}

		foreach (i; 1..n+1)
		{
			bool ok = true;
			foreach (j; 0..30)
			{
				if (cnt[j] % i)
					ok = false;
			}
			if (ok)
				ans[ti] ~= i;
		}
	}

	foreach (e; ans)
		e.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}

import core.bitop;
import std.algorithm;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int k = 0;
		foreach (z; 0..30)
		{
			int v = 0;
			foreach (i; 0..n)
			{
				v += (a[i] >> z) & 1;
			}
			k = gcd (k, v);
		}
		writefln !("%(%s %)") (iota (1, n + 1)
		    .filter !(c => k % c == 0));
	}
}

import std.algorithm, std.array, std.container, std.conv, std.format, std.functional, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

// https://www.geeksforgeeks.org/find-divisors-natural-number-set-1/
long[] SortedDivisors(long n)
{
  long[] ans;
  for (long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      // If divisors are equal, print only one
      if (n/i == i) {
        ans ~= i;
      } else { // Otherwise print both
        ans ~= i;
        ans ~= n / i;
      }
    }
  }
  ans.sort;
  return ans;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!long;
    auto a = readln.strip.split.map!(to!long).array;
    long tmp = long.max;
    long[32] cnt;
    foreach (x ; a) {
      foreach (i ; 0 .. 32) {
        if (x & (1L << i))
          cnt[i]++;
      }
    }
    long[] filtered_cnt;
    foreach (i ; 0 .. 32) {
      if (cnt[i] != 0)
        filtered_cnt ~= cnt[i];
    }
    if (filtered_cnt.length == 0) {
      writeln(iota(1, n + 1).map!text.join(" "));
    } else {
      long ans = filtered_cnt[0];
      foreach (x ; filtered_cnt)
        ans = gcd(ans, x);
      writeln(SortedDivisors(ans).map!text.join(" "));
    }
  }
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto a = ma(n, readInt!int);
	int ans = 0;
	foreach(i; 0 .. 30)
	{
		int count = 0;
		foreach(ai; a) if (ai & (1 << i)) count++;
		ans = gcd(ans, count);
	}
	if (ans == 0)
	{
		foreach(i; 1 .. n + 1) write(i, " ");
		writeln;
		return;
	}
	foreach(i; 1 .. ans + 1)
	{
		if (ans % i == 0) write(i, " ");
	}
	writeln;
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
	{
		popChar;
	}
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

import std.algorithm, std.array, std.container, std.conv, std.format, std.functional, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

// https://www.geeksforgeeks.org/find-divisors-natural-number-set-1/
long[] SortedDivisors(long n)
{
  long[] ans;
  for (long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      // If divisors are equal, print only one
      if (n/i == i) {
        ans ~= i;
      } else { // Otherwise print both
        ans ~= i;
        ans ~= n / i;
      }
    }
  }
  ans.sort;
  return ans;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!long;
    auto a = readln.strip.split.map!(to!long).array;
    long tmp = long.max;
    long[32] cnt;
    foreach (x ; a) {
      foreach (i ; 0 .. 32) {
        if (x & (1L << i))
          cnt[i]++;
      }
    }
    long[] filtered_cnt;
    foreach (i ; 0 .. 32) {
      if (cnt[i] != 0)
        filtered_cnt ~= cnt[i];
    }
    if (filtered_cnt.length == 0) {
      writeln(n);
    } else {
      long ans = filtered_cnt[0];
      foreach (x ; filtered_cnt)
        ans = gcd(ans, x);
      writeln(SortedDivisors(ans).map!text.join(" "));
    }
  }
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		auto score = new long[](n);
		foreach_reverse (i; 0..n)
		{
			score[i] = a[i];
			auto j = cast(int)(i + a[i]);
			if (j < n)
			{
				score[i] += score[j];
			}
		}

		ans[ti] = score.maxElement;
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto v = new int [n];
		foreach_reverse (i; 0..n)
		{
			auto j = i + a[i];
			v[i] = a[i] + (j >= n ? 0 : v[j]);
		}
		writeln (v.maxElement);
	}
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.datetime.stopwatch;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    string[] s;
    foreach (i; 0 .. 2) { s ~= readln.chomp; }

    debug { s.writeln; }

    int chk(int ix1) {
        int section = 1;
        int it1 = 0, it2 = 0;
        while (it1 < n && it2 < n) {
            if (s[ix1][it1] == s[1-ix1][it2]) {
                it1 += 1;
                it2 += 1;
                continue;
            }

            if (section == 1) {
                section += 1;
                it1 += 1;
            } else if (section == 2) {
                section += 1;
                it2 += 1;
            } else {
                return 0;
            }
        }

        return 1;
    }

    int ans = chk(0) + chk(1);

    ans.writeln;
}

import std.range;
import std.stdio;
import std.string;

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto s = readln ().strip ();
		auto t = readln ().strip ();
		while (!s.empty && s.front == t.front)
		{
			s.popFront ();
			t.popFront ();
		}
		while (!s.empty && s.back == t.back)
		{
			s.popBack ();
			t.popBack ();
		}
		writeln ((s[1..$] == t[0..$ - 1]) + (s[0..$ - 1] == t[1..$]));
	}
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto A = new int[M];
      auto B = new int[M];
      auto W = new int[M];
      foreach (i; 0 .. M) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
        W[i] = readInt();
      }

      // incr w, incr val
      alias Entry = Tuple!(int, "w", int, "val");
      auto sets = new RedBlackTree!Entry[N];
      foreach (u; 0 .. N) {
        sets[u] = new RedBlackTree!Entry;
      }
      auto dp = new int[M];
      foreach (i; 0 .. M) {
        // w < W[i]
        auto ranA = sets[A[i]].lowerBound(Entry(W[i], -1));
        if (!ranA.empty) {
          dp[i] = ranA.back.val;
        }
        dp[i] += 1;
        // insert
        auto ranL = sets[B[i]].lowerBound(Entry(W[i], M + 1));
        if (!ranL.empty && ranL.back.val >= dp[i]) {
          continue;
        }
        auto ranR = sets[B[i]].upperBound(Entry(W[i], -1));
        Entry[] rems;
        foreach (e; ranR) {
          if (dp[i] >= e.val) {
            rems ~= e;
          } else {
            break;
          }
        }
        foreach (e; rems) {
          sets[B[i]].removeKey(e);
        }
        sets[B[i]].insert(Entry(W[i], dp[i]));
      }
      debug {
        writeln("dp = ", dp);
      }
      const ans = dp.maxElement;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, m; readV(n, m);

  struct WL { int w, l; }
  alias rbt = RedBlackTree!(WL, "a.w < b.w");

  auto dp = new rbt[](n);
  foreach (i; 0..n) dp[i] = new rbt([WL(-1, 0)]);

  auto ans = 0;
  foreach (_; 0..m) {
    int u, v, w; readV(u, v, w); --u; --v;
    auto s = dp[u].lowerBound(WL(w, 0)).back, nl = s.l+1;
    ans = max(ans, nl);

    auto t = dp[v].lowerBound(WL(w+1, 0)).back;
    if (t.l >= nl) continue;

    dp[v].insert(WL(w, nl));
    dp[v].removeKey(dp[v].upperBound(WL(w, 0)).filter!(wl => wl.l < nl));
  }

  writeln(ans);
}

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, m; readV(n, m);

  struct WL { int w, l; }
  alias rbt = RedBlackTree!(WL, "a.w < b.w");

  auto dp = new rbt[](n);
  foreach (i; 0..n) dp[i] = new rbt([WL(-1, 0)]);

  auto ans = 0;
  foreach (_; 0..m) {
    int u, v, w; readV(u, v, w); --u; --v;
    auto s = dp[u].lowerBound(WL(w, 0)).back, nl = s.l+1;
    ans = max(ans, nl);

    auto t = dp[v].lowerBound(WL(w+1, 0)).back;
    if (t.l >= nl) continue;

    dp[v].insert(WL(w, nl));
  }

  writeln(ans);
}


import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

import std.container;
alias Edge = SList!int;

void main() {
  int n, m, k, s;
  readf (" %d %d %d %d", &n, &m, &k, &s);
  auto a = new int[n];
  auto h = new Edge[k];
  foreach (i; 0 .. n) {
    readf (" %d", &a[i]);
    --a[i];
    h[a[i]].insert (i);
  }
  auto e = new Edge[n];
  foreach (t; 0 .. m) {
    int i, j;
    readf (" %d %d", &i, &j);
    --i; --j;
    e[i].insert(j);
    e[j].insert(i);
  }
  auto c = new int[][] (k, n);
  auto q = new int[n];
  auto age = new int[n];
  age[] = -1;
  foreach (t; 0 .. k) {
    int left, right;
    auto d = c[t];
    foreach (i; h[t]) {
      d[i] = 0;
      age[i] = t;
      q[right++] = i;
    }
    while (left != right) {
      auto i = q[left++];
      foreach (j; e[i]) {
        if (age[j] != t) {
          age[j] = t;
          d[j] = d[i] + 1;
          q[right++] = j;
        }
      }
    }
  }
  auto res = new int[n];
  auto g = new int[k];
  int r = k - s;
  foreach (i; 0 .. n) {
    foreach (j; 0 .. k) g[j] = c[j][i];
    if (s < r) {
      topN (g, s - 1);
      res[i] = g[0 .. s].sum;
    } else {
      if (r > 0) {
        topN! ("a > b") (g, r);
      }
      res[i] = g[r .. k].sum;
    }
  }
  writeln (res.map! (i => i.text).join(' '));
}

import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	int n, m, k, s;
	while (readf (" %s %s %s %s", &n, &m, &k, &s) > 0)
	{
		readln;
		auto w = readln.splitter.map !(to !(int)).array;
		w[] -= 1;
		auto a = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto q = new int [n];
		int qb, qe;
		auto d = new int [] [] (n, k);
		auto d0 = new int [] [] (k, n);
		foreach (ref line; d0)
		{
			line[] = int.max;
		}

		foreach (v; 0..k)
		{
			auto cd = d0[v];
			qb = 0;
			qe = 0;
			foreach (i; 0..n)
			{
				if (w[i] == v)
				{
					cd[i] = 0;
					q[qe++] = i;
				}
			}
			while (qb < qe)
			{
				int i = q[qb++];
				auto r = cd[i] + 1;
				foreach (j; a[i])
				{
					if (cd[j] == int.max)
					{
						cd[j] = r;
						q[qe++] = j;
					}
				}
			}
		}

		foreach (i; 0..n)
		{
			foreach (j; 0..k)
			{
				d[i][j] = d0[j][i];
			}
		}

		auto ans = new int [n];
		foreach (i; 0..n)
		{
			topN (d[i], s - 1);
			ans[i] = sum (d[i][0..s]);
		}
		writefln ("%(%s %)", ans);
	}
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;

void main()
{
	int n, m, k, s;
	readln.chomp.split.tie(n, m, k, s);
	int[] a = readln.chomp.split.map!(to!int).map!(a => a -1).array;
	int[][] adj = new int[][](n);
	foreach (i; 0..m) {
		int u, v;
		readln.chomp.split.tie(u, v);
		--u;
		--v;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	int[][] types = new int[][](k);
	foreach (int i, t; a) {
		types[t] ~= i;
	}
	int[][] dist = new int[][](k, n);
	foreach (i; 0..k) {
		dist[i] []= int.max;
		int[] queue;
		foreach (j; types[i]) {
			queue ~= j;
			dist[i][j] = 0;
		}
		while (queue.length) {
			int node = queue.front;
			queue = queue[1..$];
			foreach (next; adj[node]) {
				if (dist[i][next] <= dist[i][node] + 1) {
					continue;
				}
				dist[i][next] = dist[i][node] + 1;
				queue ~= next;
			}
		}
	}
	foreach (i; 0..n) {
		int[] arr = new int[](k);
		foreach (j; 0..k) {
			arr[j] = dist[j][i];
		}
		std.algorithm.sort(arr);
		int ans = arr[0..s].sum;
		if (i) write = " ";
		ans.write;
	}
	writeln;
}

void tie(R, Args...)(R arr, ref Args args)
		if (isRandomAccessRange!R || isArray!R)
in
{
	assert (arr.length == args.length);
}
body
{
	foreach (i, ref v; args) {
		alias T = typeof(v);
		v = arr[i].to!T;
	}
}

void verbose(Args...)(in Args args)
{
	stderr.write("[");
	foreach (i, ref v; args) {
		if (i) stderr.write(", ");
		stderr.write(v);
	}
	stderr.writeln("]");
}

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

import std.container;
alias Edge = SList!int;

void main() {
  int n, m, k, s;
  readf (" %d %d %d %d", &n, &m, &k, &s);
  auto a = new int[n];
  auto h = new Edge[k];
  foreach (i; 0 .. n) {
    readf (" %d", &a[i]);
    --a[i];
    h[a[i]].insert (i);
  }
  auto e = new Edge[n];
  foreach (t; 0 .. m) {
    int i, j;
    readf (" %d %d", &i, &j);
    --i; --j;
    e[i].insert(j);
    e[j].insert(i);
  }
  auto c = new int[][] (k, n);
  auto q = new int[n];
  auto age = new int[n];
  age[] = -1;
  foreach (t; 0 .. k) {
    int left, right;
    auto d = c[t];
    foreach (i; h[t]) {
      d[i] = 0;
      age[i] = t;
      q[right++] = i;
    }
    while (left != right) {
      auto i = q[left++];
      foreach (j; e[i]) {
        if (age[j] != t) {
          age[j] = t;
          d[j] = d[i] + 1;
          q[right++] = j;
        }
      }
    }
  }
  auto res = new int[n];
  auto g = new int[k];
  int r = k - s;
  foreach (i; 0 .. n) {
    foreach (j; 0 .. k) g[j] = c[j][i];
    if (s < r) {
      partialSort (g, s);
      res[i] = g[0 .. s].sum;
    } else {
      partialSort! ("a > b") (g, r);
      res[i] = g[r .. k].sum;
    }
  }
  writeln (res.map! (i => i.text).join(' '));
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      const K = readInt();
      const S = readInt();
      auto A = new int[N];
      foreach (u; 0 .. N) {
        A[u] = readInt() - 1;
      }
      auto U = new int[M];
      auto V = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
      }

      auto G = new int[][N];
      foreach (i; 0 .. M) {
        G[U[i]] ~= V[i];
        G[V[i]] ~= U[i];
      }

      auto ds = new int[][](N, K);
      auto que = new int[N];
      auto dist = new int[N];
      foreach (k; 0 .. K) {
        int qb, qe;
        dist[] = N;
        foreach (u; 0 .. N) {
          if (A[u] == k) {
            dist[u] = 0;
            que[qe++] = u;
          }
        }
        for (; qb != qe; ) {
          const u = que[qb++];
          foreach (v; G[u]) {
            if (chmin(dist[v], dist[u] + 1)) {
              que[qe++] = v;
            }
          }
        }
        foreach (u; 0 .. N) {
          ds[u][k] = dist[u];
        }
      }

      foreach (u; 0 .. N) {
        if (u > 0) write(" ");
        ds[u].sort;
        const ans = ds[u][0 .. S].sum;
        write(ans);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.string, std.algorithm;
import std.conv, std.array;
import std.container;
import std.typecons;

void main() {
    string a = stdin.readln.chop;
    long acc = 1;
    for (int i = 1; i < a.length; i++) {
        int x, y, c, j;
        for (j = 0; i + j < a.length; j++) {
            x = a[i+j-1] - '0';
            y = a[i+j] - '0';
            if (x + y == 9) {
                c++;
            } else {
                break;
            }
        }
        i += j;
        if (c % 2 == 0) {
            acc *= c / 2 + 1;
        }
    }
    acc.writeln;
}

import std.stdio;
import std.string;
import std.algorithm;

void main() {
	string S =chomp(readln());
	int N =S.length;
	int poc[];
	long pos[];
	poc.length =pos.length =N+1;
	pos[0] =1, poc[0] =0;

	for(int i =0; i < N; i++) {
		int a =poc[i], b =0, c =0, j =i;
		while(true) {
			c +=cast(int)(S[j]-'0');
			if(c >= 9 || j == 0) break;
			j--;}
		b =poc[j];
		if(c == 9 && j == i-1) b++;
		poc[i+1] =max(a,b);
		pos[i+1] =0;
		if(poc[i+1] == a) pos[i+1] +=pos[i];
		if(poc[i+1] == b && b > poc[j]) pos[i+1] +=pos[j];}
//		writeln(pos[i]," ",poc[i]);}

	writeln(pos[N]);}

import std.stdio;
import std.string;
import std.algorithm;

void main() {
	string S =chomp(readln());
	int N =S.length;
	int poc[];
	long pos[];
	poc.length =pos.length =N+1;
	pos[0] =1, poc[0] =0;

	for(int i =0; i < N; i++) {
		int a =poc[i], b =0, c =0, j =i;
		while(true) {
			c +=cast(int)(S[j]-'0');
			if(c >= 9 || j == 0) break;
			j--;}
		b =poc[j];
		if(c == 9) b++;
		poc[i+1] =max(a,b);
		pos[i+1] =0;
		if(poc[i+1] == a) pos[i+1] +=pos[i];
		if(poc[i+1] == b && c == 9) pos[i+1] +=pos[j];}
//		writeln(pos[i]," ",poc[i]);}

	writeln(pos[N]);}