Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.

On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.

NoteIn the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5.

/** CodeForces Problem 381.A (2)
 * ACM summer training contest (3)
 * Noein
 */

import std.stdio;

T mut(T)(in T inval) {
    import std.traits;

    Unqual!T new_val = inval;

    return new_val;
}

T imut(T)(in T inval) {
    immutable new_val = inval;

    return new_val;
}

void main() {
    auto n = 0.mut;

    auto deck = new uint[](1000);

    readf("%s\n", &n);
    //writeln(n);

    for(int i; i < n; ++i) {
        readf("%s ", &deck[i]);
    }
    //writeln(deck);

    auto sS = 0, sD = 0, pF = 0, pR = (n-1);

    auto turn = false;

    for( int i; i < n; ++i ) {
        ( turn ? sD : sS )
        += (deck[pF] > deck[pR]
        ? deck[pF++]
        : deck[pR--]);

        turn ^= true;
    }

    writeln(sS, " ", sD);
}


You are given the following points with integer coordinates on the plane: M0, A0, A1, ..., An - 1, where n is odd number. Now we define the following infinite sequence of points Mi: Mi is symmetric to Mi - 1 according  (for every natural number i). Here point B is symmetric to A according M, if M is the center of the line segment AB. Given index j find the point Mj.

On the first line you will be given an integer n (1 ≤ n ≤ 105), which will be odd, and j (1 ≤ j ≤ 1018), where j is the index of the desired point. The next line contains two space separated integers, the coordinates of M0. After that n lines follow, where the i-th line contain the space separated integer coordinates of the point Ai - 1. The absolute values of all input coordinates will not be greater then 1000.

On a single line output the coordinates of Mj, space separated.

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

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

    int x0, y0;
    readf("%s %s", &x0, &y0);
    readln;

    int[] xs, ys;
    foreach (_; 0 .. n) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;

        xs ~= x;
        ys ~= y;
    }

    int go(int st, int[] arr, long j) {

        int calc(int sz) {
            int ans = 0;
            int idx = 0;
            foreach_reverse (e; arr[0 .. sz]) {
                if (idx % 2 == 0) { ans += 2*e; }
                else { ans -= 2*e; }

                ++idx;
            }

            return ans;
        }

        long cycles = j / arr.length;
        bool isCycle = cycles % 2 == 1;
        int lft = (j % arr.length).to!int;

        int lftSum = calc(lft);
        int cycleSm = isCycle ? calc(n) : 0;
        if (lft % 2 == 1) {
            cycleSm = -cycleSm;
        }

        if (j % 2 == 0) {
            st = -st;
        }

        return lftSum + cycleSm - st;
    }

    writeln(go(x0, xs, j), ' ', go(y0, ys, j));
}


This problem is different from the easy version. In this version Ujan makes at most $$$2n$$$ swaps. In addition, $$$k \le 1000, n \le 50$$$ and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most $$$2n$$$ times: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$.Ujan's goal is to make the strings $$$s$$$ and $$$t$$$ equal. He does not need to minimize the number of performed operations: any sequence of operations of length $$$2n$$$ or shorter is suitable.

The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 1000$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 50$$$), the length of the strings $$$s$$$ and $$$t$$$.  Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

For each test case, output "Yes" if Ujan can make the two strings equal with at most $$$2n$$$ operations and "No" otherwise. You can print each letter in any case (upper or lower). In the case of "Yes" print $$$m$$$ ($$$1 \le m \le 2n$$$) on the next line, where $$$m$$$ is the number of swap operations to make the strings equal. Then print $$$m$$$ lines, each line should contain two integers $$$i, j$$$ ($$$1 \le i, j \le n$$$) meaning that Ujan swaps $$$s_i$$$ and $$$t_j$$$ during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than $$$2n$$$ is suitable.

4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto k = RD!int;
	auto ans = new int[2][][](k);
	foreach (i; 0..k)
	{
		auto n = RD!int;
		auto s = RD!(char[]);
		auto t = RD!(char[]);
		auto cnt = new int[](26);
		foreach (j; 0..n)
		{
			auto c1 = s[j] - 'a';
			auto c2 = t[j] - 'a';
			++cnt[c1];
			++cnt[c2];
		}
		bool ok = true;
		foreach (e; cnt)
		{
			if (e % 2 == 1)
			{
				ok = false;
				break;
			}
		}
		if (!ok) continue;
		foreach (j; 0..n)
		{
			if (s[j] != t[j])
			{
				int pos = -1;
				foreach (l; j+1..n)
				{
					if (t[l] == t[j])
					{
						pos = l;
						break;
					}
				}
				if (pos == -1)
				{
					foreach (l; j+1..n)
					{
						if (s[l] == t[j])
						{
							pos = l;
							break;
						}
					}
					ans[i] ~= [pos, pos];
					ans[i] ~= [j, pos];
					swap(s[pos], t[pos]);
					swap(s[j], t[pos]);
				}
				else
				{
					ans[i] ~= [j, pos];
					swap(s[j], t[pos]);
				}
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln("NO");
		else
		{
			writeln("YES");
			writeln(e.length);
			foreach (ee; e)
				writeln(ee[0]+1, " ", ee[1]+1);
		}
	}
	stdout.flush();
	debug readln();
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto k = RD!int;
	auto ans = new bool[](k);
	foreach (i; 0..k)
	{
		auto n = RD!int;
		auto s = RD!string;
		auto t = RD!string;
		int[2] pos;
		int cnt;
		foreach (j; 0..n)
		{
			if (s[j] != t[j])
			{
				++cnt;
				if (cnt >= 3) break;
				pos[cnt-1] = j;
			}
		}
		if (cnt == 2)
		{
			auto p1 = pos[0];
			auto p2 = pos[1];
			if (s[p1] == s[p2] && t[p1] == t[p2])
				ans[i] = true;
		}
	}

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


The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y ≥ 0 are specified). The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x ≤ bj ≤ ai + y.

The first input line contains four integers n, m, x and y (1 ≤ n, m ≤ 105, 0 ≤ x, y ≤ 109) — the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) in non-decreasing order, separated by single spaces — the desired sizes of vests.  The third line contains m integers b1, b2, ..., bm (1 ≤ bj ≤ 109) in non-decreasing order, separated by single spaces — the sizes of the available vests.

