There are $$$n$$$ students numerated from $$$1$$$ to $$$n$$$. The level of the $$$i$$$-th student is $$$a_i$$$. You need to split the students into stable groups. A group of students is called stable, if in the sorted array of their levels no two neighboring elements differ by more than $$$x$$$.For example, if $$$x = 4$$$, then the group with levels $$$[1, 10, 8, 4, 4]$$$ is stable (because $$$4 - 1 \le x$$$, $$$4 - 4 \le x$$$, $$$8 - 4 \le x$$$, $$$10 - 8 \le x$$$), while the group with levels $$$[2, 10, 10, 7]$$$ is not stable ($$$7 - 2 = 5 > x$$$).Apart from the $$$n$$$ given students, teachers can invite at most $$$k$$$ additional students with arbitrary levels (at teachers' choice). Find the minimum number of stable groups teachers can form from all students (including the newly invited).For example, if there are two students with levels $$$1$$$ and $$$5$$$; $$$x = 2$$$; and $$$k \ge 1$$$, then you can invite a new student with level $$$3$$$ and put all the students in one stable group.

The first line contains three integers $$$n$$$, $$$k$$$, $$$x$$$ ($$$1 \le n \le 200\,000$$$, $$$0 \le k \le 10^{18}$$$, $$$1 \le x \le 10^{18}$$$) — the initial number of students, the number of students you can additionally invite, and the maximum allowed level difference. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$) — the students levels.

In the only line print a single integer: the minimum number of stable groups you can split the students into.

NoteIn the first example you can invite two students with levels $$$2$$$ and $$$11$$$. Then you can split the students into two stable groups:   $$$[1, 1, 2, 5, 8, 11, 12, 13]$$$,  $$$[20, 22]$$$. In the second example you are not allowed to invite new students, so you need $$$3$$$ groups:   $$$[1, 1, 5, 5, 20, 20]$$$  $$$[60, 70, 70, 70, 80, 90]$$$  $$$[420]$$$

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

void main()
{
  ulong n, k, x;
  readf!" %d %d %d "(n, k, x);
  auto a = readln.chomp.split.map!(to!ulong).array.sort.array;
  ulong[] gaps;
  foreach (i ; 1 .. a.length) {
    if (a[i] - a[i - 1] > x) {
      gaps ~= a[i] - a[i - 1];
    }
  }
  ulong n_groups = gaps.length + 1;
  gaps.sort;
  foreach (gap ; gaps) {
    ulong n_filler = (gap - 1) / x;
    if (k >= n_filler) {
      n_groups -= 1;
      k -= n_filler;
    }
  }
  writeln(n_groups);
}

import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
       std.container, std.typecons, std.conv, std.random, std.bigint;

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

void main() {
  long n, k, x;
  read(n, k, x);

  auto arr = list!long.sort;

  long[] breakPoints;

  foreach (i; 1 .. n.to!int) {
    if (arr[i] - arr[i - 1] > x) {
      breakPoints ~= arr[i] - arr[i - 1];
    }
  }

  sort(breakPoints);

  long answer = breakPoints.length + 1;

  foreach (b; breakPoints) {
    auto req = (b - 1) / x;
    if (req <= k) {
      k -= req;
      answer--;
    } else {
      break;
    }
  }

  writeln(answer);
}

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

void main()
{
  ulong n, k, x;
  readf!" %d %d %d "(n, k, x);
  auto a = readln.chomp.split.map!(to!ulong).array.sort.array;
  ulong[] gaps;
  foreach (i ; 1 .. a.length) {
    if (a[i] - a[i - 1] > x) {
      gaps ~= a[i] - a[i - 1];
    }
  }
  ulong n_groups = gaps.length + 1;
  gaps.sort;
  foreach (gap ; gaps) {
    ulong n_filler = gap / x;
    if (k >= n_filler) {
      n_groups -= 1;
      k -= n_filler;
    }
  }
  writeln(n_groups);
}

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

void main()
{
  ulong n, k, x;
  readf!" %d %d %d "(n, k, x);
  auto a = readln.chomp.split.map!(to!ulong).array.sort.array;
  ulong[] gaps;
  foreach (i ; 1 .. a.length) {
    if (a[i] - a[i - 1] > x) {
      gaps ~= a[i] - a[i - 1];
    }
  }
  ulong n_groups = gaps.length + 1;
  gaps.sort;
  foreach (gap ; gaps) {
    ulong n_filler = gap / (x + 1);
    if (k >= n_filler) {
      if (n_groups > 0)
        n_groups -= 1;
      k -= n_filler;
    } else {
      break;
    }
  }
  writeln(n_groups);
}

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

void main()
{
  ulong n, k, x;
  readf!" %d %d %d "(n, k, x);
  auto a = readln.chomp.split.map!(to!ulong).array.sort.array;
  ulong[] gaps;
  foreach (i ; 1 .. a.length) {
    if (a[i] - a[i - 1] > x) {
      gaps ~= a[i] - a[i - 1];
    }
  }
  ulong n_groups = gaps.length + 1;
  gaps.sort;
  foreach (gap ; gaps) {
    ulong n_filler = gap / (x + 1);
    if (k >= n_filler) {
      n_groups -= 1;
      k -= n_filler;
    } else {
      break;
    }
  }
  writeln(n_groups);
}

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

void main()
{
  ulong n, k, x;
  readf!" %d %d %d "(n, k, x);
  auto a = readln.chomp.split.map!(to!ulong).array.sort.array;
  ulong[] gaps;
  foreach (i ; 1 .. a.length) {
    if (a[i] - a[i - 1] > x) {
      gaps ~= a[i] - a[i - 1];
    }
  }
  ulong n_groups = gaps.length + 1;
  gaps.sort;
  foreach (gap ; gaps) {
    ulong n_filler = 0;
/*
    while (gap > x) {
      n_filler++;
      gap -= x + 1;
    }
*/
    n_filler = (gap + x) / (x + 1);

    if (k >= n_filler) {
      n_groups -= 1;
      k -= n_filler;
    } else {
      break;
    }
  }
  writeln(n_groups);
}


It's a walking tour day in SIS.Winter, so $$$t$$$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P":   "A" corresponds to an angry student  "P" corresponds to a patient student Such string describes the row from the last to the first student.Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $$$i$$$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $$$i+1$$$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.

The first line contains a single integer $$$t$$$ — the number of groups of students ($$$1 \le t \le 100$$$). The following $$$2t$$$ lines contain descriptions of groups of students. The description of the group starts with an integer $$$k_i$$$ ($$$1 \le k_i \le 100$$$) — the number of students in the group, followed by a string $$$s_i$$$, consisting of $$$k_i$$$ letters "A" and "P", which describes the $$$i$$$-th group of students.

For every group output single integer — the last moment a student becomes angry.

NoteIn the first test, after $$$1$$$ minute the state of students becomes PPAA. After that, no new angry students will appear.In the second tets, state of students in the first group is:   after $$$1$$$ minute — AAPAAPPAAPPP  after $$$2$$$ minutes — AAAAAAPAAAPP  after $$$3$$$ minutes — AAAAAAAAAAAP  after $$$4$$$ minutes all $$$12$$$ students are angry In the second group after $$$1$$$ minute, all students are angry.

