A sequence $$$a = [a_1, a_2, \ldots, a_l]$$$ of length $$$l$$$ has an ascent if there exists a pair of indices $$$(i, j)$$$ such that $$$1 \le i < j \le l$$$ and $$$a_i < a_j$$$. For example, the sequence $$$[0, 2, 0, 2, 0]$$$ has an ascent because of the pair $$$(1, 4)$$$, but the sequence $$$[4, 3, 3, 3, 1]$$$ doesn't have an ascent.Let's call a concatenation of sequences $$$p$$$ and $$$q$$$ the sequence that is obtained by writing down sequences $$$p$$$ and $$$q$$$ one right after another without changing the order. For example, the concatenation of the $$$[0, 2, 0, 2, 0]$$$ and $$$[4, 3, 3, 3, 1]$$$ is the sequence $$$[0, 2, 0, 2, 0, 4, 3, 3, 3, 1]$$$. The concatenation of sequences $$$p$$$ and $$$q$$$ is denoted as $$$p+q$$$.Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has $$$n$$$ sequences $$$s_1, s_2, \ldots, s_n$$$ which may have different lengths. Gyeonggeun will consider all $$$n^2$$$ pairs of sequences $$$s_x$$$ and $$$s_y$$$ ($$$1 \le x, y \le n$$$), and will check if its concatenation $$$s_x + s_y$$$ has an ascent. Note that he may select the same sequence twice, and the order of selection matters.Please count the number of pairs ($$$x, y$$$) of sequences $$$s_1, s_2, \ldots, s_n$$$ whose concatenation $$$s_x + s_y$$$ contains an ascent.

The first line contains the number $$$n$$$ ($$$1 \le n \le 100\,000$$$) denoting the number of sequences. The next $$$n$$$ lines contain the number $$$l_i$$$ ($$$1 \le l_i$$$) denoting the length of $$$s_i$$$, followed by $$$l_i$$$ integers $$$s_{i, 1}, s_{i, 2}, \ldots, s_{i, l_i}$$$ ($$$0 \le s_{i, j} \le 10^6$$$) denoting the sequence $$$s_i$$$.  It is guaranteed that the sum of all $$$l_i$$$ does not exceed $$$100\,000$$$.

Print a single integer, the number of pairs of sequences whose concatenation has an ascent.

NoteFor the first example, the following $$$9$$$ arrays have an ascent: $$$[1, 2], [1, 2], [1, 3], [1, 3], [1, 4], [1, 4], [2, 3], [2, 4], [3, 4]$$$. Arrays with the same contents are counted as their occurences.

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = new int [] [n];
		foreach (ref c; a)
		{
			c = readln.splitter.drop (1).map !(to !(int)).array;
		}

		long res = n * 1L * n;
		a = a.filter !(c => c.isSorted !(q{a > b})).array;
		auto lo = a.map !(minElement).array;
		auto hi = a.map !(maxElement).array;
		sort (lo);
		sort (hi);

		int k = lo.length.to !(int);
		int j = 0;
		foreach (i; 0..k)
		{
			while (j < k && hi[j] <= lo[i])
			{
				j += 1;
			}
			res -= j;
		}
		writeln (res);
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	int n = rint;
	int[long] uc, vc;
	foreach(i; 0 .. n){
		int l = rint;
		long[] aq = rlong(l);

		if(aq.hasAscent) continue;
		else{
			if(aq[0] !in uc) uc[aq[0]] = 0;
			uc[aq[0]] += 1;

			if(aq[$ - 1] !in vc) vc[aq[$ - 1]] = 0;
			vc[aq[$ - 1]] += 1;
		}
	}
	log("uc:", uc, "vc:", vc);

	long[] uks = uc.keys, vks = vc.keys;

	long[] zs = uks.dup;
	foreach(uk; uks) if(uk !in vc) vc[uk] = 0;
	foreach(vk; vks) if(vk !in uc) uc[vk] = 0, zs ~= vk;
	log("uks:", uks, "vks:", vks, "zs:", zs);

	zs.sort();
	log("zs:", zs);

	int[long] uu, vu;
	int vt = 0;
	foreach_reverse(z; zs){
		vu[z] = vt + vc[z];
		vt += vc[z];
	}
	int ut = 0;
	foreach(z; zs){
		uu[z] = ut + uc[z];
		ut += uc[z];
	}
	log( "uu:", uu, "vc:", vc);

	long ans = n.to!long * n.to!long;
	foreach(z; zs){
		ans -= uu[z].to!long * vc[z].to!long;
	}

	ans.writeln;

}
bool hasAscent(long[] aq){
	long b = aq[0];
	foreach(a; aq){
		if(b < a) return 1;
		else b = a;
	}
	return 0;
}

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

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

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

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

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

      auto decr = new bool[N];
      foreach (i; 0 .. N) {
        decr[i] = true;
        foreach (x; 0 .. L[i] - 1) {
          decr[i] = decr[i] && (S[i][x] >= S[i][x + 1]);
        }
      }

      int lim;
      foreach (i; 0 .. N) {
        foreach (x; 0 .. L[i]) {
          chmax(lim, S[i][x]);
        }
      }
      lim += 10;
      auto fs = new long[lim];
      foreach (i; 0 .. N) {
        if (decr[i]) {
          ++fs[S[i][0]];
        }
      }
      foreach (a; 1 .. lim) {
        fs[a] += fs[a - 1];
      }

      long ans = 1L * N * N;
      foreach (i; 0 .. N) {
        if (decr[i]) {
          ans -= fs[S[i][L[i] - 1]];
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void 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); }

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = ok - (ok-ng) / 2;
		if (unaryFun!pred(mid))
			ok = mid;
		else ng = mid;
	}
	return ok;
}

