One very important person has a piece of paper in the form of a rectangle a × b.Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi × yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?

The first line contains three integer numbers n, a and b (1 ≤ n, a, b ≤ 100). Each of the next n lines contain two numbers xi, yi (1 ≤ xi, yi ≤ 100).

Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.

NoteIn the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area.In the third example there is no such pair of seals that they both can fit on a piece of paper.

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto H = s[1];
    auto W = s[2];
    auto X = new int[](N);
    auto Y = new int[](N);
    foreach (i; 0..N) {
        s = readln.split.map!(to!int);
        X[i] = s[0];
        Y[i] = s[1];
    }

    int x1, y1, x2, y2;
    int ans = 0;

    foreach (i; 0..N) {
        foreach (j; i+1..N) {
            foreach (rot; 0..4) {
                if (rot == 0) x1 = X[i], y1 = Y[i], x2 = X[j], y2 = Y[j];
                if (rot == 1) x1 = X[i], y1 = Y[i], x2 = Y[j], y2 = X[j];
                if (rot == 2) x1 = Y[i], y1 = X[i], x2 = X[j], y2 = Y[j];
                if (rot == 3) x1 = Y[i], y1 = X[i], x2 = Y[j], y2 = X[j];
                if (x1 + x2 <= H && max(y1, y2) <= W) ans = max(ans, X[i] * Y[i] + X[j] * Y[j]);
                if (max(x1, x2) <= H && y1 + y2 <= W) ans = max(ans, X[i] * Y[i] + X[j] * Y[j]);
            }
        }
    }

    ans.writeln;
}

// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio, std.string, std.array, std.algorithm, std.conv;

void main() {
    auto nab = to!(int[])(split(chomp(readln())));
    int n = nab[0], a = nab[1], b = nab[2];
    int ans = 0;
    int[101] u, w;
    for (int i = 0; i < n; i++) {
        auto xy = to!(int[])(split(chomp(readln())));
        int x = xy[0], y = xy[1];
        int s = x * y;
        if (x <= a && y <= b) {
            for (int j = 1; j < 101; j++) {
                if (u[j] > 0 && j + x <= a) {
                    ans = max(ans, u[j] + s);
                }
                if (w[j] > 0 && j + y <= b) {
                    ans = max(ans, w[j] + s);
                }
            }
        }
        if (y <= a && x <= b) {
            for (int j = 1; j < 101; j++) {
                if (u[j] > 0 && j + y <= a) {
                    ans = max(ans, u[j] + s);
                }
                if (w[j] > 0 && j + x <= b) {
                    ans = max(ans, w[j] + s);
                }
            }
        }
        if (x <= a && y <= b) { u[x] = max(u[x], s); w[y] = max(w[y], s); }
        if (y <= a && x <= b) { u[y] = max(u[y], s); w[x] = max(w[x], s); }
    }
    writeln(ans);
}

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;

int n, a, b;
int[] x, y;

void main() {
    scan(n, a, b);

    x = new int[](n);
    y = new int[](n);

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

    bool check(int U, int V, int u, int v) {
        return (u <= U && v <= V) || (u <= V && v <= U);
    }

    int ans;

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. n) {
            if (i == j) continue;

            if (x[i] <= a && y[i] <= b) {
                if (check(a - x[i], b, x[j], y[j]) || check(a, b - y[i], x[j], y[j])) {
                    ans = max(ans, x[i]*y[i] + x[j]*y[j]);
                }
            }

            if (x[i] <= b && y[i] <= a) {
                if (check(a - y[i], b, x[j], y[j]) || check(a, b - x[i], x[j], y[j])) {
                    ans = max(ans, x[i]*y[i] + x[j]*y[j]);
                }
            }
        }
    }

    writeln(ans);
}

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

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


This is an interactive problem.Given a simple undirected graph with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, your task is to color all the vertices such that for every color $$$c$$$, the following conditions hold:   The set of vertices with color $$$c$$$ is connected;  $$$s_c \leq n_c^2$$$, where $$$n_c$$$ is the number of vertices with color $$$c$$$, and $$$s_c$$$ is the sum of degrees of vertices with color $$$c$$$.  It can be shown that there always exists a way to color all the vertices such that the above conditions hold. Initially, you are only given the number $$$n$$$ of vertices and the degree of each vertex. In each query, you can choose a vertex $$$u$$$. As a response, you will be given the $$$k$$$-th edge incident to $$$u$$$, if this is the $$$k$$$-th query on vertex $$$u$$$. You are allowed to make at most $$$n$$$ queries.An undirected graph is simple if it does not contain multiple edges or self-loops.The degree of a vertex is the number of edges incident to it. A set $$$S$$$ of vertices is connected if for every two different vertices $$$u, v \in S$$$, there is a path, which only passes through vertices in $$$S$$$, that connects $$$u$$$ and $$$v$$$. That is, there is a sequence of edges $$$(u_1, v_1), (u_2, v_2), \dots, (u_k, v_k)$$$ with $$$k \geq 1$$$ such that   $$$u_1 = u$$$, $$$v_k = v$$$, and $$$v_i = u_{i+1}$$$ for every $$$1 \leq i < k$$$; and  $$$u_k \in S$$$ and $$$v_k \in S$$$ for every $$$1 \leq i \leq k$$$.  Especially, a set containing only one vertex is connected.

NoteIn the example, there is only one test case. In the test case, there are $$$n = 5$$$ vertices with vertices $$$1, 2, 3, 4$$$ of degree $$$2$$$ and vertex $$$5$$$ of degree $$$0$$$. It is obvious that vertex $$$5$$$ is isolated, i.e., it does not connect to any other vertices. A possible interaction is shown in the sample input and output, where $$$4$$$ "?" queries are made on vertex $$$1$$$ twice and vertex $$$3$$$ twice. According to the responses to these queries, we know that each of vertex $$$1$$$ and vertex $$$3$$$ connects to two vertices $$$2$$$ and $$$4$$$. A possible solution is shown in the sample output, where vertex $$$1$$$ and vertex $$$2$$$ are colored by $$$1$$$, vertex $$$3$$$ and vertex $$$4$$$ are colored by $$$2$$$, and vertex $$$5$$$ is colored by $$$3$$$. It can be seen that this solution satisfies the required conditions as follows.   For color $$$c = 1$$$, vertex $$$1$$$ and vertex $$$2$$$ are connected. Moreover, $$$n_1 = 2$$$ and $$$s_1 = d_1 + d_2 = 2 + 2 = 4 \leq n_1^2 = 2^2 = 4$$$;  For color $$$c = 2$$$, vertex $$$3$$$ and vertex $$$4$$$ are connected. Moreover, $$$n_2 = 2$$$ and $$$s_2 = d_3 + d_4 = 2 + 2 = 4 \leq n_2^2 = 2^2 = 4$$$;  For color $$$c = 3$$$, there is only one vertex (vertex $$$5$$$) colored by $$$3$$$. Moreover, $$$n_3 = 1$$$ and $$$s_3 = d_5 = 0 \leq n_3^2 = 1^2 = 1$$$.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto d = readln.splitter.map !(to !(int)).array;

		auto order = n.iota.array;
		order.sort !((i, j) => d[i] > d[j]);
		auto color = new int [n];

		void recolorAs (int v, int u)
		{
			auto oldColor = color[v];
			auto newColor = color[u];
			foreach (i; 0..n)
			{
				if (color[i] == oldColor)
				{
					color[i] = newColor;
				}
			}
		}

		foreach (v; order)
		{
			if (color[v] != 0)
			{
				continue;
			}
			auto cur = d[v];
			color[v] = v + 1;
			foreach (step; 0..cur)
			{
				writefln !("? %s") (v + 1);
				stdout.flush ();
				auto u = readln.strip.to !(int);
				u -= 1;
				if (color[u] == 0)
				{
					color[u] = color[v];
				}
				else
				{
					recolorAs (u, v);
					break;
				}
			}
		}

		writefln !("! %(%s %)") (color);
		stdout.flush ();
	}
}

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

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

