Let's define the value of the permutation $$$p$$$ of $$$n$$$ integers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once) as follows:  initially, an integer variable $$$x$$$ is equal to $$$0$$$;  if $$$x < p_1$$$, then add $$$p_1$$$ to $$$x$$$ (set $$$x = x + p_1$$$), otherwise assign $$$0$$$ to $$$x$$$;  if $$$x < p_2$$$, then add $$$p_2$$$ to $$$x$$$ (set $$$x = x + p_2$$$), otherwise assign $$$0$$$ to $$$x$$$;  ...  if $$$x < p_n$$$, then add $$$p_n$$$ to $$$x$$$ (set $$$x = x + p_n$$$), otherwise assign $$$0$$$ to $$$x$$$;  the value of the permutation is $$$x$$$ at the end of this process. For example, for $$$p = [4, 5, 1, 2, 3, 6]$$$, the value of $$$x$$$ changes as follows: $$$0, 4, 9, 0, 2, 5, 11$$$, so the value of the permutation is $$$11$$$.You are given an integer $$$n$$$. Find a permutation $$$p$$$ of size $$$n$$$ with the maximum possible value among all permutations of size $$$n$$$. If there are several such permutations, you can print any of them.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 97$$$) — the number of test cases. The only line of each test case contains one integer $$$n$$$ ($$$4 \le n \le 100$$$).

For each test case, print $$$n$$$ integers — the permutation $$$p$$$ of size $$$n$$$ with the maximum possible value among all permutations of size $$$n$$$.

import std;

void main()
{
    int t;
    scanf("%d\n", &t);
    foreach(_; 0..t) {
        int n;
        scanf("%d\n", &n);
        int[] res = new int[n];
        res[n-1] = n;
        res[n-2] = n - 1;

        int j = 0;
        if (n % 2 == 0)
            foreach(i; iota(n-2,0,-1))
                res[j++] = i;
        else {
            ++j;
            res[0] = 1;
            foreach(i; iota(n-2,1,-1))
                res[j++] = i;
        }

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

}

import std;

void main()
{
    int t;
    scanf("%d\n", &t);
    foreach(_; 0..t) {
        int n;
        scanf("%d\n", &n);
        int[] res = new int[n];
        res[n-1] = n;
        res[n-2] = n - 1;
        res[n-3] = 1;
        int j = 0;
        foreach(i; iota(n-2,1,-1)) {
            res[j++] = i;
        }
        writefln("%(%s %)", res);
    }

}

import std;

void main()
{
    int t;
    scanf("%d\n", &t);
    foreach(_; 0..t) {
        int n;
        scanf("%d\n", &n);
        int[] res = new int[n];
        res[n-1] = n;
        res[n-2] = n - 1;
        res[n-3] = 1;
        int j = 0;
        foreach(i; iota(n-2,1,-1)) {
            res[j++] = i;
        }
        writefln("%( %s%)", res);
    }

}


It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.

The first line contains integers n, m and k (1 ≤ n ≤ 104, 1 ≤ m, k ≤ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell — it is an integer from 0 to 105.

Print a single number — the maximum number of diamonds Joe can steal.

NoteIn the second sample Joe can act like this:The diamonds' initial positions are 4 1 3.During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.Now Joe leaves with 2 diamonds in his pocket.

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

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

    auto arr = readln.chomp.split.map!(to!int).array;

    if (n == 1) {
        writeln(min(arr.front, m.to!long * k));
        return;
    }

    if (n % 2 != 1 || m < n/2 + 1) {
        writeln(0);
        return;
    }

    int ops = m / (n/2 + 1);
    int mn = arr.stride(2).minElement;

    auto ans = min(mn, ops.to!long * k);

    ans.writeln;
}

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

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

    auto arr = readln.chomp.split.map!(to!int).array;

    if (n == 1) {
        writeln(min(arr.front, m.to!long * k));
        return;
    }

    if (n % 2 != 1 || m < n/2 + 1) {
        writeln(0);
        return;
    }

    int ops = m / (n/2 + 1);
    int mn = min(arr.front, arr.back);

    auto ans = min(mn, ops.to!long * k);

    ans.writeln;
}

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

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

    auto arr = readln.chomp.split.map!(to!int).array;

    if (n == 2 || m < n - 1) {
        writeln(0);
        return;
    }

    int ops = m / (n-1);
    int mn = min(arr.front, arr.back);

    auto ans = min(mn, ops.to!long * k);

    ans.writeln;
}

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

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

    auto arr = readln.chomp.split.map!(to!int).array;

    if (n == 1 || n % 2 != 1 || m < n/2 + 1) {
        writeln(0);
        return;
    }

    int ops = m / (n/2 + 1);
    int mn = min(arr.front, arr.back);

    auto ans = min(mn, ops.to!long * k);

    ans.writeln;
}

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

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

    auto arr = readln.chomp.split.map!(to!int).array;

    if (n == 1 || n % 2 != 1 || m < n - 1) {
        writeln(0);
        return;
    }

    int ops = m / (n-1);
    int mn = min(arr.front, arr.back);

    auto ans = min(mn, ops.to!long * k);

    ans.writeln;
}


