You are given a permutation $$$p_1, p_2, \ldots, p_n$$$.In one move you can swap two adjacent values.You want to perform a minimum number of moves, such that in the end there will exist a subsegment $$$1,2,\ldots, k$$$, in other words in the end there should be an integer $$$i$$$, $$$1 \leq i \leq n-k+1$$$ such that $$$p_i = 1, p_{i+1} = 2, \ldots, p_{i+k-1}=k$$$.Let $$$f(k)$$$ be the minimum number of moves that you need to make a subsegment with values $$$1,2,\ldots,k$$$ appear in the permutation.You need to find $$$f(1), f(2), \ldots, f(n)$$$.

The first line of input contains one integer $$$n$$$ ($$$1 \leq n \leq 200\,000$$$): the number of elements in the permutation. The next line of input contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$: given permutation ($$$1 \leq p_i \leq n$$$).

Print $$$n$$$ integers, the minimum number of moves that you need to make a subsegment with values $$$1,2,\ldots,k$$$ appear in the permutation, for $$$k=1, 2, \ldots, n$$$.

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, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(x => x.to!int-1).array;

    auto B = new int[](N);
    foreach (i, a; A) B[a] = i.to!int;

    auto st = new SegmentTree!(long, (a,b)=>a+b, 0L)(N);
    st.add(B[0], 1);

    auto leftpq = new BinaryHeap!(Array!long, "a < b");
    auto rightpq = new BinaryHeap!(Array!long, "a > b");
    leftpq.insert(B[0]);

    long leftsum = B[0];
    long rightsum = 0;
    long inversion = 0;

    write(0);

    foreach (a; 1..N) {
        auto left = B[a] == 0 ? 0 : st.query(0, B[a] - 1);
        auto right = B[a] == N - 1 ? 0 : st.query(B[a] + 1, N - 1);
        inversion += right;

        if (leftpq.length == rightpq.length) {
            leftpq.insert(B[a]);
            leftsum += B[a];
        } else {
            rightpq.insert(B[a]);
            rightsum += B[a];
        }

        while (leftpq.front > rightpq.front) {
            auto l = leftpq.front;
            auto r = rightpq.front;
            leftpq.removeFront;
            rightpq.removeFront;
            leftsum -= l;
            rightsum -= r;
            leftpq.insert(r);
            rightpq.insert(l);
            leftsum += r;
            rightsum += l;
        }

        auto med = leftpq.front;
        auto n = leftpq.length.to!long - 1;
        auto m = rightpq.length.to!long;

        long ans = inversion + (med * n - (leftsum - med) - n - n * (n - 1) / 2) + (rightsum - med * m - m - m * (m - 1) / 2);

        write(" ", ans);

        st.add(B[a], 1);
    }

    writeln;
}

class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void assign(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    void add(int pos, T val) {
        pos += offset;
        table[pos] += val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}

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 bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}

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

      auto Q = new int[N];
      foreach (i; 0 .. N) {
        Q[P[i]] = i;
      }

      auto ans = new long[N];

      // move
      int cntL, cntR;
      long sumL, sumR;
      auto setL = new RedBlackTree!int;
      auto setR = new RedBlackTree!int;
      foreach (j; 0 .. N) {
        if (setR.empty || Q[j] < setR.front) {
          cntL += 1;
          sumL += Q[j];
          setL.insert(Q[j]);
        } else {
          cntR += 1;
          sumR += Q[j];
          setR.insert(Q[j]);
        }
        for (; cntL > (j + 1) / 2; ) {
          const i = setL.back;
          cntL -= 1;
          sumL -= i;
          setL.removeKey(i);
          cntR += 1;
          sumR += i;
          setR.insert(i);
        }
        for (; cntL < (j + 1) / 2; ) {
          const i = setR.front;
          cntR -= 1;
          sumR -= i;
          setR.removeKey(i);
          cntL += 1;
          sumL += i;
          setL.insert(i);
        }
        debug {
          writeln("j = ", j);
          writeln("  L: ", cntL, " ", sumL, " ", setL);
          writeln("  R: ", cntR, " ", sumR, " ", setR);
        }
        const long i0 = setR.front;
        debug {
          writeln("  cost L: ", i0 * (i0 - 1) / 2 - (i0 - cntL) * (i0 - cntL - 1) / 2, " - ", sumL);
          writeln("  cost R: ", sumR, " - ", (i0 + cntR) * (i0 + cntR - 1) / 2 - i0 * (i0 - 1) / 2);
        }
        ans[j] += i0 * (i0 - 1) / 2 - (i0 - cntL) * (i0 - cntL - 1) / 2;
        ans[j] -= sumL;
        ans[j] += sumR;
        ans[j] -= (i0 + cntR) * (i0 + cntR - 1) / 2 - i0 * (i0 - 1) / 2;
      }
      debug {
        writeln("ans (move) = ", ans);
      }

      // inversion
      auto bit = new long[N];
      long now;
      foreach (j; 0 .. N) {
        now += j - bit.bSum(Q[j]);
        ans[j] += now;
        bit.bAdd(Q[j], 1);
      }

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

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, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(x => x.to!int-1).array;

    auto B = new int[](N);
    foreach (i, a; A) B[a] = i.to!int;

    auto st = new SegmentTree!(long, (a,b)=>a+b, 0L)(N);
    st.add(B[0], 1);

    int leftmost = B[0];
    int rightmost = B[0];
    long move = 0;

    long ans = 0;
    write(ans);

    foreach (a; 1..N) {
        auto left = B[a] == 0 ? 0 : st.query(0, B[a] - 1);
        auto right = B[a] == N - 1 ? 0 : st.query(B[a] + 1, N - 1);
        ans += right;

        if (B[a] < leftmost) {
            ans += 1L * (leftmost - B[a] - 1) * a;
        } else {
            ans += B[a] - leftmost - left;
            ans -= right;
        }

        write(" ", ans);

        st.add(B[a], 1);
        leftmost = min(leftmost, B[a]);
        rightmost = max(rightmost, B[a]);
    }

    writeln;
}

