One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume that the string contains the letters of some users of the R1 mail. Recovering letters is a tedious mostly manual work. So before you start this process, it was decided to estimate the difficulty of recovering. Namely, we need to calculate the number of different substrings of the saved string that form correct e-mail addresses.We assume that valid addresses are only the e-mail addresses which meet the following criteria:  the address should begin with a non-empty sequence of letters, numbers, characters '_', starting with a letter;  then must go character '@';  then must go a non-empty sequence of letters or numbers;  then must go character '.';  the address must end with a non-empty sequence of letters. You got lucky again and the job was entrusted to you! Please note that the substring is several consecutive characters in a string. Two substrings, one consisting of the characters of the string with numbers l1, l1 + 1, l1 + 2, ..., r1 and the other one consisting of the characters of the string with numbers l2, l2 + 1, l2 + 2, ..., r2, are considered distinct if l1 ≠ l2 or r1 ≠ r2.

The first and the only line contains the sequence of characters s1s2... sn (1 ≤ n ≤ 106) — the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'.

Print in a single line the number of substrings that are valid e-mail addresses.

NoteIn the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com.In the second test case note that the e-mail x@x.x is considered twice in the answer. Note that in this example the e-mail entries overlap inside the string.

import std.ascii;
import std.stdio;
import std.string;

void main ()
{
	string s;
	while ((s = readln ().strip ()) != "")
	{
		int n = s.length;
		long res = 0;
		foreach (i; 0..n)
		{
			if (s[i] == '@')
			{
				int j;
				j = i - 1;
				long left = 0;
				while (j >= 0)
				{
					if ('a' <= s[j] && s[j] <= 'z')
					{
						j--;
						left++;
					}
					else if (('0' <= s[j] &&
					          s[j] <= '9') ||
					         (s[j] == '_'))
					{
						j--;
					}
					else
					{
						break;
					}
				}
				j = i + 1;
				while ((j < n) &&
				       (('a' <= s[j] && s[j] <= 'z') ||
				        ('0' <= s[j] && s[j] <= '9')))
				{
					j++;
				}
				if ((j == i + 1) || (j >= n) || (s[j] != '.'))
				{
					continue;
				}
				j++;
				long right = 0;
				while (j < n)
				{
					if ('a' <= s[j] && s[j] <= 'z')
					{
						j++;
						right++;
					}
					else
					{
						break;
					}
				}
				res += left * right;
			}
		}
		writeln (res);
	}
}

import std.ascii;
import std.stdio;
import std.string;

void main ()
{
	string s;
	while ((s = readln ().strip ()) != "")
	{
		int n = s.length;
		long res = 0;
		foreach (i; 0..n)
		{
			if (s[i] == '@')
			{
				int j;
				j = i - 1;
				long left = 0;
				while (j >= 0)
				{
					if ('a' <= s[j] && s[j] <= 'z')
					{
						j--;
						left++;
					}
					else if (('0' <= s[j] &&
					          s[j] <= '9') ||
					         (s[j] == '_'))
					{
						j--;
					}
					else
					{
						break;
					}
				}
				j = i + 1;
				while ((j < n) &&
				       (('a' <= s[j] && s[j] <= 'z') ||
				        ('0' <= s[j] && s[j] <= '9')))
				{
					j++;
				}
				if ((j >= n) || (s[j] != '.'))
				{
					continue;
				}
				j++;
				long right = 0;
				while (j < n)
				{
					if ('a' <= s[j] && s[j] <= 'z')
					{
						j++;
						right++;
					}
					else
					{
						break;
					}
				}
				res += left * right;
			}
		}
		writeln (res);
	}
}


The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers. All these integers are distinct, each value from $$$1$$$ to $$$n$$$ appears in the sequence exactly once.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[2, 1, 5, 4, 3]$$$ the answer is $$$4$$$ (you take $$$2$$$ and the sequence becomes $$$[1, 5, 4, 3]$$$, then you take the rightmost element $$$3$$$ and the sequence becomes $$$[1, 5, 4]$$$, then you take $$$4$$$ and the sequence becomes $$$[1, 5]$$$ and then you take $$$5$$$ and the sequence becomes $$$[1]$$$, the obtained increasing sequence is $$$[2, 3, 4, 5]$$$).

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. All these integers are pairwise distinct.

In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.

