On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper.A permutation $$$p$$$ is written from left to right on the paper. First Qingshan chooses an integer index $$$x$$$ ($$$1\le x\le n$$$) and tells it to Daniel. After that, Daniel chooses another integer index $$$y$$$ ($$$1\le y\le n$$$, $$$y \ne x$$$).The game progresses turn by turn and as usual, Qingshan moves first. The rules follow:   If it is Qingshan's turn, Qingshan must change $$$x$$$ to such an index $$$x'$$$ that $$$1\le x'\le n$$$, $$$|x'-x|=1$$$, $$$x'\ne y$$$, and $$$p_{x'}<p_x$$$ at the same time.  If it is Daniel's turn, Daniel must change $$$y$$$ to such an index $$$y'$$$ that $$$1\le y'\le n$$$, $$$|y'-y|=1$$$, $$$y'\ne x$$$, and $$$p_{y'}>p_y$$$ at the same time. The person who can't make her or his move loses, and the other wins. You, as Qingshan's fan, are asked to calculate the number of possible $$$x$$$ to make Qingshan win in the case both players play optimally.

The first line contains a single integer $$$n$$$ ($$$2\le n\le 10^5$$$) — the length of the permutation. The second line contains $$$n$$$ distinct integers $$$p_1,p_2,\dots,p_n$$$ ($$$1\le p_i\le n$$$) — the permutation.

Print the number of possible values of $$$x$$$ that Qingshan can choose to make her win.

NoteIn the first test case, Qingshan can only choose $$$x=3$$$ to win, so the answer is $$$1$$$.In the second test case, if Qingshan will choose $$$x=4$$$, Daniel can choose $$$y=1$$$. In the first turn (Qingshan's) Qingshan chooses $$$x'=3$$$ and changes $$$x$$$ to $$$3$$$. In the second turn (Daniel's) Daniel chooses $$$y'=2$$$ and changes $$$y$$$ to $$$2$$$. Qingshan can't choose $$$x'=2$$$ because $$$y=2$$$ at this time. Then Qingshan loses.

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);
    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1 || mx % 2 == 0){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
        writeln(0);
    }
}

void main(){
    long tests = 1;
    while(tests--) theCode();
}

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

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);

    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1 && mx % 2 == 0){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
        writeln(0);
    }
}

void main(){
    long tests = 1;

    while(tests--) theCode();
}

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

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);

    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
        writeln(0);
    }
}

void main(){
    long tests = 1;

    while(tests--) theCode();
}

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

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);

    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
    }
}

void main(){
    long tests = 1;

    while(tests--) theCode();
}

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

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);
    auto totel = pref ~ suff;

    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
        totel.sort;
        totel.reverse;
        if(mx % 2){
            ll max2 = totel[1];
            ll hfval = mx/2 + (mx % 2);
            ll oth = max(max2+1, hfval);
            if(oth % 2 == 0) ++oth;
            writeln(1 + (mx - oth)/2);
        }else{
            writeln(0);
        }
    }
}

void main(){
    long tests = 1;

    while(tests--) theCode();
}

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

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

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    auto pref = new ll[](n);
    auto suff = new ll[](n);
    pref[0] = 1;
    for(int i = 1; i < n; ++i) {
        if(arr[i] - arr[i-1] > 0){
            pref[i] = pref[i-1] + 1;
        }else{
            pref[i] = 1;
        }
    }
    suff[n-1] = 1;
    for(int i = n-2; i >= 0; --i) {
        if(arr[i] - arr[i+1] > 0){
            suff[i] = suff[i+1] + 1;
        }else{
            suff[i] = 1;
        }
    }
    ll cnt = 0;
    ll mx = max(pref.maxElement, suff.maxElement);
    auto totel = pref ~ suff;

    for(int i = 0; i < n;  ++i){
        if(suff[i] == mx || pref[i] == mx){
            ++cnt;
        }
    }
    if(cnt > 1){
        writeln(0);
    }else{
        for(int i = 0; i < n;  ++i){
            if(suff[i] == mx && pref[i] == mx){
                writeln(1);
                return;
            }
        }
        totel.sort;
        totel.reverse;
        if(mx % 2){
            ll max2 = totel[1];
            ll hfval = mx/2 + (mx % 2);
            ll oth = max(max2, hfval);
            if(oth % 2 == 0) ++oth;
            writeln(1 + (mx - oth)/2);
        }else{
            writeln(0);
        }
    }
}

void main(){
    long tests = 1;

    while(tests--) theCode();
}

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


Gerald got tired of playing board games with the usual six-sided die, and he bought a toy called Randomizer. It functions as follows.A Randomizer has its own coordinate plane on which a strictly convex polygon is painted, the polygon is called a basic polygon. If you shake a Randomizer, it draws some nondegenerate (i.e. having a non-zero area) convex polygon with vertices at some vertices of the basic polygon. The result of the roll (more precisely, the result of the shaking) is considered to be the number of points with integer coordinates, which were strictly inside (the points on the border are not considered) the selected polygon. Now Gerald is wondering: what is the expected result of shaking the Randomizer?During the shaking the Randomizer considers all the possible non-degenerate convex polygons with vertices at the vertices of the basic polygon. Let's assume that there are k versions of the polygons. Then the Randomizer chooses each of them with probability .

The first line of the input contains a single integer n (3 ≤ n ≤ 100 000) — the number of vertices of the basic polygon.  Next n lines contain the coordinates of the vertices of the basic polygon. The i-th of these lines contain two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th vertex of the polygon. The vertices are given in the counter-clockwise order.

Print the sought expected value with absolute or relative error at most 10 - 9.

NoteA polygon is called strictly convex if it is convex and no its vertices lie on the same line.Let's assume that a random variable takes values x1, ..., xn with probabilities p1, ..., pn, correspondingly. Then the expected value of this variable equals to .