It is Professor R's last class of his teaching career. Every time Professor R taught a class, he gave a special problem for the students to solve. You being his favourite student, put your heart into solving it one last time.You are given two polynomials $$$f(x) = a_0 + a_1x + \dots + a_{n-1}x^{n-1}$$$ and $$$g(x) = b_0 + b_1x + \dots + b_{m-1}x^{m-1}$$$, with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to $$$1$$$ for both the given polynomials. In other words, $$$gcd(a_0, a_1, \dots, a_{n-1}) = gcd(b_0, b_1, \dots, b_{m-1}) = 1$$$. Let $$$h(x) = f(x)\cdot g(x)$$$. Suppose that $$$h(x) = c_0 + c_1x + \dots + c_{n+m-2}x^{n+m-2}$$$. You are also given a prime number $$$p$$$. Professor R challenges you to find any $$$t$$$ such that $$$c_t$$$ isn't divisible by $$$p$$$. He guarantees you that under these conditions such $$$t$$$ always exists. If there are several such $$$t$$$, output any of them.As the input is quite large, please use fast input reading methods.

The first line of the input contains three integers, $$$n$$$, $$$m$$$ and $$$p$$$ ($$$1 \leq n, m \leq 10^6, 2 \leq p \leq 10^9$$$),  — $$$n$$$ and $$$m$$$ are the number of terms in $$$f(x)$$$ and $$$g(x)$$$ respectively (one more than the degrees of the respective polynomials) and $$$p$$$ is the given prime number. It is guaranteed that $$$p$$$ is prime. The second line contains $$$n$$$ integers $$$a_0, a_1, \dots, a_{n-1}$$$ ($$$1 \leq a_{i} \leq 10^{9}$$$) — $$$a_i$$$ is the coefficient of $$$x^{i}$$$ in $$$f(x)$$$. The third line contains $$$m$$$ integers $$$b_0, b_1, \dots, b_{m-1}$$$ ($$$1 \leq b_{i} \leq 10^{9}$$$)  — $$$b_i$$$ is the coefficient of $$$x^{i}$$$ in $$$g(x)$$$.

Print a single integer $$$t$$$ ($$$0\le t \le n+m-2$$$)  — the appropriate power of $$$x$$$ in $$$h(x)$$$ whose coefficient isn't divisible by the given prime $$$p$$$. If there are multiple powers of $$$x$$$ that satisfy the condition, print any.

NoteIn the first test case, $$$f(x)$$$ is $$$2x^2 + x + 1$$$ and $$$g(x)$$$ is $$$x + 2$$$, their product $$$h(x)$$$ being $$$2x^3 + 5x^2 + 3x + 2$$$, so the answer can be 1 or 2 as both 3 and 5 aren't divisible by 2.In the second test case, $$$f(x)$$$ is $$$x + 2$$$ and $$$g(x)$$$ is $$$x + 3$$$, their product $$$h(x)$$$ being $$$x^2 + 5x + 6$$$, so the answer can be any of the powers as no coefficient is divisible by the given prime.

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

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader ();
  const n = r.next!uint ();
  const m = r.next!uint ();
  const p = r.next!uint ();
  auto a = r.nextA!uint (n);
  auto b = r.nextA!uint (m);
  int i, j;
  for (i = n - 1; i >= 0 && !(a[i] % p); --i) {}
  for (j = m - 1; j >= 0 && !(b[j] % p); --j) {}
  writeln (i + j);
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    int n = scan!int, m = scan!int;
    long p = scan!long;

    long[] as = scan!long(n);
    long[] bs = scan!long(m);

    int imin, jmin;
    foreach(i, a; as) if(a % p != 0){
        imin = i.to!int;
        break;
    }
    foreach(j, b; bs) if(b % p != 0){
        jmin = j.to!int;
        break;
    }
    print(imin + jmin);
}

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

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader ();
  const n = r.next!uint ();
  const m = r.next!uint ();
  const p = r.next!uint ();
  auto a = r.nextA!uint (n);
  auto b = r.nextA!uint (m);
  int i, j;
  for (i = n - 1; i >= 0 && !(a[i] % p); --i) {}
  for (j = m - 1; j >= 0 && !(a[j] % p); --j) {}
  writeln (i + j);
}


On a random day, Neko found $$$n$$$ treasure chests and $$$m$$$ keys. The $$$i$$$-th chest has an integer $$$a_i$$$ written on it and the $$$j$$$-th key has an integer $$$b_j$$$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The $$$j$$$-th key can be used to unlock the $$$i$$$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $$$a_i + b_j \equiv 1 \pmod{2}$$$. One key can be used to open at most one chest, and one chest can be opened at most once.Find the maximum number of chests Neko can open.

The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^5$$$) — the number of chests and the number of keys. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the numbers written on the treasure chests. The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \leq b_i \leq 10^9$$$) — the numbers written on the keys.

Print the maximum number of chests you can open.