int Ask(int u) {
  writeln("? ", u + 1);
  stdout.flush;
  const v = readInt - 1;
  return v;
}

void main() {
  const numCases = readInt;
  foreach (caseId; 0 .. numCases) {
    const N = readInt;
    auto D = new int[N];
    foreach (i; 0 .. N) {
      D[i] = readInt;
    }

    int[] us = iota(N).array;
    us.sort!((u, v) => (D[u] < D[v]));

    auto uf = new int[N];
    uf[] = -1;
    auto vis = new bool[N];
    foreach_reverse (u; us) if (!vis[u]) {
      vis[u] = true;
      foreach (j; 0 .. D[u]) {
        const v = Ask(u);
        uf.connect(u, v);
        if (vis[v]) {
          break;
        }
        vis[v] = true;
      }
    }

    write("!");
    foreach (u; 0 .. N) {
      write(" ", uf.root(u) + 1);
    }
    writeln;
    stdout.flush;
  }
}


Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that.  Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then:  at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$);  at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$);  the $$$6$$$-th granny cannot be called into the yard  — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$.

For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard.

NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total.

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

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;
		sort (a);
		int res = 1;
		foreach (i, c; a)
		{
			if (c <= i + 1)
			{
				res = i + 2;
			}
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = 1;
		a.sort();
		foreach (i; 0..n)
		{
			if (a[i] <= i+1)
				ans[ti] = i+2;
		}
	}

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


You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a $$$r \times c$$$ grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad.Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say $$$a$$$, passes by another country $$$b$$$, they change the dominant religion of country $$$b$$$ to the dominant religion of country $$$a$$$.In particular, a single use of your power is this:   You choose a horizontal $$$1 \times x$$$ subgrid or a vertical $$$x \times 1$$$ subgrid. That value of $$$x$$$ is up to you;  You choose a direction $$$d$$$. If you chose a horizontal subgrid, your choices will either be NORTH or SOUTH. If you choose a vertical subgrid, your choices will either be EAST or WEST;  You choose the number $$$s$$$ of steps;  You command each country in the subgrid to send a missionary group that will travel $$$s$$$ steps towards direction $$$d$$$. In each step, they will visit (and in effect convert the dominant religion of) all $$$s$$$ countries they pass through, as detailed above.  The parameters $$$x$$$, $$$d$$$, $$$s$$$ must be chosen in such a way that any of the missionary groups won't leave the grid. The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we've chosen a $$$1 \times 4$$$ subgrid, the direction NORTH, and $$$s = 2$$$ steps.   You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country.What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism?With god, nothing is impossible. But maybe you're not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so.

The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$) denoting the number of test cases. The first line of each test case contains two space-separated integers $$$r$$$ and $$$c$$$ denoting the dimensions of the grid ($$$1 \le r, c \le 60$$$). The next $$$r$$$ lines each contains $$$c$$$ characters describing the dominant religions in the countries. In particular, the $$$j$$$-th character in the $$$i$$$-th line describes the dominant religion in the country at the cell with row $$$i$$$ and column $$$j$$$, where:   "A" means that the dominant religion is Beingawesomeism;  "P" means that the dominant religion is Pushingittoofarism.  It is guaranteed that the grid will only contain "A" or "P" characters. It is guaranteed that the sum of the $$$r \cdot c$$$ in a single file is at most $$$3 \cdot 10^6$$$.

For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string "MORTAL" (without quotes) if it is impossible to do so.

4
7 8
AAPAAAAA
PPPPAAAA
PPPPAAAA
APAAPPPP
APAPPAPP
AAAAPPAP
AAAAPPAA
6 5
AAAAA
AAAAA
AAPAA
AAPAP
AAAPP
AAAPP
4 4
PPPP
PPPP
PPPP
PPPP
3 4
PPPP
PAAP
PPPP

NoteIn the first test case, it can be done in two usages, as follows:Usage 1:  Usage 2:  In the second test case, it can be done with just one usage of the power. In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is "MORTAL".

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto r = RD!int;
		auto c = RD!int;
		auto s = new string[](r);
		foreach (y; 0..r)
			s[y] = RD!string;
		bool ok1, ok2;
		{
			ok1 = true;
			foreach (y; 0..r)
			{
				foreach (x; 0..c)
				{
					if (s[y][x] == 'P')
					{
						ok1 = false;
					}
					else
					{
						ok2 = true;
					}
				}
			}
			if (ok1)
			{
				ans[ti] = 0;
				continue;
			}
		}
		{
			bool tmp1 = true;
			bool tmp2 = true;
			foreach (x; 0..c)
			{
				if (s[0][x] == 'P')
					tmp1 = false;
				if (s[$-1][x] == 'P')
					tmp2 = false;
			}
			bool tmp3 = true;
			bool tmp4 = true;
			foreach (y; 0..r)
			{
				if (s[y][0] == 'P')
					tmp3 = false;
				if (s[y][$-1] == 'P')
					tmp4 = false;
			}
			if (tmp1 || tmp2 || tmp3 || tmp4)
			{
				ans[ti] = 1;
				continue;
			}
		}
		{
			if (s[0][0] == 'A' || s[r-1][0] == 'A' || s[0][c-1] == 'A' || s[r-1][c-1] == 'A')
			{
				ans[ti] = 2;
				continue;
			}
			bool ok;
			foreach (y; 0..r)
			{
				bool tmp = true;
				foreach (x; 0..c)
				{
					if (s[y][x] == 'P')
					{
						tmp = false;
						break;
					}
				}
				if (tmp)
				{
					ok = true;
					break;
				}
			}
			if (ok)
			{
				ans[ti] = 2;
				continue;
			}
			foreach (x; 0..c)
			{
				bool tmp = true;
				foreach (y; 0..r)
				{
					if (s[y][x] == 'P')
					{
						tmp = false;
						break;
					}
				}
				if (tmp)
				{
					ok = true;
					break;
				}
			}
			if (ok)
			{
				ans[ti] = 2;
				continue;
			}
		}
		{
			bool tmp1, tmp2;
			foreach (x; 0..c)
			{
				if (s[0][x] == 'A')
					tmp1 = true;
				if (s[$-1][x] == 'A')
					tmp2 = true;
			}
			bool tmp3, tmp4;
			foreach (y; 0..r)
			{
				if (s[y][0] == 'A')
					tmp3 = true;
				if (s[y][$-1] == 'A')
					tmp4 = true;
			}
			if (tmp1 || tmp2 || tmp3 || tmp4)
			{
				ans[ti] = 3;
				continue;
			}
		}
		if (ok2)
		{
			ans[ti] = 4;
		}
		else
		{
			ans[ti] = -1;
		}
	}
	foreach (e; ans)
	{
		if (e == -1)
			writeln("MORTAL");
		else
			writeln(e);
	}

	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto r = RD!int;
		auto c = RD!int;
		auto s = new string[](r);
		foreach (y; 0..r)
			s[y] = RD!string;
		bool ok1, ok2;
		{
			ok1 = true;
			foreach (y; 0..r)
			{
				foreach (x; 0..c)
				{
					if (s[y][x] == 'P')
					{
						ok1 = false;
					}
					else
					{
						ok2 = true;
					}
				}
			}
			if (ok1)
			{
				ans[ti] = 0;
				continue;
			}
		}
		{
			bool tmp1 = true;
			bool tmp2 = true;
			foreach (x; 0..c)
			{
				if (s[0][x] == 'P')
					tmp1 = false;
				if (s[$-1][x] == 'P')
					tmp2 = false;
			}
			bool tmp3 = true;
			bool tmp4 = true;
			foreach (y; 0..r)
			{
				if (s[y][0] == 'P')
					tmp3 = false;
				if (s[y][$-1] == 'P')
					tmp4 = false;
			}
			if (tmp1 || tmp2 || tmp3 || tmp4)
			{
				ans[ti] = 1;
				continue;
			}
		}
		{
			if (s[0][0] == 'A' || s[r-1][0] == 'A' || s[0][c-1] == 'A' || s[r-1][c-1] == 'A')
			{
				ans[ti] = 2;
				continue;
			}
		}
		{
			bool tmp1, tmp2;
			foreach (x; 0..c)
			{
				if (s[0][x] == 'A')
					tmp1 = true;
				if (s[$-1][x] == 'A')
					tmp2 = true;
			}
			bool tmp3, tmp4;
			foreach (y; 0..r)
			{
				if (s[y][0] == 'A')
					tmp3 = true;
				if (s[y][$-1] == 'A')
					tmp4 = true;
			}
			if (tmp1 || tmp2 || tmp3 || tmp4)
			{
				ans[ti] = 3;
				continue;
			}
		}
		if (ok2)
		{
			ans[ti] = 4;
		}
		else
		{
			ans[ti] = -1;
		}
	}
	foreach (e; ans)
	{
		if (e == -1)
			writeln("MORTAL");
		else
			writeln(e);
	}

	stdout.flush;
	debug readln;
}


Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire.  Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100).  The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.

