Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of $$$n$$$ locations connected by $$$m$$$ two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location.Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses.The game will start in location $$$s$$$ and end in location $$$t$$$, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from $$$s$$$ to $$$t$$$ without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as $$$s$$$ or as $$$t$$$.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 3 \cdot 10^5$$$, $$$n - 1 \le m \le 3 \cdot 10^5$$$) — the number of locations and passages, respectively. Then $$$m$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le n$$$, $$$x \ne y$$$) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location.

Print one integer — the maximum number of bosses your friend can place, considering all possible choices for $$$s$$$ and $$$t$$$.

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto tm = new int[n];
    auto lo = new int[n];
    auto dp = new int[n];

    int dfs(int v, int fr, ref int t) {
        tm[v] = lo[v] = ++t;
        auto ans = 0;
        auto s1 = 0, s2 = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (tm[u] == 0) {
                ans = max(ans, dfs(u, v, t));
                auto cur = dp[u] + (lo[u] >= tm[u] ? 1 : 0);

                if (cur > s1) s2 = s1, s1 = cur;
                else if (cur > s2) s2 = cur;
            }

            lo[v] = min(lo[v], lo[u]);
        }

        dp[v] = s1;
        ans = max(ans, s1 + s2);
        return ans;
    }

    auto t = 0;
    auto ans = dfs(0, -1, t);
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto tm = new int[n];
    auto lo = new int[n];
    auto dp = new int[n];

    int dfs(int v, int fr, ref int t) {
        tm[v] = lo[v] = ++t;
        auto ans = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (tm[u] == 0) {
                ans = max(ans, dfs(u, v, t));
            }
            lo[v] = min(lo[v], lo[u]);
        }

        auto s1 = 0, s2 = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            auto cur = dp[u] + (lo[u] > tm[v] ? 1 : 0);
            if (cur > s1) s2 = s1, s1 = cur;
            else if (cur > s2) s2 = cur;
        }

        dp[v] = s1;
        ans = max(ans, s1 + s2);
        return ans;
    }

    auto t = 0;
    auto ans = dfs(0, -1, t);
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto tm = new int[n];
    auto lo = new int[n];
    auto dp = new int[n];

    int dfs(int v, int fr, ref int t) {
        tm[v] = lo[v] = ++t;
        auto ans = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (tm[u] == 0) {
                ans = max(ans, dfs(u, v, t));
            }
            lo[v] = min(lo[v], lo[u]);
        }

        auto s1 = 0, s2 = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;
            if (lo[u] <= tm[v]) continue;

            auto cur = dp[u] + (lo[u] > tm[v] ? 1 : 0);
            if (cur > s1) s2 = s1, s1 = cur;
            else if (cur > s2) s2 = cur;
        }

        dp[v] = s1;
        ans = max(ans, s1 + s2);
        return ans;
    }

    auto t = 0;
    auto ans = dfs(0, -1, t);
    ans.writeln;
}

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

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

    auto g = new int[][] (n+1);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto vis = new bool[n+1];
    auto tm = new int[n+1];
    auto lo = new int[n+1];

    void dfs(int v, int fr, ref int t, ref int ans) {
        debug { v.writeln; }

        tm[v] = ++t;
        lo[v] = t;
        vis[v] = true;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (vis[u]) continue;

            dfs(u, v, t, ans);
        }

        foreach (u; g[v]) {
            if (u == fr) continue;

            lo[v] = min(lo[u], lo[v]);
            if (tm[v] < tm[u] && lo[u] <= tm[v]) {
                debug { writeln(u, ' ', v, ' ', lo[u]); }
                ++ans;
            }
        }
    }

    auto t = 0, sub = 0;
    dfs(1, -1, t, sub);
    auto ans = m - sub;
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto vis = new bool[n];
    auto tm = new int[n];
    auto lo = new int[n];

    int dfs(int v, int fr, ref int t, ref int ans) {
        tm[v] = ++t;
        lo[v] = t;
        vis[v] = true;
        foreach (u; g[v]) {
            if (u == fr) continue;
            if (vis[u]) {
                lo[v] = min(lo[u] - 1, lo[v]);
                continue;
            }
            lo[v] = min(lo[v], dfs(u, v, t, ans));
        }

        if (lo[v] < tm[v]) ++ans;
        return lo[v];
    }

    auto t = 0, ans = 0;
    dfs(0, -1, t, ans);
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto tm = new int[n];
    auto lo = new int[n];
    auto dp = new int[n];

    int dfs(int v, int fr, ref int t) {
        tm[v] = lo[v] = ++t;
        auto ans = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (tm[u] == 0) {
                ans = max(ans, dfs(u, v, t));
            }
            lo[v] = min(lo[v], lo[u]);
        }

        auto s1 = 0, s2 = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            auto cur = dp[u] + (lo[u] == tm[u] ? 1 : 0);
            if (cur > s1) s2 = s1, s1 = cur;
            else if (cur > s2) s2 = cur;
        }

        dp[v] = s1;
        ans = max(ans, s1 + s2);
        return ans;
    }

    auto t = 0;
    auto ans = dfs(0, -1, t);
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto tm = new int[n];
    auto lo = new int[n];
    auto dp = new int[n];

    int dfs(int v, int fr, ref int t) {
        tm[v] = lo[v] = ++t;
        auto ans = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;

            if (tm[u] == 0) {
                ans = max(ans, dfs(u, v, t));
            }
            lo[v] = min(lo[v], lo[u]);
        }

        auto s1 = 0, s2 = 0;
        foreach (u; g[v]) {
            auto cur = dp[u] + (lo[u] > tm[v] ? 1 : 0);
            if (cur > s1) s2 = s1, s1 = cur;
            else if (cur > s2) s2 = cur;
        }

        dp[v] = s1;
        ans = max(ans, s1 + s2);
        return ans;
    }

    auto t = 0;
    auto ans = dfs(0, -1, t);
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto vis = new bool[n];
    auto tm = new int[n];
    auto lo = new int[n];

    int dfs(int v, int fr, ref int t, ref int ans) {
        tm[v] = ++t;
        lo[v] = t;
        vis[v] = true;
        foreach (u; g[v]) {
            if (u == fr) continue;
            if (vis[u]) {
                lo[v] = min(lo[u] - 1, lo[v]);
                continue;
            }
            lo[v] = min(lo[v], dfs(u, v, t, ans));
        }

        if (lo[v] < tm[v]) ++ans;
        return lo[v];
    }

    auto t = 0, sub = 0;
    dfs(0, -1, t, sub);
    auto ans = n - sub;
    ans.writeln;
}

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

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

    auto g = new int[][] (n);

    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    debug { g.writeln; }

    auto vis = new bool[n];
    auto tm = new int[n];
    auto lo = new int[n];

    int dfs(int v, int fr, ref int t, ref int ans) {
        tm[v] = ++t;
        lo[v] = t;
        vis[v] = true;
        foreach (u; g[v]) {
            if (u == fr) continue;
            if (vis[u]) {
                lo[v] = min(lo[u] - 1, lo[v]);
                continue;
            }
            lo[v] = min(lo[v], dfs(u, v, t, ans));
        }

        if (lo[v] < tm[v]) ++ans;
        return lo[v];
    }

    auto t = 0, sub = 0;
    dfs(0, -1, t, sub);
    auto ans = m - sub;
    ans.writeln;
}


Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!The only thing Mrs. Smith remembered was that any permutation of $$$n$$$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.The sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $$$a_{i_1}, a_{i_2}, \ldots, a_{i_k}$$$ where $$$1\leq i_1 < i_2 < \ldots < i_k\leq n$$$ is called increasing if $$$a_{i_1} < a_{i_2} < a_{i_3} < \ldots < a_{i_k}$$$. If $$$a_{i_1} > a_{i_2} > a_{i_3} > \ldots > a_{i_k}$$$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.For example, if there is a permutation $$$[6, 4, 1, 7, 2, 3, 5]$$$, LIS of this permutation will be $$$[1, 2, 3, 5]$$$, so the length of LIS is equal to $$$4$$$. LDS can be $$$[6, 4, 1]$$$, $$$[6, 4, 2]$$$, or $$$[6, 4, 3]$$$, so the length of LDS is $$$3$$$.Note, the lengths of LIS and LDS can be different.So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.

