You are given the strings $$$a$$$ and $$$b$$$, consisting of lowercase Latin letters. You can do any number of the following operations in any order:   if $$$|a| > 0$$$ (the length of the string $$$a$$$ is greater than zero), delete the first character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_2 a_3 \ldots a_n$$$;  if $$$|a| > 0$$$, delete the last character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_1 a_2 \ldots a_{n-1}$$$;  if $$$|b| > 0$$$ (the length of the string $$$b$$$ is greater than zero), delete the first character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_2 b_3 \ldots b_n$$$;  if $$$|b| > 0$$$, delete the last character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_1 b_2 \ldots b_{n-1}$$$. Note that after each of the operations, the string $$$a$$$ or $$$b$$$ may become empty.For example, if $$$a=$$$"hello" and $$$b=$$$"icpc", then you can apply the following sequence of operations:   delete the first character of the string $$$a$$$ $$$\Rightarrow$$$ $$$a=$$$"ello" and $$$b=$$$"icpc";  delete the first character of the string $$$b$$$ $$$\Rightarrow$$$ $$$a=$$$"ello" and $$$b=$$$"cpc";  delete the first character of the string $$$b$$$ $$$\Rightarrow$$$ $$$a=$$$"ello" and $$$b=$$$"pc";  delete the last character of the string $$$a$$$ $$$\Rightarrow$$$ $$$a=$$$"ell" and $$$b=$$$"pc";  delete the last character of the string $$$b$$$ $$$\Rightarrow$$$ $$$a=$$$"ell" and $$$b=$$$"p". For the given strings $$$a$$$ and $$$b$$$, find the minimum number of operations for which you can make the strings $$$a$$$ and $$$b$$$ equal. Note that empty strings are also equal.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains the string $$$a$$$ ($$$1 \le |a| \le 20$$$), consisting of lowercase Latin letters. The second line of each test case contains the string $$$b$$$ ($$$1 \le |b| \le 20$$$), consisting of lowercase Latin letters.

For each test case, output the minimum number of operations that can make the strings $$$a$$$ and $$$b$$$ equal.

5
a
a
abcd
bc
hello
codeforces
hello
helo
dhjakjsnasjhfksafasd
adjsnasjhfksvdafdser

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
void main()
{
    auto st = readln()[].chomp;
    int n = to!int(st);

    foreach(int _; 0..n)
    {
        auto a = readln()[].chomp;
        auto b = readln()[].chomp;

        auto m = min(a.length, b.length);
        int res = a.length+b.length;
        bool match=true;

        for(int i = 0; i < a.length; i++)
        {
            for(int j = i; j < a.length; j++)
            {
                if (b.canFind(a[i..j+1]))
                {
                    res = min(res, abs(a.length-(j-i+1))+abs(b.length-(j-i+1)));
                }
            }
        }

        writeln(res);
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.functional;
void main()
{
    auto st = readln()[].chomp;
    int n = to!int(st);

    foreach(int _; 0..n)
    {
        auto a = readln()[].chomp;
        auto b = readln()[].chomp;

        auto m = min(a.length, b.length);
        int res = a.length+b.length;
        bool match=true;

        for(int i = 0; i < a.length; i++)
        {
            for(int j = i+1; j <= a.length; j++)
            {
                for(int k = 0; k < b.length; k++)
                {
                    if(k+(j-i) > b.length)
                        break;
                    int l = k+(j-i);
                    if(a[i..j].equal(b[k..l]))
                    {
                        int c = a.length - (j-i);
                        int d = b.length-(l-k);
                        res = min(res, c+d);
                    }
                }
            }
        }

        writeln(res);
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.functional;
void main()
{
    auto st = readln()[].chomp;
    int n = to!int(st);

    foreach(int _; 0..n)
    {
        auto a = readln()[].chomp;
        auto b = readln()[].chomp;

        auto m = min(a.length, b.length);
        int res = a.length+b.length;
        bool match=true;

        for(int i = 0; i < a.length; i++)
        {
            for(int j = i+1; j <= a.length; j++)
            {
                for(int k = 0; k < b.length; k++)
                {
                    for(int l = k+1; l <= b.length; l++)
                    {
                        if(a[i..j].equal(b[k..l]))
                        {
                            int c = a.length - (j-i);
                            int d = b.length-(l-k);
                            res = min(res, c+d);
                            break;
                        }
                    }

                }
            }
        }

        writeln(res);
    }
}

// Vicfred
// https://codeforces.com/contest/1506/problem/C
// brute force
import std.algorithm;
import std.stdio;

void main() {
    long t;
    readf("%s\n", &t);

while(t--) {
    string a, b;
    readf("%s\n", &a);
    readf("%s\n", &b);

    int n = cast(int)a.length;
    int m = cast(int)b.length;

    int minima = min(n, m);

    int maxima = 0;
    for(int z = 1; z <= minima; z++) {
        for(int i = 0; i + z <= n; i++) {
            for(int j = 0; j + z <= m; j++) {
                if(a[i..i+z] == b[j..j+z])
                    maxima = max(maxima, z);
            }
        }
    }

    writefln("%s", n - maxima + m - maxima);
}
}

// Vicfred
// https://codeforces.com/contest/1506/problem/C
// brute force
import std.algorithm;
import std.stdio;

void main() {
    long t;
    readf("%s\n", &t);

while(t--) {
    string a, b;
    readf("%s\n", &a);
    readf("%s\n", &b);

    int n = cast(int)a.length;
    int m = cast(int)b.length;

    int minima = min(n, m);

    int maxima = -1;
    for(int z = 1; z <= minima; ++z) {
        for(int i = 0; i + z <= n; ++i) {
            for(int j = 0; j + z <= m; ++j) {
                if(a[i..i+z] == b[j..j+z])
                    maxima = max(maxima, z);
            }
        }
    }

    writefln("%s", n - maxima + m - maxima);
}
}


You are given a permutation $$$p_1, p_2, \dots, p_n$$$. A permutation of length $$$n$$$ is a sequence such that each integer between $$$1$$$ and $$$n$$$ occurs exactly once in the sequence.Find the number of pairs of indices $$$(l, r)$$$ ($$$1 \le l \le r \le n$$$) such that the value of the median of $$$p_l, p_{l+1}, \dots, p_r$$$ is exactly the given number $$$m$$$.The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.For example, if $$$a=[4, 2, 7, 5]$$$ then its median is $$$4$$$ since after sorting the sequence, it will look like $$$[2, 4, 5, 7]$$$ and the left of two middle elements is equal to $$$4$$$. The median of $$$[7, 1, 2, 9, 6]$$$ equals $$$6$$$ since after sorting, the value $$$6$$$ will be in the middle of the sequence.Write a program to find the number of pairs of indices $$$(l, r)$$$ ($$$1 \le l \le r \le n$$$) such that the value of the median of $$$p_l, p_{l+1}, \dots, p_r$$$ is exactly the given number $$$m$$$.

The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2\cdot10^5$$$, $$$1 \le m \le n$$$) — the length of the given sequence and the required value of the median. The second line contains a permutation $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$). Each integer between $$$1$$$ and $$$n$$$ occurs in $$$p$$$ exactly once.

NoteIn the first example, the suitable pairs of indices are: $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$ and $$$(2, 4)$$$.

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

void main()
{
    int n,m;
    readf!" %s %s "(n,m);
    auto p = readln.strip.split.map!(a => to!long(a)).array;

    int mi;
    foreach (i,v; p) {
        if (v == m) mi = to!int(i);
    }

    long[int] cnt1, cnt2;

    int x = 0, y = 0;
    for (int i = mi - 1; i >= 0; i--) {
        if (p[i] > m) x++;
        else y++;

        cnt1[x - y]++;
    }
    cnt1[0]++;

    x = 0, y = 0;
    for (int i = mi+1; i < n; i++) {
        if (p[i] > m) x++;
        else y++;

        cnt2[x - y]++;
    }

    long ans = 0;
    foreach (v,c; cnt1) {
        if (-v in cnt2)
            ans += c * cnt2[-v];
        if (-v+1 in cnt2)
            ans += c * cnt2[-v+1];
    }
    if (0 in cnt1)
        ans += cnt1[0];
    if (1 in cnt1)
        ans += cnt1[1];

    writeln(ans);
}

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

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

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

    auto p = n - arr.find(m).length;

    debug { p.writeln; }

    int [bool][int] cnt;
    auto cur = 0, oddlen = false;
    long ans = 1L;
    foreach_reverse (i; 0 .. p) {
        oddlen ^= 1;
        cur = cur + (arr[i] > m ? 1 : -1);

        if (cur == 0 || (cur == 1 && oddlen)) ++ans;

        cnt[cur][oddlen] += 1;
    }

    cur = 0, oddlen = false;
    foreach (i; p+1 .. n) {
        oddlen ^= 1;
        cur = cur + (arr[i] > m ? 1 : -1);

        if (cur == 0 || (cur == 1 && oddlen)) ++ans;

        if (-cur in cnt) {
            ans += cnt[-cur].get(false, 0) + cnt[-cur].get(true, 0);
        }
        if ((-cur+1) in cnt) {
            ans += cnt[-cur+1].get(!oddlen, 0);
        }
    }

    ans.writeln;
}

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

void main()
{
    int n,m;
    readf!" %s %s "(n,m);
    auto p = readln.strip.split.map!(a => to!long(a)).array;

    int mi;
    foreach (i,v; p) {
        if (v == m) mi = to!int(i);
    }

    int[long] cnt1, cnt2;

    long x = 0, y = 0;
    for (int i = mi - 1; i >= 0; i--) {
        if (p[i] > m) x++;
        else y++;

        cnt1[x - y]++;
    }
    cnt1[0]++;

    x = 0, y = 0;
    for (int i = mi+1; i < n; i++) {
        if (p[i] > m) x++;
        else y++;

        cnt2[x - y]++;
    }

    long ans = 0;
    foreach (v,c; cnt1) {
        if (-v in cnt2)
            ans += c * cnt2[-v];
        if (-v+1 in cnt2)
            ans += c * cnt2[-v+1];
    }
    if (0 in cnt1)
        ans += cnt1[0];
    if (1 in cnt1)
        ans += cnt1[1];

    writeln(ans);
}

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

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

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

    auto p = n - arr.find(m).length;

    debug { p.writeln; }

    int [bool][int] cnt;
    auto cur = 0, oddlen = false;
    auto ans = 1;
    foreach_reverse (i; 0 .. p) {
        oddlen ^= 1;
        cur = cur + (arr[i] > m ? 1 : -1);

        if (cur == 0 || (cur == 1 && oddlen)) ++ans;

        cnt[cur][oddlen] += 1;
    }

    cur = 0, oddlen = false;
    foreach (i; p+1 .. n) {
        oddlen ^= 1;
        cur = cur + (arr[i] > m ? 1 : -1);

        if (cur == 0 || (cur == 1 && oddlen)) ++ans;

        if (-cur in cnt) {
            ans += cnt[-cur].get(false, 0) + cnt[-cur].get(true, 0);
        }
        if ((-cur+1) in cnt) {
            ans += cnt[-cur+1].get(!oddlen, 0);
        }
    }

    ans.writeln;
}


Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?

