Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.You are given a few files that Petya has managed to create. The path to each file looks as follows:diskName:\folder1\folder2\...\ foldern\fileName  diskName is single capital letter from the set {C,D,E,F,G}. folder1, ..., foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1) fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9. It is also known that there is no file whose path looks like diskName:\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.

Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once. There is at least one line in the input data.

Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.

C:
\
folder1
\
folder2
\
folder3
\
file1.txt
C:
\
folder1
\
folder2
\
folder4
\
file1.txt
D:
\
folder1
\
file1.txt

C:
\
file
\
file
\
file
\
file
\
file.txt
C:
\
file
\
file
\
file
\
file2
\
file.txt

NoteIn the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder directly contains one folder, "folder2". The "folder2" folder contains two more folders — "folder3" and "folder4". Thus, the "folder1" folder on the "C" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of "folder1".In the third example we see that the names of some folders and some subfolders are identical. Consider the "file" folder, which lies directly on the "C" disk. That folder contains another "file" folder, which in turn contains another "file" folder, which contains two more folders, "file" and "file2". Thus, the "file" folder, which lies directly on the "C" disk, contains 4 subfolders.

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()
{
    class folder {
        string name;
        string[] files;
        folder[] folders;

        this (string n) {
            name = n;
        }
    }

    folder root = new folder("root");

    string s;
    while ((s = readln.chomp) !is null) {
        auto els = s.split("\\");
        auto start = els[0] ~ els[1];
        auto file = els[$-1];
        els = start ~ els[2 .. $-1];

        auto now = root;
        foreach (e; els) {
            if (! now.folders.any!(f => f.name == e)) {
                now.folders ~= new folder(e);
            }

            foreach (f; now.folders) {
                if (e == f.name) {
                    now = f;
                    break;
                }
            }
        }

        now.files ~= file;
    }

    debug {
        void wf(folder f, int t) {
            (' ').repeat(t).to!string.write;
            writeln(f.name);
            foreach (fs; f.folders) {
                wf(fs, t+2);
            }
        }

        wf(root, 0);
    }

    Tuple!(int, int) getCnt(folder dir) {
        int dirs = dir.folders.length.to!int;
        int files = dir.files.length.to!int;

        auto ans = tuple(dirs, files);
        foreach (d; dir.folders) {
            auto res = getCnt(d);
            ans[0] += res[0];
            ans[1] += res[1];
        }

        return ans;
    }

    int mxd = 0, mxf = 0;
    foreach (dir; root.folders) {
        auto res = getCnt(dir);
        mxd = max(mxd, res[0]);
        mxf = max(mxf, res[1]);
    }

    writeln(mxd, ' ', mxf);
}

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()
{
    class folder {
        string name;
        string[] files;
        folder[] folders;

        this (string n) {
            name = n;
        }
    }

    folder root = new folder("root");

    string s;
    while ((s = readln.chomp) !is null) {
        auto els = s.split("\\");
        auto start = els[0] ~ els[1];
        auto file = els[$-1];
        els = start ~ els[2 .. $-1];

        auto now = root;
        foreach (e; els) {
            if (! now.folders.any!(f => f.name == e)) {
                now.folders ~= new folder(e);
            }

            now = now.folders.find!((f, e) => f.name == e)(e)[0];
        }

        now.files ~= file;
    }

    debug {
        void wf(folder f, int t) {
            (' ').repeat(t).to!string.write;
            writeln(f.name);
            foreach (fs; f.folders) {
                wf(fs, t+2);
            }
        }

        wf(root, 0);
    }

    Tuple!(int, int) getCnt(folder dir) {
        int dirs = dir.folders.length.to!int;
        int files = dir.files.length.to!int;

        auto ans = tuple(dirs, files);
        foreach (d; dir.folders) {
            auto res = getCnt(d);
            ans[0] += res[0];
            ans[1] += res[1];
        }

        return ans;
    }

    int mxd = 0, mxf = 0;
    foreach (dir; root.folders) {
        auto res = getCnt(dir);
        mxd = max(mxd, res[0]);
        mxf = max(mxf, res[1]);
    }

    writeln(mxd, ' ', mxf);
}


The cinema theater hall in Sereja's city is n seats lined up in front of one large screen. There are slots for personal possessions to the left and to the right of each seat. Any two adjacent seats have exactly one shared slot. The figure below shows the arrangement of seats and slots for n = 4.  Today it's the premiere of a movie called "Dry Hard". The tickets for all the seats have been sold. There is a very strict controller at the entrance to the theater, so all n people will come into the hall one by one. As soon as a person enters a cinema hall, he immediately (momentarily) takes his seat and occupies all empty slots to the left and to the right from him. If there are no empty slots, the man gets really upset and leaves.People are not very constant, so it's hard to predict the order in which the viewers will enter the hall. For some seats, Sereja knows the number of the viewer (his number in the entering queue of the viewers) that will come and take this seat. For others, it can be any order. Being a programmer and a mathematician, Sereja wonders: how many ways are there for the people to enter the hall, such that nobody gets upset? As the number can be quite large, print it modulo 1000000007 (109 + 7).

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers, the i-th integer shows either the index of the person (index in the entering queue) with the ticket for the i-th seat or a 0, if his index is not known. It is guaranteed that all positive numbers in the second line are distinct. You can assume that the index of the person who enters the cinema hall is a unique integer from 1 to n. The person who has index 1 comes first to the hall, the person who has index 2 comes second and so on.

In a single line print the remainder after dividing the answer by number 1000000007 (109 + 7).

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

immutable int MOD = 1_000_000_007;
immutable int NA = -1;

