For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.

The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.

If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.math;
import std.traits;

void main(string[] args)
{
  auto a00 = next!long;
  auto a01 = next!long;
  auto a10 = next!long;
  auto a11 = next!long;
  auto c0 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a00)) / real(2.0)));
  auto c0p = cast(long) (round((real(1.0) - sqrt(real(1.0) + 8.0 * a00)) / real(2.0)));
  auto c1 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a11)) / real(2.0)));
  auto c1p = cast(long) (round((real(1.0) - sqrt(real(1.0) + 8.0 * a11)) / real(2.0)));
  if (((c0 - 1) * c0) / 2 != a00 || ((c1 - 1) * c1) / 2 != a11)
    return writeln("Impossible");

  string tryValues(long c0, long c1)
  {
    if (c0 < 0 || c1 < 0) return null;
    if (a00 + a01 + a10 + a11 != ((c0 + c1 - 1) * (c0 + c1) / 2))
      return null;
    if (c1 == 0)
      return iota(0, c0).map!(i => "0").join;
    auto mul0 = a01 / c1;
    auto res0 = a01 % c1;
    assert(mul0 <= c0);
    if (res0 == 0)
      {
	return chain(iota(0, mul0).map!(i => "0"),
		     iota(0, c1).map!(i => "1"),
		     iota(0, c0 - mul0).map!(i => "0")).join;
      }
    else
      {
	return chain(iota(0, mul0).map!(i => "0"),
		     iota(0, c1 - res0).map!(i => "1"),
		     only("0"),
		     iota(0, res0).map!(i => "1"),
		     iota(0, c0 - mul0 - 1).map!(i => "0")).join;
      }
  }

  foreach(tc0; [c0, c0p])
    foreach(tc1; [c1, c1p])
      if (string res = tryValues(tc0, tc1))
	return res.writeln;
  writeln("Impossible");

}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.math;
import std.traits;

void main(string[] args)
{
  auto a00 = next!long;
  auto a01 = next!long;
  auto a10 = next!long;
  auto a11 = next!long;
  auto c0 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a00)) / real(2.0)));
  auto c0p = cast(long) (round((real(1.0) - sqrt(real(1.0) + 8.0 * a00)) / real(2.0)));
  auto c1 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a11)) / real(2.0)));
  auto c1p = cast(long) (round((real(1.0) - sqrt(real(1.0) + 8.0 * a11)) / real(2.0)));
  if (((c0 - 1) * c0) / 2 != a00 || ((c1 - 1) * c1) / 2 != a11)
    return writeln("Impossible");

  string tryValues(long c0, long c1)
  {
    if (c0 < 0 || c1 < 0) return null;
    if (a00 + a01 + a10 + a11 != ((c0 + c1 - 1) * (c0 + c1) / 2))
      return null;
    if (c1 == 0)
      return "0".repeat(cast(int)c0).join;
    auto mul0 = a01 / c1;
    auto res0 = a01 % c1;
    assert(mul0 <= c0);
    if (res0 == 0)
      {
	return chain("0".repeat(cast(int)mul0),
		     "1".repeat(cast(int)c1),
		     "0".repeat(cast(int)(c0 - mul0))).join;
      }
    else
      {
	return chain("0".repeat(cast(int)mul0),
		     "1".repeat(cast(int)(c1 - res0)),
		     only("0"),
		     "1".repeat(cast(int)res0),
		     "0".repeat(cast(int)(c0 - mul0 - 1))).join;
      }
  }
  foreach(tc0; [c0, c0p])
    foreach(tc1; [c1, c1p])
      if (string res = tryValues(tc0, tc1))
	return res.writeln;
  writeln("Impossible");

}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.math;
import std.traits;

void main(string[] args)
{
  auto a00 = next!long;
  auto a01 = next!long;
  auto a10 = next!long;
  auto a11 = next!long;
  auto c0 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a00)) / real(2.0)));
  auto c1 = cast(long) (round((real(1.0) + sqrt(real(1.0) + 8.0 * a11)) / real(2.0)));
  if (((c0 - 1) * c0) / 2 != a00 || ((c1 - 1) * c1) / 2 != a11
      || a00 + a01 + a10 + a11 != ((c0 + c1 - 1) * (c0 + c1)) / 2)
    return writeln("Impossible");
  auto mul0 = a01 / c1;
  auto res0 = a01 % c1;
  assert(mul0 <= c0);
  if (res0 == 0)
    {
      chain(iota(0, mul0).map!(i => "0"),
	    iota(0, c1).map!(i => "1"),
	    iota(0, c0 - mul0).map!(i => "0")).join.writeln;
    }
  else
    {
      chain(iota(0, mul0).map!(i => "0"),
	    iota(0, c1 - res0).map!(i => "1"),
	    only("0"),
	    iota(0, res0).map!(i => "1"),
	    iota(0, c0 - mul0 - 1).map!(i => "0")).join.writeln;
    }
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, numbered from $$$1$$$ to $$$n$$$. You can perform the following operation no more than $$$3n$$$ times:  choose three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 \le i, j \le n$$$; $$$0 \le x \le 10^9$$$);  assign $$$a_i := a_i - x \cdot i$$$, $$$a_j := a_j + x \cdot i$$$. After each operation, all elements of the array should be non-negative.Can you find a sequence of no more than $$$3n$$$ operations after which all elements of the array are equal?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^4$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^5$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

