Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $$$a$$$, $$$b$$$, and $$$c$$$. Now he needs to find out some possible integer length $$$d$$$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Help Yura, find any possible length of the fourth side.A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.

The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 1000$$$). The next $$$t$$$ lines describe the test cases. Each line contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ — the lengths of the three fence segments ($$$1 \le a, b, c \le 10^9$$$).

For each test case print a single integer $$$d$$$ — the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists.

NoteWe can build a quadrilateral with sides $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$.We can build a quadrilateral with sides $$$12$$$, $$$34$$$, $$$56$$$, $$$42$$$.

// Cheese-Cracker: cheese-cracker.github.io

void play(){
    ll a = rd;
    ll b = rd;
    ll c = rd;
    writeln(a + b + c - 1);
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		uint a, b, c;
		readf !(" %s %s %s") (a, b, c);
		writeln (a + b + c - 1);
	}
}

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.traits;

void main(string[] args)
{
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto a = next!long;
      auto b = next!long;
      auto c = next!long;
      auto maxside = max(a, b, c);
      writeln(maxside);
    }
}

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, 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 long[](t);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto c = RD;
		bool[long] set;
		set[a] = true;
		set[b] = true;
		set[c] = true;

		auto arr = [a, b, c];
		arr.sort();
		auto rem = arr[2] - (arr[0]+arr[1]) + 1;

		auto l = max(1, rem);
		foreach (long i; l..l+10)
		{
			if (set.get(i, false) == false)
			{
				ans[ti] = i;
				break;
			}
		}
	}

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

// Cheese-Cracker: cheese-cracker.github.io

void play(){
    ll a = rd;
    ll b = rd;
    ll c = rd;
    writeln(min(a, b, c));
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }

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 a = RD;
		auto b = RD;
		auto c = RD;
		bool[long] set;
		set[a] = true;
		set[b] = true;
		set[c] = true;

		foreach (i; 1..10)
		{
			if (set.get(i, false) == false)
			{
				ans[ti] = i;
				break;
			}
		}
	}

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


You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. $$$n$$$ is even.For each position $$$i$$$ ($$$1 \le i \le n$$$) in string $$$s$$$ you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every position must be changed exactly once.For example, letter 'p' should be changed either to 'o' or to 'q', letter 'a' should be changed to 'b' and letter 'z' should be changed to 'y'.That way string "codeforces", for example, can be changed to "dpedepqbft" ('c' $$$\rightarrow$$$ 'd', 'o' $$$\rightarrow$$$ 'p', 'd' $$$\rightarrow$$$ 'e', 'e' $$$\rightarrow$$$ 'd', 'f' $$$\rightarrow$$$ 'e', 'o' $$$\rightarrow$$$ 'p', 'r' $$$\rightarrow$$$ 'q', 'c' $$$\rightarrow$$$ 'b', 'e' $$$\rightarrow$$$ 'f', 's' $$$\rightarrow$$$ 't').String $$$s$$$ is called a palindrome if it reads the same from left to right and from right to left. For example, strings "abba" and "zz" are palindromes and strings "abca" and "zy" are not.Your goal is to check if it's possible to make string $$$s$$$ a palindrome by applying the aforementioned changes to every position. Print "YES" if string $$$s$$$ can be transformed to a palindrome and "NO" otherwise.Each testcase contains several strings, for each of them you are required to solve the problem separately.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 50$$$) — the number of strings in a testcase. Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th string. The first line of the pair contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$, $$$n$$$ is even) — the length of the corresponding string. The second line of the pair contains a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.

Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th string of the input. Print "YES" if it's possible to make the $$$i$$$-th string a palindrome by applying the aforementioned changes to every position. Print "NO" otherwise.

NoteThe first string of the example can be changed to "bcbbcb", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.The second string can be changed to "be", "bg", "de", "dg", but none of these resulting strings are palindromes.The third string can be changed to "beeb" which is a palindrome.The fifth string can be changed to "lk", "lm", "nk", "nm", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings "ll" or "mm".

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

void main() {
    auto T = readln.chomp.to!int;
    while (T--) solve;
}

void solve() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    bool ok = true;

    foreach (i; 0..N/2) {
        char x = S[i];
        char y = S[N-i-1];
        if (x != y && abs(x - y) != 2) ok = false;
    }

    writeln(ok ? "YES" : "NO");
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
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;

