This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \leq i \leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?

The first contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.

NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto S = scan;

      long ans;
      bool odd;
      foreach(s; S.group) {
        if (s[1] % 2 == 1) {
          if (odd) {
            odd = false;
          } else {
            odd = true;
            ans++;
          }
        } else {
          if (odd) ans++;
        }
      }

      return ans;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto S = scan;

      long ans;
      bool odd;
      foreach(s; S.group) {
        if (s[1] % 2 == 1) {
          if (odd) {
            odd = false;
          } else {
            odd = true;
            ans++;
          }
        } else {
          if (odd) {
            odd = false;
            ans++;
          } else {
            odd = false;
          }
        }
      }

      return ans;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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


Little X has met the following problem recently. Let's define f(x) as the sum of digits in decimal representation of number x (for example, f(1234) = 1 + 2 + 3 + 4). You are to calculate Of course Little X has solved this problem quickly, has locked it, and then has tried to hack others. He has seen the following C++ code:     ans = solve(l, r) % a;    if (ans <= 0)      ans += a; This code will fail only on the test with . You are given number a, help Little X to find a proper test for hack.

The first line contains a single integer a (1 ≤ a ≤ 1018).

Print two integers: l, r (1 ≤ l ≤ r < 10200) — the required test data. Leading zeros aren't allowed. It's guaranteed that the solution exists.

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		BigInt mm=med/(t*10);
		BigInt mm1=mm*10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=mm*t;
			if(mm1+j<med/t)x[j]+=t;
			else if(mm1+j==med/t)x[j]+=med-(mm1+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="10000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<200;i++)writeln(i," ",calc(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;i<1000;i++)dat[i]=-1;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		x=get(x);
		t=calc(x);
		//writeln(x," ",get(x)," ",t," ",t%a);
		if(dat[to!int(t%a)]!=-1&&dat[to!int(t%a)]!=x)
		{
			writeln(dat[to!int(t%a)]+1," ",x);
			break;
		}
		dat[to!int(t%a)]=x;
	}
	readln();
}

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

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

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

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

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

long A;

void main(string[] args) {
	try {
	for (; ; ) {
		A = readLong;

		long l = 1_000_000_000_000_000_000;
		long r = 2_000_000_000_000_000_000;

		long sum;
		(sum += (r - l)) %= A;
		foreach (i; 0 .. 18) {
			(sum += (r - l) / 10 * 45) %= A;
		}
		l -= sum;
		r -= sum;
		writeln(l, " ", r - 1);
	}
	} catch (EOFException) {}
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=(med/(t*10))*t;
			if(med/(t*10)*10+j<med/t)x[j]+=t;
			else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<200;i++)writeln(i," ",calc(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;i<1000;i++)dat[i]=-1;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=calc(get(x));
		//writeln(x," ",get(x)," ",t," ",t%a);
		if(dat[to!int(t%a)]!=-1&&dat[to!int(t%a)]!=t)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt n=med;
		int[]v;
		for(;;)
		{
			if(n==0)break;
			v~=n%10;
			n/=10;
		}
		BigInt cnt=0;
		BigInt t=1;
		for(int i=0;i<v.length;i++)
		{
			BigInt[]x;
			x.length=10;
			for(int j=0;j<=9;j++)
			{
				x[j]+=(med/(t*10))*t;
				if(med/(t*10)*10+j<med/t)x[j]+=t;
				else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
			}
			for(int j=0;j<=9;j++)cnt+=x[j]*j;
			t*=10;
		}
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=get(x);
		if(dat[to!int(t%a)]!=0)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=(med/(t*10))*t;
			if(med/(t*10)*10+j<med/t)x[j]+=t;
			else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<110;i++)writeln(i," ",get(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=calc(get(x));
		if(dat[to!int(t%a)]!=0)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

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

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

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

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

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

long A;

void main(string[] args) {
	try {
	for (; ; ) {
		A = readLong;

		long l = 1_000_000_000_000_000_000;
		long r = 2_000_000_000_000_000_000;

		long sum;
		foreach (i; 0 .. 18) {
			(sum += (r - l) / 10 * 45) %= A;
		}
		l -= sum;
		r -= sum;
		writeln(l, " ", r - 1);
	}
	} catch (EOFException) {}
}


You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.

The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table.

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.

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

void main ()
{
	int n, m;
	while (readf (" %s %s ", &n, &m) > 0)
	{
		string [] s;
		foreach (i; 0..n)
		{
			s ~= readln ().strip ();
		}
		auto b = new bool [n];
		long res = 0;
		foreach (j; 0..m)
		{
			bool ok = true;
			foreach (i; 1..n)
			{
				ok &= b[i] || (s[i][j] >= s[i - 1][j]);
			}
			if (!ok)
			{
				res++;
			}
			else
			{
				foreach (i; 1..n)
				{
					b[i] |= (s[i][j] > s[i - 1][j]);
				}
			}
		}
		writeln (res);
	}
}

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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto F = new string[N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp;
    }

    int ans = 0;
    string[][] xs = [ F ];
    void C() {
        foreach (x; xs) {
            auto y = x.map!((s) => s[0]).array;
            auto y1 = y.dup;
            y1.sort;
            if (y != y1) {
                ans++;
                xs = xs.map!((x) => x.map!"a[ 1 .. $ ]".array).array;
                return;
            }
        }
        string[][] nxs;
        foreach (x; xs) {
            char p = x[0][0];
            string[] buf;
            foreach (y; x) {
                if (p == y[0]) {
                    buf ~= y;
                } else {
                    p = y[0];
                    nxs ~= [buf];
                    buf = [ y ];
                }
            }
            if (!buf.empty) nxs ~= [buf];
        }
        //writeln(nxs);
        xs = nxs.map!((x) => x.map!"a[ 1 .. $ ]".array).array;
    }

    foreach (i; 0 .. M) {
        C();
    }
    writeln(ans);
}

import std.stdio;
import std.algorithm;

const int MAX = 105;

char[MAX][MAX] t;
bool[MAX][MAX] b;
int[MAX] f;

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

    foreach(i; 0..n) {
        foreach(j; 0..m) {
            readf(" %s", t[i][j+1]);
        }
    }

    foreach(j; 1..m+1) {
        foreach(k; 0..j) {
            auto combi = true;
            foreach(i; 0..n-1) {
                if (!b[i][k] && t[i][j] > t[i+1][j]) {
                    combi = false;
                    break;
                }
            }
            if (!combi) {
                continue;
            }

            if (f[j] < f[k] + 1) {
                f[j] = f[k] + 1;
                foreach(i; 0..n-1) {
                    b[i][j] = b[i][k] | (t[i][j] < t[i+1][j]);
                }
            }
        }
    }

    auto cols = 0;
    foreach(j; 1..m+1) {
        cols = max(cols, f[j]);
    }
    writeln(m - cols);
}

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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto F = new string[N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp;
    }

    int ans = 0;

    bool C(int x) {
        auto cs = new char[N];
        foreach (i; 0 .. N) {
            cs[i] = F[i][x];
        }
        auto ds = cs.dup;
        ds.sort;
        return cs == ds;
    }

    foreach (i; 0 .. M) {
        if (C(i)) {
            auto c = F[0][i];
            auto j = 0;
            while (j < N && c == F[j][i]) {
                j++;
            }
            F = F[0 .. j];
            N = j;
        } else {
            ans++;
        }
    }
    writeln(ans);
}


