Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation $$$p$$$ of length $$$n$$$, and Skynyrd bought an array $$$a$$$ of length $$$m$$$, consisting of integers from $$$1$$$ to $$$n$$$. Lynyrd and Skynyrd became bored, so they asked you $$$q$$$ queries, each of which has the following form: "does the subsegment of $$$a$$$ from the $$$l$$$-th to the $$$r$$$-th positions, inclusive, have a subsequence that is a cyclic shift of $$$p$$$?" Please answer the queries.A permutation of length $$$n$$$ is a sequence of $$$n$$$ integers such that each integer from $$$1$$$ to $$$n$$$ appears exactly once in it.A cyclic shift of a permutation $$$(p_1, p_2, \ldots, p_n)$$$ is a permutation $$$(p_i, p_{i + 1}, \ldots, p_{n}, p_1, p_2, \ldots, p_{i - 1})$$$ for some $$$i$$$ from $$$1$$$ to $$$n$$$. For example, a permutation $$$(2, 1, 3)$$$ has three distinct cyclic shifts: $$$(2, 1, 3)$$$, $$$(1, 3, 2)$$$, $$$(3, 2, 1)$$$.A subsequence of a subsegment of array $$$a$$$ from the $$$l$$$-th to the $$$r$$$-th positions, inclusive, is a sequence $$$a_{i_1}, a_{i_2}, \ldots, a_{i_k}$$$ for some $$$i_1, i_2, \ldots, i_k$$$ such that $$$l \leq i_1 < i_2 < \ldots < i_k \leq r$$$.

The first line contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 \le n, m, q \le 2 \cdot 10^5$$$) — the length of the permutation $$$p$$$, the length of the array $$$a$$$ and the number of queries. The next line contains $$$n$$$ integers from $$$1$$$ to $$$n$$$, where the $$$i$$$-th of them is the $$$i$$$-th element of the permutation. Each integer from $$$1$$$ to $$$n$$$ appears exactly once. The next line contains $$$m$$$ integers from $$$1$$$ to $$$n$$$, the $$$i$$$-th of them is the $$$i$$$-th element of the array $$$a$$$. The next $$$q$$$ lines describe queries. The $$$i$$$-th of these lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le m$$$), meaning that the $$$i$$$-th query is about the subsegment of the array from the $$$l_i$$$-th to the $$$r_i$$$-th positions, inclusive.

Print a single string of length $$$q$$$, consisting of $$$0$$$ and $$$1$$$, the digit on the $$$i$$$-th positions should be $$$1$$$, if the subsegment of array $$$a$$$ from the $$$l_i$$$-th to the $$$r_i$$$-th positions, inclusive, contains a subsequence that is a cyclic shift of $$$p$$$, and $$$0$$$ otherwise.

NoteIn the first example the segment from the $$$1$$$-st to the $$$5$$$-th positions is $$$1, 2, 3, 1, 2$$$. There is a subsequence $$$1, 3, 2$$$ that is a cyclic shift of the permutation. The subsegment from the $$$2$$$-nd to the $$$6$$$-th positions also contains a subsequence $$$2, 1, 3$$$ that is equal to the permutation. The subsegment from the $$$3$$$-rd to the $$$5$$$-th positions is $$$3, 1, 2$$$, there is only one subsequence of length $$$3$$$ ($$$3, 1, 2$$$), but it is not a cyclic shift of the permutation.In the second example the possible cyclic shifts are $$$1, 2$$$ and $$$2, 1$$$. The subsegment from the $$$1$$$-st to the $$$2$$$-nd positions is $$$1, 1$$$, its subsequences are not cyclic shifts of the permutation. The subsegment from the $$$2$$$-nd to the $$$3$$$-rd positions is $$$1, 2$$$, it coincides with the permutation. The subsegment from the $$$3$$$ to the $$$4$$$ positions is $$$2, 2$$$, its subsequences are not cyclic shifts of the permutation.

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

int [] shift (int [] a, int [] b)
{
	auto n = a.length.to !(int);
	auto res = new int [n];
	foreach (i; 0..n)
	{
		res[i] = a[b[i]];
	}
	return res;
}

int [] powFast (int [] a, int b)
{
	auto res = a.length.to !(int).iota.array;
	while (b)
	{
		if (b & 1)
		{
			res = shift (res, a);
		}
		a = shift (a, a);
		b >>= 1;
	}
	return res;
}

void main ()
{
	int n, m, q;
	while (readf (" %s %s %s", &n, &m, &q) > 0)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto a = readln.splitter.map !(to !(int)).array;
		p[] -= 1;
		a[] -= 1;

		auto r = new int [n];
		foreach (i, v; p)
		{
			r[v] = i;
		}

		auto b = [-1] ~ a.map !(x => r[x]).array ~ [-1];

		auto next = iota (m + 2).array;
		auto waitList = new int [] [n];
		foreach (j; 1..m + 1)
		{
			int x = b[j];
			int y = (x + n - 1) % n;
			foreach (pos; waitList[y])
			{
				next[pos] = j;
			}
			waitList[y].length = 0;
			waitList[x] ~= j;
		}
		foreach (y; 0..n)
		{
			foreach (pos; waitList[y])
			{
				next[pos] = m + 1;
			}
			waitList[y].length = 0;
		}

		auto f = powFast (next, n - 1);
		foreach_reverse (i; 0..m + 1)
		{
			f[i] = min (f[i], f[i + 1]);
		}

		string res;
		foreach (j; 0..q)
		{
			int lo, hi;
			readf (" %s %s", &lo, &hi);
			res ~= "01"[f[lo] <= hi];
		}
		writeln (res);
	}
}


Ilya lives in a beautiful city of Chordalsk.There are $$$n$$$ houses on the street Ilya lives, they are numerated from $$$1$$$ to $$$n$$$ from left to right; the distance between every two neighboring houses is equal to $$$1$$$ unit. The neighboring houses are $$$1$$$ and $$$2$$$, $$$2$$$ and $$$3$$$, ..., $$$n-1$$$ and $$$n$$$. The houses $$$n$$$ and $$$1$$$ are not neighboring.The houses are colored in colors $$$c_1, c_2, \ldots, c_n$$$ so that the $$$i$$$-th house is colored in the color $$$c_i$$$. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.Ilya wants to select two houses $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$, and they have different colors: $$$c_i \neq c_j$$$. He will then walk from the house $$$i$$$ to the house $$$j$$$ the distance of $$$(j-i)$$$ units.Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.Help Ilya, find this maximum possible distance.

The first line contains a single integer $$$n$$$ ($$$3 \leq n \leq 300\,000$$$) — the number of cities on the street. The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq n$$$) — the colors of the houses. It is guaranteed that there is at least one pair of indices $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$ and $$$c_i \neq c_j$$$.

Print a single integer — the maximum possible distance Ilya can walk.