NoteThe first example is described in the problem statement.

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
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.ascii, std.typecons;

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

    auto a = readln.chomp.split.map!(to!int).array;

    void gole(int p, int lm, int lst, ref dchar[] ans) {
        while (p <= lm && a[p] > lst) {
            ans ~= 'L';
            lst = a[p];
            ++p;
        }

        debug { writeln(ans.length); }
    }
    void gor(int p, int lm, int lst, ref dchar[] ans) {
        while (p >= lm && a[p] > lst) {
            ans ~= 'R';
            lst = a[p];
            --p;
        }

        debug { writeln(ans.length); }
    }
    int lst = 0;
    int le = 0, r = n-1;
    dchar[] ans;
    while (le <= r) {
        int leok = lst < a[le];
        int rok = lst < a[r];
        if (!leok && !rok) {
            break;
        }
        if (!leok) {
            gor(r, le, lst, ans);
            break;
        }
        if (!rok) {
            gole(le, r, lst, ans);
            break;
        }

        if (a[le] < a[r]) {
            ans ~= 'L';
            lst = a[le];
            ++le;
        } else if (a[r] < a[le]) {
            ans ~= 'R';
            lst = a[r];
            --r;
        } else {
            dchar[] ansle;
            dchar[] ansr;
            gole(le, r, lst, ansle);
            gor(r, le, lst, ansr);

            if (ansle.length > ansr.length) { ans ~= ansle; }
            else { ans ~= ansr; }
            break;
        }
    }

    ans.length.writeln;
    ans.writeln;
}

import std.algorithm;
import std.conv;
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 !(int)).array;
		auto p = a.length - a.findAdjacent !(q{a >= b}).length;
		auto q = a.length - a.retro.findAdjacent !(q{a >= b}).length;
		auto lo = a.take (min (p + 1, n)).array;
		auto hi = a.retro.take (min (q + 1, n)).array;
		debug {writeln (lo, " ", hi);}

		string res;
		while (true)
		{
			if (lo.empty)
			{
				res ~= 'R'.repeat (hi.length).text;
				break;
			}

			if (hi.empty)
			{
				res ~= 'L'.repeat (lo.length).text;
				break;
			}

			if (lo.front < hi.front)
			{
				res ~= 'L';
				lo.popFront ();
			}
			else if (lo.front > hi.front)
			{
				res ~= 'R';
				hi.popFront ();
			}
			else if (lo.length < hi.length)
			{
				res ~= 'R'.repeat (hi.length).text;
				break;
			}
			else
			{
				res ~= 'L'.repeat (lo.length).text;
				break;
			}
		}

		res.length = min (res.length, n);
		writeln (res.length);
		writeln (res);
	}
}

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; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){

	int n = read.to!int;
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;

	string ans;
	int i = 0, j = n - 1;
	long x = -1;
	while(i <= j){
		if(as[i] < as[j]){
			if(x < as[i]) x = as[i], ans ~= "L", i += 1;
			else if(x < as[j]) x = as[j], ans ~= "R", j -= 1;
			else break;
		}
		else{
			if(x < as[j]) x = as[j], ans ~= "R", j -= 1;
			else if(x < as[i]) x = as[i], ans ~= "L", i += 1;
			else break;
		}
	}

	ans.length.writeln;
	ans.writeln;
}

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

string FMT_F = "%.10f";

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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

long mod = 10^^9 + 7;
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 N = RD;
	auto a = RDR.ARR;

	string ans;
	long last;
	while (!a.empty)
	{
		if (a.front > last && a.front < a.back)
		{
			last = a.front;
			a.popFront;
			ans ~= "L";
		}
		else if (a.back > last && a.back < a.front)
		{
			last = a.back;
			a.popBack;
			ans ~= "R";
		}
		else if (a.front > last)
		{
			last = a.front;
			a.popFront;
			ans ~= "L";
		}
		else if (a.back > last)
		{
			last = a.back;
			a.popBack;
			ans ~= "R";
		}
		else
		{
			break;
		}
	}

	writeln(ans.length);
	writeln(ans);
	stdout.flush();
	debug readln();
}


You are given an array of integers $$$a_1,a_2,\ldots,a_n$$$. Find the maximum possible value of $$$a_ia_ja_ka_la_t$$$ among all five indices $$$(i, j, k, l, t)$$$ ($$$i<j<k<l<t$$$).

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\le t\le 2 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$5\le n\le 10^5$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-3\times 10^3\le a_i\le 3\times 10^3$$$) — given array. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, print one integer — the answer to the problem.