In the first line print a single integer k — the maximum number of soldiers equipped with bulletproof vests.  In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order. If there are multiple optimal answers, you are allowed to print any of them.

NoteIn the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped.

module cf_161A;

import std.stdio;
import std.algorithm;

struct Pair (T) {
    T first, second;
}

void main() {
    int n, m, x, y;
    int[] needSizes, availSizes;

    readf("%d %d %d %d", &n, &m, &x, &y);
    needSizes = new int[n];
    availSizes = new int[m];
    for (int i = 0; i < n; ++i) {
        readf(" %d", &needSizes[i]);
    }
    for (int i = 0; i < m; ++i) {
        readf(" %d", &availSizes[i]);
    }

    sort(needSizes);
    sort(availSizes);

    int curN = 0, curM = 0;
    Pair!int[] pairs;

    while (curN < n && curM < m) {
        if (needSizes[curN] - x <= availSizes[curM] &&
            needSizes[curN]  + y >= availSizes[curM]) {

            pairs ~= Pair!int(curN + 1, curM + 1);
            ++curN, ++curM;
        } else if (needSizes[curN] + y < availSizes[curM]) {
            ++curN;
        } else {
            ++curM;
        }
    }

    writeln(pairs.length);
    for (int i = 0; i < pairs.length; ++i) {
        writeln(pairs[i].first, " ", pairs[i].second);
    }
}

module cf_161A;

import std.stdio;
import std.algorithm;

struct Pair (T) {
    T first, second;
}

void main() {
    int n, m, x, y;
    int[] needSizes, availSizes;

    readf("%d %d %d %d", &n, &m, &x, &y);
    needSizes = new int[n];
    availSizes = new int[m];
    for (int i = 0; i < n; ++i) {
        readf(" %d", &needSizes[i]);
    }
    for (int i = 0; i < m; ++i) {
        readf(" %d", &availSizes[i]);
    }

    sort(needSizes);
    sort(availSizes);

    int curN = 0, curM = 0;
    Pair!int[] pairs;

    while (curN < n && curM < m) {
        if (needSizes[curN] - x <= availSizes[curM] &&
            needSizes[curN]  + y >= availSizes[curM]) {

            pairs ~= Pair!int(curN + 1, curM + 1);
            ++curN, ++curM;
        } else {
            ++curN;
        }
    }

    writeln(pairs.length);
    for (int i = 0; i < pairs.length; ++i) {
        writeln(pairs[i].first, " ", pairs[i].second);
    }
}

module cf_161A;

import std.stdio;
import std.algorithm;

struct Pair (T) {
    T first, second;
}

void main() {
    int n, m, x, y;
    int[] needSizes, availSizes;

    readf("%d %d %d %d", &n, &m, &x, &y);
    needSizes = new int[n];
    availSizes = new int[m];
    for (int i = 0; i < n; ++i) {
        readf(" %d", &needSizes[i]);
    }
    for (int i = 0; i < m; ++i) {
        readf(" %d", &availSizes[i]);
    }

    sort(needSizes);
    sort(availSizes);

    int curN = 0, curM = 0;
    Pair!int[] pairs;

    while (curN < n && curM < m) {
        if (needSizes[curN] >= availSizes[curM] - x &&
            needSizes[curN] <= availSizes[curM] + y) {

            pairs ~= Pair!int(curN + 1, curM + 1);
            ++curM;
        } else {
            ++curN;
        }
    }

    writeln(pairs.length);
    for (int i = 0; i < pairs.length; ++i) {
        writeln(pairs[i].first, " ", pairs[i].second);
    }
}


Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter — its complexity. The complexity of the i-th chore equals hi.As Petya is older, he wants to take the chores with complexity larger than some value x (hi > x) to leave to Vasya the chores with complexity less than or equal to x (hi ≤ x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a + b = n).In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?

The first input line contains three integers n, a and b (2 ≤ n ≤ 2000; a, b ≥ 1; a + b = n) — the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces.

Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.

NoteIn the first sample the possible values of x are 3, 4 or 5.In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.

module _template;
import std.stdio;
import std.algorithm;
import std.container;
import std.range;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

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

        int read_int() {
                return read_string.to!int;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int a = cin.read_int;
                int b = cin.read_int;

                int[] v = new int[n];
                for (int i = 0; i < n; i++) {
                        v[i] = cin.read_int;
                }
                sort(v);
                writeln(v[b] - v[b - 1]);
        }
}