NoteIn the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of $$$5-1 = 4$$$ units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of $$$1$$$ unit.In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of $$$7-3 = 4$$$ units.

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int res = 0;
		foreach (i; 0..n)
		{
			if (a[i] != a[0])
			{
				res = max (res, abs (i - 0));
			}
		}
		foreach (i; 0..n)
		{
			if (a[i] != a[n - 1])
			{
				res = max (res, abs (i - (n - 1)));
			}
		}
		writeln (res);
	}
}

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

  int n;
  rd(n);
  auto a = readln.split.to!(int[]);

  if (a[0] != a[$ - 1]) {
    writeln(n - 1);
    return;
  }
  int mx = 1;
  foreach (i; 1 .. n) {
    if (a[0] != a[i]) {
      mx = max(mx, i, n - i - 1);
    }
  }
  writeln(mx);

}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

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

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

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

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

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

int N;
int[] C;

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

      int l, r;
      for (l = 0; l < N && C[0] == C[l]; ++l) {}
      for (r = N - 1; r >= 0 && C[N - 1] == C[r]; --r) {}
      assert(l < N);
      assert(r >= 0);

      int ans;
      foreach (i; 0 .. N) {
        chmax(ans, abs(i - ((C[0] == C[i]) ? l : 0)));
        chmax(ans, abs(i - ((C[N - 1] == C[i]) ? r : (N - 1))));
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:There are $$$n$$$ new cities located in Prefecture X. Cities are numbered from $$$1$$$ to $$$n$$$. City $$$i$$$ is located $$$x_i$$$ km North of the shrine and $$$y_i$$$ km East of the shrine. It is possible that $$$(x_i, y_i) = (x_j, y_j)$$$ even when $$$i \ne j$$$.Shichikuji must provide electricity to each city either by building a power station in that city, or by making a connection between that city and another one that already has electricity. So the City has electricity if it has a power station in it or it is connected to a City which has electricity by a direct connection or via a chain of connections.     Building a power station in City $$$i$$$ will cost $$$c_i$$$ yen;     Making a connection between City $$$i$$$ and City $$$j$$$ will cost $$$k_i + k_j$$$ yen per km of wire used for the connection. However, wires can only go the cardinal directions (North, South, East, West). Wires can cross each other. Each wire must have both of its endpoints in some cities. If City $$$i$$$ and City $$$j$$$ are connected by a wire, the wire will go through any shortest path from City $$$i$$$ to City $$$j$$$. Thus, the length of the wire if City $$$i$$$ and City $$$j$$$ are connected is $$$|x_i - x_j| + |y_i - y_j|$$$ km. Shichikuji wants to do this job spending as little money as possible, since according to her, there isn't really anything else in the world other than money. However, she died when she was only in fifth grade so she is not smart enough for this. And thus, the new resident deity asks for your help.And so, you have to provide Shichikuji with the following information: minimum amount of yen needed to provide electricity to all cities, the cities in which power stations will be built, and the connections to be made.If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.

First line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$) — the number of cities. Then, $$$n$$$ lines follow. The $$$i$$$-th line contains two space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^6$$$) and $$$y_i$$$ ($$$1 \leq y_i \leq 10^6$$$) — the coordinates of the $$$i$$$-th city. The next line contains $$$n$$$ space-separated integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the cost of building a power station in the $$$i$$$-th city. The last line contains $$$n$$$ space-separated integers $$$k_1, k_2, \dots, k_n$$$ ($$$1 \leq k_i \leq 10^9$$$).

In the first line print a single integer, denoting the minimum amount of yen needed. Then, print an integer $$$v$$$ — the number of power stations to be built. Next, print $$$v$$$ space-separated integers, denoting the indices of cities in which a power station will be built. Each number should be from $$$1$$$ to $$$n$$$ and all numbers should be pairwise distinct. You can print the numbers in arbitrary order. After that, print an integer $$$e$$$ — the number of connections to be made. Finally, print $$$e$$$ pairs of integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$, $$$a \ne b$$$), denoting that a connection between City $$$a$$$ and City $$$b$$$ will be made. Each unordered pair of cities should be included at most once (for each $$$(a, b)$$$ there should be no more $$$(a, b)$$$ or $$$(b, a)$$$ pairs). You can print the pairs in arbitrary order. If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.

NoteFor the answers given in the samples, refer to the following diagrams (cities with power stations are colored green, other cities are colored blue, and wires are colored red):For the first example, the cost of building power stations in all cities is $$$3 + 2 + 3 = 8$$$. It can be shown that no configuration costs less than 8 yen.For the second example, the cost of building a power station in City 2 is 2. The cost of connecting City 1 and City 2 is $$$2 \cdot (3 + 2) = 10$$$. The cost of connecting City 2 and City 3 is $$$3 \cdot (2 + 3) = 15$$$. Thus the total cost is $$$2 + 10 + 15 = 27$$$. It can be shown that no configuration costs less than 27 yen.

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

class UnionFind {
    import std.range;
    import std.algorithm;

    private size_t[] par, rank;

    ///
    this(size_t n)
    {
        rank.length = n;
        rank[] = 1;
        par = iota(0,n).array;
    }

    ///
    size_t find(size_t a) nothrow pure @nogc @safe
    {
        if (par[a] == a) return a;
        else return par[a] = find(par[a]);
    }

    ///
    void unite(size_t a, size_t b) nothrow pure @nogc @safe
    {
        a = find(a), b = find(b);

        if (a == b) return;

        if (rank[a] < rank[b]) {
            par[a] = b;
        } else {
            par[b] = a;
            if (rank[a] == rank[b]) {
                rank[a]++;
            }
        }
    }

    ///
    bool same(size_t a, size_t b) nothrow pure @nogc @safe
    {
        return find(a) == find(b);
    }
}