long f(int n, int x, int y) {
    auto sumOdd = (x+y) % 2 == 1;
    auto rowOdd = x % 2 == 1;
    auto prevRows = n % 2 == 0 ?
        cast(long)(x-1) * n/2 :
        cast(long)(x-1)/2 * (n/2 + n/2 + 1) + (!rowOdd ? (sumOdd ? n/2 : n/2+1) : 0);
    auto prevCols = (y-1) / 2;
    return 1 + prevRows + prevCols + (sumOdd ? f(n, n, n) : 0);
}

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

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto s = readln.chomp;

        bool ok = true;
        for (int i = 0, j = n-1; i < j; ++i, --j) {
            auto dist = abs(s[i] - s[j]);
            ok &= dist == 0 || dist == 2;
        }

        writeln(ok ? "YES" : "NO");
    }
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
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()
{
    int t;
    readf("%s", &t);
    readln;

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto s = readln.chomp;

        bool ok = true;
        for (int i = 0, j = n-1; i < j; ++i, --j) {
            auto dist = abs(s[i] - s[j]);
            ok &= dist == 0 || dist == 2;
        }

        writeln(ok ? "YES" : "NO");
    }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

int[] a;
bool pos(int i,int j){
    int x=a[i],y=a[j];
    if(x==y) return true;
    if((x<26 && y>1 && x+1==y-1)
        ||(x>1 && y<26 && x-1==y+1))
        return true;
    return false;
}
void main(){
    int t=readln.chomp.to!int,n;
    bool ans;
    while(t--){
        n=readln.chomp.to!int;
        a=readln.strip
                .map!(to!int)
                .map!"a-96"
                .array;
        ans=true;
        foreach(i;0..n/2){
            if(!pos(i,n-i-1)){
                ans=false;
                break;
            }
        }
        if(ans)
            writeln("YES");
        else
            writeln("NO");
    }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int t=readln.chomp.to!int,n;
    bool ans;
    while(t--){
        n=readln.chomp.to!int;
        auto a=readln.strip
                     .map!(to!int)
                     .map!"a-96"
                     .array;
        ans=true;
        foreach(i;0..n/2){
            int d=abs(a[i]-a[n-i-1]);
            if(d==1||d>2){
                ans=false;
                break;
            }
        }
        if(ans)
            writeln("YES");
        else
            writeln("NO");
    }
}


As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.Each modification is one of the following:  Pick some row of the matrix and decrease each element of the row by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.  Pick some column of the matrix and decrease each element of the column by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing. DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k modifications? Please, help him to calculate this value.

The first line contains four space-separated integers n, m, k and p (1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100). Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.

Output a single integer — the maximum possible total pleasure value DZY could get.

NoteFor the first sample test, we can modify: column 2, row 2. After that the matrix becomes:1 10 0For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:-3 -3-2 -2

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M, K; long P; scanf("%d %d %d %d\n", &N, &M, &K, &P);
    long[][] F = new long[][N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp.split(" ").map!(to!long).array;
    }
    long[] SR = new long[N],
           SC = new long[M];
    foreach (i; 0 .. N) {
        foreach (j; 0 .. M) {
            SR[i] += F[i][j];
            SC[j] += F[i][j];
        }
    }
    SR = SR.sort.array;
    SC = SC.sort.array;

    long DR = 0, DC = 0;

    BinaryHeap!(Array!long, "a < b") PQ;
    foreach (r; SR) PQ.insert(r);
    long[] R = new long[K + 1];
    R[0] = 0;
    foreach (i; 1 .. K + 1) {
        long r = PQ.front;
        R[i] = R[i - 1] + r;
        PQ.removeFront;
        PQ.insert(r - P * M);
    }

    PQ.clear;
    foreach (c; SC) PQ.insert(c);
    long[] C = new long[K + 1];
    C[0] = 0;
    foreach (i; 1 .. K + 1) {
        long c = PQ.front;
        C[i] = C[i - 1] + c;
        PQ.removeFront;
        PQ.insert(c - P * N);
    }

    /*
    writeln(R);
    writeln(C);
    */

    long Ans = long.min;

    foreach (long i; 0 .. K + 1) {
        Ans = max(Ans, R[cast(uint)i] + C[K - cast(uint)i] - (i * (cast(long)K - i) * P));
    }
    writeln(Ans);
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int M, N, K;
long P;
long[][] A;