module _template;
import std.stdio;
import std.algorithm;
import std.container;
import std.range;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

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

        int read_int() {
                return read_string.to!int;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int a = cin.read_int;
                int b = cin.read_int;

                int[] v = new int[n];
                for (int i = 0; i < n; i++) {
                        v[i] = cin.read_int;
                }
                sort(v);
                writeln(v[b] - v[a]);
        }
}


Given an array $$$a$$$ of length $$$n$$$, find another array, $$$b$$$, of length $$$n$$$ such that:  for each $$$i$$$ $$$(1 \le i \le n)$$$ $$$MEX(\{b_1$$$, $$$b_2$$$, $$$\ldots$$$, $$$b_i\})=a_i$$$. The $$$MEX$$$ of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine this.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ ($$$0 \le a_i \le i$$$) — the elements of the array $$$a$$$. It's guaranteed that $$$a_i \le a_{i+1}$$$ for $$$1\le i < n$$$.

If there's no such array, print a single line containing $$$-1$$$. Otherwise, print a single line containing $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, $$$\ldots$$$, $$$b_n$$$ ($$$0 \le b_i \le 10^6$$$) If there are multiple answers, print any.

NoteIn the second test case, other answers like $$$[1,1,1,0]$$$, for example, are valid.

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 n = RD!int;
	auto a = RDA;

	long next;
	long[] x, y;
	bool[long] set;
	foreach (i; 0..n)
	{
		foreach (j; next..a[i])
		{
			x ~= j;
		}
		set[a[i]] = true;
		next = a[i]+1;
	}
	foreach (i; a[$-1]+1..n+1)
	{
		x ~= i;
	}
	foreach (key; set.keys.sort)
	{
		y ~= key;
	}

	auto ans = new long[](n);
	foreach (i; 0..n)
	{
		debug writeln(x);
		debug writeln(y);
		if (y.front < a[i])
			ans[i] = y.front, y.popFront;
		else
			ans[i] = x.front, x.popFront;
	}

	ans.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}


Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).

First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list. Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.

Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not. You can print each letter in any case (upper or lower).

NoteIn test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".

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

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

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

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

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

      foreach (i; 0 .. N) {
        bool ans;
        foreach (j; 0 .. i) {
          ans = ans || (S[i] == S[j]);
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n × m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.

The first line contains 2 space-separated numbers n and m (1 ≤ n, m ≤ 25) — the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.

Output one number — the maximum possible perimeter of a bargaining table for Bob's office room.

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

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

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

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

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

int M, N;
string[] A;

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		N = readInt;
		A = new string[M];
		foreach (x; 0 .. M) {
			A[x] = readToken;
		}

		int[][] sum = new int[][](M + 1, N + 1);
		foreach (x; 0 .. M) foreach (y; 0 .. N) {
			sum[x + 1][y + 1] = sum[x + 1][y] + sum[x][y + 1] - sum[x][y] + (A[x][y] - '0');
		}

		int ans;
		foreach (xa; 0 .. M) foreach (ya; 0 .. N) foreach (xb; xa + 1 .. M + 1) foreach (yb; ya + 1 .. N + 1) {
			int cnt = sum[xa][ya] - sum[xa][yb] - sum[xb][ya] + sum[xb][yb];
			if (cnt == 0) {
				chmax(ans, ((xb - xa) + (yb - ya)) * 2);
			}
		}
		writeln(ans);

	}
	} catch (EOFException) {}
}


Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.

The first line contains integer n (1 ≤ n ≤ 100) — the number of rectangles. Each of the following n lines contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ 100, 1 ≤ y1 ≤ y2 ≤ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.

In a single line print the sum of all values in the cells of the table.

NoteNote to the first sample test:Values of the table in the first three rows and columns will be as follows:121121110So, the sum of values will be equal to 10.Note to the second sample test:Values of the table in the first three rows and columns will be as follows:222222222So, the sum of values will be equal to 18.

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

void main() {
  int n = readln().strip().to!int;
  long ans = 0;
  foreach(i; 0..n) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    ans += (x2 - x1 + 1) * (y2 - y1 + 1);
  }
  writeln(ans);
}

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

void main() {
    int n = readln.chomp.to!int;
    int ans = 0;
    foreach (i; 0..n) {
        int x1, y1, x2, y2;
        readf("%d %d %d %d\n", &x1, &y1, &x2, &y2);
        ans += (x2 - x1 + 1) * (y2 - y1 + 1);
    }

    ans.writeln;
}


Mainak has an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ positive integers. He will do the following operation to this array exactly once:  Pick a subsegment of this array and cyclically rotate it by any amount.  Formally, he can do the following exactly once:  Pick two integers $$$l$$$ and $$$r$$$, such that $$$1 \le l \le r \le n$$$, and any positive integer $$$k$$$.  Repeat this $$$k$$$ times: set $$$a_l=a_{l+1}, a_{l+1}=a_{l+2}, \ldots, a_{r-1}=a_r, a_r=a_l$$$ (all changes happen at the same time). Mainak wants to maximize the value of $$$(a_n - a_1)$$$ after exactly one such operation. Determine the maximum value of $$$(a_n - a_1)$$$ that he can obtain.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 999$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