$$$m$$$ chairs are arranged in a circle sequentially. The chairs are numbered from $$$0$$$ to $$$m-1$$$. $$$n$$$ people want to sit in these chairs. The $$$i$$$-th of them wants at least $$$a[i]$$$ empty chairs both on his right and left side. More formally, if the $$$i$$$-th person sits in the $$$j$$$-th chair, then no one else should sit in the following chairs: $$$(j-a[i]) \bmod m$$$, $$$(j-a[i]+1) \bmod m$$$, ... $$$(j+a[i]-1) \bmod m$$$, $$$(j+a[i]) \bmod m$$$.Decide if it is possible to sit down for all of them, under the given limitations.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of people and the number of chairs. The next line contains $$$n$$$ integers, $$$a_1$$$, $$$a_2$$$, ... $$$a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the minimum number of empty chairs, on both sides of the $$$i$$$-th person. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$10^5$$$.

For each test case print "YES" (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and "NO" (without quotes) otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers).

NoteTest case $$$1$$$: $$$n>m$$$, so they can not sit down.Test case $$$2$$$: the first person can sit $$$2$$$-nd and the second person can sit in the $$$0$$$-th chair. Both of them want at least $$$1$$$ empty chair on both sides, chairs $$$1$$$ and $$$3$$$ are free, so this is a good solution.Test case $$$3$$$: if the second person sits down somewhere, he needs $$$2$$$ empty chairs, both on his right and on his left side, so it is impossible to find a place for the first person, because there are only $$$5$$$ chairs.Test case $$$4$$$: they can sit in the $$$1$$$-st, $$$4$$$-th, $$$7$$$-th chairs respectively.

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 m = RD;
		auto a = RDA;

		a.sort!"a > b";

		m -= a[0] * 2 + 1;
		foreach (i; 1..n-1)
		{
			m -= a[i] + 1;
		}
		if (m >= 1)
			ans[ti] = true;
	}

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


You are given an array $$$a[0 \dots n-1]$$$ of $$$n$$$ integers. This array is called a "valley" if there exists exactly one subarray $$$a[l \dots r]$$$ such that:  $$$0 \le l \le r \le n-1$$$,  $$$a_l = a_{l+1} = a_{l+2} = \dots = a_r$$$,  $$$l = 0$$$ or $$$a_{l-1} > a_{l}$$$,  $$$r = n-1$$$ or $$$a_r < a_{r+1}$$$. Here are three examples:  The first image shows the array [$$$3, 2, 2, 1, 2, 2, 3$$$], it is a valley because only subarray with indices $$$l=r=3$$$ satisfies the condition.The second image shows the array [$$$1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6$$$], it is a valley because only subarray with indices $$$l=0, r=2$$$ satisfies the codition.The third image shows the array [$$$1, 2, 3, 4, 3, 2, 1$$$], it is not a valley because two subarrays $$$l=r=0$$$ and $$$l=r=6$$$ that satisfy the condition.You are asked whether the given array is a valley or not.Note that we consider the array to be indexed from $$$0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases is smaller than $$$2\cdot10^5$$$.

For each test case, output "YES" (without quotes) if the array is a valley, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

NoteThe first three test cases are explained in the statement.

import std;

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!int;
    auto a = readln.splitter.map!(to!int).array;
    Tuple!(int, int)[] ans;
    foreach (l ; 0 .. a.length.to!int) {
      if (!(l == 0 || a[l - 1] > a[l]))
        continue;
      auto r = l;
      while (r + 1 < a.length && a[r + 1] == a[l])
        r++;
      if (r + 1 == a.length || a[r + 1] > a[r])
        ans ~= tuple(l, r);
      if (ans.length > 1)
        break;
    }
    writeln(ans.length == 1 ? "YES" : "NO");
  }
}


Two people are playing a game with a string $$$s$$$, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A player not able to make a turn loses.Your task is to determine which player will win if both play optimally.

The only line contains the string $$$s$$$, consisting of lowercase latin letters ($$$1 \leq |s| \leq 100\,000$$$), where $$$|s|$$$ means the length of a string $$$s$$$.

If the first player wins, print "Yes". If the second player wins, print "No".

NoteIn the first example the first player is unable to make a turn, so he loses.In the second example first player turns the string into "q", then second player is unable to move, so he loses.

import std.stdio;
import std.string;
import std.algorithm: max;