void main()
{
	auto n = RD!int;
	auto s = new int[][](n);
	auto mm = new int[][](n);
	auto oks = new bool[](n);
	foreach (i; 0..n)
	{
		auto l = RD!int;
		s[i].length = l;
		foreach (j; 0..l)
			s[i][j] = RD!int;

		mm[i] = [int.max, int.min];
		int x = int.max;
		foreach (j; 0..l)
		{
			if (s[i][j] > x)
				oks[i] = true;
			x.chmin(s[i][j]);
			mm[i][0].chmin(s[i][j]);
			mm[i][1].chmax(s[i][j]);
		}
	}

	long cnt_ok;
	foreach (i; 0..n)
		if (oks[i]) ++cnt_ok;

	int[] list;
	foreach (i; 0..n)
	{
		if (oks[i]) continue;
		list ~= mm[i][1];
	}
	list.sort!"a > b"();

	long ans = cnt_ok * cnt_ok;
	ans += cnt_ok * (n - cnt_ok) * 2;
	foreach (i; 0..n)
	{
		debug writeln("i:", i, " ans:", ans);
		if (oks[i]) continue;
		bool f(int x)
		{
			return list[x] > mm[i][0];
		}
		auto r = binarySearch!(f)(-1, cast(int)list.length);
		debug writeln("i:", i, " r:", r);
		ans += r+1;
	}

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void 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); }

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = ok - (ok-ng) / 2;
		if (unaryFun!pred(mid))
			ok = mid;
		else ng = mid;
	}
	return ok;
}

void main()
{
	auto n = RD!int;
	auto s = new int[][](n);
	auto mm = new int[][](n);
	auto oks = new bool[](n);
	foreach (i; 0..n)
	{
		auto l = RD!int;
		s[i].length = l;
		foreach (j; 0..l)
			s[i][j] = RD!int;

		mm[i] = [int.max, int.min];
		int x = int.max;
		foreach (j; 0..l)
		{
			if (s[i][j] > x)
				oks[i] = true;
			x.chmin(s[i][j]);
			mm[i][0].chmin(s[i][j]);
			mm[i][1].chmax(s[i][j]);
		}
	}

	int cnt_ok;
	foreach (i; 0..n)
		if (oks[i]) ++cnt_ok;

	int[] list;
	foreach (i; 0..n)
	{
		if (oks[i]) continue;
		list ~= mm[i][1];
	}
	list.sort!"a > b"();

	long ans = max(cnt_ok * (cnt_ok+1) / 2 * 2 - cnt_ok, 0);
	ans += cnt_ok * (n - cnt_ok) * 2;
	foreach (i; 0..n)
	{
		debug writeln("i:", i, " ans:", ans);
		if (oks[i]) continue;
		bool f(int x)
		{
			return list[x] > mm[i][0];
		}
		auto r = binarySearch!(f)(-1, cast(int)list.length);
		debug writeln("i:", i, " r:", r);
		ans += r+1;
	}

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


Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.

The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.

In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them.

NoteIn the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.range;
import std.range.primitives;
import std.conv;
import std.container.rbtree;

int main() {
    debug stdin = File("in.txt", "r");

    int[2000] arr;
    auto toomany = new RedBlackTree!(int, "a < b", true);
    int[] toofew;
    int[int] freq;
    int n, m;
    readf(" %s %s\n", &n, &m);
    int minmax = n / m;
    foreach ( i; 1 .. m + 1 ) {
        freq[i] = 0;
    }
    foreach ( i; 0 .. n ) {
        readf(" %s", &arr[i]);
        if (arr[i] > m) toomany.insert(arr[i]);
        else freq[arr[i]] += 1;
    }
    foreach ( i; freq.byKey ) {
        if (freq[i] > minmax) {
            foreach ( j; 0 .. freq[i] - minmax) {
                toomany.insert(i);
            }
        }
        else if (freq[i] < minmax) {
            foreach ( j; 0 .. minmax - freq[i]) {
                toofew ~= i;
            }
        }
    }
    int cnter = 0;
    for (int i = 0; !toofew.empty; ++i) {
        auto er = toomany.equalRange(arr[i]);
        if (!er.empty) {
            ++cnter;
            toomany.removeKey(arr[i]);
            arr[i] = toofew.back;
            toofew.popBack;
        }
    }
    writeln(minmax, " ", cnter);
    foreach ( i; 0 .. n ) {
        write(arr[i], " ");
    }
    writeln();

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.range;
import std.range.primitives;
import std.conv;
import std.bigint;

int main() {
    debug stdin = File("in.txt", "r");

    int[2000] arr;
    int[] toomany, toofew;
    int[int] freq;
    int n, m;
    readf(" %s %s\n", &n, &m);
    int minmax = n / m;
    foreach ( i; 1 .. m + 1 ) {
        freq[i] = 0;
    }
    foreach ( i; 0 .. n ) {
        readf(" %s", &arr[i]);
        if (arr[i] > m) toomany ~= arr[i];
        else freq[arr[i]] += 1;
    }
    foreach ( i; freq.byKey ) {
        if (freq[i] > minmax) {
            foreach ( j; 0 .. freq[i] - minmax) {
                toomany ~= i;
            }
        }
        else if (freq[i] < minmax) {
            foreach ( j; 0 .. minmax - freq[i]) {
                toofew ~= i;
            }
        }
    }
    int cnter = 0;
    for (int i = 0; !toofew.empty; ++i) {
        int idx = toomany.length - toomany.find(arr[i]).length;
        if (idx != toomany.length) {
            ++cnter;
            arr[i] = toofew.back;
            toofew.popBack;
            toomany = toomany.remove(idx);
        }
    }
    writeln(minmax, " ", cnter);
    foreach ( i; 0 .. n ) {
        write(arr[i], " ");
    }
    writeln();

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.range;
import std.range.primitives;
import std.conv;

int main() {
    debug stdin = File("in.txt", "r");

    int[2000] arr;
    int[int] freq;
    int[int] toomany;
    int[] toofew;
    int n, m;
    readf(" %s %s\n", &n, &m);
    int minmax = n / m;
    foreach ( i; 1 .. m + 1 ) {
        freq[i] = 0;
    }
    foreach ( i; 0 .. n ) {
        readf(" %s", &arr[i]);
        if (arr[i] > m) toomany[arr[i]] += 1;
        else freq[arr[i]] += 1;
    }
    foreach ( i; freq.byKey ) {
        if (freq[i] > minmax) {
            foreach ( j; 0 .. freq[i] - minmax) {
                toomany[i] += 1;
            }
        }
        else if (freq[i] < minmax) {
            foreach ( j; 0 .. minmax - freq[i]) {
                toofew ~= i;
            }
        }
    }
    int cnter = 0;
    for (int i = 0; !toofew.empty; ++i) {
        auto val = arr[i] in toomany;
        if (val && *val > 0) {
            ++cnter;
            *val -= 1;
            arr[i] = toofew.back;
            toofew.popBack;
        }
    }
    writeln(minmax, " ", cnter);
    foreach ( i; 0 .. n ) {
        write(arr[i], " ");
    }
    writeln();

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.range;
import std.range.primitives;
import std.conv;
import std.bigint;

int main() {
    debug stdin = File("in.txt", "r");

    int[2000] arr;
    int[] toomany, toofew;
    int[int] freq;
    int n, m;
    readf(" %s %s\n", &n, &m);
    int minmax = n / m;
    foreach ( i; 1 .. m + 1 ) {
        freq[i] = 0;
    }
    foreach ( i; 0 .. n ) {
        readf(" %s", &arr[i]);
        if (arr[i] > m) toomany ~= arr[i];
        else freq[arr[i]] += 1;
    }
    foreach ( i; freq.byKey ) {
        if (freq[i] > minmax) toomany ~= i;
        else if (freq[i] < minmax) toofew ~= i;
    }
    int cnter = 0;
    for (int i = 0; !toofew.empty; ++i) {
        int idx = toomany.length - toomany.find(arr[i]).length;
        if (idx != toomany.length) {
            ++cnter;
            arr[i] = toofew.back;
            toofew.popBack;
            toomany = toomany.remove(idx);
        }
    }
    writeln(minmax, " ", cnter);
    foreach ( i; 0 .. n ) {
        write(arr[i], " ");
    }
    writeln();

    return 0;
}


Given an array $$$a$$$ of $$$n$$$ elements, print any value that appears at least three times or print -1 if there is no such value.

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

For each test case, print any value that appears at least three times or print -1 if there is no such value.

NoteIn the first test case there is just a single element, so it can't occur at least three times and the answer is -1.In the second test case, all three elements of the array are equal to $$$2$$$, so $$$2$$$ occurs three times, and so the answer is $$$2$$$.For the third test case, $$$2$$$ occurs four times, so the answer is $$$2$$$.For the fourth test case, $$$4$$$ occurs three times, so the answer is $$$4$$$.For the fifth test case, $$$1$$$, $$$2$$$ and $$$3$$$ all occur at least three times, so they are all valid outputs.For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1.

import std;

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [int] c;
		foreach (ref x; chain (a, sequence !("-1")))
		{
			c[x] += 1;
			if (c[x] >= 3)
			{
				writeln (x);
				continue multitest_loop;
			}
		}
		assert (false);
	}
}


Koa the Koala and her best friend want to play a game.The game starts with an array $$$a$$$ of length $$$n$$$ consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to $$$0$$$. Koa starts.Let's describe a move in the game:  During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player. More formally: if the current score of the player is $$$x$$$ and the chosen element is $$$y$$$, his new score will be $$$x \oplus y$$$. Here $$$\oplus$$$ denotes bitwise XOR operation. Note that after a move element $$$y$$$ is removed from $$$a$$$.  The game ends when the array is empty. At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.If both players play optimally find out whether Koa will win, lose or draw the game.

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

For each test case print:   WIN if Koa will win the game.  LOSE if Koa will lose the game.  DRAW if the game ends in a draw.

NoteIn testcase $$$1$$$ of the first sample we have:$$$a = [1, 2, 2]$$$. Here Koa chooses $$$1$$$, other player has to choose $$$2$$$, Koa chooses another $$$2$$$. Score for Koa is $$$1 \oplus 2 = 3$$$ and score for other player is $$$2$$$ so Koa wins.

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

string solve (int n, int [] a)
{
	foreach_reverse (b; 0..30)
	{
		auto z = a.count !(x => (x & (1 << b)) > 0);
		if (z & 1)
		{
			if (z % 2 == n % 2 && z % 4 == 3)
			{
				return "LOSE";
			}
			return "WIN";
		}
	}
	return "DRAW";
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, a));
	}
}

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

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

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

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