NoteIn the first example, one possible way to unlock $$$3$$$ chests is as follows:  Use first key to unlock the fifth chest,  Use third key to unlock the second chest,  Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).In the third example, no key can unlock the given chest.

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(to!long).array;
    auto B = readln.split.map!(to!long).array;

    long a = A.map!(a => a % 2 == 0).sum;
    long b = A.map!(a => a % 2 == 1).sum;
    long c = B.map!(a => a % 2 == 0).sum;
    long d = B.map!(a => a % 2 == 1).sum;

    writeln(min(a, d) + min(b, c));
}

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

void main()
{
    int[] fst = readln().split.map!"a.to!int".array;
    int[] snd = readln().split.map!"a.to!int".array;
    int[] thr = readln().split.map!"a.to!int".array;

    int aUG = snd.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int aG = snd.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;
    int bUG = thr.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int bG = thr.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;

    (min(aUG, bG) + min(aG, bUG)).writeln;
}

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

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

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
}

void main() {
  auto r = new InputReader;
  int n = r.next!int, m = r.next!int;
  auto f (int k) {
    auto c = new int[2];
    foreach (i; 0 .. k) {
      ++c[1 & r.next!int];
    }
    return c;
  }
  auto a = f (n), b = f (m);
  writeln (min (a[0], b[1]) + min (a[1], b[0]));
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
// import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

	int n = read.to!int;
	int m = read.to!int;

	long[] as = readln.chomp.split.to!(long[]);
	long[] bs = readln.chomp.split.to!(long[]);

	int oddacnt, oddbcnt, evenacnt, evenbcnt;
	foreach(a; as) if(a % 2 == 0) evenacnt += 1; else oddacnt += 1;
	foreach(b; bs) if(b % 2 == 0) evenbcnt += 1; else oddbcnt += 1;

	int ans = min(evenacnt, oddbcnt) + min(oddacnt, evenbcnt);

	ans.writeln;

}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

long mod = 10^^9 + 7;
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 M = RD!int;
	auto a = RDR.ARR;
	auto b = RDR.ARR;

	long cnt_a;
	foreach (i; 0..N)
	{
		if (a[i] % 2 == 1)
			++cnt_a;
	}
	long cnt_b;
	foreach (i; 0..M)
	{
		if (b[i] % 2 == 1)
			++cnt_b;
	}
	long ans;
	ans += min(cnt_a, M - cnt_b);
	ans += min(cnt_b, N - cnt_a);

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

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

void main()
{
    int[] fst = readln().split.map!"a.to!int".array;
    int[] snd = readln().split.map!"a.to!int".array;
    int[] thr = readln().split.map!"a.to!int".array;

    int a = snd.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int b = snd.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;
    int c = thr.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int d = thr.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;

    min(a, b, c, d).writeln;
}

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

void main()
{
    int[] fst = readln().split.map!"a.to!int".array;
    int[] snd = readln().split.map!"a.to!int".array;
    int[] thr = readln().split.map!"a.to!int".array;

    int aUG = snd.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int aG = snd.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;
    int bUG = thr.filter!(x => x % 2 == 1)
        .array
        .length
        .to!int;
    int bG = thr.filter!(x => x % 2 == 0)
        .array
        .length
        .to!int;

    max(min(aUG, bG), min(aG, bUG)).writeln;
}


Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.There are  non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant.

The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of balls. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ n) where ti is the color of the i-th ball.

Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.

NoteIn the first sample, color 2 is dominant in three intervals:  An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color.  An interval [4, 4] contains one ball, with color 2 again.  An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them.

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).map !(q{a - 1}).array;
		auto t = new int [n];
		foreach (i; 0..n)
		{
			alias Pair = Tuple !(int, q{quantity}, int, q{color});
			auto c = new int [n];
			auto v = Pair (0, -0);
			foreach (j; i..n)
			{
				auto curColor = a[j];
				c[curColor]++;
				debug {writefln ("c[%s] = %s",
				    c[curColor], curColor);}
				v = max (v, Pair (c[curColor], -curColor));
				t[-v.color]++;
			}
		}
		writefln ("%(%s %)", t);
	}
}


You are given one integer $$$n$$$ ($$$n > 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.

For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main () {
	auto tests = readln.strip.to!int;
	foreach (test; 0..tests)
		readln.strip.to!int.iota.map!q{a+1}.drop(1).chain(1.only).writefln!"%(%s %)";
}

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

string FMT_F = "%.10f";

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

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

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

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;
		foreach (i; 0..n)
		{
			ans[ti] ~= (i+1)%n + 1;
		}
	}

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


The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.

The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;
import std.array;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

struct Round
{
	string name;
	int points;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	Round[] rounds; rounds.length = n;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		rounds[i].name = nm;
		rounds[i].points = points;
	}

	int max = int.min;
	foreach (string key; scoreboard.byKey)
	{
		if (scoreboard[key] > max) max = scoreboard[key];
	}

	bool[string] maxers;
	foreach (string key; scoreboard.byKey)
	{
		if (scoreboard[key] == max)
			maxers[key] = true;
		scoreboard[key] = 0;
	}

	string name = "";
	foreach (int i; 0..n)
	{
		scoreboard[rounds[i].name] += rounds[i].points;
		if (rounds[i].name in maxers && scoreboard[rounds[i].name] >= max)
		{
			name = rounds[i].name;
			break;
		}
	}

	write (name);
	return 0;
}