alias Edge = Tuple!(long,int,int);

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

    long[] x,y,c,k;
    x.length = y.length = c.length = k.length = n+1;

    foreach (i; 1 .. n+1) {
        readf!" %s %s "(x[i],y[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(c[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(k[i]);
    }

    Edge[] edges;
    edges.reserve(n*n+n);

    foreach (i; 1 .. n+1) {
        edges ~= tuple(c[i], 0, i);
    }

    foreach (i; 1 .. n+1) {
        foreach (j; i+1 .. n+1) {
            edges ~= tuple((k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j])), i, j);
        }
    }

    edges.sort();

    auto uf = new UnionFind(n+1);

    long total = 0;
    int[] es;
    Tuple!(int,int)[] cs;
    es.reserve(n);
    cs.reserve(n);

    foreach (e; edges) {
        if (uf.same(e[1], e[2])) continue;

        total += e[0];
        uf.unite(e[1], e[2]);

        if (e[1] == 0)
            es ~= e[2];
        else
            cs ~= tuple(e[1], e[2]);
    }

    writeln(total);

    writeln(es.length);
    foreach (e; es) {
        writef!"%s "(e);
    }
    writeln();

    writeln(cs.length);
    foreach (v; cs) {
        writefln!"%s %s"(v[0],v[1]);
    }
}

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

class UnionFind {
    import std.range;
    import std.algorithm;

    private size_t[] par, rank;

    ///
    this(size_t n)
    {
        rank.length = n;
        rank[] = 1;
        par = iota(0,n).array;
    }

    ///
    size_t find(size_t a) nothrow pure @nogc @safe
    {
        if (par[a] == a) return a;
        else return par[a] = find(par[a]);
    }

    ///
    void unite(size_t a, size_t b) nothrow pure @nogc @safe
    {
        a = find(a), b = find(b);

        if (a == b) return;

        if (rank[a] < rank[b]) {
            par[a] = b;
        } else {
            par[b] = a;
            if (rank[a] == rank[b]) {
                rank[a]++;
            }
        }
    }

    ///
    bool same(size_t a, size_t b) nothrow pure @nogc @safe
    {
        return find(a) == find(b);
    }
}

alias Edge = Tuple!(long,int,int);

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

    long[] x,y,c,k;
    x.length = y.length = c.length = k.length = n+1;

    foreach (i; 1 .. n+1) {
        readf!" %s %s "(x[i],y[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(c[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(k[i]);
    }

    Array!Edge edges;

    foreach (i; 1 .. n+1) {
        edges ~= tuple(c[i], 0, i);
    }

    foreach (i; 1 .. n+1) {
        foreach (j; i+1 .. n+1) {
            edges ~= tuple((k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j])), i, j);
        }
    }

    edges[].sort();

    auto uf = new UnionFind(n+1);

    long total = 0;
    Array!int es;
    Array!(Tuple!(int,int)) cs;

    foreach (e; edges) {
        if (uf.same(e[1], e[2])) continue;

        total += e[0];
        uf.unite(e[1], e[2]);

        if (e[1] == 0)
            es ~= e[2];
        else
            cs ~= tuple(e[1], e[2]);
    }

    writeln(total);

    writeln(es.length);
    foreach (e; es) {
        writef!"%s "(e);
    }
    writeln();

    writeln(cs.length);
    foreach (v; cs) {
        writefln!"%s %s"(v[0],v[1]);
    }
}

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

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

    long[] x,y;
    x.length = y.length = n+1;

    foreach (i; 0 .. n) {
        readf!" %s %s "(x[i],y[i]);
    }

    long[] c = readln.strip.split.map!(to!long).array;
    long[] k = readln.strip.split.map!(to!long).array;

    int[] b = new int[n];
    b[] = -1;

    long total;
    int[] es;
    Tuple!(int,int)[] cs;

    foreach (_; 0 .. n) {
        auto i = minIndex(c).to!int;
        total += c[i];

        if (b[i] >= 0) {
            cs ~= tuple(i+1,b[i]+1);
        } else {
            es ~= i+1;
        }

        foreach (j; 0 .. n) {
            if (c[j] != long.max) {
                long v = (k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j]));
                if (v < c[j]) {
                    c[j] = v;
                    b[j] = i;
                }
            }
        }
        c[i] = long.max;
    }

    writeln(total);

    writeln(es.length);
    writefln!"%(%s %)"(es);

    writeln(cs.length);
    foreach (p; cs)
        writefln!"%s %s"(p[0],p[1]);
}

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

alias Edge = Tuple!(long,int,int);

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

    long[] x,y;
    x.length = y.length = n+1;

    foreach (i; 1 .. n+1) {
        auto xy = readln.strip.split.map!(to!long);
        x[i] = xy[0], y[i] = xy[1];
    }

    long[] c = [0L] ~ readln.strip.split.map!(to!long).array;
    long[] k = [0L] ~ readln.strip.split.map!(to!long).array;

    Edge[] edges;

    alias pair = Tuple!(long,int);

    long total = 0;
    int[] es;
    Tuple!(int,int)[] cs;

    bool[] use = new bool[n+1];
    auto q = BinaryHeap!(pair[],"b < a")([]);
    foreach (i, v; c) {
        q.insert(tuple(v,to!int(i)));
    }

    use[0] = true;
    int[] b = new int[n+1];
    b[] = -1;

    int v = 0;
    while (!q.empty && v < n) {
        auto p = q.front; q.removeFront();

        int i = p[1];
        if (c[i] < p[0] || use[i]) continue;

        use[i] = true;

        if (b[i] >= 0)
            cs ~= tuple(i,b[i]);
        else
            es ~= i;

        v++;

        total += p[0];

        foreach (j; 1 .. n+1) {
            long newCost = (k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j]));
            if (c[j] > newCost) {
                c[j] = newCost;
                q.insert(tuple(newCost, j));
                b[j] = i;
            }
        }
    }

    writeln(total);

    writeln(es.length);
    writefln!"%(%s %)"(es);

    writeln(cs.length);
    foreach (p; cs) {
        writefln!"%s %s"(p[0],p[1]);
    }
}

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

class UnionFind {
    import std.range;
    import std.algorithm;

    private size_t[] par, rank;

    ///
    this(size_t n)
    {
        rank.length = n;
        rank[] = 1;
        par = iota(0,n).array;
    }

    ///
    size_t find(size_t a) nothrow pure @nogc @safe
    {
        if (par[a] == a) return a;
        else return par[a] = find(par[a]);
    }

    ///
    void unite(size_t a, size_t b) nothrow pure @nogc @safe
    {
        a = find(a), b = find(b);

        if (a == b) return;

        if (rank[a] < rank[b]) {
            par[a] = b;
        } else {
            par[b] = a;
            if (rank[a] == rank[b]) {
                rank[a]++;
            }
        }
    }

    ///
    bool same(size_t a, size_t b) nothrow pure @nogc @safe
    {
        return find(a) == find(b);
    }
}

alias Edge = Tuple!(long,int,int);

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

    long[] x,y;
    x.length = y.length = n+1;

    foreach (i; 1 .. n+1) {
        auto xy = readln.strip.split.map!(to!long);
        x[i] = xy[0], y[i] = xy[1];
    }

    long[] c = [0L] ~ readln.strip.split.map!(to!long).array;
    long[] k = [0L] ~ readln.strip.split.map!(to!long).array;

    Edge[] edges;

    foreach (i; 1 .. n+1) {
        edges ~= tuple(c[i], 0, i);
    }

    foreach (i; 1 .. n+1) {
        foreach (j; i+1 .. n+1) {
            edges ~= tuple((k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j])), i, j);
        }
    }

    edges.sort();

    auto uf = new UnionFind(n+1);

    long total = 0;
    int[] es;
    Tuple!(int,int)[] cs;

    foreach (e; edges) {
        if (uf.same(e[1], e[2])) continue;

        total += e[0];
        uf.unite(e[1], e[2]);

        if (e[1] == 0)
            es ~= e[2];
        else
            cs ~= tuple(e[1], e[2]);
    }

    writeln(total);

    writeln(es.length);
    foreach (e; es) {
        writef!"%s "(e);
    }
    writeln();

    writeln(cs.length);
    foreach (v; cs) {
        writefln!"%s %s"(v[0],v[1]);
    }
}

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

class UnionFind {
    import std.range;
    import std.algorithm;

    private size_t[] par, rank;

    ///
    this(size_t n)
    {
        rank.length = n;
        rank[] = 1;
        par = iota(0,n).array;
    }

