You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$); each character of $$$s$$$ is either 0 or 1.

Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$).

NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).

import std.algorithm : max, maxElement;
import std.container : Array;
import std.stdio : stdin, write, writeln;
import std.typecons : Tuple, tuple;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

        while (tokens.empty) {
            tokens = readln.split;
        }
        auto token = tokens.front;
        tokens.popFront;
        return token;
    }

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    string[] tokens;
}

int solve(string s) {
    int n = cast(int) s.length;
    int i = 0;
    while (i < n && s[i] == '0') {
        i++;
    }
    if (i == n) {
        return 0;
    }
    int j = n - 1;
    while (s[j] == '0') {
        j--;
    }
    int result = 0;
    for (; j > i; j--) {
        result += s[j] == '0';
    }
    return result;
}

void main() {
    IO io;
    int T = io.readInt;
    while (T--) {
        writeln(solve(io.readToken));
    }
}

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

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

void solve(){
	foreach(_; 0 .. rint){
		string s = readln.chomp;
		int a = -1, b = -1;
		foreach(int i, c; s){
			if(c == '1'){
				if(a < 0) a = i;
				b = i;
			}
		}
		if(a < -1) "0".writeln;
		else{
			int ans = 0;
			foreach(int i, c; s){
				if(a <= i && i <= b && c == '0') ans += 1;
			}
			ans.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 = 998244353;
//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 main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto s = RD!string;
		bool start;
		foreach (c; s)
		{
			if (c == '1')
				start = true;
			else if (start)
				++ans[ti];
		}
		foreach_reverse (c; s)
		{
			if (c == '1')
				start = false;
			else if (start)
				--ans[ti];
		}
	}

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


Boboniu has a directed graph with $$$n$$$ vertices and $$$m$$$ edges.The out-degree of each vertex is at most $$$k$$$.Each edge has an integer weight between $$$1$$$ and $$$m$$$. No two edges have equal weights.Boboniu likes to walk on the graph with some specific rules, which is represented by a tuple $$$(c_1,c_2,\ldots,c_k)$$$. If he now stands on a vertex $$$u$$$ with out-degree $$$i$$$, then he will go to the next vertex by the edge with the $$$c_i$$$-th $$$(1\le c_i\le i)$$$ smallest weight among all edges outgoing from $$$u$$$.Now Boboniu asks you to calculate the number of tuples $$$(c_1,c_2,\ldots,c_k)$$$ such that  $$$1\le c_i\le i$$$ for all $$$i$$$ ($$$1\le i\le k$$$).  Starting from any vertex $$$u$$$, it is possible to go back to $$$u$$$ in finite time by walking on the graph under the described rules.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2\le n\le 2\cdot 10^5$$$, $$$2\le m\le \min(2\cdot 10^5,n(n-1) )$$$, $$$1\le k\le 9$$$). Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$w$$$ $$$(1\le u,v\le n,u\ne v,1\le w\le m)$$$, denoting an edge from $$$u$$$ to $$$v$$$ with weight $$$w$$$. It is guaranteed that there are no self-loops or multiple edges and each vertex has at least one edge starting from itself. It is guaranteed that the out-degree of each vertex is at most $$$k$$$ and no two edges have equal weight.

Print one integer: the number of tuples.

NoteFor the first example, there are two tuples: $$$(1,1,3)$$$ and $$$(1,2,3)$$$. The blue edges in the picture denote the $$$c_i$$$-th smallest edges for each vertex, which Boboniu chooses to go through.  For the third example, there's only one tuple: $$$(1,2,2,2)$$$.  The out-degree of vertex $$$u$$$ means the number of edges outgoing from $$$u$$$.

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

void main ()
{
	int n, m, k;
	while (readf !(" %s %s %s") (n, m, k) > 0)
	{
		alias Edge = Tuple !(int, q{v}, int, q{w});
		auto adj = new Edge [] [n];
		foreach (j; 0..m)
		{
			int u, v, w;
			readf !(" %s %s %s") (u, v, w);
			u -= 1;
			v -= 1;
			adj[u] ~= Edge (v, w);
		}
		foreach (i; 0..n)
		{
			adj[i].schwartzSort !(q{a.w});
		}
		auto d = adj.map !(line => line.length.to !(int)).array;

		auto h = n.iota.map !(_ => uniform (1, long.max)).array;
		auto total = h.fold !(q{a ^ b});
		auto s = new long [] [] (k + 1, k + 1);
		foreach (i; 0..n)
		{
			foreach (j; 0..d[i])
			{
				s[d[i]][j] ^= h[adj[i][j].v];
			}
		}

		int res = 0;

		void recur (long cur, int pos)
		{
			if (pos > k)
			{
				res += (cur == total);
				return;
			}
			foreach (j; 0..pos)
			{
				recur (cur ^ s[pos][j], pos + 1);
			}
		}

		recur (0L, 1);
		writeln (res);
	}
}


Nastia has $$$2$$$ positive integers $$$A$$$ and $$$B$$$. She defines that:  The integer is good if it is divisible by $$$A \cdot B$$$;  Otherwise, the integer is nearly good, if it is divisible by $$$A$$$. For example, if $$$A = 6$$$ and $$$B = 4$$$, the integers $$$24$$$ and $$$72$$$ are good, the integers $$$6$$$, $$$660$$$ and $$$12$$$ are nearly good, the integers $$$16$$$, $$$7$$$ are neither good nor nearly good.Find $$$3$$$ different positive integers $$$x$$$, $$$y$$$, and $$$z$$$ such that exactly one of them is good and the other $$$2$$$ are nearly good, and $$$x + y = z$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. The first line of each test case contains two integers $$$A$$$ and $$$B$$$ ($$$1 \le A \le 10^6$$$, $$$1 \le B \le 10^6$$$) — numbers that Nastia has.

For each test case print:    "YES" and $$$3$$$ different positive integers $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1 \le x, y, z \le 10^{18}$$$) such that exactly one of them is good and the other $$$2$$$ are nearly good, and $$$x + y = z$$$.  "NO" if no answer exists.  YES NO If there are multiple answers, print any.