For each test case print the answer to it as follows:   if there is no suitable sequence of operations, print $$$-1$$$;  otherwise, print one integer $$$k$$$ ($$$0 \le k \le 3n$$$) — the number of operations in the sequence. Then print $$$k$$$ lines, the $$$m$$$-th of which should contain three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 \le i, j \le n$$$; $$$0 \le x \le 10^9$$$) for the $$$m$$$-th operation.  If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize $$$k$$$.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = 0L ~ readln.splitter.map !(to !(long)).array;
		if (a.sum % n != 0)
		{
			writeln (-1);
			continue;
		}
		alias Move = Tuple !(int, q{i}, int, q{j}, long, q{x});
		Move [] answer;

		void go (int i, int j, long x)
		{
			a[i] -= i * x;
			a[j] += i * x;
			answer ~= Move (i, j, x);
		}

		foreach (j; 2..n + 1)
		{
			if (a[j] % j != 0)
			{
				go (1, j, j - (a[j] % j));
			}
			assert (a[1] >= 0);
			assert (a[j] % j == 0);
			go (j, 1, a[j] / j);
		}
		auto target = a.sum / n;
		foreach (j; 2..n + 1)
		{
			go (1, j, target);
		}

		writeln (answer.length);
		foreach (ref c; answer)
		{
			writeln (c.i, " ", c.j, " ", c.x);
		}
	}
}

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) { y.modpow(mod - 2); x.modm(y); }

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

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;
		ok[ti] = true;

		foreach (i; 1..n)
		{
			if (a[i] < i+1)
			{
				if (a[0]+a[i] >= i+1)
				{
					auto rem = i+1 - a[i];
					ans[ti] ~= [1, i+1, rem];
					ans[ti] ~= [i+1, 1, 1];
					a[0] += a[i];
					a[i] = 0;
				}
			}
			else
			{
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (ti; 0..t)
	{
		if (!ok[ti])
			writeln(-1);
		else
		{
			writeln(ans[ti].length);
			foreach (ee; ans[ti])
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]-(i+1) < -x) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] = 0;
			ans[ti] ~= [i+1, 1, cnt];
		}

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]+x < i+1) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]-(i+1) < -x) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { y.modpow(mod - 2); x.modm(y); }

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

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;
		ok[ti] = true;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (ti; 0..t)
	{
		if (!ok[ti])
			writeln(-1);
		else
		{
			writeln(ans[ti].length);
			foreach (ee; ans[ti])
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]+x < i+1) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.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) { 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!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}


You have a string $$$s$$$ and a chip, which you can place onto any character of this string. After placing the chip, you move it to the right several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i + 1$$$. Of course, moving the chip to the right is impossible if it is already in the last position.After moving the chip to the right, you move it to the left several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i - 1$$$. Of course, moving the chip to the left is impossible if it is already in the first position.When you place a chip or move it, you write down the character where the chip ends up after your action. For example, if $$$s$$$ is abcdef, you place the chip onto the $$$3$$$-rd character, move it to the right $$$2$$$ times and then move it to the left $$$3$$$ times, you write down the string cdedcb.You are given two strings $$$s$$$ and $$$t$$$. Your task is to determine whether it's possible to perform the described operations with $$$s$$$ so that you write down the string $$$t$$$ as a result.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains the string $$$s$$$ ($$$1 \le |s| \le 500$$$), the second line contains the string $$$t$$$ ($$$1 \le |t| \le 2 \cdot |s| - 1$$$). Both strings consist of lowercase English characters. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$500$$$.

For each test case, print "YES" if you can obtain the string $$$t$$$ by performing the process mentioned in the statement with the string $$$s$$$, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

6
abcdef
cdedcb
aaa
aaaaa
aab
baaa
ab
b
abcdef
abcdef
ba
baa

NoteConsider the examples.The first test case is described in the statement.In the second test case, you can place the chip on the $$$1$$$-st position, move it twice to the right, and then move it twice to the left.In the fourth test case, you can place the chip on the $$$2$$$-nd position, and then don't move it at all.In the fifth test case, you can place the chip on the $$$1$$$-st position, move it $$$5$$$ times to the right, and then finish the process.

import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
       std.container, std.typecons, std.conv, std.random, std.bigint, std.string;

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

int[] kmp(char[] s) {
  auto r = new int[s.length];
  foreach (i; 1 .. s.length) {
    int j = r[i - 1];
    while (j > 0 && s[i] != s[j]) j = r[j - 1];
    if (s[j] == s[i]) j++;
    r[i] = j;
  }

  return r;
}