void main() {
  debug {
    enum lim = 100;
    auto dp = new int[][][][](lim, lim, 2, 2);
    foreach (a; 0 .. lim) foreach (b; 0 .. lim) foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
      if (a + b == 0) {
        dp[a][b][s][t] = s;
      } else if (t == 1) {
        dp[a][b][s][t] = 0;
        if (a > 0 && dp[a - 1][b][s ^ 0][t ^ 1] == 1) dp[a][b][s][t] = 1;
        if (b > 0 && dp[a][b - 1][s ^ 1][t ^ 1] == 1) dp[a][b][s][t] = 1;
      } else {
        dp[a][b][s][t] = 1;
        if (a > 0 && dp[a - 1][b][s][t ^ 1] == 0) dp[a][b][s][t] = 0;
        if (b > 0 && dp[a][b - 1][s][t ^ 1] == 0) dp[a][b][s][t] = 0;
      }
    }
    foreach (a; 0 .. lim) foreach (b; 0 .. lim) {
      if (b % 2 != 0) {
        // writeln(a, " ", b, ": ", dp[a][b]);
        foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
          assert(dp[a][b][s][t] == ((a % 2 == 0) ? (((b >> 1) & 1) ^ s ^ t) : t));
        }
      }
    }
  }

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

        long sum;
        foreach (i; 0 .. N) {
          sum ^= A[i];
        }
        if (sum == 0) {
          writeln("DRAW");
        } else {
          const e = bsr(sum);
          auto cnt = new int[2];
          foreach (i; 0 .. N) {
            ++cnt[cast(int)((A[i] >> e) & 1)];
          }
          debug {
            writeln(cnt);
          }
          const ans = (cnt[0] % 2 == 0) ? (((cnt[1] >> 1) & 1) ^ 1) : 1;
          writeln(ans ? "WIN" : "LOSE");
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

string solve (int n, int [] a)
{
	foreach_reverse (b; 0..30)
	{
		auto z = a.count !(x => (x & (1 << b)) > 0);
		if (z & 1)
		{
			if (z == n && z % 4 == 3)
			{
				return "LOSE";
			}
			return "WIN";
		}
	}
	return "DRAW";
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, a));
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (a.fold !(q{a ^ b}) == 0 ? "DRAW" : "WIN");
	}
}