NoteIn the first test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)\cdot (-2) \cdot (-3)\cdot (-4)\cdot (-5)=-120$$$.In the second test case, choosing $$$a_1,a_2,a_3,a_5,a_6$$$ is a best choice: $$$(-1)\cdot (-2) \cdot (-3)\cdot 2\cdot (-1)=12$$$.In the third test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)\cdot 0\cdot 0\cdot 0\cdot (-1)=0$$$.In the fourth test case, choosing $$$a_1,a_2,a_3,a_4,a_6$$$ is a best choice: $$$(-9)\cdot (-7) \cdot (-5)\cdot (-3)\cdot 1=945$$$.

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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void gen(){
}
void jury(){
}
void solve(){
    foreach(_; 0 .. scan!int){
        int n = scan!int;
        long[] as = scan!long(n);

        long[] xs, ys, zs; // +, -, 0
        foreach(a; as) if(a > 0) xs ~= a; else if(a < 0) ys ~= a; else zs ~= a;

        xs.sort();
        ys.sort();

        long ans = as[0] * as[1] * as[2] * as[3] * as[4];

        if(xs.length >= 5) ans.raiseTo(xs[$ - 1] * xs[$ - 2] * xs[$ - 3] * xs[$ - 4] * xs[$ - 5]);
        if(xs.length >= 4 && ys.length >= 1) ans.raiseTo(xs[0] * xs[1] * xs[2] * xs[3] * ys[$ - 1]);
        if(xs.length >= 3 && ys.length >= 2) ans.raiseTo(xs[$ - 1] * xs[$ - 2] * xs[$ - 3] * ys[0] * ys[1]);
        if(xs.length >= 2 && ys.length >= 3) ans.raiseTo(xs[0] * xs[1] * ys[$ - 1] * ys[$ - 2] * ys[$ - 3]);
        if(xs.length >= 1 && ys.length >= 4) ans.raiseTo(xs[$ - 1] * ys[0] * ys[1] * ys[2] * ys[3]);
        if(ys.length >= 5) ans.raiseTo(ys[$ - 1] * ys[$ - 2] * ys[$ - 3] * ys[$ - 4] * ys[$ - 5]);

        if(zs.length >= 1) ans.raiseTo(0);

        ans.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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		a.sort();

		bool hasPlus;
		foreach (i; 0..n)
		{
			if (a[i] >= 0)
				hasPlus = true;
		}

		if (!hasPlus || n == 5)
		{
			ans[ti] = 1;
			foreach (i; 0..5)
			{
				ans[ti] *= a[n-i-1];
			}
		}
		else
		{
			long best = long.min;
			foreach (i; 0..3)
			{
				auto cnt = i * 2;
				long x = 1;
				foreach (j; 0..cnt)
				{
					if (a[j] >= 0)
					{
						x = long.min;
						break;
					}

					x *= a[j];
				}
				if (x == long.min) continue;

				foreach (j; 0..5-cnt)
				{
					if (a[n-j-1] < 0)
					{
						x = long.min;
						break;
					}

					x *= a[n-j-1];
				}
				best.chmax(x);
			}
			ans[ti] = best;
		}
	}

	foreach (e; ans)
	{
		writeln(e);
	}
	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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		a.sort();

		bool hasPlus;
		foreach (i; 0..n)
		{
			if (a[i] >= 0)
				hasPlus = true;
		}

		if (!hasPlus)
		{
			ans[ti] = 1;
			foreach_reverse (i; 0..5)
			{
				ans[ti] *= a[i];
			}
		}
		else
		{
			long best = long.min;
			foreach (i; 0..3)
			{
				auto cnt = i * 2;
				long x = 1;
				foreach (j; 0..cnt)
				{
					if (a[j] >= 0)
					{
						x = long.min;
						break;
					}

					x *= a[j];
				}
				if (x == long.min) continue;

				foreach (j; 0..5-cnt)
				{
					if (a[n-j-1] < 0)
					{
						x = long.min;
						break;
					}

					x *= a[n-j-1];
				}
				best.chmax(x);
			}
			ans[ti] = best;
		}
	}

	foreach (e; ans)
	{
		writeln(e);
	}
	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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		a.sort();

		bool hasPlus;
		foreach (i; 0..n)
		{
			if (a[i] >= 0)
				hasPlus = true;
		}

		if (!hasPlus || n == 5)
		{
			ans[ti] = 1;
			foreach_reverse (i; 0..5)
			{
				ans[ti] *= a[i];
			}
		}
		else
		{
			long best = long.min;
			foreach (i; 0..3)
			{
				auto cnt = i * 2;
				long x = 1;
				foreach (j; 0..cnt)
				{
					if (a[j] >= 0)
					{
						x = long.min;
						break;
					}

					x *= a[j];
				}
				if (x == long.min) continue;

				foreach (j; 0..5-cnt)
				{
					if (a[n-j-1] < 0)
					{
						x = long.min;
						break;
					}

					x *= a[n-j-1];
				}
				best.chmax(x);
			}
			ans[ti] = best;
		}
	}

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


There are $$$n$$$ children, who study at the school №41. It is well-known that they are good mathematicians. Once at a break, they arranged a challenge for themselves. All children arranged in a row and turned heads either to the left or to the right.Children can do the following: in one second several pairs of neighboring children who are looking at each other can simultaneously turn the head in the opposite direction. For instance, the one who was looking at the right neighbor turns left and vice versa for the second child. Moreover, every second at least one pair of neighboring children performs such action. They are going to finish when there is no pair of neighboring children who are looking at each other. You are given the number $$$n$$$, the initial arrangement of children and the number $$$k$$$. You have to find a way for the children to act if they want to finish the process in exactly $$$k$$$ seconds. More formally, for each of the $$$k$$$ moves, you need to output the numbers of the children who turn left during this move.For instance, for the configuration shown below and $$$k = 2$$$ children can do the following steps:    At the beginning, two pairs make move: $$$(1, 2)$$$ and $$$(3, 4)$$$. After that, we receive the following configuration:    At the second move pair $$$(2, 3)$$$ makes the move. The final configuration is reached. Good job.   It is guaranteed that if the solution exists, it takes not more than $$$n^2$$$ "headturns".