    ///
    size_t find(size_t a) nothrow pure @nogc @safe
    {
        if (par[a] == a) return a;
        else return par[a] = find(par[a]);
    }

    ///
    void unite(size_t a, size_t b) nothrow pure @nogc @safe
    {
        a = find(a), b = find(b);

        if (a == b) return;

        if (rank[a] < rank[b]) {
            par[a] = b;
        } else {
            par[b] = a;
            if (rank[a] == rank[b]) {
                rank[a]++;
            }
        }
    }

    ///
    bool same(size_t a, size_t b) nothrow pure @nogc @safe
    {
        return find(a) == find(b);
    }
}

alias Edge = Tuple!(long,int,int);

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

    long[] x,y,c,k;
    x.length = y.length = c.length = k.length = n+1;

    foreach (i; 1 .. n+1) {
        readf!" %s %s "(x[i],y[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(c[i]);
    }

    foreach (i; 1 .. n+1) {
        readf!" %s "(k[i]);
    }

    Edge[] edges;

    foreach (i; 1 .. n+1) {
        edges ~= tuple(c[i], 0, i);
    }

    foreach (i; 1 .. n+1) {
        foreach (j; i+1 .. n+1) {
            edges ~= tuple((k[i] + k[j]) * (abs(x[i] - x[j]) + abs(y[i] - y[j])), i, j);
        }
    }

    edges.sort();

    auto uf = new UnionFind(n+1);

    long total = 0;
    int[] es;
    Tuple!(int,int)[] cs;

    foreach (e; edges) {
        if (uf.same(e[1], e[2])) continue;

        total += e[0];
        uf.unite(e[1], e[2]);

        if (e[1] == 0)
            es ~= e[2];
        else
            cs ~= tuple(e[1], e[2]);
    }

    writeln(total);

    writeln(es.length);
    foreach (e; es) {
        writef!"%s "(e);
    }
    writeln();

    writeln(cs.length);
    foreach (v; cs) {
        writefln!"%s %s"(v[0],v[1]);
    }
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

class Heap(T) {
  private:
    T [] a;
    int [] h;
    int [] g;
    int n;
    int size;
  final void heapify_front (int k) {
    immutable he = h[k];
    int i = k;
    int j = i << 1;
    while (j <= size) {
      if (j < size && a[h[j+1]] < a[h[j]]) {
        j++;
      }
      if (a[h[j]] >= a[he]) {
        break;
      }
      h[i] = h[j];
      g[h[i]] = i;
      i = j;
      j = i << 1;
    }
    if (i != k) {
      h[i] = he;
      g[he] = i;
    }
  }
  final void heapify_back (int k) {
    immutable he = h[k];
    int i = k;
    while (i > 1) {
      int j = i >> 1;
      if (a[he] >= a[h[j]]) {
        break;
      }
      h[i] = h[j];
      g[h[i]] = i;
      i = j;
    }
    if (i != k) {
      h[i] = he;
      g[he] = i;
    }
  }
  final void insert (int i) {
    h[++size] = i;
    g[i] = size;
    heapify_back (size);
  }
  public:
  final void update (int k, T value) in {
    assert (k >= 0 && k < n);
  } body {
    immutable pos = g[k];
    if (pos < 0) {
      a[k] = value;
      insert (k);
    } else if (value < a[k]) {
      a[k] = value;
      heapify_back (pos);
    } else if (value > a[k]) {
      a[k] = value;
      heapify_front (pos);
    }
  }
  final int extract_min () {
    assert (size > 0);
    immutable he = h[1];
    g[he] = -1;
    h[1] = h[size--];
    g[h[1]] = 1;
    if (size) {
      heapify_front (1);
    }
    return he;
  }

  inout(T) opIndex (size_t index) inout {
    return a[index];
  }

  @property
  bool empty () const { return size == 0; }

  this (int n_, T default_value) {
    n = n_;
    size = 0;
    a = new T[n];
    a[] = default_value;
    h = new int[n+1];
    g = new int[n];
    g[] = -1;
  }
}

void main() {
  auto r = new InputReader;
  immutable m = r.next!uint;
  immutable n = m + 1;
  auto x = new int[n];
  auto y = new int[n];
  foreach (i; 1 .. n) {
    x[i] = r.next!uint;
    y[i] = r.next!uint;
  }
  auto c = new int[n];
  foreach (i; 1 .. n) {
    c[i] = r.next!uint;
  }
  auto kt = new int[n];
  foreach (i; 1 .. n) {
    kt[i] = r.next!uint;
  }
  auto h = new Heap!long (n, long.max);
  h.update (0, 0);
  auto p = new int[n];
  auto b = new bool[n];
  p[] = -1;
  while (!h.empty) {
    immutable i = h.extract_min ();
    b[i] = true;
    debug stderr.writefln ("i: %d, h[i]: %d", i+1, h[i]);
    if (i == 0) {
      foreach (j; 1 .. n) {
        assert (!b[j]);
        immutable w = c[j];
        if (w < h[j]) {
          h.update (j, w);
          p[j] = i;
        }
      }
    } else {
      immutable xi = x[i], yi = y[i], ki = kt[i];
      foreach (j; 1 .. n) {
        if (b[j]) {
          continue;
        }
        long w = abs (xi - x[j]) + abs (yi - y[j]);
        w *= ki + kt[j];
        if (w < h[j]) {
          h.update (j, w);
          p[j] = i;
        }
      }
    }
  }
  iota (0, n).map! (i => h[i]).sum.writeln;
  int[] u;
  Tuple!(int, int)[] d;
  foreach (i; 1 .. n) {
    if (p[i] == 0) {
      u ~= i;
    } else {
      d ~= tuple (i.to!int, p[i]);
    }
  }
  writeln (u.length);
  writefln ("%(%s %)", u);
  writeln (d.length);
  foreach (q; d) {
    if (q[0] > q[1]) swap (q[0], q[1]);
    writeln (q[0], ' ', q[1]);
  }

}


Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows. The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.  The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie. The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie. It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.  If there are several possible answers print any of them.

NoteIn the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.

import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

void readlnTo(T...)(ref T t) {
    auto s = readln().split();
    assert(s.length == t.length);
    foreach(ref ti; t) {
        ti = s[0].to!(typeof(ti));
        s = s[1..$];
    }
}

const real eps = 1e-10;

void main(){
    auto n = readInt();
    auto a = readInts();
    auto m = readInt();
    auto b = readInts();
    auto c = readInts();
    int[int] lc;
    foreach(ai; a) {
        lc[ai] = lc.get(ai, 0) + 1;
    }
    auto idx = iota(m).array;
    zip(b.map!(x => lc.get(x, 0)).array, c.map!(x => lc.get(x,0)).array, idx).sort!((x, y) => x > y);
    writeln(idx[0]+1);
}


Alice and Bob are playing a fun game of tree tag.The game is played on a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Recall that a tree on $$$n$$$ vertices is an undirected, connected graph with $$$n-1$$$ edges.Initially, Alice is located at vertex $$$a$$$, and Bob at vertex $$$b$$$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $$$da$$$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $$$db$$$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them.If after at most $$$10^{100}$$$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins.Determine the winner if both players play optimally.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains five integers $$$n,a,b,da,db$$$ ($$$2\le n\le 10^5$$$, $$$1\le a,b\le n$$$, $$$a\ne b$$$, $$$1\le da,db\le n-1$$$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $$$n-1$$$ lines describe the edges of the tree. The $$$i$$$-th of these lines contains two integers $$$u$$$, $$$v$$$ ($$$1\le u, v\le n, u\ne v$$$), denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.

For each test case, output a single line containing the winner of the game: "Alice" or "Bob".

NoteIn the first test case, Alice can win by moving to vertex $$$1$$$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move.  In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $$$1$$$ or $$$6$$$ is farthest from Alice.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, a, b, da, db;
		readf !(" %s %s %s %s %s") (n, a, b, da, db);
		a -= 1;
		b -= 1;
		auto adj = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
			adj[v] ~= u;
		}

		auto d = new int [n];

		void recur (int v, int p, int dist)
		{
			d[v] = dist;
			foreach (u; adj[v])
			{
				if (u != p)
				{
					recur (u, v, dist + 1);
				}
			}
		}

		recur (b, -1, 0);
		if (d[a] <= da)
		{
			writeln ("Alice");
			continue;
		}

		auto c = to !(int) (d.length - d.maxPos.length);

		recur (c, -1, 0);
		auto diameter = d.maxElement;
		auto canCatch = (da * 2 >= db);
		auto canCenter = (da * 2 >= diameter);
		writeln ((canCatch || canCenter) ? "Alice" : "Bob");
	}
}

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

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

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

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