NoteIn the first test case: $$$60$$$ — good number; $$$10$$$ and $$$50$$$ — nearly good numbers.In the second test case: $$$208$$$ — good number; $$$169$$$ and $$$39$$$ — nearly good numbers.In the third test case: $$$154$$$ — good number; $$$28$$$ and $$$182$$$ — nearly good numbers.

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!long).array;
        long a = arr[0];
        long b = arr[1];

        if(b == 1)
        {
            writeln("NO");
            continue OUTER;
        }
        writeln("YES");
        writeln(format("%d %d %d", a*b, a*b+a, a*b*2+a));
    }
}

import std.stdio, std.algorithm, std.conv, std.random;

bool is_good(long x, long A, long B)
{
  if (x < 1)
    return false;
  return x % (A * B) == 0;
}

bool is_nearly_good(long x, long A, long B)
{
  if (x < 1)
    return false;
  return (x % (A * B) != 0) && (x % A == 0);
}

void main()
{
  int t;
  readf!" %d"(t);
  while (t--) {
    long A, B;
    readf!" %d %d"(A, B);
    long x, y, z;
    bool done = false;
    foreach (c1 ; 1 .. 10) {
      if (done)
        break;
      foreach (c2 ; 1 .. 10) {
        if (done)
          break;
        x = A * (B * c1 + 1);
        y = A * (B * c2 - 1);
        z = A * B * (c1 + c2);
        if (x != y && is_good(z, A, B) && is_nearly_good(x, A, B) && is_nearly_good(y, A, B)) {
          writeln("YES\n" ~ [x, y, z].map!text.joiner(" ").text);
          done = true;
        }
      }
    }
    if (!done)
      writeln("NO");
  }
}

import std.stdio, std.algorithm, std.conv, std.random;

bool is_good(long x, long A, long B)
{
  if (x < 1)
    return false;
  return x % (A * B) == 0;
}

bool is_nearly_good(long x, long A, long B)
{
  if (x < 1)
    return false;
  return (x % (A * B) != 0) && (x % A == 0);
}

void main()
{
  int t;
  readf!" %d"(t);
  while (t--) {
    long A, B;
    readf!" %d %d"(A, B);
    long x, y, z;
    bool done = false;
    foreach (c1 ; 1 .. 10) {
      if (done)
        break;
      foreach (c2 ; 1 .. 10) {
        if (done)
          break;
        x = A * (B * c1 + 1);
        y = A * (B * c2 - 1);
        z = A * B * (c1 + c2);
        if (is_good(z, A, B) && is_nearly_good(x, A, B) && is_nearly_good(y, A, B)) {
          writeln("YES\n" ~ [x, y, z].map!text.joiner(" ").text);
          done = true;
        }
      }
    }
    if (!done)
      writeln("NO");
  }
}

// Vicfred
// https://codeforces.com/contest/1521/problem/A
// math
import std.stdio;

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

while(t--) {
    long A, B;
    readf("%s %s\n", &A, &B);

    if(B == 1L) {
        "NO".writeln;
        continue;
    }

    long x = A;
    long y = A*B;
    long z = A*(B + 1L);
    "YES".writeln;
    writefln("%s %s %s", x, y, z);
}
}