For each test case, output a single integer — the maximum value of $$$(a_n - a_1)$$$ that Mainak can obtain by doing the operation exactly once.

Note In the first test case, we can rotate the subarray from index $$$3$$$ to index $$$6$$$ by an amount of $$$2$$$ (i.e. choose $$$l = 3$$$, $$$r = 6$$$ and $$$k = 2$$$) to get the optimal array: $$$$$$[1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]$$$$$$ So the answer is $$$a_n - a_1 = 11 - 1 = 10$$$. In the second testcase, it is optimal to rotate the subarray starting and ending at index $$$1$$$ and rotating it by an amount of $$$2$$$. In the fourth testcase, it is optimal to rotate the subarray starting from index $$$1$$$ to index $$$4$$$ and rotating it by an amount of $$$3$$$. So the answer is $$$8 - 1 = 7$$$.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto res = a.back () - a.front ();
		foreach (i; 0..n - 1)
		{
			res = max (res, a[i] - a[i + 1]);
		}
		res = max (res, maxElement (a[1..n], a.front) - a.front);
		res = max (res, a.back - minElement (a[0..n - 1], a.back));
		writeln (res);
	}
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers. In one move, you can jump from the position $$$i$$$ to the position $$$i - a_i$$$ (if $$$1 \le i - a_i$$$) or to the position $$$i + a_i$$$ (if $$$i + a_i \le n$$$).For each position $$$i$$$ from $$$1$$$ to $$$n$$$ you want to know the minimum the number of moves required to reach any position $$$j$$$ such that $$$a_j$$$ has the opposite parity from $$$a_i$$$ (i.e. if $$$a_i$$$ is odd then $$$a_j$$$ has to be even and vice versa).

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

Print $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$, where $$$d_i$$$ is the minimum the number of moves required to reach any position $$$j$$$ such that $$$a_j$$$ has the opposite parity from $$$a_i$$$ (i.e. if $$$a_i$$$ is odd then $$$a_j$$$ has to be even and vice versa) or -1 if it is impossible to reach such a position.

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;

immutable int INF = 1 << 29;

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

    auto G = new int[][](N);

    DList!(Tuple!(int, int)) que;
    auto ans = new int[](N);
    ans[] = -1;

    foreach (i; 0..N) {
        int j = i + A[i];
        int k = i - A[i];
        if (j < N) {
            G[j] ~= i;
        }
        if (k >= 0) {
            G[k] ~= i;
        }
        if (j < N && (B[i] ^ B[j])) {
            que.insertBack(tuple(i, 2));
            ans[i] = 1;
        } else if (k >= 0 && (B[i] ^ B[k])) {
            que.insertBack(tuple(i, 2));
            ans[i] = 1;
        }
    }

    while (!que.empty) {
        auto t = que.front;
        auto i = t[0];
        auto d = t[1];
        que.removeFront;
        foreach (j; G[i]) {
            if (ans[j] == -1) {
                ans[j] = d;
                que.insertBack(tuple(j, d + 1));
            }
        }
    }

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

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

void main(string[] args)
{
  debug stdin = File(args[1]);
  auto n = next!int;
  auto a = next!int(n);
  auto dist = new int[][](2, n);
  auto invAdj = new int[][](n);
  debug writeln(a);
  foreach(i, ai; a)
    {
      debug writeln(i, " ", ai, " ",  cast(int)i - ai);
      if (cast(int)i - ai >= 0)
        invAdj[i - ai] ~= cast(int)i;
      if (cast(int)i + ai < n)
        invAdj[i + ai] ~= cast(int)i;
    }
  foreach(p; 0 .. 2)
    {
      dist[p][] = -1;
      auto visited = new bool[](n);
      auto queue = DList!int();
      foreach(i, ai; a)
        {
          if (ai % 2 == p)
            {
              visited[i] = true;
              queue.insertBack(cast(int)i);
              dist[p][i] = 0;
            }
        }
      while(!queue.empty)
        {
          auto node = queue.front;
          queue.removeFront;
          foreach(nei; invAdj[node])
            {
              if (!visited[nei])
                {
                  visited[nei] = true;
                  dist[p][nei] = dist[p][node] + 1;
                  queue.insertBack(nei);
                }
            }
        }
    }
  foreach(i, ai; a)
    write(dist[(ai + 1) % 2][i], " ");
  writeln;
}