import std.algorithm,std.conv,std.math,std.numeric,std.range,std.stdio;
void main()
{
    int n,d;
    readf("%s ",&n);
    d=min(50,n-2);
    auto a=n.iota.map!(_=>readln.split.map!(to!int).array).array,m=d.iota.map!(i=>pow(2.,d-i)-1).array;
    a~=a,m[]/=(d<50)?pow(2.,n)-1-n-n*.5*(n-1):m.sum*2;
    writef("%.9f",sum(n.iota.map!(i=>sum(iota(i+1,i+d+1).map!(j=>m[j-i-1]*((a[i][0]-a[j][0])*1.*(a[i][1]+a[j][1])-gcd(abs(a[i][0]-a[j][0]),abs(a[i][1]-a[j][1])))))))/2+1);
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main ()
{
    int n, d, j;
    readf (" %s ", &n);
    auto a = n.iota.map !(_ => readln.split.map !(to !(int)).array).array;
    a ~= a;
    d = min (50, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1.0).array;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) : sum (mult) * 2;
    real res = 0;
    res = sum (n.iota.map !(i => sum (iota (i + 1, i + d + 1).map !(j => mult[j - i - 1] *
        ((a[i][0] - a[j][0]) * 1.0 * (a[i][1] + a[j][1]) - gcd (abs (a[i][0] - a[j][0]), abs (a[i][1] - a[j][1])))))));
    writefln ("%.20f", res / 2 + 1);
}

import core.bitop;
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;

immutable int MUCH = 70;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult) * 2;
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main()
{
    int n, d;
    readf(" %s ", &n);
    d = min(50, n - 2);
    auto a = n.iota.map!(_ => readln.split.map!(to!int).array).array, mult = d.iota.map!(i => pow(2.0, d - i) - 1).array;
    a ~= a;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * .5 * (n - 1) : mult.sum * 2;
    writef("%.9f", sum(n.iota.map!(i => sum(iota(i + 1, i + d + 1).map!(j => mult[j - i - 1] * ((a[i][0] - a[j][0]) * 1. * (a[i][1] + a[j][1]) - gcd(abs(a[i][0] - a[j][0]), abs(a[i][1] - a[j][1]))))))) / 2 + 1);
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio, std.string;
void main ()
{
    int n, d, j;
    readf (" %s ", &n);
    int [] [] a;
    foreach (i; 0..n)
        a ~= readln.split.map !(to !(int)).array;
    a ~= a;
    d = min (70, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1.0).array;
    mult[] /= (d < 70) ? pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) : sum (mult) * 2;
    real res = 0;
    foreach (i; 0..n)
        foreach (k; 0..d)
        {
            j = i + k + 1;
            real s = a[i][0] - a[j][0];
            s *= a[i][1] + a[j][1];
            s -= gcd (abs (a[i][0] - a[j][0]), abs (a[i][1] - a[j][1]));
            res += mult[k] * s;
        }
    writefln ("%.20f", res / 2 + 1);
}

import std.algorithm, std.math, std.numeric, std.stdio, std.typecons;
immutable int MUCH = 70;
void main ()
{
    int n;
    while (readf (" %s", &n) > 0)
    {
        alias Point = Tuple !(int, "x", int, "y");
        auto a = new Point [n];
        foreach (ref p; a)
            readf (" %s %s", &p.x, &p.y);
        a ~= a;

        int d = min (MUCH, n - 2);
        auto mult = new real [d];
        foreach (i; 0..d)
            mult[i] = pow (2.0, d - i) - 1.0;
        mult[] /= (d < MUCH) ?
            pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) :
            sum (mult) * 2;

        real res = 0;
        foreach (i; 0..n)
            foreach (k; 0..d)
            {
                int j = i + k + 1;
                real s = a[i].x - a[j].x;
                s *= a[j].y + a[i].y;
                s -= gcd (abs (a[j].x - a[i].x),
                    abs (a[j].y - a[i].y));
                res += mult[k] * s;
            }
        writefln ("%.20f", res / 2 + 1);
    }
}

import core.bitop;
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;

immutable int MUCH = 100;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult);
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main()
{
    int n, d;
    readf(" %s ", &n);
    auto a = n.iota.map!(_ => readln.split.map!(to!int).array).array;
    a ~= a;
    d = min (50, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1).array;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * .5 * (n - 1) : 2;
    writef("%.9f", sum(n.iota.map!(i => sum(iota(i + 1, i + d + 1).map!(j => mult[j - i - 1] * ((a[i][0] - a[j][0]) * 1. * (a[i][1] + a[j][1]) - gcd(abs(a[i][0] - a[j][0]), abs(a[i][1] - a[j][1]))))))) / 2 + 1);
}

import core.bitop;
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;

immutable int MUCH = 50;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult);
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}


Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers — saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:  Each pencil belongs to exactly one box;  Each non-empty box has at least k pencils in it;  If pencils i and j belong to the same box, then |ai - aj| ≤ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| ≤ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".

The first line contains three integer numbers n, k and d (1 ≤ k ≤ n ≤ 5·105, 0 ≤ d ≤ 109) — the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — saturation of color of each pencil.

Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".