The only line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of permutation that you need to build.

Print a permutation that gives a minimum sum of lengths of LIS and LDS.  If there are multiple answers, print any.

NoteIn the first sample, you can build a permutation $$$[3, 4, 1, 2]$$$. LIS is $$$[3, 4]$$$ (or $$$[1, 2]$$$), so the length of LIS is equal to $$$2$$$. LDS can be ony of $$$[3, 1]$$$, $$$[4, 2]$$$, $$$[3, 2]$$$, or $$$[4, 1]$$$. The length of LDS is also equal to $$$2$$$. The sum is equal to $$$4$$$. Note that $$$[3, 4, 1, 2]$$$ is not the only permutation that is valid.In the second sample, you can build a permutation $$$[2, 1]$$$. LIS is $$$[1]$$$ (or $$$[2]$$$), so the length of LIS is equal to $$$1$$$. LDS is $$$[2, 1]$$$, so the length of LDS is equal to $$$2$$$. The sum is equal to $$$3$$$. Note that permutation $$$[1, 2]$$$ is also valid.

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = 1<<29;
    int x = 1;
    for (int i = 1; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = min(N, i + x - 1); j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

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

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

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

    auto sq = sqrt(cast(double) n).to!int;

    debug { sq.writeln; }

    auto chks = (n+1).iota.dropOne.chunks(sq).map!(c => c.array).array;

    debug { chks.writeln; }

    auto ans = chks.map!(c => retro(c)).join();

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

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)
	{
		auto t = cast (int) (sqrt (n - 1.0) + 1);
		auto s = t;
		if (t * (s - 1) >= n)
		{
			s -= 1;
		}
		int [] ans;
		foreach_reverse (i; 0..s)
		{
			foreach (j; 0..t)
			{
				int c = i * t + j + 1;
				if (1 <= c && c <= n)
				{
					ans ~= c;
				}
			}
		}
		debug {writeln (s, " x ", t);}
		writefln ("%(%s %)", ans);
	}
}

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

  int n; rd(n);
  int k=1;
  while((k+1)*(k+1)<=n) k++;
  int[] perm;
  for(int i=1; i<=n; ){
    int[] a;
    for(int j=0; j<k && i<=n; j++){
      a~=(i++);
    }
    perm=a~perm;
  }
  writefln("%(%s %)", perm);
}

