$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nobody wins the fight. When two heroes of different levels fight, the one with the higher level wins, and his level increases by $$$1$$$.The winner of the tournament is the first hero that wins in at least $$$100^{500}$$$ fights (note that it's possible that the tournament lasts forever if no hero wins this number of fights, then there is no winner). A possible winner is a hero such that there exists a sequence of fights that this hero becomes the winner of the tournament.Calculate the number of possible winners among $$$n$$$ heroes.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the number of heroes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$), where $$$a_i$$$ is the initial level of the $$$i$$$-th hero.

For each test case, print one integer — the number of possible winners among the given $$$n$$$ heroes.

NoteIn the first test case of the example, the only possible winner is the first hero.In the second test case of the example, each fight between the heroes results in nobody winning it, so the tournament lasts forever and there is no winner.

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        int[] aa; get(aa);
        int min_a = int.max, c;
        foreach (a; aa) {
            if (min_a > a) {
                min_a = a;
                c = 1;
            } else if (a == min_a) {
                ++c;
            }
        }
        writeln(N - c);
    }
}

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 (n - a.count (a.minElement));
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		foreach (i; 0..n)
		{
			if (a[i] != a[0])
			{
				ans[ti] = n - i;
				break;
			}
		}
	}

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


A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].After each query you have to determine whether the number of inversions is odd or even.

The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.  The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct. The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process. Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.

NoteThe first example:  after the first query a = [2, 1, 3], inversion: (2, 1);  after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2). The second example:  a = [1, 2, 4, 3], inversion: (4, 3);  a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);  a = [1, 2, 4, 3], inversion: (4, 3);  a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    int ans = 0;

    foreach (i; 0..N) {
        foreach (j; i+1..N) {
            if (A[i] > A[j]) {
                ans ^= 1;
            }
        }
    }

    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto lr = readln.split.map!(to!int);
        auto d = lr[1] - lr[0] + 1;
        ans ^= (d * (d - 1) / 2) % 2;
        writeln(ans%2 ? "odd" : "even");
    }

}

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

alias Pair = Tuple!(dchar, "col", int, "len");

void main() {
    int n, m;
    scan(n);
    auto a = readln.split.to!(int[]);
    scan(m);

    int inv;
    foreach (i ; 0 .. n) {
        foreach (j ; i + 1 .. n) {
            if (a[i] > a[j]) inv++;
        }
    }

    inv &= 1;

    foreach (i ; 0 .. m) {
        int l, r;
        scan(l, r);
        inv ^= ((r - l + 1) * (r - l) / 2) & 1;
        writeln(inv ? "odd" : "even");
    }
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?

The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.

Print a single integer — the minimum possible valid width of the road.

NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.

import std.stdio;

void main()
{
	int n, h, x;
	int result = 0;

	scanf("%d%d", &n, &h);
	for (int i = 0; i < n; i++) {
		scanf("%d", &x);
		result += (x > h) ? 2 : 1;
	}
	printf("%d\n", result);
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.iteration: uniq, map, sum;
import std.algorithm.mutation: copy;
import std.algorithm.sorting: sort;
import std.regex;
import std.conv;
import std.array;

void main()
{
  auto nh = stdin.readln.strip.split(regex(` +`)).map!(to!int).array;
  auto as = stdin.readln.strip.split(regex(` +`)).map!(to!int);
  auto h = nh[1];
  writeln (as.map!(a=>a<=h?1:2).sum);
}

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

int main(string[] argv)
{
	int n, h;
	scanf("%d %d", &n, &h);
	int width = 0;
	foreach (int i; 0..n)
	{
		int fh;
		scanf("%d", &fh);
		width += (fh > h ? 2 : 1);
	}
	printf("%d", width);
	return 0;
}

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

void main(){

	int n, h, x;
	int result = 0;

	scanf("%d%d", &n, &h);
	for (int i = 0; i < n; i++){
		scanf("%d", &x);
		result += (x > h) ? 2 : 1;
	}

	printf("%d\n", result);
}

import std.algorithm;
import std.conv;
import std.stdio;

void main() {
  int n, h;
  readf!" %s %s "(n, h);
  auto ans = readln.splitter
    .map!(to!int)
    .map!(x => x > h ? 2 : 1)
    .sum;
  writeln(ans);
}

import std.algorithm;
import std.conv;
import std.stdio;

void main() {
  int n, h;
  readf!" %s %s "(n, h);
  // auto ans = readln.splitter
  //   .map!(to!int)
  //   .map!(x => x > h ? 2 : 1)
  //   .sum;
  // writeln(ans);
  int ans = 0;
  foreach (i; 0 .. n) {
    int x;
    readf!" %s"(x);
    ans += 1;
    if (x > h) {
      ans += 1;
    }
  }
  writeln(ans);
}

import std.stdio;

int main()
{
	int a=0;
	int b=0;
	readf(" %d", &a);
	readf(" %d", &b);
	int count=0;
	for (int i=0; i<a; i++)
	{
		int c=0;
		readf(" %d", &c);
		if (c>b)
		{
			count=count+1;
		}
	}
	writeln(a+count);
	return 0;
}


This is an interactive task.Scientists are about to invent a new optimization for the Floyd-Warshall algorithm, which will allow it to work in linear time. There is only one part of the optimization still unfinished.It is well known that the Floyd-Warshall algorithm takes a graph with $$$n$$$ nodes and exactly one edge between each pair of nodes. The scientists have this graph, what is more, they have directed each edge in one of the two possible directions.To optimize the algorithm, exactly $$$m$$$ edges are colored in pink color and all the rest are colored in green. You know the direction of all $$$m$$$ pink edges, but the direction of green edges is unknown to you. In one query you can ask the scientists about the direction of exactly one green edge, however, you can perform at most $$$2 \cdot n$$$ such queries.Your task is to find the node from which every other node can be reached by a path consisting of edges of same color. Be aware that the scientists may have lied that they had fixed the direction of all edges beforehand, so their answers may depend on your queries.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100\,000$$$, $$$0 \le m \le 100\,000$$$) — the number of nodes and the number of pink edges. The next $$$m$$$ lines describe the pink edges, the $$$i$$$-th of these lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$) — the start and the end of the $$$i$$$-th pink edge. It is guaranteed, that all unordered pairs $$$(u_i, v_i)$$$ are distinct.

When you found the answer, print "!" and the number of the node from which every other node can be reached by a single-colored path.

NoteIn the example above the answer for the query "? 1 3" is 0, so the edge is directed from 3 to 1. The answer for the query "? 4 2" is 1, so the edge is directed from 4 to 2. The answer for the query "? 3 2" is 1, so the edge is directed from 3 to 2. So there are green paths from node 3 to nodes 1 and 2 and there is a pink path from node 3 to node 4.

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

int Ask(int u, int v) {
  writefln("? %s %s", u + 1, v + 1);
  stdout.flush;
  const res = readInt();
  if (res == -1) {
    import core.stdc.stdlib;
    exit(0);
  }
  return res;
}
void Answer(int u) {
  writefln("! %s", u + 1);
  stdout.flush;
}

int N, M;
int[] U, V;

int[][][] G;
int C;
int[] scc;
int[] post;

void dfs(int s, int u) {
  scc[u] = C;
  foreach (v; G[s][u]) {
    if (scc[v] == ((s == 0) ? -1 : -2)) {
      dfs(s, v);
    }
  }
  if (s == 0) {
    post ~= u;
  }
}