class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void assign(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    void add(int pos, T val) {
        pos += offset;
        table[pos] += val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}


Shohag has an integer sequence $$$a_1, a_2, \ldots, a_n$$$. He can perform the following operation any number of times (possibly, zero):  Select any positive integer $$$k$$$ (it can be different in different operations).  Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert $$$k$$$ into the sequence at this position.  This way, the sequence $$$a$$$ changes, and the next operation is performed on this changed sequence. For example, if $$$a=[3,3,4]$$$ and he selects $$$k = 2$$$, then after the operation he can obtain one of the sequences $$$[\underline{2},3,3,4]$$$, $$$[3,\underline{2},3,4]$$$, $$$[3,3,\underline{2},4]$$$, or $$$[3,3,4,\underline{2}]$$$.Shohag wants this sequence to satisfy the following condition: for each $$$1 \le i \le |a|$$$, $$$a_i \le i$$$. Here, $$$|a|$$$ denotes the size of $$$a$$$.Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it's always possible to achieve this goal in a finite number of operations.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the initial length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the sequence.

For each test case, print a single integer  — the minimum number of operations needed to perform to achieve the goal mentioned in the statement.

NoteIn the first test case, we have to perform at least one operation, as $$$a_2=3>2$$$. We can perform the operation $$$[1, 3, 4] \rightarrow [1, \underline{2}, 3, 4]$$$ (the newly inserted element is underlined), now the condition is satisfied.In the second test case, Shohag can perform the following operations:$$$[1, 2, 5, 7, 4] \rightarrow [1, 2, \underline{3}, 5, 7, 4] \rightarrow [1, 2, 3, \underline{4}, 5, 7, 4] \rightarrow [1, 2, 3, 4, 5, \underline{3}, 7, 4]$$$.In the third test case, the sequence already satisfies the condition.

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        // read through sequence, get max diff
        int diff = 0;
        for (int j = 0; j < n; j++) {
            int i = j+1; // array index
            int d = a[j] - i;
            if (d > diff) {
                diff = d;
            }
        }

        writeln(diff);
    }
}

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        ulong n;
        readf!"%d\n"(n);

        ulong[] a = new ulong[cast(uint) n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        // read through sequence, get max diff
        ulong diff = 0;
        for (int j = 0; j < n; j++) {
            int i = j+1; // array index
            ulong d = a[j] - i;
            if (d > diff) {
                diff = d;
            }
        }

        writeln(diff);
    }
}


Long time ago there was a symmetric array $$$a_1,a_2,\ldots,a_{2n}$$$ consisting of $$$2n$$$ distinct integers. Array $$$a_1,a_2,\ldots,a_{2n}$$$ is called symmetric if for each integer $$$1 \le i \le 2n$$$, there exists an integer $$$1 \le j \le 2n$$$ such that $$$a_i = -a_j$$$.For each integer $$$1 \le i \le 2n$$$, Nezzar wrote down an integer $$$d_i$$$ equal to the sum of absolute differences from $$$a_i$$$ to all integers in $$$a$$$, i. e. $$$d_i = \sum_{j = 1}^{2n} {|a_i - a_j|}$$$.Now a million years has passed and Nezzar can barely remember the array $$$d$$$ and totally forget $$$a$$$. Nezzar wonders if there exists any symmetric array $$$a$$$ consisting of $$$2n$$$ distinct integers that generates the array $$$d$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.  The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$2n$$$ integers $$$d_1, d_2, \ldots, d_{2n}$$$ ($$$0 \le d_i \le 10^{12}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print "YES" in a single line if there exists a possible array $$$a$$$. Otherwise, print "NO". You can print letters in any case (upper or lower).

NoteIn the first test case, $$$a=[1,-3,-1,3]$$$ is one possible symmetric array that generates the array $$$d=[8,12,8,12]$$$.In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array $$$d$$$.

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

string FMT_F = "%.10f";

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

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

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

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

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

		d.sort!"a > b";
		long[] a;
		bool ok = true;
		foreach (i; 0..n)
		{
			if (d[i*2] != d[i*2+1])
			{
				ok = false;
				break;
			}
			a ~= d[i*2];
		}
		if (!ok) continue;

		long tot;
		bool[long] used;
		foreach (i; 0..n)
		{
			auto x = a[i] - tot;
			debug writeln("i:", i, " a[i]:", a[i], " x:", x, " tot:", tot);
			if (x <= 0 || x % ((n-i) * 2))
			{
				ok = false;
				break;
			}
			else
			{
				auto y = x / (n-i);
				if (used.get(y, false))
				{
					ok = false;
					break;
				}
				used[y] = true;
				tot += y;
			}
		}
		ans[ti] = ok;
	}

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

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    long[] D = readarray!long;

    bool C() {
        foreach (d; D) {
            if (d % 2 != 0) return false;
        }
        int[long] M;
        long[] X;
        foreach (d; D) {
            M[d] = M.get(d, 0) + 1;
            if (M[d] % 2 == 0) {
                X ~= d;
            }
        }
        foreach (k, cnt; M) {
            if (cnt % 2 != 0) return false;
        }
        assert(X.length == N);
        sort(X);
        auto A = new long[N];
        long S = X[0] / 2L;
        long Z = 0;
        auto K = new long[N];
        for (int i = 0; i < N-1; i++) {
            if ( (X[i+1] - X[i]) % (2L * (i+1)) != 0 ) return false;
            K[i] = (X[i+1] - X[i]) / (2L * (i+1));
        }
        long A0 = S;{
            for (int i = 0; i < N-1; i++) {
                A0 -= (N - 1L - i) * K[i];
            }
            if (A0 % N != 0) return false;
            A0 /= N;
        }
        A[0] = A0;
        for (int i = 0; i < N-1; i++) {
            A[i+1] = A[i] + K[i];
        }
        /*
        writeln("X: ", X);
        writeln("S: ", S);
        writeln(A);
        */
        auto B = A.dup;
        int m = B.sort.uniq.array.length;
        return A[0] >= 1 && S == sum(A) && m == N;
    }
    writeln(C() ? "YES" : "NO");
}

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