void main() {
  int T;
  read(T);
  while (T--) {
    string s = readln.strip;
    string t = readln.strip;

    if (s == t) { writeln("Yes"); continue; }

    bool yes = false;

    foreach (w; 0 .. s.length) {
      yes |= kmp(t ~ "#" ~ s[0 .. w + 1] ~ s[0 .. w].dup.reverse).canFind(t.length);
    }

    writeln(yes ? "Yes" : "No");
  }
}

import std.stdio;
import std.array;
import std.algorithm;
import std.ascii;
import core.stdc.stdio;

void main()
{
	int q = readInt!int;
	while (q--)
	{
		auto s = cast(char[])readString;
		auto t = cast(char[])readString;
		bool can = false;
		foreach(i; 0 .. cast(int)s.length)
		{
			foreach(j; i .. cast(int)s.length)
			{
				int currLength = cast(int)(j - i + 1);
				auto str = s[i .. j + 1];
				int remLength = cast(int)t.length - currLength;
				if (remLength < 0) continue;
				if (remLength > j) continue;
				auto other = s[j - remLength .. j].dup;
				str ~= other.reverse;
				if (str == t) { can = true; break; }
			}
			if (can) break;
		}
		if (can) writeln("yes"); else writeln("no");
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	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;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file, std.outbuffer;
import std.algorithm, std.conv, std.functional, std.range, core.bitop, std.random;
// import std.bigint, std.complex, std.bitmanip;
// import std.math, std.mathspecial, std.numeric;

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    string s, t;
    readf!"%s\n%s\n"(s, t);
    auto n = s.length, m = t.length;
    auto rs = s.dup.retro.to!string;
    bool ok = false;
    foreach(i; 0 .. n) {
      foreach(j; i .. n) {
        int l = j - i + 1;
        if (l > m || l + j < m) continue;
        if (s[i .. j + 1] == t[0 .. l] && rs[n - j .. n - j + m - l] == t[l .. $]) {
          ok = true;
        }
      }
    }
    (ok ? "YES" : "NO").writeln;
  }

} // main

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

void main()
{
  long q;
  readf!" %d "(q);
  while (q--) {
    string s = readln.strip;
    string t = readln.strip;
    string[] a;
    foreach (i ; 1 .. s.length + 1) {
      string tmp = s[0 .. i];
      a ~= tmp ~ tmp.retro.text[1 .. $];
    }
    bool good = false;
    foreach (sx ; a) {
      if (sx.indexOf(t) != -1) {
        good = true;
        break;
      }
    }
    writeln(good ? "YES" : "NO");
  }
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto s = RD!string;
		auto ss = RD!string;

		(){
		foreach (i; 0..s.length)
		{
			foreach (j; i..min(s.length, i+ss.length))
			{
				auto len = j - i + 1;
				bool ok = true;
				foreach (k; 0..len)
				{
					if (s[i+k] != ss[k])
					{
						ok = false;
						break;
					}
				}
				if (!ok) continue;
				foreach_reverse (k; 0..ss.length-len)
				{
					if (j <= k)
					{
						ok = false;
						break;
					}
					auto p = j - k - 1;
					if (s[p] != ss[len+k])
					{
						ok = false;
						break;
					}
				}
				if (ok)
				{
					ans[ti] = true;
					return;
				}
			}
		}}();
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	stdout.flush;
	debug readln;
}

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

bool solve (string s, string t)
{
	auto n = s.length.to !(int);
	foreach (pos; 0..n)
	{
		foreach (next; pos..n)
		{
			foreach (prev; 0..next + 1)
			{
				if (equal (t, chain (s[pos..next + 1],
				    s[prev..next].retro)))
				{
					return true;
				}
			}
		}
	}
	return false;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		auto t = readln.strip;
		writeln (solve (s, t) ? "YES" : "NO");
	}
}


There are $$$n$$$ robots driving along an OX axis. There are also two walls: one is at coordinate $$$0$$$ and one is at coordinate $$$m$$$.The $$$i$$$-th robot starts at an integer coordinate $$$x_i~(0 < x_i < m)$$$ and moves either left (towards the $$$0$$$) or right with the speed of $$$1$$$ unit per second. No two robots start at the same coordinate.Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.For each robot find out if it ever explodes and print the time of explosion if it happens and $$$-1$$$ otherwise.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$2 \le m \le 10^8$$$) — the number of robots and the coordinate of the right wall. The second line of each testcase contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$0 < x_i < m$$$) — the starting coordinates of the robots. The third line of each testcase contains $$$n$$$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right). All coordinates $$$x_i$$$ in the testcase are distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$3 \cdot 10^5$$$.

For each testcase print $$$n$$$ integers — for the $$$i$$$-th robot output the time it explodes at if it does and $$$-1$$$ otherwise.