long powmod (long a, int b)
{
	long res = 1;
	while (b)
	{
		if (b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

long choose (int n, int k)
{
	k = min (k, n - k);
	long res = 1;
	foreach (i; 0..k)
	{
		res = (res * (n - i)) % MOD;
		res = (res * powmod (i + 1, MOD - 2)) % MOD;
	}
	return res;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto q = new int [n];
		q[] = NA;
		auto p = new int [n];
		foreach (i, ref x; p)
		{
			readf (" %s", &x);
			x--;
			if (x != NA)
			{
				q[x] = i;
			}
		}

		bool check_empty (int lo, int hi)
		{
			foreach (i; lo..hi + 1)
			{
				if (p[i] != NA)
				{
					return false;
				}
			}
			return true;
		}

		long solve (int lo, int hi, int cur)
		{
			void adjust_borders ()
			{
				if (q[cur] == lo - 1)
				{
					lo--;
				}
				else if (q[cur] == hi + 1)
				{
					hi++;
				}
				else
				{
					enforce (false);
				}
			}

			if (lo < 0 || hi >= n)
			{
				return 0;
			}
			if (!check_empty (lo, hi))
			{
				return 0;
			}

			long res = 1;
			foreach (i; 1..cur)
			{
				res <<= 1;
				if (res >= MOD)
				{
					res -= MOD;
				}
			}
			adjust_borders ();
			debug {writeln ("res = ", res);}

			while (true)
			{
				int prev = cur;
				do
				{
					cur++;
				}
				while (cur < n && q[cur] == NA);
				debug {writeln (prev + 1, ' ', cur + 1, ' ', lo, ' ', hi);}
				if (cur == n)
				{
					int total = lo + (n - 1 - hi);
					res = (res * choose (total, lo)) % MOD;
					break;
				}
				else
				{
					int total = cur - prev - 1;
					int nlo, nhi;
					if (q[cur] < lo)
					{
						nlo = q[cur] + 1;
						nhi = hi + (total - (lo - nlo));
					}
					else if (q[cur] > hi)
					{
						nhi = q[cur] - 1;
						nlo = lo - (total - (nhi - hi));
					}
					else
					{
						enforce (false);
					}
					debug {writeln (total, ' ', nlo, ' ', nhi);}
					if (nlo < 0 || nhi >= n)
					{
						return 0;
					}
					if (nlo > lo || nhi < hi)
					{
						return 0;
					}
					if (!check_empty (nlo, lo - 1))
					{
						return 0;
					}
					if (!check_empty (hi + 1, nhi))
					{
						return 0;
					}
					res = (res * choose (total, lo - nlo)) % MOD;
					lo = nlo;
					hi = nhi;
					adjust_borders ();
					debug {writeln ("res = ", res);}
				}
			}

			return res;
		}

		int cur = 0;
		while (cur < n && q[cur] == NA)
		{
			cur++;
		}
		long res = 0;
		if (cur == n)
		{
			res = 1;
			foreach (i; 1..n)
			{
				res <<= 1;
				if (res >= MOD)
				{
					res -= MOD;
				}
			}
		}
		else
		{
			res = solve (q[cur] - cur, q[cur] - 1, cur);
			if (cur > 0)
			{
				res += solve (q[cur] + 1, q[cur] + cur, cur);
				if (res >= MOD)
				{
					res -= MOD;
				}
			}
		}
		writeln (res);
	}
}


This is the easy version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix.For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$.Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$3n$$$ operations. It can be proved that it is always possible.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 1000$$$)  — the length of the binary strings. The next two lines contain two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$1000$$$.

For each test case, output an integer $$$k$$$ ($$$0\le k\le 3n$$$), followed by $$$k$$$ integers $$$p_1,\ldots,p_k$$$ ($$$1\le p_i\le n$$$). Here $$$k$$$ is the number of operations you use and $$$p_i$$$ is the length of the prefix you flip in the $$$i$$$-th operation.

NoteIn the first test case, we have $$$01\to 11\to 00\to 10$$$.In the second test case, we have $$$01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$$$.In the third test case, the strings are already the same. Another solution is to flip the prefix of length $$$2$$$, which will leave $$$a$$$ unchanged.

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 t;
    readf("%s", &t);
    readln;

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto a = readln.chomp;
        auto b = readln.chomp;

        int[] ans;
        int p = 0;
        foreach (op; 0 .. n) {
            int cur = (a[p] - '0') ^ (op & 1);

            debug { writeln(p, ' ', op, ' ', cur); }

            if (cur == (b[n - op - 1] - '0')) {
                ans ~= 1;
            }

            ans ~= n - op;
            p = n - p - 1 + (op & 1);
        }

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

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 s = readln.strip.map !(q{a == '1'}).array;
		auto t = readln.strip.map !(q{a == '1'}).array;

		int [] answer;
		int lo = 0;
		int hi = n - 1;
		int p = 0;
		foreach_reverse (i; 0..n)
		{
			auto cur = (p ? !s[hi] : s[lo]);
			if (cur == t[i])
			{
				answer ~= 1;
			}
			answer ~= i + 1;
			if (p)
			{
				hi -= 1;
			}
			else
			{
				lo += 1;
			}
			p ^= 1;
		}
		writefln !("%s\n%(%s %)") (answer.length, answer);
	}
}

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

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

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

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

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

        int[] ansA, ansB;
        for (int i = 0, j; i < N; i = j) {
          for (j = i; j < N && A[i] == A[j]; ++j) {}
          if (!(j == N && A[N - 1] == '0')) {
            ansA ~= j;
          }
        }
        for (int i = 0, j; i < N; i = j) {
          for (j = i; j < N && B[i] == B[j]; ++j) {}
          if (!(j == N && B[N - 1] == '0')) {
            ansB ~= j;
          }
        }

        int[] ans;
        foreach (j; ansA) {
          ans ~= j;
        }
        foreach_reverse (j; ansB) {
          ans ~= j;
        }

        write(ans.length);
        foreach (j; ans) {
          write(" ", j);
        }
        writeln;
      }
    }
  } 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.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

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

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

		foreach_reverse (i; 0..n)
		{
			if (a[i] == b[i]) continue;
			if (a[0] == b[i])
			{
				ans[ti] ~= 1;
				a[0] = a[i];
			}
			ans[ti] ~= i+1;
			int l, r = i;
			while (l <= r)
			{
				swap(a[l], a[r]);
				a[l] = cast(char)(((a[l]-'0')^1) + '0');
				if (l != r)
					a[r] = cast(char)(((a[r]-'0')^1) + '0');
				++l; --r;
			}
			continue;
		}
	}

	foreach (e; ans)
	{
		write(e.length);
		if (e.empty)
		{
			writeln;
			continue;
		}
		write(" ");
		e.map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}

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

size_t[] solve(string a, string b) {
	size_t[] result;
	foreach (i; 0 .. a.length) {
		if (a[i] != b[i]) {
			result.assumeSafeAppend ~= i + 1;
			result.assumeSafeAppend ~= 1;
			result.assumeSafeAppend ~= i + 1;
		}
	}
	return result;
}

void main() {
	long tt = readln().chomp.to!long;
	foreach (t; 0 .. tt) {
		readln();
		string a = readln().chomp;
		string b = readln().chomp;
		if (a == b) {
			writeln("0");
		}
		else {
			auto arr = solve(a, b);
			writeln(arr.length, " ", arr.map!(to!string).join(" "));
		}
	}
}

// 00A00B 00C00D
// b11a11 00C00D
// 0A00B1 00C00D


We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.

The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.

Output "Yes" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and "No" (without quotes) otherwise. You can print each letter in any case (upper or lower).

NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$.

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

bool isVowel(C)(C c) {
    return "aeiou".canFind(c);
}