import std.stdio;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        long a, b;
        readf!"%d %d\n"(a, b);
        if (b == 1)
            writeln("NO");
        else
            writefln!"YES\n%d %d %d"(a, a * b, a * (b + 1));
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];
        int m = max(a,b);

        if(b == 1)
        {
            writeln("NO");
            continue OUTER;
        }
        writeln("YES");
        writeln(format("%d %d %d", a*b, a*b+a, a*b*2+a));
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];
        int m = max(a,b);

        if(b == 1) writeln("NO");
        writeln("YES");
        writeln(format("%d %d %d", a*b, a*b+a, a*b*2+a));
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];

        for(int i = a; i <= a*b*a; i+= a)
        {
            for(int j = a; j <= a*b*a; j+=a)
            {
                if(i == j) continue;
                int z = i+j;
                if(z % (a*b) == 0 || ((i % (a*b) == 0) != (j % (a*b) == 0)))
                {
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
            }
        }

        writeln("NO");
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];

        for(int i = a; i <= a*b*a; i+= a)
        {
            for(int j = a; j <= a*b*a; j+=a)
            {
                if(i == j || (i+j)%a != 0) continue;
                int z = i+j;
                if(((i % (a*b) == 0) != (j % (a*b) == 0)) != (z % (a*b) == 0))
                {
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
            }
        }

        writeln("NO");
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];

        for(int i = a; i <= int.max; i+= a)
        {
            for(int j = a; j <= int.max; j+=a)
            {
                if(i == j) continue;
                int z = i+j;
                if(((i % (a*b) == 0) != (j % (a*b) == 0)) != (z % (a*b) == 0))
                {
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
            }
        }

        writeln("NO");
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];

        for(int i = a; i <= a*b*a; i+= a)
        {
            for(int j = a; j <= a*b*a; j+=a)
            {
                if(i == j) continue;
                int z = i+j;
                if((i % (a*b) == 0) != (j % (a*b) == 0))
                {
                    if(z % (a*b) == 0) continue;
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
                else if(z % (a*b) == 0)
                {
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
            }
        }

        writeln("NO");
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.container.binaryheap;
import std.typecons;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        int a = arr[0];
        int b = arr[1];

        for(int i = a; i <= a*b*a; i+= a)
        {
            for(int j = a; j <= a*b*a; j+=a)
            {
                if(i == j) continue;

                if(i % (a*b) == 0 || j % (a*b) == 0)
                {
                    int z = i+j;
                    writeln("YES");
                    writeln(format("%d %d %d", i, j, z));
                    continue OUTER;
                }
                else
                {
                    if((i+j) % (a*b) == 0)
                    {
                        writeln("YES");
                        writeln(format("%d %d %d", i, j, i+j));
                        continue OUTER;
                    }
                }
            }
        }

        writeln("NO");
    }
}

// Vicfred
// https://codeforces.com/contest/1521/problem/A
// math
import std.stdio;

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

while(t--) {
    long A, B;
    readf("%s %s\n", &A, &B);

    if(B == 1L) {
        "NO".writeln;
        continue;
    }

    long x = A;
    long y = A*B;
    long z = A*(B + 1L);
    writefln("%s %s %s", x, y, z);
}
}


You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step:  either you can select any blue element and decrease its value by $$$1$$$;  or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \cdot 10^5$$$.

Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).

NoteIn the first test case of the example, the following sequence of moves can be performed:  choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$;  choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$;  choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$;  choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES.

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

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

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

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        int[] up = new int[0];
        int[] down = new int[0];
        for (int i = 0; i < n; i++) {
            char c;
            if (i == n-1) readf!"%c\n"(c);
            else readf!"%c"(c);

            if (c == 'R') up ~= [a[i]];
            else down ~= [a[i]];
        }

        up.sort!("a > b");
        down.sort!("a < b");

        bool possible = true;
        int upper = n;
        for (int i = 0; i < up.length; i++) {
            if (up[i] > upper) possible = false;
            upper --;
        }

        int lower = 1;
        for (int i = 0; i < down.length; i++) {
            if (down[i] < lower) possible = false;
            lower ++;
        }

        if (possible) writeln("YES");
        else writeln("NO");

    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto t = readln.strip;
		auto lo = n.iota.filter !(i => t[i] == 'B')
		    .map !(i => a[i]).array;
		auto hi = n.iota.filter !(i => t[i] == 'R')
		    .map !(i => a[i]).array;
		sort !(q{a < b}) (lo);
		sort !(q{a > b}) (hi);
		auto okLo = lo.length.iota.all !((int i) => lo[i] >= i + 1);
		auto okHi = hi.length.iota.all !((int i) => hi[i] <= n - i);
		writeln (okLo && okHi ? "YES" : "NO");
	}
}

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

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

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

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        int[] up = new int[0];
        int[] down = new int[0];
        for (int i = 0; i < n; i++) {
            char c;
            if (i == n-1) readf!"%c\n"(c);
            else readf!"%c"(c);

            if (c == 'R') up ~= [a[i]];
            else down ~= [a[i]];
        }

        up.sort!("a > b");
        down.sort!("a < b");

        bool possible = true;
        int upper = n;
        for (int i = 0; i < up.length; i++) {
            if (up[i] > upper) possible = false;
            upper --;
        }

        int lower = 0;
        for (int i = 0; i < down.length; i++) {
            if (down[i] < lower) possible = false;
            lower ++;
        }

        if (possible) writeln("YES");
        else writeln("NO");

    }
}


Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows:   $$$f(0) = a$$$;  $$$f(1) = b$$$;  $$$f(n) = f(n-1) \oplus f(n-2)$$$ when $$$n > 1$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.

The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \le a, b, n \le 10^9$$$) respectively.

NoteIn the first example, $$$f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7$$$.

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