bool solve (int n, long [] d)
{
	sort (d);
	auto g = d.group.array;
	if (!g.all !(q{a[1] == 2}))
	{
		return false;
	}
	auto h = d.stride (2).array;
	if (!h.all !(q{a % 2 == 0}))
	{
		return false;
	}
	h[] /= 2;
	bool [long] s;
	long total = 0;
	foreach_reverse (i; 0..n)
	{
		h[i] -= total;
		if (h[i] % (i + 1) != 0)
		{
			return false;
		}
		auto cur = h[i] / (i + 1);
		if (cur <= 0)
		{
			return false;
		}
		total += cur;
		s[cur] = true;
	}
	return s.length == n;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto d = readln.splitter.map !(to !(long)).array;
		writeln (solve (n, d) ? "YES" : "NO");

	}
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        long[] D; get(D);
        sort(D);
        long[] dd;
        long a;
        for (int i; i < N * 2; i += 2) {
            if (D[i] != D[i+1] || D[i] % 2 != 0) goto ng;
            if (!dd.empty && dd[$-1] == D[i]) goto ng;
            dd ~= D[i];
        }
        if (dd[$-1] % (2 * N) != 0) goto ng;
        a = dd[$-1] / N / 2;
        foreach_reverse (i; 1..N) {
            auto d = dd[i] - dd[i-1];
            if (d % (i*2) != 0) goto ng;
            a -= d / (i*2);
            if (a <= 0) goto ng;
        }
        writeln("YES");
        continue;
        ng:
        writeln("NO");
    }
}

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()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto n = readln.chomp.to!int;
        auto arr = readln.chomp.split.map!(to!long).array;

        bool solve(long[] arr) {
            if (arr.any!(x => x % 2 == 1)) { return false; }

            arr.sort();

            debug { arr.writeln; }

            long[] sums;
            foreach (i; arr.length.iota.stride(2)) {
                if (arr[i] != arr[i+1]) { return false; }

                sums ~= arr[i] / 2;
            }

            foreach (i; 1 .. sums.length) {
                if (sums[i] == sums[i-1]) { return false; }
            }

            debug { sums.writeln; }

            auto tot = sums[0];
            foreach (i; 1 .. sums.length) {
                auto left = sums.length - i.to!long;
                auto diff = sums[i] - sums[i-1];

                if (diff % i != 0) { return false; }

                tot -= diff / i * left;

                debug { tot.writeln; }
            }

            return tot > 0 && tot % sums.length == 0;
        }

        writeln(solve(arr) ? "YES": "NO");
    }
}

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    arr.sort;
    ll[] ray;
    for(int i = 0; i < n; ++i){
        if(arr[2*i] != arr[2*i + 1]){
            writeln("NO");
            return;
        }else{
            ray ~= arr[2*i];
        }
    }
    ray.reverse;
    show(ray);
    ll[] nums;
    ll msum = 0, tim =  n;
    for(int i = 0; i < n; ++i){
        if(ray[i] % 2 != 0){
            writeln("NO");
            return;
        }else{
            ll cval = ray[i]/2;
            cval -= msum;
            if(cval % tim != 0){
                writeln("NO");
                return;
            }
            ll val = cval/tim;
            nums ~= val;
            msum += val;
            --tim;
        }
    }
    show("NUMS: ", nums);
    for(int i = 1; i < n; ++i){
        if(nums[i] >= nums[i-1]){
            writeln("NO");
            return;
        }
    }
    for(int i = 0; i < n; ++i){
        if(nums[i] <= 0){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
}

void main(){
    long tests = 1;
    tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

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

string FMT_F = "%.10f";

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

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

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

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

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

		d.sort!"a > b";
		long[] a;
		bool ok = true;
		foreach (i; 0..n)
		{
			if (d[i*2] != d[i*2+1])
			{
				ok = false;
				break;
			}
			a ~= d[i*2];
		}
		if (!ok) continue;

		long tot;
		foreach (i; 0..n)
		{
			auto x = a[i] - tot;
			debug writeln("i:", i, " a[i]:", a[i], " x:", x, " tot:", tot);
			if (x <= 1 || x % (n-i))
			{
				ok = false;
				break;
			}
			else
			{
				tot += x / (n-i);
			}
		}
		ans[ti] = ok;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

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

		d.sort!"a > b";
		bool ok = true;
		int[long] cnt;
		foreach (i; 0..n)
		{
			++cnt[d[i]];
		}
		foreach (e; cnt.values)
		{
			if (e != 2)
			{
				ok = false;
				break;
			}
		}
		if (!ok) continue;

		long[] a;
		foreach (i; 0..n)
		{
			a ~= d[i*2];
		}

		long tot;
		foreach (i; 0..n)
		{
			auto x = a[i] - tot;
			debug writeln("i:", i, " a[i]:", a[i], " x:", x, " tot:", tot);
			if (x <= 0 || x % ((n-i) * 2))
			{
				ok = false;
				break;
			}
			else
			{
				tot += x / (n-i);
			}
		}
		ans[ti] = ok;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

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

		d.sort!"a > b";
		long[] a;
		bool ok = true;
		foreach (i; 0..n)
		{
			if (d[i*2] != d[i*2+1])
			{
				ok = false;
				break;
			}
			a ~= d[i*2];
		}
		if (!ok) continue;

		long tot;
		foreach (i; 0..n)
		{
			auto x = a[i] - tot;
			debug writeln("i:", i, " a[i]:", a[i], " x:", x, " tot:", tot);
			if (x <= 0 || x % ((n-i) * 2))
			{
				ok = false;
				break;
			}
			else
			{
				tot += x / (n-i);
			}
		}
		ans[ti] = ok;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

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

		d.sort!"a > b";
		long[] a;
		bool ok = true;
		foreach (i; 0..n)
		{
			if (d[i*2] != d[i*2+1])
			{
				ok = false;
				break;
			}
			a ~= d[i*2];
		}
		if (!ok) continue;

		long tot;
		foreach (i; 0..n)
		{
			auto x = a[i] - tot;
			debug writeln("i:", i, " a[i]:", a[i], " x:", x, " tot:", tot);
			if (x == 0 || x % ((n-i) * 2))
			{
				ok = false;
				break;
			}
			else
			{
				tot += x / (n-i);
			}
		}
		ans[ti] = ok;
	}

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

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    long[] D = readarray!long;

    bool C() {
        foreach (d; D) {
            if (d % 2 != 0) return false;
            if (d < 0) return false;
        }
        int[long] M;
        long[] X;
        foreach (d; D) {
            M[d] = M.get(d, 0) + 1;
            if (M[d] % 2 == 0) {
                X ~= d;
            }
        }
        foreach (k, cnt; M) {
            if (cnt % 2 != 0) return false;
        }
        assert(X.length == N);
        sort(X);
        auto A = new long[N];
        long S = X[0] / 2L;
        long Z = 0;
        auto K = new long[N];
        for (int i = 0; i < N-1; i++) {
            if ( (X[i+1] - X[i]) % (2L * (i+1)) != 0 ) return false;
            K[i] = (X[i+1] - X[i]) / (2L * (i+1));
        }
        long A0 = S;{
            for (int i = 0; i < N-1; i++) {
                A0 -= (N - 1L - i) * K[i];
            }
            if (A0 % N != 0) return false;
            A0 /= N;
        }
        A[0] = A0;
        for (int i = 0; i < N-1; i++) {
            A[i+1] = A[i] + K[i];
        }
        /*
        writeln("X: ", X);
        writeln("S: ", S);
        writeln(A);
        */
        return A[0] >= 1 && S == sum(A);
    }
    writeln(C() ? "YES" : "NO");
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    long[] D = readarray!long;

    bool C() {
        foreach (d; D) if (d % 2 != 0) return false;
        int[long] M;
        long[] X;
        foreach (d; D) {
            M[d] = M.get(d, 0) + 1;
            if (M[d] % 2 == 0) {
                X ~= d;
            }
        }
        if (X.length != N) return false;
        sort(X);
        auto A = new long[N];
        long S = X[0] / 2L;
        long Z = 0;
        auto K = new long[N];
        for (int i = 0; i < N-1; i++) {
            if ( (X[i+1] - X[i]) % (2L * (i+1)) != 0 ) return false;
            K[i] = (X[i+1] - X[i]) / (2L * (i+1));
        }
        long A0 = S;{
            for (int i = 0; i < N-1; i++) {
                A0 -= (N - 1L - i) * K[i];
            }
            if (A0 % N != 0) return false;
            A0 /= N;
        }
        A[0] = A0;
        for (int i = 0; i < N-1; i++) {
            A[i+1] = A[i] + K[i];
        }
        /*
        writeln("X: ", X);
        writeln("S: ", S);
        writeln(A);
        */
        return A[0] >= 1 && S == sum(A);
    }
    writeln(C() ? "YES" : "NO");
}

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