NoteIn the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.

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

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

    auto a = readln.split.map!( to!int ).array;
    a = -(10^^9) ~ a;
    auto sorted = a.sort();
    debug { writeln( sorted ); }

    auto vs = [0];

    foreach ( i; k..n+1 ) {
        debug { write(i, ' '); }

        auto reach = sorted.lowerBound( a[ i ] - d );
        auto idxleft = reach.length - 1;
        auto idcs = vs.assumeSorted().equalRange( idxleft ).chain( vs.assumeSorted().upperBound( idxleft ) );

        debug { write( idxleft, ' ', idcs, ' ' ); }

        bool ok = !idcs.empty() && idcs.front() <= i - k;

        debug { writeln( i - k, ' ', ok ); }

        if ( ok ) vs ~= i;
    }
    debug { writeln( vs ); }
    writeln( vs.back() == n ? "YES" : "NO" );
}

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

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

    auto a = -(10^^9) ~ readln.split.map!( to!int ).array;
    auto sorted = a.sort();
    auto vs = [0];

    foreach ( i; k..n+1 ) {
        auto reach = sorted.lowerBound( a[ i ] - d );
        auto idxleft = reach.length - 1;
        auto vsRange = vs.assumeSorted();
        auto idcs = vsRange.equalRange( idxleft ).chain( vsRange.upperBound( idxleft ) );

        bool ok = !idcs.empty() && idcs.front() <= i - k;

        debug { write( idxleft, ' ', idcs, ' ', i - k, ' ', ok ); }

        if ( ok ) vs ~= i;
    }

    debug { writeln( vs ); }
    writeln( vs.back() == n ? "YES" : "NO" );
}

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

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

    auto a = -(10^^9) ~ readln.split.map!( to!int ).array;
    a.sort();
    debug { writeln(a); }

    auto vs = DList!int(0);

    auto idxleft = 0;
    foreach ( i; k..n+1 ) {
        debug { write(i, ' '); }

        while ( a[ idxleft+1 ] < a[ i ] - d ) ++idxleft;
        while ( !vs.empty() && vs.front() < idxleft ) vs.removeFront();
        bool ok = !vs.empty() && vs.front() <= i - k;

        if ( ok ) vs ~= i;

        debug { writeln( idxleft, ' ', i - k, ' ', ok, ' ', vs.array ); }
    }

    writeln( !vs.empty() && vs.back() == n ? "YES" : "NO" );
}


In the pet store on sale there are:  $$$a$$$ packs of dog food;  $$$b$$$ packs of cat food;  $$$c$$$ packs of universal food (such food is suitable for both dogs and cats). Polycarp has $$$x$$$ dogs and $$$y$$$ cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his cats should receive one pack of suitable food for it.

The first line of input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ lines are given, each containing a description of one test case. Each description consists of five integers $$$a, b, c, x$$$ and $$$y$$$ ($$$0 \le a,b,c,x,y \le 10^8$$$).

For each test case in a separate line, output:   YES, if suitable food can be bought for each of $$$x$$$ dogs and for each of $$$y$$$ cats;  NO else.  You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int a, b, c, x, y;
		readf !(" %s %s %s %s %s") (a, b, c, x, y);
		bool ok = (a + c >= x && b + c >= y && a + b + c >= x + y);
		writeln (ok ? "YES" : "NO");
	}
}

// cheese-cracker [2022-05-05]

void solve(){
    long a = scan;
    long b = scan;
    long c = scan;
    long x = scan;
    long y = scan;
    long left = max(x - a, 0L) + max(y - b, 0);
    if(c >= left){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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


Ashish has a binary string $$$s$$$ of length $$$n$$$ that he wants to sort in non-decreasing order.He can perform the following operation:   Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any $$$k$$$ such that $$$1 \leq k \leq n$$$ and any sequence of $$$k$$$ indices $$$1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n$$$ such that $$$s_{i_1} \ge s_{i_2} \ge \ldots \ge s_{i_k}$$$.  Reverse this subsequence in-place. Formally, swap $$$s_{i_1}$$$ with $$$s_{i_k}$$$, swap $$$s_{i_2}$$$ with $$$s_{i_{k-1}}$$$, $$$\ldots$$$ and swap $$$s_{i_{\lfloor k/2 \rfloor}}$$$ with $$$s_{i_{\lceil k/2 \rceil + 1}}$$$ (Here $$$\lfloor x \rfloor$$$ denotes the largest integer not exceeding $$$x$$$, and $$$\lceil x \rceil$$$ denotes the smallest integer not less than $$$x$$$) Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most $$$n$$$ operations.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 1000)$$$  — the length of the binary string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ containing only $$$0$$$s and $$$1$$$s. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.

For each test case output the following:   The minimum number of operations $$$m$$$ in the first line ($$$0 \le m \le n$$$).  Each of the following $$$m$$$ lines should be of the form: $$$k$$$ $$$i_1$$$ $$$i_2$$$ ... $$$i_{k}$$$, where $$$k$$$ is the length and $$$i_1 \lt i_2 \lt ... \lt i_{k}$$$ are the indices of the chosen subsequence. For them the conditions from the statement must hold.

NoteIn the first test case, the binary string is already sorted in non-decreasing order.In the second test case, we can perform the following operation:   $$$k = 4:$$$ choose the indices $$$\{1, 3, 4, 5\}$$$$$$\underline{1}$$$ $$$0$$$ $$$\underline{1}$$$ $$$\underline{0}$$$ $$$\underline{0}$$$ $$$\rightarrow $$$ $$$\underline{0}$$$ $$$0$$$ $$$\underline{0}$$$ $$$\underline{1}$$$ $$$\underline{1}$$$ In the third test case, we can perform the following operation:  $$$k = 3:$$$ choose the indices $$$\{3, 5, 6\}$$$$$$0$$$ $$$0$$$ $$$\underline{1}$$$ $$$0$$$ $$$\underline{0}$$$ $$$\underline{0}$$$ $$$\rightarrow $$$ $$$0$$$ $$$0$$$ $$$\underline{0}$$$ $$$0$$$ $$$\underline{0}$$$ $$$\underline{1}$$$

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto s = readString;
	int[] p0, p1;
	foreach(i, ch; s)
	{
		if (ch == '0')
		{
			p0 ~= cast(int) i;
		}
		else
		{
			p1 ~= cast(int) i;
		}
	}
	int j = 0;
	int[] ans;
	debug writeln(p0, p1);
	while (p0.length>0 && p1.length>0
	       && p0[$-1] > p1[0])
	{
		ans ~= p0[$-1];
		ans ~= p1[0];
		p0 = p0[0 .. $ - 1];
		p1 = p1[1 .. $];
	}
	if (ans.length == 0) return writeln(0);
	writeln(1);
	sort(ans);
	ans.length.write(" ");
	foreach(v; ans) write(v+1, " ");
	writeln;
}

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

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

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

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

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

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