void main() {
  N = readInt();
  M = readInt();
  U = new int[M];
  V = new int[M];
  foreach (i; 0 .. M) {
    U[i] = readInt() - 1;
    V[i] = readInt() - 1;
  }

  G = new int[][][](2, N);
  foreach (i; 0 .. M) {
    G[0][U[i]] ~= V[i];
    G[1][V[i]] ~= U[i];
  }
  C = -2;
  scc = new int[N];
  scc[] = -1;
  foreach (u; 0 .. N) {
    if (scc[u] == -1) {
      dfs(0, u);
    }
  }
  C = 0;
  foreach_reverse (u; post) {
    if (scc[u] == -2) {
      dfs(1, u);
      ++C;
    }
  }
  debug {
    writeln("scc = ", scc);
  }

  auto H = new int[][C];
  auto inDeg = new int[C];
  foreach (i; 0 .. M) {
    const c = scc[U[i]], d = scc[V[i]];
    if (c != d) {
      H[c] ~= d;
      ++inDeg[d];
    }
  }

  auto ls = new DList!int[C];
  foreach (u; 0 .. N) {
    ls[scc[u]].insertBack(u);
  }
  DList!int q;
  foreach (c; 0 .. C) {
    if (inDeg[c] == 0) {
      q.insertBack(c);
    }
  }

  int src = ls[q.front].front;
  for (; ; ) {
    debug {
      writeln("ls = ", ls.map!(l => l[]));
      writeln("q = ", q[]);
    }
    if (q.front == scc[src]) {
      const c0 = q.front; q.removeFront;
      if (q.empty) {
        break;
      }
      const c1 = q.front; q.removeFront;
      q.insertFront(c0);
      q.insertFront(c1);
    }
    int u = ls[q.front].front;
    if (Ask(src, u) == 0) {
      swap(src, u);
    }
    const c = scc[u];
    if (q.front != c) {
      const c0 = q.front; q.removeFront;
      const c1 = q.front; q.removeFront;
      q.insertFront(c0);
      q.insertFront(c1);
    }
    ls[c].removeFront;
    if (ls[c].empty) {
      q.removeFront;
      foreach (d; H[c]) {
        if (--inDeg[d] == 0) {
          q.insertBack(d);
        }
      }
    }
  }
  Answer(src);
}

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

int Ask(int u, int v) {
  writefln("? %s %s", u + 1, v + 1);
  stdout.flush;
  const res = readInt();
  if (res == -1) {
    import core.stdc.stdlib;
    exit(0);
  }
  return res;
}
void Answer(int u) {
  writefln("! %s", u + 1);
  stdout.flush;
}

int N, M;
int[] U, V;

int[][][] G;
int C;
int[] scc;
int[] post;

void dfs(int s, int u) {
  scc[u] = C;
  foreach (v; G[s][u]) {
    if (scc[v] == ((s == 0) ? -1 : -2)) {
      dfs(s, v);
    }
  }
  if (s == 0) {
    post ~= u;
  }
}

void main() {
  N = readInt();
  M = readInt();
  U = new int[M];
  V = new int[M];
  foreach (i; 0 .. M) {
    U[i] = readInt() - 1;
    V[i] = readInt() - 1;
  }

  G = new int[][][](2, N);
  foreach (i; 0 .. M) {
    G[0][U[i]] ~= V[i];
    G[1][V[i]] ~= U[i];
  }
  C = -2;
  scc = new int[N];
  scc[] = -1;
  foreach (u; 0 .. N) {
    if (scc[u] == -1) {
      dfs(0, u);
    }
  }
  C = 0;
  foreach_reverse (u; post) {
    if (scc[u] == -2) {
      dfs(1, u);
      ++C;
    }
  }
  debug {
    writeln("scc = ", scc);
  }

  auto H = new int[][C];
  auto inDeg = new int[C];
  foreach (i; 0 .. M) {
    const c = scc[U[i]], d = scc[U[i]];
    if (c != d) {
      H[c] ~= d;
      ++inDeg[d];
    }
  }

  auto ls = new DList!int[C];
  foreach (u; 0 .. N) {
    ls[scc[u]].insertBack(u);
  }
  DList!int q;
  foreach (c; 0 .. C) {
    if (inDeg[c] == 0) {
      q.insertBack(c);
    }
  }
  debug {
    writeln("ls = ", ls.map!(l => l[]));
    writeln("q = ", q[]);
  }

  int src = ls[q.front].front;
  ls[q.front].removeFront;
  if (ls[q.front].empty) {
    const c = q.front;
    q.removeFront;
    foreach (d; H[c]) {
      if (--inDeg[d] == 0) {
        q.insertBack(d);
      }
    }
  }
  for (; !q.empty; ) {
    if (scc[src] == q.front) {
      const c0 = q.front; q.removeFront;
      const c1 = q.front; q.removeFront;
      q.insertFront(c0);
      q.insertFront(c1);
    }
    const c = q.front;
    const u = ls[c].front;
    ls[c].removeFront;
    if (Ask(src, u) == 0) {
      src = u;
    }
    if (ls[c].empty) {
      q.removeFront;
      foreach (d; H[c]) {
        if (--inDeg[d] == 0) {
          q.insertBack(d);
        }
      }
    }
  }
  Answer(src);
}


There are $$$n$$$ astronauts working on some space station. An astronaut with the number $$$i$$$ ($$$1 \le i \le n$$$) has power $$$a_i$$$.An evil humanoid has made his way to this space station. The power of this humanoid is equal to $$$h$$$. Also, the humanoid took with him two green serums and one blue serum.In one second , a humanoid can do any of three actions:  to absorb an astronaut with power strictly less humanoid power;  to use green serum, if there is still one left;  to use blue serum, if there is still one left. When an astronaut with power $$$a_i$$$ is absorbed, this astronaut disappears, and power of the humanoid increases by $$$\lfloor \frac{a_i}{2} \rfloor$$$, that is, an integer part of $$$\frac{a_i}{2}$$$. For example, if a humanoid absorbs an astronaut with power $$$4$$$, its power increases by $$$2$$$, and if a humanoid absorbs an astronaut with power $$$7$$$, its power increases by $$$3$$$.After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by $$$2$$$ times.After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by $$$3$$$ times.The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally.

The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains integers $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of astronauts and $$$h$$$ ($$$1 \le h \le 10^6$$$) — the initial power of the humanoid. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^8$$$) — powers of astronauts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.

NoteIn the first case, you can proceed as follows:   use green serum. $$$h = 1 \cdot 2 = 2$$$  absorb the cosmonaut $$$2$$$. $$$h = 2 + \lfloor \frac{1}{2} \rfloor = 2$$$  use green serum. $$$h = 2 \cdot 2 = 4$$$  absorb the spaceman $$$1$$$. $$$h = 4 + \lfloor \frac{2}{2} \rfloor = 5$$$  use blue serum. $$$h = 5 \cdot 3 = 15$$$  absorb the spaceman $$$3$$$. $$$h = 15 + \lfloor \frac{8}{2} \rfloor = 19$$$  absorb the cosmonaut $$$4$$$. $$$h = 19 + \lfloor \frac{9}{2} \rfloor = 23$$$

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

int solve(long[] a, long h, long[] mulorder)
{
  int i = 0;
  while (i < a.length) {
    if (a[i] < h) {
      h += a[i] / 2;
      i++;
      continue;
    }
    if (mulorder.length > 0) {
      h *= mulorder.front;
      mulorder = mulorder[1 .. $];
      continue;
    }
    break;
  }
  return i;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n, h;
    readf!" %d %d "(n, h);
    auto a = readln.splitter.map!(to!long).array.sort.array;
    int best = -1;
    foreach (p ; [2L, 2L, 3L].permutations) {
      best = max(best, solve(a, h, p.array));
    }
    writeln(best);
  }
}

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

int solve (int n, long h, int [] a, int [] p)
{
	foreach (int k, ref c; a)
	{
		while (h <= c && !p.empty)
		{
			h *= p.front;
			p.popFront ();
		}
		if (h <= c)
		{
			return k;
		}
		h += c / 2;
	}
	return n;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, h;
		readf !(" %s %s") (n, h);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);

		auto p = [2, 2, 3];
		int res = 0;
		do
		{
			res = max (res, solve (n, h, a, p));
		}
		while (nextPermutation (p));
		writeln (res);
	}
}

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

int solve1(long[] a, long h)
{
    int mul2cnt = 2;
    int mul3cnt = 1;
    int i = 0;
    while (i < a.length) {
      if (a[i] < h) {
        h += a[i] / 2;
      } else if (a[i] < h * 2 && mul2cnt > 0) {
        h *= 2;
        h += a[i] / 2;
        mul2cnt--;
      } else if (a[i] < h * 3 && mul3cnt > 0) {
        h *= 3;
        h += a[i] / 2;
        mul3cnt--;
      } else {
        if (mul2cnt > 0) {
          h *= 2;
          mul2cnt--;
          continue;
        }
        if (mul3cnt > 0) {
          h *= 3;
          mul3cnt--;
          continue;
        }
        break;
      }
      i++;
    }
    return i;
}