On the i-th line of the output print the number of birds on the i-th wire.

import std.algorithm;
import std.stdio;
import std.typecons;

void main ()
{
	int n;
	while (readf (" %d", &n) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %d", &a[i]);
		}
		int m;
		readf (" %d", &m);
		foreach (j; 0..m)
		{
			int x, y;
			readf (" %d %d", &x, &y);
			x--;
			y--;
			if (x - 1 >= 0)
			{
				a[x - 1] += y;
			}
			if (x + 1 < n)
			{
				a[x + 1] += a[x] - y - 1;
			}
			a[x] = 0;
		}
		writefln ("%(%d\n%)", a);
	}
}

module sigod.codeforces.p294A;

import std.conv;
import std.stdio;
import std.string;

void main()
{
	int N;
	stdin.readf("%s", &N);
	stdin.readln();

	int[] counts = read_array!int();

	int M;
	stdin.readf("%s", &M);
	stdin.readln();

	foreach (i; 0 .. M) {
		int x, y;

		stdin.readf("%s %s", &x, &y);
		stdin.readln();

		--x;

		if (x > 0) {
			counts[x - 1] += y - 1;
		}
		if (x < N - 1) {
			counts[x + 1] += counts[x] - y;
		}

		counts[x] = 0;
	}

	foreach (count; counts) {
		stdout.writeln(count);
	}
}

private
T[] read_array(T)()
{
	auto input = stdin.readln().strip().split();

	T[] result = new T[input.length];

	foreach (index, ref element; input) {
		result[index] = element.to!T();
	}

	return result;
}


Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, and after that go to some other city $$$c_2 > c_1$$$, then to some other city $$$c_3 > c_2$$$, and so on, until she chooses to end her journey in some city $$$c_k > c_{k - 1}$$$. So, the sequence of visited cities $$$[c_1, c_2, \dots, c_k]$$$ should be strictly increasing.There are some additional constraints on the sequence of cities Tanya visits. Each city $$$i$$$ has a beauty value $$$b_i$$$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $$$c_i$$$ and $$$c_{i + 1}$$$, the condition $$$c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$$$ must hold.For example, if $$$n = 8$$$ and $$$b = [3, 4, 4, 6, 6, 7, 8, 9]$$$, there are several three possible ways to plan a journey:  $$$c = [1, 2, 4]$$$;  $$$c = [3, 5, 6, 8]$$$;  $$$c = [7]$$$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above.Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of cities in Berland. The second line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$1 \le b_i \le 4 \cdot 10^5$$$), where $$$b_i$$$ is the beauty value of the $$$i$$$-th city.

Print one integer — the maximum beauty of a journey Tanya can choose.

NoteThe optimal journey plan in the first example is $$$c = [2, 4, 5]$$$.The optimal journey plan in the second example is $$$c = [1]$$$.The optimal journey plan in the third example is $$$c = [3, 6]$$$.

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

void main()
{
  long n; get(n);
  auto b = new long[cast(size_t)n]; get(b);
  long[long] mb;
  long m = long.min;
  zip(iota(0, n), b).each!((p) {mb[p[1] - p[0]] += b[cast(size_t)p[0]]; m = max(m, mb[p[1] - p[0]]);});
  ans(m);
}

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

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

void solve(){
    int n = scan!int;
    long[] as = scan!long(n);

    long[] xs = new long[](600_999);
    foreach(i, a; as){
        int d = n + a.to!int - (i + 1).to!int;
        xs[d] += a;
    }

    long ans = 0;
    foreach(x; xs) ans.raiseTo(x);

    ans.writeln;
}

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

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

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

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

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

      long[long] sums;
      foreach (i; 0 .. N) {
        sums[B[i] - i] += B[i];
      }
      const ans = sums.values.maxElement;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}

void main()
{
  int n; get(n);
  auto b = new int[n]; get(b);
  int[int] mb;
  int m = int.min;
  zip(iota(0, n), b).each!((p) {mb[p[1] - p[0]] += b[p[0]]; m = max(m, mb[p[1] - p[0]]);});
  ans(m);
}


Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment $$$0$$$. Also, let's say that the train will visit $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$ along its way, and that Alexey destination is the station $$$n$$$.Alexey learned from the train schedule $$$n$$$ integer pairs $$$(a_i, b_i)$$$ where $$$a_i$$$ is the expected time of train's arrival at the $$$i$$$-th station and $$$b_i$$$ is the expected time of departure.Also, using all information he has, Alexey was able to calculate $$$n$$$ integers $$$tm_1, tm_2, \dots, tm_n$$$ where $$$tm_i$$$ is the extra time the train need to travel from the station $$$i - 1$$$ to the station $$$i$$$. Formally, the train needs exactly $$$a_i - b_{i-1} + tm_i$$$ time to travel from station $$$i - 1$$$ to station $$$i$$$ (if $$$i = 1$$$ then $$$b_0$$$ is the moment the train leave the terminal, and it's equal to $$$0$$$).The train leaves the station $$$i$$$, if both conditions are met:   it's on the station for at least $$$\left\lceil \frac{b_i - a_i}{2} \right\rceil$$$ units of time (division with ceiling);  current time $$$\ge b_i$$$. Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station $$$n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of stations. Next $$$n$$$ lines contain two integers each: $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i < b_i \le 10^6$$$). It's guaranteed that $$$b_i < a_{i+1}$$$.  Next line contains $$$n$$$ integers $$$tm_1, tm_2, \dots, tm_n$$$ ($$$0 \le tm_i \le 10^6$$$).