import std.stdio;
import std.ascii;
import std.conv;
import std.algorithm;
import std.container;
import std.exception;

string nextToken() {
    char c;
    string res;
    while (readf("%s", &c) == 1) {
        if (isWhite(c)) {
            if (res.length > 0)
                break;
        } else
            res ~= c;
    }
    return res;
}

auto read(T)() {
    return nextToken.to!T;
}
bool read(T)(ref T val) {
    try {
        val = read!T;
        return true;
    } catch (ConvException)
        return false;
}

void reset(T)(ref T val) {
    val = T.init;
}

bool solve(int test) {
    int n;
    if (!read(n))
        return false;

    struct op {
        string name;
        int val;
    }
    op[] ops = new op[n];
    int score[string];

    foreach (ref cur; ops) {
        cur.name = read!string;
        cur.val = read!int;

        if (cur.name !in score)
            score[cur.name] = 0;
        score[cur.name] += cur.val;
    }
    int maxScore = 0;
    foreach (cur; score.byValue)
        maxScore = max(maxScore, cur);

    byte[string] maxPlayers;
    foreach (name, cur; score) {
        if (cur == maxScore)
            maxPlayers[name] = 0;
    }

    score.reset;
    bool found = false;
    foreach (cur; ops) {
        if (cur.name !in maxPlayers)
            continue;
        if (cur.name !in score)
            score[cur.name] = 0;
        score[cur.name] += cur.val;
        if (score[cur.name] >= maxScore) {
            writeln(cur.name);
            found = true;
            break;
        }
    }
    assert(found);
    return true;
}

void main() {
    int test = 0;
    while (solve(test))
        test++;
}

import std.stdio;
import std.algorithm;

struct Score
{
    public string player;
    public int score;
}

void main()
{
    int n;
    Score[] game;
    int[string] totalScores;

    readf(" %d", &n);
    for (int i = 0; i < n; ++i)
    {
        string player;
        int score;

        readf(" %s %d", &player, &score);
        game ~= Score(player, score);
        totalScores[player] += score;
    }

    int maxScore = 0;
    foreach (score; totalScores.values)
    {
        if (score > maxScore)
        {
            maxScore = score;
        }
    }

    string[] potentialWinners;
    foreach (player, score; totalScores)
    {
        if (score == maxScore)
        {
            potentialWinners ~= player;
        }
    }

    totalScores.clear();

    string winner;
    foreach (con; game)
    {
        totalScores[con.player] += con.score;
        if (totalScores[con.player] >= maxScore && canFind(potentialWinners, con.player))
        {
            winner = con.player;
            break;
        }
    }

    writeln(winner);
}

import std.stdio, std.array, std.algorithm;

void main() {
	uint n;
	long[string] score;
	long[string] s2;
	string[] gn;
	long[] gs;
	readf("%s", &n);
	string mname;
	foreach(i; 0 .. n) {
		long sc;
		string name;
		readf("\n%s %d", &name, &sc);
		score[name] += sc;
		gn ~= name;
		gs ~= sc;
	}
	auto m = score.reduce!(max);

	foreach(i; 0 .. n) {
		s2[gn[i]] += gs[i];
		if (s2[gn[i]] >= m && score[gn[i]] == m) {
			writeln(gn[i]);
			break;
		}
	}
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;

int main()
{
	int n;
	scanf("%d", &n);
	int[string] scoreboard;
	char[32] name;
	string max = "";
	int max_points = 0;
	foreach (int i; 0..n)
	{
		int points;
		scanf("%s %d", &name, &points);
		string nm = to!string(name);
		scoreboard[nm] += points;
		if (scoreboard[nm] > max_points)
		{
			max = nm;
			max_points = scoreboard[nm];
		}
	}
	write(max);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;
import std.array;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string[] max;
	max ~= "";
	int[] max_points;
	max_points ~= int.min;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (nm == max.back)
		{
			max_points.back = scoreboard[nm];
			if (max_points[$-1] < max_points[$-2])
			{
				max_points.popBack();
				max.popBack();
			}
		} else
		if (scoreboard[nm] > max_points.back)
		{
			max ~= nm;
			max_points ~= scoreboard[nm];
		}
	}
	write(max.back);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string max = "";
	int max_points = int.min;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (nm == max)
		{
			max_points = scoreboard[nm];
		} else
		if (scoreboard[nm] > max_points)
		{
			max = nm;
			max_points = scoreboard[nm];
		}
	}
	write(max);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;
import std.array;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string[int] maxers;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (!(scoreboard[nm] in maxers))
		{
			maxers[scoreboard[nm]] = nm;
		}
	}

	int max = int.min;
	foreach (string key; scoreboard.byKey)
	{
		if (scoreboard[key] > max)
			max = scoreboard[key];
	}
	write(maxers[max]);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;