void solve(){
	int t = read.to!int;
	foreach(_; 0 .. t){
		long a = read.to!long;
		long b = read.to!long;
		long n = read.to!long;

		if(n % 3 == 0) a.writeln;
		if(n % 3 == 1) b.writeln;
		if(n % 3 == 2) (a ^b).writeln;
	}

}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const A = readLong();
        const B = readLong();
        const N = readLong();
        long ans;
        switch (N % 3) {
          case 0: ans = A; break;
          case 1: ans = B; break;
          case 2: ans = A ^ B; break;
          default: assert(false);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto n = RD;
		long x, y;
		if (n == 0)
		{
			x = a;
		}
		else if ((n-1) % 3 != 0)
		{
			x = a;
		}
		if (n % 3 != 0)
		{
			y = b;
		}
		ans[i] = x^y;
	}

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


You are the head of a large enterprise. $$$n$$$ people work at you, and $$$n$$$ is odd (i. e. $$$n$$$ is not divisible by $$$2$$$).You have to distribute salaries to your employees. Initially, you have $$$s$$$ dollars for it, and the $$$i$$$-th employee should get a salary from $$$l_i$$$ to $$$r_i$$$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:  the median of the sequence $$$[5, 1, 10, 17, 6]$$$ is $$$6$$$,  the median of the sequence $$$[1, 2, 1]$$$ is $$$1$$$. It is guaranteed that you have enough money to pay the minimum salary, i.e $$$l_1 + l_2 + \dots + l_n \le s$$$.Note that you don't have to spend all your $$$s$$$ dollars on salaries.You have to answer $$$t$$$ test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$) — the number of test cases. The first line of each query contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n < 2 \cdot 10^5$$$, $$$1 \le s \le 2 \cdot 10^{14}$$$) — the number of employees and the amount of money you have. The value $$$n$$$ is not divisible by $$$2$$$. The following $$$n$$$ lines of each query contain the information about employees. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 10^9$$$). It is guaranteed that the sum of all $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $$$\sum\limits_{i=1}^{n} l_i \le s$$$.

For each test case print one integer — the maximum median salary that you can obtain.

NoteIn the first test case, you can distribute salaries as follows: $$$sal_1 = 12, sal_2 = 2, sal_3 = 11$$$ ($$$sal_i$$$ is the salary of the $$$i$$$-th employee). Then the median salary is $$$11$$$.In the second test case, you have to pay $$$1337$$$ dollars to the only employee.In the third test case, you can distribute salaries as follows: $$$sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$$$. Then the median salary is $$$6$$$.

import std.stdio;
import std.algorithm;
import std.typecons : tuple, Tuple;
import std.container.rbtree;

int n;
long s;
int[] l;
int[] r;

bool can(long m) {
	int cnt = 0;
	int other = n;
	long sum = 0;
	for(int i = 0; i < n; ++i) {
		if (m < l[i]) {
			++cnt;
			sum += l[i];
			--other;
		} else if (r[i] < m) {
			sum += l[i];
			--other;
		}
	}
	if (cnt + other < (n + 1) / 2) {
		return false;
	}
	int[] ls = new int[other];
	int ptr = 0;
	for(int i = 0; i < n; ++i) {
		if (l[i] <= m && m <= r[i]) {
			ls[ptr] = l[i];
			++ptr;
		}
	}
	sort(ls);
	for(int i = other - 1; i >= 0; --i) {
		if (i >= other - ((n+1)/2 - cnt)) {
			sum += m;
		} else {
			sum += ls[i];
		}
	}
	return sum <= s;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t --> 0) {
		scanf("%d%lld", &n, &s);
		l = new int[n];
		r = new int[n];
		for(int i = 0; i < n; ++i) {
			scanf("%d%d", &l[i], &r[i]);
		}
		long tl = 0, tr = s + 1;
		while (tl+1 < tr) {
			long tm = (tl+tr) / 2;
			if (can(tm)) {
				tl = tm;
			} else {
				tr = tm;
			}
		}
		printf("%lld\n", tl);
	}

	return 0;
}

import std.stdio;
import std.algorithm;
import std.typecons : tuple, Tuple;
import std.container.rbtree;

int n;
long s;
int[] l;
int[] r;

bool can(long m) {
	int cnt = 0;
	long sum = 0;
	int other = n;
	for(int i = 0; i < n; ++i) {
		if (m < l[i]) {
			++cnt;
			sum += l[i];
			--other;
		} else if (r[i] < m) {
			sum += l[i];
			--other;
		}
	}
	if (cnt + other < (n + 1) / 2) {
		return false;
	}
	int[] ls = new int[other];
	int ptr = 0;
	for(int i = 0; i < n; ++i) {
		if (l[i] <= m && m <= r[i]) {
			ls[ptr] = l[i];
			++ptr;
		}
	}
	sort(ls);
	for(int i = other - 1; i >= 0; --i) {
		if (i >= other - cnt) {
			sum += m;
		} else sum += ls[i];
	}
	return sum <= s;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t --> 0) {
		scanf("%d%lld", &n, &s);
		l = new int[n];
		r = new int[n];
		for(int i = 0; i < n; ++i) {
			scanf("%d%d", &l[i], &r[i]);
		}
		long tl = 0, tr = s + 1;
		while (tl+1 < tr) {
			long tm = (tl+tr) / 2;
			if (can(tm)) {
				tl = tm;
			} else {
				tr = tm;
			}
		}
		printf("%lld\n", tl);
	}

	return 0;
}