void main() {
    string s = readln.strip;
    string t = readln.strip;

    bool[] cv_s = s.map!(c => c.isVowel).array;
    bool[] cv_t = t.map!(c => c.isVowel).array;

    if (cv_s == cv_t) {
        writeln("Yes");
    } else {
        writeln("No");
    }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

bool isv(ref char c){
    return c=='a'||c=='e'||c=='i'||c=='o'||c=='u';
}
void main(){
    char[] s=readln.strip.to!(char[]),
           t=readln.strip.to!(char[]);
    if(s.length!=t.length)
        writeln("No");
    else{
        foreach(i;0..s.length){
            if((!s[i].isv || !t[i].isv) && (s[i].isv || t[i].isv)){
                writeln("No");
                return;
            }
        }
        writeln("Yes");
    }
}


There is a bus stop near the university. The lessons are over, and n students come to the stop. The i-th student will appear at the bus stop at time ti (all ti's are distinct).We shall assume that the stop is located on the coordinate axis Ox, at point x = 0, and the bus goes along the ray Ox, that is, towards the positive direction of the coordinate axis, and back. The i-th student needs to get to the point with coordinate xi (xi > 0).The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to m passengers. At the moment when m students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (n-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance y in time y.Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1 + [k / 2] units of time to get off the bus, where k is the number of students who leave at this point. Expression [k / 2] denotes rounded down k / 2. As soon as the last student leaves the bus, the bus turns around and goes back to the point x = 0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated.If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students.Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off).

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of students and the number of passengers the bus can transport, correspondingly. Next n lines contain descriptions of the students, one per line. Each line contains a pair of integers ti, xi (1 ≤ ti ≤ 105, 1 ≤ xi ≤ 104). The lines are given in the order of strict increasing of ti. Values of xi can coincide.

Print n numbers w1, w2, ..., wn, wi — the moment of time when the i-th student got off the bus. Print the numbers on one line and separate them with single spaces.

NoteIn the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8.In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1 + [1 / 2] = 1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14 + 5 = 19. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1 + [4 / 2] = 3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time.

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.split.map!(to!int);
    auto n = s[0];
    auto m = s[1];
    auto A = n.iota.map!(i => readln.split.map!(to!long).array ~ i).array;
    A.sort!"a[0] < b[0]";

    long pos = 0;
    long now = 0;
    auto ans = new long[](n);
    auto pq = new BinaryHeap!(Array!(Tuple!(long, long, long)), "a[1] > b[1]");
    int[] tmp;

    foreach (i; iota(0, n, m)) {
        int last = min(i + m - 1, n - 1);
        now = max(A[last][0], now);
        foreach (j; i..last+1) {
            pq.insert(tuple(A[j][0], A[j][1], A[j][2]));
        }
        while (!pq.empty) {
            auto t = pq.front;
            while (!pq.empty && pq.front[1] == t[1]) {
                tmp ~= pq.front[2].to!int;
                pq.removeFront;
            }
            now += t[1] - pos;
            pos = t[1];
            foreach (idx; tmp) ans[idx] = now;
            now += tmp.length / 2 + 1;
            tmp = [];
        }
        now += pos;
        pos = 0;
    }

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


Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps.There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i. e. the sums of lamps' temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i. e. each part should contain at least one lamp.  Help Dima to find a suitable way to cut the garland, or determine that this is impossible.While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can't be included in the answer.

The first line contains single integer n (3 ≤ n ≤ 106) — the number of lamps in the garland. Then n lines follow. The i-th of them contain the information about the i-th lamp: the number lamp ai, it is hanging on (and 0, if is there is no such lamp), and its temperature ti ( - 100 ≤ ti ≤ 100). The lamps are numbered from 1 to n.

If there is no solution, print -1. Otherwise print two integers — the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them.

NoteThe garland and cuts scheme for the first example:

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int head = -1;

alias array_t=int[];
int  [1_000_001] a;
array_t[1_000_001] b;
int  [1_000_001] t;
int  [1_000_001] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
            }
            else
            {
                writeln(first_good_kid, " ", good_kid);
                exit(0);
            }
        }
    }

    if (s[root] == 2 * sum3 && first_good_kid >= 0 && root != head)
    {
        writeln(first_good_kid, " ", root);
        exit(0);
    }

    if (s[root] == sum3)
        return root;

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    head = b[0][0];

    //writeln(b[0..n]);

    int sum = dfs(head);
    if (sum % 3 != 0)
    {
        writeln(-1);
        return 0;
    }

    dfs2(head, sum / 3);
    writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_000] a;
array_t[1_000_000] b;
int  [1_000_000] t;
int  [1_000_000] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    if (s[root] == sum3)
    {
        return root;
    }

    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (s[root] == 2 * sum3)
            {
                writeln(good_kid, " ", root);
                exit(0);
            }

            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
                continue;
            }

            writeln(first_good_kid, " ", good_kid);
            exit(0);
        }
    }

    return -1;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
        writeln(-1);
    else if (dfs2(b[0][0], sum / 3) < 0)
        writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_001] a;
array_t[1_000_001] b;
int  [1_000_001] t;
int  [1_000_001] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
            }
            else
            {
                writeln(first_good_kid, " ", good_kid);
                exit(0);
            }
        }
    }

    if (s[root] == 2 * sum3 && first_good_kid >= 0)
    {
        writeln(first_good_kid, " ", root);
        exit(0);
    }

    if (s[root] == sum3)
        return root;

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
    {
        writeln(-1);
        return 0;
    }

    dfs2(b[0][0], sum / 3);
    writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_000] a;
array_t[1_000_000] b;
int  [1_000_000] t;
int  [1_000_000] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    if (s[root] == sum3)
    {
        return root;
    }

    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (s[root] == 2 * sum3)
            {
                writeln(good_kid, " ", root);
                exit(0);
            }

            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
                continue;
            }

            writeln(first_good_kid, " ", good_kid);
            exit(0);
        }
    }

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
    {
        writeln(-1);
        return 0;
    }

    dfs2(b[0][0], sum / 3);
    writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_000] a;
array_t[1_000_000] b;
int  [1_000_000] t;
int  [1_000_000] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (s[root] == 2 * sum3)
            {
                writeln(good_kid, " ", root);
                exit(0);
            }

            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
                continue;
            }

            writeln(first_good_kid, " ", good_kid);
            exit(0);
        }
    }

    if (s[root] == sum3)
        return root;

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
    {
        writeln(-1);
        return 0;
    }

    dfs2(b[0][0], sum / 3);
    writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_000] a;
array_t[1_000_000] b;
int  [1_000_000] t;
int  [1_000_000] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    if (s[root] == sum3)
    {
        return root;
    }

    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (s[root] == 2 * sum3)
            {
                writeln(good_kid, " ", root);
                exit(0);
            }

            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
                continue;
            }

            writeln(first_good_kid, " ", good_kid);
            exit(0);
        }
    }

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
        writeln(-1);
    else if (dfs2(b[0][0], sum / 3) < 0)
        writeln(-1);

    return 0;
}

import std.stdio;
import core.stdc.stdlib;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

alias array_t=int[];
int  [1_000_000] a;
array_t[1_000_000] b;
int  [1_000_000] t;
int  [1_000_000] s;

int dfs(int root)
{
    s[root] = t[root];
    foreach (v; b[root])
    {
        s[root] += dfs(v);
    }

    return s[root];
}

int dfs2(int root, int sum3)
{
    int first_good_kid = -1;

    foreach (v; b[root])
    {
        int good_kid = dfs2(v, sum3);

        if (good_kid >= 0)
        {
            if (first_good_kid < 0)
            {
                first_good_kid = good_kid;
            }
            else
            {
                writeln(first_good_kid, " ", good_kid);
                exit(0);
            }
        }
    }

    if (s[root] == 2 * sum3 && first_good_kid >= 0)
    {
        writeln(first_good_kid, " ", root);
        exit(0);
    }

    if (s[root] == sum3)
        return root;

    return first_good_kid;
}