/*
  19
  [17 18 19] [13 14 15 16] [9 10 11 12] [5 6 7 8] [1 2 3 4]
*/

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

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

// floor(sqrt(a))
long floorSqrt(long a) {
  import core.bitop : bsr;
  import std.algorithm : min;
  long b = a, x = 0, y = 0;
  for (int e = bsr(a) & ~1; e >= 0; e -= 2) {
    x <<= 1;
    y <<= 1;
    if (b >= (y | 1) << e) {
      b -= (y | 1) << e;
      x |= 1;
      y += 2;
    }
  }
  return x;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const sqrtN = cast(int)(floorSqrt(N - 1)) + 1;
      debug {
        writeln(N, ": ", sqrtN);
      }

      int[] ans;
      foreach_reverse (x; 0 .. sqrtN) {
        foreach (y; 0 .. sqrtN) {
          const a = x * sqrtN + y;
          if (0 <= a && a < N) {
            ans ~= a;
          }
        }
      }
      assert(ans.length == N);

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

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N+1;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

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

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; i < N && p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

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

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

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


Let's consider all integers in the range from $$$1$$$ to $$$n$$$ (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$\mathrm{gcd}(a, b)$$$, where $$$1 \leq a < b \leq n$$$.The greatest common divisor, $$$\mathrm{gcd}(a, b)$$$, of two positive integers $$$a$$$ and $$$b$$$ is the biggest integer that is a divisor of both $$$a$$$ and $$$b$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^6$$$).

For each test case, output the maximum value of $$$\mathrm{gcd}(a, b)$$$ among all $$$1 \leq a < b \leq n$$$.

NoteIn the first test case, $$$\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1$$$.In the second test case, $$$2$$$ is the maximum possible value, corresponding to $$$\mathrm{gcd}(2, 4)$$$.

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto N = readln.chomp.to!int;
        writeln(N/2);
    }
}

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)
	{
		writeln (readln.strip.to !(int) / 2);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = n / 2;
	}

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


You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Print the only number — distance between two nearest minimums in the array.

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto M = A.reduce!min;

    int last = - (1 << 29);
    int ans = 1 << 29;
    foreach (i; 0..N) {
        if (A[i] == M) {
            ans = min(ans, i - last);
            last = i;
        }
    }

    ans.writeln;
}

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

void main() {
    int n;
    scan(n);
    auto a = readln.split.to!(int[]);
    auto amin = a.reduce!min;
    auto b = iota(n).filter!(i => a[i] == amin).array;

    int ans = 2*10^^5;

    foreach (i ; 0 .. b.length - 1) {
        ans = min(ans, b[i+1] - b[i]);
    }

    writeln(ans);
}

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

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

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

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

  auto mn=reduce!(min)(a);
  int[] ids;
  foreach(int i; 0..n){
    if(a[i]==mn) ids~=i;
  }
  int dist=n;
  foreach(i; 0..(ids.length-1)){
    dist=min(dist, ids[i+1]-ids[i]);
  }

  writeln(dist);
}

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));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) write(e, " ");
  writeln();
}


Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≤ k ≤ n)? Help him and print this number modulo 1000000007 (109 + 7). Operation x & y denotes bitwise AND operation of two numbers.

The first line contains a single integer n (1 ≤ n ≤ 106). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 106).

Output a single integer representing the number of required groups modulo 1000000007 (109 + 7).

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

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

immutable long MO = 1000000007;
immutable L = 20;
int N;
int[] A;