NoteHere is the picture for the seconds $$$0, 1, 2$$$ and $$$3$$$ of the first testcase:   Notice that robots $$$2$$$ and $$$3$$$ don't collide because they meet at the same point $$$2.5$$$, which is not integer.After second $$$3$$$ robot $$$6$$$ just drive infinitely because there's no robot to collide with.

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	auto x = ma(n, readInt!int);
	auto d = ma(n, readString).map!(s => cast(char)s[0]).array;
	assert(d.length == n);
	auto si = iota(0, n).array;
	sort!((i, j) => x[i] < x[j])(si);
	debug writeln("sorted indices ", si);
	auto odd = si.filter!(i => x[i] % 2 == 1).array;
	auto even = si.filter!(i => x[i] % 2 == 0).array;
	debug writeln("even indices ", even);
	debug writeln("odd indices ", odd);
	auto time = new int[](n); time[] = 0;
	void collide(int i, int j)
	{
		assert(x[i] < x[j]);
		if (d[i] == 'R' && d[j] == 'L')
		{
			auto dt = (x[j]-x[i])>>1;
			time[i] += dt;
			time[j] += dt;
		}
		else if (d[i] == 'R' && d[j] == 'R')
		{
			auto ct = m - x[j];
			x[i] += ct;
			x[j] += ct;
			d[j] = 'L';
			time[i] += ct;
			time[j] += ct;
			collide(i, j);
		}
		else if (d[i] == 'L' && d[j] == 'R')
		{
			auto cti = x[i];
			auto ctj = m-x[j];
			auto et = abs(cti - ctj);
			auto ct = max(cti, ctj);
			time[i] += ct;
			time[j] += ct;
			if (cti < ctj)
			{
				x[i] = et;
				x[j] = m;
				d[i] = 'R';
				d[j] = 'L';
				collide(i, j);
			}
			else
			{
				x[j] = m-et;
				x[i] = 0;
				d[i] = 'R';
				d[j] = 'L';
				collide(i, j);
			}
		}
		else
		{
			assert(d[i] == 'L' && d[j] == 'L');
			auto ct = x[i];
			x[i] -= ct;
			x[j] -= ct;
			d[i] = 'R';
			time[i] += ct;
			time[j] += ct;
			collide(i, j);
		}
	}
	void collideIndices(int[] ixs)
	{
		debug
		{
			writeln("colliding");
			foreach(i; ixs) write(i, " ");
			writeln;
			foreach(i; ixs) write(x[i], " ");
			writeln;
			foreach(i; ixs) write(d[i], " ");
			writeln;
		}
		int[] q = null;
		foreach(ix; ixs)
		{
			if (q.length && d[q[$ - 1]] == 'R' && d[ix] == 'L')
			{
				collide(q[$ - 1], ix);
				q = q[0 .. $ - 1];
				continue;
			}
			else
			{
				q ~= ix;
			}
		}
		debug writeln("after doing first collisions: ", q);
		while (q.length >= 2 && d[q[0]] == 'L' && d[q[1]] == 'L')
		{
			collide(q[0], q[1]);
			q = q[2 .. $];
		}
		debug writeln("after triming l ", q);
		while (q.length >= 2 && d[q[$ - 1]] == 'R' && d[q[$ - 2]] == 'R')
		{
			collide(q[$-2], q[$-1]);
			q = q[0 .. $ - 2];
		}
		assert(q.length <= 2);
		if (q.length == 2)
		{
			collide(q[0], q[1]);
		}
		return;
	}
	collideIndices(even);
	collideIndices(odd);
	foreach(ti; time) write(ti? ti : -1, " ");
	writeln;
}

// main {{{
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;
}
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;
// }}}


You are given $$$n$$$ rectangles on a plane with coordinates of their bottom left and upper right points. Some $$$(n-1)$$$ of the given $$$n$$$ rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.Find any point with integer coordinates that belongs to at least $$$(n-1)$$$ given rectangles.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 132\,674$$$) — the number of given rectangles. Each the next $$$n$$$ lines contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$-10^9 \le x_1 < x_2 \le 10^9$$$, $$$-10^9 \le y_1 < y_2 \le 10^9$$$) — the coordinates of the bottom left and upper right corners of a rectangle.

Print two integers $$$x$$$ and $$$y$$$ — the coordinates of any point that belongs to at least $$$(n-1)$$$ given rectangles.

NoteThe picture below shows the rectangles in the first and second samples. The possible answers are highlighted.  The picture below shows the rectangles in the third and fourth samples.

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