Imagine that your city is an infinite 2D plane with Cartesian coordinate system. The only crime-affected road of your city is the x-axis. Currently, there are n criminals along the road. No police station has been built on this road yet, so the mayor wants to build one.As you are going to be in charge of this new police station, the mayor has asked you to choose a suitable position (some integer point) for building it. You should choose the best position for the police station, so that you could minimize the total time of your criminal catching mission. Your mission of catching the criminals will operate only from this station. The new station will have only one patrol car. You will go to the criminals by this car, carry them on the car, bring them back to the police station and put them in prison. The patrol car can carry at most m criminals at a time. Note that, the criminals don't know about your mission. So, they will stay where they are instead of running away.Your task is to find the position for the police station, so that total distance you need to cover to catch all the criminals will be minimum possible. Note that, you also can built the police station on the positions where one or more criminals already exist. In such a case all these criminals are arrested instantly.

The first line of the input will have two integers n (1 ≤ n ≤ 106) and m (1 ≤ m ≤ 106) separated by spaces. The next line will contain n integers separated by spaces. The ith integer is the position of the ith criminal on the x-axis. Absolute value of positions will not exceed 109. If a criminal has position x, he/she is located in the point (x, 0) of the plane.  The positions of the criminals will be given in non-decreasing order. Note, that there can be more than one criminal standing at some point of the plane. Note: since the size of the input/output could be very large, don't use slow input/output techniques in your language. For example, do not use input/output streams (cin, cout) in C++.

Print a single integer, that means the minimum possible distance you need to cover to catch all the criminals.

import std.algorithm;
import std.stdio;

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		auto lo = new long [n];
		lo[0] = a[0];
		for (int i = m; i < n; i += m)
		{
			lo[i] = a[i] + lo[i - m];
		}
		auto hi = new long [n];
		hi[n - 1] = a[n - 1];
		for (int i = n - m - 1; i >= 0; i -= m)
		{
			hi[i] = a[i] + hi[i + m];
		}
		long res = long.max;
		foreach (i; 0..n)
		{
			long nlo = (i + m) / m;
			long nhi = (n - i - 1 + m) / m;
			int dlo = i % m;
			int dhi = (n - 1 - i) % m;
			long cur = 0;
			cur += a[i] * nlo - lo[i - dlo];
			cur += hi[i + dhi] - a[i] * nhi;
			res = min (res, cur);
		}
		res *= 2;
		writeln (res);
	}
}


During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know that many participants use random numbers in their programs and are often sent several solutions with the same source code to check.Each participant is identified by some unique positive integer k, and each sent solution A is characterized by two numbers: x — the number of different solutions that are sent before the first solution identical to A, and k — the number of the participant, who is the author of the solution. Consequently, all identical solutions have the same x.It is known that the data in the testing system are stored in the chronological order, that is, if the testing system has a solution with number x (x > 0) of the participant with number k, then the testing system has a solution with number x - 1 of the same participant stored somewhere before.During the competition the checking system crashed, but then the data of the submissions of all participants have been restored. Now the jury wants to verify that the recovered data is in chronological order. Help the jury to do so.

The first line of the input contains an integer n (1 ≤ n ≤ 105) — the number of solutions. Each of the following n lines contains two integers separated by space x and k (0 ≤ x ≤ 105; 1 ≤ k ≤ 105) — the number of previous unique solutions and the identifier of the participant.

A single line of the output should contain «YES» if the data is in chronological order, and «NO» otherwise.

import std.stdio, std.string;

int f[100010];

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        foreach (int i; 0 .. 100001)
        {
            f[i] = -1;
        }
        int succ = 1;
        foreach (int i; 0 .. n)
        {
            int x, k;
            scanf("%d%d", &x, &k);
            if (f[k] + 1 < x)
            {
                succ = 0;
            }
            else if (f[k] + 1 == x)
            {
                ++ f[k];
            }
        }
        if (succ == 1)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
}

import std.stdio;

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

	bool[uint][] nums = new bool[uint][100_000];

	for(uint i = 0; i < n; i++)
	{
		uint x, k;
		readf(" %s %s", &x, &k);

		if(x != 0)
		{
			if(x - 1 !in nums[k])
			{
				writeln("NO");
				return;
			}
		}

		nums[k][x] = true;
	}

	writeln("YES");
}


You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year.

The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$). It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$.

Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.

NoteIn the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times.

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

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

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

    d = d ~ d;

    debug { d.writeln; }

    long sumklast( int mx, int k ) {
        long sm( int k ) {
            return (1 + k).to!long * k / 2;
        }
        return sm( mx ) - sm( mx - k );
    }

    int le = 2*n - 1;
    long tot = 0;
    long ans = 0, cur = 0;
    foreach_reverse ( r; n .. 2*n ) {
        le = min( le, r );

        while ( tot + d[le] <= x ) {
            tot += d[le];
            cur += sumklast( d[le], d[le] );
            --le;
        }

        auto spillover = sumklast( d[le], (x - tot).to!int );

        debug { writeln( r, ' ', cur, ' ', spillover ); }

        ans = max( ans, cur + spillover );

        if ( r - le > 0 ) {
            auto tosub = min( d[r], x ).to!int;
            cur -= sumklast( d[r], tosub );
            tot -= tosub;
        }
    }

    ans.writeln;
}

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

long t (long x)
{
	return x * (x + 1L) / 2;
}

void main ()
{
	int n;
	long x;
	while (readf !(" %s %s") (n, x) > 0)
	{
		readln;
		auto d = readln.splitter.map !(to !(int)).array;
		long res = 0;
		long cur = 0;
		int j = n - 1;
		foreach_reverse (i; 0..n)
		{
			while (d[j] <= x)
			{
				cur += t (d[j]);
				x -= d[j];
				j = (j + n - 1) % n;
			}
			res = max (res, cur + t (d[j]) - t (d[j] - x));
			cur -= t (d[i]);
			x += d[i];
		}
		writeln (res);
	}
}

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