void main() {
	string s = readln;
	char[] t = ['#'];
	foreach (char c; s) {
		t ~= [c];
		if (c==t[$-2]) t=t[0..$-2];
	}
	if ((s.count - t.count) / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

// https://codeforces.com/contest/1104/problem/B
import std.stdio;
import std.string;
import std.range.primitives;

void main() {
    string s = readln.chomp;
    char[] stack;
    bool first = false;
    foreach(ch; s) {
        if(stack.length > 0 && stack[$-1] == ch) {
            stack.popBack();
            first = !first;
        } else {
            stack ~= ch;
        }
    }
    if(first)
        writeln("Yes");
    else
        writeln("No");
}

import std.stdio;
import std.string;
import std.algorithm: max;

void main() {
	string s = readln;
	string t = "#";
	foreach (char c; s) {
		t ~= c;
		if (c==t[$-2]) t=t[0..$-2];
	}
	if (s.count - t.count / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;
import std.algorithm: max;
import std.range.primitives: popBack;

void main() {
	string s = readln;
	char[] t = ['#'];
	foreach (char c; s) {
		if (c==t[$-1]) t.popBack;
		t ~= [c];
	}
	if ((s.count - t.count) / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;

void main() {
	string a = readln;
	bool conseguido = true;
	int b = 0;
	while (conseguido) {
		conseguido = false;
		for (int i=1; i<a.count; i++) {
			if (a[i-1] == a[i]) {
				a = a[i+1..$];
				conseguido = true;
				break;
			}
		}
		b += 1;
	}

	if (b % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;

void main() {
	string a = readln;
	bool conseguido = true;
	int b = 0;
	int last = 1;
	while (conseguido) {
		conseguido = false;
		for (int i=last; i<a.count; i++) {
			if (a[i-1] == a[i]) {
				a = a[0..i-1] ~ a[i+1..$];
				conseguido = true;
				last = i-2;
				break;
			}
		}
		b += 1;
	}

	if (b % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}


You are given an array $$$a$$$ of $$$n$$$ integers and a set $$$B$$$ of $$$m$$$ positive integers such that $$$1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor$$$ for $$$1\le i\le m$$$, where $$$b_i$$$ is the $$$i$$$-th element of $$$B$$$. You can make the following operation on $$$a$$$:  Select some $$$x$$$ such that $$$x$$$ appears in $$$B$$$. Select an interval from array $$$a$$$ of size $$$x$$$ and multiply by $$$-1$$$ every element in the interval. Formally, select $$$l$$$ and $$$r$$$ such that $$$1\leq l\leq r \leq n$$$ and $$$r-l+1=x$$$, then assign $$$a_i:=-a_i$$$ for every $$$i$$$ such that $$$l\leq i\leq r$$$. Consider the following example, let $$$a=[0,6,-2,1,-4,5]$$$ and $$$B=\{1,2\}$$$:  $$$[0,6,-2,-1,4,5]$$$ is obtained after choosing size $$$2$$$ and $$$l=4$$$, $$$r=5$$$. $$$[0,6,2,-1,4,5]$$$ is obtained after choosing size $$$1$$$ and $$$l=3$$$, $$$r=3$$$. Find the maximum $$$\sum\limits_{i=1}^n {a_i}$$$ you can get after applying such operation any number of times (possibly zero).

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2\leq n \leq 10^6$$$, $$$1 \leq m \leq \lfloor \frac{n}{2} \rfloor$$$) — the number of elements of $$$a$$$ and $$$B$$$ respectively.  The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-10^9\leq a_i \leq 10^9$$$).  The third line of each test case contains $$$m$$$ distinct positive integers $$$b_1,b_2,\ldots,b_m$$$ ($$$1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor$$$) — the elements in the set $$$B$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

For each test case print a single integer — the maximum possible sum of all $$$a_i$$$ after applying such operation any number of times.

NoteIn the first test, you can apply the operation $$$x=1$$$, $$$l=3$$$, $$$r=3$$$, and the operation $$$x=1$$$, $$$l=5$$$, $$$r=5$$$, then the array becomes $$$[0, 6, 2, 1, 4, 5]$$$.In the second test, you can apply the operation $$$x=2$$$, $$$l=2$$$, $$$r=3$$$, and the array becomes $$$[1, 1, -1, -1, 1, -1, 1]$$$, then apply the operation $$$x=2$$$, $$$l=3$$$, $$$r=4$$$, and the array becomes $$$[1, 1, 1, 1, 1, -1, 1]$$$. There is no way to achieve a sum bigger than $$$5$$$.

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

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

		auto g = b.fold !(gcd);
		auto lo = new int [g];
		lo[] = int.max;
		auto neg = new int [g];
		foreach (i; 0..n)
		{
			auto rem = i % g;
			if (a[i] < 0)
			{
				neg[rem] ^= 1;
				a[i] *= -1;
			}
			lo[rem] = min (lo[rem], a[i]);
		}

		long total = a.sum (0L);
		long best0 = g.iota.filter !(i => neg[i] == 0)
		    .map !(i => lo[i]).sum (0L);
		long best1 = g.iota.filter !(i => neg[i] == 1)
		    .map !(i => lo[i]).sum (0L);
		writeln (total - 2 * min (best0, best1));
	}
}


After learning about polynomial hashing, Heidi decided to learn about shift-xor hashing. In particular, she came across this interesting problem.Given a bitstring $$$y \in \{0,1\}^n$$$ find out the number of different $$$k$$$ ($$$0 \leq k < n$$$) such that there exists $$$x \in \{0,1\}^n$$$ for which $$$y = x \oplus \mbox{shift}^k(x).$$$In the above, $$$\oplus$$$ is the xor operation and $$$\mbox{shift}^k$$$ is the operation of shifting a bitstring cyclically to the right $$$k$$$ times. For example, $$$001 \oplus 111 = 110$$$ and $$$\mbox{shift}^3(00010010111000) = 00000010010111$$$.

The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$), the length of the bitstring $$$y$$$. The second line contains the bitstring $$$y$$$.

Output a single integer: the number of suitable values of $$$k$$$.

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;
string Y;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      Y = readToken();

      auto cnt = new int[N + 1];
      foreach (a; 1 .. N + 1) {
        ++cnt[gcd(a, N)];
      }

      int ans;
      foreach (a; 1 .. N + 1) {
        if (N % a == 0) {
          auto sum = new int[a];
          foreach (i; 0 .. N) {
            sum[i % a] += Y[i] - '0';
          }
          debug {
            writeln(a, ": ", sum);
          }
          if (sum.all!"a % 2 == 0") {
            ans += cnt[a];
          }
        }
      }
      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)); }

int N;
string Y;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      Y = readToken();

      int ans = N - 1;
      if (Y.count('0') == N) {
        ++ans;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns. Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?

The first line contains two integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 109) — the number of lanterns and the length of the street respectively.  The next line contains n integers ai (0 ≤ ai ≤ l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the street.

Print the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.

NoteConsider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment [3, 5]. Thus, the whole street will be lit.

import std.stdio;
import std.string;
import std.conv;
import std.regex;
import std.algorithm.iteration;
import std.algorithm.searching;
import std.algorithm.sorting;
import  std.array;
import std.range;
import std.algorithm.comparison;

void main()
{
  auto nl = stdin.readln.strip.split(regex(` +`)).map!(to!int).array;
  auto n = nl[0], l=nl[1];
  auto as = stdin.readln.strip.split(regex(` +`)).map!(to!int).array;
  double a = any!(a => a==l)(as) ? 0 : l - as.maxElement;
  double b = any!(a => a==0)(as) ? 0 : as.minElement;
  as.sort();
  writef("%.9f", max( (as.length>1 ? as.slide(2).map!"a[1]-a[0]".array.maxElement.to!double / 2:0), a, b));
}

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

int main(string[] argv)
{
	int n;
	int l;
	scanf("%d %d", &n, &l);
	int[] lanterns; lanterns.length = n;
	foreach (int i; 0..n)
	{
		scanf("%d", &lanterns[i]);
	}
	lanterns.sort!("a < b");
	int max = (lanterns[0] - 0) * 2;
	foreach (int i; 1..lanterns.length)
	{
		int diff = lanterns[i] - lanterns[i-1];
		if (diff > max) max = diff;
	}
	if (((l - lanterns[$-1]) * 2) > max) max = (l - lanterns[$-1]) * 2;
	printf("%.10f", (cast(float)max / 2.0f));
	return 0;
}

import std.stdio: writefln, readf;
import std.string;
import std.conv: to;
import std.array: array;
import std.algorithm: map, sort, max;

void main() {
	int n, l;
	readf("%d %d", &n, &l);

	int[] a = new int[n];
	foreach (ref x; a)
		readf(" %d", &x);
	a.sort;

	int dif;
	double max_dif;
   	if (a[0] > l - a[n-1]) max_dif = a[0] * 2; else max_dif = (l - a[$-1]) * 2;
	foreach (int i; 1..n) {
		dif = a[i] - a[i - 1];
		if (dif > max_dif) max_dif = dif;
	}

	writefln("%.10f", max_dif / 2.0);
}

import std.stdio: writefln, readf;
import std.string;
import std.conv: to;
import std.array: array;
import std.algorithm: map, sort, max;

void main() {
	int n, l;
	readf("%d %d", &n, &l);

	int[] a = new int[n];
	foreach (ref x; a)
		readf(" %d", &x);
	a.sort;

	int dif;
	double max_dif = max(a[0], l - a[n-1]) * 2;
	foreach (int i; 1..n) {
		dif = a[i] - a[i - 1];
		if (dif > max_dif) max_dif = dif;
	}

	writefln("%.10f", max_dif / 2.0);
}

import std.stdio: writefln, readf;
import std.string;
import std.conv: to;
import std.array: array;
import std.algorithm: map, sort, max;

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

	int l;
	readf(" %s", &l);

	int[] a = new int[n];
	foreach (ref x; a)
		readf(" %s", &x);
	a.sort;

	double dif = max(a[0], l - a[n-1]) * 2;
	for (int i = 1; i < n; i++) {
		dif = max(dif, a[i] - a[i - 1]);
	}

	dif /= 2.0;

	writefln("%.10f", dif);
}

import std.stdio: writefln, readln;
import std.string;
import std.conv: to;
import std.array: array;
import std.algorithm: map, sort, max;

void main() {
	int[2] line = readln.chomp.split(" ").map!(to!int).array;
	int n = line[0];
	int l = line[1];

	int[] a = readln.chomp.split(" ").map!(to!int).array.sort.array;

	double dif = max(a[0], l - a[n-1]) * 2;
	for (int i = 1; i < n; i++) {
		dif = max(dif, a[i] - a[i - 1]);
	}

	dif /= 2.0;

	writefln("%.10f", dif);
}

import std.stdio;
import std.algorithm;

void main() { while (solve()) {} }

bool solve() {
	int n;
	if (readf(" %s", &n) != 1) return false;

	int len;
	readf(" %s", &len);
	int[] a = new int[n];
	foreach (ref x; a)
		readf(" %s", &x);
	a.sort;
	int res = 0;
	foreach (i; 1..n) {
		res = max(res, a[i] - a[i - 1]);
	}
	res = max(res, max(len - a[$ - 1], a[0]) * 2);

	writef("%s", res / 2);
	if (res % 2 != 0)
		writef(".5");
	writeln;

	return true;
}

import std.stdio: writeln, readln;
import std.string;
import std.conv: to;
import std.array: array;
import std.algorithm: map, sort, max;

void main() {
	int[2] line = readln.chomp.split(" ").map!(to!int).array;
	int n = line[0];
	int l = line[1];

	int[] a = readln.chomp.split(" ").map!(to!int).array.sort.array;

	float dif = max(a[0], l - a[a.count - 1]);
	for (int i = 1; i < n; i++) {
		dif = max(dif, (a[i] - a[i - 1]) / 2.0);
	}

	dif.writeln;
}


You are given four integers $$$n$$$, $$$c_0$$$, $$$c_1$$$ and $$$h$$$ and a binary string $$$s$$$ of length $$$n$$$.A binary string is a string consisting of characters $$$0$$$ and $$$1$$$.You can change any character of the string $$$s$$$ (the string should be still binary after the change). You should pay $$$h$$$ coins for each change.After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character $$$0$$$ you should pay $$$c_0$$$ coins, to buy the character $$$1$$$ you should pay $$$c_1$$$ coins.Find the minimum number of coins needed to buy the string.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of the description of each test case contains four integers $$$n$$$, $$$c_{0}$$$, $$$c_{1}$$$, $$$h$$$ ($$$1 \leq n, c_{0}, c_{1}, h \leq 1000$$$). The second line of the description of each test case contains the binary string $$$s$$$ of length $$$n$$$.

For each test case print a single integer — the minimum number of coins needed to buy the string.

NoteIn the first test case, you can buy all characters and pay $$$3$$$ coins, because both characters $$$0$$$ and $$$1$$$ costs $$$1$$$ coin.In the second test case, you can firstly change $$$2$$$-nd and $$$4$$$-th symbols of the string from $$$1$$$ to $$$0$$$ and pay $$$2$$$ coins for that. Your string will be $$$00000$$$. After that, you can buy the string and pay $$$5 \cdot 10 = 50$$$ coins for that. The total number of coins paid will be $$$2 + 50 = 52$$$.

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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto c0 = RD!int;
		auto c1 = RD!int;
		auto h = RD!int;
		auto s = RD!string;

		auto c = c0 < c1 ? '1' : '0';
		auto d = abs(c0 - c1) - h;
		foreach (i; 0..n)
		{
			if (s[i] == '0')
				ans[ti] += c0;
			else
				ans[ti] += c1;
			if (s[i] == c)
			{
				ans[ti] -= max(0, d);
			}
		}
	}

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