void main()
{
    struct pt { int x, y; }
    struct rect { pt ld, ru; }

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

    rect[] arr;
    foreach (_; 0 .. n) {
        int x1, x2, y1, y2;
        readf("%s %s %s %s", &x1, &y1, &x2, &y2);
        readln;

        arr ~= rect(pt(x1, y1), pt(x2, y2));
    }

    debug { arr.writeln; }

    immutable int MX = 10 ^^ 9 + 1;
    pt ldmx = pt(-MX, -MX), rumx = pt(MX, MX);
    pt[2] mxup = [ldmx, ldmx], mxr = [ldmx, ldmx], mxd = [rumx, rumx], mxle = [rumx, rumx];

    void go(pt p, ref pt[2] arr, bool function(pt p, pt a) cmp) {
        if (cmp(p, arr[0])) arr[1] = arr[0], arr[0] = p;
        else if (cmp(p, arr[1])) arr[1] = p;
    }

    foreach (e; arr) {
        go(e.ld, mxup, (p1, p2) => p1.y > p2.y);
        go(e.ld, mxr, (p1, p2) => p1.x > p2.x);
        go(e.ru, mxd, (p1, p2) => p1.y < p2.y);
        go(e.ru, mxle, (p1, p2) => p1.x < p2.x);
    }

    debug { mxup.writeln; mxr.writeln; mxd.writeln; mxle.writeln; }

    foreach (e; arr) {
        pt nowmxup = e.ld != mxup[0] ? mxup[0] : mxup[1];
        pt nowmxr = e.ld != mxr[0] ? mxr[0] : mxr[1];
        pt nowmxd = e.ru != mxd[0] ? mxd[0] : mxd[1];
        pt nowmxle = e.ru != mxle[0] ? mxle[0] : mxle[1];

        auto nowld = pt(nowmxr.x, nowmxup.y);
        auto nowru = pt(nowmxle.x, nowmxd.y);
        if (nowru.x < nowld.x || nowru.y < nowld.y) continue;

        writeln(nowld.x, ' ', nowld.y);
        return;
    }

    assert(false);
}


We know that lucky digits are digits 4 and 7, however Vasya's got another favorite digit 0 and he assumes it also is lucky! Lucky numbers are such non-negative integers whose decimal record only contains lucky digits. For example, numbers 0, 47, 7074 are lucky, but 1, 7377, 895,  -7 are not.Vasya has t important positive integers he needs to remember. Vasya is quite superstitious and he wants to remember lucky numbers only, so he is asking you for each important number to represent it as a sum of exactly six lucky numbers (Vasya just can't remember more numbers). Then Vasya can just remember these six numbers and calculate the important number at any moment.For each of t important integers represent it as the sum of six lucky numbers or state that this is impossible.

The first line contains a single integer t (1 ≤ t ≤ 5000). Next t lines contain a single positive integer ni (1 ≤ ni ≤ 1018) — the list of important numbers. Please, do not use the %lld to read or write 64-bit integers С++. It is preferred to read the cin, cout streams or the %I64d specifier.

Print t lines. The i-th line must contain the answer for the i-th important number: if the solution exists, the line must contain exactly six lucky numbers the sum of which equals ni, if the solution doesn't exist the string must contain a single integer -1. If there are multiple answers print any of them.

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;

immutable int MAX_K = 20;
immutable int MAX_P = 6;
immutable int MED_N = 10;
immutable int MAX_N = MED_N * MAX_P + 1;
immutable int NA    = -1;

int [] lucky;
int [] g;
int [] r;

int [] get_lucky (int lim = MED_N)
{
	int [] lucky;

	void recur (int cur, int lim)
	{
		if (cur >= lim)
		{
			return;
		}
		lucky ~= cur;
		cur *= 10;
		recur (cur + 0, lim);
		recur (cur + 4, lim);
		recur (cur + 7, lim);
	}

	lucky ~= 0;
	recur (4, lim);
	recur (7, lim);
	lucky = lucky.sort.uniq.array;
	debug {writeln (lucky.length, ' ', lucky);}
	return lucky;
}

void prepare (int lim = MAX_N)
{
	g = new int [lim];
	r = new int [lim];
	g[] = 7;
	g[0] = 0;
	r[0] = 0;
	foreach (i; 0..lim)
	{
		foreach (c; lucky)
		{
			int j = i + c;
			if (j >= lim)
			{
				break;
			}
			if (g[j] > g[i] + 1)
			{
				g[j] = g[i] + 1;
				r[j] = c;
			}
		}
	}
	debug
	{
		foreach (i; 0..lim)
		{
			writefln ("%6d %d %d", i, g[i], r[i]);
		}
	}
}

void main ()
{
	lucky = get_lucky ();
	prepare ();

	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		long n;
		readf (" %s", &n);
		int [] a;
		foreach (i; 0..MAX_K)
		{
			a ~= n % 10;
			n /= 10;
		}
		debug {writeln (a);}
		int [MAX_P] [MAX_K + 1] f;
		foreach (i; 0..MAX_K + 1)
		{
			foreach (p; 0..MAX_P)
			{
				f[i][p] = NA;
			}
		}
		f[0][0] = 0;
		// f[i][p] get the last i digits plus p to i-th digit
		foreach (i; 0..MAX_K)
		{
			foreach (p; 0..MAX_P)
			{
				if (f[i][p] != NA)
				{
					int v = a[i] - p;
					int addq = (v >= 10);
					foreach (q; 0..MAX_P - addq)
					{
						// get a[i] + p + q * 10
						int w = v + q * 10;
						if (w < 0)
						{
							continue;
						}
						debug {writefln ("%s %s %s",
						       v, w, g[w]);}
						if (g[w] <= 6)
						{
							f[i + 1][q + addq] = p;
							debug {writefln
							    ("%s %s -> " ~
							     "%s %s", i, p,
							     i + 1, q + addq);}
						}
					}
				}
			}
		}
		debug {writeln (f[MAX_K][0]);}
		if (f[MAX_K][0] == NA)
		{
			writeln (-1);
		}
		else
		{
			long [MAX_P] res;
			int q = 0;
			foreach_reverse (i; 0..MAX_K)
			{
				int p = f[i + 1][q];
				int v = a[i] - p;
				int w = v + q * 10;
				debug {writefln ("i = %s, p = %s, q = %s, " ~
				                 "v = %s, w = %s",
				                 i, p, q, v, w);}
				enforce (g[w] <= 6);
				foreach (d; 0..MAX_P)
				{
					res[d] = res[d] * 10 + r[w];
					w -= r[w];
				}
				enforce (w == 0);
				q = p;
			}
			writefln ("%(%s %)", res);
		}
	}
}