For each test case, print one integer — the time of Alexey's arrival at the last station.

NoteIn the first test case, Alexey arrives at station $$$1$$$ without any delay at the moment $$$a_1 = 2$$$ (since $$$tm_1 = 0$$$). After that, he departs at moment $$$b_1 = 4$$$. Finally, he arrives at station $$$2$$$ with $$$tm_2 = 2$$$ extra time, or at the moment $$$12$$$.In the second test case, Alexey arrives at the first station with $$$tm_1 = 1$$$ extra time, or at moment $$$2$$$. The train, from one side, should stay at the station at least $$$\left\lceil \frac{b_1 - a_1}{2} \right\rceil = 2$$$ units of time and from the other side should depart not earlier than at moment $$$b_1 = 4$$$. As a result, the trains departs right at the moment $$$4$$$.Using the same logic, we can figure out that the train arrives at the second station at the moment $$$9$$$ and departs at the moment $$$10$$$; at the third station: arrives at $$$14$$$ and departs at $$$15$$$; at the fourth: arrives at $$$22$$$ and departs at $$$23$$$. And, finally, arrives at the fifth station at $$$32$$$.

import std.stdio, std.string, std.algorithm, std.range, std.conv;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n;
        readf!"%d\n"(n);
        int[] a = new int[n], b = new int[n];
        foreach (i; 0 .. n)
            readf!"%d %d\n"(a[i], b[i]);
        int[] tm = readln.split.map!(to!int).array;
        int res = 0;
        foreach (i; 0 .. n)
        {
            res += a[i] - (i > 0 ? b[i - 1] : 0) + tm[i];
            if (i < n - 1)
                res = max(res + (b[i] - a[i] + 1) / 2, b[i]);
        }
        writeln(res);
    }
}

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 a = new long[](n);
		auto b = new long[](n);
		foreach (i; 0..n)
		{
			a[i] = RD;
			b[i] = RD;
		}
		auto tm = RDA;

		long time;
		foreach (i; 0..n)
		{
			long d;
			if (i == 0)
				d = a[i] + tm[i];
			else
				d = a[i] - b[i-1] + tm[i];
			time += d;
			debug writeln("time:", time);
			if (i == n-1) break;
			auto w = (b[i]-a[i]+1) / 2;
			time = max(time+w, b[i]);
		}
		ans[ti] = time;
	}

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


Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second.Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecting segments, the $$$i$$$-th segment which is in the rain is represented as $$$[l_i, r_i]$$$ ($$$0 \le l_i < r_i \le a$$$).There are $$$m$$$ umbrellas lying on the line, the $$$i$$$-th umbrella is located at point $$$x_i$$$ ($$$0 \le x_i \le a$$$) and has weight $$$p_i$$$. When Polycarp begins his journey, he doesn't have any umbrellas.During his journey from $$$x = 0$$$ to $$$x = a$$$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $$$x$$$ to $$$x + 1$$$ if a segment $$$[x, x + 1]$$$ is in the rain (i.e. if there exists some $$$i$$$ such that $$$l_i \le x$$$ and $$$x + 1 \le r_i$$$).The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.Can Polycarp make his way from point $$$x = 0$$$ to point $$$x = a$$$? If yes, find the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.

The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l_i < r_i \le a$$$) — the borders of the $$$i$$$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $$$i$$$ and $$$j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$0 \le x_i \le a$$$, $$$1 \le p_i \le 10^5$$$) — the location and the weight of the $$$i$$$-th umbrella.

Print "-1" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.

NoteIn the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and keep it till the end (the total fatigue at $$$x = 10$$$ will be equal to $$$14$$$). In the second example the only possible strategy is to take the first umbrella, move with it till the point $$$x = 9$$$, throw it away and proceed without an umbrella till the end.

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, std.bitmanip;

void main() {
    long INF = 1L << 59;

    auto s = readln.split.map!(to!int);
    auto A = s[0];
    auto M = s[1];
    auto N = s[2];
    auto R = M.iota.map!(_ => readln.split.map!(to!int).array).array;
    auto U = N.iota.map!(_ => readln.split.map!(to!int).array).array;

    auto rain = new bool[](A+1);
    auto umb = new int[][](A+1);

    foreach (lr; R) {
        foreach (j; lr[0]+1..lr[1]+1) {
            rain[j] = true;
        }
    }

    foreach (i; 0..N) {
        umb[U[i][0]] ~= i;
    }

    auto dp = new long[][](A+1, N+1);
    foreach (i; 0..A+1) dp[i].fill(INF);
    dp[0][N] = 0;

    foreach (i; 0..A) {
        // swap umbrellas
        long m = dp[i].reduce!min;
        foreach (u; umb[i]) dp[i][u] = m;
        dp[i][N] = rain[i+1] ? INF : m;

        //
        foreach (j; 0..N) {
            if (dp[i][j] == INF) continue;
            dp[i+1][j] = min(dp[i+1][j], dp[i][j] + U[j][1]);
        }
        dp[i+1][N] = dp[i][N];
    }

    long ans;
    if (rain[A]) {
        ans = dp[A][0..N].reduce!min;
    } else {
        ans = dp[A].reduce!min;
    }

    writeln(ans >= INF ? -1 : ans);
}


A pair of positive integers $$$(a,b)$$$ is called special if $$$\lfloor \frac{a}{b} \rfloor = a \bmod b$$$. Here, $$$\lfloor \frac{a}{b} \rfloor$$$ is the result of the integer division between $$$a$$$ and $$$b$$$, while $$$a \bmod b$$$ is its remainder.You are given two integers $$$x$$$ and $$$y$$$. Find the number of special pairs $$$(a,b)$$$ such that $$$1\leq a \leq x$$$ and $$$1 \leq b \leq y$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$x$$$, $$$y$$$ ($$$1 \le x,y \le 10^9$$$).

For each test case print the answer on a single line.

NoteIn the first test case, the only special pair is $$$(3, 2)$$$.In the second test case, there are no special pairs.In the third test case, there are two special pairs: $$$(3, 2)$$$ and $$$(4, 3)$$$.

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

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto v = readln.splitter.map!(to!int).array;
        auto x = v[0], y = v[1];

        long ans = 0;
        foreach (r; 1 .. y) {
            int left = x - r;
            if (left <= 1) { break; }

            int mxk = min(left / r, y);
            if (mxk <= r) { break; }

            int cur = mxk - r;

            ans += cur;
        }

        ans.writeln;
    }
}

import std;

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