bool solve (int n, long [] d)
{
	sort (d);
	auto g = d.group.array;
	if (!g.all !(q{a[1] == 2}))
	{
		return false;
	}
	auto h = d.stride (2).array;
	if (!h.all !(q{a % 2 == 0}))
	{
		return false;
	}
	if (h.front * 2 <= h.back)
	{
		return false;
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto d = readln.splitter.map !(to !(long)).array;
		writeln (solve (n, d) ? "YES" : "NO");

	}
}

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

bool solve (int n, int [] d)
{
	sort (d);
	auto g = d.group.array;
	if (!g.all !(q{a[1] == 2}))
	{
		return false;
	}
	auto h = d.stride (2).array;
	if (!h.all !(q{a % 2 == 0}))
	{
		return false;
	}
/*
	if (h.front * 2 <= h.back)
	{
		return false;
	}
*/
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto d = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, d) ? "YES" : "NO");

	}
}

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

bool solve (int n, int [] d)
{
	sort (d);
	auto g = d.group.array;
	if (!g.all !(q{a[1] == 2}))
	{
		return false;
	}
	auto h = d.stride (2).array;
	if (!h.all !(q{a % 2 == 0}))
	{
		return false;
	}
	if (h.front * 2 <= h.back)
	{
		return false;
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto d = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, d) ? "YES" : "NO");

	}
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        long[] D; get(D);
        if (N == 1) {
            writeln(D == [0, 0] ? "YES" : "NO");
            continue;
        }
        sort(D);
        long[] dd;
        long a;
        for (int i; i < N * 2; i += 2) {
            if (D[i] != D[i+1] || D[i] % 2 != 0) goto ng;
            dd ~= D[i];
        }
        if (dd[$-1] % (2 * N) != 0) goto ng;
        a = dd[$-1] / N / 2;
        foreach_reverse (i; 1..N) {
            auto d = dd[i] - dd[i-1];
            if (d % (i*2) != 0) goto ng;
            a -= d / (i*2);
            if (a <= 0) goto ng;
        }
        writeln("YES");
        continue;
        ng:
        writeln("NO");
    }
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        long[] D; get(D);
        sort(D);
        long[] dd;
        long a;
        for (int i; i < N * 2; i += 2) {
            if (D[i] != D[i+1] || D[i] % 2 != 0) goto ng;
            dd ~= D[i];
        }
        if (dd[$-1] % (2 * N) != 0) goto ng;
        a = dd[$-1] / N / 2;
        foreach_reverse (i; 1..N) {
            auto d = dd[i] - dd[i-1];
            if (d % (i*2) != 0) goto ng;
            a -= d / (i*2);
            if (a <= 0) goto ng;
        }
        writeln("YES");
        continue;
        ng:
        writeln("NO");
    }
}

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()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto n = readln.chomp.to!int;
        auto arr = readln.chomp.split.map!(to!long).array;

        bool solve(long[] arr) {
            if (arr.any!(x => x % 2 == 1)) { return false; }

            arr.sort();

            debug { arr.writeln; }

            long[] sums;
            foreach (i; arr.length.iota.stride(2)) {
                if (arr[i] != arr[i+1]) { return false; }

                sums ~= arr[i] / 2;
            }

            debug { sums.writeln; }

            auto tot = sums[0];
            foreach (i; 1 .. sums.length) {
                auto left = sums.length - i.to!long;
                auto diff = sums[i] - sums[i-1];

                if (diff % i != 0) { return false; }

                tot -= diff / i * left;

                debug { tot.writeln; }
            }

            return tot >= 0 && tot % sums.length == 0;
        }

        writeln(solve(arr) ? "YES": "NO");
    }
}

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    arr.sort;
    ll[] ray;
    for(int i = 0; i < n; ++i){
        if(arr[2*i] != arr[2*i + 1]){
            writeln("NO");
            return;
        }else{
            ray ~= arr[2*i];
        }
    }
    ray.reverse;
    show(ray);
    ll[] nums;
    ll msum = 0, tim =  n, maxval;
    for(int i = 0; i < n; ++i){
        if(ray[i] % 2 != 0){
            writeln("NO");
            return;
        }else{
            ll cval = ray[i]/2;
            cval -= msum;
            if(cval % tim != 0){
                writeln("NO");
                return;
            }
            ll val = cval/tim;
            nums ~= val;
            msum += val;
            --tim;
        }
    }
    show("NUMS: ", nums);
    for(int i = 1; i < n; ++i){
        if(nums[i] > nums[i-1]){
            writeln("NO");
            return;
        }
    }
    for(int i = 0; i < n; ++i){
        if(nums[i] <= 0){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
}

void main(){
    long tests = 1;
    tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");


This is an easier version of the next problem. In this version, $$$q = 0$$$.A sequence of integers is called nice if its elements are arranged in blocks like in $$$[3, 3, 3, 4, 1, 1]$$$. Formally, if two elements are equal, everything in between must also be equal.Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $$$x$$$ to value $$$y$$$, you must also change all other elements of value $$$x$$$ into $$$y$$$ as well. For example, for $$$[3, 3, 1, 3, 2, 1, 2]$$$ it isn't allowed to change first $$$1$$$ to $$$3$$$ and second $$$1$$$ to $$$2$$$. You need to leave $$$1$$$'s untouched or change them to the same value.You are given a sequence of integers $$$a_1, a_2, \ldots, a_n$$$ and $$$q$$$ updates.Each update is of form "$$$i$$$ $$$x$$$" — change $$$a_i$$$ to $$$x$$$. Updates are not independent (the change stays for the future).Print the difficulty of the initial sequence and of the sequence after every update.

The first line contains integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 200\,000$$$, $$$q = 0$$$), the length of the sequence and the number of the updates. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 200\,000$$$), the initial sequence. Each of the following $$$q$$$ lines contains integers $$$i_t$$$ and $$$x_t$$$ ($$$1 \le i_t \le n$$$, $$$1 \le x_t \le 200\,000$$$), the position and the new value for this position.