long[] solve(long[] xs, long dec) {
	auto q = heapify(xs);
	long[] ret = new long[K + 1];
	foreach (k; 0 .. K) {
		const now = q.front;
		q.removeFront;
		ret[k + 1] = ret[k] + now;
		q.insert(now - dec);
	}
	return ret;
}

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		N = readInt;
		K = readInt;
		P = readLong;
		A = new long[][](M, N);
		foreach (x; 0 .. M) foreach (y; 0 .. N) {
			A[x][y] = readLong;
		}

		long[] bs = new long[M];
		long[] cs = new long[N];
		foreach (x; 0 .. M) foreach (y; 0 .. N) {
			bs[x] += A[x][y];
			cs[y] += A[x][y];
		}

		long[] fbs = solve(bs, P * N);
		long[] fcs = solve(cs, P * M);
// writeln(fbs);
// writeln(fcs);
		long ans = long.min;
		foreach (k; 0 .. K + 1) {
			chmax(ans, fbs[k] + fcs[K - k] - P * k * (K - k));
		}
		writeln(ans);

	}
	} catch (EOFException) {}
}

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M, K; long P; scanf("%d %d %d %d\n", &N, &M, &K, &P);
    long[][] F = new long[][N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp.split(" ").map!(to!long).array;
    }
    long[] SR = new long[N],
           SC = new long[M];
    foreach (i; 0 .. N) {
        foreach (j; 0 .. M) {
            SR[i] += F[i][j];
            SC[j] += F[i][j];
        }
    }
    SR = SR.sort.array;
    SC = SC.sort.array;

    long DR = 0, DC = 0;

    BinaryHeap!(Array!long, "a < b") PQ;
    foreach (r; SR) PQ.insert(r);
    long[] R = new long[K + 1];
    R[0] = 0;
    foreach (i; 1 .. K + 1) {
        long r = PQ.front;
        R[i] = R[i - 1] + r;
        PQ.removeFront;
        PQ.insert(r - P * M);
    }

    PQ.clear;
    foreach (c; SC) PQ.insert(c);
    long[] C = new long[K + 1];
    C[0] = 0;
    foreach (i; 1 .. K + 1) {
        long c = PQ.front;
        C[i] = C[i - 1] + c;
        PQ.removeFront;
        PQ.insert(c - P * N);
    }

    /*
    writeln(R);
    writeln(C);
    */

    long Ans = 0;

    foreach (i; 0 .. K + 1) {
        Ans = max(Ans, R[i] + C[K - i] - (i * (K - i) * P));
    }
    writeln(Ans);
}

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M, K, P; scanf("%d %d %d %d\n", &N, &M, &K, &P);
    int[][] F = new int[][N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp.split(" ").map!(to!int).array;
    }
    long[] SR = new long[N],
           SC = new long[M];
    foreach (i; 0 .. N) {
        foreach (j; 0 .. M) {
            SR[i] += F[i][j];
            SC[j] += F[i][j];
        }
    }
    SR = SR.sort.array;
    SC = SC.sort.array;

    struct V {
        int type; // row or column? 1 => row, 2 => column
        long value;
    }
    long DR = 0, DC = 0;
    bool comp(in V a, in V b) {
        long A = a.value - (a.type == 1 ? DR : DC),
             B = b.value - (b.type == 1 ? DR : DC);
        return A < B;
    }

    BinaryHeap!(Array!V, comp) PQ;
    foreach (r; SR) PQ.insert(V(1, r));
    foreach (c; SC) PQ.insert(V(2, c));

    long Calc(in V a) {
        return a.value - (a.type == 1 ? DR : DC);
    }

    long Ans = 0;
    while (K--) {
        V v = PQ.front;
        (v.type == 1 ? DC : DR) += P;
        PQ.insert(V(v.type, v.value - P * (v.type == 1 ? M : N)));
        Ans += Calc(PQ.front);
        PQ.removeFront;
    }
    writeln(Ans);
}

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M, K, P; scanf("%d %d %d %d\n", &N, &M, &K, &P);
    int[][] F = new int[][N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp.split(" ").map!(to!int).array;
    }
    long[] SR = new long[N],
           SC = new long[M];
    foreach (i; 0 .. N) {
        foreach (j; 0 .. M) {
            SR[i] += F[i][j];
            SC[j] += F[i][j];
        }
    }
    SR = SR.sort.array;
    SC = SC.sort.array;

    long DR = 0, DC = 0;

    BinaryHeap!(Array!long, "a < b") PQ;
    foreach (r; SR) PQ.insert(r);
    long[] R = new long[K + 1];
    R[0] = 0;
    foreach (i; 1 .. K + 1) {
        long r = PQ.front;
        R[i] = R[i - 1] + r;
        PQ.removeFront;
        PQ.insert(r - P * M);
    }

    PQ.clear;
    foreach (c; SC) PQ.insert(c);
    long[] C = new long[K + 1];
    C[0] = 0;
    foreach (i; 1 .. K + 1) {
        long c = PQ.front;
        C[i] = C[i - 1] + c;
        PQ.removeFront;
        PQ.insert(c - P * N);
    }

    /*
    writeln(R);
    writeln(C);
    */

    long Ans = 0;

    foreach (i; 0 .. K + 1) {
        Ans = max(Ans, R[i] + C[K - i] - (i * (K - i) * P));
    }
    writeln(Ans);
}

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, M, K; long P; scanf("%d %d %d %d\n", &N, &M, &K, &P);
    long[][] F = new long[][N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp.split(" ").map!(to!long).array;
    }
    long[] SR = new long[N],
           SC = new long[M];
    foreach (i; 0 .. N) {
        foreach (j; 0 .. M) {
            SR[i] += F[i][j];
            SC[j] += F[i][j];
        }
    }
    SR = SR.sort.array;
    SC = SC.sort.array;

    long DR = 0, DC = 0;

    BinaryHeap!(Array!long, "a < b") PQ;
    foreach (r; SR) PQ.insert(r);
    long[] R = new long[K + 1];
    R[0] = 0;
    foreach (i; 1 .. K + 1) {
        long r = PQ.front;
        R[i] = R[i - 1] + r;
        PQ.removeFront;
        PQ.insert(r - P * M);
    }

    PQ.clear;
    foreach (c; SC) PQ.insert(c);
    long[] C = new long[K + 1];
    C[0] = 0;
    foreach (i; 1 .. K + 1) {
        long c = PQ.front;
        C[i] = C[i - 1] + c;
        PQ.removeFront;
        PQ.insert(c - P * N);
    }

    /*
    writeln(R);
    writeln(C);
    */

    long Ans = 0;

    foreach (long i; 0 .. K + 1) {
        Ans = max(Ans, R[cast(uint)i] + C[K - cast(uint)i] - (i * (cast(long)K - i) * P));
    }
    writeln(Ans);
}