You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi — the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum .

The only line contains integer n (1 ≤ n ≤ 5·105).

Print 2n integers — the permuted array a that minimizes the value of the sum s.

import std.stdio;
import std.algorithm;

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

	int[] res = new int[2 * n];
	fill(res, n);
	for (int i = 1, j = 0, k = n - 1; i < n; i += 2, j++, k--)
		res[j] = res[k] = i;
	for (int i = 2, j = n, k = 2 * n - 2; i < n; i += 2, j++, k--)
		res[j] = res[k] = i;

	for (int i = 0; i < res.length; i++)
		printf("%d%c", res[i], i == res.length - 1 ? '\n' : ' ');
}

import std.stdio;
import std.algorithm;

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

	int[] res = new int[2 * n];
	fill(res, n);
	for (int i = 1, j = 0, k = n - 1; i < n; i += 2, j++, k--)
		res[j] = res[k] = i;
	for (int i = 2, j = n, k = 2 * n - 2; i < n; i += 2, j++, k--)
		res[j] = res[k] = i;

	writefln("%(%d %)", res);
}


You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric:Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.

The first line contains string s (1 ≤ |s| ≤ 106). The second line contains string t (1 ≤ |t| ≤ 106).

Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them.

NoteIn the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.In the third example there are no '?' characters and the suitability of the string is 0.

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

void main() {
    auto S = readln.chomp;
    auto T = readln.chomp;

    long hatena = 0;
    foreach (s; S) if (s == '?') hatena += 1;
    auto cnt2 = new long[](26);
    foreach (s; S) if (s != '?') cnt2[s - 'a'] += 1;

    auto cnt = new long[](26);
    foreach (t; T) cnt[t - 'a'] += 1;

    long hi = 10^^7;
    long lo = 0;
    auto hoge = new long[](26);
    long h;

    while (hi - lo > 1) {
        long mid = (hi + lo) / 2;
        foreach (i; 0..26) hoge[i] = cnt[i] * mid - cnt2[i];
        h = hatena;
        foreach (i; 0..26) h -= max(0, hoge[i]);
        if (h >= 0) lo = mid;
        else hi = mid;
    }

    foreach (i; 0..26) cnt[i] = cnt[i] * lo - cnt2[i];
    int p = 0;
    while (p < 26 && cnt[p] <= 0) p += 1;
    auto ans = new char[](S.length);

    foreach (i; 0..S.length) {
        if (S[i] == '?' && p < 26) {
            ans[i] = (p.to!char + 'a').to!char;
            cnt[p] -= 1;
            while (p < 26 && cnt[p] <= 0) p += 1;
        } else if (S[i] == '?' && p >= 26){
            ans[i] = 'z';
        } else {
            ans[i] = S[i];
        }
    }

    ans.writeln;
}

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

void main(){
	auto s=readln.strip, t=readln.strip;
	int w=0;
	int[26] f,g;
	foreach(c;s) ++('a'<=c&&c<='z'?f[c-'a']:w);
	foreach(c;t) ++g[c-'a'];
	auto ns=s.dup;
	int i=0;
	while(f[].all!(c=>c>=0)){
		f[]-=g[];
		foreach(k,ref c;f){
			if(c>=0) continue;
			int d=min(-c,w);
			c+=d,w-=d;
			foreach(j;0..d){
				while(ns[i]!='?') i++;
				ns[i]=cast(char)('a'+k);
			}
		}
	}
	writeln(ns.to!string);
}

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 S = readln.chomp;
    auto T = readln.chomp;

    long hatena = 0;
    foreach (s; S) if (s == '?') hatena += 1;
    auto cnt2 = new long[](26);
    foreach (s; S) if (s != '?') cnt2[s - 'a'] += 1;

    auto cnt = new long[](26);
    foreach (t; T) cnt[t - 'a'] += 1;

    int hi = 10^^7;
    int lo = 0;
    auto hoge = new long[](26);
    long h;

    while (hi - lo > 1) {
        int mid = (hi + lo) / 2;
        foreach (i; 0..26) hoge[i] = cnt[i] * mid - cnt2[i];
        h = hatena;
        foreach (i; 0..26) h -= max(0, hoge[i]);
        if (h >= 0) lo = mid;
        else hi = mid;
    }

    foreach (i; 0..26) cnt[i] = cnt[i] * lo - cnt2[i];
    int p = 0;
    while (p < 26 && cnt[p] <= 0) p += 1;
    auto ans = new char[](S.length);

    foreach (i; 0..S.length) {
        if (S[i] == '?' && p < 26) {
            ans[i] = (p.to!char + 'a').to!char;
            cnt[i] -= 1;
            while (p < 26 && cnt[p] <= 0) p += 1;
        } else if (S[i] == '?' && p >= 26){
            ans[i] = 'z';
        } else {
            ans[i] = S[i];
        }
    }

    ans.writeln;
}