int N, A, B, C, D;
int[] U, V;

int[][] G;
int[] dep;

void dfs(int u, int p) {
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  foreach (i; G[u]) {
    const v = U[i] ^ V[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt();
        A = readInt() - 1;
        B = readInt() - 1;
        C = readInt();
        D = readInt();
        U = new int[N - 1];
        V = new int[N - 1];
        foreach (i; 0 .. N - 1) {
          U[i] = readInt() - 1;
          V[i] = readInt() - 1;
        }

        G = new int[][N];
        foreach (i; 0 .. N - 1) {
          G[U[i]] ~= i;
          G[V[i]] ~= i;
        }

        int ans;

        dep = new int[N];
        dfs(A, -1);
        if (dep[B] <= C) {
          ans = 1;
        } else {
          const r = cast(int)(dep.maxIndex);
          dfs(r, -1);
          const diam = dep.maxElement;
          if (2 * C >= min(D, diam)) {
            ans = 1;
          } else {
            ans = 0;
          }
        }

        writeln(ans ? "Alice" : "Bob");
      }
    }
  } catch (EOFException e) {
  }
}

// Vicfred
// https://codeforces.com/contest/1405/problem/D
// dfs, graph, bfs, tree
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int n, a, b, da, db;
int[][] tree;

int[] depth;
int diameter;

int dfs(int x, int p) {
    int len = 0;

    foreach(child; tree[x]) {
        if(child != p) {
            depth[child] = depth[x] + 1;
            int cur = 1 + dfs(child, x);
            diameter = max(diameter, cur + len);
            len = max(len, cur);
        }
    }

    return len;
}

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

    while(t--) {
        readf("%s %s %s %s %s", &n, &a, &b, &da, &db);
        readln;

        tree = new int[][](n+1);
        depth = new int[n+1];
        diameter = 0;

        foreach(_; 1..n) {
            int u, v;
            readf("%s %s", &u, &v);
            readln;
            tree[u] ~= v;
            tree[v] ~= u;
        }

        depth[a] = 0;
        dfs(a, -1);

        if(2 * da >= min(diameter, db) || depth[b] <= da)
            "Alice".writeln;
        else
            "Bob".writeln;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RD!int-1;
		auto b = RD!int-1;
		auto da = RD!int;
		auto db = RD!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}

		if (db <= da*2)
		{
			ans[ti] = true;
			continue;
		}

		bool dfs1(int pos, int last, int depth)
		{
			if (pos == b) return true;
			if (depth == da) return false;

			foreach (v; edges[pos])
			{
				if (v == last) continue;
				auto res = dfs1(v, pos, depth+1);
				if (res) return true;
			}
			return false;
		}
		auto otk = dfs1(a, -1, 0);
		if (otk)
		{
			ans[ti] = true;
			continue;
		}

		int[] dfs2(int pos, int last, int depth)
		{
			int[] res = [depth, pos];
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				auto r = dfs2(v, pos, depth+1);
				if (r[0] > res[0])
				{
					res = r;
				}
			}
			return res;
		}
		auto r = dfs2(0, -1, 0);
		auto r2 = dfs2(r[1], -1, 0);
		ans[ti] = r2[0] <= da*2;
	}

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


There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).Let's simplify this game a bit. Formally, there are $$$n$$$ participants, the $$$i$$$-th participant chose the number $$$a_i$$$. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of $$$a$$$ the minimum one is the winning one).Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is $$$1$$$-based, i. e. the participants are numbered from $$$1$$$ to $$$n$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of participants. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th participant chosen number. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [] [int] b;
		foreach (i, c; a)
		{
			b[c] ~= i + 1;
		}
		auto f = b.byKey.filter !(c => b[c].length == 1);
		writeln (f.empty ? -1 : b[f.minElement].front);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto cnt = new long[](n);
		foreach (i; 0..n)
		{
			++cnt[a[i]];
		}

		long x = -1;
		foreach (i; 0..n)
		{
			if (cnt[i] == 1)
			{
				x = i;
				break;
			}
		}

		ans[ti] = -1;
		foreach (i; 0..n)
		{
			if (a[i] == x)
			{
				ans[ti] = i+1;
				break;
			}
		}
	}

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


Sereja has an n × m rectangular table a, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size h × w, then the component must contain exactly hw cells.A connected component of the same values is a set of cells of the table that meet the following conditions:  every two cells of the set have the same value;  the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table);  it is impossible to add any cell to the set unless we violate the two previous conditions. Can Sereja change the values of at most k cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case?

The first line contains integers n, m and k (1 ≤ n, m ≤ 100; 1 ≤ k ≤ 10). Next n lines describe the table a: the i-th of them contains m integers ai1, ai2, ..., aim (0 ≤ ai, j ≤ 1) — the values in the cells of the i-th row.

Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed.

import std.algorithm, std.stdio;

void main ()
{
	int n, m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		auto a = new int [] [] (min (n, m), max (n, m));
		foreach (i; 0..n)
			foreach (j; 0..m)
				readf (" %s", n > m ? &a[j][i] : &a[i][j]);
		if (n > m)
			swap (n, m);

		auto s = new int [2] [m];
		int res = k + 1;
		int cur;

		void recur (int i)
		{
			if (i >= n)
			{
				res = min (res, cur);
				return;
			}
			foreach (v; 0..2)
			{
				cur = 0;
				foreach (j; 0..m)
				{
					s[j][0] += a[i][j] ^ v;
					s[j][1] += a[i][j] ^ !v;
					cur += min (s[j][0], s[j][1]);
				}
				if (cur <= k)
					recur (i + 1);
				foreach (j; 0..m)
				{
					s[j][0] -= a[i][j] ^ v;
					s[j][1] -= a[i][j] ^ !v;
				}
			}
		}

		recur (0);
		writeln (res > k ? -1 : res);
	}
}

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;