The first line contains four space-separated integers p, q, l, r (1 ≤  p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j.

Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.

import std.stdio;

void main() {
  int p, q, l, r; readf(" %s %s %s %s", &p, &q, &l, &r);
  auto a = new int[p];
  auto b = new int[p];
  for (int i = 0; i < p; i++) {
    readf(" %s %s", &a[i], &b[i]);
  }
  auto c = new int[q];
  auto d = new int[q];
  for (int i = 0; i < q; i++) {
    readf(" %s %s", &c[i], &d[i]);
  }
  int ans = 0;
  for (int t = l; t <= r; t++) {
    bool ok = false;
    for (int i = 0; i < p; i++) {
      for (int j = 0; j < q; j++) {
        int la = a[i], lb = b[i];
        int lc = c[j] + t, ld = d[j] + t;
        if (la <= ld && lb >= lc) {
          ok = true;
        }
      }
    }
    ans += ok;
  }
  writeln(ans);
}


The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance.This time, the brothers are dealing with a strange piece of wood marked with their names. This plank of wood can be represented as a string of $$$n$$$ characters. Each character is either a 'D' or a 'K'. You want to make some number of cuts (possibly $$$0$$$) on this string, partitioning it into several contiguous pieces, each with length at least $$$1$$$. Both brothers act with dignity, so they want to split the wood as evenly as possible. They want to know the maximum number of pieces you can split the wood into such that the ratios of the number of occurrences of 'D' to the number of occurrences of 'K' in each chunk are the same.Kaeya, the curious thinker, is interested in the solution for multiple scenarios. He wants to know the answer for every prefix of the given string. Help him to solve this problem!For a string we define a ratio as $$$a:b$$$ where 'D' appears in it $$$a$$$ times, and 'K' appears $$$b$$$ times. Note that $$$a$$$ or $$$b$$$ can equal $$$0$$$, but not both. Ratios $$$a:b$$$ and $$$c:d$$$ are considered equal if and only if $$$a\cdot d = b\cdot c$$$. For example, for the string 'DDD' the ratio will be $$$3:0$$$, for 'DKD' — $$$2:1$$$, for 'DKK' — $$$1:2$$$, and for 'KKKKDD' — $$$2:4$$$. Note that the ratios of the latter two strings are equal to each other, but they are not equal to the ratios of the first two strings.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$) — the length of the wood. The second line of each test case contains a string $$$s$$$ of length $$$n$$$. Every character of $$$s$$$ will be either 'D' or 'K'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

For each test case, output $$$n$$$ space separated integers. The $$$i$$$-th of these numbers should equal the answer for the prefix $$$s_{1},s_{2},\dots,s_{i}$$$.

NoteFor the first test case, there is no way to partition 'D' or 'DDK' into more than one block with equal ratios of numbers of 'D' and 'K', while you can split 'DD' into 'D' and 'D'.For the second test case, you can split each prefix of length $$$i$$$ into $$$i$$$ blocks 'D'.

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

Tuple!(int, int)[Tuple!(int, int)] cache;

int[] cumzero;
int[] cumone;

int solve(ref int[] a, int n, int c_zero, int c_one)
{
//  writeln(a);

  if (c_zero == 0 || c_one == 0)
    return n;

  int div = gcd(c_zero, c_one);
  c_zero /= div;
  c_one /= div;

//  writefln("z=%d o=%d", c_zero, c_one);
//  writeln(a);

  Tuple!(int, int) tmp = cache.get(tuple(c_zero, c_one), tuple(0, 0));
//  writeln(tmp);
  int i = tmp[0];
  int chunks = tmp[1];
  int zero = 0, one = 0;
  int x = 0;

  int maxchunks = (n - i) / (c_zero + c_one);
/*
  if (chunks + 1 == maxchunks) {
    chunks = maxchunks;
    cache[tuple(c_zero, c_one)] = tuple(n, chunks);
    return chunks;
//    writefln("blah");
  }
*/
  int min_chunk_size = c_zero + c_one;
  while (i + min_chunk_size - 1 <= n) {
    x += (cumzero[i + min_chunk_size - 1] - (i > 0 ? cumzero[i - 1] : 0)) * c_one;
    x -= (cumone[i + min_chunk_size - 1] - (i > 0 ? cumone[i - 1] : 0)) * c_zero;
    if (x == 0)
      chunks++;
    i += min_chunk_size;
  }
/*
  while (i < n) {
    if (a[i] == 0)
      x += c_one;
    else
      x -= c_zero;
    if (x == 0) {
      chunks++;
    }
    i++;
  }
*/
  assert(x == 0);
  assert(zero == 0 && one == 0);
  cache[tuple(c_zero, c_one)] = tuple(n, chunks);
//  writeln(chunks);

  return chunks;
}

void main()
{
  int t;
  readf!" %d "(t);
  foreach (casenum ; 0 .. t) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split("").map!((x) => x[0] == 'D' ? 1 : 0).array;
    cache.clear;
    int c_zero = 0, c_one = 0;
    cumzero = [];
    cumone = [];

    foreach (int i ; 0 .. cast(int)a.length) {
      if (a[i] == 0)
        c_zero++;
      else
        c_one++;
      cumzero ~= c_zero;
      cumone ~= c_one;
    }

    int[] result;
    foreach (int i ; 0 .. cast(int)a.length) {
/*
      if (a[i] == 0)
        c_zero++;
      else
        c_one++;
*/
//      result ~= solve(a, i + 1, c_zero, c_one);
      result ~= solve(a, i + 1, cumzero[i], cumone[i]);
    }
    writeln(result.map!text.joiner(" "));
  }
}

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