import std.array;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	int[string] pos;
	int[string] ms;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		int pv = (nm in scoreboard ? scoreboard[nm] : int.min);
		scoreboard[nm] += points;
		if (nm in ms)
		{
			if (scoreboard[nm] > ms[nm])
			{
				ms[nm] = scoreboard[nm];
				pos[nm] = i;
			}
		} else {
			ms[nm] = scoreboard[nm];
			pos[nm] = i;
		}
	}

	int max = int.min;
	int idx = -1;
	string name = "";
	foreach (string key; scoreboard.byKey)
	{
		if (scoreboard[key] > max)
		{
			max = scoreboard[key];
			name = key;
			idx = pos[key];
		} else
		if (scoreboard[key] == max && pos[key] < idx)
		{
			idx = pos[key];
			max = scoreboard[key];
			name = key;
		}
	}
	write(name);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string max = "";
	int max_points = int.min;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (scoreboard[nm] > max_points)
		{
			max = nm;
			max_points = scoreboard[nm];
		}
	}
	write(max);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string max = "";
	int max_points = 0;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (scoreboard[nm] > max_points)
		{
			max = nm;
			max_points = scoreboard[nm];
		}
	}
	write(max);
	return 0;
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;
import std.string;
import core.stdc.stdio;
import core.stdc.string;
import std.array;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

int main()
{
	int n;
	string l = readline();
	n = parse!int(l);
	int[string] scoreboard;
	string[] max;
	max ~= "";
	int[] max_points;
	max_points ~= int.min;
	string[int] maxers;
	foreach (int i; 0..n)
	{
		int points;
		l = readline();
		string[] values = split(l);
		points = parse!int(values[1]);
		string nm = values[0];
		scoreboard[nm] += points;
		if (nm == max.back)
		{
			max_points.back = scoreboard[nm];
			if (max_points[$-1] < max_points[$-2])
			{
				max_points.popBack();
				max.popBack();
			} else {
				if (!(scoreboard[nm] in maxers))
					maxers[scoreboard[nm]] = nm;
			}
		} else
		if (scoreboard[nm] > max_points.back)
		{
			if (!(scoreboard[nm] in maxers))
				maxers[scoreboard[nm]] = nm;
			max ~= nm;
			max_points ~= scoreboard[nm];
		}
	}
	write(maxers[max_points.back]);
	return 0;
}

import std.stdio;

struct Score
{
    public string player;
    public int score;
}

void main()
{
    int n;
    Score[] game;
    int[string] totalScores;

    readf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        string player;
        int score;

        readf("%s %d", &player, &score);
        game ~= Score(player, score);
        totalScores[player] += score;
    }

    int maxScore = 0;
    foreach (score; totalScores.values)
    {
        if (score > maxScore)
        {
            maxScore = score;
        }
    }
    totalScores.clear();

    string winner;
    foreach (con; game)
    {
        totalScores[con.player] += con.score;
        if (totalScores[con.player] == maxScore)
        {
            winner = con.player;
            break;
        }
    }

    writeln(winner);
}

import std.stdio, std.array, std.algorithm;

void main() {
	long n;
	long[string] score;
	readf("%s\n", &n);
	long max = 0;
	string mname;
	foreach(i; 0 .. n) {
		long sc;
		string name;
		readf("%s %d\n", &name, &sc);
		score[name] += sc;
		if (score[name] > max) {
			mname = name;
			max = score[name];
		}
	}
	writeln(mname);
}


The 2050 volunteers are organizing the "Run! Chase the Rising Sun" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are $$$n+1$$$ checkpoints on the trail. They are numbered by $$$0$$$, $$$1$$$, ..., $$$n$$$. A runner must start at checkpoint $$$0$$$ and finish at checkpoint $$$n$$$. No checkpoint is skippable — he must run from checkpoint $$$0$$$ to checkpoint $$$1$$$, then from checkpoint $$$1$$$ to checkpoint $$$2$$$ and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are $$$m$$$ different paths to choose. For any $$$1\le i\le n$$$, to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$, a runner can choose exactly one from the $$$m$$$ possible paths. The length of the $$$j$$$-th path between checkpoint $$$i-1$$$ and $$$i$$$ is $$$b_{i,j}$$$ for any $$$1\le j\le m$$$ and $$$1\le i\le n$$$.To test the trail, we have $$$m$$$ runners. Each runner must run from the checkpoint $$$0$$$ to the checkpoint $$$n$$$ once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length $$$l_i$$$ between checkpoint $$$i-1$$$ and $$$i$$$ ($$$1\le i\le n$$$), his tiredness is $$$$$$\min_{i=1}^n l_i,$$$$$$ i. e. the minimum length of the paths he takes.Please arrange the paths of the $$$m$$$ runners to minimize the sum of tiredness of them.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 100$$$). The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i,1}$$$, $$$b_{i,2}$$$, ..., $$$b_{i,m}$$$ ($$$1 \le b_{i,j} \le 10^9$$$). It is guaranteed that the sum of $$$n\cdot m$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, output $$$n$$$ lines. The $$$j$$$-th number in the $$$i$$$-th line should contain the length of the path that runner $$$j$$$ chooses to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$. There should be exactly $$$m$$$ integers in the $$$i$$$-th line and these integers should form a permuatation of $$$b_{i, 1}$$$, ..., $$$b_{i, m}$$$ for all $$$1\le i\le n$$$. If there are multiple answers, print any.