String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length  are (k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, "abaaba" has degree equals to 3.You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes.

The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5·106. The string is case-sensitive.

Output the only number — the sum of the polindrome degrees of all the string's prefixes.

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

	}*/
	s.length=5000000;
	debug writeln(cast(int)('C'));
	readln(s);
	s=s[0..$-1];
	auto h1=new int[s.length];
	auto st=new int[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=(st[i-1]*rb)%mod;
	}
	debug writeln(st);
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		if(ans<0)ans+=mod;
		ans*=st[s.length-1-r];
		if(ans>=mod)ans%=mod;
		return ans;
	}
	auto ans=new int[s.length];
	debug writeln(h1,'\n',h2);
	debug writeln(hsub(0)==h1[0]);
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	debug writeln(ans);
	writeln(ans.sum);
}

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

	}*/
	debug writeln(cast(int)('C'));
	auto s=readln.strip;
	auto h1=new int[s.length];
	auto st=new int[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=(st[i-1]*rb)%mod;
	}
	debug writeln(st);
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		if(ans<0)ans+=mod;
		ans*=st[s.length-1-r];
		if(ans>=mod)ans%=mod;
		return ans;
	}
	auto ans=new int[s.length];
	debug writeln(h1,'\n',h2);
	debug writeln(hsub(0)==h1[0]);
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	debug writeln(ans);
	writeln(ans.sum);
}

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

void main()
{
	/*int n;
	loop:while(read(&n))
	{

	}*/
	auto s=readln.strip;
	auto h1=new long[s.length];
	auto st=new long[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+mulmod(s[i],p,hashmod))%hashmod;
		p=mulmod(p,base,hashmod);
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+mulmod(s[i],p,hashmod))%hashmod;
		p=mulmod(p,base,hashmod);
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=mulmod(st[i-1],rb);
	}
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		ans=mulmod(ans,st[s.length-1-r]);
		ans=(ans+hashmod)%hashmod;
		return ans;
	}
	auto ans=new long[s.length];
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	writeln(ans.sum);
}


The problem statement looms below, filling you with determination.Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't.You are given a grid $$$a$$$ of dimensions $$$n \times m$$$ , i. e. a grid with $$$n$$$ rows and $$$m$$$ columns. You need to answer $$$q$$$ queries ($$$1 \leq q \leq 2 \cdot 10^5$$$). Each query gives two integers $$$x_1, x_2$$$ ($$$1 \leq x_1 \leq x_2 \leq m$$$) and asks whether the subgrid of $$$a$$$ consisting of the columns $$$x_1, x_1 + 1, \ldots, x_2 - 1, x_2$$$ is determinable.

The first line contains two integers $$$n, m$$$ ($$$1 \leq n, m \leq 10^6$$$, $$$nm \leq 10^6$$$)  — the dimensions of the grid $$$a$$$. $$$n$$$ lines follow. The $$$y$$$-th line contains $$$m$$$ characters, the $$$x$$$-th of which is 'X' if the cell on the intersection of the the $$$y$$$-th row and $$$x$$$-th column is filled and "." if it is empty. The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$)  — the number of queries. $$$q$$$ lines follow. Each line contains two integers $$$x_1$$$ and $$$x_2$$$ ($$$1 \leq x_1 \leq x_2 \leq m$$$), representing a query asking whether the subgrid of $$$a$$$ containing the columns $$$x_1, x_1 + 1, \ldots, x_2 - 1, x_2$$$ is determinable.

For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted).

NoteFor each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise.For the first query: ..X EEN... EEE... EEE... EEEFor the second query: X N. E. E. ENote that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid.For the third query: XX NNX. NNX. NNX. NNThis subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XXXXXXXXFor the fourth query: X N. E. E. EFor the fifth query: ..XXX EENNN...X. EEENN...X. EEENN...X. EEENNThis query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX...XX...XX...XX

immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	pair[m-1] = m-1;
	foreach_reverse(i; 0 .. m-1)
	{
		if (compatible[i] && !hasUnknowable[i+1])
		{
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

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

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

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

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

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

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 n, m;
    readf!"%s %s"(n, m);
    readln;

    string[] arr;
    foreach (i; 0 .. n) {
        arr ~= readln.chomp;
    }

    int[] cols;
    outer: foreach (j; 0 .. m-1) {
        foreach (i; 1 .. n) {
            if (arr[i][j] == 'X' && arr[i-1][j+1] == 'X') {
                cols ~= j+1;
                continue outer;
            }
        }
    }

    auto sorted = assumeSorted(cols);

    debug { sorted.writeln; }

    int q;
    readf!"%s"(q);
    readln;
    while (q--) {
        int x1, x2;
        readf!"%s %s"(x1, x2);
        readln;

        auto bigger = sorted.upperBound(x1-1);
        auto ambiguous = x1 < x2 && bigger.any() && bigger.front < x2;

        debug { writeln(bigger, ' ', x1, ' ', x2); }

        writeln(ambiguous ? "NO" : "YES");
    }
}

immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	foreach_reverse(i; 0 .. m)
	{
		if (compatible[i])
		{
//			while (j > i && hasUnknowable[j]) j--;
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

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

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

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

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

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

immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	foreach_reverse(i; 0 .. m)
	{
		if (compatible[i])
		{
			while (j > i && hasUnknowable[j]) j--;
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

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

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

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

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

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


Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?

The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.

Print the total minimum number of moves.

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;
long[] A;

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

		long[] ASum = new long[N + 1];
		foreach (i; 0 .. N) {
			ASum[i + 1] = ASum[i] + A[i];
		}
		assert(ASum[N] % N == 0);
		const target = ASum[N] / N;
		long ans;
		foreach (i; 1 .. N) {
			ans += abs(ASum[i] - target * i);
		}
		writeln(ans);
	}
	} catch (EOFException) {}
}


Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \le n, g, b \le 10^9$$$) — the length of the highway and the number of good and bad days respectively.

Print $$$T$$$ integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.

NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

long ceilDiv(long a, long b) {
  return a/b + (a%b > 0);
}

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n+1)/2;
    m = ceilDiv(k, g);
    writeln((m-1)*b + k + max(n - k - b*(m-1), 0));
  }
}

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n + 1) >> 1;
    m = (k/g + (k%g > 0) - 1)*b + k;
    writeln(max(n, m));
  }
}

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n + 1) >> 1;
    m = (k/g + (k%g > 0) - 1)*b + k;
    writeln(m + max(n - m, 0));
  }
}