This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.

NoteArray from sample: $$$[1, 2, 0, 3]$$$.

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;
	foreach (ti; 0..t)
	{
		auto n = RD!int;

		long[] arr = [1, 2, 3];
		writeln("? 1 2 3");
		stdout.flush;
		auto last = RD;
		long p = 4;
		while (p <= n)
		{
			writeln("? ", arr[0], " ", arr[1], " ", p);
			stdout.flush;
			auto s1 = RD;
			writeln("? ", arr[0], " ", arr[2], " ", p);
			stdout.flush;
			auto s2 = RD;
			if (max(s1, s2) < last)
				++p;
			else if (s2 >= s1)
			{
				if (s1 >= last)
					arr = [p, arr[0], arr[2]];
				else
					arr = [p, arr[2], arr[0]];
				last = s2;
				++p;
			}
			else
			{
				if (s2 >= last)
					arr = [p, arr[0], arr[1]];
				else
					arr = [p, arr[1], arr[0]];
				last = s1;
				++p;
			}
		}
		long[] ans;
		foreach (i; 1..n+1)
		{
			bool ok = true;
			foreach (e; arr)
			{
				if (e == i)
					ok = false;
			}
			if (!ok) continue;

			writeln("? ", arr[1], " ", arr[2], " ", i);
			stdout.flush;
			auto s1 = RD;
			writeln("? ", arr[0], " ", arr[2], " ", i);
			stdout.flush;
			auto s2 = RD;
			writeln("? ", arr[0], " ", arr[1], " ", i);
			stdout.flush;
			auto s3 = RD;
			if (s1 == last)
				ans = [arr[1], arr[2]];
			else if (s2 == last)
				ans = [arr[0], arr[2]];
			else if (s3 == last)
				ans = [arr[0], arr[1]];
			break;
		}
		writeln("! ", ans[0], " ", ans[1]);
		stdout.flush;
	}

	//stdout.flush;
	debug readln;
}


The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them.

The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$.

For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them.

// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		auto a = new int [n];
		auto b = new int [n];
		auto m = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s %s") (a[i], b[i], m[i]);
		}

		alias Segment = Tuple !(int, q{hi}, int, q{lo}, int, q{id});
		Segment [] [int] s;
		foreach (i; 0..n)
		{
			int total = a[i] + b[i] - m[i];
			int hiA = min (a[i], m[i]);
			int loB = m[i] - hiA;
			int hiB = min (b[i], m[i]);
			int loA = m[i] - hiB;
			s[total] ~= Segment (a[i] - loA, a[i] - hiA, i);
		}

		auto x = new long [n];
		auto y = new long [n];
		int res = 0;
		foreach (total, t; s)
		{
			sort (t);
			int cur = int.min;
			foreach (ref seg; t)
			{
				if (cur < seg.lo)
				{
					cur = seg.hi;
					res += 1;
				}
				auto i = seg.id;
				x[seg.id] = a[i] - cur;
				y[seg.id] = m[i] - x[seg.id];
			}
		}

		writeln (res);
		foreach (i; 0..n)
		{
			writeln (x[i], " ", y[i]);
		}
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		auto a = new int [n];
		auto b = new int [n];
		auto m = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s %s") (a[i], b[i], m[i]);
		}

		alias Segment = Tuple !(int, q{hi}, int, q{lo}, int, q{id});
		Segment [] [int] s;
		foreach (i; 0..n)
		{
			int total = a[i] + b[i] - m[i];
			int hiA = min (a[i], m[i]);
			int loB = m[i] - hiA;
			int hiB = min (b[i], m[i]);
			int loA = m[i] - hiB;
			s[total] ~= Segment (a[i] - loA, a[i] - hiA, i);
		}

		auto x = new long [n];
		auto y = new long [n];
		int res = 0;
		foreach (total, t; s)
		{
			sort (t);
			int cur = int.min;
			foreach (ref seg; t)
			{
				if (cur < seg.lo)
				{
					cur = seg.hi;
					res += 1;
				}
				auto i = seg.id;
				x[seg.id] = a[i] - cur;
				y[seg.id] = m[i] - x[seg.id];
			}
		}

		foreach (i; 0..n)
		{
			writeln (x[i], " ", y[i]);
		}
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		auto a = new int [n];
		auto b = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s") (a[i], b[i]);
		}

		auto x = new long [n];
		auto y = new long [n];
		long sa = 0;
		long sb = 0;
		foreach (i; 0..n)
		{
			x[i] = min (a[i], m);
			y[i] = m - x[i];
			sa += a[i] - x[i];
			sb += b[i] - y[i];
		}

		foreach (i; 0..n)
		{
			auto cur = max (0, sb - sa) >> 1;
			auto yAlt = min (b[i], m);
			auto xAlt = m - yAlt;
			auto delta = min (cur, x[i] - xAlt);
			x[i] -= delta;
			y[i] += delta;
			sa += delta;
			sb -= delta;
		}

		writeln (abs (sa - sb));
		foreach (i; 0..n)
		{
			writeln (x[i], " ", y[i]);
		}
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		auto a = new int [n];
		auto b = new int [n];
		auto m = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s %s") (a[i], b[i], m[i]);
		}

		alias Segment = Tuple !(int, q{hi}, int, q{lo}, int, q{id});
		Segment [] [int] s;
		foreach (i; 0..n)
		{
			int total = a[i] + b[i] - m[i];
			int hiA = min (a[i], m[i]);
			int loB = m[i] - hiA;
			int hiB = min (b[i], m[i]);
			int loA = m[i] - hiB;
			s[total] ~= Segment (a[i] - loA, a[i] - hiA, i);
		}

		auto x = new long [n];
		auto y = new long [n];
		int res = 0;
		foreach (total, t; s)
		{
			sort (t);
			writeln (t);
			int cur = int.min;
			foreach (ref seg; t)
			{
				if (cur < seg.lo)
				{
					cur = seg.hi;
					res += 1;
				}
				auto i = seg.id;
				x[seg.id] = a[i] - cur;
				y[seg.id] = m[i] - x[seg.id];
			}
		}

		writeln (res);
		foreach (i; 0..n)
		{
			writeln (x[i], " ", y[i]);
		}
	}
}


Johnny's younger sister Megan had a birthday recently. Her brother has bought her a box signed as "Your beautiful necklace — do it yourself!". It contains many necklace parts and some magic glue. The necklace part is a chain connecting two pearls. Color of each pearl can be defined by a non-negative integer. The magic glue allows Megan to merge two pearls (possibly from the same necklace part) into one. The beauty of a connection of pearls in colors $$$u$$$ and $$$v$$$ is defined as follows: let $$$2^k$$$ be the greatest power of two dividing $$$u \oplus v$$$ — exclusive or of $$$u$$$ and $$$v$$$. Then the beauty equals $$$k$$$. If $$$u = v$$$, you may assume that beauty is equal to $$$20$$$.Each pearl can be combined with another at most once. Merging two parts of a necklace connects them. Using the glue multiple times, Megan can finally build the necklace, which is a cycle made from connected necklace parts (so every pearl in the necklace is combined with precisely one other pearl in it). The beauty of such a necklace is the minimum beauty of a single connection in it. The girl wants to use all available necklace parts to build exactly one necklace consisting of all of them with the largest possible beauty. Help her!

The first line contains $$$n$$$ $$$(1 \leq n \leq 5 \cdot 10^5)$$$ — the number of necklace parts in the box. Each of the next $$$n$$$ lines contains two integers $$$a$$$ and $$$b$$$ $$$(0 \leq a, b < 2^{20})$$$, which denote colors of pearls presented in the necklace parts. Pearls in the $$$i$$$-th line have indices $$$2i - 1$$$ and $$$2i$$$ respectively.

The first line should contain a single integer $$$b$$$ denoting the maximum possible beauty of a necklace built from all given parts. The following line should contain $$$2n$$$ distinct integers $$$p_i$$$ $$$(1 \leq p_i \leq 2n)$$$ — the indices of initial pearls in the order in which they appear on a cycle. Indices of pearls belonging to the same necklace part have to appear at neighboring positions in this permutation (so $$$1\,4\,3\,2$$$ is not a valid output, whereas $$$2\,1\,4\,3$$$ and $$$4\,3\,1\,2$$$ are). If there are many possible answers, you can print any.

NoteIn the first example the following pairs of pearls are combined: $$$(7, 9)$$$, $$$(10, 5)$$$, $$$(6, 1)$$$, $$$(2, 3)$$$ and $$$(4, 8)$$$. The beauties of connections equal correspondingly: $$$3$$$, $$$3$$$, $$$3$$$, $$$20$$$, $$$20$$$.The following drawing shows this construction.

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

alias Edge = Tuple !(int, q{u}, int, q{v});