Let's call a list of positive integers $$$a_0, a_1, ..., a_{n-1}$$$ a power sequence if there is a positive integer $$$c$$$, so that for every $$$0 \le i \le n-1$$$ then $$$a_i = c^i$$$.Given a list of $$$n$$$ positive integers $$$a_0, a_1, ..., a_{n-1}$$$, you are allowed to:  Reorder the list (i.e. pick a permutation $$$p$$$ of $$$\{0,1,...,n - 1\}$$$ and change $$$a_i$$$ to $$$a_{p_i}$$$), then  Do the following operation any number of times: pick an index $$$i$$$ and change $$$a_i$$$ to $$$a_i - 1$$$ or $$$a_i + 1$$$ (i.e. increment or decrement $$$a_i$$$ by $$$1$$$) with a cost of $$$1$$$. Find the minimum cost to transform $$$a_0, a_1, ..., a_{n-1}$$$ into a power sequence.

The first line contains an integer $$$n$$$ ($$$3 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_0, a_1, ..., a_{n-1}$$$ ($$$1 \le a_i \le 10^9$$$).

Print the minimum cost to transform $$$a_0, a_1, ..., a_{n-1}$$$ into a power sequence.

NoteIn the first example, we first reorder $$$\{1, 3, 2\}$$$ into $$$\{1, 2, 3\}$$$, then increment $$$a_2$$$ to $$$4$$$ with cost $$$1$$$ to get a power sequence $$$\{1, 2, 4\}$$$.

// Vicfred
// https://codeforces.com/contest/1397/problem/B
// math
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 35) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10L^^14;
    for(long c = 1L; c <= 10^^9; c++) {
        long minima = 0L;

        for(int i = 0; i < n; i++)
            minima += abs(a[i] - c^^i);

        if(minima > ans)
            break;

        ans = min(ans,minima);
    }

    ans.writeln;
}

// Vicfred
// https://codeforces.com/contest/1397/problem/B
// math
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 35) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10L^^14;
    for(long c = 1L; c <= 10^^9; c++) {
        //writefln("c: %s", c);
        long minima = 0L;
        for(int i = 0L; i < n; i++) {
            minima += abs(a[i] - c^^i);
            //writefln("i: %s", i);
        }
        //writefln("minima: %s", minima);
        if(minima > ans && ans != 10^^18)
            break;
        ans = min(ans,minima);
    }

    ans.writeln;
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(real)).array;
		sort (a);
		real res = real.max;
		for (real base = 1; log10 (base) * (n - 1) <= 12; base++)
		{
			res = min (res, n.iota.map !(i =>
			    abs (a[i] - pow (base, i))).sum);
		}
		writefln !("%.0f") (res);
	}
}

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

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    int n = read!int;
    readln;
    auto arr = readln.strip.split.map!(x => x.to!long).array.sort;
    debug writeln("length = ", arr.length);
    auto up = pow(10_000_000_001, 1.0/(n-1)).floor.to!long;

    long m = long.max;
    debug writeln("up = ", up);
    foreach (c; 1..up + 1)
    {
	long t = arr[0] - 1;
	long q = 1;
	debug writeln("c = ", c);
	for (int i = 1; i < n; i++)
	{
	    q *= c;
	    debug writeln("q = ", q);
	    t += abs(q - arr[i]).to!long;
	}

	if (t < m)
	    m = t;

	debug writeln(t);
    }

    //debug writeln ("c = ", c, " m = ", m);
    writeln (m);

    return 0;
}

import std.math;
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.experimental.checkedint;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long();
  auto a = next!long(n);
  sort(a);
  bool admissibleC(long c)
  {
    auto p = checked!Saturate(long(1));
    foreach(i; 0 .. n)
      p *= c;
    return p != long.max;
  }
  long mincost = long.max;
  foreach(c; 0 .. 1_000_000_000)
    {
      if (!admissibleC(c))
        break;
      long cost = 0;
      long p = 1;
      foreach(i; 0 .. n)
        {
          cost += abs(a[cast(size_t)i] - p);
          p *= c;
        }
      mincost = min(mincost, cost);
    }
  writeln(mincost);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 4096;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