void main ()
{
	int n, m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		auto a = new int [] [] (n, m);
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				readf (" %s", &a[i][j]);
			}
		}
		if (n > m)
		{
			auto b = new int [] [] (m, n);
			foreach (i; 0..n)
			{
				foreach (j; 0..m)
				{
					b[j][i] = a[i][j];
				}
			}
			swap (n, m);
			a = b;
		}
		assert (n <= m);

		int [2] [] s = new int [2] [m];
		int res = k + 1;
		int cur = k + 1;

		void recur (int i)
		{
			debug {writeln (i, ' ', cur);}
			if (i >= n)
			{
				res = min (res, cur);
				return;
			}

			// put 0
			cur = 0;
			foreach (j; 0..m)
			{
				s[j][0] += (a[i][j] != 0);
				s[j][1] += (a[i][j] != 1);
				cur += min (s[j][0], s[j][1]);
			}
			if (cur <= k)
			{
				recur (i + 1);
			}
			foreach (j; 0..m)
			{
				s[j][0] -= (a[i][j] != 0);
				s[j][1] -= (a[i][j] != 1);
			}

			// put 1
			cur = 0;
			foreach (j; 0..m)
			{
				s[j][0] += (a[i][j] != 1);
				s[j][1] += (a[i][j] != 0);
				cur += min (s[j][0], s[j][1]);
			}
			if (cur <= k)
			{
				recur (i + 1);
			}
			foreach (j; 0..m)
			{
				s[j][0] -= (a[i][j] != 1);
				s[j][1] -= (a[i][j] != 0);
			}
		}

		recur (0);
		if (res > k)
		{
			res = -1;
		}
		writeln (res);
	}
}

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

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

		int ans = int.max;

		if (M > K) {
			foreach (sx; 0 .. M) {
				int sum;
				foreach (x; 0 .. M) {
					int cnt;
					foreach (y; 0 .. N) {
						if (A[sx][y] != A[x][y]) {
							++cnt;
						}
					}
					sum += min(cnt, N - cnt);
				}
				chmin(ans, sum);
			}
		}

		if (N > K) {
			foreach (sy; 0 .. N) {
				int sum;
				foreach (y; 0 .. N) {
					int cnt;
					foreach (x; 0 .. M) {
						if (A[x][sy] != A[x][y]) {
							++cnt;
						}
					}
					sum += min(cnt, M - cnt);
				}
				chmin(ans, sum);
			}
		}

		if (M <= K && N <= K) {
			foreach (p; 0 .. 1 << M) foreach (q; 0 .. 1 << N) {
				int cnt;
				foreach (x; 0 .. M) foreach (y; 0 .. N) {
					if (A[x][y] != (((p >> x) & 1) ^ ((q >> y) & 1))) {
						++cnt;
					}
				}
				chmin(ans, cnt);
			}
		}

// writeln("ans = ",ans);
		if (ans > K) {
			writeln(-1);
		} else {
			writeln(ans);
		}

	}
	} catch (EOFException) {}
}


The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$.You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $$$0$$$).Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.You (suddenly!) have the ability to see the future. You know that during the day you will receive $$$n$$$ messages, the $$$i$$$-th message will be received from the friend with ID $$$id_i$$$ ($$$1 \le id_i \le 10^9$$$).If you receive a message from $$$id_i$$$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.Otherwise (i.e. if there is no conversation with $$$id_i$$$ on the screen):  Firstly, if the number of conversations displayed on the screen is $$$k$$$, the last conversation (which has the position $$$k$$$) is removed from the screen.  Now the number of conversations on the screen is guaranteed to be less than $$$k$$$ and the conversation with the friend $$$id_i$$$ is not displayed on the screen.  The conversation with the friend $$$id_i$$$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $$$n$$$ messages.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 200)$$$ — the number of messages and the number of conversations your smartphone can show. The second line of the input contains $$$n$$$ integers $$$id_1, id_2, \dots, id_n$$$ ($$$1 \le id_i \le 10^9$$$), where $$$id_i$$$ is the ID of the friend which sends you the $$$i$$$-th message.

In the first line of the output print one integer $$$m$$$ ($$$1 \le m \le min(n, k)$$$) — the number of conversations shown after receiving all $$$n$$$ messages. In the second line print $$$m$$$ integers $$$ids_1, ids_2, \dots, ids_m$$$, where $$$ids_i$$$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $$$i$$$ after receiving all $$$n$$$ messages.

NoteIn the first example the list of conversations will change in the following way (in order from the first to last message):  $$$[]$$$;  $$$[1]$$$;  $$$[2, 1]$$$;  $$$[3, 2]$$$;  $$$[3, 2]$$$;  $$$[1, 3]$$$;  $$$[1, 3]$$$;  $$$[2, 1]$$$. In the second example the list of conversations will change in the following way:  $$$[]$$$;  $$$[2]$$$;  $$$[3, 2]$$$;  $$$[3, 2]$$$;  $$$[1, 3, 2]$$$;  and then the list will not change till the end.

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto k = RD!int;
	auto id = RDA;
	long[] ans;
	bool[long] flag;
	foreach (i; 0..n)
	{
		if (flag.get(id[i], false)) continue;
		ans ~= id[i];
		flag[id[i]] = true;
		if (ans.length > k)
		{
			flag[ans.front] = false;
			ans.popFront;
		}
	}
	ans.reverse;
	writeln(ans.length);
	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.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto k = RD!int;
	auto id = RDA;
	long[] ans;
	bool[long] flag;
	foreach (i; 0..n)
	{
		if (flag.get(id[i], false)) continue;
		ans ~= id[i];
		flag[id[i]] = true;
		if (ans.length > k)
		{
			flag[ans.front] = false;
			ans.popFront;
		}
	}
	ans.reverse;
	ans.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}


There are $$$n$$$ rectangles in a row. You can either turn each rectangle by $$$90$$$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of rectangles. Each of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \leq w_i, h_i \leq 10^9$$$) — the width and the height of the $$$i$$$-th rectangle.

Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO". You can print each letter in any case (upper or lower).

NoteIn the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one.

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

  int n; rd(n);
  auto w=new int[](n), h=new int[](n);
  foreach(i; 0..n) rd(w[i], h[i]);

  int cur=1_000_000_000+1;
  foreach(i; 0..n){
    if(cur<min(w[i], h[i])){
      writeln("NO");
      return;
    }
    if(cur>=max(w[i], h[i])) cur=max(w[i], h[i]);
    else cur=min(w[i], h[i]);
  }

  writeln("YES");

}

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