You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.What is the minimum number of operations you need to make all of the elements equal to 1?

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

NoteIn the first sample you can turn all numbers to 1 using the following 5 moves:  [2, 2, 3, 4, 6].  [2, 1, 3, 4, 6]  [2, 1, 3, 1, 6]  [2, 1, 1, 1, 6]  [1, 1, 1, 1, 6]  [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.

// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio      : readln, writeln;
import std.array      : split;
import std.string     : chomp;
import std.conv       : to;
import std.numeric    : gcd;

void main()
{
    auto n = readln.chomp.to!int;
    auto xs = readln.chomp.split.to!(int[]);

    auto p = xs[0];
    auto e = 0;
    foreach (x; xs)
    {
        p = gcd(p, x);
        if (x == 1)
        {
            e++;
        }
    }
    if (p != 1)
    {
        writeln(-1);
        return;
    }
    if (e > 0)
    {
        writeln(n-e);
        return;
    }
    auto ans = n * 10;
    void dfs(uint i, int c)
    {
        if (c >= ans)
        {
            return;
        }
        if (i > 0)
        {
            auto g = gcd(xs[i], xs[i-1]);
            if (g == 1)
            {
                ans = c;
                return;
            }
            if (xs[i] != g)
            {
                auto t1 = xs[i]; xs[i] = g; dfs(i, c + 1); xs[i] = t1;
            }
            if (xs[i-1] != g)
            {
                auto t2 = xs[i-1]; xs[i-1] = g; dfs(i-1, c+1); xs[i-1] = t2;
            }
        }
        if (i+1 < n)
        {
            auto g = gcd(xs[i], xs[i+1]);
            if (g == 1)
            {
                ans = c;
                return;
            }
            if (xs[i] != g)
            {
                auto t1 = xs[i]; xs[i] = g; dfs(i, c+1); xs[i] = t1;
            }
            if (xs[i+1] != g)
            {
                auto t2 = xs[i+1]; xs[i+1] = g; dfs(i+1, c+1); xs[i+1] = t2;
            }
        }
    }
    foreach (i, _ ; xs)
    {
        dfs(i, 0);
    }
    ans += n;
    writeln(ans);
}

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

int gcd(int a, int b)
{
    if (b == 0) return a;
    return gcd(b, a % b);
}

void solve(int[] a)
{
    auto n = cast(int)a.length;
    int c = 0;
    foreach (i; 0 .. n)
    {
        if (a[i] == 1) ++ c;
    }
    if (c > 0)
    {
        writeln(n - c);
        return;
    }
    int ans = int.max;
    foreach (i; 0 .. n)
    {
        auto g = a[i];
        foreach (j; i + 1 .. n)
        {
            if (j - i + n - 1 >= ans) break;
            g = gcd(g, a[j]);
            if (g == 1)
            {
                ans = min(ans, j - i + n - 1);
                break;
            }
        }
    }
    if (ans == int.max) ans = -1;
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a);
    }
    return 0;
}

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.7.4"
+/