// INPUT

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

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

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA!int;
	auto ans = new int[](n);
	ans[] = int.max;
	auto edges = new int[][](n);
	foreach (i; 0..n)
	{
		auto l = i - a[i];
		auto r = i + a[i];
		int u = -1, v = -1;
		if (l >= 0)
		{
			if (a[i] % 2 != a[l] % 2)
				ans[i] = 1;
			else
				u = l;
		}
		if (r < n)
		{
			if (a[i] % 2 != a[r] % 2)
				ans[i] = 1;
			else
				v = r;
		}
		if (ans[i] != 1)
		{
			if (u != -1)
				edges[u] ~= i;
			if (v != -1)
				edges[v] ~= i;
			if (u == -1 && v == -1)
				ans[i] = -1;
		}
	}

	int[][] open;
	foreach (i; 0..n)
	{
		if (ans[i] == 1)
		{
			open ~= [i, 1];
		}
	}

	while (!open.empty)
	{
		auto nd = open.front; open.popFront;
		auto from = nd[0];
		auto c = nd[1];
		foreach (to; edges[from])
		{
			if (c+1 < ans[to])
			{
				ans[to] = c + 1;
				open ~= [to, c + 1];
			}
		}
	}

	foreach (ref e; ans)
		if (e == int.max)
			e = -1;

	ans.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA!int;
	auto ans = new int[](n);
	ans[] = int.max;
	auto edges = new int[][](n);
	foreach (i; 0..n)
	{
		auto l = i - a[i];
		auto r = i + a[i];
		int u = -1, v = -1;
		if (l >= 0)
		{
			if (a[i] % 2 != a[l] % 2)
				ans[i] = 1;
			else
				u = l;
		}
		if (r < n)
		{
			if (a[i] % 2 != a[r] % 2)
				ans[i] = 1;
			else
				v = r;
		}
		if (ans[i] != 1)
		{
			if (u != -1)
				edges[u] ~= i;
			if (v != -1)
				edges[v] ~= i;
			if (u == -1 && v == -1)
				ans[i] = -1;
		}
	}

	int[][] open;
	foreach (i; 0..n)
	{
		if (ans[i] == 1)
		{
			open ~= [i, 1];
		}
	}

	while (!open.empty)
	{
		auto nd = open.front; open.popFront;
		auto from = nd[0];
		auto c = nd[1];
		foreach (to; edges[from])
		{
			if (c+1 < ans[to])
			{
				ans[to] = c + 1;
				open ~= [to, c + 1];
			}
		}
	}

	ans.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}


There are two sisters Alice and Betty. You have $$$n$$$ candies. You want to distribute these $$$n$$$ candies between two sisters in such a way that:  Alice will get $$$a$$$ ($$$a > 0$$$) candies;  Betty will get $$$b$$$ ($$$b > 0$$$) candies;  each sister will get some integer number of candies;  Alice will get a greater amount of candies than Betty (i.e. $$$a > b$$$);  all the candies will be given to one of two sisters (i.e. $$$a+b=n$$$). Your task is to calculate the number of ways to distribute exactly $$$n$$$ candies between sisters in a way described above. Candies are indistinguishable.Formally, find the number of ways to represent $$$n$$$ as the sum of $$$n=a+b$$$, where $$$a$$$ and $$$b$$$ are positive integers and $$$a>b$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^9$$$) — the number of candies you have.

For each test case, print the answer — the number of ways to distribute exactly $$$n$$$ candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print $$$0$$$.

NoteFor the test case of the example, the $$$3$$$ possible ways to distribute candies are:  $$$a=6$$$, $$$b=1$$$;  $$$a=5$$$, $$$b=2$$$;  $$$a=4$$$, $$$b=3$$$.

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!long;
        writeln((N+1)/2-1);
    }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		writeln ((n - 1) / 2);
	}
}

import std.stdio;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		readf(" %d", &a);
		if (a==1)
		{
			writeln(0);
		}
		else
		{
			if (a%2==0)
			{
				writeln(a/2-1);
			}
			else
			{
				writeln(a/2);
			}
		}
	}
	return 0;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD;
		if (n <= 2) continue;

		auto half = n / 2 - (n % 2 == 0 ? 1 : 0);
		ans[ti] = half;
	}

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


Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.The game is played on a tree having n nodes, numbered from 1 to n. Each node i has an initial value initi, which is either 0 or 1. The root of the tree is node 1.One can perform several (possibly, zero) operations on the tree during the game. The only available type of operation is to pick a node x. Right after someone has picked node x, the value of node x flips, the values of sons of x remain the same, the values of sons of sons of x flips, the values of sons of sons of sons of x remain the same and so on.The goal of the game is to get each node i to have value goali, which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.

The first line contains an integer n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) meaning there is an edge between nodes ui and vi.  The next line contains n integer numbers, the i-th of them corresponds to initi (initi is either 0 or 1). The following line also contains n integer numbers, the i-th number corresponds to goali (goali is either 0 or 1).

In the first line output an integer number cnt, representing the minimal number of operations you perform. Each of the next cnt lines should contain an integer xi, representing that you pick a node xi.

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

void main() {
    int N; scanf("%d\n", &N);
    auto T = new int[][N];
    foreach (i; 0 .. N - 1) {
        int u, v; scanf("%d %d\n", &u, &v);
        u--; v--;
        T[u] ~= v;
        T[v] ~= u;
    }
    auto I = readln.chomp.split(" ").map!(to!int).array;
    auto G = readln.chomp.split(" ").map!(to!int).array;

    auto used = new bool[N];
    int[] ans;
    void dfs(int v, bool a, bool b) {
        if (a) I[v] = !I[v];
        used[v] = true;
        if (I[v] != G[v]) { ans ~= v + 1; a = !a;}
        foreach (next; T[v]) {
            if (used[next]) continue;
            dfs(next, b, a);
        }
    }
    dfs(0, false, false);
    writefln("%d", ans.length);
    writefln("%(%s\n%)", ans);
}

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

struct node {
    bool visited;
    int[] child;
}

__gshared {
    node[] tree;
    int[] diff;
    int answer;
    int[] answerlist;
}