import std.stdio;

void main() {
  int t;
  readf!"%d\n"(t);
  for(int i = 0; i < t; i++) {
    int k;
    readf!"%d\n"(k);
    int max = 0;
    int cur = 0;
    bool calm = true;
    for(int j = 0; j < k; j++) {
      char c;
      readf!"%c"(c);
      if(c == 'P') {
        if(calm) continue;
        cur++;
        if(cur > max) max = cur;
      }
      if(c == 'A') {
        calm = false;
        cur = 0;
      }
    }
    readf!"\n"();
    writeln(max);
  }
}

import std.algorithm;
import std.conv : to;
import std.container;
import std.numeric : gcd;
import std.range;
import std.stdio;
import std.typecons : Tuple, tuple;

string readToken() {
    static string[] tokens;
    while (tokens.empty)
        tokens = readln.split;
    auto token = tokens.front;
    tokens.popFront;
    return token;
}

int readInt() {
    return readToken.to!int;
}

void main() {
    int T = readInt;
    while (T--) {
        int n = readInt;
        string s = readToken;
        int result = 0, count = 0;
        for (int i = n; i--;) {
            if (s[i] == 'P') {
                count++;
            }
            else {
                result = max(result, count), count = 0;
            }
        }
        writeln(result);
    }
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(t[i]);
        }
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value = E.init)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

void main()
{
  int t;
  read(t);
  foreach(tc; 0 .. t)
    {
      int k;
      string s;
      read(k, s);
      s ~= 'A';
      size_t last = -1;
      size_t maxspace = 0;
      foreach(i, c; s)
        {
          if (c == 'A')
            {
              if (last != -1)
                maxspace = max(maxspace, i - last - 1);
              last = i;
            }
        }
      writeln(maxspace);
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto k = RD!int;
		auto s = RD!string;
		bool hasA;
		int streak;
		foreach (i; 0..k)
		{
			if (!hasA)
			{
				if (s[i] == 'A')
					hasA = true;
			}
			else if (s[i] == 'P')
			{
				++streak;
			}
			else
			{
				ans[ti].chmax(streak);
				streak = 0;
			}
		}
		ans[ti].chmax(streak);
	}

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


In this problem you will write a simple code generator for a 2D programming language derived from Brainfuck.The code in this language is a rectangular grid of characters '.' and 'X'. The code is converted to a Brainfuck program as follows: the characters are read in the usual order (top to bottom, left to right), and each 'X' character is converted a Brainfuck instruction to be executed. The instruction is defined by the left, top and right neighbors of the 'X' character using the following conversion table:  You are given a string. Output a program in the described language which prints this string.You can download the language interpreter used for judging here: https://assets.codeforces.com/rounds/952/puzzling-interpreter.cpp (use C++11 to compile the code). Note several implementation details: The first step of the language interpretation is conversion to a Brainfuck program, which is then executed. The code must be rectangular, with all lines of the same length. It can have at most 10,000 lines and 10,000 columns, and can have at most 500,000 'X' characters. The code has toroidal topology, i.e. the 'X' on the first line will have top neighbor in the last line. Brainfuck interpreter has 30000 memory cells which store integers from 0 to 255 with increment/decrement done modulo 256. Console input (, command) is allowed in Brainfuck code but has no effect when executed.

The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive.

Output a program in the described language which, when executed, will print the given message.

NoteThe example corresponds to the following Brainfuck program:       -      >+<     >+++<    >+++++<   >+++++++<  >+++++++++< >+++++++++++<<             >...The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines print the '$' character three times.

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

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		string a = ".";
		string b = ".";
		ubyte cur = 0;
		foreach (c; s.representation)
		{
			while (c != cur)
			{
				cur -= 1;
				a ~= "X.";
				b ~= "..";
			}
			a ~= ".X..";
			b ~= "XXX.";
		}
		writeln (a);
		writeln (b);
	}
}

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

void main(){

	string s;
	while ((s = readln.strip) != ""){
		string a = ".";
		string b = ".";
		ubyte cur = 0;
		foreach (c; s.representation){
			while (c != cur){
				cur -= 1;
				a ~= "X.";
				b ~= "..";
			}
			a ~= ".X..";
			b ~= "XXX.";
		}
		writeln (a);
		writeln (b);
	}
}


You are given $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. Find the maximum value of $$$max(a_l, a_{l + 1}, \ldots, a_r) \cdot min(a_l, a_{l + 1}, \ldots, a_r)$$$ over all pairs $$$(l, r)$$$ of integers for which $$$1 \le l < r \le n$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.

For each test case, print a single integer  — the maximum possible value of the product from the statement.

import std.stdio;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int t = readInt!int;
	while (t--) solve;
}

void solve()
{
	int n = readInt!int;
	auto a = new int[](n); foreach(ref ai; a) ai = readInt!int;
	iota(0, n - 1).map!(i => cast(long)a[i] * a[i+1]).fold!max.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

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

		foreach (i; 0..n-1)
		{
			ans[ti].chmax(a[i]*a[i+1]);
		}
	}

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


You are given a permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$, where $$$n$$$ is an even number. Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:   take two indices $$$i$$$ and $$$j$$$ such that $$$2 \cdot |i - j| \geq n$$$ and swap $$$p_i$$$ and $$$p_j$$$. There is no need to minimize the number of operations, however you should use no more than $$$5 \cdot n$$$ operations. One can show that it is always possible to do that.

The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$, $$$n$$$ is even) — the length of the permutation.  The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the given permutation.

On the first line print $$$m$$$ ($$$0 \le m \le 5 \cdot n$$$) — the number of swaps to perform. Each of the following $$$m$$$ lines should contain integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$, $$$|a_i - b_i| \ge \frac{n}{2}$$$) — the indices that should be swapped in the corresponding swap. Note that there is no need to minimize the number of operations. We can show that an answer always exists.

NoteIn the first example, when one swap elements on positions $$$1$$$ and $$$2$$$, the array becomes sorted.In the second example, pay attention that there is no need to minimize number of swaps.In the third example, after swapping elements on positions $$$1$$$ and $$$5$$$ the array becomes: $$$[4, 5, 3, 1, 2, 6]$$$. After swapping elements on positions $$$2$$$ and $$$5$$$ the array becomes $$$[4, 2, 3, 1, 5, 6]$$$ and finally after swapping elements on positions $$$1$$$ and $$$4$$$ the array becomes sorted: $$$[1, 2, 3, 4, 5, 6]$$$.

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int half = n / 2;
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		p[] -= 1;
		auto q = new int [n];
		foreach (i, v; p)
		{
			q[v] = i;
		}

		int [] [] ans;

		void doSwap (int i, int j)
		{
			ans ~= [i + 1, j + 1];
			p.swapAt (i, j);
			q.swapAt (p[i], p[j]);
		}

		foreach (i; 0..half)
		{
			int j = i + half;
			int last = n - 1;

			if (q[j] < j)
			{
				doSwap (q[j], last);
			}
			if (q[j] > j)
			{
				doSwap (q[j], i);
			}
			if (q[j] == i)
			{
				doSwap (q[j], j);
			}
			if (q[j] != j)
			{
				assert (false);
			}

			if (q[i] > i && q[i] < j)
			{
				doSwap (q[i], last);
			}
			if (q[i] > j)
			{
				doSwap (q[i], i);
			}
			if (q[i] != i)
			{
				assert (false);
			}
		}

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

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

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

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