int main(string[] args)
{
    int n = read!int;
    int root;

    for (int i = 1; i <= n; i++)
    {
        a[i] = read!int;
        b[a[i]] ~= i;
        t[i] = read!int;
    }

    //writeln(b[0..n]);

    int sum = dfs(b[0][0]);
    if (sum % 3 != 0)
    {
        writeln(-1);
        return 0;
    }

    dfs2(b[0][0], sum / 3);
    writeln(-1);

    return 0;
}


Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here  denotes n divided by two, rounded up.

The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence.

Print exactly  numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses.

NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction.

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

immutable int infinity = int.max / 2;

int cost (int prev, int cur, int next)
{
	int res = 0;
	res += max (0, prev + 1 - cur);
	res += max (0, next + 1 - cur);
	return res;
}

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

		auto f = new int [] [] (2, n);
		int b = 0;
		int [] ans;
		foreach (i; 0..n)
		{
			int prev = (i - 1 >= 0) ? a[i - 1] : -1;
			int next = (i + 1 < n) ? a[i + 1] : -1;
			f[b][i] = cost (prev, a[i], next);
		}
		ans ~= minElement (f[b]);
		debug {writeln (f[b]);}

		foreach (k; 1..m)
		{
			b ^= 1;
			debug {f[b][] = -1;}
			int minHistory = infinity;
			int best = infinity;
			foreach (i; k * 2..n)
			{
				int prev = a[i - 1];
				int prevClose = min (prev, a[i - 2] - 1);
				int next = (i + 1 < n) ? a[i + 1] : -1;
				int cur = cost (prevClose, a[i], next) +
				    f[!b][i - 2];
				cur = min (cur, cost (prev, a[i], next) +
				    minHistory);

				f[b][i] = cur;
				best = min (best, cur);
				minHistory = min (minHistory, f[!b][i - 2]);
			}
			ans ~= best;
			debug {writeln (f[b]);}
		}

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

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

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

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

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

enum INF = 10^^9;

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

      const K = (N + 1) / 2;
      auto dp = new int[][](K + 1, N);
      foreach (k; 0 .. K + 1) {
        dp[k][] = INF;
      }
      foreach (i; 0 .. N) {
        int cost;
        if (i - 1 >= 0) {
          cost += max(A[i - 1] - A[i] + 1, 0);
        }
        if (i + 1 < N) {
          cost += max(A[i + 1] - A[i] + 1, 0);
        }
        dp[1][i] = cost;
      }
      auto dpMin = new int[N + 1];
      foreach (k; 1 .. K) {
        dpMin[0] = INF;
        foreach (i; 0 .. N) {
          dpMin[i + 1] = min(dpMin[i], dp[k][i]);
        }
        foreach (i; 2 .. N) {
          {
            int cost = dpMin[i - 2];
            cost += max(A[i - 1] - A[i] + 1, 0);
            if (i + 1 < N) {
              cost += max(A[i + 1] - A[i] + 1, 0);
            }
            chmin(dp[k + 1][i], cost);
          }
          {
            int cost = dp[k][i - 2];
            cost -= max(A[i - 1] - A[i - 2] + 1, 0);
            cost += max(A[i - 1] - min(A[i - 2], A[i]) + 1, 0);
            if (i + 1 < N) {
              cost += max(A[i + 1] - A[i] + 1, 0);
            }
            chmin(dp[k + 1][i], cost);
          }
        }
      }
      debug {
        foreach (k; 1 .. K + 1) {
          writeln(k, ": ", dp[k]);
        }
      }

      foreach (k; 1 .. K + 1) {
        if (k > 1) write(" ");
        write(dp[k].minElement);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute$$$$$$ (a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\ $$$$$$Here $$$x \oplus y$$$ is a bitwise XOR operation (i.e. $$$x$$$ ^ $$$y$$$ in many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.

The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 400\,000$$$) — the number of integers in the array. The second line contains integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^7$$$).

Print a single integer — xor of all pairwise sums of integers in the given array.

NoteIn the first sample case there is only one sum $$$1 + 2 = 3$$$.In the second sample case there are three sums: $$$1 + 2 = 3$$$, $$$1 + 3 = 4$$$, $$$2 + 3 = 5$$$. In binary they are represented as $$$011_2 \oplus 100_2 \oplus 101_2 = 010_2$$$, thus the answer is 2.$$$\oplus$$$ is the bitwise xor operation. To define $$$x \oplus y$$$, consider binary representations of integers $$$x$$$ and $$$y$$$. We put the $$$i$$$-th bit of the result to be 1 when exactly one of the $$$i$$$-th bits of $$$x$$$ and $$$y$$$ is 1. Otherwise, the $$$i$$$-th bit of the result is put to be 0. For example, $$$0101_2 \, \oplus \, 0011_2 = 0110_2$$$.

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

immutable int logLimit = 25;
immutable int limit = 1 << logLimit;

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

		int res = 0;
		foreach_reverse (z; 0..logLimit)
		{
			auto half = 1 << z;
			auto full = half << 1;

			foreach (ref c; a)
			{
				c %= full;
			}
			sort (a);
			debug {writeln (half, ": ", a);}

			long cur = 0;

			foreach (bound; [half, half * 2, half * 3])
			{
				int j = n;
				foreach (i; 0..n)
				{
					j = max (j, i + 1);
					while (j > i + 1 &&
					    a[i] + a[j - 1] >= bound)
					{
						j -= 1;
					}
					debug {writeln (i, " ", n - j);}
					cur += n - j;
				}
			}

			if (cur & 1)
			{
				res ^= 1 << z;
			}
		}

		writeln (res);
	}
}


You are given two strings $$$s$$$ and $$$t$$$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $$$1$$$. You can't choose a string if it is empty.For example:  by applying a move to the string "where", the result is the string "here",  by applying a move to the string "a", the result is an empty string "". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.Write a program that finds the minimum number of moves to make two given strings $$$s$$$ and $$$t$$$ equal.

The first line of the input contains $$$s$$$. In the second line of the input contains $$$t$$$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $$$2\cdot10^5$$$, inclusive.

Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.

NoteIn the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".In the second example, the move should be applied to the string "codeforces" $$$8$$$ times. As a result, the string becomes "codeforces" $$$\to$$$ "es". The move should be applied to the string "yes" once. The result is the same string "yes" $$$\to$$$ "es".In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.In the fourth example, the first character of the second string should be deleted.

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

void main()
{
    auto s = readln.chomp;
    auto t = readln.chomp;

    long ans = s.length + t.length;

    ans -= 2L * zip(s.retro, t.retro).until!(t => t[0] != t[1]).array.length;

    ans.writeln;
}