Print $$$q+1$$$ integers, the answer for the initial sequence and the answer after every update.

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 rtype(T)(){ return read.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 = rtype!real;
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
	int n = rint;
	int q = rint;
	long[] as = rlong(n);

	int[long] lm, rm;
	foreach(int i, a; as){
		if(a !in lm) lm[a] = i;
		if(a !in rm) rm[a] = i;
		rm[a] = i;
	}

	K[] ks;
	int l, r;
	foreach(int i, a; as){
		if(r < i){
			ks ~= K(l, r);
			l = i;
			r = i;
		}
		r = max(r, rm[a]);
	}
	ks ~= K(l, r);

	int ans;
	foreach(k; ks){
		int[long] cnt;
		int maxcnt = 0;
		foreach(i; k.l .. k.r + 1){
			long a = as[i];
			if(a !in cnt) cnt[a] = 0;
			cnt[a] += 1;
			maxcnt = max(maxcnt, cnt[a]);
		}
		ans += (k.r + 1 - k.l) - maxcnt;
	}

	ans.writeln;
}

struct K{
	int l, 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;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  auto q = next!int;
  assert(q == 0);
  auto a = next!int(n);
  auto totalCount = new int[](200_000 + 1);
  foreach(i, ref ai; a)
    totalCount[ai]++;
  auto count = new int[](200_000 + 1);
  int openIntervals = 0;
  int biggestOpen = 0;
  int res = 0;
  size_t firstPosition = 0;
  foreach(i, ref ai; a)
    {
      if (openIntervals == 0)
	{
	  firstPosition = i;
	  assert(count[ai] == 0);
	}
      if (count[ai] == 0)
	{
	  openIntervals++;
	  biggestOpen = max(biggestOpen, totalCount[ai]);
	}
      count[ai]++;
      if (count[ai] == totalCount[ai])
	{
	  openIntervals--;
	  if (openIntervals == 0)
	    {
	      res += i - firstPosition + 1 - biggestOpen;
	      biggestOpen = 0;
	    }
	}
    }
  res.writeln;
  return;
}

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


AquaMoon had $$$n$$$ strings of length $$$m$$$ each. $$$n$$$ is an odd number.When AquaMoon was gone, Cirno tried to pair these $$$n$$$ strings together. After making $$$\frac{n-1}{2}$$$ pairs, she found out that there was exactly one string without the pair!In her rage, she disrupted each pair of strings. For each pair, she selected some positions (at least $$$1$$$ and at most $$$m$$$) and swapped the letters in the two strings of this pair at the selected positions.For example, if $$$m = 6$$$ and two strings "abcdef" and "xyzklm" are in one pair and Cirno selected positions $$$2$$$, $$$3$$$ and $$$6$$$ she will swap 'b' with 'y', 'c' with 'z' and 'f' with 'm'. The resulting strings will be "ayzdem" and "xbcklf".Cirno then stole away the string without pair and shuffled all remaining strings in arbitrary order.AquaMoon found the remaining $$$n-1$$$ strings in complete disarray. Also, she remembers the initial $$$n$$$ strings. She wants to know which string was stolen, but she is not good at programming. Can you help her?

This problem is made as interactive. It means, that your solution will read the input, given by the interactor. But the interactor will give you the full input at the beginning and after that, you should print the answer. So you should solve the problem, like as you solve the usual, non-interactive problem because you won't have any interaction process. The only thing you should not forget is to flush the output buffer, after printing the answer. Otherwise, you can get an "Idleness limit exceeded" verdict. Refer to the interactive problems guide for the detailed information about flushing the output buffer. The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq m \leq 10^5$$$) — the number of strings and the length of each string, respectively. The next $$$n$$$ lines each contain a string with length $$$m$$$, describing the original $$$n$$$ strings. All string consists of lowercase Latin letters. The next $$$n-1$$$ lines each contain a string with length $$$m$$$, describing the strings after Cirno exchanged and reordered them. It is guaranteed that $$$n$$$ is odd and that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$. Hack format: The first line should contain a single integer $$$t$$$. After that $$$t$$$ test cases should follow in the following format: The first line should contain two integers $$$n$$$ and $$$m$$$. The following $$$n$$$ lines should contain $$$n$$$ strings of length $$$m$$$, describing the original strings. The following $$$\frac{n-1}{2}$$$ lines should describe the pairs. They should contain, in the following order: the index of the first string $$$i$$$ ($$$1 \leq i \leq n$$$), the index of the second string $$$j$$$ ($$$1 \leq j \leq n$$$, $$$i \neq j$$$), the number of exchanged positions $$$k$$$ ($$$1 \leq k \leq m$$$), and the list of $$$k$$$ positions that are exchanged ($$$k$$$ distinct indices from $$$1$$$ to $$$m$$$ in any order). The final line should contain a permutation of integers from $$$1$$$ to $$$n$$$, describing the way the strings should be reordered. The strings will be placed in the order indices placed in this permutation, the stolen string index will be ignored.