alias T = Tuple!(int, int);

void main() {
  auto r = new InputReader;
  immutable n = r.next!int;
  auto p = r.nextA!int(n);
  p[] -= 1;
  auto q = new int[n];
  foreach (i; 0 .. n) {
    q[p[i]] = i;
  }
  int m = n / 2;
  T[] res;
  int s (int i, int j) {
    debug stderr.writefln ("swap: %d %d\n", i + 1, j + 1);
    if (i == j) {
      return j;
    }
    debug stderr.writefln ("%d %d", 2 * abs (i - j), n);
    assert (2 * abs (i - j) >= n);
    res ~= tuple (i + 1, j + 1);
    swap (p[i], p[j]);
    q[p[i]] = i;
    q[p[j]] = j;
    return j;
  }
  immutable last = n - 1;
  debug stderr.writeln ("m = ", m);
  foreach_reverse (i; 1 .. m) {
    int pos = m - i;
    int u = q[pos];
    debug stderr.writeln ("p = ", p, ", pos = ", pos, ", u = ", u);
    if (u != pos) {
      if (u < m) {
        u = s (u, last);
      } else {
        u = s (u, 0);
        u = s (0, last);
      }
      assert (u == last);
      s (last, pos);
    }
    pos = m + i - 1;
    u = q[pos];
    debug stderr.writeln ("p = ", p, ", pos = ", pos, ", u = ", u);
    if (u != pos) {
      if (u >= m) {
        u = s (u, 0);
      } else {
        u = s (u, last);
        u = s (last, 0);
      }
      assert (u == 0);
      s (0, pos);
    }
  }
  if (p[0] != 0) {
    s (0, last);
  }
  writeln (res.length);
  foreach (t; res) {
    writeln (t[0], ' ', t[1]);
  }
  debug {
    stderr.writeln (p);
    foreach (i; 0 .. n) {
      assert (p[i] == i);
    }
  }
}

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

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

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

	int n = read.to!int;

	int[] as;
	foreach(i; 0 .. n) as ~= read.to!int - 1;
	print!1("as:", as);

	int[] bs = new int[](n);
	foreach(int i, a; as) bs[a] = i;
	print!1("bs:", bs);

	int[2][] ans;
	void swap(int i, int j){
		print!1("i:", i, " j:", j);
		if(i == j) return;
		else if(i > j){
			swap(j, i);
			return;
		}
		else if(j - i >= n / 2){
			ans ~= [i + 1, j + 1];
			int x = as[i], y = as[j];
			as[i] = y, as[j] = x;
			bs[y] = i, bs[x] = j;
			print!1("-> i:", i, " j:", j, " as:", as);
			return;
		}
		else if(i < n  / 2 && j < n / 2){
			swap(i, n - 1);
			swap(j, n - 1);
			swap(i, n - 1);
			return;
		}
		else if(i < n / 2 && j >= n / 2){
			swap(i, n - 1);
			swap(0, j);
			swap(0, n - 1);
			swap(i, n - 1);
			swap(0, j);
			return;
		}
		else if(i >= n / 2 && j >= n / 2){
			swap(0, i);
			swap(0, j);
			swap(0, i);
			return;
		}
		assert(0);
	}

	foreach(i; 0 .. n){
		if(as[i] == i) continue;
		int j = bs[i];
		swap(i, j);
	}

	ans.length.writeln;
	foreach(an; ans){
		writeln(an[0], " ", an[1]);
	}

}


Ashish has $$$n$$$ elements arranged in a line. These elements are represented by two integers $$$a_i$$$ — the value of the element and $$$b_i$$$ — the type of the element (there are only two possible types: $$$0$$$ and $$$1$$$). He wants to sort the elements in non-decreasing values of $$$a_i$$$.He can perform the following operation any number of times:  Select any two elements $$$i$$$ and $$$j$$$ such that $$$b_i \ne b_j$$$ and swap them. That is, he can only swap two elements of different types in one move. Tell him if he can sort the elements in non-decreasing values of $$$a_i$$$ after performing any number of operations.

The first line contains one integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ $$$(1 \le n \le 500)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_i$$$ $$$(1 \le a_i \le 10^5)$$$  — the value of the $$$i$$$-th element. The third line containts $$$n$$$ integers $$$b_i$$$ $$$(b_i \in \{0, 1\})$$$  — the type of the $$$i$$$-th element.

For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value. You may print each letter in any case (upper or lower).

NoteFor the first case: The elements are already in sorted order.For the second case: Ashish may first swap elements at positions $$$1$$$ and $$$2$$$, then swap elements at positions $$$2$$$ and $$$3$$$.For the third case: The elements are already in sorted order.For the fourth case: No swap operations may be performed as there is no pair of elements $$$i$$$ and $$$j$$$ such that $$$b_i \ne b_j$$$. The elements cannot be sorted.For the fifth case: Ashish may swap elements at positions $$$3$$$ and $$$4$$$, then elements at positions $$$1$$$ and $$$2$$$.

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

void main ()
{
	foreach (test; 0..readln.strip.to !(int))
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		auto ok = a.isSorted || (0 < b.sum && b.sum < n);
		writeln (ok ? "Yes" : "No");
	}
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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;
		auto b = RDA;

		long cnt, cnt0, cnt1;
		long last0, last1;
		foreach (i; 0..n)
		{
			if (b[i] == 0)
			{
				if (a[i] < last0)
				{
					++cnt0;
				}
				last0 = a[i];
				++cnt;
			}
			else
			{
				if (a[i] < last1)
				{
					++cnt1;
				}
				last1 = a[i];
			}
		}
		if (min(cnt, n-cnt) == 0)
			ans[ti] = max(cnt0, cnt1) == 0;
		else
			ans[ti] = true;
	}

	foreach (e; ans)
		writeln(e ? "Yes" : "No");
	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;
		auto b = RDA;

		long cnt, cnt0, cnt1;
		long last0, last1;
		foreach (i; 0..n)
		{
			if (b[i] == 0)
			{
				if (a[i] < last0)
				{
					++cnt0;
				}
				last0 = a[i];
				++cnt;
			}
			else
			{
				if (a[i] < last1)
				{
					++cnt1;
				}
				last1 = a[i];
			}
		}
		ans[ti] = max(cnt0, cnt1) <= min(cnt, n-cnt);
	}

	foreach (e; ans)
		writeln(e ? "Yes" : "No");
	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;
		auto b = RDA;

		bool ok = true;
		long last0, last1;
		foreach (i; 0..n)
		{
			if (b[i] == 0)
			{
				if (a[i] < last0)
				{
					ok = false;
					break;
				}
				last0 = a[i];
			}
			else
			{
				if (a[i] < last1)
				{
					ok = false;
					break;
				}
				last1 = a[i];
			}
		}
		ans[ti] = ok;
	}

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


Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $$$n$$$ integers written on it.Polycarp wrote the numbers from the disk into the $$$a$$$ array. It turned out that the drive works according to the following algorithm:   the drive takes one positive number $$$x$$$ as input and puts a pointer to the first element of the $$$a$$$ array;  after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $$$a$$$ array, the last element is again followed by the first one;  as soon as the sum is at least $$$x$$$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $$$m$$$ questions. To answer the $$$i$$$-th of them, you need to find how many seconds the drive will work if you give it $$$x_i$$$ as input. Please note that in some cases the drive can work infinitely.For example, if $$$n=3, m=3$$$, $$$a=[1, -3, 4]$$$ and $$$x=[1, 5, 2]$$$, then the answers to the questions are as follows:   the answer to the first query is $$$0$$$ because the drive initially points to the first item and the initial sum is $$$1$$$.  the answer to the second query is $$$6$$$, the drive will spin the disk completely twice and the amount becomes $$$1+(-3)+4+1+(-3)+4+1=5$$$.  the answer to the third query is $$$2$$$, the amount is $$$1+(-3)+4=2$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of two positive integers $$$n$$$, $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of numbers on the disk and the number of asked questions. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). The third line of each test case contains $$$m$$$ positive integers $$$x_1, x_2, \ldots, x_m$$$ ($$$1 \le x \le 10^9$$$). It is guaranteed that the sums of $$$n$$$ and $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$.