void main(string[] args)
{
  auto n = next!int;
  auto x = next!long;
  auto d = next!long(n);
  if (x == 0) return 0.println;
  int i = 0;
  int j = 0;
  long noDaysInMonth = d[i];
  long hugs = d[i] * (d[i] + 1) / 2;
  long best = long.min;
  while (noDaysInMonth < x)
    {
      j--; if (j == -1) j = n - 1;
      noDaysInMonth += d[j];
      hugs += d[j] * (d[j] + 1) / 2;
    }
  while(i < n)
    {
      auto rem = noDaysInMonth - x;
      auto realHugs = hugs - rem * (rem + 1) / 2;

      debug print("i", i, "j", j, "rem", rem, "realHugs", realHugs);
      debug writeln;

      best = max(realHugs, best);
      i++;
      if (i == n) break;
      noDaysInMonth += d[i];
      hugs += d[i] * (d[i] + 1) / 2;
      while(noDaysInMonth >= x)
	{
	  noDaysInMonth -= d[j];
	  hugs -= d[j] * (d[j] + 1) / 2;
	  j++; if (j == n) j = 0 ;
	}
      j--; if (j < 0) j = n - 1;
      noDaysInMonth += d[j];
      hugs += d[j] * (d[j] + 1) / 2;
    }
  best.writeln;
}

static this()
{
  popChar;
}

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

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

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

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

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

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto x = RD;
	auto d = RDA;

	long ans;
	long cnt;
	long remain = x;
	long[][] log;
	foreach_reverse (_i; 0..n*2)
	{
		auto i = _i % n;
		auto y = d[i] * (d[i]+1) / 2;
		auto z = max(d[i]-remain, 0);
		ans.chmax(cnt + y - z * (z+1) / 2);
		debug writeln("i:", i, " cnt1:", cnt, " remain:", remain, " log:", log);
		if (remain < d[i])
		{
			(){
			while (remain < d[i])
			{
				if (log.empty)
				{
					cnt = 0;
					auto zz = max(d[i]-x, 0);
					cnt    += y - zz * (zz+1) / 2;
					ans.chmax(cnt);
					cnt = 0;
					remain = x;
					debug writeln("i:", i, " cnt2:", cnt);
					return;
				}
				else
				{
					auto l = log.front; log.popFront;
					remain += l[1];
					cnt    -= l[0];
					auto zz = max(d[i]-remain, 0);
					if (remain > 0)
						ans.chmax(cnt + y - zz * (zz+1) / 2);
					debug writeln("i:", i, " cnt3:", cnt, " ans:", ans);
				}
			}
			cnt += y;
			ans.chmax(cnt);
			remain -= d[i];
			log ~= [y, d[i]];
			}();
		}
		else
		{
			cnt    += y;
			remain -= d[i];
			log    ~= [y, d[i]];
		}
		debug writeln("ans:", ans);
	}

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

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

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

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

    d = d ~ d;

    debug { d.writeln; }

    long sumklast( int mx, int k ) {
        long sm( int k ) {
            return (1 + k).to!long * k / 2;
        }
        return sm( mx ) - sm( mx - k );
    }

    int le = 2*n - 1, tot = 0;
    long ans = 0, cur = 0;
    foreach_reverse ( r; n .. 2*n ) {
        while ( tot + d[le] <= x ) {
            tot += d[le];
            cur += sumklast( d[le], d[le] );
            --le;
        }

        auto spillover = sumklast( d[le], x - tot );

        debug { writeln( r, ' ', cur, ' ', spillover ); }

        ans = max( ans, cur + spillover );

        auto tosub = min( d[r], x );
        cur -= sumklast( d[r], tosub );
        tot -= tosub;
    }

    ans.writeln;
}

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

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

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

    d = d ~ d;

    debug { d.writeln; }

    long sumklast( int mx, int k ) {
        long sm( int k ) {
            return (1 + k).to!long * k / 2;
        }
        return sm( mx ) - sm( mx - k );
    }

    int le = 2*n - 1;
    long tot = 0;
    long ans = 0, cur = 0;
    foreach_reverse ( r; n .. 2*n ) {
        while ( tot + d[le] <= x ) {
            tot += d[le];
            cur += sumklast( d[le], d[le] );
            --le;
        }

        auto spillover = sumklast( d[le], (x - tot).to!int );

        debug { writeln( r, ' ', cur, ' ', spillover ); }

        ans = max( ans, cur + spillover );

        auto tosub = min( d[r], x ).to!int;
        cur -= sumklast( d[r], tosub );
        tot -= tosub;
    }

    ans.writeln;
}

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

void main(string[] args)
{
  auto n = next!int;
  auto x = next!int;
  auto d = next!long(n);
  if (x == 0) return 0.println;
  int i = 0;
  int j = 0;
  long noDaysInMonth = d[i];
  long hugs = d[i] * (d[i] + 1) / 2;
  long best = long.min;
  while (noDaysInMonth < x)
    {
      j--; if (j == -1) j = n - 1;
      noDaysInMonth += d[j];
      hugs += d[j] * (d[j] + 1) / 2;
    }
  while(i < n)
    {
      auto rem = noDaysInMonth - x;
      auto realHugs = hugs - rem * (rem + 1) / 2;

      debug print("i", i, "j", j, "rem", rem, "realHugs", realHugs);
      debug writeln;

      best = max(realHugs, best);
      i++;
      if (i == n) break;
      noDaysInMonth += d[i];
      hugs += d[i] * (d[i] + 1) / 2;
      while(noDaysInMonth >= x)
	{
	  noDaysInMonth -= d[j];
	  hugs -= d[j] * (d[j] + 1) / 2;
	  j++; if (j == n) j = 0 ;
	}
      j--; if (j < 0) j = n - 1;
      noDaysInMonth += d[j];
      hugs += d[j] * (d[j] + 1) / 2;
    }
  best.writeln;
}

static this()
{
  popChar;
}

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

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

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

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

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

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