void solve(){
    int n = scan!int;
    auto arr = new long[](n);
    auto word = scan!(dchar[]);
    long cnt = 0;
    for(int i = 0; i < n; ++i){
        if(word[i] == '1'){
            ++cnt;
            ++arr[i];
        }
    }
    for(int i = n-1; i >= n - cnt; --i){
        arr[i] = !arr[i];
    }
    ll[] res;
    for(int i = 0; i < n; ++i){
        if(arr[i]){
            res ~= i;
        }
    }
    if(res.length){
        writeln(1);
        write(res.length, " ");
        res.each!((k) => write(k+1, " "));
        writeln;
    }else{
        writeln(0);
    }
}

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

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

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

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

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

        string a;
        readf!"%s\n"(a);

        int zeros = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == '0') zeros ++;
        }

        int[] indices = new int[0];

        for (int i = 0; i < n; i++) {
            if (a[i] == '0') {
                zeros --;
            } else {
                zeros --;
                indices ~= [i];
            }
            if (zeros <= 0) {
                // add the next indices.length zeros
                int to_add = cast(int) indices.length;
                for (int j = i+1; j < n; j++) {
                    if (a[j] == '0') {
                        indices ~= [j];
                        to_add --;
                    }
                    if (to_add <= 0) break;
                }

                break;
            }
        }

        if (indices.length != 0) writeln(1);
        write(indices.length);
        write(' ');
        for (int i = 0; i < indices.length; i++) {
            write(indices[i] + 1);
            write(' ');
        }
        writeln();

    }
}

/*
100001010101111000110011
|    | | | |   |||  ||
000000000000111111111111

1001010100100010101010111111111010101110011
|  | | |  |   | |
0000000000000000011111111111111111111111111

101
|
011

11111111111111111101
|
01111111111111111111*/

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

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

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

        string a;
        readf!"%s\n"(a);

        int zeros = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == '0') zeros ++;
        }

        int[] indices = new int[0];

        for (int i = 0; i < n; i++) {
            if (a[i] == '0') {
                zeros --;
            } else {
                zeros --;
                indices ~= [i];
            }
            if (zeros <= 0) {
                // add the next indices.length zeros
                int to_add = cast(int) indices.length;
                for (int j = i+1; j < n; j++) {
                    if (a[j] == '0') {
                        indices ~= [j];
                    }
                    to_add --;
                    if (to_add <= 0) break;
                }

                break;
            }
        }

        if (indices.length != 0) writeln(1);
        write(indices.length);
        write(' ');
        for (int i = 0; i < indices.length; i++) {
            write(indices[i] + 1);
            write(' ');
        }
        writeln();

    }
}

/*
100001010101111000110011
|    | | | |   |||  ||
000000000000111111111111

1001010100100010101010111111111010101110011
|  | | |  |   | |
0000000000000000011111111111111111111111111

101
|
011

11111111111111111101
|
01111111111111111111*/


A string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the English alphabet, is given.You must choose some number $$$k$$$ between $$$0$$$ and $$$n$$$. Then, you select $$$k$$$ characters of $$$s$$$ and permute them however you want. In this process, the positions of the other $$$n-k$$$ characters remain unchanged. You have to perform this operation exactly once.For example, if $$$s=\texttt{"andrea"}$$$, you can choose the $$$k=4$$$ characters $$$\texttt{"a_d_ea"}$$$ and permute them into $$$\texttt{"d_e_aa"}$$$ so that after the operation the string becomes $$$\texttt{"dneraa"}$$$.Determine the minimum $$$k$$$ so that it is possible to sort $$$s$$$ alphabetically (that is, after the operation its characters appear in alphabetical order).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 40$$$) — the length of the string. The second line of each test case contains the string $$$s$$$. It is guaranteed that $$$s$$$ contains only lowercase letters of the English alphabet.

For each test case, output the minimum $$$k$$$ that allows you to obtain a string sorted alphabetically, through the operation described above.

NoteIn the first test case, we can choose the $$$k=2$$$ characters $$$\texttt{"_ol"}$$$ and rearrange them as $$$\texttt{"_lo"}$$$ (so the resulting string is $$$\texttt{"llo"}$$$). It is not possible to sort the string choosing strictly less than $$$2$$$ characters.In the second test case, one possible way to sort $$$s$$$ is to consider the $$$k=6$$$ characters $$$\texttt{"_o__force_"}$$$ and rearrange them as $$$\texttt{"_c__efoor_"}$$$ (so the resulting string is $$$\texttt{"ccdeefoors"}$$$). One can show that it is not possible to sort the string choosing strictly less than $$$6$$$ characters.In the third test case, string $$$s$$$ is already sorted (so we can choose $$$k=0$$$ characters).In the fourth test case, we can choose all $$$k=4$$$ characters $$$\texttt{"dcba"}$$$ and reverse the whole string (so the resulting string is $$$\texttt{"abcd"}$$$).

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

void main()
{
	int t = readInt!int;
	while (t--)
	{
		solve();
	}
}