You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_{i} < a_{j}$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of the array. The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$1 \le a_i \le n$$$), the initial permutation $$$a$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print $$$n$$$ integers $$$x_{1}, x_{2}, \ldots, x_{n}$$$ ($$$1 \le x_{i} \le n$$$ for each $$$1 \le i \le n$$$) indicating that the $$$i$$$-th operation must be applied to the suffix starting at index $$$x_{i}$$$. If there are multiple answers, print any of them.

NoteIn the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $$$1$$$). The final array $$$[11, 12, 13, 14]$$$ contains $$$0$$$ inversions.In the second test case, $$$a$$$ will be equal to $$$[2, 4, 3, 5, 6]$$$, $$$[2, 4, 3, 7, 8]$$$, $$$[2, 4, 6, 10, 11]$$$, $$$[2, 8, 10, 14, 15]$$$ and $$$[7, 13, 15, 19, 20]$$$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $$$a$$$ has zero inversions.

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 p = readln.splitter.map !(to !(int)).array;
		auto q = new int [n];
		foreach (i; 0..n)
		{
			q[n - p[i]] = i + 1;
		}
		writefln !("%(%s %)") (q);
	}
}

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;
        auto A = new int[N + 1];
        foreach (i; 1 .. N + 1) {
          A[i] = readInt;
        }

        auto ans = new int[N + 1];
        foreach (i; 1 .. N + 1) {
          ans[A[i]] = i + 1;
        }
        ans[A[N]] = 1;

        foreach (j; 1 .. N + 1) {
          if (j > 1) write(" ");
          write(ans[j]);
        }
        writeln;

        debug {
          auto as = A.dup;
          writeln(as);
          foreach (j; 1 .. N + 1) {
            as[ans[j] .. N + 1] += j;
            writeln(as);
          }
        }
      }
    }
  } 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);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto p = readln.splitter.map !(to !(int)).array;
		p[] -= 1;
		auto q = new int [n];
		foreach (i; 0..n)
		{
			q[p[i]] = i;
		}
		q[] = n - q[];
		writefln !("%(%s %)") (q);
	}
}

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;
        auto A = new int[N + 1];
        foreach (i; 1 .. N + 1) {
          A[i] = readInt;
        }

        auto ans = new int[N + 1];
        foreach (i; 1 .. N + 1) {
          ans[A[i]] = N - i;
        }
        ans[A[N]] = N;

        foreach (j; 1 .. N + 1) {
          if (j > 1) write(" ");
          write(ans[j]);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}


The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the $$$i$$$-th figure equals $$$t_i$$$.    The example of the carousel for $$$n=9$$$ and $$$t=[5, 5, 1, 15, 1, 5, 5, 1, 1]$$$. You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$.

The input contains one or more test cases. The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. One test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of figures in the carousel. Figures are numbered from $$$1$$$ to $$$n$$$ in order of carousel moving. Assume that after the $$$n$$$-th figure the figure $$$1$$$ goes. The second line of the test case contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le 2 \cdot 10^5$$$), where $$$t_i$$$ is the type of the animal of the $$$i$$$-th figure. The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

Print $$$q$$$ answers, for each test case print two lines. In the first line print one integer $$$k$$$ — the minimum possible number of distinct colors of figures. In the second line print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le k$$$), where $$$c_i$$$ is the color of the $$$i$$$-th figure. If there are several answers, you can print any.

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 q = RD!int;
	auto k = new int[](q);
	auto ans = new int[][](q);
	foreach (ti; 0..q)
	{
		auto n = RD!int;
		auto t = RDA!int;
		ans[ti].length = n;

		int cnt;
		foreach (i; 1..n)
		{
			if (t[i] != t[i-1])
				++cnt;
		}
		if (t[0] != t[$-1])
			++cnt;

		if (cnt == 0)
		{
			k[ti] = 1;
			ans[ti][] = 1;
		}
		else if (cnt % 2 == 0)
		{
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				ans[ti][i] = c + 1;
			}
			k[ti] = 2;
 		}
		else
		{
			bool f;
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				else if (!f)
				{
					c ^= 1;
					f = true;
				}
				ans[ti][i] = c + 1;
			}
			if (t[0] == t[$-1])
			{
				f = true;
			}
			if (f)
			{
				k[ti] = 2;
			}
			else
			{
				k[ti] = 3;
				//if (t[0] != t[$-1])
					ans[ti][$-1] = 3;
				/*else
				{
					foreach_reverse (i; 1..n)
					{
						if (t[i] != t[i-1])
						{
							foreach_reverse (j; 1..i)
							{
								ans[ti][j] = 3;
								if (t[j] != t[j-1])
									break;
							}
							break;
						}
					}
				}*/
			}
		}
	}

	foreach (ti; 0..q)
	{
		writeln(k[ti]);
		ans[ti].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; }
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 q = RD!int;
	auto k = new int[](q);
	auto ans = new int[][](q);
	foreach (ti; 0..q)
	{
		auto n = RD!int;
		auto t = RDA!int;
		ans[ti].length = n;
		bool[int] set;
		foreach (i; 0..n)
		{
			set[t[i]] = true;
		}
		if (set.keys.length == 1)
		{
			k[ti] = 1;
			ans[ti][] = 1;
		}
		else if (set.keys.length % 2 == 0)
		{
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				ans[ti][i] = c + 1;
			}
			k[ti] = 2;
 		}
		else
		{
			bool f;
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				else if (!f)
				{
					c ^= 1;
					f = true;
				}
				ans[ti][i] = c + 1;
			}
			if (f)
			{
				k[ti] = 2;
			}
			else
			{
				k[ti] = 3;
				ans[ti][$-1] = 3;
			}
		}
	}

	foreach (ti; 0..q)
	{
		writeln(k[ti]);
		ans[ti].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; }
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 q = RD!int;
	auto k = new int[](q);
	auto ans = new int[][](q);
	foreach (ti; 0..q)
	{
		auto n = RD!int;
		auto t = RDA!int;
		ans[ti].length = n;

		int cnt;
		foreach (i; 1..n)
		{
			if (t[i] != t[i-1])
				++cnt;
		}
		if (t[0] != t[$-1])
			++cnt;

		if (cnt == 0)
		{
			k[ti] = 1;
			ans[ti][] = 1;
		}
		else if (cnt % 2 == 0)
		{
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				ans[ti][i] = c + 1;
			}
			k[ti] = 2;
 		}
		else
		{
			bool f;
			ans[ti][0] = 1;
			auto c = 0;
			foreach (i; 1..n)
			{
				if (t[i] != t[i-1])
				{
					c ^= 1;
				}
				else if (!f)
				{
					c ^= 1;
					f = true;
				}
				ans[ti][i] = c + 1;
			}
			if (f)
			{
				k[ti] = 2;
			}
			else
			{
				k[ti] = 3;
				ans[ti][$-1] = 3;
			}
		}
	}

	foreach (ti; 0..q)
	{
		writeln(k[ti]);
		ans[ti].map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}