The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 3000$$$, $$$1 \le k \le 3000000$$$)  — the number of children and required number of moves. The next line contains a string of length $$$n$$$ and consists only of characters L and R, where L means that the child looks to the left and R means that the child looks to the right.

If there is no solution, print a single line with number $$$-1$$$. Otherwise, output $$$k$$$ lines. Each line has to start with a number $$$n_i$$$ ($$$1\le n_i \le \frac{n}{2}$$$)  — the number of pairs of children, who turn at this move. After that print $$$n_i$$$ distinct integers  — the numbers of the children who will turn left during this move.  After performing all "headturns", there can't be a pair of two neighboring children looking at each other. If there are many solutions, print any of them.

NoteThe first sample contains a pair of children who look at each other. After one move, they can finish the process.In the second sample, children can't make any move. As a result, they can't end in $$$k>0$$$ moves.The third configuration is described in the statement.

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

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto s = readln.strip.map !(c => c == 'R').array;
		int [] [] ans;
		int total;
		while (true)
		{
			int [] cur;
			for (int i = 0; i + 1 < n; i++)
			{
				if (s[i] && !s[i + 1])
				{
					s[i] ^= true;
					s[i + 1] ^= true;
					cur ~= i + 1;
					i += 1;
				}
			}
			if (cur.empty)
			{
				break;
			}
			ans ~= cur;
			total += cur.length.to !(int);
		}

		if (total < k || k < ans.length)
		{
			writeln (-1);
			continue;
		}
		int [] [] ans2;
		while (ans.length + ans2.length != k)
		{
			ans2 ~= [ans.back.back];
			ans.back.length -= 1;
			if (ans.back.length == 0)
			{
				ans.length -= 1;
			}
		}
		foreach (line; chain (ans, ans2.retro))
		{
			writefln !("%s%( %s%)") (line.length, line);
		}
	}
}

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

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto s = readln.strip.map !(c => c == 'R').array;
		int [] [] ans;
		int total;
		while (true)
		{
			int [] cur;
			foreach (i; 0..n - 1)
			{
				if (s[i] && !s[i + 1])
				{
					s[i] ^= true;
					s[i + 1] ^= true;
					cur ~= i + 1;
				}
			}
			if (cur.empty)
			{
				break;
			}
			ans ~= cur;
			total += cur.length.to !(int);
		}

		if (total < k || k < ans.length)
		{
			writeln (-1);
			continue;
		}
		int [] [] ans2;
		while (ans.length + ans2.length != k)
		{
			ans2 ~= [ans.back.back];
			ans.back.length -= 1;
			if (ans.back.length == 0)
			{
				ans.length -= 1;
			}
		}
		foreach (line; chain (ans, ans2.retro))
		{
			writefln !("%s%( %s%)") (line.length, line);
		}
	}
}


To improve the boomerang throwing skills of the animals, Zookeeper has set up an $$$n \times n$$$ grid with some targets, where each row and each column has at most $$$2$$$ targets each. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$n$$$ from left to right.  For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a $$$90$$$ degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid.   In the above example, $$$n=6$$$ and the black crosses are the targets. The boomerang in column $$$1$$$ (blue arrows) bounces $$$2$$$ times while the boomerang in column $$$3$$$ (red arrows) bounces $$$3$$$ times. The boomerang in column $$$i$$$ hits exactly $$$a_i$$$ targets before flying out of the grid. It is known that $$$a_i \leq 3$$$.However, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them.

The first line contains a single integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$.  The next line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(0 \leq a_i \leq 3)$$$.

If no configuration of targets exist, print $$$-1$$$.  Otherwise, on the first line print a single integer $$$t$$$ $$$(0 \leq t \leq 2n)$$$: the number of targets in your configuration.   Then print $$$t$$$ lines with two spaced integers each per line. Each line should contain two integers $$$r$$$ and $$$c$$$ $$$(1 \leq r,c \leq n)$$$, where $$$r$$$ is the target's row and $$$c$$$ is the target's column. All targets should be different.   Every row and every column in your configuration should have at most two targets each.

NoteFor the first test, the answer configuration is the same as in the picture from the statement. For the second test, the boomerang is not supposed to hit anything, so we can place $$$0$$$ targets. For the third test, the following configuration of targets matches the number of hits, but is not allowed as row $$$3$$$ has $$$4$$$ targets.   It can be shown for this test case that no valid configuration of targets will result in the given number of target hits.

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

alias Coord = Tuple !(int, q{row}, int, q{col});
immutable int NA = -1;
immutable Coord bad = Coord (NA, NA);