import std.stdio, std.algorithm, std.range, std.conv;
import std.numeric : gcd;
// import dcomp.foundation, dcomp.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    long[] a;
    sc.read(a);
    int zc = a.count(1).to!int;
    if (zc) {
        writeln(n - zc);
        return 0;
    }
    int ans = 10000;
    foreach (l; 0..n) {
        long g = 0;
        foreach (r; l+1..n+1) {
            g = gcd(g, a[r-1]);
            if (g == 1) {
                ans = min(ans, 2*(r-l-1) + l + (n-r));
            }
        }
    }
    if (ans == 10000) ans = -1;
    writeln(ans);
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}

import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
    public import core.bitop : popcnt;
    int popcnt(ulong v) {
        return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
    }
}

bool poppar(ulong v) {
    v^=v>>1;
    v^=v>>2;
    v&=0x1111111111111111UL;
    v*=0x1111111111111111UL;
    return ((v>>60) & 1) != 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.array;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                FastAppender!(E[]) buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;

T[N] fixed(T, size_t N)(T[N] a) {return a;}

struct FastAppender(A, size_t MIN = 4) {
    import std.algorithm : max;
    import std.conv;
    import std.range.primitives : ElementEncodingType;
    import core.stdc.string : memcpy;

    private alias T = ElementEncodingType!A;
    private T* _data;
    private uint len, cap;

    @property size_t length() const {return len;}
    bool empty() const { return len == 0; }

    void reserve(size_t nlen) {
        import core.memory : GC;
        if (nlen <= cap) return;

        void* nx = GC.malloc(nlen * T.sizeof);

        cap = nlen.to!uint;
        if (len) memcpy(nx, _data, len * T.sizeof);
        _data = cast(T*)(nx);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void opOpAssign(string op : "~")(T item) {
        if (len == cap) {
            reserve(max(MIN, cap*2));
        }
        _data[len++] = item;
    }

    void insertBack(T item) {
        this ~= item;
    }

    void removeBack() {
        len--;
    }

    void clear() {
        len = 0;
    }
    ref inout(T) back() inout { assert(len); return _data[len-1]; }
    ref inout(T) opIndex(size_t i) inout { return _data[i]; }

    T[] data() {
        return (_data) ? _data[0..len] : null;
    }
}

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		int c = a.count (1);
		if (c > 0)
		{
			writeln (n - c);
			continue;
		}

		int d = n;
		for (int i = 0; i < n; i++)
		{
			int v = a[i];
			for (int j = i + 1; j < n; j++)
			{
				v = gcd (v, a[j]);
				if (v == 1)
				{
					d = min (d, j - i);
					break;
				}
			}
		}
		if (d < n)
		{
			writeln (d + n - 1);
			continue;
		}

		writeln (-1);
	}
}

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

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

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

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

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

      int ans = int.max;

      const cnt1 = cast(int)(A.count(1));
      if (cnt1 >= 1) {
        chmin(ans, N - cnt1);
      }

      foreach (i; 0 .. N) {
        long g = 0;
        foreach (j; i .. N) {
          g = gcd(g, A[j]);
          if (g == 1) {
            chmin(ans, (j - i) + (N - 1));
            break;
          }
        }
      }
      writeln((ans >= int.max) ? -1 : ans);
    }
  } catch (EOFException e) {
  }
}


In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.

The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom. The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen.

In the only line print the integer S — the minimum number of burles which are had to spend.

NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.

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