int [] euler (const ref Edge [] e, int p)
{
	auto n = e.length.to !(int);
	auto m = 1 << p;
	auto mAnd = m - 1;
	auto adj = new int [] [m];
	foreach (i; 0..n)
	{
		adj[e[i].u & mAnd] ~= i;
		adj[e[i].v & mAnd] ~= i;
	}
	if (!adj.all !(line => !(line.length & 1)))
	{
		return null;
	}

	int [] res;
	res.reserve (n * 2);
	auto used = new bool [n];

	void recur (int v)
	{
		while (true)
		{
			while (!adj[v].empty && used[adj[v].front])
			{
				adj[v].popFront ();
			}
			if (adj[v].empty)
			{
				break;
			}

			int i = adj[v].front;
			used[i] = true;
			adj[v].popFront;
			int u = e[i].u & mAnd;
			bool pickU = true;
			if (u == (v & mAnd))
			{
				u = e[i].v & mAnd;
				pickU = false;
			}
			recur (u);
			res ~= i * 2 + 1 + !pickU;
			res ~= i * 2 + 1 + pickU;
		}
	}

	foreach (v; 0..m)
	{
		if (!adj[v].empty)
		{
			recur (v);
			break;
		}
	}
	if (res.length != n * 2)
	{
		res = null;
	}
	return res;
}

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto e = new Edge [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s") (e[i].u, e[i].v);
		}
		int lo = 0;
		int hi = 21;
		while (lo + 1 < hi)
		{
			int me = (lo + hi) / 2;
			auto cur = euler (e, me);
			if (cur is null)
			{
				hi = me;
			}
			else
			{
				lo = me;
			}
		}

		writeln (lo);
		auto res = euler (e, lo);
		writefln !("%(%s %)") (res);
	}
}


The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular n-gon. They are numbered in clockwise order with numbers from 1 to n.The site of the University has already conducted a voting that estimated each sculpture's characteristic of ti — the degree of the sculpture's attractiveness. The values of ti can be positive, negative or zero.When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that:   the remaining sculptures form a regular polygon (the number of vertices should be between 3 and n),  the sum of the ti values of the remaining sculptures is maximized. Help the Vice Rector to analyze the criticism — find the maximum value of ti sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved.

The first input line contains an integer n (3 ≤ n ≤ 20000) — the initial number of sculptures. The second line contains a sequence of integers t1, t2, ..., tn, ti — the degree of the i-th sculpture's attractiveness ( - 1000 ≤ ti ≤ 1000). The numbers on the line are separated by spaces.

Print the required maximum sum of the sculptures' attractiveness.

NoteIn the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 и 3.

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

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

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

    auto ans = arr.sum();
    foreach (e; 3 .. n) {
        if (n % e != 0) continue;

        auto d = n / e;

        foreach (i; 0 .. d) {
            auto cur = arr2[i..$].stride(d).take(e).sum;

            debug { writeln(e, ' ', d, ' ', i, ' ', cur); }

            ans = max(ans, cur);
        }
    }

    ans.writeln;
}


You are given a string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.A substring of a string is a contiguous subsequence of that string. So, string "forces" is substring of string "codeforces", but string "coder" is not.Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).It is guaranteed that there is at least two different characters in $$$s$$$.Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.Since the answer can be rather large (not very large though) print it modulo $$$998244353$$$.If you are Python programmer, consider using PyPy instead of Python when you submit your code.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. It is guaranteed that there is at least two different characters in $$$s$$$.

Print one integer — the number of ways modulo $$$998244353$$$ to remove exactly one substring from $$$s$$$ in such way that all remaining characters are equal.

NoteLet $$$s[l; r]$$$ be the substring of $$$s$$$ from the position $$$l$$$ to the position $$$r$$$ inclusive.Then in the first example you can remove the following substrings:   $$$s[1; 2]$$$;  $$$s[1; 3]$$$;  $$$s[1; 4]$$$;  $$$s[2; 2]$$$;  $$$s[2; 3]$$$;  $$$s[2; 4]$$$. In the second example you can remove the following substrings:   $$$s[1; 4]$$$;  $$$s[1; 5]$$$;  $$$s[1; 6]$$$;  $$$s[1; 7]$$$;  $$$s[2; 7]$$$;  $$$s[3; 7]$$$. In the third example you can remove the following substrings:   $$$s[1; 1]$$$;  $$$s[1; 2]$$$;  $$$s[2; 2]$$$.

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.random;
import std.range;
import std.stdio;
import std.string;

immutable long mod = 998_244_353;

void main()
{
    int n;
    readf!" %s "(n);
    string s = readln.strip;

    long pre = 0, post = 0;
    foreach (c; s) {
        if (c == s[0]) pre++;
        else break;
    }

    foreach_reverse (c; s) {
        if (c == s[n-1]) post++;
        else break;
    }

    long ans = 1 + pre + post;

    if (s[0] == s[n-1]) {
        ans = (ans + pre * post % mod) % mod;
    }

    writeln(ans);
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);

  auto bf = new int[](26), af = new int[](26);
  int kind = 0;
  foreach (c; s) {
    af[c - 'a']++;
    if (af[c - 'a'] == 1) {
      kind++;
    }
  }
  int ans = 0, mod = 998244353;
  for (int i = 0, j = 0; i < n; i++) { // [i, j) remove
    while (j < n && kind >= 2) {
      if ((--af[s[j++] - 'a']) == 0) {
        kind--;
      }
    }
    if (kind == 1) {
      ans += (n - j + 1);
      ans %= mod;
    }
    if ((++af[s[i] - 'a']) == 1) {
      kind++;
    }
  }
  writeln(ans);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

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

alias Tree = RBT!int;
alias ll = long;
const ll mod = 998244353;

void main() {
  GC.disable();

  int n;
  readf(" %s\n", n);
  string s = readln.strip;

  long sb = -1;
  foreach(i; 0 .. s.length) if (s[i] != s[0]) { sb = i; break; }

  long se = -1;
  foreach_reverse(i; 0 .. s.length) if (s[i] != s[$-1]) { se = i; break; }

  long ans = 1;
  if (sb == -1) {
    ans = (s.length * (s.length+1) / 2 ) % mod;
  } else if (s[0] == s[$-1]) {
    ans = ((sb + 1L) * ( s.length - se ) ) % mod;

  } else { // two different cases?
    ans =  ( (sb + 1) +  ( s.length - se)  - 1 ) % mod;
  }

  writeln(ans);

}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);

  auto bf = new int[](26), af = new int[](26);
  int kind = 0;
  foreach (c; s) {
    af[c - 'a']++;
    if (af[c - 'a'] == 1) {
      kind++;
    }
  }
  int ans = 0;
  for (int i = 0, j = 0; i < n; i++) { // [i, j) remove
    while (j < n && kind >= 2) {
      if ((--af[s[j++] - 'a']) == 0) {
        kind--;
      }
      // writeln(j, " ", kind);
    }
    if (kind == 1) {
      ans += (n - j + 1);
    }
    // writeln("! ", i, " ", j, " ", kind, " ", ans);
    // while (i <= j && kind < 2) {
    // if ((++af[s[i++] - 'a']) == 1) {
    // kind++;
    // }
    // if (kind == 1) {
    // ans++;
    // }
    // }
    if ((++af[s[i] - 'a']) == 1) {
      kind++;
    }
    // writeln(i, " ", j, " ", kind, " ", ans);
  }
  writeln(ans);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

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

alias Tree = RBT!int;
alias ll = long;
const ll mod = 998244353;