void dfs(int v, bool iseven, bool evenpar, bool oddpar) {
    tree[v].visited = true;
    if (iseven) {
        if (diff[v] != evenpar) {
            answer += 1;
            answerlist ~= v;
            evenpar = !evenpar;
        }
    }
    else {
        if (diff[v] != oddpar) {
            answer += 1;
            answerlist ~= v;
            oddpar = !oddpar;
        }
    }
    foreach (i; tree[v].child) {
        if (!tree[i].visited) dfs(i, !iseven, evenpar, oddpar);
    }
}

int main() {
    debug stdin = File("in.txt", "r");

    int n;
    readf(" %s\n", &n);
    tree.length = n + 1;
    diff.length = n + 1;
    foreach (i; 1 .. n) {
        int u, v;
        readf(" %s %s\n", &u, &v);
        tree[u].child ~= v;
        tree[v].child ~= u;
    }
    foreach (i; 1 .. n + 1) {
        readf(" %s", &diff[i]);
    }
    readf("\n");
    foreach (i; 1 .. n + 1) {
        int b;
        readf(" %s", &b);
        diff[i] = diff[i] ^ b;
    }
    dfs(1, true, false, false);
    writeln(answer);
    foreach (i; answerlist) writeln(i);

    return 0;
}

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 NA = -1;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
		}

		auto q = new int [n];
		foreach (ref x; q)
		{
			readf (" %s", &x);
		}

		int [] ans;

		void recur (int v, int w, int z0, int z1)
		{
			if (z0 ^ (p[v] != q[v]))
			{
				ans ~= v + 1;
				z0 ^= 1;
			}

			foreach (u; a[v])
			{
				if (u != w)
				{
					recur (u, v, z1, z0);
				}
			}
		}

		recur (0, NA, 0, 0);

		writeln (ans.length);
		writefln ("%(%s\n%)", ans);
	}
}

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

void main() {
    int N; scanf("%d\n", &N);
    auto T = new int[][N];
    foreach (i; 0 .. N - 1) {
        int u, v; scanf("%d %d\n", &u, &v);
        u--; v--;
        T[u] ~= v;
        T[v] ~= u;
    }
    auto I = readln.chomp.split(" ").map!(to!int).array;
    auto G = readln.chomp.split(" ").map!(to!int).array;

    auto used = new bool[N];
    int[] ans;
    void dfs(int v, bool a, bool b) {
        if (a) { I[v] = !I[v]; }
        used[v] = true;
        foreach (next; T[v]) {
            if (used[next]) continue;
            if (I[next] == G[next]) {
                dfs(next, b, a);
            } else {
                ans ~= next + 1;
                dfs(next, !b, a);
            }
        }
    }
    dfs(0, I[0] != G[0], false);
    writefln("%d", ans.length);
    writefln("%(%s\n%)", ans);
}

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

void main() {
    int N; scanf("%d\n", &N);
    auto T = new int[][N];
    foreach (i; 0 .. N - 1) {
        int u, v; scanf("%d %d\n", &u, &v);
        u--; v--;
        T[u] ~= v;
        T[v] ~= u;
    }
    auto I = readln.chomp.split(" ").map!(to!int).array;
    auto G = readln.chomp.split(" ").map!(to!int).array;

    auto used = new bool[N];
    int[] ans;
    void dfs(int v, bool a, bool b) {
        foreach (next; T[v]) {
            if (used[next]) continue;
            used[next] = true;
            if (I[next] == G[next]) {
                dfs(next, b, a);
            } else {
                ans ~= next + 1;
                I[next] = !I[next];
                dfs(next, !b, a);
            }
        }
    }
    used[0] = true;
    dfs(0, I[0] != G[0], false);
    writefln("%d", ans.length);
    writefln("%(%s\n%)", ans);
}

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

void main() {
    int N; scanf("%d\n", &N);
    auto T = new int[][N];
    foreach (i; 0 .. N - 1) {
        int u, v; scanf("%d %d\n", &u, &v);
        u--; v--;
        T[u] ~= v;
        T[v] ~= u;
    }
    auto I = readln.chomp.split(" ").map!(to!int).array;
    auto G = readln.chomp.split(" ").map!(to!int).array;

    auto used = new bool[N];
    int[] ans;
    void dfs(int v, bool a, bool b) {
        foreach (next; T[v]) {
            if (used[next]) continue;
            used[next] = true;
            if (I[next] == G[next]) {
                dfs(next, b, a);
            } else {
                ans ~= next + 1;
                I[next] = !I[next];
                dfs(next, !b, a);
            }
        }
    }
    used[0] = true;
    if (I[0] != G[0]) {
        ans ~= 1;
        I[0] = !I[0];
        dfs(0, true, false);
    } else {
        dfs(0, false, false);
    }
    writefln("%d", ans.length);
    writefln("%(%s\n%)", ans);
}

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

void main() {
    int N; scanf("%d\n", &N);
    auto T = new int[][N];
    foreach (i; 0 .. N - 1) {
        int u, v; scanf("%d %d\n", &u, &v);
        u--; v--;
        T[u] ~= v;
        T[v] ~= u;
    }
    auto I = readln.chomp.split(" ").map!(to!int).array;
    auto G = readln.chomp.split(" ").map!(to!int).array;

    auto used = new bool[N];
    int[] ans;
    void dfs(int v, bool a, bool b) {
        if (a) { I[v] = !I[v]; }
        foreach (next; T[v]) {
            if (used[next]) continue;
            used[next] = true;
            if (I[next] == G[next]) {
                dfs(next, b, a);
            } else {
                ans ~= next + 1;
                dfs(next, !b, a);
            }
        }
    }
    used[0] = true;
    dfs(0, I[0] != G[0], false);
    writefln("%d", ans.length);
    writefln("%(%s\n%)", ans);
}

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