Tuple!(int, int)[Tuple!(int, int)] cache;

int solve(ref int[] a, int n, int c_zero, int c_one)
{
//  writeln(a);

  if (c_zero == 0 || c_one == 0)
    return n;

  int div = gcd(c_zero, c_one);
  c_zero /= div;
  c_one /= div;

//  writefln("z=%d o=%d", c_zero, c_one);
//  writeln(a);

  Tuple!(int, int) tmp = cache.get(tuple(c_zero, c_one), tuple(0, 0));
//  writeln(tmp);
  int i = tmp[0];
  int chunks = tmp[1];
  int zero = 0, one = 0;

  if (i != 0) {
    return cast(int)(cast(long)n * chunks / i);
  }

  while (i < n) {
    if (a[i] == 0)
      zero++;
    else
      one++;
    if (c_zero * one == c_one * zero) {
//      writeln(i);
      chunks++;
      zero = one = 0;
    }
    i++;
  }
  assert(zero == 0 && one == 0);
  cache[tuple(c_zero, c_one)] = tuple(n, chunks);
//  writeln(chunks);
  return chunks;
}

void main()
{
  int t;
  readf!" %d "(t);
  foreach (casenum ; 0 .. t) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split("").map!((x) => x[0] == 'D' ? 1 : 0).array;
    cache.clear;

    int[] result;
    int c_zero = 0, c_one = 0;
    foreach (int i ; 0 .. cast(int)a.length) {
      if (a[i] == 0)
        c_zero++;
      else
        c_one++;
      result ~= solve(a, i + 1, c_zero, c_one);
    }
    writeln(result.map!text.joiner(" "));
  }
}

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

Tuple!(int, int)[Tuple!(int, int)] cache;

int solve(ref int[] a, int n, int c_zero, int c_one)
{
//  writeln(a);

  if (c_zero == 0 || c_one == 0)
    return n;

  int div = gcd(c_zero, c_one);
  c_zero /= div;
  c_one /= div;

//  writefln("z=%d o=%d", c_zero, c_one);
//  writeln(a);

  Tuple!(int, int) tmp = cache.get(tuple(c_zero, c_one), tuple(0, 0));
//  writeln(tmp);
  int i = tmp[0];
  int chunks = tmp[1];
  int zero = 0, one = 0;

  if (i != 0) {
    return n / i * chunks;
  }

  while (i < n) {
    if (a[i] == 0)
      zero++;
    else
      one++;
    if (c_zero * one == c_one * zero) {
//      writeln(i);
      chunks++;
      zero = one = 0;
    }
    i++;
  }
  assert(zero == 0 && one == 0);
  cache[tuple(c_zero, c_one)] = tuple(n, chunks);
//  writeln(chunks);
  return chunks;
}

void main()
{
  int t;
  readf!" %d "(t);
  foreach (casenum ; 0 .. t) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split("").map!((x) => x[0] == 'D' ? 1 : 0).array;
    cache.clear;

    int[] result;
    int c_zero = 0, c_one = 0;
    foreach (int i ; 0 .. cast(int)a.length) {
      if (a[i] == 0)
        c_zero++;
      else
        c_one++;
      result ~= solve(a, i + 1, c_zero, c_one);
    }
    writeln(result.map!text.joiner(" "));
  }
}


Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.There are $$$n$$$ banknote denominations on Mars: the value of $$$i$$$-th banknote is $$$a_i$$$. Natasha has an infinite number of banknotes of each denomination.Martians have $$$k$$$ fingers on their hands, so they use a number system with base $$$k$$$. In addition, the Martians consider the digit $$$d$$$ (in the number system with base $$$k$$$) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base $$$k$$$ is $$$d$$$, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.Determine for which values $$$d$$$ Natasha can make the Martians happy.Natasha can use only her banknotes. Martians don't give her change.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100\,000$$$, $$$2 \le k \le 100\,000$$$) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — denominations of banknotes on Mars. All numbers are given in decimal notation.

On the first line output the number of values $$$d$$$ for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation.

NoteConsider the first test case. It uses the octal number system.If you take one banknote with the value of $$$12$$$, you will get $$$14_8$$$ in octal system. The last digit is $$$4_8$$$.If you take one banknote with the value of $$$12$$$ and one banknote with the value of $$$20$$$, the total value will be $$$32$$$. In the octal system, it is $$$40_8$$$. The last digit is $$$0_8$$$.If you take two banknotes with the value of $$$20$$$, the total value will be $$$40$$$, this is $$$50_8$$$ in the octal system. The last digit is $$$0_8$$$.No other digits other than $$$0_8$$$ and $$$4_8$$$ can be obtained. Digits $$$0_8$$$ and $$$4_8$$$ could also be obtained in other ways.The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero.

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;
    A = A.map!(a => a % K).array.sort().uniq().array;

    if (A.length == 1 && A[0] == 0) {
        writeln(1);
        writeln(0);
        return;
    }

    if (A[0] == 0) A.popFront;
    auto G = A.length > 1 ? A.reduce!gcd : A[0];
    G = gcd(G, K);

    int[] ans = [G];
    while (ans.front != (ans.back + G) % K) {
        ans ~= (ans.back + G) % K;
    }

    ans.sort();
    ans.length.writeln;
    foreach (a; ans) write(a, " ");
    writeln;
}

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

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[] %= k;
		auto d = a.fold !(gcd) (k);
		d += !d * k;
		auto r = iota (0, k, d);
		writefln ("%s\n%(%s %)", r.walkLength, r);
	}
}

import std.algorithm, std.conv, std.numeric, std.range, std.stdio;
void main () {
	int n, k;
	readf (" %s %s ", &n, &k);
	int d = readln.split.map !(to!int).fold!gcd (k);
	d += !d * k;
	writefln ("%s\n%(%s %)", k / d, iota (0, k, d));
}

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

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

    auto d = arr.map!(x => x % k).fold!(gcd)(k);

    if (d == 0) d = k;

    debug { d.writeln; }

    auto ans = iota(0, k, d);

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

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

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[] %= k;
		auto d = a.reduce !(gcd);
		d += !d * k;
		auto r = iota (0, k, d);
		writefln ("%s\n%(%s %)", r.walkLength, r);
	}
}


The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order:  She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\le 1$$$, possibly $$$y=x$$$).  She collects all mushrooms on point $$$y$$$.  A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10 ^ 5$$$, $$$1\le k \le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial number of mushrooms on point $$$1,2,\ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.

For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes.

NoteTest case 1:Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms.It can be shown that it is impossible to collect more than $$$12$$$ mushrooms.Test case 2:This is one of her possible moving path:$$$2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$$$It can be shown that it is impossible to collect more than $$$37$$$ mushrooms.

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