import std.algorithm : max, maxElement;
import std.container : Array;
import std.stdio : stdin, write, writeln;
import std.typecons : Tuple, tuple;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

        while (tokens.empty) {
            tokens = readln.split;
        }
        auto token = tokens.front;
        tokens.popFront;
        return token;
    }

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    string[] tokens;
}

void main() {
    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int g = io.readInt;
        int b = io.readInt;
        int req = n + 1 >> 1;
        int k = (req - 1) / g;
        writeln(max(cast(long) k * (g + b) + (req - k * g), n));
    }
}

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

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

void solve(){
	foreach(_; 0 .. rint){
		long n = rlong, g = rlong, b = rlong;

		long n1 = (n + 1) / 2; // minimum good pavement
		long n2 = n - n1; // maximum bad pavement

		long ans;
		long x = n2 / b;
		if((x + 1) * g >= n1){
			ans = n;
		}
		else{
			long n3 = n1 - (x + 1) * g;
			if(n3 % g == 0) ans = x * (g + b) + g + (n3 / g) * (g + b);
			else ans = x * (g + b) + g + (n3 / g) * (g + b) + b + (n3 % g);
		}

		ans.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 = 998244353;
//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 main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto g = RD!int;
		auto b = RD!int;

		long need = (n+1) / 2;
		long cnt = need / g * (g+b) - b;
		long r = need % g;
		if (r != 0)
			cnt += b + r;
		ans[ti] = max(n, cnt);
	}

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


Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .

The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105). The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges of the tree. It's guaranteed that the given graph is a tree.

Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).

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

void main() {
    auto N = readln.chomp.to!int;
    auto edges = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        edges[s[0] - 1] ~= s[1] - 1;
        edges[s[1] - 1] ~= s[0] - 1;
    }

    auto depth = new int[](2);

    void dfs1(int n, int p, int d) {
        depth[d % 2] += 1;
        foreach (m; edges[n]) if (m != p) dfs1(m, n, d + 1);
    }

    long ans = 0;

    void dfs2(int n, int p, int d) {
        ans += depth[1 - d % 2] - edges[n].length.to!int;
        foreach (m; edges[n]) if (m != p) dfs2(m, n, d + 1);
    }

    dfs1(0, -1, 0);
    dfs2(0, -1, 0);
    writeln(ans / 2);
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto edges = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        edges[s[0] - 1] ~= s[1] - 1;
        edges[s[1] - 1] ~= s[0] - 1;
    }

    auto depth = new int[](2);

    void dfs1(int n, int p, int d) {
        depth[d % 2] += 1;
        foreach (m; edges[n]) if (m != p) dfs1(m, n, d + 1);
    }

    long ans = 0;

    void dfs2(int n, int p, int d) {
        ans += depth[1 - d % 2] - edges[n].length.to!int;
        writeln(n, ans);
        foreach (m; edges[n]) if (m != p) dfs2(m, n, d + 1);
    }

    dfs1(0, -1, 0);
    dfs2(0, -1, 0);
    writeln(ans / 2);
}


Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.The park can be represented as a connected graph with $$$n$$$ nodes and $$$m$$$ bidirectional edges. Initially Bob is at the node $$$1$$$ and he records $$$1$$$ on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes $$$a_1, a_2, \ldots, a_n$$$ is recorded.Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.A sequence $$$x$$$ is lexicographically smaller than a sequence $$$y$$$ if and only if one of the following holds:   $$$x$$$ is a prefix of $$$y$$$, but $$$x \ne y$$$ (this is impossible in this problem as all considered sequences have the same length);  in the first position where $$$x$$$ and $$$y$$$ differ, the sequence $$$x$$$ has a smaller element than the corresponding element in $$$y$$$.

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^5$$$), denoting the number of nodes and edges, respectively. The following $$$m$$$ lines describe the bidirectional edges in the graph. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), representing the nodes the $$$i$$$-th edge connects. Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.