For an array $$$a$$$ of integers let's denote its maximal element as $$$\max(a)$$$, and minimal as $$$\min(a)$$$. We will call an array $$$a$$$ of $$$k$$$ integers interesting if $$$\max(a) - \min(a) \ge k$$$. For example, array $$$[1, 3, 4, 3]$$$ isn't interesting as $$$\max(a) - \min(a) = 4 - 1 = 3 < 4$$$ while array $$$[7, 3, 0, 4, 3]$$$ is as $$$\max(a) - \min(a) = 7 - 0 = 7 \ge 5$$$.You are given an array $$$a$$$ of $$$n$$$ integers. Find some interesting nonempty subarray of $$$a$$$, or tell that it doesn't exist.An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

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

For each test case, output "NO" in a separate line if there is no interesting nonempty subarray in $$$a$$$.  Otherwise, output "YES" in a separate line. In the next line, output two integers $$$l$$$ and $$$r$$$ ($$$1\le l \le r \le n$$$) — bounds of the chosen subarray. If there are multiple answers, print any. You can print each letter in any case (upper or lower).

NoteIn the second test case of the example, one of the interesting subarrays is $$$a = [2, 0, 1, 9]$$$: $$$\max(a) - \min(a) = 9 - 0 = 9 \ge 4$$$.

void main() {
	auto t = ri;
	foreach(_; 0..t) {
		auto n = ri;
		auto a = readAs!(int[]);
		bool flag = false;
		foreach(i; 0..n-1) {
			if(abs(a[i] - a[i+1]) >= 2) {
				writeln("YES");
				writefln("%d %d", i+1, i+2);
				flag = true;
				break;
			}
		}
		if(!flag) writeln("NO");
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

import std.stdio;
import std.array;
import std.range : enumerate, slide;
import std.algorithm : map;
import std.math : abs;
import std.string : strip, split;
import std.conv : to;

void main()
{
  int t;
  readf!"%d\n"(t);

  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);
    auto a = readln.strip.split.map!(to!int);
    auto found = false;
    // an array is interesting if max(a) - min(a) >= len(a)
    // find an interesting nonempty subarray if it exists
    foreach (l, w; a.slide(2).enumerate) {
      if (abs(w[0] - w[1]) >= 2) {
	writefln("YES\n%d %d", l+1, l+2);
	found = true;
	break;
      }
    }

    if (!found) writeln("NO");
  }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
multitest_loop:
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		foreach (i; 1..n)
		{
			if (abs (a[i] - a[i - 1]) > 1)
			{
				writeln ("YES");
				writeln (i, " ", i + 1);
				continue multitest_loop;
			}
		}
		writeln ("NO");
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

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

		foreach(i; 0 .. n - 1){
			long a = as[i], b = as[i + 1];
			if(abs(b - a) >= 2){
				"YES".writeln;
				writeln(i + 1, " ", i + 2);
				continue A;
			}
		}

		"NO".writeln;

	}

}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		int l, r;
		foreach (i; 0..n)
		{
			auto d = a[i] - a[l];
			auto len = i-l+1;
			if (d >= len)
			{
				r = i;
				break;
			}
			else if (a[i] < a[l]+len)
			{
				l = i;
			}
		}
		if (r != 0)
		{
			ans[ti] = [l, r];
			continue;
		}
		l = 0;
		foreach (i; 0..n)
		{
			auto d = a[l] - a[i];
			auto len = i-l+1;
			if (d >= len)
			{
				r = i;
				break;
			}
			else if (a[i] > a[l]-len)
			{
				l = i;
			}
		}
		if (r != 0)
		{
			ans[ti] = [l, r];
			continue;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln("NO");
		else
		{
			writeln("YES");
			writeln(e[0]+1, " ", e[1]+1);
		}
	}
	stdout.flush;
	debug readln;
}


Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with m pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2 × 2 square consisting of black pixels is formed. Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers i and j, denoting respectively the row and the column of the pixel to be colored on the current move.Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 × 2 square consisting of black pixels is formed.

The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 105) — the number of rows, the number of columns and the number of moves that Pasha is going to perform.  The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n, 1 ≤ j ≤ m), representing the row number and column number of the pixel that was painted during a move.

If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed. If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves, print 0.

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

int M, N, K;
int[] X, Y;

int solve() {
	auto col = new bool[][](M + 2, N + 2);
	foreach (k; 0 .. K) {
		col[X[k]][Y[k]] = true;
		foreach (x; X[k] - 1 .. X[k] + 1) foreach (y; Y[k] - 1 .. Y[k] + 1) {
			if (col[x][y] && col[x][y + 1] && col[x + 1][y] && col[x + 1][y + 1]) {
				return k + 1;
			}
		}
	}
	return 0;
}

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		N = readInt;
		K = readInt;
		X = new int[K];
		Y = new int[K];
		foreach (k; 0 .. K) {
			X[k] = readInt;
			Y[k] = readInt;
		}

		auto res = solve;
		writeln(res);

	}
	} catch (EOFException) {}
}


Today the kindergarten has a new group of $$$n$$$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $$$1$$$ to $$$4n$$$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $$$a$$$ and $$$b$$$ ($$$a \neq b$$$) will indulge if:   $$$gcd(a, b) = 1$$$ or,  $$$a$$$ divides $$$b$$$ or $$$b$$$ divides $$$a$$$. $$$gcd(a, b)$$$ — the maximum number $$$x$$$ such that $$$a$$$ is divisible by $$$x$$$ and $$$b$$$ is divisible by $$$x$$$.For example, if $$$n=3$$$ and the kids sit on chairs with numbers $$$2$$$, $$$3$$$, $$$4$$$, then they will indulge since $$$4$$$ is divided by $$$2$$$ and $$$gcd(2, 3) = 1$$$. If kids sit on chairs with numbers $$$4$$$, $$$6$$$, $$$10$$$, then they will not indulge.The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $$$2$$$ of the kid that can indulge. More formally, she wants no pair of chairs $$$a$$$ and $$$b$$$ that the kids occupy to fulfill the condition above.Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of kids.