Coord [] solve (int n, int [] a)
{
	auto depFrom = new int [n];
	depFrom[] = NA;
	auto depTo = new int [n];
	depTo[] = NA;

	int [] ones;
	foreach_reverse (i; 0..n)
	{
		if (a[i] == 2)
		{
			if (ones.empty)
			{
				return [bad];
			}
			depFrom[i] = ones.back;
			depTo[ones.back] = i;
			ones.popBack ();
			ones.assumeSafeAppend ();
		}
		if (a[i] == 1)
		{
			ones ~= i;
		}
	}

	int [] positives;
	foreach_reverse (i; 0..n)
	{
		if (a[i] == 3)
		{
			if (positives.empty)
			{
				return [bad];
			}
			depFrom[i] = positives.back;
			depTo[positives.back] = i;
			positives.popBack ();
			positives.assumeSafeAppend ();
		}
		if (a[i] > 0 && depTo[i] == NA)
		{
			positives ~= i;
		}
	}

	Coord [] answer;
	foreach (i; 0..n)
	{
		if (a[i] == 0)
		{
		}
		else if (a[i] == 1)
		{
			if (depTo[i] == NA)
			{
				answer ~= Coord (i, i);
			}
		}
		else if (a[i] == 2)
		{
			if (depTo[i] == NA)
			{
				answer ~= Coord (i, i);
			}
			answer ~= Coord (i, depFrom[i]);
		}
		else if (a[i] == 3)
		{
			if (depTo[i] == NA)
			{
				answer ~= Coord (i, i);
			}
			answer ~= Coord (i, depFrom[i]);
			answer ~= Coord (depFrom[i], depFrom[i]);
		}
	}
	return answer;
}

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto answer = solve (n, a);
		if (answer == [bad])
		{
			writeln (-1);
		}
		else
		{
			writeln (answer.length);
			foreach (ref c; answer)
			{
				writeln (c.row + 1, " ", c.col + 1);
			}
		}
	}
}


You are given a set Y of n distinct positive integers y1, y2, ..., yn.Set X of n distinct positive integers x1, x2, ..., xn is said to generate set Y if one can transform X to Y by applying some number of the following two operation to integers in X:  Take any integer xi and multiply it by two, i.e. replace xi with 2·xi.  Take any integer xi, multiply it by two and add one, i.e. replace xi with 2·xi + 1. Note that integers in X are not required to be distinct after each operation.Two sets of distinct integers X and Y are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.Note, that any set of integers (or its permutation) generates itself.You are given a set Y and have to find a set X that generates Y and the maximum element of X is mininum possible.

The first line of the input contains a single integer n (1 ≤ n ≤ 50 000) — the number of elements in Y. The second line contains n integers y1, ..., yn (1 ≤ yi ≤ 109), that are guaranteed to be distinct.

Print n integers — set of distinct integers that generate Y and the maximum element of which is minimum possible. If there are several such sets, print any of them.

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		auto q=rbt!(true,int);
		foreach(i;0..n)
		{
			int k;
			input(&k);
			q.insert(k);
		}
		for(;;)
		{
			int t=q.back;
			int d;
			for(int i=2;i<=t;i*=2)
			{
				if(q.equalRange(t/i).empty){
					d=t/i;
					break;
				}
			}
			if(d==0)break;
			q.removeKey(t);
			q.insert(d);
		}
		putarr(q);
		writeln;
	}
	debug system("pause");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		auto r = redBlackTree !(q{a > b}, int) ();
		foreach (c; a)
		{
			r.insert (c);
		}
		while (true)
		{
			auto v = r.front ();
			auto w = v;
			while (w > 1)
			{
				w /= 2;
				if (w !in r)
				{
					break;
				}
			}
			if (w !in r)
			{
				r.removeKey (v);
				r.insert (w);
			}
			else
			{
				break;
			}
		}
		writefln ("%(%s %)", r[]);
	}
}

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 n = next!int;
  auto y = next!int(n);
  int[] can(int max)
  {
    auto used = redBlackTree!int();
    auto res = new int[](n);
    foreach(i, yi; y)
      {
	while(yi > max)
	  yi >>= 1;
	while(yi && yi in used)
	  yi >>= 1;
	if (yi == 0) return null;
	used.insert(yi);
	res[i] = yi;
      }
    return res;
  }
  int low = 1;
  int high = y.fold!max;
  int[] lowestCan = null;
  while(low <= high)
    {
      int mid = (low + high) / 2;
      if (auto res = can(mid))
	{
	  lowestCan = res;
	  high = mid - 1;
	}
      else
	{
	  low = mid + 1;
	}
    }
  foreach(yi; lowestCan)
    write(yi, " ");
  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$$$ integers. You want to distribute these $$$n$$$ integers into two groups $$$s_1$$$ and $$$s_2$$$ (groups can be empty) so that the following conditions are satisfied:  For each $$$i$$$ $$$(1 \leq i \leq n)$$$, $$$a_i$$$ goes into exactly one group. The value $$$|sum(s_1)| - |sum(s_2)|$$$ is the maximum possible among all such ways to distribute the integers.Here $$$sum(s_1)$$$ denotes the sum of the numbers in the group $$$s_1$$$, and $$$sum(s_2)$$$ denotes the sum of the numbers in the group $$$s_2$$$.Determine the maximum possible value of $$$|sum(s_1)| - |sum(s_2)|$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 2 \cdot 10^4)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$  — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2 \ldots a_n$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$  — elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, output a single integer  — the maximum possible value of $$$|sum(s_1)| - |sum(s_2)|$$$.

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.functional;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

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 solve() {
    int n = read!int;
    auto a = readarray!long;
    long plus, minus;
    foreach(e; a)
        if (e > 0)
            plus += e;
        else
            minus += e;
    writeln(abs(plus - abs(minus)));
}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

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()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!long;
    auto a = readln.splitter.map!(to!long).array.sort.array;
    long totalsum = a.sum;
    long xsum = 0;
    long ans = 0;
    foreach (x ; a) {
      xsum += x;
      ans = max(ans, abs(abs(xsum) - abs(totalsum - xsum)));
    }
    writeln(ans);
  }
}