Frodo was caught by Saruman. He tore a pouch from Frodo's neck, shook out its contents —there was a pile of different rings: gold and silver..."How am I to tell which is the One?!" the mage howled."Throw them one by one into the Cracks of Doom and watch when Mordor falls!" Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found $$$n$$$ rings. And the $$$i$$$-th ring was either gold or silver. For convenience Saruman wrote down a binary string $$$s$$$ of $$$n$$$ characters, where the $$$i$$$-th character was 0 if the $$$i$$$-th ring was gold, and 1 if it was silver.Saruman has a magic function $$$f$$$, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, $$$f(001010) = 10, f(111) = 7, f(11011101) = 221$$$.Saruman, however, thinks that the order of the rings plays some important role. He wants to find $$$2$$$ pairs of integers $$$(l_1, r_1), (l_2, r_2)$$$, such that: $$$1 \le l_1 \le n$$$, $$$1 \le r_1 \le n$$$, $$$r_1-l_1+1\ge \lfloor \frac{n}{2} \rfloor$$$  $$$1 \le l_2 \le n$$$, $$$1 \le r_2 \le n$$$, $$$r_2-l_2+1\ge \lfloor \frac{n}{2} \rfloor$$$  Pairs $$$(l_1, r_1)$$$ and $$$(l_2, r_2)$$$ are distinct. That is, at least one of $$$l_1 \neq l_2$$$ and $$$r_1 \neq r_2$$$ must hold. Let $$$t$$$ be the substring $$$s[l_1:r_1]$$$ of $$$s$$$, and $$$w$$$ be the substring $$$s[l_2:r_2]$$$ of $$$s$$$. Then there exists non-negative integer $$$k$$$, such that $$$f(t) = f(w) \cdot k$$$.Here substring $$$s[l:r]$$$ denotes $$$s_ls_{l+1}\ldots s_{r-1}s_r$$$, and $$$\lfloor x \rfloor$$$ denotes rounding the number down to the nearest integer.Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.

Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^4$$$) — length of the string. The second line of each test case contains a non-empty binary string of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For every test case print four integers $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, $$$r_2$$$, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively. If there are multiple solutions, print any.

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