void solve() {
    long N, K; readf("%d %d\n", &N, &K);
    auto A = readln.chomp.split(" ").map!(to!long).array;
    if (K >= N) {
        writeln(A.reduce!"a+b" + (K - N) * N + N * (N-1) / 2);
    } else {
        int k = cast(int)(K);
        long x = A[0 .. k].reduce!"a+b";
        long mx = x;
        for (int l = 0; l + k < N; l++) {
            int r = l + k;
            x = x - A[l] + A[r];
            mx = max(mx, x);
        }
        writeln(mx + K * (K-1) / 2);
    }
}

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

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, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = [0L];
		foreach (ref c; a)
		{
			s ~= s.back + c;
		}
		long res = 0;
		if (k >= n)
		{
			res = a.sum (0L);
			foreach (i; 0..n)
			{
				res += k - i - 1;
			}
		}
		else
		{
			res += zip (s, s.drop (k)).map !(q{a[1] - a[0]})
			    .maxElement;
			foreach (i; 0..k)
			{
				res += i;
			}
		}
		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;
        const K = readLong;
        auto A = new long[N];
        foreach (i; 0 .. N) {
          A[i] = readLong;
        }

        auto ASum = new long[N + 1];
        foreach (i; 0 .. N) {
          ASum[i + 1] = ASum[i] + A[i];
        }

        const k = min(K, N);
        long ans;
        foreach (i; 0 .. N - k + 1) {
          chmax(ans, ASum[cast(int)(i + k)] - ASum[cast(int)(i)]);
        }
        foreach (j; 0 .. k) {
          ans += (K - 1 - j);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


You are given a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$ and an integer $$$k$$$, such that $$$1 \leq k \leq n$$$. A permutation means that every number from $$$1$$$ to $$$n$$$ is contained in $$$p$$$ exactly once.Let's consider all partitions of this permutation into $$$k$$$ disjoint segments. Formally, a partition is a set of segments $$$\{[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]\}$$$, such that:  $$$1 \leq l_i \leq r_i \leq n$$$ for all $$$1 \leq i \leq k$$$;  For all $$$1 \leq j \leq n$$$ there exists exactly one segment $$$[l_i, r_i]$$$, such that $$$l_i \leq j \leq r_i$$$. Two partitions are different if there exists a segment that lies in one partition but not the other.Let's calculate the partition value, defined as $$$\sum\limits_{i=1}^{k} {\max\limits_{l_i \leq j \leq r_i} {p_j}}$$$, for all possible partitions of the permutation into $$$k$$$ disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by $$$998\,244\,353$$$.

The first line contains two integers, $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 200\,000$$$) — the size of the given permutation and the number of segments in a partition. The second line contains $$$n$$$ different integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$) — the given permutation.

Print two integers — the maximum possible partition value over all partitions of the permutation into $$$k$$$ disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo $$$998\,244\,353$$$. Please note that you should only find the second value modulo $$$998\,244\,353$$$.

NoteIn the first test, for $$$k = 2$$$, there exists only two valid partitions: $$$\{[1, 1], [2, 3]\}$$$ and $$$\{[1, 2], [3, 3]\}$$$. For each partition, the partition value is equal to $$$2 + 3 = 5$$$. So, the maximum possible value is $$$5$$$ and the number of partitions is $$$2$$$.In the third test, for $$$k = 3$$$, the partitions with the maximum possible partition value are $$$\{[1, 2], [3, 5], [6, 7]\}$$$, $$$\{[1, 3], [4, 5], [6, 7]\}$$$, $$$\{[1, 4], [5, 5], [6, 7]\}$$$, $$$\{[1, 2], [3, 6], [7, 7]\}$$$, $$$\{[1, 3], [4, 6], [7, 7]\}$$$, $$$\{[1, 4], [5, 6], [7, 7]\}$$$. For all of them, the partition value is equal to $$$7 + 5 + 6 = 18$$$. The partition $$$\{[1, 2], [3, 4], [5, 7]\}$$$, however, has the partition value $$$7 + 3 + 6 = 16$$$. This is not the maximum possible value, so we don't count it.

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

immutable int mod = 998_244_353;

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

		long best = k;
		int res = 1;
		int prev = -1;
		foreach (i, ref c; p)
		{
			if (c + k >= n)
			{
				best += c;
				if (prev >= 0)
				{
					res = (res * 1L * (i - prev)) % mod;
				}
				prev = i;
			}
		}

		writeln (best, " ", res);
	}
}

// Code By H~$~C

module main;

import std.stdio, std.array, std.string, std.math, std.uni, std.format, std.bigint;
import std.algorithm, std.conv, std.container, std.range, std.functional;
import std.random, std.typecons;

struct modular_int(uint M_) {
  alias M = M_;
  uint x;
  modular_int inv() const {
    uint a = M, b = x;
    long u = 0, v = 1;
    while (b) {
      uint t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    if (u < 0) u += M;
    return modular_int(u);
  }
  this(modular_int a) { x = a.x; }
  this(long a) { a %= M; if (a < 0) a += M; x = cast(int)a; }
  ref modular_int opAssign(long a) { return (this = modular_int(a)); }
  ref modular_int opOpAssign(string op)(modular_int a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x & (1U << 31)) x += M; }
    else static if (op == "*") { x = cast(uint)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref modular_int opOpAssign(string op)(long a) {
    static if (op == "^^") {
      modular_int t2 = this, te = modular_int(1);
      if (a < 0) a = -a, t2 = t2.inv();
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = te.x;
      return this;
    }
    else return mixin("this " ~ op ~ "= modular_int(a)");
  }
  modular_int opUnary(string op)() const if (op == "-") {
    return modular_int(M - x);
  }
  modular_int opBinary(string op, T)(T a) const {
    return mixin("modular_int(this) " ~ op ~ "= a");
  }
  modular_int opBinaryRight(string op)(long a) const {
    return mixin("modular_int(a) " ~ op ~ "= this");
  }
  T to(T)() const {
    return x.to!T;
  }
}
alias modint = modular_int!998244353;

immutable Maxn = 200005;

int n, k;
int[Maxn] a;
long[Maxn] b;

// FIXME
void solve(in int testcase) {
  n.read, k.read;
  a[1 .. n + 1] = readarray!int.array;
  b[1 .. n + 1] = a[1 .. n + 1].map!(to!long).array.sort.array;
  b[n - k + 1 .. n + 1].sum.writef!"%s ";
  int q = cast(int)(b[n - k + 1]);
  int[] vec;
  foreach(i; 1 .. n + 1)
    if (a[i] >= q)
      vec ~= i;
  modint ans = 1;
  foreach(i; 0 .. vec.length - 1) {
    ans *= cast(long)(vec[i + 1] - vec[i]);
  }
  ans.to!int.writeln;
}

// ************************************************************

T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;
alias set = RedBlackTree;
alias Set = redBlackTree;

// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;

// functions
T element(alias pred, T)(in T[] a) {
  int pos = 0;
  for (int i = 1; i < cast(int)(a.length); ++i)
    if (binaryFun!pred(a[i], a[pos])) pos = i;
  return a[pos];
}
string readToken() {
  for (; _tokens.empty; ) {
    if (stdin.eof) return "";
    _tokens = readln.split;
  }
  auto token = _tokens.front;
  _tokens.popFront;
  return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
  int l = -1, h = cast(int)(as.length), mid;
  for (; l + 1 < h; ) {
    mid = (l + h) >> 1;
    (unaryFun!pred(as[mid]) ? h : l) = mid;
  }
  return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => pred(val, a));
}

void main(string[] args) {
  int tests = 1;
//  tests.read();
  foreach(test; 1 .. tests + 1) solve(test);
}

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, k = scan!int;
    int[] ps = scan!int(n);

    long[] us;
    long sum;
    foreach(i, p; ps){
        if(p >= n - k + 1) sum += p, us ~= i.to!long;
    }

    Finite.mod = 998_244_353;
    Finite ans = Finite(1);
    foreach(i; 0 .. us.length - 1){
        ans *= us[i + 1] - us[i];
    }
    print(sum, ans);

}

struct Finite{
	long value; static long mod = 1_000_000_007;
	this(long v){ value = val(v); }
	static long val(long v){ return (v + mod  - (v / mod) * mod) % mod; }
	bool opCast(T: bool)(){ return value != 0; }
	bool opEquals(Finite b){ return(value == b.value); }
	string toString(){ return value.to!string; }

	Finite opUnary(string s){ long v;
		if(s == "+") v = value;
		else if(s == "-") v = mod - value;
		else if(s == "++") v = value + 1;
		else if(s == "--") v = value + mod - 1;
		else assert(0, "Operator unary " ~ s ~ " not implemented");
		return Finite(v);
	}