void main(string[] args) {
	long[] two = new long[1000005];
	two[0] = 1;
	foreach (i; 1 .. two.length) {
		two[i] = (two[i - 1] * 2) % MO;
	}
	int[] pop = new int[1 << L];
	foreach (p; 1 .. 1 << L) {
		pop[p] = 1 + pop[p & (p - 1)];
	}

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

		int[] cnt = new int[1 << L];
		foreach (i; 0 .. N) {
			++cnt[((1 << L) - 1) ^ A[i]];
		}
		foreach (l; 0 .. L) {
			foreach (p; 0 .. 1 << L) if (!(p & 1 << l)) {
				cnt[p | 1 << l] += cnt[p];
			}
		}
// writeln("cnt = ",cnt[0..8]);
		long ans;
		foreach (p; 0 .. 1 << L) {
			(ans += ((pop[p] % 2 != 0) ? -1 : +1) * two[cnt[p]]) %= MO;
		}
		ans = (ans % MO + MO) % MO;
		writeln(ans);
	}
	} catch (EOFException) {}
}

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

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

immutable long MO = 1000000007;
immutable L = 20;
int N;
int[] A;

void main(string[] args) {
	long[] two = new long[100005];
	two[0] = 1;
	foreach (i; 1 .. two.length) {
		two[i] = (two[i - 1] * 2) % MO;
	}
	int[] pop = new int[1 << L];
	foreach (p; 1 .. 1 << L) {
		pop[p] = 1 + pop[p & (p - 1)];
	}

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

		int[] cnt = new int[1 << L];
		foreach (i; 0 .. N) {
			++cnt[((1 << L) - 1) ^ A[i]];
		}
		foreach (l; 0 .. L) {
			foreach (p; 0 .. 1 << L) if (!(p & 1 << l)) {
				cnt[p | 1 << l] += cnt[p];
			}
		}
// writeln("cnt = ",cnt[0..8]);
		long ans;
		foreach (p; 0 .. 1 << L) {
			(ans += ((pop[p] % 2 != 0) ? -1 : +1) * two[cnt[p]]) %= MO;
		}
		ans = (ans % MO + MO) % MO;
		writeln(ans);
	}
	} catch (EOFException) {}
}


The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presuppose that the set elements are written in the increasing order.We'll say that the secret is safe if the following conditions are hold:  for any two indexes i, j (1 ≤ i < j ≤ k) the intersection of sets Ui and Uj is an empty set;  the union of sets U1, U2, ..., Uk is set (1, 2, ..., n);  in each set Ui, its elements ui, 1, ui, 2, ..., ui, |Ui| do not form an arithmetic progression (in particular, |Ui| ≥ 3 should hold). Let us remind you that the elements of set (u1, u2, ..., us) form an arithmetic progression if there is such number d, that for all i (1 ≤ i < s) fulfills ui + d = ui + 1. For example, the elements of sets (5), (1, 10) and (1, 5, 9) form arithmetic progressions and the elements of sets (1, 2, 4) and (3, 6, 8) don't.Your task is to find any partition of the set of words into subsets U1, U2, ..., Uk so that the secret is safe. Otherwise indicate that there's no such partition.

The input consists of a single line which contains two integers n and k (2 ≤ k ≤ n ≤ 106) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.

If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret. If there are multiple solutions, print any of them.

import std.algorithm;
import std.math;
import std.stdio;

int main ()
{
	int n, k;
	while ((readf (" %s %s", &n, &k) >= 0) && !stdin.eof ())
	{
		if (k * 3 > n)
		{
			writefln ("-1");
		}
		else
		{
			auto s = new int [n];
			foreach (i; 0..k)
			{
				s[2 * i + 0] = i;
				s[2 * i + 1] = i;
				s[2 * k + i] = i;
			}
			foreach (i; 0..n)
			{
				writef ("%s%s", s[i] + 1,
				        i + 1 < n ? ' ' : '\n');
			}
		}
	}
	return 0;
}


You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.You can perform the following operation any number of times (possibly zero): select an index $$$i$$$ ($$$1 \leq i \leq n$$$) and swap $$$a_i$$$ and $$$b_i$$$.Let's define the cost of the array $$$a$$$ as $$$\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2$$$. Similarly, the cost of the array $$$b$$$ is $$$\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2$$$.Your task is to minimize the total cost of two arrays.

Each test case consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 40$$$) — the number of test cases. The following is a description of the input data sets. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of both arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$) — elements of the first array. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$) — elements of the second array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.

For each test case, print the minimum possible total cost.

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

enum long INF = 1L<<56;

void solve() {
    int N = read!int;
    auto A = readarray!int;
    auto B = readarray!int;

    long sq(int x) {
        return x*x;
    }

    int L = 100*100;
    auto f = new long[][](N + 1, L + 1);
    auto s = new int[N + 1];
    foreach (i; 0 .. N) {
        s[i + 1] = s[i] + A[i] + B[i];
    }
    foreach (ref l; f) l[] = INF;
    f[1][A[0]] = f[1][B[0]] = 0;
    for (int k = 1; k < N; k++) {
        for (int sa = 0; sa <= L; sa++) {
            if (sa + A[k] > L || sa + B[k] > L) continue;
            int sb = s[k] - sa;
            f[k + 1][sa + A[k]] = min(f[k + 1][sa + A[k]], f[k][sa] + sa * A[k] + sb * B[k]);
            f[k + 1][sa + B[k]] = min(f[k + 1][sa + B[k]], f[k][sa] + sa * B[k] + sb * A[k]);
        }
    }
    long base = 0;
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            base += sq(A[i]) + sq(A[j]) + sq(B[i]) + sq(B[j]);
        }
    }
    long add = INF;
    foreach (x; f[N]) {
        add = min(add, x);
    }
    writeln(base + add * 2);
}

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