struct node {
    int[] child;
}

__gshared {
    node[] tree;
    int[] diff;
    int answer;
    int[] answerlist;
}

void dfs(int v, bool iseven, bool evenpar, bool oddpar) {
    if (iseven) {
        if (diff[v] != evenpar) {
            answer += 1;
            answerlist ~= v;
            evenpar = !evenpar;
        }
    }
    else {
        if (diff[v] != oddpar) {
            answer += 1;
            answerlist ~= v;
            oddpar = !oddpar;
        }
    }
    foreach (i; tree[v].child) dfs(i, !iseven, evenpar, oddpar);
}

int main() {
    debug stdin = File("in.txt", "r");

    int n;
    readf(" %s\n", &n);
    tree.length = n + 1;
    diff.length = n + 1;
    foreach (i; 1 .. n) {
        int u, v;
        readf(" %s %s\n", &u, &v);
        int p = min(u, v);
        int c = max(u, v);
        tree[p].child ~= c;
    }
    foreach (i; 1 .. n + 1) {
        readf(" %s", &diff[i]);
    }
    readf("\n");
    foreach (i; 1 .. n + 1) {
        int b;
        readf(" %s", &b);
        diff[i] = diff[i] ^ b;
    }
    dfs(1, true, false, false);
    writeln(answer);
    foreach (i; answerlist) writeln(i);

    return 0;
}

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

struct node {
    int[] child;
}

__gshared node[] tree;
__gshared int[] diff;
__gshared int answer;
__gshared int[] answerlist;

void dfs(int v, bool iseven, bool evenpar, bool oddpar) {
    if (iseven) {
        if (diff[v] != evenpar) {
            answer += 1;
            answerlist ~= v;
            evenpar = !evenpar;
        }
    }
    else {
        if (diff[v] != oddpar) {
            answer += 1;
            answerlist ~= v;
            oddpar = !oddpar;
        }
    }
    foreach (i; tree[v].child) dfs(i, !iseven, evenpar, oddpar);
}

int main() {
    debug stdin = File("in.txt", "r");

    int n;
    readf(" %s\n", &n);
    tree.length = n + 1;
    diff.length = n + 1;
    foreach (i; 1 .. n) {
        int u, v;
        readf(" %s %s\n", &u, &v);
        int p = min(u, v);
        int c = max(u, v);
        tree[p].child ~= c;
    }
    foreach (i; 1 .. n + 1) {
        readf(" %s", &diff[i]);
    }
    readf("\n");
    foreach (i; 1 .. n + 1) {
        int b;
        readf(" %s", &b);
        diff[i] = diff[i] ^ b;
    }
    dfs(1, true, false, false);
    writeln(answer);
    foreach (i; answerlist) writeln(i);

    return 0;
}

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 NA = -1;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
		}

		auto q = new int [n];
		foreach (ref x; q)
		{
			readf (" %s", &x);
		}

		int [] ans;

		void recur (int v, int w, int z)
		{
			if (z ^ (p[v] != q[v]))
			{
				ans ~= v + 1;
				z ^= 1;
			}

			foreach (u; a[v])
			{
				if (u != w)
				{
					recur (u, v, z);
				}
			}
		}

		recur (0, NA, 0);

		writeln (ans.length);
		writefln ("%(%s\n%)", ans);
	}
}


In the snake exhibition, there are $$$n$$$ rooms (numbered $$$0$$$ to $$$n - 1$$$) arranged in a circle, with a snake in each room. The rooms are connected by $$$n$$$ conveyor belts, and the $$$i$$$-th conveyor belt connects the rooms $$$i$$$ and $$$(i+1) \bmod n$$$. In the other words, rooms $$$0$$$ and $$$1$$$, $$$1$$$ and $$$2$$$, $$$\ldots$$$, $$$n-2$$$ and $$$n-1$$$, $$$n-1$$$ and $$$0$$$ are connected with conveyor belts.The $$$i$$$-th conveyor belt is in one of three states:  If it is clockwise, snakes can only go from room $$$i$$$ to $$$(i+1) \bmod n$$$.  If it is anticlockwise, snakes can only go from room $$$(i+1) \bmod n$$$ to $$$i$$$.  If it is off, snakes can travel in either direction.   Above is an example with $$$4$$$ rooms, where belts $$$0$$$ and $$$3$$$ are off, $$$1$$$ is clockwise, and $$$2$$$ is anticlockwise.Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$): the number of test cases. The description of the test cases follows.   The first line of each test case description contains a single integer $$$n$$$ ($$$2 \le n \le 300\,000$$$): the number of rooms.  The next line of each test case description contains a string $$$s$$$ of length $$$n$$$, consisting of only '<', '>' and '-'.   If $$$s_{i} = $$$ '>', the $$$i$$$-th conveyor belt goes clockwise.  If $$$s_{i} = $$$ '<', the $$$i$$$-th conveyor belt goes anticlockwise.  If $$$s_{i} = $$$ '-', the $$$i$$$-th conveyor belt is off.  It is guaranteed that the sum of $$$n$$$ among all test cases does not exceed $$$300\,000$$$.