void solve()
{
	int n = readInt!int;
	char[] s = cast(char[])readString;
	int[] si = s.map!(c => cast(int)c).array;
	int[] ssi = si.dup; sort(ssi);
	iota(0, n).count!(i => si[i] != ssi[i]).writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

void main() {
  int T;
  read(T);

  while (T--) {
    int n;
    read(n);
    auto s = readln.strip.array();
    auto cp = s.dup;
    sort(s);

    int t = 0;

    foreach (i; 0 .. n) {
      if (s[i] != cp[i]) t++;
    }

    writeln(t);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long[] a;
		foreach (c; s)
		{
			a ~= c - 'a';
		}
		a.sort;
		foreach (i; 0..n)
		{
			auto x = s[i]-'a';
			if (a[i] != x)
				++ans[ti];
		}
	}

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

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split("");
    auto sa = a.dup.sort;
    int result;
    foreach (i ; 0 .. a.length) {
      if (a[i] != sa[i])
        result++;
    }
    writeln(result);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		auto t = s.dup;
		sort (t.byChar);
		writeln (zip (s, t).count !(q{a[0] != a[1]}));
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const S = readToken();

        auto cs = S.dup;
        foreach (_; 0 .. N - 1) foreach (i; 0 .. N - 1) {
          if (cs[i] > cs[i + 1]) {
            swap(cs[i], cs[i + 1]);
          }
        }
        int ans;
        foreach (i; 0 .. N) {
          if (S[i] != cs[i]) {
            ++ans;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all nodes for which $$$v$$$ is the parent. A vertex is a leaf if it has no children.The rooted tree Serval owns has $$$n$$$ nodes, node $$$1$$$ is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation $$$\max$$$ or $$$\min$$$ written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. Assume that there are $$$k$$$ leaves in the tree. Serval wants to put integers $$$1, 2, \ldots, k$$$ to the $$$k$$$ leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?

The first line contains an integer $$$n$$$ ($$$2 \leq n \leq 3\cdot 10^5$$$), the size of the tree. The second line contains $$$n$$$ integers, the $$$i$$$-th of them represents the operation in the node $$$i$$$. $$$0$$$ represents $$$\min$$$ and $$$1$$$ represents $$$\max$$$. If the node is a leaf, there is still a number of $$$0$$$ or $$$1$$$, but you can ignore it. The third line contains $$$n-1$$$ integers $$$f_2, f_3, \ldots, f_n$$$ ($$$1 \leq f_i \leq i-1$$$), where $$$f_i$$$ represents the parent of the node $$$i$$$.

Output one integer — the maximum possible number in the root of the tree.

NotePictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes.In the first example, no matter how you arrange the numbers, the answer is $$$1$$$.  In the second example, no matter how you arrange the numbers, the answer is $$$4$$$.  In the third example, one of the best solution to achieve $$$4$$$ is to arrange $$$4$$$ and $$$5$$$ to nodes $$$4$$$ and $$$5$$$.  In the fourth example, the best solution is to arrange $$$5$$$ to node $$$5$$$.

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;

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

    auto G = new int[][](N);
    foreach (i; 1..N) G[i] ~= P[i], G[P[i]] ~= i;
    int k = 0;

    int dfs(int n, int p) {
        if (p != -1 && G[n].length == 1) {
            k += 1;
            return 0;
        } else if (A[n] == 0) {
            int res = 0;
            foreach (m; G[n]) {
                if (m == p) continue;
                res += max(1, dfs(m, n));
            }
            return res;
        } else {
            int res = 1 << 29;
            foreach (m; G[n]) {
                if (m == p) continue;
                res = min(res, dfs(m, n));
            }
            return res;
        }
    }

    int ans = dfs(0, -1);
    if (ans == 0) {
        k.writeln;
    } else {
        (k - ans + 1).writeln;
    }
}

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

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

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

    auto f = readln.chomp.split.map!(to!int).map!(x => x-1).array;

    auto g = new int[][] (n);
    foreach (i; 1 .. n) {
       g[f[i-1]] ~= i;
    }

    debug { g.writeln; }

    int leaves = 0;
    int dfs(int v) {
        if (g[v].empty()) {
            leaves += 1;
            return 1;
        }

        int ans = dfs(g[v].front);
        foreach (u; g[v].dropOne) {
            int res = dfs(u);
            if (mnmx[v] == 0) { ans += res; }
            else { ans = min(ans, res); }
        }

        debug { writeln(v, ' ', ans); }

        return ans;
    }

    int ans = dfs(0);

    ans = leaves - ans + 1;

    ans.writeln;
}

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

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

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

    auto f = readln.chomp.split.map!(to!int).map!(x => x-1).array;

    auto g = new int[][] (n);
    foreach (i; 1 .. n) {
       g[f[i-1]] ~= i;
    }

    debug { g.writeln; }

    int leaves = 0;
    int dfs(int v, ref int leaves) {
        if (g[v].empty()) {
            leaves += 1;
            return 1;
        }

        int ans = dfs(g[v].front, leaves);
        foreach (u; g[v].dropOne) {
            int res = dfs(u, leaves);
            if (mnmx[v] == 0) { ans += res; }
            else { ans = min(ans, res); }
        }

        debug { writeln(v, ' ', ans); }

        return ans;
    }

    int ans = dfs(0, leaves);

    ans = leaves - ans + 1;

    ans.writeln;
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

const long mod = 1_000_000_007;

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	int n = read.to!int;

	Node[] nodes;
	foreach(i; 0 .. n){
		bool isMax = read.to!int.to!bool;
		nodes ~= new Node(i, isMax);
	}

	foreach(i; 1 .. n){
		int j = read.to!int - 1;
		nodes[i].parent = nodes[j];
		nodes[j].kids ~= nodes[i];
	}

	int v = nodes[0].solve;

	auto leafcount = nodes.count!(x => x.kids.length == 0);
	(leafcount + 1 - v).writeln;

	print!1(nodes.map!(x => x.solve()));

}

/*

Tree DP.
Each node holds "min of at least how many leaves comes here?".
For example, if a node has only "max" descenders its value will be 1.

If a node is max node, its value is the min of its direct sons.
If a node is min node, its value is the sum of its direct sons.

*/

class Node{
	bool isMax;
	long _b, _w;
	int id;
	Node[] nodes;
	Node[] kids;
	Node parent;
	bool isVisited;
	this(int id, bool isMax){
		this.id = id;
		this.isMax = isMax;
	}
	int solve(){
		int res;
		if(kids.length == 0) return 1;
		else if(isMax){
			res = 900_000;
			foreach(nd; kids) res = min(res, nd.solve);
		}
		else{
			res = 0;
			foreach(nd; kids) res += nd.solve;
		}
		return res;
	}
}

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

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int nl = 0;
  int mins(int v)
  {
    if (ch[v].len == 0)
      {
        nl++;
        return 0;
      }
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  auto pres = mins(0);
  writeln(nl - pres);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    auto cr = ch[v].map!mins;
    final switch(op[v])
      {
      case 0:
        return cast(int)(cr.sum + ch[v].len - 1);
      case 1:
        return cast(int)(cr.fold!min);
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto operation = next!int(n);
  debug writeln(operation);
  auto children = new int[][](n);
  foreach(i; 1 .. n)
    children[next!int - 1] ~= i;
  debug writeln(children);
  auto noleafs = cast(int) children.count!(child => child.length == 0);
  int dfs(int node)
  {
    if (children[node].length == 0)
      return 0;
    if (operation[node] == 0) // min
      return cast(int)(children[node].map!(child => dfs(child)).sum + children[node].length - 1);
    else // max
      return cast(int)(children[node].map!(child => dfs(child)).fold!min);
  }
  writeln(noleafs - dfs(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  int[300_000] sop;
  int[][300_000] sch;
  auto n = nt!int;
  auto op = sop[0 .. n];
  foreach(i; 0 .. n)
    op[i] = nt!int;
  auto ch = sch[0 .. n];
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int nl = 0;
  int mins(int v)
  {
    if (ch[v].len == 0)
      {
        nl++;
        return 0;
      }
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  auto pres = mins(0);
  writeln(nl - pres);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto operation = next!int(n);
  debug writeln(operation);
  auto children = new int[][](n);
  foreach(i; 1 .. n)
    children[next!int - 1] ~= i;
  debug writeln(children);
  auto noleafs = cast(int) children.count!(child => child.length == 0);
  int dfs(int node)
  {
    if (children[node].length == 0)
      {
        return 0;
      }
    if (operation[node] == 0) // min
      {
        int acc = 0;
        foreach(child; children[node])
          acc += dfs(child);
        acc += children[node].length - 1;
        return acc;
      }
    else // max
      {
        int m = int.max;
        foreach(child; children[node])
          m = min(m, dfs(child));
        return m;
      }
  }
  writeln(noleafs - dfs(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}


There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.

The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.

Print two integer numbers — the minimal distance and the quantity of pairs with this distance.

NoteIn the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.

import std.bigint,std.container,std.array,std.algorithm,std.conv,
std.functional,std.math,std.numeric,std.string,std.range,std.complex,
std.typecons,std.regex,std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias gcd=std.numeric.gcd;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
	{
		pair!(X,Y) pp;
		pp.fi=x_;
		pp.se=y_;
		return pp;
	}
	big gcd(big a,big b)
	{
		while(b)
		{
			a%=b;
			swap(a,b);
		}
		return a;
	}
	X binpow(X,Y)(X base,Y exp)if(is(typeof(exp>>1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)if(is(typeof(exp>>1)) && is(typeof(base%mm)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~"\n", ptrs)==ptrs.length;}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		auto a=arread!int;

		sort(a);int m=int.max,k=0;
		foreach(i;0..a.length-1)
		{
			if(a[i+1]-a[i]<m)
			{
				m=a[i+1]-a[i];
				k=1;
			}
			else if(a[i+1]-a[i]==m)k++;
		}
		writeln(m,' ',k);
	}
	debug system("pause");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		sort (a);
		auto b = (n - 1).iota.map !(i => a[i + 1] - a[i]).array;
		auto m = b.reduce !(min);
		writeln (m, " ", b.count (m));
	}
}


It is a simplified version of problem F2. The difference between them is the constraints (F1: $$$k \le 2$$$, F2: $$$k \le 10$$$).You are given an integer $$$n$$$. Find the minimum integer $$$x$$$ such that $$$x \ge n$$$ and the number $$$x$$$ is $$$k$$$-beautiful.A number is called $$$k$$$-beautiful if its decimal representation having no leading zeroes contains no more than $$$k$$$ different digits. E.g. if $$$k = 2$$$, the numbers $$$3434443$$$, $$$55550$$$, $$$777$$$ and $$$21$$$ are $$$k$$$-beautiful whereas the numbers $$$120$$$, $$$445435$$$ and $$$998244353$$$ are not.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le k \le 2$$$).

For each test case output on a separate line $$$x$$$ — the minimum $$$k$$$-beautiful integer such that $$$x \ge n$$$.

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

const ll INF = to!long(1e12);
ll[][3] numlist;

ll numify(ll[] arr){
    ll sol = 0;
    foreach(d; arr){
        sol *= 10L;
        sol += d;
    }
    return sol;
}

void preProcess(){
    for(int msz = 1; msz <= 11; ++msz){
        ll mxmsk = (1L<<msz);
        /* show(mxmsk, msz); */
        for(ll msk = 0; msk < mxmsk; ++msk){
            ll[] num;
            for(int i = 0; i < msz; ++i){
                num ~= ((msk & (1L<<i)) > 0);
            }
            for(ll a = 0; a <= 9; ++a){
                for(ll b = 0; b <= 9; ++b){
                    ll[] ber;
                    foreach(d; num){
                        ber ~= (d ? a : b);
                    }
                    if(ber[0] == 0) continue;
                    int curk = 1;
                    for(int i = 1; i < ber.length; ++i){
                        if(ber[i] != ber[i-1]){ ++curk; break;}
                    }
                    ll val = numify(ber);
                    numlist[curk] ~= val;
                }
            }
        }
    }
    numlist[1].sort;
    numlist[2].sort;
}

void theCode(){
    ll n = scan;
    ll k = scan;
    int dig = 0;
    ll m = n;
    ll res = INF;
    foreach(num; numlist[1]){
        if(num >= n){
            res = num;
            break;
        }
    }
    if(k == 2){
        int lo = -1;
        int hi = numlist[2].length.to!int;
        while(hi - lo > 1){
            int midd = (lo + hi)/2;
            /* show(lo, midd, hi); */
            ( numlist[2][midd] >= n ? hi : lo) = midd;
        }
        res = min(numlist[2][hi], res);
    }
    writeln(res);
}

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

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


Let's introduce the designation , where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aсba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.

The first line contains two integers b, d (1 ≤ b, d ≤ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.

In a single line print an integer — the largest number p. If the required value of p doesn't exist, print 0.

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

void main ()
{
	int b, d;
	while (readf (" %s %s ", &b, &d) > 0)
	{
		auto a = readln ().strip ();
		auto c = readln ().strip ();
		int m = a.length;
		int n = c.length;

		alias Tuple !(int, "num", int, "pos", int, "cat") match;

		bool ok = true;
		foreach (x; c)
		{
			if (find (a, x).length == 0)
			{
				ok = false;
			}
		}
		if (!ok)
		{
			writeln (0);
			continue;
		}

		match [] e;
		auto cur = match (0, 0, 0);
//		e ~= cur;
		for (int i = 0; i < m * 4; i++)
		{
			for (cur.cat = 0; cur.cat < n; cur.cat++)
			{
				while (a[cur.pos] != c[cur.cat])
				{
					cur.pos++;
					if (cur.pos == m)
					{
						cur.pos = 0;
						cur.num++;
					}
				}
				e ~= cur;

				cur.pos++;
				if (cur.pos == m)
				{
					cur.pos = 0;
					cur.num++;
				}
			}
		}

		int p = int.max;
		int q = int.max;
		for (int r = 1; r <= m; r++)
		{
			if (e[$ - 1].pos == e[$ - r * n - 1].pos &&
			    e[$ - 1].cat == e[$ - r * n - 1].cat)
			{
				p = e[$ - 1].num - e[$ - r * n - 1].num;
				q = r;
				break;
			}
		}
		assert (p != int.max);
		assert (q != int.max);
		debug {writefln ("p = %s, q = %s", p, q);}
		debug {writefln ("%(%s\n%)", e);}

		// asymptotically, each p strings a give q strings c
		// we have b strings a
		// long res = (q * b) / (p * d);

		long res = 0;
		int left = b;
		while (left >= e[$ - 1].num)
		{
			left -= p;
			res += q;
		}

		int pos = 0;
		while (e[pos].num < left)
		{
			pos++;
		}
		debug {writefln ("res = (%s + %s / %s) / %s", res, pos, m, d);}
		res += pos / n;

		res /= d;
		writeln (res);
	}
}


During cleaning the coast, Alice found $$$n$$$ piles of stones. The $$$i$$$-th pile has $$$a_i$$$ stones.Piles $$$i$$$ and $$$i + 1$$$ are neighbouring for all $$$1 \leq i \leq n - 1$$$. If pile $$$i$$$ becomes empty, piles $$$i - 1$$$ and $$$i + 1$$$ doesn't become neighbouring.Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:   Select two neighboring piles and, if both of them are not empty, remove one stone from each of them. Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:   Before the start of cleaning, you can select two neighboring piles and swap them. Determine, if it is possible to remove all stones using the superability not more than once.

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 the single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the number of stones in each pile. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.

NoteIn the first test case, you can remove all stones without using a superability: $$$[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$$$.In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $$$a = [2, 2, 2, 3, 1]$$$.In the fourth test case, you can apply superability to the first and the second piles, thus getting $$$a = [1900, 2100, 1600, 3000, 1600]$$$.

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

int [] go (T) (T a)
{
	debug {write (a, ": ");}
	int [] res;
	auto cur = 0;
	bool good = true;
	res ~= cur;
	foreach (ref next; a)
	{
		cur = next - cur;
		good &= (cur >= 0);
		res ~= good ? cur : -1;
	}
	debug {writeln (res);}
	return res;
}

bool solve (int n, int [] a)
{
	if (n == 2)
	{
		return a.front == a.back;
	}
	auto p = go (a);
	auto q = go (a.retro).retro.array;
	if (p.back == 0)
	{
		return true;
	}
	foreach (i; 1..n)
	{
		if (p[i - 1] >= 0 && q[i + 1] >= 0)
		{
			auto r =
			    go (only (p[i - 1], a[i], a[i - 1], q[i + 1]));
			if (r.back == 0)
			{
				return true;
			}
		}
	}
	return false;
}

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


NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles:  It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture.

The first and the only line of the input file contains two numbers $$$n$$$ and $$$r$$$ ($$$3 \leq n \leq 100$$$, $$$1 \leq r \leq 100$$$) — the number of the outer circles and the radius of the inner circle respectively.

Output a single number $$$R$$$ — the radius of the outer circle required for building the required picture.  Your answer will be accepted if its relative or absolute error does not exceed $$$10^{-6}$$$. Formally, if your answer is $$$a$$$ and the jury's answer is $$$b$$$. Your answer is accepted if and only when $$$\frac{|a-b|}{max(1, |b|)} \le 10^{-6}$$$.

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

void main() {
    double n,r;
    readf!" %s %s"(n,r);

    writefln("%.10f", r * sin(PI/n) / (1.0 - sin(PI/n)));
}


Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldn’t program or make up problems (in fact, few people saw a computer those days) but he was nevertheless happy. He worked in a warehouse where a magical but non-alcoholic drink Ogudar-Olok was kept. We won’t describe his work in detail and take a better look at a simplified version of the warehouse.The warehouse has one set of shelving. It has n shelves, each of which is divided into m sections. The shelves are numbered from top to bottom starting from 1 and the sections of each shelf are numbered from left to right also starting from 1. Each section can contain exactly one box of the drink, and try as he might, DravDe can never put a box in a section that already has one. In the course of his work DravDe frequently notices that he has to put a box in a filled section. In that case his solution is simple. DravDe ignores that section and looks at the next one to the right. If it is empty, he puts the box there. Otherwise he keeps looking for the first empty section to the right. If no empty section is found by the end of the shelf, he looks at the shelf which is under it, then the next one, etc. Also each time he looks at a new shelf he starts from the shelf’s beginning. If DravDe still can’t find an empty section for the box, he immediately drinks it all up and throws the empty bottles away not to be caught.After one great party with a lot of Ogudar-Olok drunk DravDe asked you to help him. Unlike him, you can program and therefore modeling the process of counting the boxes in the warehouse will be easy work for you.The process of counting contains two types of query messages:   «+1 x y id» (where x, y are integers, 1 ≤ x ≤ n, 1 ≤ y ≤ m, and id is a string of lower case Latin letters — from 1 to 10 characters long). That query means that the warehouse got a box identified as id, which should be put in the section y on the shelf x. If the section is full, use the rules described above. It is guaranteed that every moment of the process the identifiers of all the boxes in the warehouse are different. You don’t have to answer this query.  «-1 id» (where id is a string of lower case Latin letters — from 1 to 10 characters long). That query means that a box identified as id is removed from the warehouse. You have to answer this query (see output format).

The first input line contains integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ 2000) — the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following k lines the queries are given in the order of appearance in the format described above.

For each query of the «-1 id» type output two numbers in a separate line — index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output «-1 -1» without quotes.

2 2 9
+1 1 1 cola
+1 1 1 fanta
+1 1 1 sevenup
+1 1 1 whitekey
-1 cola
-1 fanta
-1 sevenup
-1 whitekey
-1 cola

2 2 8
+1 1 1 cola
-1 cola
+1 1 1 fanta
-1 fanta
+1 1 1 sevenup
-1 sevenup
+1 1 1 whitekey
-1 whitekey

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

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

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

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

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

int M, N, K;

void main(string[] args) {
	auto inp = File("input.txt", "r");
	auto outp = File("output.txt", "w");
	for (string line; (line = inp.readln) != null; ) {
		{
			auto tks = line.split;
			M = tks[0].to!int;
			N = tks[1].to!int;
			K = tks[2].to!int;
		}

		string[][] boxes = new string[][](M, N);
		foreach (k; 0 .. K) {
			auto tks = inp.readln.split;
			switch (tks[0].to!int) {
				case +1: {
					const sx = tks[1].to!int - 1;
					const sy = tks[2].to!int - 1;
					const name = tks[3];
					bool flg;
					foreach (x; 0 .. M) foreach (y; 0 .. N) {
						if (x == sx && y == sy) {
							flg = true;
						}
						if (flg) {
							if (boxes[x][y] == "") {
								boxes[x][y] = name;
								goto done;
							}
						}
					}
				done:{}
				} break;
				case -1: {
					const name = tks[1];
					foreach (x; 0 .. M) foreach (y; 0 .. N) {
						if (boxes[x][y] == name) {
							boxes[x][y] = "";
							outp.writeln(x + 1, " ", y + 1);
							goto found;
						}
					}
					outp.writeln("-1 -1");
				found:{}
				} break;
				default: assert(false);
			}
		}

	}
}


Recently a tournament in k kinds of sports has begun in Berland. Vasya wants to make money on the bets.The scheme of the tournament is very mysterious and not fully disclosed. Competitions are held back to back, each of them involves two sportsmen who have not left the tournament yet. Each match can be held in any of the k kinds of sport. Loser leaves the tournament. The last remaining sportsman becomes the winner. Apart of this, the scheme can be arbitrary, it is not disclosed in advance.Vasya knows powers of sportsmen in each kind of sport. He believes that the sportsmen with higher power always wins.The tournament is held every year, and each year one new participant joins it. In the first tournament, only one sportsman has participated, in the second there were two sportsmen, and so on. Vasya has been watching the tournament for the last n years. Help him to find the number of possible winners for each of the n tournaments.

The first line contains two integers n and k (1 ≤ n ≤ 5·104, 1 ≤ k ≤ 10) — the number of tournaments and the number of kinds of sport, respectively. Each of the next n lines contains k integers si1, si2, ..., sik (1 ≤ sij ≤ 109), where sij is the power of the i-th sportsman in the j-th kind of sport. The sportsman with higher powers always wins. It's guaranteed that for any kind of sport all of these powers are distinct.

For each of the n tournaments output the number of contenders who can win.

NoteIn the first sample:In the first tournament there is only one sportsman, and he is the winner.In the second tournament, there are two sportsmen, and everyone can defeat another, depending on kind of sports.In the third tournament, the third sportsman in the strongest in both kinds of sports, so he is the winner regardless of the scheme.

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

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

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

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

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

      class Component {
        int size;
        int[] mins, maxs;
        this(int[] s) {
          size = 1;
          mins = s.dup;
          maxs = s.dup;
        }
        void add(Component o) {
          size += o.size;
          foreach (k; 0 .. K) {
            chmin(mins[k], o.mins[k]);
            chmax(maxs[k], o.maxs[k]);
          }
        }
      }

      auto set = new RedBlackTree!(Component, "a.mins[0] < b.mins[0]");
      foreach (i; 0 .. N) {
        auto c = new Component(S[i]);
        for (; ; ) {
          {
            auto ran = set.lowerBound(c);
            if (!ran.empty) {
              auto cc = ran.back;
              bool ok;
              foreach (k; 0 .. K) {
                if (cc.maxs[k] >= c.mins[k]) {
                  ok = true;
                  break;
                }
              }
              if (ok) {
                c.add(cc);
                set.removeKey(cc);
                goto found;
              }
            }
          }
          {
            auto ran = set.upperBound(c);
            if (!ran.empty) {
              auto cc = ran.front;
              bool ok;
              foreach (k; 0 .. K) {
                if (c.maxs[k] >= cc.mins[k]) {
                  ok = true;
                  break;
                }
              }
              if (ok) {
                c.add(cc);
                set.removeKey(cc);
                goto found;
              }
            }
          }
          break;
         found:
        }
        set.insert(c);
        debug {
          foreach (cc; set) {
            writef("(%s, %s, %s) ", cc.size, cc.mins, cc.maxs);
          }
          writeln();
        }
        writeln(set.back.size);
      }
    }
  } catch (EOFException e) {
  }
}