NoteIn the first case, the sum of tiredness is $$$\min(2,5) + \min(3,3) + \min(4,1) = 6$$$.  In the second case, the sum of tiredness is $$$\min(2,4,3) + \min(3,1,5) = 3$$$.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		int [] [] a;
		foreach (i; 0..n)
		{
			a ~= readln.splitter.map !(to !(int)).array;
		}
		foreach (i; 0..n)
		{
			sort (a[i]);
		}
		auto answer = new int [] [] (n, m);
		foreach (j; 0..m)
		{
			int best = 0;
			foreach (i; 1..n)
			{
				if (!a[i].empty && a[best].front > a[i].front)
				{
					best = i;
				}
			}
			answer[best][j] = a[best].front;
			a[best].popFront ();
		}
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				if (answer[i][j] == 0)
				{
					answer[i][j] = a[i].front;
					a[i].popFront ();
				}
			}
		}
		foreach (i; 0..n)
		{
			writefln !("%(%s %)") (answer[i]);
		}
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto B = new long[][](N, M);
        foreach (i; 0 .. N) {
          foreach (j; 0 .. M) {
            B[i][j] = readLong();
          }
          B[i].sort;
        }
        debug {
          writeln("B = ", B);
        }

        auto ls = new int[N];
        auto rs = new int[N];
        ls[] = 0;
        rs[] = M;

        auto ans = new long[][](M, N);
        foreach (h; 0 .. M) {
          int im = -1;
          foreach (i; 0 .. N) {
            if (im == -1 || B[im][ls[im]] > B[i][ls[i]]) {
              im = i;
            }
          }
          debug {
            writeln(ls, " ", rs, " ", im, " ", B[im][ls[im]]);
          }
          ans[h][im] = B[im][ls[im]++];
          foreach (i; 0 .. N) {
            if (im != i) {
              ans[h][i] = B[i][--rs[i]];
            }
          }
        }

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


In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size $$$x$$$ ($$$1 \leq x \leq 26$$$) contains only the first $$$x$$$ Latin letters. For example an alphabet of size $$$4$$$ contains only the characters $$$\texttt{a}$$$, $$$\texttt{b}$$$, $$$\texttt{c}$$$ and $$$\texttt{d}$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.

For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message $$$s$$$.

5

1

a

4

down

10

codeforces

3

bcf

5

zzzzz

NoteFor the first test case, Atilla needs to know only the character $$$\texttt{a}$$$, so the alphabet of size $$$1$$$ which only contains $$$\texttt{a}$$$ is enough.For the second test case, Atilla needs to know the characters $$$\texttt{d}$$$, $$$\texttt{o}$$$, $$$\texttt{w}$$$, $$$\texttt{n}$$$. The smallest alphabet size that contains all of them is $$$23$$$ (such alphabet can be represented as the string $$$\texttt{abcdefghijklmnopqrstuvw}$$$).

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

void main()
{
    auto t = readln().strip().to!int;
    foreach (i; 0..t)
    {
        auto n = readln().strip().to!int;
        auto s = readln().strip().dup.array;
        sort(s);
        writefln("%d", s[$-1] - 'a' + 1);
    }
}

import std;

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!long;
    auto s = readln.strip;
    int ans = 0;
    foreach (ch ; s)
      ans = max(ans, ch - 'a' + 1);
    writeln(ans);
  }
}


You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b \cdot x$$$.You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$$x$$$ becomes equal to $$$y$$$ after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.For example:   if $$$x = 3$$$ and $$$y = 75$$$, you may choose $$$a = 2$$$ and $$$b = 5$$$, so that $$$x$$$ becomes equal to $$$3 \cdot 5 \cdot 5 = 75$$$;  if $$$x = 100$$$ and $$$y = 100$$$, you may choose $$$a = 3$$$ and $$$b = 1$$$, so that $$$x$$$ becomes equal to $$$100 \cdot 1 \cdot 1 \cdot 1 = 100$$$;  if $$$x = 42$$$ and $$$y = 13$$$, there is no answer since you cannot decrease $$$x$$$ with the given operations.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 100$$$).

If it is possible to choose a pair of positive integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$ after the aforementioned process, print these two integers. The integers you print should be not less than $$$1$$$ and not greater than $$$10^9$$$ (it can be shown that if the answer exists, there is a pair of integers $$$a$$$ and $$$b$$$ meeting these constraints). If there are multiple such pairs, print any of them. If it is impossible to choose a pair of integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$, print the integer $$$0$$$ twice.

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto X = scan!int;
      auto Y = scan!int;

      foreach(b; 1..101) {
        int x = X;
        foreach(a; 1..101) {
          x *= b;
          if (x == Y) return "%s %s".format(a, b);
          if (x > Y) break;
        }
      }

      return "0 0";
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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


Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.Two ways are considered different if sets of indexes of elements chosen by these ways are different.Since the answer can be very large, you should find the answer modulo 109 + 7.