int solve2(long[] a, long h)
{
    int mul2cnt = 2;
    int mul3cnt = 1;
    int i = 0;
    while (i < a.length) {
      if (a[i] < h) {
        h += a[i] / 2;
      } else if (a[i] < h * 3 && mul3cnt > 0) {
        h *= 3;
        h += a[i] / 2;
        mul3cnt--;
      } else if (a[i] < h * 2 && mul2cnt > 0) {
        h *= 2;
        h += a[i] / 2;
        mul2cnt--;
      } else {
        if (mul3cnt > 0) {
          h *= 3;
          mul3cnt--;
          continue;
        }
        if (mul2cnt > 0) {
          h *= 2;
          mul2cnt--;
          continue;
        }
        break;
      }
      i++;
    }
    return i;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n, h;
    readf!" %d %d "(n, h);
    auto a = readln.splitter.map!(to!long).array.sort.array;
    writeln(max(solve1(a, h), solve2(a, h)));
  }
}

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

int solve1(long[] a, long h)
{
    int mul2cnt = 2;
    int mul3cnt = 1;
    int i = 0;
    while (i < a.length) {
      if (a[i] < h) {
        h += a[i] / 2;
      } else if (a[i] < h * 2 && mul2cnt > 0) {
        h *= 2;
        h += a[i] / 2;
        mul2cnt--;
      } else if (a[i] < h * 3 && mul3cnt > 0) {
        h *= 3;
        h += a[i] / 2;
        mul3cnt--;
      } else {
        break;
      }
      i++;
    }
    return i;
}

int solve2(long[] a, long h)
{
    int mul2cnt = 2;
    int mul3cnt = 1;
    int i = 0;
    while (i < a.length) {
      if (a[i] < h) {
        h += a[i] / 2;
      } else if (a[i] < h * 3 && mul3cnt > 0) {
        h *= 3;
        h += a[i] / 2;
        mul3cnt--;
      } else if (a[i] < h * 2 && mul2cnt > 0) {
        h *= 2;
        h += a[i] / 2;
        mul2cnt--;
      } else {
        break;
      }
      i++;
    }
    return i;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n, h;
    readf!" %d %d "(n, h);
    auto a = readln.splitter.map!(to!long).array.sort.array;
    writeln(max(solve1(a, h), solve2(a, h)));
  }
}


You are given two arrays of integers $$$a_1,\ldots,a_n$$$ and $$$b_1,\ldots,b_m$$$.Your task is to find a non-empty array $$$c_1,\ldots,c_k$$$ that is a subsequence of $$$a_1,\ldots,a_n$$$, and also a subsequence of $$$b_1,\ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n,m\le 1000$$$)  — the lengths of the two arrays. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1\le a_i\le 1000$$$)  — the elements of the first array. The third line of each test case contains $$$m$$$ integers $$$b_1,\ldots,b_m$$$ ($$$1\le b_i\le 1000$$$)  — the elements of the second array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$1000$$$ ($$$\sum\limits_{i=1}^t n_i, \sum\limits_{i=1}^t m_i\le 1000$$$).

For each test case, output "YES" if a solution exists, or "NO" otherwise. If the answer is "YES", on the next line output an integer $$$k$$$ ($$$1\le k\le 1000$$$)  — the length of the array, followed by $$$k$$$ integers $$$c_1,\ldots,c_k$$$ ($$$1\le c_i\le 1000$$$)  — the elements of the array. If there are multiple solutions with the smallest possible $$$k$$$, output any.

NoteIn the first test case, $$$[4]$$$ is a subsequence of $$$[10, 8, 6, 4]$$$ and $$$[1, 2, 3, 4, 5]$$$. This array has length $$$1$$$, it is the smallest possible length of a subsequence of both $$$a$$$ and $$$b$$$.In the third test case, no non-empty subsequences of both $$$[3]$$$ and $$$[2]$$$ exist, so the answer is "NO".

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

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		foreach (x; a)
		{
			foreach (y; b)
			{
				if (x == y)
				{
					writeln ("YES");
					writeln (1, " ", x);
					continue multitest_loop;
				}
			}
		}
		writeln ("NO");
	}
}

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

void main() {
	int tt = readln().chomp.to!int;
	foreach (t; 0 .. tt) {
		readln();
		int[] a = readln().chomp.split(" ").map!(to!int).array;
		int[] b = readln().chomp.split(" ").map!(to!int).array;
		bool[int] aSet;
		foreach (i; a) {
			aSet[i] = true;
		}
		Nullable!int ans;
		foreach (i; b) {
			if (i in aSet) {
				ans = Nullable!int(i);
			}
		}
		if (ans.isNull) {
			writeln("NO");
		}
		else {
			writeln("YES");
			writeln("1 ", ans.get);
		}
	}
}


Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence $$$a_1, a_2, \ldots, a_n$$$ of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.Let's denote the fingers of hand by numbers from $$$1$$$ to $$$5$$$. We call a fingering any sequence $$$b_1, \ldots, b_n$$$ of fingers numbers. A fingering is convenient if for all $$$1\leq i \leq n - 1$$$ the following holds:  if $$$a_i < a_{i+1}$$$ then $$$b_i < b_{i+1}$$$, because otherwise Paul needs to take his hand off the keyboard to play the $$$(i+1)$$$-st note;  if $$$a_i > a_{i+1}$$$ then $$$b_i > b_{i+1}$$$, because of the same;  if $$$a_i = a_{i+1}$$$ then $$$b_i\neq b_{i+1}$$$, because using the same finger twice in a row is dumb. Please note that there is $$$\neq$$$, not $$$=$$$ between $$$b_i$$$ and $$$b_{i+1}$$$. Please provide any convenient fingering or find out that there is none.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the number of notes. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$) denoting the positions of notes on the keyboard.

If there is no convenient fingering, print $$$-1$$$. Otherwise, print $$$n$$$ numbers $$$b_1, b_2, \ldots, b_n$$$, each from $$$1$$$ to $$$5$$$, denoting a convenient fingering, separated by spaces.

NoteThe third sample test is kinda "Non stop" song by Reflex.

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

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

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

    auto via = new int[][] (n+1, 6);
    foreach (i; 1 .. n) {
        foreach (nxt; 1 .. 6) {
            via[i+1][nxt] = -1;

            foreach (prev; 1 .. 6) {
                if (via[i][prev] == -1) { continue; }

                if (arr[i-1] == arr[i] && prev == nxt) { continue; }
                if (arr[i-1] > arr[i] && prev <= nxt) { continue; }
                if (arr[i-1] < arr[i] && prev >= nxt) { continue; }

                via[i+1][nxt] = prev;
            }
        }
    }

    debug { via.each!writeln; }

    int f = 1;
    while (f <= 5 && via[n][f] == -1) { ++f; }

    if (f == 6) {
        writeln(-1);
        return;
    }

    int[] ans;
    ans ~= f;
    foreach_reverse (i; 2 .. n+1) {
        ans ~= via[i][f];
        f = via[i][f];
    }

    ans.reverse();
    ans.writefln!"%(%s %)";
}


AmShZ has traveled to Italy from Iran for the Thom Yorke concert. There are $$$n$$$ cities in Italy indexed from $$$1$$$ to $$$n$$$ and $$$m$$$ directed roads indexed from $$$1$$$ to $$$m$$$. Initially, Keshi is located in the city $$$1$$$ and wants to go to AmShZ's house in the city $$$n$$$. Since Keshi doesn't know the map of Italy, AmShZ helps him to see each other as soon as possible.In the beginning of each day, AmShZ can send one of the following two messages to Keshi: AmShZ sends the index of one road to Keshi as a blocked road. Then Keshi will understand that he should never use that road and he will remain in his current city for the day. AmShZ tells Keshi to move. Then, Keshi will randomly choose one of the cities reachable from his current city and move there. (city $$$B$$$ is reachable from city $$$A$$$ if there's an out-going road from city $$$A$$$ to city $$$B$$$ which hasn't become blocked yet). If there are no such cities, Keshi will remain in his current city.Note that AmShZ always knows Keshi's current location. AmShZ and Keshi want to find the smallest possible integer $$$d$$$ for which they can make sure that they will see each other after at most $$$d$$$ days. Help them find $$$d$$$.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ $$$(2 \le n \le 2 \cdot 10^5, 1 \le m \le 2 \cdot 10^5)$$$  — the number of cities and roads correspondingly. The $$$i$$$-th line of the following $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ $$$(1 \le v_i , u_i \le n,v_i \neq u_i)$$$, denoting a directed road going from city $$$v_i$$$ to city $$$u_i$$$. It is guaranteed that there is at least one route from city $$$1$$$ to city $$$n$$$. Note that there may be more than one road between a pair of cities in each direction.