void main() {
  GC.disable();

  int n;
  readf(" %s\n", n);
  string s = readln.strip;

  int sb = -1;
  foreach(i; 0 .. s.length) if (s[i] != s[0]) { sb = i; break; }

  int se = -1;
  foreach_reverse(i; 0 .. s.length) if (s[i] != s[$-1]) { se = i; break; }

  long ans = 1;
  if (sb == -1) {
    ans = (s.length * (s.length+1) / 2 ) % mod;
  } else if (s[0] == s[$-1]) {
    ans = ((sb + 1) * ( s.length - se ) ) % mod;

  } else { // two different cases?
    ans =  ( (sb + 1) +  ( s.length - se)  - 1 ) % mod;
  }

  writeln(ans);

}


Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn. Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input. The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).

Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

NoteIn the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.

module B;

import std.stdio;
import std.math;

void main(string[] args)
{
	long count;
	readf("%d", &count);

	debug
	{
		writeln("Array length: ", count);
	}

	long sum;
	long prev = 0, curr;

	for (long i = 0; i < count; ++i)
	{
		readf(" %d", &curr);
		sum += abs(curr - prev);
		prev = curr;
	}

	writeln(sum);
}


You have $$$a$$$ coins of value $$$n$$$ and $$$b$$$ coins of value $$$1$$$. You always pay in exact change, so you want to know if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ ($$$0 \le x \le a$$$) coins of value $$$n$$$ and $$$y$$$ ($$$0 \le y \le b$$$) coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$.You have to answer $$$q$$$ independent test cases.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases. Then $$$q$$$ test cases follow. The only line of the test case contains four integers $$$a$$$, $$$b$$$, $$$n$$$ and $$$S$$$ ($$$1 \le a, b, n, S \le 10^9$$$) — the number of coins of value $$$n$$$, the number of coins of value $$$1$$$, the value $$$n$$$ and the required total value.

For the $$$i$$$-th test case print the answer on it — YES (without quotes) if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ coins of value $$$n$$$ and $$$y$$$ coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

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

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

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

void main()
{
	auto q = RD!int;
	auto ans = new bool[](q);
	foreach (i; 0..q)
	{
		auto a = RD;
		auto b = RD;
		auto n = RD;
		auto S = RD;

		auto x = S / n;
		S -= min(x, a) * n;
		ans[i] = S <= b;
	}

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


Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).  Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.

The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai   — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.

In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace. Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.

NoteIn the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.In the second sample there is only one way to compose a necklace.

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int odd = a.count !(x => x % 2 != 0);
		int g = a.reduce !(gcd);
		string res;
		if (n == 1)
		{
			writeln (a.front);
			res = 'a'.repeat (a.front).array;
		}
		else if (odd >= 2)
		{
			writeln (0);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c).array;
			}
		}
		else if (odd == 1)
		{
			writeln (g);
			char d;
			foreach (i, c; a)
			{
				if (c & 1)
				{
					d = cast (char) (i + 'a');
					c -= g;
				}
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / (2 * g)).array;
			}
			res ~= d ~ res.retro.text;
			res = res.repeat (g).join ("");
		}
		else
		{
			writeln (g);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / g).array;
			}
			res ~= res.retro.text;
			res = res.repeat (g / 2).join ("");
		}
		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int total = a.sum;
		int odd = a.count !(x => x % 2 != 0);
		string res;
		if (odd >= 2)
		{
			writeln (0);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c).array;
			}
		}
		else if (odd == 1)
		{
			writeln (1);
			char d;
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / 2).array;
			}
			res ~= res.retro.text;
			res ~= d;
		}
		else
		{
			int g = a.reduce !(gcd);
			writeln (g);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / g).array;
			}
			res ~= res.retro.text;
			res = res.repeat (g / 2).join ("");
		}
		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int total = a.sum;
		int odd = a.count !(x => x % 2 != 0);
		string res;
		if (odd >= 2)
		{
			writeln (0);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c).array;
			}
		}
		else if (odd == 1)
		{
			writeln (1);
			char d;
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / 2).array;
				if (c & 1)
				{
					d = cast (char) (i + 'a');
				}
			}
			res ~= res.retro.text;
			res ~= d;
		}
		else
		{
			int g = a.reduce !(gcd);
			writeln (g);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / g).array;
			}
			res ~= res.retro.text;
			res = res.repeat (g / 2).join ("");
		}
		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int odd = a.count !(x => x % 2 != 0);
		string res;
		if (n == 1)
		{
			writeln (a.front);
			res = 'a'.repeat (a.front).array;
		}
		else if (odd >= 2)
		{
			writeln (0);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c).array;
			}
		}
		else if (odd == 1)
		{
			writeln (1);
			char d;
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / 2).array;
				if (c & 1)
				{
					d = cast (char) (i + 'a');
				}
			}
			res ~= res.retro.text;
			res ~= d;
		}
		else
		{
			int g = a.reduce !(gcd);
			writeln (g);
			foreach (i, c; a)
			{
				res ~= (cast (char) (i + 'a'))
				    .repeat (c / g).array;
			}
			res ~= res.retro.text;
			res = res.repeat (g / 2).join ("");
		}
		writeln (res);
	}
}


Bizon the Champion isn't just friendly, he also is a rigorous coder.Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.

A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).

Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.

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

long[] divisors(long n) {
    if (n == 1) return [1];
    static long[][long] cache;
    if (n in cache) return cache[n];
    long[] ret = [1, n];
    for (long x = 2; x * x <= n; x++) {
        if (n % x == 0) {
            auto y = n / x;
            ret ~= x;
            if (x != y) ret ~= y;
        }
    }
    ret.sort;
    return cache[n] = ret.dup;
}