Output $$$t$$$ lines, which contain $$$n$$$ distinct integers from $$$1$$$ to $$$4n$$$ — the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $$$n$$$ numbers in any order.

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto used = new bool[](n*4+1);
		foreach_reverse (i; 2..n*2+1)
		{
			auto x = 2 * i;
			bool ok = true;
			foreach (j; 2..n*2+1)
			{
				auto y = x * j;
				if (y > n*4) break;
				if (used[y])
				{
					ok = false;
					break;
				}
			}
			if (!ok) continue;
			ans[ti] ~= x;
			used[x] = true;
			if (ans[ti].length == n) break;
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto used = new bool[](n*4+1);
		foreach (i; 2..n*2+1)
		{
			auto x = 2 * i;
			if (used[i]) continue;
			ans[ti] ~= x;
			if (ans[ti].length == n) break;
			foreach (j; 1..n*2)
			{
				auto y = x * j;
				if (y > n*4) break;
				used[y] = true;
			}
		}
	}

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


Note that the only differences between easy and hard versions are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions are solved.Slime is interested in sequences. He defined good positive integer sequences $$$p$$$ of length $$$n$$$ as follows: For each $$$k>1$$$ that presents in $$$p$$$, there should be at least one pair of indices $$$i,j$$$, such that $$$1 \leq i < j \leq n$$$, $$$p_i = k - 1$$$ and $$$p_j = k$$$.For the given integer $$$n$$$, the set of all good sequences of length $$$n$$$ is $$$s_n$$$. For the fixed integer $$$k$$$ and the sequence $$$p$$$, let $$$f_p(k)$$$ be the number of times that $$$k$$$ appears in $$$p$$$. For each $$$k$$$ from $$$1$$$ to $$$n$$$, Slime wants to know the following value:$$$$$$\left(\sum_{p\in s_n} f_p(k)\right)\ \textrm{mod}\ 998\,244\,353$$$$$$

The first line contains one integer $$$n\ (1\le n\le 5000)$$$.

Print $$$n$$$ integers, the $$$i$$$-th of them should be equal to $$$\left(\sum_{p\in s_n} f_p(i)\right)\ \textrm{mod}\ 998\,244\,353$$$.

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

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;

enum LIM = 5010;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
  } else {
    return Mint(0);
  }
}

void main() {
  prepare;
  auto eul = new Mint[][](LIM, LIM);
  foreach (n; 0 .. LIM) {
    eul[n][0] = 0;
    eul[n][n] = 1;
    foreach (k; 1 .. n) {
      eul[n][k] = (n - k + 1) * eul[n - 1][k - 1] + k * eul[n - 1][k];
    }
  }
  debug {
    foreach (n; 0 .. 10) {
      writeln(n, ": ", eul[n][0 .. 10]);
    }
  }

  try {
    for (; ; ) {
      const N = readInt();

      auto ans = new Mint[N + 1];
      foreach (n; 1 .. N + 1) {
        foreach (k; 1 .. n + 1) {
          ans[k] += eul[n][k] * invFac[n] * fac[N];
        }
      }

      foreach (k; 1 .. N + 1) {
        if (k > 1) write(" ");
        write(ans[k]);
      }
      writeln;
    }
  } catch (EOFException e) {
  }
}


Let $$$f(x)$$$ be the sum of digits of a decimal number $$$x$$$.Find the smallest non-negative integer $$$x$$$ such that $$$f(x) + f(x + 1) + \dots + f(x + k) = n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 150$$$) — the number of test cases. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 150$$$, $$$0 \le k \le 9$$$).

For each test case, print one integer without leading zeroes. If there is no such $$$x$$$ that $$$f(x) + f(x + 1) + \dots + f(x + k) = n$$$, print $$$-1$$$; otherwise, print the minimum $$$x$$$ meeting that constraint.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		k += 1;
		long res = long.max;
		foreach (d; 0..10)
		{
			foreach (nines; 0..20)
			{
				int cur = d;
				int total = 0;
				bool wrapped = false;
				foreach (i; 0..k)
				{
					total += cur;
					if (!wrapped)
					{
						total += 9 * nines;
					}
					cur += 1;
					if (cur % 10 == 0)
					{
						cur -= 9;
						wrapped = true;
					}
				}
				if (total <= n && (n - total) % k == 0)
				{
					int add = (n - total) / k;
					char [] s;
					s ~= d.text;
					foreach (i; 0..nines)
					{
						s ~= '9';
					}
					int hi = 8;
					while (add > 0)
					{
						s ~= text (min (hi, add));
						add -= min (hi, add);
						hi = 9;
					}
					reverse (s);
					auto temp = s.to !(long);
					res = min (res, temp);
				}
			}
		}
		if (res == long.max)
		{
			res = -1;
		}
		writeln (res);
	}
}


Let us define a magic grid to be a square matrix of integers of size $$$n \times n$$$, satisfying the following conditions.   All integers from $$$0$$$ to $$$(n^2 - 1)$$$ inclusive appear in the matrix exactly once.  Bitwise XOR of all elements in a row or a column must be the same for each row and column. You are given an integer $$$n$$$ which is a multiple of $$$4$$$. Construct a magic grid of size $$$n \times n$$$.

The only line of input contains an integer $$$n$$$ ($$$4 \leq n \leq 1000$$$). It is guaranteed that $$$n$$$ is a multiple of $$$4$$$.

Print a magic grid, i.e. $$$n$$$ lines, the $$$i$$$-th of which contains $$$n$$$ space-separated integers, representing the $$$i$$$-th row of the grid. If there are multiple answers, print any. We can show that an answer always exists.