For each test case print a single line with the stolen string.

3
3 5
aaaaa
bbbbb
ccccc
aaaaa
bbbbb
3 4
aaaa
bbbb
cccc
aabb
bbaa
5 6
abcdef
uuuuuu
kekeke
ekekek
xyzklm
xbcklf
eueueu
ayzdem
ukukuk

NoteIn the first test case, "aaaaa" and "bbbbb" exchanged all positions, and "ccccc" is the stolen string.In the second test case, "aaaa" and "bbbb" exchanged two first positions, and "cccc" is the stolen string.This is the first test in the hack format: 33 5aaaaabbbbbccccc1 2 5 1 2 3 4 52 1 33 4aaaabbbbcccc1 2 2 1 22 1 35 6abcdefuuuuuukekekeekekekxyzklm1 5 3 2 3 62 4 3 2 4 65 4 1 2 3

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

int main(string[] args)
{
    auto t = to!int(readln.strip);
    auto c = new int[26];
    auto sc = new int[26];
    auto lc = new int[][](100010, 26);
    auto cc = new int[][](100010, 26);
    auto scc = new int[][](100010, 26);
    auto s = new string[100010];
    foreach (_; 0 .. t)
    {
        int n, m;
        readf("%d %d\n", &n, &m);
        fill(c, 0);
        foreach (i; 0 .. m) fill(cc[i], 0);
        foreach (i; 0 .. n)
        {
            s[i] = readln.strip;
            fill(lc[i], 0);
            foreach (j; 0 .. m)
            {
                ++ c[s[i][j] - 'a'];
                ++ lc[i][s[i][j] - 'a'];
                ++ cc[j][s[i][j] - 'a'];
            }
        }
        fill(sc, 0);
        foreach (i; 0 .. m) fill(scc[i], 0);
        foreach (i; 0 .. n - 1)
        {
            auto ss = readln.strip;
            foreach (j; 0 .. m)
            {
                ++ sc[ss[j] - 'a'];
                ++ scc[j][ss[j] - 'a'];
            }
        }
        int[] res;
        foreach (i; 0 .. n)
        {
            int j;
            for (j = 0; j < 26; ++ j)
            {
                if (c[j] - lc[i][j] != sc[j]) break;
                int k;
                for (k = 0; k < m; ++ k)
                {
                    if (s[i][k] - 'a' == j && cc[k][j] - 1 != scc[k][j]) break;
                    if (s[i][k] - 'a' != j && cc[k][j] != scc[k][j]) break;
                }
                if (k < m) break;
            }
            if (j == 26)
            {
                writeln(s[i]);
                stdout.flush;
                break;
            }
        }
    }
    return 0;
}

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

void main() {
    int t = readln.chomp.to!int;
while(t--) {
    int n, m;
    readf("%s %s\n", &n, &m);
    string[] original, shuffled;
    for(int i = 0; i < n; ++i) {
        string s;
        readf("%s\n", &s);
        original ~= s;
    }
    for(int i = 1; i < n; ++i) {
        string s;
        readf("%s\n", &s);
        shuffled ~= s;
    }
    int[char][int] counter;
    foreach(s; original) {
        for(int i = 0; i < m; i++) {
            counter[i][s[i]] += 1;
        }
    }
    foreach(s; shuffled) {
        for(int i = 0; i < m; i++) {
            counter[i][s[i]] += 1;
        }
    }
    for(int i = 0; i < m; ++i) {
        for(char ch = 'a'; ch <= 'z'; ++ch) {
            counter[i][ch] += 0;
            if(counter[i][ch] % 2 == 1) {
                ch.write;
            }
        }
    }
    "".writeln;
}
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    auto a = readln.strip.split.map!(to!int).array;
    int n = a[0];
    int m = a[1];
    int i, j;
    string[] orig, shuf;
    for (i = 0; i < n; i++)
      orig ~= readln.strip;
    for (i = 0; i < n - 1; i++)
      shuf ~= readln.strip;
    int[][] cnt = new int[][](m, 30);
    foreach (s ; orig) {
      for (i = 0; i < m; i++) {
        cnt[i][s[i] - 'a']++;
      }
    }
    foreach (s ; shuf) {
      for (i = 0; i < m; i++) {
        cnt[i][s[i] - 'a']--;
      }
    }
    char[] result = cnt.map!(x => cast(char)('a' + x.countUntil(1))).array;
    writeln(result.idup);
    stdout.flush;
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		string[] a, b;
		foreach (i; 0..n)
			a ~= RD!string;
		foreach (i; 0..n-1)
			b ~= RD!string;

		auto cnt = new long[][](m, 26);
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				++cnt[j][a[i][j]-'a'];
			}
		}
		foreach (i; 0..n-1)
		{
			foreach (j; 0..m)
			{
				--cnt[j][b[i][j]-'a'];
			}
		}

		foreach (i; 0..n)
		{
			bool ok = true;
			foreach (j; 0..m)
			{
				if (cnt[j][a[i][j]-'a'] != 1)
				{
					ok = false;
					break;
				}
			}
			if (ok)
			{
				ans[ti] = a[i].idup;
				break;
			}
		}
	}

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

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

int main(string[] args)
{
    auto t = to!int(readln.strip);
    auto c = new int[26];
    auto sc = new int[26];
    auto lc = new int[][](100010, 26);
    auto s = new string[100010];
    foreach (_; 0 .. t)
    {
        int n, m;
        readf("%d %d\n", &n, &m);
        fill(c, 0);
        fill(sc, 0);
        foreach (i; 0 .. n)
        {
            s[i] = readln.strip;
            fill(lc[i], 0);
            foreach (j; 0 .. m)
            {
                ++ c[s[i][j] - 'a'];
                ++ lc[i][s[i][j] - 'a'];
            }
        }
        foreach (i; 0 .. n - 1)
        {
            auto ss = readln.strip;
            foreach (j; 0 .. m) ++ sc[ss[j] - 'a'];
        }
        foreach (i; 0 .. n)
        {
            int j;
            for (j = 0; j < 26; ++ j) if (c[j] - lc[i][j] != sc[j]) break;
            if (j == 26)
            {
                writeln(s[i]);
                stdout.flush;
                break;
            }
        }
    }
    return 0;
}