// Vicfred
// https://codeforces.com/contest/1397/problem/B
// math
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 30) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10^^14;
    for(long c = 1; c <= 10^^9; ++c) {
        //writefln("c: %s", c);
        long minima = 0;
        for(int i = 0; c^^i <= 10^^14; i++) {
            if(i >= n) break;
            minima += abs(a[i] - c^^i);
            //writefln("i: %s", i);
        }
        //writefln("minima: %s", minima);
        if(minima > ans)
            break;
        else
            ans = min(ans,minima);
    }

    ans.writeln;
}

import std.math;
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long();
  auto a = next!long(n);
  sort(a);
  bool admissibleC(long c)
  {
    long p = 1;
    foreach(i; 0 .. n)
      {
        if (p >= 2_000_000_000)
          {
            return false;
          }
        p *= c;
      }
    return true;
  }
  long mincost = long.max;
  foreach(c; 0 .. 1_000_000_000)
    {
      if (!admissibleC(c))
        break;
      long cost = 0;
      long p = 1;
      foreach(i; 0 .. n)
        {
          cost += abs(a[cast(size_t)i] - p);
          p *= c;
        }
      mincost = min(mincost, cost);
    }
  writeln(mincost);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 4096;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}


An array of integers $$$p_{1},p_{2}, \ldots,p_{n}$$$ is called a permutation if it contains each number from $$$1$$$ to $$$n$$$ exactly once. For example, the following arrays are permutations: $$$[3,1,2], [1], [1,2,3,4,5]$$$ and $$$[4,3,1,2]$$$. The following arrays are not permutations: $$$[2], [1,1], [2,3,4]$$$.There is a hidden permutation of length $$$n$$$.For each index $$$i$$$, you are given $$$s_{i}$$$, which equals to the sum of all $$$p_{j}$$$ such that $$$j < i$$$ and $$$p_{j} < p_{i}$$$. In other words, $$$s_i$$$ is the sum of elements before the $$$i$$$-th element that are smaller than the $$$i$$$-th element.Your task is to restore the permutation.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the size of the permutation. The second line contains $$$n$$$ integers $$$s_{1}, s_{2}, \ldots, s_{n}$$$ ($$$0 \le s_{i} \le \frac{n(n-1)}{2}$$$). It is guaranteed that the array $$$s$$$ corresponds to a valid permutation of length $$$n$$$.

Print $$$n$$$ integers $$$p_{1}, p_{2}, \ldots, p_{n}$$$ — the elements of the restored permutation. We can show that the answer is always unique.

NoteIn the first example for each $$$i$$$ there is no index $$$j$$$ satisfying both conditions, hence $$$s_i$$$ are always $$$0$$$.In the second example for $$$i = 2$$$ it happens that $$$j = 1$$$ satisfies the conditions, so $$$s_2 = p_1$$$.In the third example for $$$i = 2, 3, 4$$$ only $$$j = 1$$$ satisfies the conditions, so $$$s_2 = s_3 = s_4 = 1$$$. For $$$i = 5$$$ all $$$j = 1, 2, 3, 4$$$ are possible, so $$$s_5 = p_1 + p_2 + p_3 + p_4 = 10$$$.

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.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    struct fwtr {
        int mx;
        long[] arr;
        this(int n) {
            mx = n;
            arr = new long[] (n+1);
        }

        void add(int p, int v) {
            while (p <= mx) {
                arr[p] += v;
                p += (p & (-p));
            }
        }

        long sm(int p) {
            long ret = 0;
            while (p > 0) {
                ret += arr[p];
                p -= (p & (-p));
            }

            return ret;
        }
    }

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

    auto arr = readln.chomp.split.map!(to!long).array;
    auto sums = (n+1).iota.dropOne.cumulativeFold!((a, b) => a + b);

    debug { sums.writeln; }

    auto f = new fwtr(n);

    int binS(long sm) {
        long getSm(int idx) {
            return (idx.to!long * (idx + 1)) / 2 - f.sm(idx);
        }

        int le = 1, r = n;
        while (le < r) {
            int m = (le + r) / 2 + 1;
            if (getSm(m-1) <= sm) { le = m; }
            else { r = m - 1; }
        }

        return le;
    }

    auto ans = new int[] (n);

    foreach_reverse (i, sm; arr) {
        int val = binS(sm);
        f.add(val, val);
        ans[i] = val;
    }

    ans.map!(to!string).join(" ").writeln;
}

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(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;
	long[] ss;
	foreach(i; 0 .. n) ss ~= read.to!long;
	log("ss:", ss);

	auto seg = new SegTree!long(0, n, ((x, y) => x + y), 0);
	foreach(i; 0 .. n) seg.setValue(i, n, i + 1);
	log("seg:", n.iota.map!(i => seg.getValue(i)));

	long[] ans;
	foreach_reverse(s; ss){
		int j = uplimit!int(0, n - 1, (i => (seg.getValue(i) <= s))) + 1;
		long an = j + 1;
		ans ~= an;
		seg.setValue(j, n, -an);
		log("seg:", n.iota.map!(i => seg.getValue(i)));
	}
	log("ans:", ans);

	ans.reverse.map!(to!string).join(" ").writeln;

}