You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.

The first line contains two integers nA, nB (1 ≤ nA, nB ≤ 105), separated by a space — the sizes of arrays A and B, correspondingly. The second line contains two integers k and m (1 ≤ k ≤ nA, 1 ≤ m ≤ nB), separated by a space. The third line contains nA numbers a1, a2, ... anA ( - 109 ≤ a1 ≤ a2 ≤ ... ≤ anA ≤ 109), separated by spaces — elements of array A. The fourth line contains nB integers b1, b2, ... bnB ( - 109 ≤ b1 ≤ b2 ≤ ... ≤ bnB ≤ 109), separated by spaces — elements of array B.

Print "YES" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in array A was strictly less than any number chosen in array B. Otherwise, print "NO" (without the quotes).

NoteIn the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).In the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B: .

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int na = cin.readInt;
                int nb = cin.readInt;
                int a = cin.readInt;
                int b = cin.readInt;
                int[] arr1 = new int[na];
                int[] arr2 = new int[nb];
                foreach (ref i; arr1) i = cin.readInt;
                foreach (ref i; arr2) i = cin.readInt;
                bool possible = true;
                for (int i = 0; i < a; i++) if (arr1[i] >= arr2[nb - b]) possible = false;
                writeln(possible ? "YES" : "NO");
        }
}

module main;
import std.c.stdio;

int main() {
  int na, nb;
  scanf("%d %d", &na, &nb);
  int k, m;
  scanf("%d %d", &k, &m);
  int a[100005];
  int b[100005];
  for (int i = 0; i < na; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < nb; i++) {
    scanf("%d", &b[i]);
  }
  if (a[k - 1] < b[nb - m]) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
  return 0;
}

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

void main() {
    readln;
    int k, m;
    readf("%d %d\n", &k, &m);
    int[] a = readln.chomp.split.map!(to!int).array;
    int[] b = readln.chomp.split.map!(to!int).array;

    if (a[k - 1] < b[$ - m]) {
        "YES".writeln;
    } else {
        "NO".writeln;
    }
}

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

void main() {
    readln;
    int k, m;
    readf("%d %d\n", &k, &m);
    int[] a = readln.chomp.split.map!(to!int).array;
    int[] b = readln.chomp.split.map!(to!int).array;

    if (a[k - 1] < b[0]) {
        "YES".writeln;
    } else {
        "NO".writeln;
    }
}


Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle $$$a\times b$$$ which is divided into $$$ab$$$ cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible.Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this.

The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 100$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1\leq a, b\leq 100$$$), representing a corresponding test case.

For each test case print the single integer on a separate line — the answer to the problem.

NoteSome possible escape plans for the example test cases are shown below. Broken walls are shown in gray, not broken walls are shown in black.

import std.stdio;

void main() {
    int t; readf(" %d",&t);
    while (t--) {
        int a,b; readf(" %d %d",&a,&b);
        writeln(a*b);
    }
}


Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b.Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them.Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.

The first line contains integer n (1 ≤ n ≤ 200 000) — the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≤ ai ≤ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line.

If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them.

NoteIn the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result.In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main ()
{
	auto d = readln.strip.to !(int).iota.array;
	auto a = readln.split.map !(to !(int)).array;
	a[] += d[];
	sort (a);
	a[] -= d[];
	writeln (isSorted (a) ? a.map !(text).join (" ") : ":(");
}