// cheese-cracker [2022-02-12]

int[][] dp;
int[][] par;
int[] a, b;

int go(int i, int s){
    if(s < 0){
        return 0;
    }
    if(i < 0){
        return (s == 0);
    }
    if(dp[i][s] != -1){
        return dp[i][s];
    }
    int res = 0;
    if(go(i-1, s - a[i])){
        par[i][s] = 1;
        res = 1;
    }else if(go(i-1, s - b[i])){
        par[i][s] = 2;
        res = 1;
    }
    dp[i][s] = res;

    return res;
}

int[] res;

void btrack(int i, int s){
    if(i < 0){
        return;
    }
    res ~= par[i][s];

    if(res.back == 2){
        btrack(i-1, s - b[i]);
    }else{
        btrack(i-1, s - a[i]);
    }
}

long valu(int[] arr){
    long summ = 0;
    for(int i = 0; i < arr.length; ++i){
        for(int j = i+1; j < arr.length; ++j){
            summ += (arr[i] + arr[j])* (arr[i] + arr[j]);
        }
    }
    return summ;
}

void solve(){
    int n = scan!int;

    dp = new int[][](n, 10005);
    for(int i = 0; i < n; ++i){
        dp[i][] = -1;
    }
    par = new int[][](n, 10005);
    a = scanArray!int;
    b = scanArray!int;
    int summ = a.sum + b.sum;
    for(int s = summ/2; s >= 0; --s){
        if(go(n-1, s)){
            btrack(n-1, s);
            res.reverse;
            break;
        }
    }

    int[] t1, t2;
    for(int i = 0; i < n; ++i){
        if(res[i] == 1){
            t1 ~= a[i];
            t2 ~= b[i];
        }else if(res[i] == 2){
            t1 ~= b[i];
            t2 ~= a[i];
        }
    }
    /* show(t1, t2); */
    writeln(valu(t1) + valu(t2));
}

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

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