Output a line containing the lexicographically smallest sequence $$$a_1, a_2, \ldots, a_n$$$ Bob can record.

NoteIn the first sample, Bob's optimal wandering path could be $$$1 \rightarrow 2 \rightarrow 1 \rightarrow 3$$$. Therefore, Bob will obtain the sequence $$$\{1, 2, 3\}$$$, which is the lexicographically smallest one.In the second sample, Bob's optimal wandering path could be $$$1 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 5$$$. Therefore, Bob will obtain the sequence $$$\{1, 4, 3, 2, 5\}$$$, which is the lexicographically smallest one.

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;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);

    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    auto ans = new int[](N);
    int p = 0;

    auto used = new bool[](N);
    auto pq = new BinaryHeap!(Array!int, "a > b");
    pq.insert(0);

    while (!pq.empty) {
        auto n = pq.front;
        pq.popFront;
        if (used[n]) continue;
        used[n] = true;
        ans[p++] = n + 1;
        foreach (m; G[n]) {
            if (used[m]) continue;
            pq.insert(m);
        }
    }

    ans.map!(to!string).join(" ").writeln;
}

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

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    int n, m;
    scan(n, m);

    auto adj = new int[][](n, 0);
    foreach (i ; 0 .. m) {
        int u, v;
        scan(u, v);
        u--, v--;
        adj[u] ~= v;
        adj[v] ~= u;
    }

    auto bh = new BinaryHeap!(Array!int, "a > b")();
    bh.insert(0);

    auto visited = new bool[](n);
    visited[0] = true;
    auto ans = new int[](n);

    foreach (i ; 0 .. n) {
        auto v = bh.front; bh.popFront();
        ans[i] = v + 1;

        foreach (u ; adj[v]) {
            if (visited[u]) continue;
            visited[u] = true;
            bh.insert(u);
        }
    }

    writefln("%(%s %)", ans);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}


You are given an array $$$A$$$, consisting of $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$, and an array $$$B$$$, consisting of $$$m$$$ positive integers $$$b_1, b_2, \dots, b_m$$$. Choose some element $$$a$$$ of $$$A$$$ and some element $$$b$$$ of $$$B$$$ such that $$$a+b$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. For example, if $$$A = [2, 1, 7]$$$ and $$$B = [1, 3, 4]$$$, we can choose $$$1$$$ from $$$A$$$ and $$$4$$$ from $$$B$$$, as number $$$5 = 1 + 4$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. However, we can't choose $$$2$$$ from $$$A$$$ and $$$1$$$ from $$$B$$$, as $$$3 = 2 + 1$$$ belongs to $$$B$$$.It can be shown that such a pair exists. If there are multiple answers, print any.Choose and print any such two numbers.

The first line contains one integer $$$n$$$ ($$$1\le n \le 100$$$) — the number of elements of $$$A$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 200$$$) — the elements of $$$A$$$. The third line contains one integer $$$m$$$ ($$$1\le m \le 100$$$) — the number of elements of $$$B$$$. The fourth line contains $$$m$$$ different integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le 200$$$) — the elements of $$$B$$$. It can be shown that the answer always exists.

Output two numbers $$$a$$$ and $$$b$$$ such that $$$a$$$ belongs to $$$A$$$, $$$b$$$ belongs to $$$B$$$, but $$$a+b$$$ doesn't belong to nor $$$A$$$ neither $$$B$$$. If there are multiple answers, print any.

NoteIn the first example, we can choose $$$20$$$ from array $$$[20]$$$ and $$$20$$$ from array $$$[10, 20]$$$. Number $$$40 = 20 + 20$$$ doesn't belong to any of those arrays. However, it is possible to choose $$$10$$$ from the second array too.In the second example, we can choose $$$3$$$ from array $$$[3, 2, 2]$$$ and $$$1$$$ from array $$$[1, 5, 7, 7, 9]$$$. Number $$$4 = 3 + 1$$$ doesn't belong to any of those arrays.In the third example, we can choose $$$1$$$ from array $$$[1, 3, 5, 7]$$$ and $$$1$$$ from array $$$[7, 5, 3, 1]$$$. Number $$$2 = 1 + 1$$$ doesn't belong to any of those arrays.

import std.stdio;
int main()
{
	int a=0;
	int b=0;
	readf(" %d", &a);
	int[] m = new int[a];
	for (int i=0; i<a; i++)
	{
		readf(" %d", &m[i]);
	}
	readf(" %d", &b);
	int[] n = new int[b];
	for (int i=0; i<b; i++)
	{
		readf(" %d", &n[i]);
	}
	for (int i=0; i<a; i++)
	{
		for (int j=0; j<b; j++)
		{
			int r=m[i]+n[j];
			int count=0;
			for (int k=0; k<a; k++)
			{
				if (r==m[k])
				{
					count=count+1;
				}
			}
			for (int k=0; k<b; k++)
			{
				if (r==n[k])
				{
					count=count+1;
				}
			}
			if (count==0)
			{
				writeln(m[i]);
				writeln(n[j]);
				return 0;
			}
		}
	}
	return 0;
}