Output the smallest possible integer $$$d$$$ to make sure that AmShZ and Keshi will see each other after at most $$$d$$$ days.

NoteIn the first sample, it's enough for AmShZ to send the second type of message.In the second sample, on the first day, AmShZ blocks the first road. So the only reachable city from city $$$1$$$ will be city $$$4$$$. Hence on the second day, AmShZ can tell Keshi to move and Keshi will arrive at AmShZ's house.It's also possible for AmShZ to tell Keshi to move for two days.

import std.container;
import std.stdio;
import std.typecons;

immutable int infinity = int.max / 4;

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		auto adj = new int [] [n];
		auto adjR = new int [] [n];
		auto deg = new int [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
			adjR[v] ~= u;
			deg[u] += 1;
		}

		auto d = new int [n];
		d[] = infinity;
		d[n - 1] = 0;
		auto used = new bool [n];
		alias Record = Tuple !(int, q{dist}, int, q{v});
		auto t = redBlackTree !(Record) ();
		foreach (i; 0..n)
		{
			t.insert (Record (d[i], i));
		}
		while (!t.empty)
		{
			auto cur = t.front;
			t.removeFront ();
			used[cur.v] = true;
			foreach (u; adjR[cur.v])
			{
				deg[u] -= 1;
				if (!used[u] && d[u] > deg[u] + d[cur.v] + 1)
				{
					t.removeKey (Record (d[u], u));
					d[u] = deg[u] + d[cur.v] + 1;
					t.insert (Record (d[u], u));
				}
			}
		}

		writeln (d[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)); }

enum INF = 10^^9;

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

      auto ds = new int[N];
      auto G = new int[][N];
      foreach (i; 0 .. M) {
        ++ds[A[i]];
        G[B[i]] ~= i;
      }

      alias Entry = Tuple!(int, "c", int, "u");
      BinaryHeap!(Array!Entry, "a.c > b.c") que;
      auto vis = new bool[N];
      auto dist = new int[N];
      dist[] = INF;
      dist[N - 1] = 0;
      que.insert(Entry(0, N - 1));
      for (; !que.empty; ) {
        const c = que.front.c;
        const u = que.front.u;
        que.removeFront;
        if (!vis[u]) {
          vis[u] = true;
          foreach (i; G[u]) {
            const v = A[i];
            const cc = c + 1 + (--ds[v]);
            debug {
              writefln("%s <- %s: %s", u, v, cc);
            }
            if (chmin(dist[v], cc)) {
              que.insert(Entry(cc, v));
            }
          }
        }
      }
      debug {
        writeln("dist = ", dist);
      }
      writeln(dist[0]);
    }
  } catch (EOFException e) {
  }
}


You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.

For each test case, print the maximum possible sum of the sequence $$$c$$$.

// Ported to D by unihernandez22
// Solution by Vicfred
// https://codeforces.com/contest/1401/problem/B

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

void main() {
  int t = readln.chomp.to!int;
  foreach (_; 0 .. t) {
    int[] a = readln.split.map!(to!int).array,
      b = readln.split.map!(to!int).array;
    int ans = 0;

    if (a[2] > 0) {
      int taken = min(b[1], a[2]);
      a[2] -= taken;
      b[1] -= taken;

      ans += taken*2;
    }

    if (a[2] > 0) {
      int taken = min(b[2],a[2]);
      a[2] -= taken;
      b[2] -= taken;
    }

    if (a[2] > 0) {
      int taken = min(b[0],a[2]);
      a[2] -= taken;
      b[0] -= taken;
    }

    if (a[1] > 0) {
      int taken = min(b[1],a[1]);
      a[1] -= taken;
      b[1] -= taken;
    }

    if (a[1] > 0) {
      int taken = min(b[0],a[1]);
      a[1] -= taken;
      b[0] -= taken;
    }

    if (a[1] > 0) {
      int taken = min(b[2],a[1]);
      a[1] -= taken;
      b[2] -= taken;
      ans += taken*(-2);
    }

    writeln(ans);
  }
}

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 a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;

		auto num (int i, int j)
		{
			int res = min (a[i], b[j]);
			a[i] -= res;
			b[j] -= res;
			return res;
		}

		int res = num (2, 1) * 2;
		num (1, 1);
		num (1, 0);
		res -= num (1, 2) * 2;
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto x = min(xyz1[2], xyz2[1]);
		ans[ti] += x * 2;
		xyz1[0] -= x;
		xyz2[1] -= x;
		auto y = xyz2[2] - (xyz1[0]+xyz1[2]);
		if (y > 0)
			ans[ti] -= y * 2;
	}

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


In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.You are given two strings s1, s2 and another string called virus. Your task is to find the longest common subsequence of s1 and s2, such that it doesn't contain virus as a substring.

The input contains three strings in three separate lines: s1, s2 and virus (1 ≤ |s1|, |s2|, |virus| ≤ 100). Each string consists only of uppercase English letters.

Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted.  If there is no valid common subsequence, output 0.

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

int saiki(int i, int j, int k, string a, string b, string v, int[][][] dp, Tuple!(int, int, int)[][][] result, int[][] jump)
{
    auto la = cast(int)a.length;
    auto lb = cast(int)b.length;
    auto lv = cast(int)v.length;
    if (dp[i][j][k] != -1)
    {
        return dp[i][j][k];
    }
    if (k == lv)
    {
        dp[i][j][k] = int.max;
        return int.max;
    }
    if (i == la || j == lb)
    {
        dp[i][j][k] = 0;
        return 0;
    }
    auto res = 0;
    if (a[i] == b[j])
    {
        auto ret = saiki(i + 1, j + 1, jump[k][a[i] - 'A'], a, b, v, dp, result, jump);
        if (ret + 1 > res)
        {
            result[i][j][k] = Tuple!(int, int, int)(i + 1, j + 1, jump[k][a[i] - 'A']);
            res = ret + 1;
        }
    }
    auto ret = saiki(i + 1, j, k, a, b, v, dp, result, jump);
    if (ret > res)
    {
        res = ret;
        result[i][j][k] = Tuple!(int, int, int)(i + 1, j, k);
    }
    ret = saiki(i, j + 1, k, a, b, v, dp, result, jump);
    if (ret > res)
    {
        res = ret;
        result[i][j][k] = Tuple!(int, int, int)(i, j + 1, k);
    }
    dp[i][j][k] = res;
    return res;
}

int[][] getJump(string s)
{
    auto n = cast(int)s.length;
    auto jump = new int[][](n + 1, 26);
    foreach (i; 0 .. n)
    {
        foreach (j; 0 .. 26)
        {
            if (s[i] == 'A' + j)
            {
                jump[i][j] = i + 1;
            }
            else
            {
                jump[i][j] = 0;
                for (int k = i - 1; k >= 0; -- k)
                {
                    if (s[k] == 'A' + j)
                    {
                        auto x = k - 1, y = i - 1;
                        while (x >= 0 && y >= 0)
                        {
                            if (s[x] != s[y])
                            {
                                break;
                            }
                            -- x;
                            -- y;
                        }
                        if (x < 0)
                        {
                            jump[i][j] = k + 1;
                            break;
                        }
                    }
                }
            }
        }
    }
    return jump;
}