import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main ()
{
	auto d = readln.strip.to !(int).iota.array;
	auto a = readln.split.map !(to !(int)).array;
	a[] += d[];
	sort (a);
	a[] -= d[];
	if (!isSorted (a))
	{
		writeln (":(");
	}
	else
	{
		writefln ("%(%s %)", a);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		a[] -= n.iota.retro.array[];
		if (!equal (a, a.uniq))
		{
			writeln (":(");
		}
		else
		{
			sort !(q{a < b}, SwapStrategy.stable) (a);
			a[] += n.iota.retro.array[];
			if (a.canFind !(q{a < 0}))
			{
				writeln (":(");
			}
			else if (!isSorted !(q{a < b}) (a))
			{
				writeln (":(");
			}
			else
			{
				writefln ("%(%s %)", a);
			}
		}
	}
}

import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main ()
{
	readln;
	auto a = readln.split.map !(to !(uint)).array;
	a[] -= a.length.iota.retro.array[];
	sort (a);
	a[] += a.length.iota.retro.array[];
	if (!isSorted (a))
	{
		writeln (":(");
	}
	else
	{
		writefln ("%(%s %)", a);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		a[] -= n.iota.retro.array[];
		if (!equal (a, a.uniq))
		{
			writeln (":(");
		}
		else
		{
			sort !(q{a < b}, SwapStrategy.stable) (a);
			a[] += n.iota.retro.array[];
			writefln ("%(%s %)", a);
		}
	}
}

import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main ()
{
	readln;
	auto a = readln.split.map !(to !(uint)).array;
	a[] -= a.length.iota.retro.array[];
	sort (a);
	a[] += a.length.iota.retro.array[];
	if (!equal (a, a.uniq) || !isSorted (a))
	{
		writeln (":(");
	}
	else
	{
		writefln ("%(%s %)", a);
	}
}


Luke likes to eat. There are $$$n$$$ piles of food aligned in a straight line in front of him. The $$$i$$$-th pile contains $$$a_i$$$ units of food. Luke will walk from the $$$1$$$-st pile towards the $$$n$$$-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the $$$i$$$-th pile, he can eat that pile if and only if $$$|v - a_i| \leq x$$$, where $$$x$$$ is a fixed integer, and $$$v$$$ is Luke's food affinity.Before Luke starts to walk, he can set $$$v$$$ to any integer. Also, for each $$$i$$$ ($$$1 \leq i \leq n$$$), Luke can change his food affinity to any integer before he eats the $$$i$$$-th pile.Find the minimum number of changes needed to eat every pile of food.Note that the initial choice for $$$v$$$ is not considered as a change.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. For each test case, the first line contains two integers, $$$n, x$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq x \leq 10^9$$$) — the number of piles, and the maximum difference between the size of a pile and Luke's food affinity, such that Luke can eat the pile. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots , a_n$$$ ($$$1 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output an integer on a separate line, which is the minimum number of changes needed.

NoteIn the first test case, Luke can set $$$v$$$ to $$$5$$$ before he starts to walk. And he can walk straight to eat every piles of food without changing $$$v$$$.In the second test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$10$$$ before he eats the second pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.In the fourth test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$8$$$ before he eats the sixth pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.In the fifth test case, Luke can set $$$v$$$ to $$$4$$$ before he starts to walk. And he could change $$$v$$$ to $$$6$$$ before he eats the fourth pile. Then he could change $$$v$$$ to $$$12$$$ before he eats the seventh pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.

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)
	{
		int n, x;
		readf !(" %s %s") (n, x);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int lo = a.front;
		int hi = a.front;
		int res = 0;
		foreach (ref c; a)
		{
			lo = min (lo, c);
			hi = max (hi, c);
			if (hi - lo > x * 2)
			{
				res += 1;
				lo = c;
				hi = c;
			}
		}
		writeln (res);
	}
}

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)); }