int [101] mas;
void main(string[] args)
{
    int n = 0;
    int k = 0;
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&mas[i]);
        k = max(k,mas[i]);
    }
    int ans=0;
    for(int i = 0; i < n; i++)
    {
        ans += k - mas[i];
    }
    writeln(ans);
}

import std.stdio;
import std.string;

int main()
{
	int a=0;
	readf(" %d", &a);
	int[] m = new int[a];
	for (int i=0; i<a; i++)
	{
		readf(" %d", &m[i]);
	}
	int max=-1;
	int sum=0;
	for (int i=0; i<a; i++)
	{
		if (m[i]>max)
		{
			max=m[i];
		}
	}
	for (int i=0; i<a; i++)
	{
		sum=sum+m[i];
	}
	writeln(max*(a-1)-(sum-max));
	return 0;
}


You are given a set of $$$n\ge 2$$$ pairwise different points with integer coordinates. Your task is to partition these points into two nonempty groups $$$A$$$ and $$$B$$$, such that the following condition holds:For every two points $$$P$$$ and $$$Q$$$, write the Euclidean distance between them on the blackboard: if they belong to the same group — with a yellow pen, and if they belong to different groups — with a blue pen. Then no yellow number is equal to any blue number.It is guaranteed that such a partition exists for any possible input. If there exist multiple partitions, you are allowed to output any of them.

The first line contains one integer $$$n$$$ $$$(2 \le n \le 10^3)$$$ — the number of points. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^6 \le x_i, y_i \le 10^6$$$) — the coordinates of the $$$i$$$-th point.  It is guaranteed that all $$$n$$$ points are pairwise different.

In the first line, output $$$a$$$ ($$$1 \le a \le n-1$$$) — the number of points in a group $$$A$$$. In the second line, output $$$a$$$ integers — the indexes of points that you include into group $$$A$$$. If there are multiple answers, print any.

NoteIn the first example, we set point $$$(0, 0)$$$ to group $$$A$$$ and points $$$(0, 1)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$1$$$ yellow number $$$\sqrt{2}$$$ and $$$2$$$ blue numbers $$$1$$$ on the blackboard.In the second example, we set points $$$(0, 1)$$$ and $$$(0, -1)$$$ to group $$$A$$$ and points $$$(-1, 0)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$2$$$ yellow numbers $$$2$$$, $$$4$$$ blue numbers $$$\sqrt{2}$$$ on the blackboard.

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		alias Point = Tuple !(int, q{x}, int, q{y});
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf !(" %s %s") (c.x, c.y);
		}
		int d;
		while (true)
		{
			d = p.count !(c => ((c.x ^ c.y) & 1) == 1);
			if (d != 0 && d != n)
			{
				break;
			}
			if (d == n)
			{
				foreach (ref c; p)
				{
					c.y += 1;
				}
			}
			foreach (ref c; p)
			{
				c = Point ((c.x + c.y) / 2, (c.x - c.y) / 2);
			}
		}
		writeln (d);
		writefln ("%(%s %)", n.iota.filter !(i =>
		    ((p[i].x ^ p[i].y) & 1) == 1).map !(q{a + 1}));
	}
}


Oleg the bank client and Igor the analyst are arguing again. This time, they want to pick a gift as a present for their friend, ZS the coder. After a long thought, they decided that their friend loves to eat carrots the most and thus they want to pick the best carrot as their present.There are n carrots arranged in a line. The i-th carrot from the left has juiciness ai. Oleg thinks ZS loves juicy carrots whereas Igor thinks that he hates juicy carrots. Thus, Oleg would like to maximize the juiciness of the carrot they choose while Igor would like to minimize the juiciness of the carrot they choose.To settle this issue, they decided to play a game again. Oleg and Igor take turns to play the game. In each turn, a player can choose a carrot from either end of the line, and eat it. The game ends when only one carrot remains. Oleg moves first. The last remaining carrot will be the carrot that they will give their friend, ZS.Oleg is a sneaky bank client. When Igor goes to a restroom, he performs k moves before the start of the game. Each move is the same as above (eat a carrot from either end of the line). After Igor returns, they start the game with Oleg still going first. Oleg wonders: for each k such that 0 ≤ k ≤ n - 1, what is the juiciness of the carrot they will give to ZS if he makes k extra moves beforehand and both players play optimally?

The first line of input contains a single integer n (1 ≤ n ≤ 3·105) — the total number of carrots. The next line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Here ai denotes the juiciness of the i-th carrot from the left of the line.