string construct(string a, string b, string v, Tuple!(int, int, int)[][][] result, int[][] jump)
{
    auto la = cast(int)a.length;
    auto lb = cast(int)b.length;
    auto lv = cast(int)v.length;
    auto i = 0, j = 0, k = 0;
    string res;
    while (i < la && j < lb)
    {
        if (result[i][j][k] == tuple(-1, -1, -1))
        {
            break;
        }
        auto ni = result[i][j][k][0];
        auto nj = result[i][j][k][1];
        auto nk = result[i][j][k][2];
        if (ni == i + 1 && nj == j + 1)
        {
            auto c = a[i];
            res ~= c;
        }
        i = ni;
        j = nj;
        k = nk;
    }
    return res;
}

void solve(string a, string b, string v)
{
    auto la = cast(int)a.length;
    auto lb = cast(int)b.length;
    auto lv = cast(int)v.length;
    auto jump = getJump(v);
    auto dp = new int[][][](la + 1, lb + 1, lv + 1);
    auto result = new Tuple!(int, int, int)[][][](la + 1, lb + 1, lv + 1);
    foreach (i; 0 .. la + 1)
    {
        foreach (j; 0 .. lb + 1)
        {
            fill(dp[i][j], -1);
            fill(result[i][j], tuple(-1, -1, -1));
        }
    }
    auto ans = saiki(0, 0, 0, a, b, v, dp, result, jump);
    if (ans == 0)
    {
        writeln(0);
    }
    else
    {
        auto res = construct(a, b, v, result, jump);
        writeln(res);
    }
}

int main(string[] args)
{
    string s0, s1, virus;
    s0 = readln().strip();
    s1 = readln().strip();
    virus = readln().strip();
    solve(s0, s1, virus);
    return 0;
}

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

int saiki(int i, int j, int k, int la, int lb, int lv, int[][][] dp, Tuple!(int, int, int)[][][] result, int[][] nexta, int[][] nextb, int[][] jump)
{
    if (dp[i][j][k] != -1)
    {
        return dp[i][j][k];
    }
    if (k == lv)
    {
        dp[i][j][k] = int.max;
        return int.max;
    }
    if (i == la || j == lb)
    {
        dp[i][j][k] = 0;
        return 0;
    }
    auto res = 0;
    foreach (idx; 0 .. 26)
    {
        if (nexta[i][idx] != -1 && nextb[j][idx] != -1)
        {
            auto ret = saiki(nexta[i][idx] + 1, nextb[j][idx] + 1, jump[k][idx], la, lb, lv, dp, result, nexta, nextb, jump);
            if (ret != int.max)
            {
                if (ret + 1 > res)
                {
                    result[i][j][k] = Tuple!(int, int, int)(nexta[i][idx], nextb[j][idx], jump[k][idx]);
                    res = ret + 1;
                }
            }
        }
    }
    dp[i][j][k] = res;
    return res;
}

int[][] getNext(string s)
{
    auto n = cast(int)s.length;
    auto next = new int[][](n + 1, 26);
    foreach (i; 0 .. n)
    {
        foreach (j; 0 .. 26)
        {
            next[i][j] = -1;
            foreach (k; i .. n)
            {
                if (s[k] == 'A' + j)
                {
                    next[i][j] = k;
                    break;
                }
            }
        }
    }
    return next;
}

int[][] getJump(string s)
{
    auto n = cast(int)s.length;
    auto jump = new int[][](n + 1, 26);
    foreach (i; 0 .. n)
    {
        foreach (j; 0 .. 26)
        {
            if (s[i] == 'A' + j)
            {
                jump[i][j] = i + 1;
            }
            else
            {
                jump[i][j] = 0;
                for (int k = i - 1; k >= 0; -- k)
                {
                    if (s[k] == 'A' + j)
                    {
                        auto x = k - 1, y = i - 1;
                        while (x >= 0 && y >= 0)
                        {
                            if (s[x] != s[y])
                            {
                                break;
                            }
                            -- x;
                            -- y;
                        }
                        if (x < 0)
                        {
                            jump[i][j] = k + 1;
                            break;
                        }
                    }
                }
            }
        }
    }
    return jump;
}

string configure(string a, string b, string v, Tuple!(int, int, int)[][][] result, int[][] jump)
{
    auto la = cast(int)a.length;
    auto lb = cast(int)b.length;
    auto lv = cast(int)v.length;
    auto i = 0, j = 0, k = 0;
    string res;
    while (i < la && j < lb)
    {
        if (result[i][j][k] == tuple(-1, -1, -1))
        {
            break;
        }
        auto c = a[result[i][j][k][0]];
        res ~= c;
        auto ni = result[i][j][k][0] + 1;
        auto nj = result[i][j][k][1] + 1;
        auto nk = jump[k][c - 'A'];
        i = ni;
        j = nj;
        k = nk;
    }
    return res;
}

void solve(string a, string b, string v)
{
    auto la = cast(int)a.length;
    auto lb = cast(int)b.length;
    auto lv = cast(int)v.length;
    auto nexta = getNext(a);
    auto nextb = getNext(b);
    auto jump = getJump(v);
    auto dp = new int[][][](la + 1, lb + 1, lv + 1);
    auto result = new Tuple!(int, int, int)[][][](la + 1, lb + 1, lv + 1);
    foreach (i; 0 .. la + 1)
    {
        foreach (j; 0 .. lb + 1)
        {
            fill(dp[i][j], -1);
            fill(result[i][j], tuple(-1, -1, -1));
        }
    }
    auto ans = saiki(0, 0, 0, la, lb, lv, dp, result, nexta, nextb, jump);
    if (ans == 0)
    {
        writeln(0);
    }
    else
    {
        auto res = configure(a, b, v, result, jump);
        writeln(res);
    }
}

int main(string[] args)
{
    string s0, s1, virus;
    s0 = readln().strip();
    s1 = readln().strip();
    virus = readln().strip();
    solve(s0, s1, virus);
    return 0;
}

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

void main ()
{
	string r;
	while ((r = readln ().strip ()) != "")
	{
		string s = readln ().strip ();
		string t = readln ().strip ();
		int u = r.length;
		int v = s.length;
		int w = t.length;
		auto p = new int [w + 1];
		p[] = 0;
		foreach (i; 1..w + 1)
		{
			foreach (j; 1..i)
			{
				if (t[0..j] == t[i - j..i])
				{
					p[i] = max (p[i], j);
				}
			}
		}
		auto f = new int [] [] [] (u + 1, v + 1, w + 1);
		alias Tuple !(int, int, int) prev;
		auto q = new prev [] [] [] (u + 1, v + 1, w + 1);
		foreach (i; 0..u + 1)
		{
			foreach (j; 0..v + 1)
			{
				foreach (k; 0..w + 1)
				{
					f[i][j][k] = -3;
					q[i][j][k] = prev (-3, -3, -3);
				}
			}
		}
		f[0][0][0] = 0;
		foreach (int i; 0..u + 1)
		{
			foreach (int j; 0..v + 1)
			{
				foreach (int k; 0..w)
				{
					auto cur = f[i][j][k];
					if (cur == -3)
					{
						continue;
					}

					void move (int x, int y, int z)
					{
						if (f[x][y][z] < cur)
						{
							f[x][y][z] = cur;
							q[x][y][z] =
								prev (i, j, k);
						}
					}

					if (i < u)
					{
						move (i + 1, j, k);
					}
					if (j < v)
					{
						move (i, j + 1, k);
					}
					if (i < u && j < v && r[i] == s[j])
					{
						auto z = k;
						while (z > 0 && r[i] != t[z])
						{
							z = p[z];
						}
						if (r[i] == t[z])
						{
							z++;
						}
						cur++;
						move (i + 1, j + 1, z);
					}
				}
			}
		}
		int pi = u, pj = v, pk = -3;
		int res = -3;
		foreach (k; 0..w)
		{
			if (res < f[pi][pj][k])
			{
				res = f[pi][pj][k];
				pk = k;
			}
		}
		if (res <= 0)
		{
			writeln (0);
		}
		else
		{
			char [] ans;
			while (pk != -3)
			{
				int qi, qj, qk;
				qi = q[pi][pj][pk][0];
				qj = q[pi][pj][pk][1];
				qk = q[pi][pj][pk][2];
				if (qi == pi - 1 && qj == pj - 1)
				{
					ans ~= r[qi];
				}
				pi = qi;
				pj = qj;
				pk = qk;
			}
			ans.reverse ();
			writeln (ans);
		}
	}
}