void solve() {
    long X, Y; readf("%d %d\n", &X, &Y);
    long bs; {
        long lb = 0, ub = 1L<<30;
        while (lb + 1 < ub) {
            long mid = (lb + ub) / 2;
            (mid - 1 <= X / (mid + 1L) ? lb : ub) = mid;
        }
        bs = min(lb, Y);
    }
    long A = (1L + bs) * bs / 2L - bs;
    long B = 0;
    if (bs == Y) {
    } else {
        for (long k = 1; k <= X / (bs + 1L); k++) {
            long U = min(Y, X / k - 1L);
            long L = max(bs, X / (k + 1L) - 1L);
            B += k * max(0L, U - L);
        }
    }
    writeln(A + B);
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        long x, y; get(x, y);
        long res;
        for (long i = 1; i^^2 <= x; ++i) {
            res += max(0, min((x - i) / i, y) - i);
        }
        writeln(res);
    }
}

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 x = RD;
		auto y = RD;

		for (long i = 1; i*i < x; ++i)
		{
			auto xx = x - i;
			auto r = min(xx / i, y) - i;
			if (r > 0)
				ans[ti] += r;
		}
	}

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

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 x, y;
		readf !(" %s %s") (x, y);
		long res = 0;
		for (int r = 1; ; r++)
		{
			int lo = r + 2;
			int hi = min (y + 1, x / r);
			if (hi < lo)
			{
				break;
			}
			res += hi - lo + 1;
		}
		writeln (res);
	}
}

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

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto v = readln.splitter.map!(to!int).array;
        auto x = v[0], y = v[1];

        int ans = 0;
        foreach (r; 1 .. y) {
            int left = x - r;
            if (left <= 1) { break; }

            int mxk = min(left / r, y);
            if (mxk <= r) { break; }

            int cur = mxk - r;

            ans += cur;
        }

        ans.writeln;
    }
}

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

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto v = readln.splitter.map!(to!int).array;
        auto x = v[0], y = v[1];

        int ans = 0;
        foreach (b; 1 .. y + 1) {
            int left = x - b;
            if (left <= 1) { break; }

            int mxk = min(left / b, y);
            if (mxk - b <= 0) { break; }

            int cur = mxk - b;

            debug { writeln(b, ' ', cur); }

            ans += cur;
        }

        ans.writeln;
    }
}

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

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto v = readln.splitter.map!(to!int).array;
        auto x = v[0], y = v[1];

        int sq = sqrt(x.to!real).to!int;

        int ans = 0;
        foreach (b; 1 .. min(y, sq) + 1) {
            int left = x - b;
            if (left <= 1) { break; }

            int mxk = min(left / b, y);
            if (mxk - b <= 0) { break; }

            int cur = mxk - b;

            debug { writeln(b, ' ', cur); }

            ans += cur;
        }

        ans.writeln;
    }
}


One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.

The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops. Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct.

If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).

import std.stdio;

void main() {
	int n;
	readf("%s", &n);
	foreach(_; 0..n) {
		int ai, bi;
		readf(" %s %s ", &ai, &bi);
		if (ai != bi) {
			writeln("Happy Alex");
			return;
		}
	}
	writeln("Poor Alex");
}

import std.typecons;
import std.algorithm;

alias Note = Tuple!(uint, "a", uint, "b");

bool noteLess(const Note a, const Note b) {
	if (a.a == b.a) {
		return a.b > b.b;
	}

	return a.a < b.a;
}

bool solve(Note[] ns) {
	sort!(noteLess)(ns);
	foreach (i; 1..ns.length) {
		Note prev = ns[i - 1];
		Note cur = ns[i];
		if (prev.a < cur.a && prev.b > cur.b) {
			return true;
		}
	}
	return false;
}

unittest {
	{
		Note[] ns = [Note(1, 2), Note(2, 1)];
		assert(solve(ns));
	}

	{
		Note[] ns = [Note(2, 1), Note(1, 2)];
		assert(solve(ns));
	}

	{
		Note[] ns = [Note(1, 1)];
		assert(false == solve(ns));
	}

	{
		Note[] ns = [Note(1, 1), Note(2, 2), Note(3, 3), Note(4, 4), Note(5, 5)];
		assert(false == solve(ns));
	}

	{
		Note[] ns = [Note(4, 5), Note(2, 2), Note(3, 3), Note(1, 1), Note(5, 4)];
		assert(solve(ns));
	}
}

int main(string[] argv) {
	import std.stdio;
	uint n = 0;
	readf(" %s", &n);
	auto ns = new Note[](n);
	foreach (i; 0..n) {
		readf(" %s %s", &(ns[i].a), &(ns[i].b));
	}
	writeln(solve(ns) ? "Happy Alex" : "Poor Alex");
    return 0;
}

import std.stdio;

void main() {
    int n; readf("%d\n", &n);
    bool poor = true;
    for (int i = 0; i < n; ++i) {
        int a, b;
        readf("%d %d\n", &a, &b);
        if (poor && a != b) {
            poor = false;
        }
    }
    if (poor)
        writeln("Poor Alex");
    else
        writeln("Happy Alex");
}

import std.stdio;
import std.string;
import std.conv;
import std.regex;
import std.algorithm.iteration;
import std.algorithm.sorting;
import std.typecons;

void main()
{
  auto n = stdin.readln.strip.to!int;
  alias DicEntry = Tuple!(int, "price", int, "quality");
  DicEntry[]  prices;
  prices.length = n;

  for(int i=0; i<n; i++) {
    auto s = stdin.readln.strip.split(regex(` +`)).map!(to!int);
    prices[i].price = s[0];
    prices[i].quality = s[1];
  }
  auto s = "Poor Alex";
  prices.sort!((a,b)=>a.price < b.price);
  for(int i=0; i<n-1; i++) {
    if (prices[i].price < prices[i+1].price && prices[i].quality > prices[i+1].quality) {
        s = "Happy Alex";
        break;
    }
  }
  writeln(s);
}

// https://codeforces.com/problemset/problem/456/A

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

void main() {
    int n = readln.chomp.to!int;
    int[][] laptops;

    int a, b;
    foreach(idx; 0..n) {
        int[] laptop;
        readf("%d %d\n", &a, &b);
        laptop ~= [a,b];
        laptops ~= laptop;
    }

    bool happy = false;
    //laptops.sort();

    for(int i = 0; i < n; i++)
        if(laptops[i][0] != laptops[i][1])
            happy = true;

    /* brute force
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(laptops[i][0] < laptops[j][0] &&
                laptops[i][1] > laptops[j][1])
                happy = true;
        }
    }
   */

    if(happy)
        writeln("Happy Alex");
    else
        writeln("Poor Alex");
}

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

void main() {
    int n = readln.chomp.to!int;
    int[][] info;

    foreach (i; 0..n) {
        info ~= readln.chomp.split.map!(to!int).array;
    }

    string ans = "Poor Alex";
    info.sort;
    foreach (i; 1..n) {
        if (info[i][1] < info[i - 1][1]) {
            ans = "Happy Alex";
            break;
        }
    }

    ans.writeln;
}

import std.stdio;
import std.algorithm;

unittest{
  assert(solve([[1,2],[2,1]]) == true, "Test 1");
  assert(solve([[1,1],[2,2]]) == false, "Test 2");
  assert(solve([[2,2],[3,3],[1,1]]) == false, "Test 3");

}

bool solve(int[2][] lap){
  for(int i =1; i < lap.length; i++){
    if(!(lap[i][0] == lap[i][1])){
      return true;
    }
  }
  return false;
}

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

  int[2][] lap = new int[2][n];
  for(int i = 0; i < n; i++){
    int p,q;
    readf("%s %s\n",&p,&q);
    lap[i][0] = p;
    lap[i][1] = q;
  }

  if (solve(lap)) {
    writeln("Happy Alex");
  } else {
    writeln("Poor Alex");
  }
}

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 N;
int[] A, B;

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

		auto ps = new Pair!(int, int)[N];
		foreach (i; 0 .. N) {
			ps[i] = pair(A[i], B[i]);
		}
		ps.sort();