void theCode(){
    int n = scan!int;
    auto w = scan!(dchar[]);
    int l1, l2, r1, r2;
    for(int i = n/2; i < n; ++i){
        if(w[i] == '0'){
            l1 = 0;
            r1 = i;
            l2 = 0;
            r2 = i-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = 0; i < n/2; ++i){
        if(w[i] == '0'){
            l1 = i;
            r1 = n-1;
            l2 = i+1;
            r2 = n-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = 0; i < n; ++i){
        assert(w[i] == '1');
    }
    l1 = 0;
    r1 = n/2;
    l2 = 1;
    r2 = min(n/2 + 1, n-1);
    writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
}

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

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

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

void solve(string s)
{
  int lastzero = -1;
  foreach_reverse (i ; 0 .. cast(int)s.length) {
    if (s[i] == '0') {
      lastzero = i;
      break;
    }
  }
  if (lastzero >= cast(int)s.length / 2) {
//    writeln(s[1 - 1 .. lastzero]);
//    writeln(s[1 - 1 .. lastzero + 1]);
    writefln("%d %d %d %d", 1, lastzero + 1, 1, lastzero);
    return;
  }
  int mididx = cast(int)s.length / 2;
  if (s[mididx - 1] == '1') {
//    writeln(s[mididx + 1 - 1 .. s.length]);
//    writeln(s[mididx - 1 .. s.length - 1]);
    writefln("%d %d %d %d", mididx + 1, s.length, mididx, s.length - 1);
  } else {
//    writeln(s[mididx - 1 .. s.length]);
//    writeln(s[mididx + 1 - 1 .. s.length]);
    writefln("%d %d %d %d", mididx, s.length, mididx + 1, s.length);
  }
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readln.formattedRead!" %d "(n);
    string s = readln.strip;
//    writeln(s);
    solve(s);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long pos;
		foreach (i; n/2..n)
		{
			if (s[i] == '0')
			{
				pos = i;
				break;
			}
		}
		if (pos != 0)
		{
			ans[ti] = [1, pos+1, 1, pos];
			continue;
		}

		if (s[n/2-1] == '1')
			ans[ti] = [n/2, n-1, n/2+1, n];
		else
			ans[ti] = [n/2+1, n, n/2, n];
	}

	foreach (e; ans)
	{
		writeln(e[0], " ", e[1], " ", e[2], " ", e[3]);
	}
	stdout.flush;
	debug readln;
}

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

void theCode(){
    int n = scan!int;
    auto w = scan!(dchar[]);
    int l1, l2, r1, r2;
    for(int i = n/2; i < n; ++i){
        if(w[i] == '0'){
            l1 = 0;
            r1 = i;
            l2 = 0;
            r2 = i-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = 0; i < n/2; ++i){
        if(w[i] == '0'){
            l1 = i;
            r1 = n-1;
            l2 = i+1;
            r2 = n-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = 0; i < n; ++i){
        assert(w[i] == '1');
    }
    l1 = 0;
    r1 = n/2;
    l2 = 1;
    r2 = n/2 + 1;
    writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
}

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

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

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

void theCode(){
    int n = scan!int;
    auto w = scan!(dchar[]);
    int l1, l2, r1, r2;
    for(int i = 0; i < n/2; ++i){
        if(w[i] == '0'){
            l1 = i;
            r1 = n-1;
            l2 = i+1;
            r2 = n-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = n/2; i < n; ++i){
        if(w[i] == '0'){
            l1 = 0;
            r1 = i;
            l2 = 0;
            r2 = i-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = 0; i < n; ++i){
        assert(w[i] == '1');
    }
    l1 = 0;
    r1 = n/2;
    l2 = 1;
    r2 = n/2 + 1;
    writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
}

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

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

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

void theCode(){
    int n = scan!int;
    auto w = scan!(dchar[]);
    int l1, l2, r1, r2;
    dchar[] sol1, sol2;
    for(int i = 0; i < n/2; ++i){
        if(w[i] == '0'){
            /* sol1 ~= w[i]; */
            /* for(int j = i + 1; j < n; ++j){ */
            /*   sol1 ~= w[j]; */
            /*   sol2 ~= w[j]; */
            /*   f = 1; */
            /*   break; */
            /* } */
            l1 = i;
            r1 = n-1;
            l2 = i+1;
            r2 = n-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    for(int i = n/2; i < n; ++i){
        if(w[i] == '0'){
            /* for(int j = n/2; j < i; ++j){ */
            /*     sol1 ~= w[i]; */
            /*     sol2 ~= w[i]; */
            /* } */
            /* f = 1; */
            /* sol1 ~= w[i]; */
            l1 = 0;
            r1 = i;
            l2 = 0;
            r2 = i-1;
            writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
            return;
        }
    }
    l1 = 0;
    r1 = n/2;
    l2 = 1;
    r2 = n/2 + 1;
    writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1);
}

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

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

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

void solve(string s)
{
  int lastzero = -1;
  foreach_reverse (i ; 0 .. cast(int)s.length) {
    if (s[i] == '0') {
      lastzero = i;
      break;
    }
  }
  if (lastzero >= s.length / 2) {
//    writeln(s[1 - 1 .. lastzero]);
//    writeln(s[1 - 1 .. lastzero + 1]);
    writefln("%d %d %d %d", 1, lastzero + 1, 1, lastzero);
    return;
  }
  int mididx = cast(int)s.length / 2;
  if (s[mididx - 1] == '1') {
//    writeln(s[mididx + 1 - 1 .. s.length]);
//    writeln(s[mididx - 1 .. s.length - 1]);
    writefln("%d %d %d %d", mididx + 1, s.length, mididx, s.length - 1);
  } else {
//    writeln(s[mididx - 1 .. s.length]);
//    writeln(s[mididx + 1 - 1 .. s.length]);
    writefln("%d %d %d %d", mididx, s.length, mididx + 1, s.length);
  }
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readln.formattedRead!" %d "(n);
    string s = readln.strip;
//    writeln(s);
    solve(s);
  }
}

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

void solve(string s)
{
  int lastzero = -1;
  foreach_reverse (i ; 0 .. cast(int)s.length) {
    if (s[i] == '0') {
      lastzero = i;
      break;
    }
  }
  if (lastzero >= s.length / 2) {
//    writeln(s[1 - 1 .. lastzero]);
//    writeln(s[1 - 1 .. lastzero + 1]);
    writefln("%d %d %d %d", 1, lastzero, 1, lastzero + 1);
    return;
  }
  int mididx = cast(int)s.length / 2;
  if (s[mididx - 1] == '1') {
//    writeln(s[mididx + 1 - 1 .. s.length]);
//    writeln(s[mididx - 1 .. s.length - 1]);
    writefln("%d %d %d %d", mididx + 1, s.length, mididx, s.length - 1);
  } else {
//    writeln(s[mididx - 1 .. s.length]);
//    writeln(s[mididx + 1 - 1 .. s.length]);
    writefln("%d %d %d %d", mididx, s.length, mididx + 1, s.length);
  }
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readln.formattedRead!" %d "(n);
    string s = readln.strip;
//    writeln(s);
    solve(s);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long pos;
		foreach (i; n/2..n)
		{
			if (s[i] == '0')
			{
				pos = i;
				break;
			}
		}
		if (pos != 0)
		{
			ans[ti] = [1, pos, 1, pos+1];
			continue;
		}

		if (s[n/2-1] == '1')
			ans[ti] = [n/2, n-1, n/2+1, n];
		else
			ans[ti] = [n/2, n, n/2+1, n];
	}

	foreach (e; ans)
	{
		writeln(e[0], " ", e[1], " ", e[2], " ", e[3]);
	}
	stdout.flush;
	debug readln;
}


There are $$$n$$$ quests. If you complete the $$$i$$$-th quest, you will gain $$$a_i$$$ coins. You can only complete at most one quest per day. However, once you complete a quest, you cannot do the same quest again for $$$k$$$ days. (For example, if $$$k=2$$$ and you do quest $$$1$$$ on day $$$1$$$, then you cannot do it on day $$$2$$$ or $$$3$$$, but you can do it again on day $$$4$$$.)You are given two integers $$$c$$$ and $$$d$$$. Find the maximum value of $$$k$$$ such that you can gain at least $$$c$$$ coins over $$$d$$$ days. If no such $$$k$$$ exists, output Impossible. If $$$k$$$ can be arbitrarily large, output Infinity.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n,c,d$$$ ($$$2 \leq n \leq 2\cdot10^5$$$; $$$1 \leq c \leq 10^{16}$$$; $$$1 \leq d \leq 2\cdot10^5$$$) — the number of quests, the number of coins you need, and the number of days. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the rewards for the quests. The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$, and the sum of $$$d$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

For each test case, output one of the following.    If no such $$$k$$$ exists, output Impossible.  If $$$k$$$ can be arbitrarily large, output Infinity.  Otherwise, output a single integer — the maximum value of $$$k$$$ such that you can gain at least $$$c$$$ coins over $$$d$$$ days.  case-sensitive

NoteIn the first test case, one way to earn $$$5$$$ coins over $$$4$$$ days with $$$k=2$$$ is as follows:   Day 1: do quest 2, and earn $$$2$$$ coins.  Day 2: do quest 1, and earn $$$1$$$ coin.  Day 3: do nothing.  Day 4: do quest 2, and earn $$$2$$$ coins.  In total, we earned $$$2+1+2=5$$$ coins.In the second test case, we can make over $$$20$$$ coins on the first day itself by doing the first quest to earn $$$100$$$ coins, so the value of $$$k$$$ can be arbitrarily large, since we never need to do another quest.In the third test case, no matter what we do, we can't earn $$$100$$$ coins over $$$3$$$ days.

import std, core.checkedint;

bool solve(long[] pfa, long k, long c, long d)
{
  long fullruns = d / (k + 1);
  long coins_per_run = pfa.back;
  if (k + 1 < pfa.length)
    coins_per_run = pfa[(k + 1).to!int] - pfa[0];
  bool overflow;
  c -= muls(fullruns, coins_per_run, overflow);
  if (overflow || c <= 0)
    return true;
  long remdays = d % (k + 1);
  long remcoins = pfa.back;
  if (min(remdays, k + 1) < pfa.length)
    remcoins = pfa[min(remdays, k + 1).to!int] - pfa[0];
  return remcoins >= c;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n, c, d;
    readf!" %d %d %d "(n, c, d);
    auto a = readln.splitter.map!(to!long).array.sort!"a>b".array;
    long[] pfa = [0L];
    foreach (x ; a)
      pfa ~= pfa.back + x;

    long L = 0, R = long.max - 1, mid, ans = -1;
    while (L <= R) {
      mid = L + (R - L) / 2;
      if (solve(pfa, mid, c, d)) {
        ans = mid;
        L = mid + 1;
      } else {
        R = mid - 1;
      }
    }
    if (ans == -1)
      writeln("Impossible");
    else if (ans == long.max - 1)
      writeln("Infinity");
    else
      writeln(ans);
  }
}


There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle.

First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$).

Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer.

NoteIllustration for the first example: Illustration for the second example:

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

  int n;
  rd(n);
  int len = 0;
  foreach (i; 0 .. n) {
    int x, y;
    rd(x, y);
    len = max(len, x + y);
  }
  writeln(len);
}

void rd(T...)(ref T x) {
  import std.stdio, std.string, std.conv;

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


There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (109 + 7).

The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.

In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7).

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

immutable int MOD   = 1_000_000_007;
immutable int MAX_N =         1_003;

int [MAX_N] [MAX_N] c;

void main ()
{
	int n, m;

	foreach (i; 0..MAX_N)
	{
		c[i][0] = 1;
		foreach (j; 1..i + 1)
		{
			c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
		}
	}

	while (readf (" %d %d", &n, &m) > 0)
	{
		auto a = new int [m];
		foreach (i; 0..m)
		{
			readf (" %d", &a[i]);
		}
		sort (a);
		a = 0 ~ a ~ (n + 1);
		int s = 0;
		int res = 1;
		foreach (i; 0..m + 1)
		{
			int d = a[i + 1] - a[i] - 1;
			res = (cast (long) res * c[s + d][s]) % MOD;
			if (i != 0 && i != m)
			{
				foreach (j; 0..d - 1)
				{
					res = (res * 2) % MOD;
				}
			}
			s += d;
		}
		writeln (res);
	}
}

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

immutable int MOD   = 1_000_000_007;
immutable int MAX_N =         1_003;

int [MAX_N] [MAX_N] c;

void main ()
{
	int n, m;

	foreach (i; 0..MAX_N)
	{
		c[i][0] = 1;
		foreach (j; 1..i + 1)
		{
			c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
		}
	}

	while (readf (" %d %d", &n, &m) > 0)
	{
		auto a = new int [m];
		foreach (i; 0..m)
		{
			readf (" %d", &a[i]);
		}
		sort (a);
		a = 0 ~ a ~ (n + 1);
		int s = 0;
		int res = 1;
		foreach (i; 0..m + 1)
		{
			int d = a[i + 1] - a[i] - 1;
			res = (res * c[s + d][s]) % MOD;
			if (i != 0 && i != m)
			{
				foreach (j; 0..d - 1)
				{
					res = (res * 2) % MOD;
				}
			}
			s += d;
		}
		writeln (res);
	}
}


In the game of Mastermind, there are two players  — Alice and Bob. Alice has a secret code, which Bob tries to guess. Here, a code is defined as a sequence of $$$n$$$ colors. There are exactly $$$n+1$$$ colors in the entire universe, numbered from $$$1$$$ to $$$n+1$$$ inclusive.When Bob guesses a code, Alice tells him some information about how good of a guess it is, in the form of two integers $$$x$$$ and $$$y$$$.The first integer $$$x$$$ is the number of indices where Bob's guess correctly matches Alice's code. The second integer $$$y$$$ is the size of the intersection of the two codes as multisets. That is, if Bob were to change the order of the colors in his guess, $$$y$$$ is the maximum number of indices he could get correct.For example, suppose $$$n=5$$$, Alice's code is $$$[3,1,6,1,2]$$$, and Bob's guess is $$$[3,1,1,2,5]$$$. At indices $$$1$$$ and $$$2$$$ colors are equal, while in the other indices they are not equal. So $$$x=2$$$. And the two codes have the four colors $$$1,1,2,3$$$ in common, so $$$y=4$$$.  Solid lines denote a matched color for the same index. Dashed lines denote a matched color at a different index. $$$x$$$ is the number of solid lines, and $$$y$$$ is the total number of lines. You are given Bob's guess and two values $$$x$$$ and $$$y$$$. Can you find one possibility of Alice's code so that the values of $$$x$$$ and $$$y$$$ are correct?

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of each test case contains three integers $$$n,x,y$$$ ($$$1\le n\le 10^5, 0\le x\le y\le n$$$)  — the length of the codes, and two values Alice responds with. The second line of each test case contains $$$n$$$ integers $$$b_1,\ldots,b_n$$$ ($$$1\le b_i\le n+1$$$)  — Bob's guess, where $$$b_i$$$ is the $$$i$$$-th color of the guess. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.

For each test case, on the first line, output "YES" if there is a solution, or "NO" if there is no possible secret code consistent with the described situation. You can print each character in any case (upper or lower). If the answer is "YES", on the next line output $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1\le a_i\le n+1$$$)  — Alice's secret code, where $$$a_i$$$ is the $$$i$$$-th color of the code. If there are multiple solutions, output any.