int[] primes;
static this() {
    auto L = 1_000_001;
    auto isPrime = new bool[L];
    isPrime[] = true;
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i < L; i++) {
        if (isPrime[i]) {
            primes ~= i;
            for (int j = i + i; j < L; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

bool isPrime(long x) {
    foreach (p; primes) {
        if (x == p) return true;
        if (x % p == 0) return false;
    }
    return true;
}

void main() {
    long X, K; scanf("%lld %lld\n", &X, &K);

    long[] enumerate(long x, long k, long limit) {
        if (k == 0) return [x];
        if (k == 1) return x.divisors;
        if (x == 1) return [1L];
        if (x.isPrime) {
            long[] ret;
            foreach (i; 0 .. min(k, limit)) ret ~= 1;
            if (k < limit) {
                return ret ~ x;
            } else {
                return ret;
            }
        }
        long[] ret;
        auto ds = divisors(x);
        foreach (d; ds) {
            auto ns = enumerate(d, k - 1, limit - ret.length);
            ret ~= ns;
            if (ret.length >= limit) break;
        }
        return ret;
    }

    writefln("%(%s %)", enumerate(X, K, 100_000)[0 .. min($, 100_000)]);

}

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

long[] divisors(long n) {
    if (n == 1) return [1];
    static long[][long] cache;
    if (n in cache) return cache[n];
    long[] ret = [1, n];
    for (long x = 2; x * x <= n; x++) {
        if (n % x == 0) {
            auto y = n / x;
            ret ~= x;
            if (x != y) ret ~= y;
        }
    }
    ret.sort;
    return cache[n] = ret;
}

int[] primes;
static this() {
    auto L = 1_000_001;
    auto isPrime = new bool[L];
    isPrime[] = true;
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i < L; i++) {
        if (isPrime[i]) {
            primes ~= i;
            for (int j = i + i; j < L; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

bool isPrime(long x) {
    foreach (p; primes) {
        if (x == p) return true;
        if (x % p == 0) return false;
    }
    return true;
}

void main() {
    long X, K; scanf("%lld %lld\n", &X, &K);

    long[] enumerate(long x, long k, long limit) {
        if (k == 0) return [];
        if (k == 1) return x.divisors;
        if (x == 1) return [1L];
        if (x.isPrime) {
            long[] ret;
            foreach (i; 0 .. min(k, limit)) ret ~= 1;
            if (k < limit) {
                return ret ~ x;
            } else {
                return ret;
            }
        }
        long[] ret;
        auto ds = divisors(x);
        foreach (d; ds) {
            auto ns = enumerate(d, k - 1, limit - ret.length);
            ret ~= ns;
            if (ret.length >= limit) break;
        }
        return ret;
    }

    writefln("%(%s %)", enumerate(X, K, 100_000));

}

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

long[] divisors(long n) {
    if (n == 1) return [1];
    static long[][long] cache;
    if (n in cache) return cache[n];
    long[] ret = [1, n];
    for (long x = 2; x * x <= n; x++) {
        if (n % x == 0) {
            auto y = n / x;
            ret ~= x;
            if (x != y) ret ~= y;
        }
    }
    ret.sort;
    return cache[n] = ret.dup;
}

int[] primes;
static this() {
    auto L = 1_000_001;
    auto isPrime = new bool[L];
    isPrime[] = true;
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i < L; i++) {
        if (isPrime[i]) {
            primes ~= i;
            for (int j = i + i; j < L; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

bool isPrime(long x) {
    foreach (p; primes) {
        if (x == p) return true;
        if (x % p == 0) return false;
    }
    return true;
}

void main() {
    long X, K; scanf("%lld %lld\n", &X, &K);

    long[] enumerate(long x, long k, long limit) {
        if (k == 0) return [x];
        if (k == 1) return x.divisors;
        if (x == 1) return [1L];
        if (x.isPrime) {
            long[] ret;
            foreach (i; 0 .. min(k, limit)) ret ~= 1;
            if (k < limit) {
                return ret ~ x;
            } else {
                return ret;
            }
        }
        long[] ret;
        auto ds = divisors(x);
        foreach (d; ds) {
            auto ns = enumerate(d, k - 1, limit - ret.length);
            ret ~= ns;
            if (ret.length >= limit) break;
        }
        return ret;
    }

    writefln("%(%s %)", enumerate(X, K, 100_000));

}

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

long[] divisors(long n) {
    if (n == 1) return [1];
    static long[][long] cache;
    if (n in cache) return cache[n];
    long[] ret = [1, n];
    for (long x = 2; x * x <= n; x++) {
        if (n % x == 0) {
            auto y = n / x;
            ret ~= x;
            if (x != y) ret ~= y;
        }
    }
    ret.sort;
    return cache[n] = ret;
}

int[] primes;
static this() {
    auto L = 1_000_001;
    auto isPrime = new bool[L];
    isPrime[] = true;
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i < L; i++) {
        if (isPrime[i]) {
            primes ~= i;
            for (int j = i + i; j < L; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

bool isPrime(long x) {
    foreach (p; primes) {
        if (x == p) return true;
        if (x % p == 0) return false;
    }
    return true;
}

void main() {
    long X, K; scanf("%lld %lld\n", &X, &K);

    long[] enumerate(long x, long k, long limit) {
        if (k == 0) return [x];
        if (k == 1) return x.divisors;
        if (x == 1) return [1L];
        if (x.isPrime) {
            long[] ret;
            foreach (i; 0 .. min(k, limit)) ret ~= 1;
            if (k < limit) {
                return ret ~ x;
            } else {
                return ret;
            }
        }
        long[] ret;
        auto ds = divisors(x);
        foreach (d; ds) {
            auto ns = enumerate(d, k - 1, limit - ret.length);
            ret ~= ns;
            if (ret.length >= limit) break;
        }
        return ret;
    }

    writefln("%(%s %)", enumerate(X, K, 100_000));

}


You are given an array $$$a$$$ consisting of $$$n$$$ integers. You can perform the following operations arbitrary number of times (possibly, zero):  Choose a pair of indices $$$(i, j)$$$ such that $$$|i-j|=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i + |a_i - a_j|$$$;  Choose a pair of indices $$$(i, j)$$$ such that $$$|i-j|=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i - |a_i - a_j|$$$. The value $$$|x|$$$ means the absolute value of $$$x$$$. For example, $$$|4| = 4$$$, $$$|-3| = 3$$$.Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.It is guaranteed that you always can obtain the array of equal elements using such operations.Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value.

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

In the first line print one integer $$$k$$$ — the minimum number of operations required to obtain the array of equal elements. In the next $$$k$$$ lines print operations itself. The $$$p$$$-th operation should be printed as a triple of integers $$$(t_p, i_p, j_p)$$$, where $$$t_p$$$ is either $$$1$$$ or $$$2$$$ ($$$1$$$ means that you perform the operation of the first type, and $$$2$$$ means that you perform the operation of the second type), and $$$i_p$$$ and $$$j_p$$$ are indices of adjacent elements of the array such that $$$1 \le i_p, j_p \le n$$$, $$$|i_p - j_p| = 1$$$. See the examples for better understanding. Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value. If there are many possible answers, you can print any.

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

immutable long MOD = 10^^9 + 7;

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

    long[long] C;
    foreach (a; A) C[a] += 1;

    long maxv = 0;
    long maxk = 0;
    foreach (k; C.keys) {
        if (C[k] > maxv) {
            maxv = C[k];
            maxk = k;
        }
    }

    int last = -1;
    Tuple!(int, int, int)[] ans;

    foreach (i; 0..N) {
        if (A[i] != maxk && last == -1) {
            last = i;
        } else if (A[i] == maxk && last != -1) {
            foreach_reverse (j; last..i) {
                if (A[j] < maxk) {
                    ans ~= tuple(1, j + 1, j + 2);
                } else {
                    ans ~= tuple(2, j + 1, j + 2);
                }
            }
            last = -1;
        }
    }

    if (A.back != maxk) {
        foreach (j; last..N) {
            if (A[j] < maxk) {
                ans ~= tuple(1, j + 1, j);
            } else {
                ans ~= tuple(2, j + 1, j);
            }
        }
    }

    ans.length.writeln;
    ans.each!(a => writeln(a[0], " ", a[1], " ", a[2]));
}