	Finite opBinary(string s)(Finite b){ return opBinary!s(b.value); }
	Finite opBinary(string s)(long u){ long v;
		if(s == "+") v = value + u;
		else if(s == "-") v = value + mod - u;
		else if(s == "*") v = value * u;
		else if(s == "/") v = value * inv(u);
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opBinaryRight(string s)(long u){ long v;
		if(s == "+") v = u + value;
		else if(s == "-") v = u + mod - value;
		else if(s == "*") v = u * value;
		else if(s == "/") v = u * invvalue;
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opAssign(long v){ value = v; return this; }

	Finite opOpAssign(string s)(Finite b){ return opOpAssign!s(b.value); }
	Finite opOpAssign(string s)(long v){
		if(s == "+") value = (value + v) % mod;
		else if(s == "-") value = (value + mod - v) % mod;
		else if(s == "*") value = (value * v) % mod;
		else if(s == "/") value = (value * inv(v)) % mod;
		else assert(0, "Operator " ~ s ~ "= not implemented");
		return this;
	}

	private static long[] _inv = [0, 1];
	long invvalue(){ return inv(value); }
	long inv(long v){ int i = val(v).to!int;
		while(i >= _inv.length){
			_inv ~= _inv[(mod % $).to!int] * (mod - mod / _inv.length) % mod;
		}
		return _inv[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)); }

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;

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

      long ans0;
      foreach (k; 0 .. K) {
        ans0 += (N - k);
      }

      int[] xs;
      foreach (i; 0 .. N) {
        if (P[i] > N - K) {
          xs ~= i;
        }
      }
      Mint ans1 = 1;
      foreach (k; 0 .. K - 1) {
        ans1 *= (xs[k + 1] - xs[k]);
      }

      writeln(ans0, " ", ans1);
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto k = RD!int;
	auto p = RDA!int;

	long ans0;
	int[] pos;
	foreach (i; 0..n)
	{
		if (p[i] > n-k)
		{
			pos ~= i;
			ans0 += p[i];
		}
	}

	long ans = 1;
	foreach (i; 1..pos.length)
	{
		auto d = pos[i] - pos[i-1];
		ans.modm(d);
	}

	writeln(ans0, " ", ans);
	stdout.flush;
	debug readln;
}

// Code By H~$~C

module main;

import std.stdio, std.array, std.string, std.math, std.uni, std.format, std.bigint;
import std.algorithm, std.conv, std.container, std.range, std.functional;
import std.random, std.typecons;

struct modular_int(uint M_) {
  alias M = M_;
  uint x;
  modular_int inv() const {
    uint a = M, b = x;
    long u = 0, v = 1;
    while (b) {
      uint t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    if (u < 0) u += M;
    return modular_int(u);
  }
  this(modular_int a) { x = a.x; }
  this(long a) { a %= M; if (a < 0) a += M; x = cast(int)a; }
  ref modular_int opAssign(long a) { return (this = modular_int(a)); }
  ref modular_int opOpAssign(string op)(modular_int a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x & (1U << 31)) x += M; }
    else static if (op == "*") { x = cast(uint)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref modular_int opOpAssign(string op)(long a) {
    static if (op == "^^") {
      modular_int t2 = this, te = modular_int(1);
      if (a < 0) a = -a, t2 = t2.inv();
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = te.x;
      return this;
    }
    else return mixin("this " ~ op ~ "= modular_int(a)");
  }
  modular_int opUnary(string op)() const if (op == "-") {
    return modular_int(M - x);
  }
  modular_int opBinary(string op, T)(T a) const {
    return mixin("modular_int(this) " ~ op ~ "= a");
  }
  modular_int opBinaryRight(string op)(long a) const {
    return mixin("modular_int(a) " ~ op ~ "= this");
  }
  T to(T)() const {
    return x.to!T;
  }
}
alias modint = modular_int!998244353;

immutable Maxn = 200005;

int n, k;
int[Maxn] a, b;

// FIXME
void solve(in int testcase) {
  n.read, k.read;
  a[1 .. n + 1] = readarray!int.array;
  b[1 .. n + 1] = a[1 .. n + 1].dup.sort.array;
  b[n - k + 1 .. n + 1].sum.writef!"%s ";
  int q = b[n - k + 1];
  int[] vec;
  foreach(i; 1 .. n + 1)
    if (a[i] >= q)
      vec ~= i;
  modint ans = 1;
  foreach(i; 0 .. vec.length - 1) {
    ans *= cast(long)(vec[i + 1] - vec[i]);
  }
  ans.to!int.writeln;
}

// ************************************************************

T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;
alias set = RedBlackTree;
alias Set = redBlackTree;

// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;

// functions
T element(alias pred, T)(in T[] a) {
  int pos = 0;
  for (int i = 1; i < cast(int)(a.length); ++i)
    if (binaryFun!pred(a[i], a[pos])) pos = i;
  return a[pos];
}
string readToken() {
  for (; _tokens.empty; ) {
    if (stdin.eof) return "";
    _tokens = readln.split;
  }
  auto token = _tokens.front;
  _tokens.popFront;
  return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
  int l = -1, h = cast(int)(as.length), mid;
  for (; l + 1 < h; ) {
    mid = (l + h) >> 1;
    (unaryFun!pred(as[mid]) ? h : l) = mid;
  }
  return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => pred(val, a));
}

void main(string[] args) {
  int tests = 1;
//  tests.read();
  foreach(test; 1 .. tests + 1) solve(test);
}


Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.A subtree of some vertex is a subgraph containing that vertex and all its descendants.Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree. Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree. The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him. Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

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, std.bitmanip;

int N;
int[] colors;
int[][] adj;

void main() {
    N = readln.chomp.to!int;
    adj = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int).array;
        adj[s[0]-1] ~= s[1]-1;
        adj[s[1]-1] ~= s[0]-1;
    }
    colors = readln.split.map!(to!int).array;

    int[int] cnt;
    foreach (c; colors) {
        if (c in cnt) cnt[c]++;
        else cnt[c] = 1;
    }

    if (cnt.keys.length == 1) {
        writeln("YES");
        writeln(1);
    }
    else {
        Tuple!(int, int)[] borders;
        foreach (i; 0..N) {
            foreach (j; adj[i]) {
                if (colors[i] != colors[j]) {
                    borders ~= tuple(i, j);
                }
            }
        }

        foreach (a; borders[0]) {
            bool flag = true;
            foreach (b; borders) {
                if (a != b[0] && a != b[1]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                writeln("YES");
                writeln(a+1);
                return;
            }
        }

        writeln("NO");
    }
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.variant,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias std.numeric.gcd gcd;
immutable int mod=10^^9+7;
pure nothrow
{
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
	{
		pair!(X,Y) pp;
		pp.fi=x_;
		pp.se=y_;
		return pp;
	}
	big gcd(big a,big b)
	{
		while(b)
		{
			a%=b;
			swap(a,b);
		}
		return a;
	}
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (g==a[l])?l:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X && !isSomeChar(ElementType!X))
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{
	string f;
	foreach(i;T)
	{
		if(isSomeChar!i)f~=" %c";
		else f~=" %s";
	}
	return readf(f,ptrs)==ptrs.length;
}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{
	string f;
	foreach(i;T)
	{
		if(isSomeChar!i)f~=" %c";
		else f~=" %s";
	}
	f~='\n';
	return readf(f,ptrs)==ptrs.length;
}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning---------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		auto g=new int[][n+1];
		foreach(i;0..n-1)
		{
			int u,v;
			read(&u,&v);
			u--;v--;
			g[u]~=v;
			g[v]~=u;
		}
		auto cl=arread!int;
		auto a=new int[n];
		int s;
		foreach(i;0..n)
		{
			foreach(x;g[i])
			{
				if(cl[i]!=cl[x])
				{
					a[i]++;
					a[x]++;
					s++;
				}
			}
		}
		foreach(i;0..n)
		{
			if(a[i]==s)
			{
				writeln("YES\n",i+1);
				continue loop;
			}
		}
		writeln("NO");
	}
}

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, std.bitmanip;

int N;
int[] colors;
int[][] adj;

bool dfs(int n, int prev) {
    foreach (m; adj[n]) {
        if (m == prev) continue;
        if (colors[m] != colors[n]) return false;
        if (!dfs(m, n)) return false;
    }
    return true;
}