There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.

The first line contains two positive integers n and s (1 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of workers and the id of the chief. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.

Print the minimum number of workers that could make a mistake.

NoteIn the first example it is possible that only the first worker made a mistake. Then:   the immediate superior of the first worker is the second worker,  the immediate superior of the third worker is the first worker,  the second worker is the chief.

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int n, root;
int[200_000] _a;
int[ ] a;

void main() {
    while (read(&n, &root)) {
        a = _a[0 .. n];
        root--;
        version (LocalProject)
            a[ ] = 0;
        int result = 0;
        foreach (i; 0 .. n) {
            int x;
            read(&x);
            if (i == root) {
                a[0]++;
                result = x != 0;
            } else
                a[x]++;
        }
        int i = 0, j = n;
        result += a[0] - 1;
        debug writeln(a);
        while (a[0] != 1) {
            while (a[i])
                i++;
            a[i]++;
            a[0]--;
        }
        while (i != j - 1) {
            if (!a[i]) {
                while (j != i && !a[j - 1])
                    j--;
                if (j == i)
                    break;
                a[j - 1]--;
                result++;
            }
            i++;
        }
        writeln(result);
    }
}


Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?

The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.  Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree.

Print a single integer — the maximum number of fruit that Valera can collect.

NoteIn the first sample, in order to obtain the optimal answer, you should act as follows.   On the first day collect 3 fruits from the 1-st tree.  On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.  On the third day collect the remaining fruits from the 2-nd tree.  In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.

import std.stdio;
import std.algorithm;

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

        struct tree {
            int cnt, day;
        }
        auto a = new tree[n];

        foreach (ref cur; a)
            readf(" %s %s", &cur.day, &cur.cnt);
        a.sort!((a, b) => a.day < b.day);

        //foreach(cur; a)
            //writeln(cur.day, ' ', cur.cnt);

        int curDay = -100500;
        bool okDay(int day) {
            return day == curDay || day + 1 == curDay;
        }

        int it = 0;
        int ans = 0;
        while (it < a.length) {
            curDay = max(curDay, a[it].day);
            while (it < a.length && !okDay(a[it].day)) {
                it++;
                if (it < a.length)
                    curDay = max(curDay, a[it].day);
            }
            if (it >= a.length)
                break;
            int sum = 0;
            while (it < a.length && okDay(a[it].day) && sum < v) {
                int get = min(sum + a[it].cnt, v) - sum;
                sum += get;
                //writeln("IT ", it, ' ' , a[it].cnt, ' ', get);
                a[it].cnt -= get;
                if (a[it].cnt == 0)
                    it++;
            }
            //writeln(curDay, ' ', sum, ' ', it);
            assert(0 <= sum && sum <= v);
            ans += sum;
            curDay++;
        }
        writeln(ans);
    }
}

import std.stdio;
import std.algorithm;

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

        struct tree {
            int cnt, day;
        }
        auto a = new tree[n];

        foreach (ref cur; a)
            readf(" %s %s", &cur.day, &cur.cnt);
        a.sort!((a, b) => a.day < b.day);

        //foreach(cur; a)
            //writeln(cur.day, ' ', cur.cnt);

        int curDay = a[0].day;
        bool okDay(int day) {
            return day == curDay || day + 1 == curDay;
        }

        int it = 0;
        int ans = 0;
        while (it < a.length) {
            if (curDay < a[it].day)
                curDay = a[it].day;
            while (it < a.length && !okDay(a[it].day))
                it++;
            if (it >= a.length)
                break;
            int sum = 0;
            while (it < a.length && okDay(a[it].day) && sum < v) {
                int get = min(sum + a[it].cnt, v) - sum;
                sum += get;
                //writeln("IT ", it, ' ' , a[it].cnt, ' ', get);
                a[it].cnt -= get;
                if (a[it].cnt == 0)
                    it++;
            }
            //writeln(curDay, ' ', sum, ' ', it);
            ans += sum;
            curDay++;
        }
        writeln(ans);
    }
}


Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:  Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1]. Count how many numbers equal to k are on the segment from l to r (both borders inclusive). Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n). The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries. As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n. To decode the queries from the data given in input, you need to perform the following transformations: li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1. Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

For each query of the 2-nd type print the answer on a single line.

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, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	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)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in 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)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable BLOCK_SIZE = 1000;

int N;
int[] A;

int numBlocks;
int[][] valss;
int[][] cntss;

void rotate(int l, int r) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;
	int[] work;

	const a0 = valss[qr][$ - 1 - rr];
	valss[qr] = valss[qr][0 .. $ - 1 - rr] ~ valss[qr][$ - rr .. $];
	--cntss[qr][a0];

	valss[ql] = valss[ql][0 .. $ - rl] ~ a0 ~ valss[ql][$ - rl .. $];
	++cntss[ql][a0];

	foreach (j; ql .. qr) {
		const a = valss[j][0];
		valss[j] = valss[j][1 .. $];
		--cntss[j][a];
		valss[j + 1] ~= a;
		++cntss[j + 1][a];
	}
}

int count(int l, int r, int k) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;

	int ret;
	if (ql == qr) {
		foreach (a; valss[ql][$ - 1 - rr .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
	} else {
		foreach (a; valss[ql][0 .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
		foreach (j; ql + 1 .. qr) {
			ret += cntss[j][k];
		}
		foreach (a; valss[qr][$ - 1 - rr .. $]) {
			if (a == k) {
				ret += 1;
			}
		}
	}
	return ret;
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new int[N];
		foreach (i; 0 .. N) {
			A[i] = readInt;
		}

		numBlocks = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
		valss = new int[][numBlocks];
		cntss = new int[][](numBlocks, N + 1);
		foreach_reverse (i; 0 .. N) {
			const qi = i / BLOCK_SIZE;
			valss[qi] ~= A[i];
			++cntss[qi][A[i]];
		}

		int lastans = 0;
		foreach (q; 0 .. readInt) {
debug{
writeln("valss = ",valss);
writeln("cntss = ",cntss);
}
			switch (readInt) {
				case 1: {
					int l_ = readInt;
					int r_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** rotate ",l," ",r," ****");
}
					--l;
					--r;
					if (l < r) {
						rotate(l, r);
					}
				} break;
				case 2: {
					int l_ = readInt;
					int r_ = readInt;
					int k_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					int k = ((k_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** count  ",l," ",r," ",k," ****");
}
					--l;
					--r;
					const res = count(l, r, k);
					writeln(res);
					lastans = res;
				} break;
				default: assert(false);
			}
		}
	}
	} catch (EOFException) {}
}

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, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	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)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in 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)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable BLOCK_SIZE = 3;