Given $$$n$$$, find any array $$$a_1, a_2, \ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \le a_i \le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 < a_2 < \ldots <a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.

The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

For each test case print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.

NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2<3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$.

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  foreach(i; 2 .. 2 + n)
    write(i, " ");
  writeln;
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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

void solve(){
    long n = scan;
    for(int i = 2; i < 2 + n; ++i){
        write(i, " ");
    }
    writeln;
}

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

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

import std.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);
		iota (10 ^^ 9 - n, 10 ^^ 9).writefln !("%(%s %)");
	}
}


You are given an array of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. You can make the following operation: choose an integer $$$x \geq 2$$$ and replace each number of the array by the remainder when dividing that number by $$$x$$$, that is, for all $$$1 \leq i \leq n$$$ set $$$a_i$$$ to $$$a_i \bmod x$$$.Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 \cdot 10^5$$$.

For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

NoteIn the first test case, one can apply the operation with $$$x = 3$$$ to obtain the array $$$[2, 2, 0, 2]$$$, and then apply the operation with $$$x = 2$$$ to obtain $$$[0, 0, 0, 0]$$$.In the second test case, all numbers are already equal.In the fourth test case, applying the operation with $$$x = 4$$$ results in the array $$$[1, 1, 1, 1]$$$.

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

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

		bool has1, hasD1;
		foreach (i; 0..n)
		{
			if (a[i] == 1)
				has1 = true;
			if (i != n-1)
			{
				if (a[i+1] - a[i] == 1)
					hasD1 = true;
			}
		}

		if (!(has1 && hasD1))
			ans[ti] = true;
	}

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

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

bool solve (int [] a, int n)
{
	if (!a.canFind (1))
	{
		return true;
	}
	sort (a);
	foreach (i; 1..n)
	{
		if (a[i] - a[i - 1] == 1)
		{
			return false;
		}
	}
	return true;
}

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 (a, n) ? "YES" : "NO");
	}
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

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

		bool has0, has1;
		foreach (i; 0..n)
		{
			if (a[i] > 2) continue;
			if (a[i] % 2)
				has1 = true;
			else
				has0 = true;
		}

		if (!(has0 && has1))
			ans[ti] = true;
	}

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


Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.  They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000). The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes). The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

After each round, print the current score of the bears.

import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInt() {
	return readInts()[0];
}
auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}

int rScore(int[] r) {
	int ans;
	int tmp;
	foreach(i; 0..r.length) {
		if(r[i] == 1) {
			++tmp;
		} else {
			ans = max(ans, tmp);
			tmp = 0;
		}
	}
	return max(tmp, ans);
}
void main() {
	auto l = readInts();
	auto n = l[0];
	auto m = l[1];
	auto q = l[2];
	int[][] b;
	foreach(i; 0..n) {
		b ~= readInts();
	}
	int[] rs;
	foreach(i; 0..n) {
		rs ~= rScore(b[i]);
	}
	foreach(k; 0..q) {
		auto ij = readInts();
		auto i = ij[0]-1;
		auto j = ij[1]-1;
		b[i][j] ^= 1;
		rs[i] = rScore(b[i]);
		writeln(reduce!(max)(rs));
	}
}


There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance.Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of students. The second line of the query contains a permutation of indices $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$), where $$$p_i$$$ is the index of the $$$i$$$-th student (in clockwise order). It is guaranteed that all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).

For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

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

void main()
{
	auto q = RD!int;
	auto ans = new bool[](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		auto p = RDA(-1);
		bool ok = true;
		foreach (j; 1..n)
		{
			if ((p[j-1]+1)%n != p[j])
			{
				ok = false;
				break;
			}
		}
		if (!ok)
		{
			ok = true;
			foreach (j; 1..n)
			{
				if (p[j-1] != (p[j]+1)%n)
				{
					ok = false;
					break;
				}
			}
		}
		ans[i] = ok;
	}

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


Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations?

The first line contains a single integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of numbered cards. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le n$$$) — the initial cards in Nauuo's hands. $$$0$$$ represents an empty card. The third line contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$0\le b_i\le n$$$) — the initial cards in the pile, given in order from top to bottom. $$$0$$$ represents an empty card. It is guaranteed that each number from $$$1$$$ to $$$n$$$ appears exactly once, either in $$$a_{1..n}$$$ or $$$b_{1..n}$$$.

The output contains a single integer — the minimum number of operations to make the $$$n$$$ numbered cards piled up in increasing order.

NoteExample 1We can play the card $$$2$$$ and draw the card $$$3$$$ in the first operation. After that, we have $$$[0,3,0]$$$ in hands and the cards in the pile are $$$[0,1,2]$$$ from top to bottom.Then, we play the card $$$3$$$ in the second operation. The cards in the pile are $$$[1,2,3]$$$, in which the cards are piled up in increasing order.Example 2Play an empty card and draw the card $$$1$$$, then play $$$1$$$, $$$2$$$, $$$3$$$ in order.

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

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

		int res = 0;
		foreach (i; 0..n)
		{
			if (a[i] > 0)
			{
				res = max (res, n - a[i] + 1);
			}
		}

		int limit = n;
		foreach (i; 0..n)
		{
			if (b[i] == 1)
			{
				if (b[i..$].equal (iota (1, n + 1 - i)))
				{
					limit = i;
				}
			}
		}

		foreach (i; 0..limit)
		{
			if (b[i] > 0)
			{
				res = max (res, i + 1 + n - b[i] + 1);
			}
		}

		if (res > limit)
		{
			foreach (i; 0..n)
			{
				if (b[i] > 0)
				{
					res = max (res, i + 1 + n - b[i] + 1);
				}
			}
		}
		writeln (res);
	}
}

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

int N;
int[] A, B;

int[] pos;

int solveDirect() {
  // B[N - 1] at 2 N - 1
  const d = (2 * N - 1) - B[N - 1];
  bool ok = true;
  foreach (x; 1 .. B[N - 1] + 1) {
    ok = ok && (pos[x] == d + x);
  }
  foreach (x; B[N - 1] + 1 .. N + 1) {
    ok = ok && (pos[x] <= d + x - N - 1);
  }
  return ok ? (N - B[N - 1]) : -1;
}

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

      pos = new int[N + 1];
      foreach (i; 0 .. N) {
        pos[A[i]] = i;
      }
      foreach (i; 0 .. N) {
        pos[B[i]] = N + i;
      }

      int ans = solveDirect();
      if (ans == -1) {
        int lo = -1, hi = 2 * N;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          // 1 at 2 N + mid
          const d = (2 * N + mid) - 1;
          bool ok = true;
          foreach (x; 1 .. N + 1) {
            ok = ok && (pos[x] <= d + x - N - 1);
          }
          debug {
            writeln(mid, " ", d, " ", ok);
          }
          ok ? (hi = mid) : (lo = mid);
        }
        ans = hi + N;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
  GC.disable();

  // BEGIN HERE

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

  auto a = new int[n];
  auto b = new int[n];
  iota(0,n).each!(i=> readf(" %s", a[i]));
  iota(0,n).each!(i=> readf(" %s", b[i]));

  auto step = new int[n+1];
  iota(0,n).each!(i=> step[ b[i] ] = i+1);
  iota(0,n).each!(i=> step[ a[i] ] = 0);

  // can I append to the tail?
  if (step[1] > 0) {
    auto delta = n - step[1]+ 1;
    if (step[1 .. delta+1] == iota(step[1],n+1).array)
      if (step[delta+1 ..$].enumerate.all!"a.value<=a.index") {
        writeln(n-delta);
        return;
      }
  }

  // I can't
  writeln(step[1 ..$].enumerate.map!"a[1]-to!int(a[0])".maxElement + n);

  // END HERE

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
  GC.disable();

  // BEGIN HERE

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

  auto a = new int[n];
  auto b = new int[n];
  iota(0,n).each!(i=> readf(" %s", a[i]));
  iota(0,n).each!(i=> readf(" %s", b[i]));

  auto step = new int[n+1];
  iota(0,n).each!(i=> step[ b[i] ] = i+1);
  iota(0,n).each!(i=> step[ a[i] ] = 0);

  // can I append to the tail?
  if (step[1] > 0) {
    auto delta = n - step[1]+ 1;
    if (step[1 .. delta+1] == iota(step[1],n+1).array) {
      if (step[delta+1 ..$].enumerate.all!"a[1]<=a[0]") {
        writeln(n-delta); return;}
    }
  }

  // I can't
  auto ii = iota(0, step.length).map!(x=>to!int(x)).array;
  step[] -= ii[];
  auto ans = (step[1..$].maxElement +1) + n;

  writeln(ans);

  // END HERE

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