debug{
writeln("ps = ",ps);
}

		bool ans;
		int max;
		foreach (i; 0 .. N) {
			if (max > ps[i].y) {
				ans = true;
			}
			chmax(max, ps[i].y);
		}
		writeln(ans ? "Happy Alex" : "Poor Alex");

	}
	} catch (EOFException) {}
}

import std.stdio;

void main() {
	int n;
	readf("%s", &n);
	foreach(_; 0..n) {
		int ai, bi;
		readf(" %s %s ", &ai, &bi);
		if (ai != bi) {
			writeln("Poor Alex");
			return;
		}
	}
	writeln("Happy Alex");
}

import std.stdio;
import std.algorithm;

unittest{
  assert(solve([[1,2,0],[2,1,1]]) == true, "Test 1");
}

bool solve(int[3][] lap){
  for(int i =1; i < lap.length; i++){
    if(!(lap[i][0] == lap[i][i])){
      return true;
    }
  }
  return false;
}

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

  int[3][] lap = new int[3][n];
  for(int i = 0; i < n; i++){
    int p,q;
    readf("%s %s\n",&p,&q);
    lap[i][0] = p;
    lap[i][1] = q;
    lap[i][2] = i;
  }

  if (solve(lap)) {
    writeln("Happy Alex");
  } else {
    writeln("Poor Alex");
  }
}

import std.stdio;
import std.algorithm;

unittest{
  // assert(solve([[1,2,0],[2,1,1]]) == true, "Test 1");
}

bool solve(int[3][] lap){
  sort!((int[3] a,int[3] b){ return a[0] < b[0];})(lap);
  //writeln(lap);

  for(int i =1; i < lap.length; i++){
    for(int j = 0; j < i; j++){
      if(lap[j][1] > lap[i][1]){
        return true;
      }
    }
  }
  return false;
}

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

  int[3][] lap = new int[3][n];
  for(int i = 0; i < n; i++){
    int p,q;
    readf("%s %s\n",&p,&q);
    lap[i][0] = p;
    lap[i][1] = q;
    lap[i][2] = i;
  }

  if (solve(lap)) {
    writeln("Happy Allex");
  } else {
    writeln("Poor Allex");
  }
}

import std.stdio;
import std.algorithm;

unittest{
  assert(solve([[1,2],[2,1]]) == true, "Test 1");
}

bool solve(int[2][] lap){
  for(int i =1; i < lap.length; i++){
    for(int j = 0; j < i; j++){
      if(!(lap[i] == lap[j])){
        return true;
      }
    }
  }
  return false;
}

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

  int[2][] lap = new int[2][n];
  for(int i = 0; i < n; i++){
    int p,q;
    readf("%s %s\n",&p,&q);
    lap[i][0] = p;
    lap[i][1] = q;
  }

  if (solve(lap)) {
    writeln("Happy Allex");
  } else {
    writeln("Poor Allex");
  }
}


After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size $$$n \times m$$$ (where $$$n$$$ is the number of rows, $$$m$$$ is the number of columns) and starts to draw snakes in cells.Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only $$$26$$$ letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed $$$26$$$.Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals $$$1$$$, i.e. each snake has size either $$$1 \times l$$$ or $$$l \times 1$$$, where $$$l$$$ is snake's length. Note that snakes can't bend.When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake "over the top" and overwrites the previous value in the cell.Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases to solve. Then $$$t$$$ test cases follow. The first line of the test case description contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 2000$$$) — length and width of the checkered sheet of paper respectively. Next $$$n$$$ lines of test case description contain $$$m$$$ symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell. It is guaranteed that the total area of all sheets in one test doesn't exceed $$$4\cdot10^6$$$.

Print the answer for each test case in the input. In the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO. If the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer $$$k$$$ ($$$0 \le k \le 26$$$) — number of snakes. Then print $$$k$$$ lines, in each line print four integers $$$r_{1,i}$$$, $$$c_{1,i}$$$, $$$r_{2,i}$$$ and $$$c_{2,i}$$$ — coordinates of extreme cells for the $$$i$$$-th snake ($$$1 \le r_{1,i}, r_{2,i} \le n$$$, $$$1 \le c_{1,i}, c_{2,i} \le m$$$). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them. Note that Polycarp starts drawing of snakes with an empty sheet of paper.

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
multitest_loop:
	foreach (test; 0..tests)
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		char [] [] board;
		board.reserve (rows);
		foreach (row; 0..rows)
		{
			board ~= readln.strip.dup;
		}

		int [] [] ans;
		bool started = false;
		int aRow = int.max;
		int aCol = int.max;
		foreach_reverse (letter; 'a'..'{')
		{
			int rLo = int.max;
			int rHi = int.min;
			int cLo = int.max;
			int cHi = int.min;
			foreach (row; 0..rows)
			{
				foreach (col; 0..cols)
				{
					if (board[row][col] == letter)
					{
						rLo = min (rLo, row);
						rHi = max (rHi, row);
						cLo = min (cLo, col);
						cHi = max (cHi, col);
					}
				}
			}
			if (rLo == int.max)
			{
				if (started)
				{
					ans ~= [aRow, aCol, aRow, aCol];
				}
				continue;
			}
			if (rLo != rHi && cLo != cHi)
			{
				writeln ("NO");
				continue multitest_loop;
			}
			ans ~= [rLo, cLo, rHi, cHi];
			foreach (row; rLo..rHi + 1)
			{
				foreach (col; cLo..cHi + 1)
				{
					if (board[row][col] != letter &&
					    board[row][col] != '*')
					{
						writeln ("NO");
						continue multitest_loop;
					}
					board[row][col] = '*';
				}
			}
			started = true;
			aRow = rLo;
			aCol = cLo;
		}
		debug {writefln ("%-(%s\n%)", board);}
		reverse (ans);
		writeln ("YES");
		writefln ("%s%(\n%(%s %)%)", ans.length,
		    ans.map !(line => line.map !(x => x + 1)));
	}
}


A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.

The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.

Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.

module main;

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

int main()
{
    auto s = readln().strip();

    int[char] countOfLetters;

    foreach (letter; s)
        if (countOfLetters.get(letter, false) == false)
            countOfLetters[letter] = s.countchars(letter.to! (string));

    debug {writeln(countOfLetters);};

    string result;

    foreach (key; countOfLetters.keys())
        if (countOfLetters[key] / 2 > 0) {
            result ~= key.to! (string).repeat(countOfLetters[key] / 2)
                         .join();
            if (countOfLetters[key] % 2 == 0)
                countOfLetters.remove(key);
            else
                countOfLetters[key] = 1;
        }

    while (countOfLetters.length > 1) {
        auto minKey = minPos(countOfLetters.keys()).front.to! (char);
        auto maxKey = minPos!("a > b")(countOfLetters.keys()).front.to! (char);

        result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();
        result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();
        countOfLetters.remove(minKey);
        countOfLetters.remove(maxKey);
    }

    if (countOfLetters.length == 1) {
        auto temp = countOfLetters.keys().front.to! (char);
        result = result.dup.sort.to! (string) ~
                 temp.to! (string).repeat(countOfLetters[temp]).join() ~
                 result.dup.sort.reverse.to! (string);
    } else
        result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);

    writeln(result);

	return 0;
}

module main;

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