void main() {
    N = readln.chomp.to!int;
    adj = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int).array;
        adj[s[0]-1] ~= s[1]-1;
        adj[s[1]-1] ~= s[0]-1;
    }
    colors = readln.split.map!(to!int).array;

    int[int] cnt;
    foreach (c; colors) {
        if (c in cnt) cnt[c]++;
        else cnt[c] = 1;
    }

    if (N <= 3) {
        writeln("YES");
        writeln(1);
    }
    else if (cnt.keys.length == 1) {
        writeln("YES");
        writeln(1);
    }
    else if (cnt.keys.length == 2) {
        int n1 = -1;
        int n2 = -1;
        int c1 = 0;
        int c2 = 0;

        foreach (i; 0..N) {
            foreach (j; adj[i]) {
                if (colors[i] != colors[j]) {
                    if (colors[i] < colors[j]) {
                        c1++;
                        n1 = i;
                    }
                    else {
                        c2++;
                        n2 = i;
                    }
                    break;
                }
            }
        }

        if (c1 == 1) {
            writeln("YES");
            writeln(n1+1);
        }
        else if (c2 == 1) {
            writeln("YES");
            writeln(n2+1);
        }
        else {
            writeln("NO");
        }

    }
    else if (cnt.keys.length == 3) {
        foreach (i; 0..N) {
            if (cnt[colors[i]] == 1) {
                bool flag = true;
                foreach (j; adj[i]) {
                    if (!dfs(j, i)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    writeln("YES");
                    writeln(i+1);
                    return;
                }
            }
        }
        writeln("NO");
    }
    else {
        writeln("NO");
    }
}

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, std.bitmanip;

int N;
int[] colors;
int[][] adj;

bool dfs(int n, int prev) {
    foreach (m; adj[n]) {
        if (m == prev) continue;
        if (colors[m] != colors[n]) return false;
        if (!dfs(m, n)) return false;
    }
    return true;
}

void main() {
    N = readln.chomp.to!int;
    adj = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int).array;
        adj[s[0]-1] ~= s[1]-1;
        adj[s[1]-1] ~= s[0]-1;
    }
    colors = readln.split.map!(to!int).array;

    int[int] cnt;
    foreach (c; colors) {
        if (c in cnt) cnt[c]++;
        else cnt[c] = 1;
    }

    if (cnt.keys.length == 1) {
        writeln("YES");
        writeln(1);
    }
    else if (cnt.keys.length == 2) {
        int n1 = -1;
        int n2 = -1;
        int c1 = 0;
        int c2 = 0;

        foreach (i; 0..N) {
            foreach (j; adj[i]) {
                if (colors[i] != colors[j]) {
                    if (colors[i] < colors[j]) {
                        c1++;
                        n1 = i;
                    }
                    else {
                        c2++;
                        n2 = i;
                    }
                    break;
                }
            }
        }

        if (c1 == 1) {
            writeln("YES");
            writeln(n1+1);
        }
        else if (c2 == 1) {
            writeln("YES");
            writeln(n2+1);
        }
        else {
            writeln("NO");
        }

    }
    else if (cnt.keys.length == 3) {

        foreach (i; 0..N) {
            if (cnt[colors[i]] == 1) {
                bool flag = true;
                foreach (j; adj[i]) {
                    if (!dfs(j, i)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    writeln("YES");
                    writeln(i+1);
                    return;
                }
            }
        }
        writeln("NO");
    }
    else {
        writeln("NO");
    }
}

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, std.bitmanip;

int N;
int[] colors;
int[][] adj;

bool dfs(int n, int prev) {
    foreach (m; adj[n]) {
        if (m == prev) continue;
        if (colors[m] != colors[n]) return false;
        if (!dfs(m, n)) return false;
    }
    return true;
}

void main() {
    N = readln.chomp.to!int;
    adj = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int).array;
        adj[s[0]-1] ~= s[1]-1;
        adj[s[1]-1] ~= s[0]-1;
    }
    colors = readln.split.map!(to!int).array;

    int[int] cnt;
    foreach (c; colors) {
        if (c in cnt) cnt[c]++;
        else cnt[c] = 1;
    }

    if (cnt.keys.length == 1) {
        writeln("YES");
        writeln(1);
    }
    else if (cnt.keys.length == 2) {
        int n1 = -1;
        int n2 = -1;
        int c1 = 0;
        int c2 = 0;

        foreach (i; 0..N) {
            foreach (j; adj[i]) {
                if (colors[i] != colors[j]) {
                    if (colors[i] < colors[j]) {
                        c1++;
                        n1 = i;
                    }
                    else {
                        c2++;
                        n2 = i;
                    }
                    break;
                }
            }
        }

        if (c1 == 1) {
            writeln("YES");
            writeln(n1+1);
        }
        else if (c2 == 1) {
            writeln("YES");
            writeln(n2+1);
        }
        else {
            writeln("NO");
        }

    }
    else {
        foreach (i; 0..N) {
            if (cnt[colors[i]] == 1) {
                bool flag = true;
                foreach (j; adj[i]) {
                    if (!dfs(j, i)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    writeln("YES");
                    writeln(i+1);
                    return;
                }
            }
        }
        writeln("NO");
    }
}


The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon.Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square.You can rotate $$$2n$$$-gon and/or the square.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 200$$$) — the number of test cases. Next $$$T$$$ lines contain descriptions of test cases — one per line. Each line contains single odd integer $$$n$$$ ($$$3 \le n \le 199$$$). Don't forget you need to embed $$$2n$$$-gon, not an $$$n$$$-gon.

Print $$$T$$$ real numbers — one per test case. For each test case, print the minimum length of a side of the square $$$2n$$$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto n = readln.chomp.to!double * 2;
        if (n == 4) {
            writefln("%.12f", 1.0);
            continue;
        }
        auto r = 1.0 / sin(PI / n);

        auto d = 2.0 * PI / n;
        auto res = 0.0;
        foreach (x; 0..n.to!int) {
            auto t = 45.0 * PI / 180.0 + d * x;
            res = max(res, r * sin(t));
        }
        writefln("%.12f", res);
    }
}

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

real solve (int n)
{
	auto alpha = PI / n;
	real x = 0.0;
	real y = 0.0;
	real phi = 0.0;
	foreach (i; 0..n)
	{
		x += cos (phi);
		y += sin (phi);
		phi += alpha;
	}
	return cos (alpha / 4) * hypot (y, x);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.to !(int).solve.writefln !("%.20f");
	}
}

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

real solve (int n)
{
	auto alpha = PI / n;
	writeln (alpha * 180.0 / PI);
	real x = 0.0;
	real y = 0.0;
	real phi = 0.0;
	foreach (i; 0..n)
	{
		x += cos (phi);
		y += sin (phi);
		phi += alpha;
	}
	return cos (alpha / 4) * hypot (y, x);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.to !(int).solve.writefln !("%.20f");
	}
}


You are given a permutation, $$$p_1, p_2, \ldots, p_n$$$.Imagine that some positions of the permutation contain bombs, such that there exists at least one position without a bomb.For some fixed configuration of bombs, consider the following process. Initially, there is an empty set, $$$A$$$.For each $$$i$$$ from $$$1$$$ to $$$n$$$: Add $$$p_i$$$ to $$$A$$$.  If the $$$i$$$-th position contains a bomb, remove the largest element in $$$A$$$.After the process is completed, $$$A$$$ will be non-empty. The cost of the configuration of bombs equals the largest element in $$$A$$$.You are given another permutation, $$$q_1, q_2, \ldots, q_n$$$.For each $$$1 \leq i \leq n$$$, find the cost of a configuration of bombs such that there exists a bomb in positions $$$q_1, q_2, \ldots, q_{i-1}$$$. For example, for $$$i=1$$$, you need to find the cost of a configuration without bombs, and for $$$i=n$$$, you need to find the cost of a configuration with bombs in positions $$$q_1, q_2, \ldots, q_{n-1}$$$.

The first line contains a single integer, $$$n$$$ ($$$2 \leq n \leq 300\,000$$$). The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n)$$$. The third line contains $$$n$$$ distinct integers $$$q_1, q_2, \ldots, q_n$$$ ($$$1 \leq q_i \leq n)$$$.

Print $$$n$$$ space-separated integers, such that the $$$i$$$-th of them equals the cost of a configuration of bombs in positions $$$q_1, q_2, \ldots, q_{i-1}$$$.