Output n space-separated integers x0, x1, ..., xn - 1. Here, xi denotes the juiciness of the carrot the friends will present to ZS if k = i.

NoteFor the first example, When k = 0, one possible optimal game is as follows: Oleg eats the carrot with juiciness 1. Igor eats the carrot with juiciness 5. Oleg eats the carrot with juiciness 2. The remaining carrot has juiciness 3.When k = 1, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2. Igor eats the carrot with juiciness 5. The remaining carrot has juiciness 3.When k = 2, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2 beforehand. Oleg eats the carrot with juiciness 3. The remaining carrot has juiciness 5.When k = 3, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2 beforehand. Oleg eats the carrot with juiciness 3 beforehand. The remaining carrot has juiciness 5.Thus, the answer is 3, 3, 5, 5.For the second sample, Oleg can always eat the carrot with juiciness 1 since he always moves first. So, the remaining carrot will always have juiciness 1000000000.

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

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

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

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

void main() {
  /*
  debug {
    enum lim = 10;
    auto dp = new int[][][](lim, 2);
    dp[1] = [[0, 1], [0, 1]];
    foreach (n; 2 .. lim) {
      foreach (t; 0 .. 2) {
        dp[n][t] = new int[1 << n];
        foreach (p; 0 .. 1 << n) {
          dp[n][t][p] = t ^ 1;
          if (dp[n - 1][t ^ 1][p >> 1] == t) {
            dp[n][t][p] = t;
          }
          if (dp[n - 1][t ^ 1][p & ~(1 << (n - 1))] == t) {
            dp[n][t][p] = t;
          }
        }
      }
    }
    foreach (n; 1 .. lim) {
      foreach (t; 0 .. 2) {
        foreach (p; 0 .. 1 << n) {
          write(n, " ", t, " ");
          foreach (i; 0 .. n) {
            write((p >> i) & 1);
          }
          writeln(": ", dp[n][t][p]);
          if (t == 1) {
            int win;
            if (n == 1) {
              win = ((p >> 0) & 1);
            } else if (n % 2 == 0) {
              win |= ((p >> (n / 2 - 1)) & 1);
              win |= ((p >> (n / 2)) & 1);
            } else {
              win |= ((p >> (n / 2 - 1)) & 1) & ((p >> (n / 2)) & 1);
              win |= ((p >> (n / 2)) & 1) & ((p >> (n / 2 + 1)) & 1);
            }
            // assert(dp[n][t][p] == win);
          }
        }
      }
    }
  }
  //*/

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

      auto ans = new int[N];
      // even: 01, 10, 11
      foreach (i; 0 .. N - 1) {
        const a = max(A[i], A[i + 1]);
        const k = abs((i - 0) - (N - (i + 2)));
        chmax(ans[k], a);
      }
      // odd: 011, 110, 111
      foreach (i; 0 .. N - 2) {
        const a = max(min(A[i], A[i + 1]), min(A[i + 1], A[i + 2]));
        const k = abs((i - 0) - (N - (i + 3)));
        chmax(ans[k], a);
      }

      foreach (k; 2 .. N) {
        chmax(ans[k], ans[k - 2]);
      }
      ans[N - 1] = A.maxElement;

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


Let's call an array $$$a_1, a_2, \dots, a_m$$$ of nonnegative integer numbers good if $$$a_1 + a_2 + \dots + a_m = 2\cdot(a_1 \oplus a_2 \oplus \dots \oplus a_m)$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.For example, array $$$[1, 2, 3, 6]$$$ is good, as $$$1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6)$$$. At the same time, array $$$[1, 2, 1, 3]$$$ isn't good, as $$$1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3)$$$.You are given an array of length $$$n$$$: $$$a_1, a_2, \dots, a_n$$$. Append at most $$$3$$$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n \le 10^5)$$$ — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0\le a_i \le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output two lines. In the first line, output a single integer $$$s$$$ ($$$0\le s\le 3$$$) — the number of elements you want to append. In the second line, output $$$s$$$ integers $$$b_1, \dots, b_s$$$ ($$$0\le b_i \le 10^{18}$$$) — the elements you want to append to the array. If there are different solutions, you are allowed to output any of them.

NoteIn the first test case of the example, the sum of all numbers is $$$12$$$, and their $$$\oplus$$$ is $$$6$$$, so the condition is already satisfied.In the second test case of the example, after adding $$$4, 4$$$, the array becomes $$$[8, 4, 4]$$$. The sum of numbers in it is $$$16$$$, $$$\oplus$$$ of numbers in it is $$$8$$$.