// a <= x <=  c の中でfをみたす最大(二分探索; binary search) ※テンプレート版
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c;
	if(! f(a)) return a - 1;
	while(a + 1 < c){
		T b = (a + c) / 2;
		if(f(b)) a = b;
		else c = b;
	}
	return a;
}

/*
セグメント木 区分木 segtree segment tree
範囲に書き込み、1点で読み出す。
たとえば、 [l, r) の範囲に一律 x を足すクエリがくるなど。

引数
a, b　添字の範囲。 この意味はa から b - 1 までなので注意。
apply　(x, y) => すでに値 x があるところに y を加えるとどうなるか ※結合的な演算である必要がある
neutral　すべての x に対して apply(z, x) = apply(x, z) = x をみたす z

*/
class SegTree(T){
	T delegate(T, T) apply;
	T neutral;
	int a, b; // a <= x < b を担当する
	bool isTerminal;
	SegTree left, right; // 左の子(a <= x < c)、右の子(c <= x < b)
	T value;
	this(int a, int b, T delegate(T, T) apply, T neutral = T.init){
		this.a = a, this.b = b;
		this.apply = apply;
		this.neutral = neutral;
		this.value = neutral;
		if(b - a == 1){
			this.isTerminal = 1;
		}
		else{
			int c = (a + b) / 2;
			this.left = new SegTree(a, c, apply, neutral);
			this.right = new SegTree(c, b, apply, neutral);
		}
	}

	// [a, b) に値を設定する ※[a, b) はこのノードの担当区間とかぶっていなくてもよい
	void setValue(int a, int b, T value){
		if(b <= this.a || this.b <= a) return;
		if(a <= this.a && this.b <= b){
			this.value = apply(this.value, value);
		}
		else{
			divideValue();
			left.setValue(a, b, value);
			right.setValue(a, b, value);
		}
	}

	// 自分の持っていた値を子に引き継ぐ
	void divideValue(){
		left.value = apply(left.value, this.value);
		right.value = apply(right.value, this.value);
		this.value = neutral;
	}

	// iにおける値 ※iは必ずこのノードの担当区間内である前提
	T getValue(int i){
		assert(a <= i && i < b);
		if(this.isTerminal) return this.value;
		else divideValue();
		if(i < left.b) return left.getValue(i);
		else return right.getValue(i);
	}

	// 配列に変換したもの（デバッグ用）
	T[] array(){
		T[] res;
		foreach(i; a .. b) res ~= this.value;
		return res;
	}

	// 文字列に変換したもの（デバッグ用）
	override string toString(){
		if(this.isTerminal) return this.array.to!string;
		else	return this.array.to!string ~ "\n" ~ left.toString ~ right.toString ~ "\n";
	}
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }

enum INF = 10L^^18;
alias Entry = Tuple!(long, "val", int, "key");

class SegmentTree {
  int n;
  Entry[] mx;
  long[] add;
  this(long[] ini) {
    const n_ = cast(int)(ini.length);
    for (n = 1; n < n_; n <<= 1) {}
    mx = new Entry[n << 1];
    add = new long[n << 1];
    mx[] = Entry(-INF, -1);
    add[] = 0;
    foreach (i; 0 .. n_) {
      mx[n + i] = Entry(ini[i], i);
    }
    foreach_reverse (a; 1 .. n) {
      mx[a] = max(mx[a << 1], mx[a << 1 | 1]);
    }
  }
  // [a, b)
  private Entry query(int u, int x, int y, int a, int b, long val) {
    chmax(a, x);
    chmin(b, y);
    if (a >= b) {
      return Entry(-INF, -1);
    }
    if (a == x && b == y) {
      mx[u].val += val;
      add[u] += val;
      return mx[u];
    }
    assert(u < n);
    const uL = u << 1 | 0;
    const uR = u << 1 | 1;
    const mid = (x + y) >> 1;
    if (add[u] != 0) {
      mx[uL].val += add[u];
      mx[uR].val += add[u];
      add[uL] += add[u];
      add[uR] += add[u];
      add[u] = 0;
    }
    const resL = query(uL, x, mid, a, b, val);
    const resR = query(uR, mid, y, a, b, val);
    mx[u] = max(mx[uL], mx[uR]);
    return max(resL, resR);
  }
  // [a, b)
  Entry query(int a, int b, long val) {
    return query(1, 0, n, a, b, val);
  }
}

int N;
long[] S;

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

      S[] *= -1;
      debug {
        writeln("S = ", S);
      }
      auto seg = new SegmentTree(S);
      auto ans = new int[N];

      foreach (x; 1 .. N + 1) {
        const e = seg.query(0, N, 0);
        debug {
          writefln("x = %s: e = %s", x, e);
        }
        assert(e.val == 0);
        ans[e.key] = x;
        seg.query(e.key, e.key + 1, -INF);
        seg.query(e.key + 1, N, x);
      }

      foreach (i; 0 .. N) {
        if (i > 0) {
          write(" ");
        }
        write(ans[i]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}