NoteIn the first test:  If there are no bombs, $$$A$$$ is equal to $$$\{1, 2, 3\}$$$ at the end of the process, so the cost of the configuration is $$$3$$$.  If there is one bomb in position $$$1$$$, $$$A$$$ is equal to $$$\{1, 2\}$$$ at the end of the process, so the cost of the configuration is $$$2$$$;  If there are two bombs in positions $$$1$$$ and $$$2$$$, $$$A$$$ is equal to $$$\{1\}$$$ at the end of the process, so the cost of the configuration is $$$1$$$. In the second test:Let's consider the process for $$$i = 4$$$. There are three bombs on positions $$$q_1 = 5$$$, $$$q_2 = 2$$$, and $$$q_3 = 1$$$.At the beginning, $$$A = \{\}$$$.  Operation $$$1$$$: Add $$$p_1 = 2$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{2\}$$$. There exists a bomb in position $$$1$$$, so we should delete the largest element from $$$A$$$. $$$A$$$ is equal to $$$\{\}$$$.  Operation $$$2$$$: Add $$$p_2 = 3$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{3\}$$$. There exists a bomb in position $$$2$$$, so we should delete the largest element from $$$A$$$. $$$A$$$ is equal to $$$\{\}$$$.  Operation $$$3$$$: Add $$$p_3 = 6$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{6\}$$$. There is no bomb in position $$$3$$$, so we do nothing.  Operation $$$4$$$: Add $$$p_4 = 1$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{1, 6\}$$$. There is no bomb in position $$$4$$$, so we do nothing.  Operation $$$5$$$: Add $$$p_5 = 5$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{1, 5, 6\}$$$. There exists a bomb in position $$$5$$$, so we delete the largest element from $$$A$$$. Now, $$$A$$$ is equal to $$$\{1, 5\}$$$.  Operation $$$6$$$: Add $$$p_6 = 4$$$ to $$$A$$$, so $$$A$$$ is equal to $$$\{1, 4, 5\}$$$. There is no bomb in position $$$6$$$, so we do nothing. In the end, we have $$$A = \{1, 4, 5\}$$$, so the cost of the configuration is equal to $$$5$$$.

// Code By H~$~C

module main;

import std.stdio, std.array, std.string, std.math, std.uni, std.format, std.bigint;
import std.algorithm, std.conv, std.container, std.range, std.functional;
import std.random, std.typecons;

immutable Maxn = 300005;

int n;
int[Maxn] p, q;

struct node {
  this()() {
    sum = suf = 0;
  }
  this()(in node x) { *this = x; }
  int sum, suf;
  void apply(int l, int r) {
    sum = suf = 0;
  }
  void apply(int l, int r, int x) {
    sum += x, suf += x;
  }
}
void pullup(ref node a, in node b, in node c) {
  a.suf = max(c.suf, b.suf + c.sum);
  a.sum = b.sum + c.sum;
}

struct segment_tree {
  int N;
  node[] tr;
  private void pushup(int p) {
    pullup(tr[p], tr[p * 2], tr[p * 2 + 1]);
  }
  private void modify_(T...)(int p, int l, int r, int L, int R, T args) {
    if (L == l && r == R) return tr[p].apply(l, r, args);
    pushdown(p, l, r);
    int mid = (l + r) >> 1;
    if (R <= mid) modify_(p * 2, l, mid, L, R, args);
    else if (L > mid) modify_(p * 2 + 1, mid + 1, r, L, R, args);
    else {
      modify_(p * 2, l, mid, L, mid, args);
      modify_(p * 2 + 1, mid + 1, r, mid + 1, R, args);
    }
    pushup(p);
  }
  private node query_(int p, int l, int r, int L, int R) {
    if (L == l && r == R) return tr[p];
    pushdown(p, l, r);
    int mid = (l + r) >> 1;
    if (R <= mid) return query_(p * 2, l, mid, L, R);
    if (L > mid) return query_(p * 2 + 1, mid + 1, r, L, R);
    node left = query_(p * 2, l, mid, L, mid);
    node right = query_(p * 2 + 1, mid + 1, r, mid + 1, R);
    node ans; pullup(ans, left, right); return ans;
  }
  public void build(T...)(int len, T args) {
    N = len;
    tr.length = (N + 1) << 2;
    build(1, 1, N, args);
  }
  public void modify(T...)(int L, int R, T args) {
    if (L > R) return ;
    modify_(1, 1, N, L, R, args);
  }
  public node query(int L, int R) {
    if (L > R) return node();
    return query_(1, 1, N, L, R);
  }
  private void pushdown(int p, int l, int r) {
    int mid = (l + r) >> 1;
  }
  private void build(int p, int l, int r) {
    if (l == r) {
      tr[p].apply(l, r);
      return ;
    }
    int mid = (l + r) >> 1;
    build(p * 2, l, mid);
    build(p * 2 + 1, mid + 1, r);
    pushup(p);
  }
}
segment_tree sgt;

// FIXME
void solve(in int testcase) {
  n.read;
  p[1 .. n + 1] = readarray!int;
  foreach(i; 1 .. n + 1) q[p[i]] = i;
  sgt.build(n);
  int now = n;
  foreach(i; 1 .. n + 1) {
    while (sgt.query(1, n).suf <= 0) {
      sgt.modify(q[now], q[now], 1);
      --now;
    }
    (now + 1).write, (" \n"[i == n]).write;
    int pos = reader!int;
    sgt.modify(pos, pos, -1);
  }
}

// ************************************************************

T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;

// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;

// functions
T element(alias pred, T)(in T[] a) {
  int pos = 0;
  for (int i = 1; i < cast(int)(a.length); ++i)
    if (binaryFun!pred(a[i], a[pos])) pos = i;
  return a[pos];
}
string readToken() {
  for (; _tokens.empty; ) {
    if (stdin.eof) return "";
    _tokens = readln.split;
  }
  auto token = _tokens.front;
  _tokens.popFront;
  return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
  int l = -1, h = cast(int)(as.length), mid;
  for (; l + 1 < h; ) {
    mid = (l + h) >> 1;
    (unaryFun!pred(as[mid]) ? h : l) = mid;
  }
  return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
  return as.binary_bearch!(a => pred(val, a));
}

void main(string[] args) {
  int tests = 1;
//  tests.read();
  foreach(test; 1 .. tests + 1) solve(test);
}

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

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

// t -> max{t + a, b}
struct T {
  int a, b;
  T opBinary(string op)(const(T) t) const {
    return T(a + t.a, max(b + t.a, t.b));
  }
}
enum INF = 10^^9;
enum ID_T = T(0, -INF);

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new int[N];
      foreach (i; 0 .. N) {
        P[i] = readInt();
      }
      auto Q = new int[N];
      foreach (j; 0 .. N) {
        Q[j] = readInt() - 1;
      }

      auto poss = new int[N + 1];
      foreach (i; 0 .. N) {
        poss[P[i]] = i;
      }

      auto ans = new int[N];
      auto seg = new SegmentTree!(T, "a * b")(2 * N, ID_T);
      {
        int x = N + 1;
        foreach (j; 0 .. N) {
          for (; max(0 + seg.ts[1].a, seg.ts[1].b) <= 0; ) {
            // add large
            --x;
            seg.set(2 * poss[x], T(1, 0));
            debug {
              writeln(j, " ", x, ": ", seg.ts[1]);
            }
          }
          ans[j] = x;
          // bomb
          seg.set(2 * Q[j] + 1, T(-1, 0));
        }
      }
      foreach (j; 0 .. N) {
        if (j > 0) write(" ");
        write(ans[j]);
      }
      writeln();
    }
  } catch (EOFException 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)); }

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

// t -> max{t + a, b}
struct T {
  int a, b;
  T opBinary(string op)(const(T) t) const {
    return T(a + t.a, max(b + t.a, t.b));
  }
}
enum INF = 10^^9;
enum ID_T = T(0, -INF);