Print $$$m$$$ numbers on a separate line for each test case. The $$$i$$$-th number is:    $$$-1$$$ if the drive will run infinitely;  the number of seconds the drive will run, otherwise.

// cheese-cracker [2022-01-26]

void solve(){
    int n = scan!int;
    int m = scan!int;
    auto arr = scanArray;
    auto pref = arr.cumulativeFold!((a, b) => a + b)(0L).array;
    tup[] selects;
    selects ~= tup(pref.front, 0L);
    for(int i = 1; i < n; ++i){
        if(pref[i] > selects.back[0]){
            selects ~= tup(pref[i], i.to!long);
        }
    }

    long summ = pref.back;
    long maxk = selects.back[0];

    long[] res;
    auto qrs = scanArray;
    foreach(x; qrs){
        // Rotations is ceil of this
        long num = x - maxk;
        long rots = 0;
        if(summ > 0 && num > 0){
            rots = num / summ + (num % summ != 0);
        }else{
            rots = 0;
        }
        /* show(x, num, summ, rots); */

        // Soln Exists

        long left = x - summ * rots;
        if(selects.back[0] < left){
            res ~= -1;
            continue;
        }
        /* show(left); */

        int lo = -1, hi = selects.length.to!int - 1;
        while(hi - lo > 1){
            int midd = (lo + hi)/2;
            /* show(lo, midd, hi); */
            ( (selects[midd][0] >= left ) ?  hi : lo ) = midd;
        }
        res ~= (rots * n + selects[hi][1]);
    }
    res.each!(a => write(a, " "));
    writeln;
}

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

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

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T); while (T--) {
        int N, M; get(N, M);
        long[] AA; get(AA);
        long[] XX; get(XX);

        auto ss = new long[](N);
        ss[0] = AA[0];
        foreach (i; 1..N) ss[i] += ss[i - 1] + AA[i];

        auto ms = new long[](N);
        ms[0] = ss[0];
        foreach (i; 1..N) ms[i] = max(ms[i-1], ss[i]);

        long[] res;
        foreach (x; XX) {
            if (ss[$-1] <= 0 && x > ms[$-1]) {
                res ~= -1;
                continue;
            }
            auto min_a = max(0, x - ms[$-1]);
            auto c = ss[$-1] <= 0 ? 0 : (min_a + ss[$-1] - 1) / ss[$-1];
            auto d = ss[$-1] * c;
            if (d + AA[0] >= x) {
                res ~= c * N;
                continue;
            }
            int l, r = (N-1).to!int;
            while (l + 1 < r) {
                auto m = (l + r) / 2;
                if (d + ms[m] >= x) {
                    r = m;
                } else {
                    l = m;
                }
            }
            res ~= c * N + r;
        }
        writefln!"%(%d %)"(res);
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = [0L];
		foreach (ref c; a)
		{
			s ~= s.back + c;
		}
		auto t = s.dup;
		foreach (i; 0..n)
		{
			t[i + 1] = max (t[i + 1], t[i]);
		}
		auto x = readln.splitter.map !(to !(int)).array;
		long [] ans;
		foreach (long y; x)
		{
			if (t[n] < y && s[n] <= 0)
			{
				ans ~= -1;
				continue;
			}
			long steps = 0;
			if (t[n] < y)
			{
				steps = (y + s[n] - 1 - t[n]) / s[n];
			}
			y -= steps * s[n];
			auto shift = t[1..n].assumeSorted.lowerBound (y)
			    .length.to !(int);
			ans ~= steps * n + shift;
		}
		writefln !("%(%s %)") (ans);
	}
}


For the given integer $$$n$$$ ($$$n > 2$$$) let's write down all the strings of length $$$n$$$ which contain $$$n-2$$$ letters 'a' and two letters 'b' in lexicographical (alphabetical) order.Recall that the string $$$s$$$ of length $$$n$$$ is lexicographically less than string $$$t$$$ of length $$$n$$$, if there exists such $$$i$$$ ($$$1 \le i \le n$$$), that $$$s_i < t_i$$$, and for any $$$j$$$ ($$$1 \le j < i$$$) $$$s_j = t_j$$$. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.For example, if $$$n=5$$$ the strings are (the order does matter):  aaabb  aabab  aabba  abaab  ababa  abbaa  baaab  baaba  babaa  bbaaa It is easy to show that such a list of strings will contain exactly $$$\frac{n \cdot (n-1)}{2}$$$ strings.You are given $$$n$$$ ($$$n > 2$$$) and $$$k$$$ ($$$1 \le k \le \frac{n \cdot (n-1)}{2}$$$). Print the $$$k$$$-th string from the list.

The input contains one or more test cases. The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case is written on the the separate line containing two integers $$$n$$$ and $$$k$$$ ($$$3 \le n \le 10^5, 1 \le k \le \min(2\cdot10^9, \frac{n \cdot (n-1)}{2})$$$. The sum of values $$$n$$$ over all test cases in the test doesn't exceed $$$10^5$$$.

For each test case print the $$$k$$$-th string from the list of all described above strings of length $$$n$$$. Strings in the list are sorted lexicographically (alphabetically).

aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa

import std.stdio: writeln, readln;
import std.array: array, split;
import std.string: chomp, count;
import std.conv: to;
import std.algorithm: map;

string multi_str(string a, ulong times) {
    string result;
    foreach (ulong i; 0..times) {
        result ~= a;
    }
    return result;
}
// 7 6 --> 0001100
void main() {
    int t = readln.chomp.to!int;
    foreach (int _; 0..t) {
        int[2] words = readln.split.map!(to!int).array;
        int[] a = [1];
        int i;
        while (a[$-1] <= words[1]) {
            a ~= ++i + a[$-1];
        }
        writeln(multi_str("a", words[0] - a.count) ~ "b" ~ multi_str("a", a.count - words[1] + a[$-2] - 2) ~ "b" ~ multi_str("a", words[1] - a[$-2]));
    }
}

// https://codeforces.com/problemset/problem/1328/B
import std.stdio;
import std.string;
import std.conv;

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

    int n, k;
foreach(kase; 0..t) {
    readf("%d %d\n", &n, &k);

    char[] ans = "".dup;

    foreach(ch; 0..n) ans ~= "a";

    for(int idx = n-2; idx >= 0; idx--) {
        if(k <= (n-idx-1)) {
            ans[idx] = 'b';
            ans[n-k] = 'b';
            break;
        }
        k -= n-idx-1;
    }
    ans.writeln;
}

}

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