enum INF = 10L^^18;

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

        int ans;
        long lb = -INF, ub = INF;
        foreach (i; 0 .. N) {
          const llb = max(lb, A[i] - X);
          const uub = min(ub, A[i] + X);
          if (llb <= uub) {
            lb = llb;
            ub = uub;
          } else {
            ++ans;
            lb = A[i] - X;
            ub = A[i] + X;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


Alexandra has an even-length array $$$a$$$, consisting of $$$0$$$s and $$$1$$$s. The elements of the array are enumerated from $$$1$$$ to $$$n$$$. She wants to remove at most $$$\frac{n}{2}$$$ elements (where $$$n$$$ — length of array) in the way that alternating sum of the array will be equal $$$0$$$ (i.e. $$$a_1 - a_2 + a_3 - a_4 + \dotsc = 0$$$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.For example, if she has $$$a = [1, 0, 1, 0, 0, 0]$$$ and she removes $$$2$$$nd and $$$4$$$th elements, $$$a$$$ will become equal $$$[1, 1, 0, 0]$$$ and its alternating sum is $$$1 - 1 + 0 - 0 = 0$$$.Help her!

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

For each test case, firstly, print $$$k$$$ ($$$\frac{n}{2} \leq k \leq n$$$) — number of elements that will remain after removing in the order they appear in $$$a$$$. Then, print this $$$k$$$ numbers. Note that you should print the numbers themselves, not their indices. We can show that an answer always exists. If there are several answers, you can output any of them.

NoteIn the first and second cases, alternating sum of the array, obviously, equals $$$0$$$.In the third case, alternating sum of the array equals $$$1 - 1 = 0$$$.In the fourth case, alternating sum already equals $$$1 - 1 + 0 - 0 = 0$$$, so we don't have to remove anything.

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;

alias ll = long;
alias to!int isz;
alias rbt = redBlackTree;
static string[] token;
T rd(T = long)() { while(!token.length) token = readln.chomp.split; string res = token[0]; token.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
/*******************************It's a Me, Mario!**********************************************/

void play(){
    int n;
    n = rd!int;
    ll[] arr = rdarr;
    ll[] res;
    ll cnt0 = 0, cnt1 = 0;
    foreach(el; arr){
        cnt0 += (el == 0);
        cnt1 += (el == 1);
    }
    ll hf = n / 2;
    ll put = hf + (hf % 2);
    if(cnt1 > hf || (hf % 2 == 0 && cnt1 == hf)){
        writeln(put);
        foreach(i; 0..put){
            write(1, " ");
        }
    }else if(cnt0 >= hf){
        writeln(cnt0);
        foreach(i; 0..cnt0){
            write(0, " ");
        }
    }
    writeln;
}

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

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;

void main(string[] args)
{
  inputFile = stdin;
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto a = next!int(n);
      int[2] cnt;
      foreach(ai; a) cnt[ai]++;
      if (cnt[0] >= cnt[1])
	{
	  writeln(cnt[0]);
	  foreach(i; 0 .. cnt[0]) write("0 ");
	  writeln;
	}
      else
	{
	  cnt[1] -= cnt[1] % 2;
	  writeln(cnt[1]);
	  foreach(i; 0 .. cnt[1]) write("1 ");
	  writeln;
	}
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  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.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
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;
    }
}

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 n = RD!int;
		auto a = RDA;

		long cnt0, cnt1;
		foreach (i; 0..n)
		{
			if (a[i] == 0)
				++cnt0;
			else
				++cnt1;
		}
		if (cnt0 >= cnt1)
		{
			foreach (i; 0..cnt0)
				ans[ti] ~= 0;
		}
		else
		{
			if ((n/2) % 2)
			{
				foreach (i; 0..n/2+1)
					ans[ti] ~= 1;
			}
			else
			{
				foreach (i; 0..n/2)
					ans[ti] ~= 1;
			}
		}
	}

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

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;

alias ll = long;
alias to!int isz;
alias rbt = redBlackTree;
static string[] token;
T rd(T = long)() { while(!token.length) token = readln.chomp.split; string res = token[0]; token.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
/*******************************It's a Me, Mario!**********************************************/

void play(){
    int n;
    n = rd!int;
    ll[] arr = rdarr;
    ll[] res;
    ll cnt0 = 0, cnt1 = 0;
    foreach(el; arr){
        cnt0 += (el == 0);
        cnt1 += (el == 1);
    }
    ll put = (n/2) + (n % 2);
    ll hf = n / 2;
    if(cnt1 > hf || (hf % 2 == 0 && cnt1 == hf)){
        writeln(put);
        foreach(i; 0..put){
            write(1, " ");
        }
    }else if(cnt0 >= hf){
        writeln(cnt0);
        foreach(i; 0..cnt0){
            write(0, " ");
        }
    }
    writeln;
}

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

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 n = RD!int;
		auto a = RDA;

		bool hasZero;
		foreach (i; 0..n)
		{
			if (a[i] == 0)
				hasZero = true;
		}
		if (hasZero)
			ans[ti] = [0];
		else
			ans[ti] = [1, 1];
	}

	foreach (e; ans)
	{
		writeln(e.length);
		e.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 long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		long cnt0, cnt1;
		foreach (i; 0..n)
		{
			if (a[i] == 0)
				++cnt0;
			else
				++cnt1;
		}
		if (cnt0 >= cnt1)
		{
			foreach (i; 0..n/2)
				ans[ti] ~= 0;
		}
		else
		{
			foreach (i; 0..n/2)
				ans[ti] ~= 1;
		}
	}

	foreach (e; ans)
	{
		writeln(e.length);
		e.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 long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		long cnt0, cnt1;
		foreach (i; 0..n)
		{
			if (a[i] == 0)
				++cnt0;
			else
				++cnt1;
		}
		if (cnt0 > cnt1)
		{
			foreach (i; 0..(n+2)/2)
				ans[ti] ~= 0;
		}
		else
		{
			if ((n/2) % 2)
			{
				foreach (i; 0..n/2-1)
					ans[ti] ~= 1;
				if (cnt0 == 0)
					ans[ti] ~= [1, 1];
				else
					ans[ti] ~= 0;
			}
			else
			{
				foreach (i; 0..n/2)
					ans[ti] ~= 1;
			}
		}
	}

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