int solve(int[] step, int ts) {
  // return -1 -> cannot
  int s= 1;
  foreach(i;1..step.length) {
    if (step[i] == -1) continue;
    if (step[i] <= ts) {
      ts++;
    } else return -1;
  }

  return ts;
}

void main() {
  GC.disable();

  // BEGIN HERE

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

  auto a = new int[n];
  auto b = new int[n];
  iota(0,n).each!(i=> readf(" %s", a[i]));
  iota(0,n).each!(i=> readf(" %s", b[i]));

  // writeln(a); writeln(b);

  auto step = new int[n+1];
  step[] = n;
  iota(0,n).each!(i=> step[ b[i] ] = i+1);
  iota(0,n).each!(i=> step[ a[i] ] = 0);

  // can I append to the tail?
  auto ss = step.dup;
  auto del = n - ss[1];
  bool flag = false;
  int bb = ss[1];
  foreach(i; 0..del+1) {
    if (ss[1+i] ==0) {flag=true; break;}
    if (ss[1 + i] != bb + i) { flag = true; break;}
    ss[1+i] = -1;
  }

  // writeln(iota(0,ss.length));
  // writeln(step); writeln(ss);
  if (!flag) {  // try to append
    auto ans = solve(ss, 0);
    if (ans != -1) { writeln(ans); return; }
  }

  // I can't
  auto sss = step.dup;
  sss[0] = 0;
  auto ii = iota(0, step.length).map!(x=>to!int(x)).array;
  sss[] -= ii[];
  auto ans = (sss[1..$].maxElement +1) + n;

  writeln(ans);

  // END HERE

}

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

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

		int res = 0;
		foreach (i; 0..n)
		{
			if (a[i] > 0)
			{
				res = max (res, n - a[i] + 1);
			}
		}

		int limit = n;
		foreach (i; 0..n)
		{
			if (b[i] == 1)
			{
				if (b[i..$].equal (iota (1, n + 1 - i)))
				{
					limit = i;
				}
			}
		}

		foreach (i; 0..limit)
		{
			if (b[i] > 0)
			{
				res = max (res, i + 1 + n - b[i] + 1);
			}
		}
		writeln (res);
	}
}

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

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

		int res = 0;
		foreach (i; 0..n)
		{
			if (a[i] > 0)
			{
				res = max (res, n - a[i] + 1);
			}
		}

		int limit = n;
		foreach (i; 0..n)
		{
			if (b[i] == 1)
			{
				if (b[i..$].equal (iota (1, n + 1 - i)))
				{
					limit = i;
				}
			}
		}

		foreach (i; 0..limit)
		{
			if (b[i] > 0)
			{
				res = max (res, i + 1 + n - b[i] + 1);
			}
		}

		if (res > n - limit)
		{
			foreach (i; 0..n)
			{
				if (b[i] > 0)
				{
					res = max (res, i + 1 + n - b[i] + 1);
				}
			}
		}
		writeln (res);
	}
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

int solve(int[] step, int ts) {
  // return -1 -> cannot
  int s= 1;
  foreach(i;1..step.length) {
    if (step[i] == -1) continue;
    if (step[i] <= ts) {
      ts++;
    } else return -1;
  }

  return ts;
}

void main() {
  GC.disable();

  // BEGIN HERE

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

  auto a = new int[n];
  auto b = new int[n];
  iota(0,n).each!(i=> readf(" %s", a[i]));
  iota(0,n).each!(i=> readf(" %s", b[i]));

  // writeln(a); writeln(b);

  auto step = new int[n+1];
  step[] = n;
  iota(0,n).each!(i=> step[ b[i] ] = i+1);
  iota(0,n).each!(i=> step[ a[i] ] = 0);

  // can I append to the tail?
  auto ss = step.dup;
  auto del = n - ss[1];
  bool flag = false;
  int bb = ss[1];
  foreach(i; 0..del+1) {
    if (ss[1 + i] != bb + i) { flag = true; break;}
    ss[1+i] = -1;
  }

  // writeln(iota(0,ss.length));
  // writeln(step); writeln(ss);
  if (!flag) {  // try to append
    auto ans = solve(ss, 0);
    if (ans != -1) { writeln(ans); return; }
  }

  // I can't
  auto sss = step.dup;
  sss[0] = 0;
  auto ii = iota(0, step.length).map!(x=>to!int(x)).array;
  sss[] -= ii[];
  auto ans = (sss.maxElement +1) + n;

  writeln(ans);

  // END HERE

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

int solve(int[] step, int ts) {
  // return -1 -> cannot
  int s= 1;
  foreach(i;1..step.length) {
    if (step[i] == -1) continue;
    if (step[i] <= ts) {
      ts++;
    } else return -1;
  }

  return ts;
}

void main() {
  GC.disable();

  // BEGIN HERE

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

  auto a = new int[n];
  auto b = new int[n];
  iota(0,n).each!(i=> readf(" %s", a[i]));
  iota(0,n).each!(i=> readf(" %s", b[i]));

  // writeln(a);
  // writeln(b);

  auto step = new int[n+1];
  step[] = n;
  iota(0,n).each!(i=> step[ b[i] ] = i+1);
  iota(0,n).each!(i=> step[ a[i] ] = 0);

  // can I append to the tail?
  auto ss = step.dup;
  auto del = n - ss[1];
  bool flag = false;
  foreach(i; 0..del+1) {
    if (ss[1 + i] != ss[1] + i) { flag = true; break;}
    ss[1+i] = -1;
  }

  // writeln(iota(0,ss.length));
  // writeln(step);
  // writeln(ss);
  if (!flag) {  // try to append
    auto ans = solve(ss, 0);
    if (ans != -1) { writeln(ans); return; }
  }

  // I can't
  auto sss = step.dup;
  sss[0] = 0;
  auto ii = iota(0, step.length).map!(x=>to!int(x)).array;
  sss[] -= ii[];
  auto ans = (sss.maxElement +1) + n;

  writeln(ans);

  // END HERE

}


You are given an array $$$a$$$ of $$$n$$$ integers.You need to find the maximum value of $$$a_{i} | ( a_{j} \& a_{k} )$$$ over all triplets $$$(i,j,k)$$$ such that $$$i < j < k$$$.Here $$$\&$$$ denotes the bitwise AND operation, and $$$|$$$ denotes the bitwise OR operation.

The first line of input contains the integer $$$n$$$ ($$$3 \le n \le 10^{6}$$$), the size of the array $$$a$$$. Next line contains $$$n$$$ space separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 2 \cdot 10^{6}$$$), representing the elements of the array $$$a$$$.

Output a single integer, the maximum value of the expression given in the statement.

NoteIn the first example, the only possible triplet is $$$(1, 2, 3)$$$. Hence, the answer is $$$2 | (4 \& 6) = 6$$$.In the second example, there are $$$4$$$ possible triplets:   $$$(1, 2, 3)$$$, value of which is $$$2|(8\&4) = 2$$$.  $$$(1, 2, 4)$$$, value of which is $$$2|(8\&7) = 2$$$.  $$$(1, 3, 4)$$$, value of which is $$$2|(4\&7) = 6$$$.  $$$(2, 3, 4)$$$, value of which is $$$8|(4\&7) = 12$$$. The maximum value hence is $$$12$$$.

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

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

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

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

enum E = 21;

void chmax2(int[] t, int[] f) {
  /*
  foreach (j; 0 .. 2) {
    int tmp = f[j];
    foreach (i; 0 .. 2) {
      if (t[i] < tmp) {
        swap(t[i], tmp);
      }
    }
  }
  */
  int tmp = f[0];
  if (t[0] < tmp) swap(t[0], tmp);
  if (t[1] < tmp) t[1] = tmp;
  if (t[1] < f[1]) t[1] = f[1];
}