First line contains one integer n (1 ≤ n ≤ 105) — the number of elements in the array. Second line contains n integers ai (1 ≤ ai ≤ 70) — the elements of the array.

Print one integer — the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.

NoteIn first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.

module solution;
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.functional;
alias nt = long;
nt inputN;
nt[] inputA;
nt[71] numberOf = 0;
immutable nt mod = 1_000_000_000 + 7;
nt waysSelectEven(nt n)
{
  if (n == 0) return 1;
  return (memoize!waysSelectEven(n - 1) + memoize!waysSelectOdd(n - 1)) % mod;
}
nt waysSelectOdd(nt n)
{
  if (n == 0) return 0;
  return (memoize!waysSelectEven(n - 1) + memoize!waysSelectOdd(n - 1)) % mod;
}
alias seven = memoize!waysSelectEven;
alias sodd = memoize!waysSelectOdd;
bool isPrime(int n)
{
  return !(iota(2, n).any!(t => n % t == 0));
}
immutable primes = iota(int(2), int(70)).filter!isPrime.array;
immutable numberBitmask = iota(nt(0), nt(71)).map!primeDecomp.array;
int primeDecomp(nt n)
{
  if (n == 0) return 0;
  int decomp = 0;
  foreach(i, prime; primes)
    {
      if (prime > n)
	return decomp;
      while(n % prime == 0)
	{
	  decomp ^= int(1) << i;
	  n /= prime;
	}
    }
  return decomp;
}
int main()
{
  readf!" %s"(inputN);
  inputA.length = cast(size_t)(inputN);
  foreach(ref ai; inputA)
    {
      readf!" %s"(ai);
      numberOf[cast(size_t) ai]++;
    }
  enum noMasks = long(1) << 19;
  auto dp = new int[noMasks][71];
  for(nt objective = 0; objective < noMasks; objective++)
    dp[0][cast(size_t)objective] = objective == 0;
  for(nt upperBound = 1; upperBound <= 70; upperBound++)
    {
      auto mseven = seven(numberOf[cast(size_t)upperBound]);
      auto msodd = sodd(numberOf[cast(size_t)upperBound]);
      for(nt objective = 0; objective < noMasks; objective++)
	{
	  nt result = (cast(long)mseven * cast(long)dp[cast(size_t)(upperBound - 1)][cast(size_t)(objective)]) % mod;
	  result %= mod;
	  result += (cast(long)msodd * cast(long)dp[cast(size_t)(upperBound - 1)][cast(size_t)(objective ^ numberBitmask[cast(size_t)upperBound])]) % mod;
	  result %= mod;
	  dp[cast(size_t)upperBound][cast(size_t)objective] = cast(int)result;
	}
    }
  writeln(((dp[70][0] - 1)%mod+mod)%mod);
  return 0;
}


Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav.

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements.

In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise.

NoteIn the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs.

module cf_296A;

import std.stdio;

void main() {
    immutable MAX_NUMBER = 1000;

    int n, num;
    int[MAX_NUMBER + 1] counters;

    readf("%d", &n);
    for (int i = 0; i < n; ++i) {
        readf(" %d", &num);
        ++counters[num];
    }

    bool canExchange = true;
    for (int i = counters.length - 1; i >= 0; --i) {
        if (2 * counters[i] - 1 > n) {
            canExchange = false;
            break;
        }
    }

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

module sigod.codeforces.p296A;

import std.stdio;

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

	int[] array = new int[n];

	foreach (ref a; array) {
		stdin.readf(" %s", &a);
	}

	stdout.writeln(solve(array) ? "YES" : "NO");
}

bool solve(int[] array)
{
	if (array.length == 1) return true;

	int[int] counts;

	foreach (e; array) {
		++counts[e];
	}

	int max = 0;
	foreach (value; counts.byValue) {
		if (value > max) max = value;
	}

	return (max - 1) <= (array.length - max);
}

unittest {
	assert(solve([1]) == true);
	assert(solve([1, 1, 2]) == true);
	assert(solve([7, 7, 7, 7]) == false);
}


The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead.  In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster).

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$A$$$ ($$$1 \leq A \leq 10^6$$$), $$$B$$$ ($$$1 \leq B \leq 10^6$$$) and $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the attack power of the great hero, the initial health value of the great hero, and the number of monsters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$), where $$$a_i$$$ denotes the attack power of the $$$i$$$-th monster. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq 10^6$$$), where $$$b_i$$$ denotes the initial health value of the $$$i$$$-th monster. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the answer: "YES" (without quotes) if the great hero can kill all the monsters. Otherwise, print "NO" (without quotes).