//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!int;
		auto b = RDA!int;

		auto dp = new int[][](n+1, n*100+1);
		dp[0][0] = 1;
		foreach (i; 0..n)
		{
			foreach (j; 0..i*100+1)
			{
				if (dp[i][j] == 0) continue;
				dp[i+1][j+a[i]] = 1;
				dp[i+1][j+b[i]] = 2;
			}
		}

		auto tot = a.sum + b.sum;
		int p;
		foreach (j; 0..n*100+1)
		{
			if (dp[n][j] == 0) continue;
			if (abs(j*2 - tot) < abs(p*2 - tot))
			{
				p = j;
			}
		}

		long[] aa, bb;
		foreach_reverse (i; 0..n)
		{
			auto num = dp[i+1][p];
			if (num == 1)
			{
				aa ~= a[i];
				bb ~= b[i];
				p -= a[i];
			}
			else
			{
				aa ~= b[i];
				bb ~= a[i];
				p -= b[i];
			}
		}
		foreach (i; 0..n-1)
		{
			foreach (j; i+1..n)
			{
				ans[ti] += (aa[i]+aa[j])^^2 + (bb[i]+bb[j])^^2;
			}
		}
	}

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

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

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

		foreach (i; 0..n)
		{
			if (a[i] > b[i])
			{
				swap (a[i], b[i]);
			}
		}

		auto c = new int [n];
		c[] = b[] - a[];
		auto total = sum (c);

		auto f = new bool [total + 1];
		f[0] = true;
		foreach (i; 0..n)
		{
			foreach_reverse (j; c[i]..total + 1)
			{
				f[j] |= f[j - c[i]];
			}
		}

		auto half = total / 2;
		while (!f[half])
		{
			half -= 1;
		}

		int res = 0;
		res += (sum (a) + half) ^^ 2;
		res += (sum (a) + total - half) ^^ 2;
		foreach (i; 0..n)
		{
			res += (n - 2) * a[i] ^^ 2;
			res += (n - 2) * b[i] ^^ 2;
		}
		writeln (res);
	}
}

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

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

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

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

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

        foreach (i; 0 .. N) {
          if (A[i] > B[i]) {
            swap(A[i], B[i]);
          }
        }
        const sumA = A.sum;
        const sumB = B.sum;
        const lim = sumB - sumA;
        auto dp = new bool[lim + 1];
        dp[0] = 1;
        foreach (i; 0 .. N) {
          const d = B[i] - A[i];
          foreach_reverse (x; 0 .. lim - d + 1) {
            if (dp[x]) {
              dp[x + d] = true;
            }
          }
        }

        long ans = long.max;
        foreach (x; 0 .. lim + 1) if (dp[x]) {
          const long sa = sumA + x;
          const long sb = sumB - x;
          chmin(ans, sa^^2 + sb^^2);
        }
        foreach (i; 0 .. N) {
          ans += 1L * (N - 2) * (A[i]^^2 + B[i]^^2);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:  Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.  Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.  The total height of all rings used should be maximum possible.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock. The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.

Print one integer — the maximum height of the tower that can be obtained.

NoteIn the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.multiSort!("a.outer > b.outer",
		   "a.inner > b.inner",
		   "a.height > b.height");
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.sort!((a, b) {
      if (a.outer > b.outer) return true;
      if (a.outer < b.outer) return false;
      if (a.inner > b.inner) return true;
      if (a.inner < b.inner) return false;
      if (a.height > b.height) return true;
      if (b.height > b.height) return false;
      return false;
    });
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.multiSort!("a.outer > b.outer",
		   "a.inner > b.inner",
		   "a.height > b.height",
		   SwapStrategy.unstable);
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.sort!((a, b) {
      if (a.outer > b.outer) return true;
      if (a.outer < b.outer) return false;
      if (a.inner > b.inner) return true;
      if (a.inner < b.inner) return false;
      if (a.height > b.height) return true;
      if (b.height > b.height) return false;
      return false;
    });
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new int[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}


Nauuo is a girl who loves playing chess.One day she invented a game by herself which needs $$$n$$$ chess pieces to play on a $$$m\times m$$$ chessboard. The rows and columns are numbered from $$$1$$$ to $$$m$$$. We denote a cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column as $$$(r,c)$$$.The game's goal is to place $$$n$$$ chess pieces numbered from $$$1$$$ to $$$n$$$ on the chessboard, the $$$i$$$-th piece lies on $$$(r_i,\,c_i)$$$, while the following rule is satisfied: for all pairs of pieces $$$i$$$ and $$$j$$$, $$$|r_i-r_j|+|c_i-c_j|\ge|i-j|$$$. Here $$$|x|$$$ means the absolute value of $$$x$$$.However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.She wants to find the smallest chessboard on which she can put $$$n$$$ pieces according to the rules.She also wonders how to place the pieces on such a chessboard. Can you help her?

The only line contains a single integer $$$n$$$ ($$$1\le n\le 1000$$$) — the number of chess pieces for the game.

The first line contains a single integer — the minimum value of $$$m$$$, where $$$m$$$ is the length of sides of the suitable chessboard. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$r_i$$$ and $$$c_i$$$ ($$$1\le r_i,c_i\le m$$$) — the coordinates of the $$$i$$$-th chess piece. If there are multiple answers, print any.

NoteIn the first example, you can't place the two pieces on a $$$1\times1$$$ chessboard without breaking the rule. But you can place two pieces on a $$$2\times2$$$ chessboard like this:In the second example, you can't place four pieces on a $$$2\times2$$$ chessboard without breaking the rule. For example, if you place the pieces like this:then $$$|r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1$$$, $$$|1-3|=2$$$, $$$1<2$$$; and $$$|r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2$$$, $$$|1-4|=3$$$, $$$2<3$$$. It doesn't satisfy the rule.However, on a $$$3\times3$$$ chessboard, you can place four pieces like this:

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

  int n;
  rd(n);

  auto m = n / 2 + 1;
  writeln(m);
  for (int i = 1, r = 1, c = 1; i <= n; i++) {
    writeln(r, " ", c);
    if (i & 1) {
      c++;
    } else {
      r++;
    }
  }

}

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.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
    GC.disable();

    int n;
    readf(" %s", n);

    if (n == 1) {
        writeln("1\n1 1\n");
        return;
    }

    writeln((n + 2) / 2);

    int c = 0;
    foreach (i; 0 .. n / 2 + 1) {
        c++;
        if (c > n)
            break;
        writefln("%s %s", i + 1, i + 1);
        c++;
        if (c > n)
            break;
        writefln("%s %s", i + 1, i + 2);
    }

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
  GC.disable();

  int n;
  readf(" %s", n);

  writeln( n / 2 + 1);
  foreach(i; 1..n+1) {
    if (i % 2==1) {
      auto idx = (i+1)/2;
      writeln(idx, " ", idx);
    } else {
      auto idx = i/2;
      writeln(idx, " ", idx+1);
    }
  }
}

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; }
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 x = n / 2;
	writeln(x + 1);
	foreach (i; 0..n)
	{
		writeln(i / 2 + 1, " ", (i+1) / 2 + 1);
	}

	//writeln(ans);
	stdout.flush();
	debug readln();
}


In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:  a1 ≤ a2 ≤ a3 ≤ a4  a1 = a2  a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?

The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.

The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