import std.stdio;
import std.array;
import std.algorithm : map, sum, fold;
import std.string : strip, split;
import std.conv : to;

void main()
{
  int t;
  readf!"%d\n"(t);
  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);
    auto a = readln.strip.split.map!(to!ulong);

    // append at most 3 elements so that s = 2*x (= x<<1)
    auto s = a.sum;
    auto x = a.fold!"a ^ b"(0uL);

    writefln("2\n%d %d", x, s+x);
  }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		auto totalSum = a.reduce !(q{a + b});
		auto totalXor = a.reduce !(q{a ^ b});

		a ~= totalXor;
		totalSum += totalXor;
		totalXor ^= totalXor;

		a ~= totalSum;
		totalXor ^= totalSum;
		totalSum += totalSum;

		writeln (2, " ", a[$ - 2], " ", a[$ - 1]);
	}
}

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 rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long[] as = rlong(n);
		log("as:", as);

		long u;
		foreach(a; as) u += a;

		long v;
		foreach(a; as) v ^= (a * 2);

		log("u:", u, "v:", v);

		long[] ans;

		{
			long tmp = 1;
			while(tmp <= u || tmp <= v) tmp *= 2;
			if(u % 2 == 1 && tmp % 2 == 0) tmp += 1;
			if(u % 2 == 0 && tmp % 2 == 1) tmp -= 1;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}

		if(u < v){
			log("u < v");
			long tmp = (v - u) / 2;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}

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

	}
}

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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//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 main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		auto x = a.sum;
		if (x % 2 == 1)
		{
			a ~= 1;
			x += 1;
			ans[ti] ~= 1;
		}
		long y;
		foreach (i; 0..a.length)
		{
			y ^= a[i];
		}
		debug writeln("x:", x, " y:", y);
		long tmp;
		foreach (i; 1..64)
		{
			auto bit = 1L << i;
			if (((x >> i) & 1) == ((y >> (i-1)) & 1))
				continue;
			tmp += bit;
			x   += bit;
			y   ^= bit;
		}
		ans[ti] ~= tmp;
	}

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

import std.stdio;
import std.array;
import std.algorithm : map, sum, fold;
import std.string : strip, split;
import std.conv : to;

void main()
{
  int t;
  readf!"%d\n"(t);
  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);
    auto a = readln.strip.split.map!(to!int);

    // append at most 3 elements so that s = 2*x (= x<<1)
    auto s = a.sum;
    auto x = a.fold!"a ^ b"(0);

    writefln("2\n%d %d", x, s+x);
  }
}

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 rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long[] as = rlong(n);
		log("as:", as);

		long u;
		foreach(a; as) u += a;

		long v;
		foreach(a; as) v ^= (a * 2);

		log("u:", u, "v:", v);

		long[] ans;

		{
			long tmp = 1;
			while(tmp <= u || tmp <= v) tmp *= 2;
			if(u % 2 && tmp > 1) tmp += 1;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}

		if(u < v){
			log("u < v");
			long tmp = (v - u) / 2;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}

		ans.length.writeln;
		ans.map!(to!string).array.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 rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long[] as = rlong(n);
		log("as:", as);

		long u;
		foreach(a; as) u += a;

		long v;
		foreach(a; as) v ^= (a * 2);

		log("u:", u, "v:", v);

		long[] ans;
		if(u > v){
			log("u > v");
			long tmp = 1;
			while(tmp <= u) tmp *= 2;
			if(u % 2) tmp += 1;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}
		if(u < v){
			log("u < v");
			long tmp = (v - u) / 2;
			log("tmp:", tmp);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			ans ~= tmp, u += tmp, v ^= (tmp * 2);
			log("ans:", ans, "u:", u, "v:", v);
		}

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

	}
}

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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//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 main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		auto x = a.sum;
		if (x % 2 == 1)
		{
			a ~= 1;
			x += 1;
			ans[ti] ~= 1;
		}
		long y;
		foreach (i; 0..n)
		{
			y ^= a[i];
		}
		long tmp;
		foreach (i; 1..64)
		{
			auto bit = 1L << i;
			if (((x >> i) & 1) == ((y >> (i-1)) & 1))
				continue;
			tmp += bit;
			x   += bit;
			y   ^= bit;
		}
		ans[ti] ~= tmp;
	}

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