int N;
int[] A;

int L;
int[] B;

int[] mx0, mx1;

bool check() {
  mx0[0 .. 1 << L] = -1;
  mx1[0 .. 1 << L] = -1;
  foreach (i; 0 .. N) {
    int tmp = i;
    if (mx0[B[i]] < tmp) swap(mx0[B[i]], tmp);
    if (mx1[B[i]] < tmp) mx1[B[i]] = tmp;
  }
  foreach (l; 0 .. L) {
    foreach (p; 0 .. 1 << L) {
      if (!((p & 1 << l))) {
        int tmp = mx0[p | 1 << l];
        if (mx0[p] < tmp) swap(mx0[p], tmp);
        if (mx1[p] < tmp) mx1[p] = tmp;
        if (mx1[p] < mx1[p | 1 << l]) mx1[p] = mx1[p | 1 << l];
      }
    }
  }
  foreach (i; 0 .. N) {
    if (i < mx1[((1 << L) - 1) & ~B[i]]) {
      return true;
    }
  }
  return false;
}

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

      int ans;
      L = 0;
      B = new int[N];
      mx0 = new int[1 << E];
      mx1 = new int[1 << E];
      foreach_reverse (e; 0 .. E) {
        foreach (i; 0 .. N) {
          if ((A[i] >> e) & 1) {
            B[i] |= 1 << L;
          }
        }
        ++L;
        if (check()) {
          ans |= 1 << e;
        } else {
          --L;
          B[] &= ((1 << L) - 1);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

enum INF = 10L^^18;

int N, W;
int[] L;
long[][] A;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      W = readInt();
      L = new int[N];
      A = new long[][N];
      foreach (i; 0 .. N) {
        L[i] = readInt();
        A[i] = new long[L[i]];
        foreach (j; 0 .. L[i]) {
          A[i][j] = readLong();
        }
      }

      auto ans = new long[W + 1];

      alias Entry = Tuple!(long, "val", int, "key");
      auto que = new Entry[W];
      int qb, qe;

      foreach (i; 0 .. N) {
        if (W >= 2 * L[i]) {
          // always empty ok
          auto ls = A[i].dup, rs = A[i].dup;
          foreach (j; 0 .. L[i] - 1) {
            chmax(ls[j + 1], ls[j]);
          }
          foreach_reverse (j; 1 .. L[i]) {
            chmax(rs[j - 1], rs[j]);
          }
          foreach (x; 0 .. L[i]) {
            long mx = ls[x];
            chmax(mx, 0);
            debug {
              writefln("long ans %s %s: %s", i, x, mx);
            }
            ans[x] += mx;
            ans[x + 1] -= mx;
          }
          foreach (x; W - L[i] .. W) {
            long mx = rs[x - (W - L[i])];
            chmax(mx, 0);
            debug {
              writefln("long ans %s %s: %s", i, x, mx);
            }
            ans[x] += mx;
            ans[x + 1] -= mx;
          }
          // [L[i], W - L[i])
          {
            long mx = rs[0];
            chmax(mx, 0);
            debug {
              writefln("long ans %s [%s, %s): %s", i, L[i], W - L[i], mx);
            }
            ans[L[i]] += mx;
            ans[W - L[i]] -= mx;
          }
        } else {
          qb = qe = 0;
          /*
            index j at position x
            <=> j <= x && L[i] - j <= W - x
            <=> x - (W - L[i]) <= j <= x
          */
          foreach (x; 0 .. W) {
            if (x < L[i]) {
              // push x
              for (; qe - qb >= 1 && que[qe - 1].val <= A[i][x]; --qe) {}
              que[qe++] = Entry(A[i][x], x);
            }
            for (; qe - qb >= 1 && !(x - (W - L[i]) <= que[qb].key); ++qb) {}
            assert(qe - qb >= 1);
            long mx = que[qb].val;
            if (x >= L[i] || W - 1 - x >= L[i]) {
              chmax(mx, 0);
            }
            debug {
              writefln("short ans %s %s: %s", i, x, mx);
            }
            ans[x] += mx;
            ans[x + 1] -= mx;
          }
        }
      }

      foreach (x; 0 .. W) {
        ans[x + 1] += ans[x];
      }
      assert(ans[W] == 0);

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


Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:  The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1).  No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q).  All elements of the sequence are good integers. Find the length of the longest good sequence.

The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105; ai < ai + 1).

Print a single integer — the length of the longest good sequence.

NoteIn the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.

import std.stdio;
import std.math;

static immutable N = 100000;

int n, q, ans;
int[N + 1] a, top, f;
int[20][N + 1] prime;

int main() {
  readf(" %d", &n);
  for (int i = 1; i <= n; ++ i) {
    readf(" %d", &a[i]);
    q = cast(int) sqrt(real(a[i]));
    for (int j = 2; j <= q; ++ j) {
      if (! (a[i] % j)) {
        prime[i][top[i] ++] = j;
        while (! (a[i] % j)) a[i] /= j;
      }
    }
    if (a[i] > 1) prime[i][top[i] ++] = a[i];
  }
  f[1] = 1;
  for (int i = 1; i <= n; ++ i) {
    int rec = 0;
    for (int j = 0; j < top[i]; ++ j)
      if (rec < f[prime[i][j]]) rec = f[prime[i][j]];
    ++ rec;
    for (int j = 0; j < top[i]; ++ j) f[prime[i][j]] = rec;
  }
  for (int i = 1; i <= N; ++ i)
    if (f[i] > ans) ans = f[i];
  writeln(ans);
  return 0;
}

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

void main() {
    immutable int MAX = 10^^5 + 1;
    bool[] table = new bool[](MAX);
    fill(table, true);
    table[0] = table[1] = false;

    int[][] P = new int[][](MAX);

    foreach (i; 2..MAX) {
        if (!table[i]) continue;
        P[i] ~= i;
        for (int j = i + i; j < MAX; j += i) {
            table[j] = false;
            P[j] ~= i;
        }
    }

    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto dp = new int[](MAX);

    foreach (i; 0..N) {
        int tmp = 0;
        foreach (j; P[A[i]]) tmp = max(tmp, dp[j] + 1);
        foreach (j; P[A[i]]) dp[j] = tmp;
    }

    int ans = dp.reduce!max;
    max(ans, 1).writeln;
}

import std.stdio;
import std.math;

static immutable N = 100000;

int n, q, ans;
int[N + 1] a, top, f;
int[N + 1][20] prime;

int main() {
  readf(" %d", &n);
  for (int i = 1; i <= n; ++ i) {
    readf(" %d", &a[i]);
    q = cast(int) sqrt(real(a[i]));
    for (int j = 2; j <= q; ++ j) {
      if (! (a[i] % j)) {
        prime[i][++ top[i]] = j;
        while (! (a[i] % j)) a[i] /= j;
      }
    }
    if (a[i] > 1) prime[i][++ top[i]] = a[i];
  }
  for (int i = 1; i <= n; ++ i) {
    int rec = 0;
    for (int j = 1; j <= top[i]; ++ j)
      if (rec < f[prime[i][j]]) rec = f[prime[i][j]];
    ++ rec;
    for (int j = 1; j <= top[i]; ++ j) f[prime[i][j]] = rec;
  }
  for (int i = 1; i <= N; ++ i)
    if (f[i] > ans) ans = f[i];
  writeln(ans);
  return 0;
}

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

void main() {
    immutable int MAX = 10^^5 + 1;
    bool[] table = new bool[](MAX);
    fill(table, true);
    table[0] = table[1] = false;

    int[][] P = new int[][](MAX);

    foreach (i; 2..MAX) {
        if (!table[i]) continue;
        P[i] ~= i;
        for (int j = i + i; j < MAX; j += i) {
            table[j] = false;
            P[j] ~= i;
        }
    }

    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto dp = new int[](MAX);

    foreach (i; 0..N) {
        int tmp = 0;
        foreach (j; P[A[i]]) tmp = max(tmp, dp[j] + 1);
        foreach (j; P[A[i]]) dp[j] = tmp;
    }

    dp.reduce!max.writeln;
}