void main() {
	readln;
	uint[uint] aa;

	foreach(a; readln.strip.splitter(' ').map!(a => a.to!uint).array)
		aa[a]++;

	auto keys = aa.keys;
	sort(keys);

	foreach_reverse(k; keys) {
		auto cnt = &aa[k];

		if(*cnt % 2) {
			(*cnt)--;
			aa[k - 1]++;
		}
	}

	//keys = aa.keys;
	//sort(keys);

	BigInt res;

	uint p;

	foreach_reverse(k; keys) {
		int cnt = aa[k] / 2;

		if(!cnt) continue;

		if(p) {
			res += ulong(p) * k;
			cnt--;
		}

		res += (ulong(k) * k) * (cnt / 2);
		p = cnt % 2 ? k : 0;
 	}

	res.write;
}

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

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    for (int i = cast(int)nums.length - 1, next; i >= 0; i = next)
    {
        auto d = cnt[i] / 4;
        auto r = cnt[i] % 4;
        ans += cast(long)nums[i] * nums[i] * d;
        if (i > 0)
        {
            if (r == 0)
            {
                next = i - 1;
                continue;
            }
            else if (r == 1)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ++ cnt[i - 1];
                }
                next = i - 1;
                continue;
            }
            else if (r == 3)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ans += cast(long)nums[i] * nums[i - 1];
                    -- cnt[i - 1];
                    next = i - 1;
                    continue;
                }
            }
            int j;
            for (j = i - 1; j >= 0; -- j)
            {
                if (cnt[j] >= 2) break;
                if (j > 0 && nums[j] - nums[j - 1] == 1) break;
            }
            if (j == -1) break;
            if (cnt[j] >= 2)
            {
                ans += cast(long)nums[i] * nums[j];
                cnt[j] -= 2;
                next = j;
            }
            else
            {
                ans += cast(long)nums[i] * nums[j - 1];
                -- cnt[j - 1];
                next = j - 1;
            }
        }
        else
        {
            next = -1;
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *sa[]...) {
		loop: foreach(ref e; r.equalRange(s))
			if(*e.p && e.p != s.p) {
				foreach(a; sa) if(e.p == a.p) continue loop;
				return e;
			}
		return S.init;
	}

	ulong square(ref S a1) {
		S a2 = equalS(a1);

		if(!a2.len) return 0;

		S b1 = equalS(a1, &a2), b2;

		if(b1.len) b2 = equalS(a1, &a2, &b1);

		if(!b2.len)
			foreach_reverse(ref e; r.lowerBound(a1)) {
				if(!*e.p)
					continue;

				b2 = equalS(e, &a1, &a2);

				if(b2.len) {
					b1 = e;
					break;
				}
			}

		//writefln(`%s %s %s %s`, a1, a2, b1, b2);

		if(b2.len) {
			*a1.p = 0;
			*a2.p = 0;
			*b1.p = 0;
			*b2.p = 0;
			return ulong(a1.len) * b1.len;
		}

		return 0;
	}

	BigInt res;

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *s2 = null) {
		foreach(ref e; r.equalRange(s)) {
			//e.writeln;
			if(*e.p && e.p != s.p && (!s2 || e.p != s2.p))
				return e;
		}
		return S.init;
	}

	ulong square(ref S s) {
		S a = equalS(s);

		if(!a.len) return 0;

		S b, c;

		foreach_reverse(ref e; r.lowerBound(a)) {
			if(e.p != a.p) {
				c = equalS(e, &a);

				if(c.len) {
					b = e;
					break;
				}
			}
		}

		if(b.len) {
			*a.p = 0;
			*b.p = 0;
			*s.p = 0;
			*c.p = 0;
			return ulong(a.len) * b.len;
		}

		return 0;
	}

	BigInt res;// = square(ss[$ - 1]);

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;

	static if(0) {
	auto t = arr.redBlackTree!(/*(a, b) => a.len < b.len,*/ true);

	;

	uint canMake(uint len) {
		uint a;

		foreach(i; 0..2) {
			auto r = t.equalRange(len);
			if(r.empty) return 0;
			if(!i) r.popBack;
			if(!r.empty) {
				a = len;
				break;
			}
			len--;
		}

		uint b;

		{
			auto r = t.lowerRange(a);

			while(!r.empty) {
				b = r.back;
				r.popBack;
			}
		}

		/*auto a = t.equalRange(len).length > 1 ? len : (t.equalRange(len - 1).length ? len - 1 : 0);

		if(!a) return 0;

        foreach_reverse(k; t.lowerRange(a)) {}*/
        return 0;
	}

	foreach_reverse(len; arr) {
		;
	}
	}
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *sa[]...) {
		loop: foreach(ref e; r.equalRange(s))
			if(*e.p && e.p != s.p) {
				foreach(a; sa) if(e.p == a.p) continue loop;
				return e;
			}
		return S.init;
	}

	ulong square(ref S a1) {
		S a2 = equalS(a1);

		if(!a2.len) return 0;

		S b1 = equalS(a1, &a2), b2;

		if(b1.len) b2 = equalS(a1, &a2, &b1);

		if(!b2.len)
			foreach_reverse(ref e; r.lowerBound(a1)) {
				if(!(*e.p))
					continue;

				b2 = equalS(e);

				if(b2.len) {
					b1 = e;
					break;
				}
			}

		if(b2.len) {
			*a1.p = 0;
			*a2.p = 0;
			*b1.p = 0;
			*b2.p = 0;
			return ulong(a1.len) * b1.len;
		}

		return 0;
	}

	BigInt res;// = square(ss[$ - 1]);

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;

	static if(0) {
	auto t = arr.redBlackTree!(/*(a, b) => a.len < b.len,*/ true);

	;

	uint canMake(uint len) {
		uint a;

		foreach(i; 0..2) {
			auto r = t.equalRange(len);
			if(r.empty) return 0;
			if(!i) r.popBack;
			if(!r.empty) {
				a = len;
				break;
			}
			len--;
		}

		uint b;

		{
			auto r = t.lowerRange(a);

			while(!r.empty) {
				b = r.back;
				r.popBack;
			}
		}

		/*auto a = t.equalRange(len).length > 1 ? len : (t.equalRange(len - 1).length ? len - 1 : 0);

		if(!a) return 0;

        foreach_reverse(k; t.lowerRange(a)) {}*/
        return 0;
	}

	foreach_reverse(len; arr) {
		;
	}
	}
}

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

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    for (int i = cast(int)nums.length - 1, next; i >= 0; i = next)
    {
        auto d = cnt[i] / 4;
        auto r = cnt[i] % 4;
        ans += cast(long)nums[i] * nums[i] * d;
        if (i > 0)
        {
            if (r == 0)
            {
                next = i - 1;
                continue;
            }
            else if (r == 1)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ++ cnt[i - 1];
                }
                next = i - 1;
                continue;
            }
            else if (r == 3)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ans += cast(long)nums[i] * nums[i - 1];
                    -- cnt[i - 1];
                    next = i - 1;
                    continue;
                }
            }
            int j;
            for (j = i - 1; j >= 0; -- j)
            {
                if (cnt[j] >= 2) break;
                if (j > 0 && nums[j] - nums[j - 1] == 1) break;
            }
            if (j == -1) break;
            if (cnt[j] == 2)
            {
                ans += cast(long)nums[i] * nums[j];
                cnt[j] -= 2;
                next = j;
            }
            else
            {
                ans += cast(long)nums[i] * nums[j - 1];
                -- cnt[j - 1];
                next = j - 1;
            }
        }
        else
        {
            next = -1;
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

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

long calc(int a, int b, int c, int d)
{
    if (b - a > 1 || d - c > 1) return 0;
    return cast(long)a * c;
}

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    for (int i = 0; i <= arr.length - 4; ++ i)
    {
        auto ret = calc(arr[i], arr[i + 1], arr[i + 2], arr[i + 3]);
        ans = cast(long)fmax(ans, ret);
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

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

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    int pos = -1;
    for (int i = cast(int)nums.length - 1; i >= 0; -- i)
    {
        if (cnt[i] >= 2 || i < nums.length - 1 && nums[i + 1] - nums[i] == 1)
        {
            pos = i;
            if (i < nums.length - 1 && nums[i + 1] - nums[i] == 1) cnt[i] += cnt[i + 1];
            break;
        }
    }
    if (pos != -1)
    {
        if (cnt[pos] >= 4)
        {
            ans = cast(long)nums[pos] * nums[pos];
        }
        else
        {
            for (int i = pos - 1; i >= 0; -- i)
            {
                if (cnt[i] >= 2 || nums[i + 1] - nums[i] == 1 && (i < pos - 1 || cnt[pos] == 3))
                {
                    ans = cast(long)nums[pos] * nums[i];
                    break;
                }
            }
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}


Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square.Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits.

The first line contains two space-separated integers n, k (1 ≤ n, k ≤ 50). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). It is guaranteed that all given squares are distinct.

In a single line print two space-separated integers x and y (0 ≤ x, y ≤ 109) — the coordinates of the point that belongs to exactly k squares. If there are multiple answers, you are allowed to print any of them.  If there is no answer, print "-1" (without the quotes).

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

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int k = cin.readInt;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.readInt;
                }
                if (n - k < 0) {
                        writeln(-1);
                        return;
                }
                reverse(sort(arr));
                writeln(arr[k - 1], " ", 0);
        }
}

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

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int k = cin.readInt;
                int d = n - k;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.readInt;
                }
                if (d < 0) {
                        writeln(-1);
                        return;
                }
                sort(arr);
                d--;
                if (arr[d + 1] - arr[d] == 1) writeln(-1);
                else writeln(arr[d] + 1, " ", 0);
        }
}