NoteThe first test case is described in the statement.In the second test case, $$$x=3$$$ because the colors are equal at indices $$$2,4,5$$$. And $$$y=4$$$ because they share the colors $$$1,1,1,2$$$.In the third test case, $$$x=0$$$ because there is no index where the colors are the same. But $$$y=4$$$ because they share the colors $$$3,3,5,5$$$.In the fourth test case, it can be proved that no solution exists.

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int len = left.length.to !(int);
		int hi = len / 2;
		foreach (lo; 0..len)
		{
			int dest = left[hi];
			if (y == 0 || left[lo] == dest)
			{
				dest = notFound;
			}
			else
			{
				y -= 1;
			}
			trans ~= [left[lo], dest];
			hi = (hi + 1) % (len + !len);
		}

		if (y > 0)
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}

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

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

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

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

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

        const M = N + 2;
        auto iss = new int[][M];
        foreach (i; 0 .. N) {
          iss[B[i]] ~= i;
        }
        int fake;
        foreach (b; 1 .. M) {
          if (iss[b].empty) {
            fake = b;
            break;
          }
        }

        int mLim;
        foreach (m; 0 .. N - X + 1) {
          if (min(2 * (N - X - m), N - X) >= Y - X) {
            chmax(mLim, m);
          }
        }
        debug {
          writeln("mLim = ", mLim);
        }

        int must;
        foreach (b; 1 .. M) {
          const len = cast(int)(iss[b].length);
          must += max(len - mLim, 0);
        }
        if (must <= X) {
          int[] hits;
          foreach (b; 1 .. M) {
            const len = cast(int)(iss[b].length);
            if (len - mLim >= 0) {
              hits ~= iss[b][mLim .. len];
              iss[b].length = mLim;
            }
          }
          int x = X - must;
          foreach (b; 1 .. M) {
            const len = cast(int)(iss[b].length);
            const tmp = min(len, x);
            hits ~= iss[b][len - tmp .. len];
            iss[b].length = len - tmp;
            x -= tmp;
          }
          debug {
            writeln("hits = ", hits);
            writeln("iss = ", iss);
          }
          auto ans = new int[N];
          ans[] = fake;
          foreach (i; hits) {
            ans[i] = B[i];
          }
          int bm;
          foreach (b; 1 .. M) {
            if (iss[bm].length < iss[b].length) {
              bm = b;
            }
          }
          if (2 * iss[bm].length <= N - X) {
            int[] blows;
            foreach (b; 1 .. M) {
              blows ~= iss[b];
            }
            foreach (j; 0 .. Y - X) {
              ans[blows[j]] = B[blows[(j + (N - X) / 2) % (N - X)]];
            }
          } else {
            int[] others;
            foreach (b; 1 .. M) {
              if (b != bm) {
                others ~= iss[b];
              }
            }
            const othersLen = cast(int)(others.length);
            assert(2 * othersLen >= Y - X);
            int y = Y - X;
            foreach (j; 0 .. othersLen) {
              if (y > 0) {
                ans[others[j]] = B[iss[bm][j]];
                --y;
              }
            }
            foreach (j; 0 .. othersLen) {
              if (y > 0) {
                ans[iss[bm][j]] = B[others[j]];
                --y;
              }
            }
          }
          writeln("YES");
          foreach (i; 0 .. N) {
            if (i > 0) write(" ");
            write(ans[i]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int len = left.length.to !(int);
		int hi = c.front.value % (len + !len);
		foreach (lo; 0..len)
		{
			int dest = (y > 0) ? left[hi] : notFound;
			y -= 1;
			trans ~= [left[lo], dest];
			hi = (hi + 1) % (len + !len);
		}

		if (trans.any !(p => p[0] == p[1]))
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int hi = c.front.value;
		int len = left.length.to !(int);
		foreach (lo; 0..len)
		{
			int dest = (y > 0) ? left[hi] : notFound;
			y -= 1;
			trans ~= [left[lo], dest];
			hi = (hi + 1) % len;
		}

		if (trans.any !(p => p[0] == p[1]))
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int len = left.length.to !(int);
		int hi = len / 2;
		foreach (lo; 0..len)
		{
			int dest = left[hi];
			if (y == 0 || left[lo] == dest)
			{
				dest = notFound;
			}
			else
			{
				y -= 1;
			}
			trans ~= [left[lo], dest];
			hi = (hi + 1) % (len + !len);
		}

		if (trans.any !(p => p[0] == p[1]))
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int len = left.length.to !(int);
		int hi = len / 2;
		foreach (lo; 0..len)
		{
			int dest = (y > 0) ? left[lo] : notFound;
			y -= 1;
			trans ~= [left[hi], dest];
			hi = (hi + 1) % (len + !len);
		}

		if (trans.any !(p => p[0] == p[1]))
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}

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

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

		int [int] b;
		foreach (ref t; a)
		{
			b[t] += 1;
		}
		auto c = b.byPair.array;
		c.schwartzSort !(q{-a.value});
		y -= x;

		int [] inPlace;
		while (x > 0)
		{
			int cur = c.front.value;
			auto d = c;
			while (x > 0 && !d.empty && d.front.value == cur)
			{
				inPlace ~= d.front.key;
				d.front.value -= 1;
				d.popFront ();
				x -= 1;
			}
		}

		int notFound = 1;
		while (notFound in b)
		{
			notFound += 1;
		}

		c.schwartzSort !(q{-a.value});

		int [] left;
		foreach (ref p; c)
		{
			foreach (i; 0..p.value)
			{
				left ~= p.key;
			}
		}

		int [2] [] trans;
		int len = left.length.to !(int);
		int hi = c.front.value % (len + !len);
		foreach (lo; 0..len)
		{
			int dest = (y > 0) ? left[lo] : notFound;
			y -= 1;
			trans ~= [left[hi], dest];
			hi = (hi + 1) % (len + !len);
		}

		if (trans.any !(p => p[0] == p[1]))
		{
			writeln ("NO");
			continue multitest_loop;
		}

		sort (inPlace);
		sort (trans);
		auto q = n.iota.array;
		makeIndex (a, q);
		auto res = new int [n];
		foreach (i; q)
		{
			if (!inPlace.empty && inPlace.front == a[i])
			{
				res[i] = a[i];
				inPlace.popFront ();
			}
			else if (!trans.empty && trans.front[0] == a[i])
			{
				res[i] = trans.front[1];
				trans.popFront ();
			}
			else
			{
				assert (false);
			}
		}
		writeln ("YES");
		writefln !("%(%s %)") (res);
	}
}