string FMT_F = "%.10f";

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

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

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

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

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

		int x;
		size_t pos1, pos2;
		foreach (i; 0..n)
		{
			x += i+1;
			if (x >= k)
			{
				pos1 = n-2-i;
				pos2 = pos1 + 1 + x - k;
				break;
			}
		}
		debug writeln("pos:", pos1, ", ", pos2);
		foreach (i; 0..n)
		{
			if (i == pos1 || i == pos2)
				ans[ti] ~= 'b';
			else
				ans[ti] ~= 'a';
		}
	}

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


There is a strange peculiarity: if you connect the cities of Rostov, Taganrog and Shakhty, peculiarly, you get a triangle«Unbelievable But True»Students from many different parts of Russia and abroad come to Summer Informatics School. You marked the hometowns of the SIS participants on a map.Now you decided to prepare an interesting infographic based on this map. The first thing you chose to do is to find three cities on this map, such that they form a triangle with area $$$S$$$.

The first line of input contains two integers $$$n$$$ and $$$S$$$ ($$$3 \le n \le 2000$$$, $$$1 \le S \le 2 \cdot 10^{18}$$$) — the number of cities on the map and the area of the triangle to be found. The next $$$n$$$ lines contain descriptions of the cities, one per line. Each city is described by its integer coordinates $$$x_i$$$, $$$y_i$$$ ($$$-10^9 \le x_i, y_i \le 10^9$$$).  It is guaranteed that all cities are located at distinct points. It is also guaranteed that no three cities lie on the same line.

If the solution doesn't exist — print «No». Otherwise, print «Yes», followed by three pairs of coordinates $$$(x, y)$$$ — the locations of the three cities, which form the triangle of area $$$S$$$.

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