NoteIn the first example, XOR of each row and each column is $$$13$$$.In the second example, XOR of each row and each column is $$$60$$$.

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;

	int[][] zf = [
		[8, 9, 1, 13],
		[3, 12, 7, 5],
		[0, 2, 4, 11],
		[6, 10, 15, 14]
	];

	int[][] xf = new int[][](n, n);
	foreach(i; 0 .. n) foreach(j; 0 .. n){
		int zi = i % 4, zj = j % 4;
		int wi = i / 4, wj = j / 4;
		int w = wi * (n / 4) + wj;
		xf[i][j] = w * 16 + zf[zi][zj];
	}

	log("check i:", n.iota.map!((i){ int res = 0; foreach(j; 0 .. n) res ^= xf[i][j]; return res;}));
	log("check j:", n.iota.map!((j){ int res = 0; foreach(i; 0 .. n) res ^= xf[i][j]; return res;}));

	foreach(i; 0 .. n) xf[i].map!(to!string).array.join(" ").writeln;

}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();

      auto a = new int[][](N, N);
      foreach (x; 0 .. N) foreach (y; 0 .. N) {
        a[x][y] = (y / 4) * (4 * N) + x * 4 + (y % 4);
      }

      debug {
        foreach (x; 0 .. N) {
          int sum;
          foreach (y; 0 .. N) {
            sum ^= a[x][y];
          }
          writeln("row ", x, ": ", sum);
        }
        foreach (y; 0 .. N) {
          int sum;
          foreach (x; 0 .. N) {
            sum ^= a[x][y];
          }
          writeln("col ", y, ": ", sum);
        }
      }

      foreach (x; 0 .. N) {
        foreach (y; 0 .. N) {
          if (y > 0) {
            write(" ");
          }
          write(a[x][y]);
        }
        writeln();
      }
    }
  } catch (EOFException e) {
  }
}


You wrote down all integers from $$$0$$$ to $$$10^n - 1$$$, padding them with leading zeroes so their lengths are exactly $$$n$$$. For example, if $$$n = 3$$$ then you wrote out 000, 001, ..., 998, 999.A block in an integer $$$x$$$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.For example, in the integer $$$00027734000$$$ there are three blocks of length $$$1$$$, one block of length $$$2$$$ and two blocks of length $$$3$$$.For all integers $$$i$$$ from $$$1$$$ to $$$n$$$ count the number of blocks of length $$$i$$$ among the written down integers.Since these integers may be too large, print them modulo $$$998244353$$$.

The only line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).

In the only line print $$$n$$$ integers. The $$$i$$$-th integer is equal to the number of blocks of length $$$i$$$. Since these integers may be too large, print them modulo $$$998244353$$$.

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

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

void solve(){
    int n = scan!int;

    Finite.mod = 998244353;

    Finite[] ans;
    foreach(i; 1 .. n + 1){
        if(i == n) ans ~= Finite(10);
        else ans ~= power(10, n - i) * 18 + 81 * (n - i - 1) * power(10, n - i - 1);
    }

    ans.unsplit.print;
}

struct Finite{
	long value; static long mod = 1_000_000_007;
	this(long v){ value = val(v); }
	static long val(long v){ return (v + mod  - (v / mod) * mod) % mod; }
	bool opCast(T: bool)(){ return value != 0; }
	bool opEquals(Finite b){ return(value == b.value); }
	string toString(){ return value.to!string; }

	Finite opUnary(string s){ long v;
		if(s == "+") v = value;
		else if(s == "-") v = mod - value;
		else if(s == "++") v = value + 1;
		else if(s == "--") v = value + mod - 1;
		else assert(0, "Operator unary " ~ s ~ " not implemented");
		return Finite(v);
	}

	Finite opBinary(string s)(Finite b){ return opBinary!s(b.value); }
	Finite opBinary(string s)(long u){ long v;
		if(s == "+") v = value + u;
		else if(s == "-") v = value + mod - u;
		else if(s == "*") v = value * u;
		else if(s == "/") v = value * inv(u);
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opBinaryRight(string s)(long u){ long v;
		if(s == "+") v = u + value;
		else if(s == "-") v = u + mod - value;
		else if(s == "*") v = u * value;
		else if(s == "/") v = u * invvalue;
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opAssign(long v){ value = v; return this; }

	Finite opOpAssign(string s)(Finite b){ return opOpAssign!s(b.value); }
	Finite opOpAssign(string s)(long v){
		if(s == "+") value = (value + v) % mod;
		else if(s == "-") value = (value + mod - v) % mod;
		else if(s == "*") value = (value * v) % mod;
		else if(s == "/") value = (value * inv(v)) % mod;
		else assert(0, "Operator " ~ s ~ "= not implemented");
		return this;
	}

	private static long[] _inv = [0, 1];
	long invvalue(){ return inv(value); }
	long inv(long v){ int i = val(v).to!int;
		while(i >= _inv.length){
			_inv ~= _inv[(mod % $).to!int] * (mod - mod / _inv.length) % mod;
		}
		return _inv[i];
	}

}

// ---以下はユーティリティ--- //

/// mod p における累乗
/// 例 power(5, 3) = Finite(125)
/// オーダーは a ** b を計算する場合 O(log b)
Finite power(long a, long b){
	Finite m = Finite(a);
	Finite ans = Finite(1);
	while(b > 0){
		if(b % 2) ans *= m;
		m *= m;
		b /= 2;
	}
	return ans;
}
Finite power(Finite a, long b){ return power(a.value, b); }


You are given $$$n$$$ sticks with positive integral length $$$a_1, a_2, \ldots, a_n$$$.You can perform the following operation any number of times (possibly zero):   choose one stick, then either increase or decrease its length by $$$1$$$. After each operation, all sticks should have positive lengths. What is the minimum number of operations that you have to perform such that it is possible to select three of the $$$n$$$ sticks and use them without breaking to form an equilateral triangle?An equilateral triangle is a triangle where all of its three sides have the same length.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 300$$$) — the number of sticks. 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 lengths of the sticks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$300$$$.

For each test case, print one integer on a single line — the minimum number of operations to be made.

NoteIn the first test case, you can increase the length of the first stick by $$$1$$$, then decrease the length of the third stick by $$$1$$$. In total, you perform $$$2$$$ operations, such that the three sticks form an equilateral triangle of side length $$$2$$$.In the fourth test case, you can decrease the length of the seventh stick by $$$1$$$. An equilateral triangle of side length $$$1$$$ can be selected and formed by the second, fourth, and seventh sticks.

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

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void solve() {
    int N = readln.chomp.to!int;
    int[] A = readln.chomp.split(" ").map!(to!int).array.sort.array;
    int ans = int.max;
    for (int i = 1; i + 1 < N; i++) {
        // form an equilateral triangul of length A[i]
        int L = A[i];
        auto X = abs(L - A[i-1]) + abs(A[i+1] - L);
        ans = min(X, ans);
    }
    writeln(ans);
}

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