Sereja has two sequences a1, a2, ..., an and b1, b2, ..., bm, consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions:  Choose several (at least one) first elements of sequence a (non-empty prefix of a), choose several (at least one) first elements of sequence b (non-empty prefix of b); the element of sequence a with the maximum index among the chosen ones must be equal to the element of sequence b with the maximum index among the chosen ones; remove the chosen elements from the sequences.  Remove all elements of both sequences. The first action is worth e energy units and adds one dollar to Sereja's electronic account. The second action is worth the number of energy units equal to the number of elements Sereja removed from the sequences before performing this action. After Sereja performed the second action, he gets all the money that he earned on his electronic account during the game.Initially Sereja has s energy units and no money on his account. What maximum number of money can Sereja get? Note, the amount of Seraja's energy mustn't be negative at any time moment.

The first line contains integers n, m, s, e (1 ≤ n, m ≤ 105; 1 ≤ s ≤ 3·105; 103 ≤ e ≤ 104). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105). The third line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 105).

Print a single integer — maximum number of money in dollars that Sereja can get.

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, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	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)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, 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)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int M, N, S, E;
int[] A, B;

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		N = readInt;
		S = readInt;
		E = readInt;
		A = new int[M];
		foreach (i; 0 .. M) {
// if(M==100_000){A[i]=1;continue;}
			A[i] = readInt;
		}
		B = new int[N];
		foreach (j; 0 .. N) {
// if(N==100_000){B[j]=1;continue;}
			B[j] = readInt;
		}

		int[][int] posB;
		foreach (j; 0 .. N) {
			posB[B[j]] ~= j;
		}
// writeln(posB);

		int ans;

		const L = S / E;
		int[] dp = new int[L + 1];
		dp[] = N + 1;
		dp[0] = 0;
		foreach (i; 0 .. M + 1) {
			foreach (l; 0 .. L + 1) if (dp[l] <= N) {
				if (i + dp[l] <= S - E * l) {
					chmax(ans, l);
				}
			}
			if (i == M) {
				break;
			}
			if (A[i] in posB) {
				int[] poss = posB[A[i]];
				foreach_reverse (l; 0 .. L) if (dp[l] <= N) {
					const idx = poss.lowerBound(dp[l]);
					if (idx < poss.length) {
						chmin(dp[l + 1], poss[idx] + 1);
					}
					poss = poss[0 .. idx];
				}
			}
		}

		writeln(ans);

	}
	} catch (EOFException) {}
}


You are given $$$n$$$ numbers $$$a_1, a_2, \ldots, a_n$$$. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array $$$[1, 4, 5, 6, 7, 8]$$$, the arrangement on the left is valid, while arrangement on the right is not, as $$$5\ge 4 + 1$$$ and $$$8> 1 + 6$$$.

The first line contains a single integer $$$n$$$ ($$$3\le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \le 10^9$$$) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).

If there is no solution, output "NO" in the first line.  If there is a solution, output "YES" in the first line. In the second line output $$$n$$$ numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.

NoteOne of the possible arrangements is shown in the first example: $$$4< 2 + 3$$$;$$$2 < 4 + 3$$$;$$$3< 4 + 2$$$.One of the possible arrangements is shown in the second example.No matter how we arrange $$$13, 8, 5$$$ in a circle in the third example, $$$13$$$ will have $$$8$$$ and $$$5$$$ as neighbors, but $$$13\ge 8 + 5$$$. There is no solution in the fourth example.

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 n = read.to!int;
 long[] as;
 foreach(i; 0 .. n) as ~= read.to!long;

 as.sort!"a>b"();
 if(as[0] >= as[1] + as[2]){
  "NO".writeln;
  return;
 }
 else{
  "YES".writeln;
  long[] ans;
  for(int i = 0; i < as.length; i += 2) ans ~= as[i];
  for(int i = as.length / 2 * 2 - 1; i >= 0; i -= 2) ans ~= as[i];
  ans.map!(to!string).array.join(" ").writeln;
 }
}