alias Point = Tuple !(int, q{x}, int, q{y});

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = cast (int) (s);
		int sn = -sp;
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				p[j].x -= p[i].x;
				p[j].y -= p[i].y;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp || t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (p[j].x) *
					    p[k].y -
					    cast (long) (p[k].x) *
					    p[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (p[i].x, " ", p[i].y);
						writeln (p[j].x + p[i].x, " ",
						    p[j].y + p[i].y);
						writeln (p[k].x + p[i].x, " ",
						    p[k].y + p[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				p[j].x += p[i].x;
				p[j].y += p[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 107;
immutable int mod2 = mod ^^ 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = (+s % mod + mod) % mod;
		int sn = (-s % mod + mod) % mod;
		auto b = new bool [mod2 * 2];
		foreach (i; -mod2..mod2)
		{
			if ((i % mod + mod) % mod == (sp % mod + mod) % mod ||
			    (i % mod + mod) % mod == (sn % mod + mod) % mod)
			{
				b[i + mod2] = true;
			}
		}
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto q = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				p[j].x -= p[i].x;
				p[j].y -= p[i].y;
				q[j].x = (p[j].x % mod + mod) % mod;
				q[j].y = (p[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int qjx = q[j].x;
				int qjy = q[j].y;
				int k;
				enum span = 4;
				for (k = j + 1; k + span <= n; k += span)
				{
					bool ok = false;
					static foreach (r; 0..span)
					{{
						auto t = qjx * q[k + r].y -
						    qjy * q[k + r].x;
						ok |= b[t + mod2];
					}}
					if (!ok)
					{
						continue;
					}
					foreach (r; 0..span)
					{{
						auto t = cast (long) (p[j].x) *
						    p[k + r].y -
						    cast (long) (p[k + r].x) *
						    p[j].y;
						if (t == +s || t == -s)
						{
							writeln ("Yes");
							writeln (p[i].x, " ", p[i].y);
							writeln (p[j].x + p[i].x, " ",
							    p[j].y + p[i].y);
							writeln (p[k + r].x + p[i].x, " ",
							    p[k + r].y + p[i].y);
							continue multitest_loop;
						}
					}}
				}
				for ( ; k < n; k++)
				{
					auto t = cast (long) (p[j].x) *
					    p[k].y -
					    cast (long) (p[k].x) *
					    p[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (p[i].x, " ", p[i].y);
						writeln (p[j].x + p[i].x, " ",
						    p[j].y + p[i].y);
						writeln (p[k].x + p[i].x, " ",
						    p[k].y + p[i].y);
						continue multitest_loop;
					}
				}
			}
			foreach (j; i + 1..n)
			{
				p[j].x += p[i].x;
				p[j].y += p[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 44_221;
immutable int mod2 = mod ^^ 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = cast (int) (s);
		int sn = -sp;
		sp = (sp % mod2 + mod2) % mod2;
		sn = (sn % mod2 + mod2) % mod2;
		int msp = -sp;
		int msn = -sn;
		auto q = new Point [n];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto p = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				p[j].x = ((q[j].x - q[i].x) % mod + mod) % mod;
				p[j].y = ((q[j].y - q[i].y) % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (q[j].x) *
					    q[k].y -
					    cast (long) (q[k].x) *
					    q[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (q[i].x, " ", q[i].y);
						writeln (q[j].x + q[i].x, " ",
						    q[j].y + q[i].y);
						writeln (q[k].x + q[i].x, " ",
						    q[k].y + q[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				p[j].x += p[i].x;
				p[j].y += p[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 31627;
immutable int mod2 = mod ^^ 2;
immutable int mod2x2 = mod2 * 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = (+s % mod2x2 + 1L * mod2x2) % mod2x2;
		int sn = (-s % mod2x2 + 1L * mod2x2) % mod2x2;
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto q = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				p[j].x -= p[i].x;
				p[j].y -= p[i].y;
				q[j].x = (p[j].x % mod + mod) % mod;
				q[j].y = (p[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int qjx = q[j].x;
				int qjy = q[j].y;
				int v;
				bool ok = false;
				enum span = 8;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = qjx * q[v + r].y -
						    qjy * q[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = qjx * q[v].y - qjy * q[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (p[j].x) *
					    p[k].y -
					    cast (long) (p[k].x) *
					    p[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (p[i].x, " ", p[i].y);
						writeln (p[j].x + p[i].x, " ",
						    p[j].y + p[i].y);
						writeln (p[k].x + p[i].x, " ",
						    p[k].y + p[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				p[j].x += p[i].x;
				p[j].y += p[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 31627;
immutable int mod2 = mod ^^ 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = cast (int) (s);
		int sn = -sp;
		sp = (sp % mod2 + mod2) % mod2;
		sn = (sn % mod2 + mod2) % mod2;
		int msp = -sp;
		int msn = -sn;
		auto q = new Point [n];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto p = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				q[j].x -= q[i].x;
				q[j].y -= q[i].y;
				p[j].x = (q[j].x % mod + mod) % mod;
				p[j].y = (q[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (q[j].x) *
					    q[k].y -
					    cast (long) (q[k].x) *
					    q[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (q[i].x, " ", q[i].y);
						writeln (q[j].x + q[i].x, " ",
						    q[j].y + q[i].y);
						writeln (q[k].x + q[i].x, " ",
						    q[k].y + q[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				q[j].x += q[i].x;
				q[j].y += q[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 31627;
immutable int mod2 = mod ^^ 2 * 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = (+s % mod2 + mod2) % mod2;
		int sn = (-s % mod2 + mod2) % mod2;
		auto q = new Point [n];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto p = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				q[j].x -= q[i].x;
				q[j].y -= q[i].y;
				p[j].x = (q[j].x % mod + mod) % mod;
				p[j].y = (q[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (q[j].x) *
					    q[k].y -
					    cast (long) (q[k].x) *
					    q[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (q[i].x, " ", q[i].y);
						writeln (q[j].x + q[i].x, " ",
						    q[j].y + q[i].y);
						writeln (q[k].x + q[i].x, " ",
						    q[k].y + q[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				q[j].x += q[i].x;
				q[j].y += q[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 31627;
immutable int mod2 = mod ^^ 2 * 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = cast (int) (s);
		int sn = -sp;
		sp = (sp % mod2 + mod2) % mod2;
		sn = (sn % mod2 + mod2) % mod2;
		int msp = -sp;
		int msn = -sn;
		auto q = new Point [n];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto p = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				q[j].x -= q[i].x;
				q[j].y -= q[i].y;
				p[j].x = (q[j].x % mod + mod) % mod;
				p[j].y = (q[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (q[j].x) *
					    q[k].y -
					    cast (long) (q[k].x) *
					    q[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (q[i].x, " ", q[i].y);
						writeln (q[j].x + q[i].x, " ",
						    q[j].y + q[i].y);
						writeln (q[k].x + q[i].x, " ",
						    q[k].y + q[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				q[j].x += q[i].x;
				q[j].y += q[i].y;
			}
		}
		writeln ("No");
	}
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

immutable int mod = 44_221;
immutable int mod2 = mod ^^ 2;

void main ()
{
	int n;
	long s;
multitest_loop:
	while (readf (" %s %s", &n, &s) > 0)
	{
		s *= 2;
		int sp = cast (int) (s);
		int sn = -sp;
		sp = (sp % mod2 + mod2) % mod2;
		sn = (sn % mod2 + mod2) % mod2;
		int msp = -sp;
		int msn = -sn;
		auto q = new Point [n];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto p = new Point [n];
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				q[j].x -= q[i].x;
				q[j].y -= q[i].y;
				p[j].x = (q[j].x % mod + mod) % mod;
				p[j].y = (q[j].y % mod + mod) % mod;
			}
			foreach (j; i + 1..n)
			{
				int pjx = p[j].x;
				int pjy = p[j].y;
				int v;
				bool ok = false;
				enum span = 4;
				for (v = j + 1; v + span <= n; v += span)
				{
					static foreach (r; 0..span)
					{{
						auto t = pjx * p[v + r].y -
						    pjy * p[v + r].x;
						ok |= (t == sp) || (t == sn) ||
						    (-t == sp) || (-t == sn);
					}}
				}
				for ( ; v < n; v++)
				{
					auto t = pjx * p[v].y - pjy * p[v].x;
					ok |= (t == sp) || (t == sn) ||
					    (-t == sp) || (-t == sn);
				}
				if (!ok)
				{
					continue;
				}
				foreach (k; j + 1..n)
				{
					auto t = cast (long) (q[j].x) *
					    q[k].y -
					    cast (long) (q[k].x) *
					    q[j].y;
					if (t == +s || t == -s)
					{
						writeln ("Yes");
						writeln (q[i].x, " ", q[i].y);
						writeln (q[j].x + q[i].x, " ",
						    q[j].y + q[i].y);
						writeln (q[k].x + q[i].x, " ",
						    q[k].y + q[i].y);
						continue multitest_loop;
					}
				}
//				assert (false);
			}
			foreach (j; i + 1..n)
			{
				q[j].x += q[i].x;
				q[j].y += q[i].y;
			}
		}
		writeln ("No");
	}
}


You are given an array of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$You can apply the following operation an arbitrary number of times:   select an index $$$i$$$ ($$$1 \le i \le n$$$) and replace the value of the element $$$a_i$$$ with the value $$$a_i + (a_i \bmod 10)$$$, where $$$a_i \bmod 10$$$ is the remainder of the integer dividing $$$a_i$$$ by $$$10$$$. For a single index (value $$$i$$$), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of $$$a_i$$$ is taken into account each time. For example, if $$$a_i=47$$$ then after the first operation we get $$$a_i=47+7=54$$$, and after the second operation we get $$$a_i=54+4=58$$$.Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.For example, you have an array $$$[6, 11]$$$.   Let's apply this operation to the first element of the array. Let's replace $$$a_1 = 6$$$ with $$$a_1 + (a_1 \bmod 10) = 6 + (6 \bmod 10) = 6 + 6 = 12$$$. We get the array $$$[12, 11]$$$.  Then apply this operation to the second element of the array. Let's replace $$$a_2 = 11$$$ with $$$a_2 + (a_2 \bmod 10) = 11 + (11 \bmod 10) = 11 + 1 = 12$$$. We get the array $$$[12, 12]$$$. Thus, by applying $$$2$$$ operations, you can make all elements of an array equal.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. What follows is a description of each test case. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \le a_i \le 10^9$$$) — array elements. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print:   YES if it is possible to make all array elements equal;  NO otherwise.  You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) .

NoteThe first test case is clarified above.In the second test case, it is impossible to make all array elements equal.In the third test case, you need to apply this operation once to all elements equal to $$$5$$$.In the fourth test case, you need to apply this operation to all elements until they become equal to $$$8$$$.In the fifth test case, it is impossible to make all array elements equal.In the sixth test case, you need to apply this operation to all elements until they become equal to $$$102$$$.

import std;

int t,n;

int[] even = [1,2,4,8]; // 0
int[] odd  = [3,6,7,9]; // 1
int[] five = [0,5]; // 2

int checkCase(int a) {
    if (a < 10) {
        if (even.canFind(a))
            return 0;
        else if (odd.canFind(a))
            return 1;
        else if (five.canFind(a))
            return 2;
    }
    else {
        if (a % 10 == 1) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if ((a % 10 == 0) || (a % 10 == 5)) {
            return 2;
        }
        else if ((a % 10 == 3) || (a % 10 == 7) || (a % 10 == 9)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
        else if ((a % 10 == 2) || (a % 10 == 4) || (a % 10 == 8)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if (a % 10 == 6) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
    }
    return 0;
}

void main()
{
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        scanf("%d", &n);
        getchar();
        auto arr = readln.split.to!(int[]);
        if (n == 1)
            writeln("yes");
        else {
            auto initType = checkCase(arr[0]);
            bool isSame = true;
            foreach(i; 1..n)
                if (initType != checkCase(arr[i])) {
                    isSame = false;
                    break;
                }
            if (isSame) {
                if (initType == 2) {
                    auto tmp = arr.sort.uniq.array;
                    if ((tmp.length == 2) && (maxElement(tmp) - minElement(tmp) == 5) && (minElement(tmp) % 10 == 5))
                        writeln("yes");
                    else if (tmp.length == 1)
			writeln("yes");
		    else
                        writeln("no");
                }
                else
                    writeln("yes");
            }
            else
                writeln("no");
        }
    }
}

import std;

int t,n;

int[] even = [1,2,4,8]; // 0
int[] odd  = [3,6,7,9]; // 1
int[] five = [0,5]; // 2

int checkCase(int a) {
    if (a < 10) {
        if (even.canFind(a))
            return 0;
        else if (odd.canFind(a))
            return 1;
        else if (five.canFind(a))
            return 2;
    }
    else {
        if (a % 10 == 1) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if ((a % 10 == 0) || (a % 10 == 5)) {
            return 2;
        }
        else if ((a % 10 == 3) || (a % 10 == 7) || (a % 10 == 9)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
        else if ((a % 10 == 2) || (a % 10 == 4) || (a % 10 == 8)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if (a % 10 == 6) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
    }
    return 0;
}

void main()
{
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        scanf("%d", &n);
        getchar();
        auto arr = readln.split.to!(int[]);
        if (n == 1)
            writeln("yes");
        else {
            auto initType = checkCase(arr[0]);
            bool isSame = true;
            foreach(i; 1..n)
                if (initType != checkCase(arr[i])) {
                    isSame = false;
                    break;
                }
            if (isSame) {
                if (initType == 2) {
                    auto tmp = arr.sort.uniq.array;
                    if ((tmp.length == 2) && (maxElement(tmp) - minElement(tmp) == 5) && (minElement(tmp) % 10 == 5))
                        writeln("yes");
                    else
                        writeln("no");
                }
                else
                    writeln("yes");
            }
            else
                writeln("no");
        }
    }
}

import std;

int t,n;

int[] even = [1,2,4,8]; // 0
int[] odd  = [3,6,7,9]; // 1
int[] five = [0,5]; // 2

int checkCase(int a) {
    if (a < 10) {
        if (even.canFind(a))
            return 0;
        else if (odd.canFind(a))
            return 1;
        else if (five.canFind(a))
            return 2;
    }
    else {
        if (a % 10 == 1) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if ((a % 10 == 0) || (a % 10 == 5)) {
            return 2;
        }
        else if ((a % 10 == 3) || (a % 10 == 7) || (a % 10 == 9)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
        else if ((a % 10 == 2) || (a % 10 == 4) || (a % 10 == 8)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if (a % 10 == 6) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
    }
    return 0;
}

void main()
{
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        scanf("%d", &n);
        getchar();
        auto arr = readln.split.to!(int[]);
        if (n == 1)
            writeln("yes");
        else {
            auto initType = checkCase(arr[0]);
            bool isSame = true;
            foreach(i; 1..n)
                if (initType != checkCase(arr[i])) {
                    isSame = false;
                    break;
                }
            if (isSame) {
                if (initType == 2) {
                    auto tmp = arr.sort.uniq.array;
                    if ((tmp.length == 2) && (maxElement(tmp) - minElement(tmp) == 5) && (minElement(tmp) != 0))
                        writeln("yes");
                    else
                        writeln("no");
                }
                else
                    writeln("yes");
            }
            else
                writeln("no");
        }
    }
}

import std;

int t,n;

int[] even = [1,2,4,8]; // 0
int[] odd  = [3,6,7,9]; // 1
int[] five = [0,5]; // 2

int checkCase(int a) {
    if (a < 10) {
        if (even.canFind(a))
            return 0;
        else if (odd.canFind(a))
            return 1;
        else if (five.canFind(a))
            return 2;
    }
    else {
        if (a % 10 == 1) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if ((a % 10 == 0) || (a % 10 == 5)) {
            return 2;
        }
        else if ((a % 10 == 3) || (a % 10 == 7) || (a % 10 == 9)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;
        }
        else if ((a % 10 == 2) || (a % 10 == 4) || (a % 10 == 8)) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 0;
            else
                return 1;
        }
        else if (a % 10 == 6) {
            if (((a - a % 10) / 10) % 2 == 0)
                return 1;
            else
                return 0;

        }
    }
    return 0;
}

void main()
{
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        scanf("%d", &n);
        getchar();
        auto arr = readln.split.to!(int[]);
        if (arr.length == 1)
            writeln("yes");
        else {
            auto initType = checkCase(arr[0]);
            bool isSame = true;
            foreach(i; 1..n)
                if (initType != checkCase(arr[i])) {
                    isSame = false;
                    break;
                }
            if (isSame) {
                if (initType == 2) {
                    auto tmp = arr.sort.uniq.array;
                    if ((tmp.length == 2) && (maxElement(tmp) - minElement(tmp) == 5))
                        writeln("yes");
                    else
                        writeln("no");
                }
                else
                    writeln("yes");
            }
            else
                writeln("no");
        }
    }
}


There are $$$n$$$ products in the shop. The price of the $$$i$$$-th product is $$$a_i$$$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product $$$i$$$ in such a way that the difference between the old price of this product $$$a_i$$$ and the new price $$$b_i$$$ is at most $$$k$$$. In other words, the condition $$$|a_i - b_i| \le k$$$ should be satisfied ($$$|x|$$$ is the absolute value of $$$x$$$).He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price $$$b_i$$$ of each product $$$i$$$ should be positive (i.e. $$$b_i > 0$$$ should be satisfied for all $$$i$$$ from $$$1$$$ to $$$n$$$).Your task is to find out the maximum possible equal price $$$B$$$ of all productts with the restriction that for all products the condiion $$$|a_i - B| \le k$$$ should be satisfied (where $$$a_i$$$ is the old price of the product and $$$B$$$ is the same new price of all products) or report that it is impossible to find such price $$$B$$$.Note that the chosen price $$$B$$$ should be integer.You should answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is presented by two lines. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100, 1 \le k \le 10^8$$$) — the number of products and the value $$$k$$$. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^8$$$), where $$$a_i$$$ is the price of the $$$i$$$-th product.

Print $$$q$$$ integers, where the $$$i$$$-th integer is the answer $$$B$$$ on the $$$i$$$-th query. If it is impossible to equalize prices of all given products with restriction that for all products the condition $$$|a_i - B| \le k$$$ should be satisfied (where $$$a_i$$$ is the old price of the product and $$$B$$$ is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.

NoteIn the first example query you can choose the price $$$B=2$$$. It is easy to see that the difference between each old price and each new price $$$B=2$$$ is no more than $$$1$$$.In the second example query you can choose the price $$$B=6$$$ and then all the differences between old and new price $$$B=6$$$ will be no more than $$$2$$$.In the third example query you cannot choose any suitable price $$$B$$$. For any value $$$B$$$ at least one condition out of two will be violated: $$$|1-B| \le 2$$$, $$$|6-B| \le 2$$$.In the fourth example query all values $$$B$$$ between $$$1$$$ and $$$7$$$ are valid. But the maximum is $$$7$$$, so it's the answer.

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.string;

immutable long INF = 1L << 59;

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto s = readln.split.map!(to!int);
        auto N = s[0];
        auto K = s[1].to!long;
        auto A = readln.split.map!(to!long).array;
        long hi = INF;
        long lo = -INF;
        foreach (a; A) {
            hi = min(hi, a + K);
            lo = max(lo, a - K);
        }
        writeln(hi >= lo ? hi : -1);
    }
}

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

  int q;
  rd(q);
  while (q--) {
    int n, k;
    rd(n, k);
    auto a = readln.split.to!(int[]);
    a.sort;
    auto lb = a[n - 1] - k, ub = a[0] + k;
    if (lb <= ub) {
      writeln(ub);
    } else {
      writeln(-1);
    }
  }
}

// [a[n - 1] - k, a[0] + k]

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

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

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto q = RD!int;
	auto ans = new long[](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDR.ARR;
		a.sort();
		if (a[0] + k < a[$-1] - k)
		{
			ans[i] = -1;
		}
		else
		{
			ans[i] = a[0] + k;
		}
	}

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


You are fed up with your messy room, so you decided to clean it up.Your room is a bracket sequence $$$s=s_{1}s_{2}\dots s_{n}$$$ of length $$$n$$$. Each character of this string is either an opening bracket '(' or a closing bracket ')'.In one operation you can choose any consecutive substring of $$$s$$$ and reverse it. In other words, you can choose any substring $$$s[l \dots r]=s_l, s_{l+1}, \dots, s_r$$$ and change the order of elements in it into $$$s_r, s_{r-1}, \dots, s_{l}$$$.For example, if you will decide to reverse substring $$$s[2 \dots 4]$$$ of string $$$s=$$$"((()))" it will be equal to $$$s=$$$"()(())".A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.A prefix of a string $$$s$$$ is a substring that starts at position $$$1$$$. For example, for $$$s=$$$"(())()" there are $$$6$$$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()".In your opinion, a neat and clean room $$$s$$$ is a bracket sequence that:  the whole string $$$s$$$ is a regular bracket sequence;  and there are exactly $$$k$$$ prefixes of this sequence which are regular (including whole $$$s$$$ itself). For example, if $$$k = 2$$$, then "(())()" is a neat and clean room.You want to use at most $$$n$$$ operations to make your room neat and clean. Operations are applied one after another sequentially.It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $$$n$$$ or less operations.

The first line contains integer number $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le \frac{n}{2}, 2 \le n \le 2000$$$, $$$n$$$ is even) — length of $$$s$$$ and required number of regular prefixes. The second line of a test case contains $$$s$$$ of length $$$n$$$ — the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $$$\frac{n}{2}$$$ characters '(' and exactly $$$\frac{n}{2}$$$ characters ')' in the given string. The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$2000$$$.

For each test case print an answer. In the first line print integer $$$m$$$ ($$$0 \le m \le n$$$) — the number of operations. You do not need to minimize $$$m$$$, any value is suitable. In the following $$$m$$$ lines print description of the operations, each line should contain two integers $$$l,r$$$ ($$$1 \le l \le r \le n$$$), representing single reverse operation of $$$s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$$$. Operations are applied one after another sequentially. The final $$$s$$$ after all operations should be a regular, also it should be exactly $$$k$$$ prefixes (including $$$s$$$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any.

NoteIn the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $$$s$$$).

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new size_t[2][][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto K = RD!int;
		auto s = RD!(char[]);
		int cnt = n/2 - K;
		foreach (j; 0..cnt+1)
		{
			if (s[j] == '(') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == ')') continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
				break;
			}
		}
		foreach (j; cnt+1..(cnt+1)*2)
		{
			if (s[j] == ')') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == '(') continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
				break;
			}
		}
		foreach (j; (cnt+1)*2..n)
		{
			auto c = j % 2 == 0 ? '(' : ')';
			if (s[j] == c) continue;
			foreach (k; j+1..n)
			{
				if (s[k] != c) continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
				break;
			}
		}
	}

	foreach (e; ans)
	{
		writeln(e.length);
		foreach (ee; e)
			writeln(ee[0]+1, " ", ee[1]+1);
	}

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

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.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 = readInt();
        const S = readToken();

        string t;
        foreach (_; 0 .. N / 2 - (K - 1)) t ~= '(';
        foreach (_; 0 .. N / 2 - (K - 1)) t ~= ')';
        foreach (_; 0 .. K - 1) t ~= "()";
        debug {
          writeln("t = ", t);
        }

        auto s = S.dup;
        writeln(N);
        foreach (i; 0 .. N) {
          foreach (j; i .. N) {
            if (s[j] == t[i]) {
              s[i .. j + 1].reverse;
              writeln(i + 1, " ", j + 1);
              goto found;
            }
          }
          assert(false);
         found:
        }
        assert(s == t);
      }
    }
  } 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.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new size_t[2][][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto K = RD!int;
		auto s = RD!(char[]);
		int cnt = n/2 - K;
		foreach (j; 0..cnt+1)
		{
			if (s[j] == '(') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == ')') continue;
				auto d = k - j;
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
					ans[i] ~= [l, r];
				}
			}
		}
		foreach (j; cnt+1..(cnt+1)*2)
		{
			if (s[j] == ')') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == '(') continue;
				auto d = k - j;
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
					ans[i] ~= [l, r];
				}
			}
		}
		foreach (j; (cnt+1)*2..n)
		{
			auto c = j % 2 == 0 ? '(' : ')';
			if (s[j] == c) continue;
			foreach (k; j+1..n)
			{
				if (s[k] != c) continue;
				auto d = k - j;
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
					ans[i] ~= [l, r];
				}
			}
		}
	}

	foreach (e; ans)
	{
		writeln(e.length);
		foreach (ee; e)
			writeln(ee[0]+1, " ", ee[1]+1);
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new size_t[2][][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto K = RD!int;
		auto s = RD!(char[]);
		int cnt = n/2 - K;
		foreach (j; 0..cnt+1)
		{
			if (s[j] == '(') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == ')') continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
			}
		}
		foreach (j; cnt+1..(cnt+1)*2)
		{
			if (s[j] == ')') continue;
			foreach (k; j+1..n)
			{
				if (s[k] == '(') continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
			}
		}
		foreach (j; (cnt+1)*2..n)
		{
			auto c = j % 2 == 0 ? '(' : ')';
			if (s[j] == c) continue;
			foreach (k; j+1..n)
			{
				if (s[k] != c) continue;
				auto d = k - j;
				ans[i] ~= [j, k];
				foreach (l; j..j+(d+1)/2)
				{
					auto r = k-(l-j);
					swap(s[l], s[r]);
				}
			}
		}
	}

	foreach (e; ans)
	{
		writeln(e.length);
		foreach (ee; e)
			writeln(ee[0]+1, " ", ee[1]+1);
	}

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