There is a classroom with two rows of computers. There are $$$n$$$ computers in each row and each computer has its own grade. Computers in the first row has grades $$$a_1, a_2, \dots, a_n$$$ and in the second row — $$$b_1, b_2, \dots, b_n$$$.Initially, all pairs of neighboring computers in each row are connected by wire (pairs $$$(i, i + 1)$$$ for all $$$1 \le i < n$$$), so two rows form two independent computer networks.Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $$$i$$$-th computer from the first row and the $$$j$$$-th computer from the second row costs $$$|a_i - b_j|$$$.You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.That is the minimum total cost to make a fault-tolerant network?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of computers in each row. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the grades of computers in the first row. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the grades of computers in the second row. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.

NoteIn the first test case, it's optimal to connect four pairs of computers:   computer $$$1$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$;  computer $$$3$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$;  computer $$$2$$$ from the first row with computer $$$1$$$ from the second row: cost $$$|10 - 20| = 10$$$;  computer $$$2$$$ from the first row with computer $$$3$$$ from the second row: cost $$$|10 - 25| = 15$$$;  In total, $$$3 + 3 + 10 + 15 = 31$$$.In the second test case, it's optimal to connect $$$1$$$ from the first row with $$$1$$$ from the second row, and $$$4$$$ from the first row with $$$4$$$ from the second row.

import std.stdio;
import std.math;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t ; 0 .. T) {
        int N = readln.chomp.to!int;
        auto A = readln.chomp.split(" ").map!(to!int).array;
        auto B = readln.chomp.split(" ").map!(to!int).array;

        long conn(int i, int j) {
            if (i >= 0 && j >= 0) return abs(A[i] - B[j]);
            if (i == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    ret = min(ret, abs(A[k] - B[j]));
                }
                return ret;
            }
            if (j == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    ret = min(ret, abs(A[i] - B[k]));
                }
                return ret;
            }
            assert(false);
        }

        auto cands = [
            conn(0, 0) + conn(N-1, N-1),
            conn(0, 0) + conn(N-1, -1) + conn(-1, N-1),
            conn(0, N-1) + conn(N-1, 0),
            conn(0, N-1) + conn(N-1, -1) + conn(-1, 0),
            conn(N-1, 0) + conn(0, -1) + conn(-1, N-1),
            conn(N-1, N-1) + conn(0, -1) + conn(-1, 0),
            conn(0, -1) + conn(N-1, -1) + conn(-1, 0) + conn(-1, N-1)
        ];
        writeln(cands.reduce!min);
    }
}

import std.stdio;
import std.math;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t ; 0 .. T) {
        int N = readln.chomp.to!int;
        auto A = readln.chomp.split(" ").map!(to!int).array;
        auto B = readln.chomp.split(" ").map!(to!int).array;

        long conn(int i, int j) {
            if (i >= 0 && j >= 0) return abs(A[i] - B[j]);
            if (i == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    ret = min(ret, abs(A[k] - B[j]));
                }
                return ret;
            }
            if (j == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    ret = min(ret, abs(A[i] - B[k]));
                }
                return ret;
            }
            assert(false);
        }

        auto cands = [
            conn(0, N-1) + conn(N-1, 0),
            conn(0, N-1) + conn(N-1, -1) + conn(-1, 0),
            conn(N-1, 0) + conn(0, -1) + conn(-1, N-1),
            conn(0, -1) + conn(N-1, -1) + conn(-1, 0) + conn(-1, N-1)
        ];
        writeln(cands.reduce!min);
    }
}

import std.stdio;
import std.math;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t ; 0 .. T) {
        int N = readln.chomp.to!int;
        auto A = readln.chomp.split(" ").map!(to!int).array;
        auto B = readln.chomp.split(" ").map!(to!int).array;

        long conn(int i, int j) {
            if (i >= 0 && j >= 0) return abs(A[i] - B[j]);
            if (i == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    if (j == k) continue;
                    ret = min(ret, abs(A[k] - B[j]));
                }
                return ret;
            }
            if (j == -1) {
                long ret = long.max;
                for (int k = 0; k < N; k++) {
                    if (i == k) continue;
                    ret = min(ret, abs(A[i] - B[k]));
                }
                return ret;
            }
            assert(false);
        }

        auto cands = [
            conn(0, N-1) + conn(N-1, 0),
            conn(0, N-1) + conn(N-1, -1) + conn(-1, 0),
            conn(N-1, 0) + conn(0, -1) + conn(-1, N-1),
            conn(0, -1) + conn(N-1, -1) + conn(-1, 0) + conn(-1, N-1)
        ];
        writeln(cands.reduce!min);
    }
}


Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right.  Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of students in each row. The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$$$ ($$$1 \le h_{1, i} \le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row. The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$$$ ($$$1 \le h_{2, i} \le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row.

Print a single integer — the maximum possible total height of players in a team Demid can choose.

NoteIn the first example Demid can choose the following team as follows:   In the second example Demid can choose the following team as follows:

/// https://codeforces.com/problemset/problem/1195/c
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.array : array, split;
import std.conv  : to;
import std.algorithm : map, max;

void main()
{
  int n, q, sum = 0;
  stdin.readf!"%d\n"(n);

  int[] a = stdin.readln().split().map!(to!int).array;
  int[] b = stdin.readln().split().map!(to!int).array;

  long m1 = 0, m2 = 0, _m1, _m2;
  for (int i = 0; i < n; i++) {
    _m1 = max(m1, m2 + a[i]);
    _m2 = max(m2, m1 + b[i]);
    m1 = _m1; m2 = _m2;
  }

  writeln(max(m1, m2));
}

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

	long[] xs, ys;
	foreach(i; 0 .. n){
		if(i == 0) xs ~= as[0], ys ~= bs[0];
		else{
			xs ~= max(ys[i - 1] + as[i], xs[i - 1]);
			ys ~= max(xs[i - 1] + bs[i], ys[i - 1]);
		}
	}

	long ans = max(xs[n - 1], ys[n - 1]);
	ans.writeln;

}

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

string FMT_F = "%.10f";

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

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

long mod = 10^^9 + 7;
//long mod = 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 h1 = RDA;
	auto h2 = RDA;

	auto dp = new long[][](n+1, 3);
	foreach (i; 0..n)
	{
		dp[i+1][0] = max(dp[i][1], dp[i][2]) + h1[i];
		dp[i+1][1] = max(dp[i][0], dp[i][2]) + h2[i];
		dp[i+1][2] = max(dp[i][0], max(dp[i][1], dp[i][2]));
	}

	writeln(max(dp[n][0], max(dp[n][1], dp[n][2])));
	stdout.flush();
	debug readln();
}

/// https://codeforces.com/problemset/problem/1195/c
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.array : array, split;
import std.conv  : to;
import std.algorithm : map, max;

void main()
{
  int n, q, sum = 0;
  stdin.readf!"%d\n"(n);

  int[] a = stdin.readln().split().map!(to!int).array;
  int[] b = stdin.readln().split().map!(to!int).array;

  int m1 = 0, m2 = 0, _m1, _m2;
  for (int i = 0; i < n; i++) {
    _m1 = max(m1, m2 + a[i]);
    _m2 = max(m2, m1 + b[i]);
    m1 = _m1; m2 = _m2;
  }

  writeln(max(m1, m2));
}