void main() {
writeln(T(-1,0)*T(-1,0));
writeln(T(-1,0)*T(-1,0)*T(1,0));
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new int[N];
      foreach (i; 0 .. N) {
        P[i] = readInt();
      }
      auto Q = new int[N];
      foreach (j; 0 .. N) {
        Q[j] = readInt() - 1;
      }

      auto poss = new int[N + 1];
      foreach (i; 0 .. N) {
        poss[P[i]] = i;
      }

      auto ans = new int[N];
      auto seg = new SegmentTree!(T, "a * b")(2 * N, ID_T);
      {
        int x = N + 1;
        foreach (j; 0 .. N) {
          for (; max(0 + seg.ts[1].a, seg.ts[1].b) <= 0; ) {
            // add large
            --x;
            seg.set(2 * poss[x], T(1, 0));
            debug {
              writeln(j, " ", x, ": ", seg.ts[1]);
            }
          }
          ans[j] = x;
          // bomb
          seg.set(2 * Q[j] + 1, T(-1, 0));
        }
      }
      foreach (j; 0 .. N) {
        if (j > 0) write(" ");
        write(ans[j]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


Hands that shed innocent blood!There are n guilty people in a line, the i-th of them holds a claw with length Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j ≥ i - Li.You are given lengths of the claws. You need to find the total number of alive people after the bell rings.

The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people. Second line contains n space-separated integers L1, L2, ..., Ln (0 ≤ Li ≤ 109), where Li is the length of the i-th person's claw.

Print one integer — the total number of alive people after the bell rings.

NoteIn first sample the last person kills everyone in front of him.

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

void main() {
  int n = readln.chomp.to!int;
  auto L = readln.chomp.split.map!(to!int).array;
  int[] is_alive = new int[n];

  for (int i = n - 1; i > 0; i--) {
    if (L[i] == 0) continue;
    int idx = max(0, i - L[i]);
    is_alive[idx] += 1;
    is_alive[i] -= 1;
  }

  int ans = 0;

  for (int i = 1; i < n; ++i) is_alive[i] += is_alive[i - 1];
  for (int i = 0; i < n; ++i) if (is_alive[i] == 0) ans++;

  ans.writeln;
}

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

void main() {
  int n = readln.chomp.to!int;
  auto L = readln.chomp.split.map!(to!int).array;
  int[] is_alive = new int[n];

  for (int i = n - 1; i > 0; i--) {
    if (L[i] == 0) continue;
    int idx = max(0, i - L[i]);
    is_alive[idx] = max(is_alive[idx], i);
  }

  int ans = 0;

  for (int i = 0; i < n; ++i) {
    if (is_alive[i] == 0) ans++;
    else {
      i = is_alive[i] - 1;
    }
  }

  ans.writeln;
}


Naruto has sneaked into the Orochimaru's lair and is now looking for Sasuke. There are $$$T$$$ rooms there. Every room has a door into it, each door can be described by the number $$$n$$$ of seals on it and their integer energies $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$. All energies $$$a_i$$$ are nonzero and do not exceed $$$100$$$ by absolute value. Also, $$$n$$$ is even.In order to open a door, Naruto must find such $$$n$$$ seals with integer energies $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ that the following equality holds: $$$a_{1} \cdot b_{1} + a_{2} \cdot b_{2} + ... + a_{n} \cdot b_{n} = 0$$$. All $$$b_i$$$ must be nonzero as well as $$$a_i$$$ are, and also must not exceed $$$100$$$ by absolute value. Please find required seals for every room there.

The first line contains the only integer $$$T$$$ ($$$1 \leq T \leq 1000$$$) standing for the number of rooms in the Orochimaru's lair. The other lines contain descriptions of the doors. Each description starts with the line containing the only even integer $$$n$$$ ($$$2 \leq n \leq 100$$$) denoting the number of seals. The following line contains the space separated sequence of nonzero integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$|a_{i}| \leq 100$$$, $$$a_{i} \neq 0$$$) denoting the energies of seals.

For each door print a space separated sequence of nonzero integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$|b_{i}| \leq 100$$$, $$$b_{i} \neq 0$$$) denoting the seals that can open the door. If there are multiple valid answers, print any. It can be proven that at least one answer always exists.

NoteFor the first door Naruto can use energies $$$[-100, 1]$$$. The required equality does indeed hold: $$$1 \cdot (-100) + 100 \cdot 1 = 0$$$.For the second door Naruto can use, for example, energies $$$[1, 1, 1, -1]$$$. The required equality also holds: $$$1 \cdot 1 + 2 \cdot 1 + 3 \cdot 1 + 6 \cdot (-1) = 0$$$.

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

string FMT_F = "%.10f";

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

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

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

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

		ans[ti].length = n;
		foreach (i; 0..n/2)
		{
			ans[ti][i*2] = a[i*2+1];
			ans[ti][i*2+1] = -a[i*2];
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

		ans[ti].length = n;
		auto tot = a.sum;
		long cnt;
		foreach (i; 0..n)
		{
			cnt += a[i];
			if (abs(tot-cnt*2) <= 100)
			{
				auto l = lcm(cnt, tot-cnt);
				auto x = l / cnt;
				auto y = l / (tot-cnt);
				foreach (j; 0..i+1)
				{
					ans[ti][j] = x;
				}
				foreach (j; i+1..n)
				{
					ans[ti][j] = -y;
				}
				break;
			}
		}
	}

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


In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order.For each action several separate windows are singled out: k1 separate windows for the first action (the first type windows), k2 windows for the second one (the second type windows), and k3 for the third one (the third type windows). The service time for one person in any of the first type window equals to t1. Similarly, it takes t2 time to serve a person in any of the second type windows. And it takes t3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa.At some moment n people come to the embassy, the i-th person comes at the moment of time ci. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window.The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly.

The first line contains three space-separated integers k1, k2, k3 (1 ≤ ki ≤ 109), they are the number of windows of the first, second and third type correspondingly. The second line contains three space-separated integers t1, t2, t3 (1 ≤ ti ≤ 105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly.  The third line contains an integer n (1 ≤ n ≤ 105), it is the number of people. The fourth line contains n space-separated integers ci (1 ≤ ci ≤ 109) in the non-decreasing order; ci is the time when the person number i comes to the embassy.

Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).

NoteIn the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time). Windows in the second test work like this:The first window of the first type: [1, 6) — the first person, [6, 11) — third person, [11, 16) — fifth personThe second window of the first type: [2, 7) — the second person, [7, 12) — the fourth personThe only second type window: [6, 7) — first, [7, 8) — second, [11, 12) — third, [12, 13) — fourth, [16, 17) — fifthThe only third type window: [7, 8) — first, [8, 9) — second, [12, 13) — third, [13, 14) — fourth, [17, 18) — fifthWe can see that it takes most time to serve the fifth person.

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()
{
    auto r = () => readln.chomp.split.map!(to!int).array;
    auto ks = r();
    auto ts = r();

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

    struct cl { long tm, wait; }
    auto arr = readln.chomp.split.map!(to!int).map!(x => cl(x, 0)).array;

    debug { arr.writeln; }

    auto dl = make!(DList!cl)(arr);
    foreach (i; 0 .. 3) {
        auto windows = make!(DList!long);
        foreach (_; 0 .. min(ks[i], n)) {
            windows ~= 0;
        }

        foreach (_; 0 .. n) {
            auto cur = dl.front;
            dl.removeFront();

            auto w = windows.front;
            windows.removeFront();

            cur.wait += max(w - cur.tm, 0);
            cur.tm = max(cur.tm, w) + ts[i];
            w = cur.tm;

            dl.insertBack(cur);
            windows.insertBack(w);
        }
    }

    auto ans = dl.array.map!(c => c.wait).maxElement + ts.sum;
    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()
{
    auto r = () => readln.chomp.split.map!(to!int).array;
    auto ks = r();
    auto ts = r();

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

    struct cl { int tm, wait; }
    auto arr = readln.chomp.split.map!(to!int).map!(x => cl(x, 0)).array;

    debug { arr.writeln; }

    auto dl = make!(DList!cl)(arr);
    foreach (i; 0 .. 3) {
        auto windows = make!(DList!int);
        foreach (_; 0 .. min(ks[i], n)) {
            windows ~= 0;
        }

        foreach (_; 0 .. n) {
            auto cur = dl.front;
            dl.removeFront();

            auto w = windows.front;
            windows.removeFront();

            cur.wait += max(w - cur.tm, 0);
            cur.tm = max(cur.tm, w) + ts[i];
            w = cur.tm;

            dl.insertBack(cur);
            windows.insertBack(w);
        }
    }

    auto ans = dl.array.map!(c => c.wait).maxElement + ts.sum;
    ans.writeln;
}