int N;
int[] A;

int numBlocks;
int[][] valss;
int[][] cntss;

void rotate(int l, int r) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;
	int[] work;

	const a0 = valss[qr][$ - 1 - rr];
writeln("a0 = ",a0);
	valss[qr] = valss[qr][0 .. $ - 1 - rr] ~ valss[qr][$ - rr .. $];
	--cntss[qr][a0];

	valss[ql] = valss[ql][0 .. $ - rl] ~ a0 ~ valss[ql][$ - rl .. $];
	++cntss[ql][a0];

	foreach (j; ql .. qr) {
		const a = valss[j][0];
		valss[j] = valss[j][1 .. $];
		--cntss[j][a];
		valss[j + 1] ~= a;
		++cntss[j + 1][a];
	}
}

int count(int l, int r, int k) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;

	int ret;
	if (ql == qr) {
		foreach (a; valss[ql][$ - rr .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
	} else {
		foreach (a; valss[ql][0 .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
		foreach (j; ql + 1 .. qr) {
			ret += cntss[j][k];
		}
		foreach (a; valss[qr][$ - rr .. $]) {
			if (a == k) {
				ret += 1;
			}
		}
	}
	return ret;
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new int[N];
		foreach (i; 0 .. N) {
			A[i] = readInt;
		}

		numBlocks = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
		valss = new int[][numBlocks];
		cntss = new int[][](numBlocks, N + 1);
		foreach_reverse (i; 0 .. N) {
			const qi = i / BLOCK_SIZE;
			valss[qi] ~= A[i];
			++cntss[qi][A[i]];
		}

		int lastans = 0;
		foreach (q; 0 .. readInt) {
debug{
writeln("valss = ",valss);
writeln("cntss = ",cntss);
}
			switch (readInt) {
				case 1: {
					int l_ = readInt;
					int r_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** rotate ",l," ",r," ****");
}
					--l;
					--r;
					if (l < r) {
						rotate(l, r);
					}
				} break;
				case 2: {
					int l_ = readInt;
					int r_ = readInt;
					int k_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					int k = ((k_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** count  ",l," ",r," ",k," ****");
}
					--l;
					const res = count(l, r, k);
					writeln(res);
					lastans = res;
				} break;
				default: assert(false);
			}
		}
	}
	} catch (EOFException) {}
}

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, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	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)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in 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)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable BLOCK_SIZE = 300;

int N;
int[] A;

int numBlocks;
int[][] valss;
int[][] cntss;

void rotate(int l, int r) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;
	int[] work;

	const a0 = valss[qr][$ - 1 - rr];
writeln("a0 = ",a0);
	valss[qr] = valss[qr][0 .. $ - 1 - rr] ~ valss[qr][$ - rr .. $];
	--cntss[qr][a0];

	valss[ql] = valss[ql][0 .. $ - rl] ~ a0 ~ valss[ql][$ - rl .. $];
	++cntss[ql][a0];

	foreach (j; ql .. qr) {
		const a = valss[j][0];
		valss[j] = valss[j][1 .. $];
		--cntss[j][a];
		valss[j + 1] ~= a;
		++cntss[j + 1][a];
	}
}

int count(int l, int r, int k) {
	const ql = l / BLOCK_SIZE, rl = l % BLOCK_SIZE;
	const qr = r / BLOCK_SIZE, rr = r % BLOCK_SIZE;

	int ret;
	if (ql == qr) {
		foreach (a; valss[ql][$ - rr .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
	} else {
		foreach (a; valss[ql][0 .. $ - rl]) {
			if (a == k) {
				ret += 1;
			}
		}
		foreach (j; ql + 1 .. qr) {
			ret += cntss[j][k];
		}
		foreach (a; valss[qr][$ - rr .. $]) {
			if (a == k) {
				ret += 1;
			}
		}
	}
	return ret;
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new int[N];
		foreach (i; 0 .. N) {
			A[i] = readInt;
		}

		numBlocks = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
		valss = new int[][numBlocks];
		cntss = new int[][](numBlocks, N + 1);
		foreach_reverse (i; 0 .. N) {
			const qi = i / BLOCK_SIZE;
			valss[qi] ~= A[i];
			++cntss[qi][A[i]];
		}

		int lastans = 0;
		foreach (q; 0 .. readInt) {
debug{
writeln("valss = ",valss);
writeln("cntss = ",cntss);
}
			switch (readInt) {
				case 1: {
					int l_ = readInt;
					int r_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** rotate ",l," ",r," ****");
}
					--l;
					--r;
					if (l < r) {
						rotate(l, r);
					}
				} break;
				case 2: {
					int l_ = readInt;
					int r_ = readInt;
					int k_ = readInt;
					int l = ((l_ + lastans - 1) % N) + 1;
					int r = ((r_ + lastans - 1) % N) + 1;
					int k = ((k_ + lastans - 1) % N) + 1;
					if (l > r) {
						swap(l, r);
					}
debug{
writeln("**** count  ",l," ",r," ",k," ****");
}
					--l;
					const res = count(l, r, k);
					writeln(res);
					lastans = res;
				} break;
				default: assert(false);
			}
		}
	}
	} catch (EOFException) {}
}


Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.

The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.

In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.

NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.

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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto n = RD!int;
	auto s = RD!(char[]);
	long ans;
	foreach (i; 0..n/2)
	{
		if (s[i*2] == s[i*2+1])
		{
			++ans;
			s[i*2] = s[i*2] == 'a' ? 'b' : 'a';
		}
	}

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