NoteIn the first example: There will be $$$6$$$ fights between the hero and the only monster. After that, the monster is dead and the health value of the hero becomes $$$17 - 6 \times 2 = 5 > 0$$$. So the answer is "YES", and moreover, the hero is still living.In the second example: After all monsters are dead, the health value of the hero will become $$$709$$$, regardless of the order of all fights. So the answer is "YES".In the third example: A possible order is to fight with the $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd and $$$4$$$-th monsters. After all fights, the health value of the hero becomes $$$-400$$$. Unfortunately, the hero is dead, but all monsters are also dead. So the answer is "YES".In the fourth example: The hero becomes dead but the monster is still living with health value $$$1000 - 999 = 1$$$. So the answer is "NO".

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

alias E = Tuple!(long, "a", long, "b");

void main()
{
    int T; get(T);
    while (T--) {
        long A, B; int N; get(A, B, N);
        auto es = new E[](N);
        foreach (i, a; readln.split) es[i].a = a.to!long;
        foreach (i, b; readln.split) es[i].b = b.to!long;
        sort!"a.a < b.a"(es);
        foreach (e; es) with (e) {
            auto t = b / A - (b % A == 0 ? 1 : 0);
            B -= a * t;
            if (B <= 0) {
                writeln("NO");
                goto next;
            }
            B -= a;
        }
        writeln("YES");
        next:
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto index = a.MAKE_IDX;
		bool ok = true;
		foreach (i; index)
		{
			if (B <= 0)
			{
				ok = false;
				break;
			}
			auto cnt = (b[i]+A-1) / A;
			B -= a[i] * cnt;
			if (B <= -a[i])
			{
				ok = false;
				break;
			}
		}
		ans[ti] = ok;
	}

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

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        long A, B; int N; get(A, B, N);
        long[] aa; get(aa);
        long[] bb; get(bb);
        foreach (i; 0..N) {
            auto a = aa[i];
            auto b = bb[i];
            auto t = b / A - (b % A == 0 ? 1 : 0);
            B -= a * t;
            if (B <= 0) {
                writeln("NO");
                goto next;
            }
            B -= a;
        }
        writeln("YES");
        next:
    }
}


Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.

The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).

The output contains a single number — the maximum total gain possible.

NoteIahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

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

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

		auto f = new int [4] [] [] (n, m);
		foreach (k; 0..4)
		{
			foreach (i; 0..n)
			{
				foreach (j; 0..m)
				{
					int cur = 0;
					if (i > 0)
					{
						cur = max (cur, f[i - 1][j][k]);
					}
					if (j > 0)
					{
						cur = max (cur, f[i][j - 1][k]);
					}
					f[i][j][k] = cur + a[i][j];
				}
			}

			auto b = new int [] [] (m, n);
			auto g = new int [4] [] [] (m, n);
			foreach (i; 0..n)
			{
				foreach (j; 0..m)
				{
					b[j][n - 1 - i] = a[i][j];
					g[j][n - 1 - i] = f[i][j];
				}
			}
			a = b;
			f = g;
			swap (m, n);
		}
		debug {writeln (f);}

		int res = 0;
		foreach (i; 1..n - 1)
		{
			foreach (j; 1..m - 1)
			{
				int cur = max (f[i - 1][j][0] +
				               f[i + 1][j][2] +
				               f[i][j - 1][1] +
				               f[i][j + 1][3],
				               f[i - 1][j][3] +
				               f[i + 1][j][1] +
				               f[i][j - 1][0] +
				               f[i][j + 1][2]);
				res = max (res, cur);
			}
		}
		writeln (res);
	}
}


Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of favourite integers of Bob.  The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^{18}$$$) — Bob's favourite integers.

For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).

NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible.

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

void main() {
    auto T = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    foreach (a; A) {
        auto m = a % 14;
        if (a <= 14) {
            writeln("NO");
        } else if (m != 0 && m <= 6) {
            writeln("YES");
        } else {
            writeln("NO");
        }
    }
}

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

bool solve (long n)
{
	return n > 14 && 1 <= n % 14 && n % 14 <= 6;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		long n;
		readf !(" %s") (n);
		writeln (solve (n) ? "YES " : "NO");
	}
}

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

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

void solve(){
	int n = rint;
	long[] as = rlong(n);

	foreach(a; as){
		if(a < 15) "NO".writeln;
		else if(a % 14 >= 1 && a % 14 <= 6) "YES".writeln;
		else "NO".writeln;
	}
}

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 X = readLong();
        const q = X / 14, r = X % 14;
        const ans = (q >= 1 && 1 <= r && r <= 6);
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 t = RD!int;
	auto x = RDA;
	foreach (i; 0..t)
	{
		if (x[i] < 15)
			writeln("NO");
		else
		{
			auto y = x[i] % 14;
			writeln(y <= 6 && y != 0 ? "YES" : "NO");
		}
	}

	stdout.flush;
	debug readln;
}

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

void main() {
    auto T = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    foreach (a; A) {
        auto m = a % 14;
        if (a <= 14) {
            writeln("NO");
        } else if (m <= 6) {
            writeln("YES");
        } else {
            writeln("NO");
        }
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto x = RDA;
	foreach (i; 0..t)
	{
		auto y = x[i] % 14;
		writeln(y <= 6 && y != 0 ? "YES" : "NO");
	}

	stdout.flush;
	debug readln;
}