int main()
{
    auto s = readln().strip();

    int[char] countOfLetters;

    foreach (letter; s)
        if (countOfLetters.get(letter, false) == false)
            countOfLetters[letter] = s.countchars(letter.to! (string));

    debug {writeln(countOfLetters);};

    string result;

    foreach (key; countOfLetters.keys())
        if (countOfLetters[key] % 2 == 0) {
            result ~= key.to! (string).repeat(countOfLetters[key] / 2)
                         .join();
            countOfLetters.remove(key);
        }

    while (countOfLetters.length > 1) {
        auto minKey = minPos(countOfLetters.keys()).front.to! (char);
        auto maxKey = minPos!("a > b")(countOfLetters.keys()).front.to! (char);

        result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();
        result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();
        countOfLetters.remove(minKey);
        countOfLetters.remove(maxKey);
    }

    if (countOfLetters.length == 1)
        result = result.dup.sort.to! (string) ~
                 countOfLetters.keys().front.to! (string) ~
                 result.dup.sort.reverse.to! (string);
    else
        result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);

    writeln(result);

	return 0;
}

module main;

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

int main()
{
    auto s = readln().strip();

    int[char] countOfLetters;

    foreach (letter; s)
        if (countOfLetters.get(letter, false) == false)
            countOfLetters[letter] = s.countchars(letter.to! (string));

    debug {writeln(countOfLetters);};

    string result;

    foreach (key; countOfLetters.keys())
        if (countOfLetters[key] % 2 == 0) {
            result ~= key.to! (string).repeat(countOfLetters[key] / 2)
                         .join();
            countOfLetters.remove(key);
        }

    while (countOfLetters.length > 1) {
        auto minKey = minPos(countOfLetters.keys()).front.to! (char);
        auto maxKey = minPos!("a > b")(countOfLetters.keys()).front.to! (char);

        result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();
        result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();
        countOfLetters.remove(minKey);
        countOfLetters.remove(maxKey);
    }

    if (countOfLetters.length == 1) {
        auto temp = countOfLetters.keys().front.to! (char);
        result = result.dup.sort.to! (string) ~
                 temp.to! (string).repeat(countOfLetters[temp]).join() ~
                 result.dup.sort.reverse.to! (string);
    } else
        result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);

    writeln(result);

	return 0;
}


A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner.

The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters.

Print the sought number of ways to cut string t in two so that each part made s happy.

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()
{
    string s, t;
    s = readln.chomp;
    t = readln.chomp;

    int GetPos(string s, string t) {
        int idx = 0;
        foreach (i, e; t) {
            if (e == s[idx]) { ++idx; }

            if (idx == s.length) { return i.to!int; }
        }

        return t.length.to!int;
    }

    int le = GetPos(s, t);
    int r = t.length.to!int - 1 - GetPos(s.dup.retro.to!string, t.dup.retro.to!string);

    debug { writeln(le, ' ', r); }

    int ans = max(0, r - le);

    ans.writeln;
}


Bob is playing a game named "Walk on Matrix".In this game, player is given an $$$n \times m$$$ matrix $$$A=(a_{i,j})$$$, i.e. the element in the $$$i$$$-th row in the $$$j$$$-th column is $$$a_{i,j}$$$. Initially, player is located at position $$$(1,1)$$$ with score $$$a_{1,1}$$$. To reach the goal, position $$$(n,m)$$$, player can move right or down, i.e. move from $$$(x,y)$$$ to $$$(x,y+1)$$$ or $$$(x+1,y)$$$, as long as player is still on the matrix.However, each move changes player's score to the bitwise AND of the current score and the value at the position he moves to.Bob can't wait to find out the maximum score he can get using the tool he recently learnt  — dynamic programming. Here is his algorithm for this problem.   However, he suddenly realize that the algorithm above fails to output the maximum score for some matrix $$$A$$$. Thus, for any given non-negative integer $$$k$$$, he wants to find out an $$$n \times m$$$ matrix $$$A=(a_{i,j})$$$ such that   $$$1 \le n,m \le 500$$$ (as Bob hates large matrix);  $$$0 \le a_{i,j} \le 3 \cdot 10^5$$$ for all $$$1 \le i\le n,1 \le j\le m$$$ (as Bob hates large numbers);  the difference between the maximum score he can get and the output of his algorithm is exactly $$$k$$$. It can be shown that for any given integer $$$k$$$ such that $$$0 \le k \le 10^5$$$, there exists a matrix satisfying the above constraints.Please help him with it!

The only line of the input contains one single integer $$$k$$$ ($$$0 \le k \le 10^5$$$).

Output two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 500$$$) in the first line, representing the size of the matrix.  Then output $$$n$$$ lines with $$$m$$$ integers in each line, $$$a_{i,j}$$$ in the $$$(i+1)$$$-th row, $$$j$$$-th column.

NoteIn the first example, the maximum score Bob can achieve is $$$300000$$$, while the output of his algorithm is $$$300000$$$.In the second example, the maximum score Bob can achieve is $$$7\&3\&3\&3\&7\&3=3$$$, while the output of his algorithm is $$$2$$$.

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