For each test case, output the number of returnable rooms.

NoteIn the first test case, all rooms are returnable except room $$$2$$$. The snake in the room $$$2$$$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		if (s.all !(q{a != '>'}) || s.all !(q{a != '<'}))
		{
			writeln (n);
			continue;
		}
		int res = n;
		foreach (i; 0..n)
		{
			if (s[i] != '-' && s[(i + 1) % n] != '-')
			{
				res -= 1;
			}
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

		long cnt0, cnt1;
		foreach (i; 0..n)
		{
			if (s[i] == '<')
				++cnt0;
			else if (s[i] == '>')
				++cnt1;
			if (s[(n+i-1)%n] == '-' || s[i] == '-')
				++ans[ti];
		}

		if (cnt0 == 0 || cnt1 == 0)
			ans[ti] = n;
	}

	foreach (e; ans)
		writeln(e);

	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		int res = n;
		if (s.all !(q{a == '>'}) || s.all !(q{a == '<'}))
		{
			writeln (n);
			continue;
		}
		foreach (i; 0..n)
		{
			if (s[i] != '-' && s[(i + 1) % n] != '-')
			{
				res -= 1;
			}
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

		ans[ti] = n;
		foreach (i; 0..n)
		{
			if (s[(n+i-1)%n] == '<' && s[i] == '>')
				--ans[ti];
			else if (s[(n+i-1)%n] == '>' && s[i] == '<')
				--ans[ti];
		}

	}

	foreach (e; ans)
		writeln(e);

	stdout.flush;
	debug readln;
}


There are $$$n$$$ shovels in the nearby shop. The $$$i$$$-th shovel costs $$$a_i$$$ bourles.Misha has to buy exactly $$$k$$$ shovels. Each shovel can be bought no more than once.Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.There are also $$$m$$$ special offers in the shop. The $$$j$$$-th of them is given as a pair $$$(x_j, y_j)$$$, and it means that if Misha buys exactly $$$x_j$$$ shovels during one purchase then $$$y_j$$$ most cheapest of them are for free (i.e. he will not pay for $$$y_j$$$ most cheapest shovels during the current purchase).Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).Your task is to calculate the minimum cost of buying $$$k$$$ shovels, if Misha buys them optimally.

The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$$$) — the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the cost of the $$$i$$$-th shovel. The next $$$m$$$ lines contain special offers. The $$$j$$$-th of them is given as a pair of integers $$$(x_i, y_i)$$$ ($$$1 \le y_i \le x_i \le n$$$) and means that if Misha buys exactly $$$x_i$$$ shovels during some purchase, then he can take $$$y_i$$$ most cheapest of them for free.

Print one integer — the minimum cost of buying $$$k$$$ shovels if Misha buys them optimally.

NoteIn the first example Misha can buy shovels on positions $$$1$$$ and $$$4$$$ (both with costs $$$2$$$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $$$3$$$ and $$$6$$$ (with costs $$$4$$$ and $$$3$$$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $$$7$$$ with cost $$$1$$$. So the total cost is $$$4 + 2 + 1 = 7$$$.In the second example Misha can buy shovels on positions $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$8$$$ (costs are $$$6$$$, $$$8$$$, $$$5$$$, $$$1$$$ and $$$2$$$) and get three cheapest (with costs $$$5$$$, $$$1$$$ and $$$2$$$) for free. And then he can buy shovels on positions $$$6$$$, $$$7$$$ and $$$9$$$ (all with costs $$$1$$$) without using any special offers. So the total cost is $$$6 + 8 + 1 + 1 + 1 = 17$$$.In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $$$17$$$.

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto A = readln.split.map!(to!long).array;
    auto X = M.iota.map!(_ => readln.split.map!(to!int).array).array;

    A = A.sort().take(K).array;
    auto B = new long[](K+1);
    foreach (i; 0..K) B[i+1] = B[i] + A[i];

    auto dp = new long[](K+1);
    dp[] = 1L << 59;
    dp[0] = 0;

    foreach (i; 0..K) {
        dp[i+1] = min(dp[i+1], dp[i] + A[i]);
        foreach (x; X) {
            if (i + x[0] <= K) {
                dp[i+x[0]] = min(dp[i+x[0]], dp[i] + B[i+x[0]] - B[i+x[1]]);
            }
        }
    }

    dp[K].writeln;
}

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; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){

	long n = read!long;
	long m = read!long;
	int k = read!int;
	long[] as = read!long(n);
	log("as:", as);

	as.sort();
	as = as[0 .. k];
	log("as:", as);

	int[] qs = new int[](k + 10);
	foreach(i; 0 .. m){
		int x = read!int;
		int y = read!int;
		if(x > k) continue;
		if(qs[x] < y) qs[x] = y;
	}
	log("qs:", qs);

	long[] us = new long[](k + 10);
	us[0] = 0;
	foreach(i; 1 .. k + 1){
		foreach(j; 0 .. i){
			int y = qs[i - j];
			long u = us[j] + as[j .. j + y].sum;
			if(u > us[i]) us[i] = u;
			log("i:", i, "j:", j, "u:", u);
		}
	}
	log("us:", us);

	(as.sum - us[k]).writeln;

}