void main() {
    auto K = readln.chomp.to!int;
    writeln("3 3");
    auto IK = 0b100000000000000000 + K;
    auto IO = 0b100000000000000000;
    auto OK = K;
    auto ans =
    [[IK, IO, 0],
     [OK, IK, OK],
     [0, 0, OK]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}

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

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

    auto ans = new int[][] (3, 2);

    immutable int bigBit = 2 ^^ 17;

    ans[0][0] = bigBit + k;
    ans[1][0] = bigBit;
    ans[0][1] = k;

    ans[1][1] = bigBit + k;

    ans[2][0] = 0;
    ans[2][1] = k;

    writeln(3, ' ', 2);
    foreach (i; 0 .. 3) {
        ans[i].map!(to!string).join(" ").writeln;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto k = RD!int;
	int digit;
	auto x = k;
	while (x != 0)
	{
		x >>= 1;
		++digit;
	}

	int y = 1 << digit;
	int z = k | y;

	auto ans = new int[][](2, 3);
	ans[0][0] = z;
	ans[1][0] = y;
	ans[0][1] = k;
	ans[1][1] = z;
	ans[0][2] = 0;
	ans[1][2] = k;

	writeln("2 3");
	foreach (i; 0..2)
	{
		ans[i].map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}

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

void main() {
    auto K = readln.chomp.to!int;
    auto ans =
    [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],
     [0b1000000000000000000, 0, K],
     [0b1000000000000000000, 0, K]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}

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

void main() {
    auto K = readln.chomp.to!int;
    writeln("3 3");
    auto ans =
    [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],
     [0b1000000000000000000, 0, K],
     [0b1000000000000000000, 0b1000000000000000000, 0b1000000000000000000+K]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}

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

void main() {
    auto K = readln.chomp.to!int;
    writeln("3 3");
    auto ans =
    [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],
     [0b1000000000000000000, 0, K],
     [0b1000000000000000000, 0, K]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}

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

void main() {
    auto K = readln.chomp.to!int;
    writeln("3 3");
    auto IK = 0b1000000000000000000 + K;
    auto IO = 0b1000000000000000000;
    auto OK = K;
    auto ans =
    [[IK, IO, IO],
     [OK, IO, IO],
     [OK, OK, IK]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}

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

void main() {
    auto K = readln.chomp.to!int;
    writeln("3 3");
    auto IK = 0b1000000000000000000 + K;
    auto IO = 0b1000000000000000000;
    auto OK = K;
    auto ans =
    [[IK, IO, 0],
     [OK, IO, OK],
     [0, 0, OK]];
    foreach (a; ans)  {
        foreach (b; a) write(b, " ");
        writeln;
    }
}


There are $$$n$$$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.The $$$i$$$-th student has integer programming skill $$$a_i$$$. All programming skills are distinct and between $$$1$$$ and $$$n$$$, inclusive.Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $$$k$$$ closest students to the left of him and $$$k$$$ closest students to the right of him (if there are less than $$$k$$$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of students and the value determining the range of chosen students during each move, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student. It is guaranteed that all programming skills are distinct.

Print a string of $$$n$$$ characters; $$$i$$$-th character should be 1 if $$$i$$$-th student joins the first team, or 2 otherwise.

NoteIn the first example the first coach chooses the student on a position $$$3$$$, and the row becomes empty (all students join the first team).In the second example the first coach chooses the student on position $$$4$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team).In the third example the first coach chooses the student on position $$$1$$$, and the row becomes $$$[1, 3, 5, 4, 6]$$$ (students with programming skills $$$[2, 7]$$$ join the first team). Then the second coach chooses the student on position $$$5$$$, and the row becomes $$$[1, 3, 5]$$$ (students with programming skills $$$[4, 6]$$$ join the second team). Then the first coach chooses the student on position $$$3$$$, and the row becomes $$$[1]$$$ (students with programming skills $$$[3, 5]$$$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $$$1$$$ joins the second team).In the fourth example the first coach chooses the student on position $$$3$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team).

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 s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;

    auto B = N.iota.map!(i => tuple(i, A[i])).array;
    B.sort!"a[1] > b[1]";

    auto L = N.iota.map!(i => i.to!int - 1).array;
    auto R = N.iota.map!(i => i.to!int + 1).array;

    auto ans = new int[](N);
    auto used = new bool[](N);

    for (int p = 0, t = 0; p < N; t ^= 1) {
        auto start = B[p][0];
        ans[start] = t + 1;
        used[start] = true;

        int new_l = -1, new_r = N;

        for (int j = 0, i = L[start]; j < K && i != -1; ++j, i = L[i], new_l = i) {
            ans[i] = t + 1;
            used[i] = true;
        }
        for (int j = 0, i = R[start]; j < K && i != N; ++j, i = R[i], new_r = i) {
            ans[i] = t + 1;
            used[i] = true;
        }

        if (new_l != -1) {
            R[new_l] = new_r;
        }
        if (new_r != N) {
            L[new_r] = new_l;
        }

        while (p < N && used[B[p][0]]) ++p;
    }

    ans.map!(a => a.to!string).join("").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, k;
    readf("%s %s", &n, &k);
    readln;

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

    auto place = new int[] (n+1);
    foreach (i, e; arr) { place[e] = i.to!int; }

    auto adjr = n.iota.map!(x => x+1).array;
    auto adjle = n.iota.map!(x => x-1).array;

    auto ans = new int[] (n);

    int now = 1;
    foreach_reverse (e; (n+1).iota.dropOne) {
        int idx = place[e];

        if (ans[idx] != 0) { continue; }

        debug { e.writeln; }

        ans[idx] = now;
        int idxr = adjr[idx], step = 1;
        while (step <= k && idxr < n) {
            ans[idxr] = now;
            idxr = adjr[idxr];
            ++step;
        }
        int idxle = adjle[idx];
        step = 1;
        while (step <= k && idxle >= 0) {
            ans[idxle] = now;
            idxle = adjle[idxle];
            ++step;
        }

        if (idxle >= 0) { adjr[idxle] = idxr; }
        if (idxr < n) { adjle[idxr] = idxle; }

        now = 3 - now;
    }

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

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;
	int k = read.to!int;

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

	foreach(i; 0 .. n - 1){
		nodes[i].next = nodes[i + 1];
		nodes[i + 1].prev = nodes[i];
	}

	nodes.sort!"a.value>b.value"();

	int t = 1;
	foreach(nd; nodes){
		if(nd.isDone) continue;
		else{
			nd.setTeam(t);
			Node prev = nd.takePrev(t, k);
			Node next = nd.takeNext(t, k);
			if(prev) prev.next = next;
			if(next) next.prev = prev;
		}
		t = 3 - t;
	}

	nodes.sort!"a.id<b.id"();

	nodes.map!(nd => nd.team.to!string).array.join("").writeln;
}

class Node{
	int id;
	int value;
	int team;
	bool isDone = 0;
	Node prev, next;
	this(int id, int value){
		this.id = id;
		this.value = value;
	}
	void setTeam(int t){
		this.isDone = 1;
		this.team = t;
	}
	Node takePrev(int t, int k){
		this.setTeam(t);
		if(k == 0) return this.prev;
		else if(this.prev) return this.prev.takePrev(t, k - 1);
		else return null;
	}
	Node takeNext(int t, int k){
		this.setTeam(t);
		if(k == 0) return this.next;
		else if(this.next) return this.next.takeNext(t, k - 1);
		else return null;
	}
}

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 s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;

    auto B = N.iota.map!(i => tuple(i, A[i])).array;
    B.sort!"a[1] > b[1]";

    auto L = N.iota.map!(i => i.to!int - 1).array;
    auto R = N.iota.map!(i => i.to!int + 1).array;

    auto ans = new int[](N);
    auto used = new bool[](N);

    for (int p = 0, t = 0; p < N; t ^= 1) {
        auto start = B[p][0];
        ans[start] = t + 1;
        used[start] = true;

        int new_l, new_r;

        for (int j = 0, i = L[start]; j < K && i != -1; ++j, i = L[i], new_l = i) {
            ans[i] = t + 1;
            used[i] = true;
        }
        for (int j = 0, i = R[start]; j < K && i != N; ++j, i = R[i], new_r = i) {
            ans[i] = t + 1;
            used[i] = true;
        }

        if (new_l != -1) {
            R[new_l] = new_r;
        }
        if (new_r != N) {
            L[new_r] = new_l;
        }

        while (p < N && used[B[p][0]]) ++p;
    }

    ans.map!(a => a.to!string).join("").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;
	int k = read.to!int;

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

	foreach(i; 0 .. n - 1){
		nodes[i].next = nodes[i + 1];
		nodes[i + 1].prev = nodes[i];
	}

	nodes.sort!"a.value>b.value"();

	int t = 1;
	foreach(nd; nodes){
		if(nd.isDone) continue;
		else{
			nd.setTeam(t);
			Node prev = nd.takePrev(t, k);
			Node next = nd.takeNext(t, k);
			if(prev) prev.next = next;
			if(next) next.prev = prev;
		}
		t = 3 - t;
	}

	nodes.sort!"a.id<b.id"();

	nodes.map!(nd => nd.team.to!string).array.join(" ").writeln;
}

class Node{
	int id;
	int value;
	int team;
	bool isDone = 0;
	Node prev, next;
	this(int id, int value){
		this.id = id;
		this.value = value;
	}
	void setTeam(int t){
		this.isDone = 1;
		this.team = t;
	}
	Node takePrev(int t, int k){
		this.setTeam(t);
		if(k == 0) return this.prev;
		else if(this.prev) return this.prev.takePrev(t, k - 1);
		else return null;
	}
	Node takeNext(int t, int k){
		this.setTeam(t);
		if(k == 0) return this.next;
		else if(this.next) return this.next.takeNext(t, k - 1);
		else return null;
	}
}