You are given an array $$$a$$$ of $$$n$$$ integers.You want to make all elements of $$$a$$$ equal to zero by doing the following operation exactly three times:  Select a segment, for each number in this segment we can add a multiple of $$$len$$$ to it, where $$$len$$$ is the length of this segment (added integers can be different). It can be proven that it is always possible to make all elements of $$$a$$$ equal to zero.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 100\,000$$$): the number of elements of the array. The second line contains $$$n$$$ elements of an array $$$a$$$ separated by spaces: $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

The output should contain six lines representing three operations. For each operation, print two lines:   The first line contains two integers $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n$$$): the bounds of the selected segment.  The second line contains $$$r-l+1$$$ integers $$$b_l, b_{l+1}, \dots, b_r$$$ ($$$-10^{18} \le b_i \le 10^{18}$$$): the numbers to add to $$$a_l, a_{l+1}, \ldots, a_r$$$, respectively; $$$b_i$$$ should be divisible by $$$r - l + 1$$$.

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		if (n == 1)
		{
			writeln ("1 1");
			writeln (-a.front);
			writeln ("1 1");
			writeln ("0");
			writeln ("1 1");
			writeln ("0");
			continue;
		}

		writeln (1, " ", n - 1);
		long [] ans;
		foreach (i; 0..n - 1)
		{
			auto rem = ((-a[i] % n) + n) % n;
			auto delta = (-(n - 1) * rem);
			a[i] += delta;
			ans ~= delta;
		}
		writefln !("%(%s %)") (ans);

		writeln (n, " ", n);
		auto deltaLast = -a.back;
		writeln (deltaLast);
		a.back += deltaLast;

		writeln (1, " ", n);
		writefln !("%(%s %)") (a.map !(q{-a}));
	}
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 8 * 1024 * 1024;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

alias type = single;
struct TestCase
{
  mixin prettyPrinting;

  int n;
  @Dim(q{n}) long[] a;

  void solve(long tc = -1)
  {
    if (n == 1)
      {
        writeln(1, " ", 1);
        writeln(0);
        writeln(1, " ", 1);
        writeln(0);
        writeln(1, " ", 1);
        writeln(-a[0]);
        return;
      }
    long x, y;
    egcd(n, n - 1, x, y);
    assert(n * x + (n - 1) * y == 1);
    writeln(1, " ", n);
    foreach(i, ai; a)
      {
        write(- ai * x * n, " ");
      }
    writeln;
    writeln(1, " ", n - 1);
    foreach(i, ai; a[0 .. $ - 1])
      {
        write(- ai * y * (n - 1), " ");
      }
    writeln;
    writeln(2, " ", n);
    foreach(ai; a[1 .. $ - 1])
      {
        write(0, " ");
      }
    writeln(- a[$ - 1] * y * (n - 1));
  }
}

struct Interval
{
  long l, h;

  bool empty()
  {
    return l > h;
  }
}

Interval intersect(Interval a, Interval b)
{
  return Interval(max(a.l, b.l), min(a.h, b.h));
}

struct If
{
  string condition;
}
enum ignore = If("false");
struct Dim
{
  string dimensions;
  int levels;
  this(string dimensions)
  {
    this.dimensions = dimensions;
    levels = cast(int)dimensions.count(",") + 1;
  }
}

template getArrayLevel(W)
{
  static if (isDynamicArray!W && !is(W == string))
    enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));
  else
    enum getArrayLevel = 0;
}

static foreach(fieldName; 'a' .. cast(char)('z' + 1))
  {
    static if (fieldName != 'o')
      mixin("enum d", fieldName, " = Dim(\"", fieldName, "\");");
  }

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template removeArray(W, int times)
{
  static if (times == 0)
    alias removeArray = W;
  else static if (times == 1)
    alias removeArray = typeof(function(){W w; return w[0];}());
  else
    {
      static assert(isDynamicArray!W);
      alias removeArray = removeArray!(removeArray!(W, 1), times - 1);
    }
}

template baseType(W)
{
  static if (isDynamicArray!W && !is(W == string))
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(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);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, If))
              enum condition = getUDAs!(fieldSymbol, If)[0].condition;
            else
              enum condition = "true";

            if (mixin(condition))
              {
                static if (hasUDA!(fieldSymbol, Dim))
                  {
                    static assert(isDynamicArray!(typeOfField)
                                  , text(W.stringof, ".", fieldName, " has Dim UDA but isn't a Dynamic Array."));
                    enum uda = getUDAs!(fieldSymbol, Dim)[0];
                    static assert(getArrayLevel!(typeOfField) == uda.levels,
                                  text(W.stringof, ".", fieldName, " has a Dim UDA with different number of dimensions than it's type."));
                    mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});
                  }
                else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
                  {
                    pragma(msg, "Warning: You probably want to add dimension to input ", W.stringof, ".", fieldName);
                  }
                read(mixin(fieldName));
              }
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(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)
{
  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;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main(string[] args)
{
  debug stdin = File(args[1]);
  type;
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 4 * 1024 * 1024;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

long egcd(long a, long b, ref long x, ref long y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long x1, y1;
  long d = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

void main(string[] args)
{
  inputFile = stdin;
  auto n = next!long;
  auto a = next!long(n);

  if (n == 1)
    {
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(0);
      writeln(1, " ", 1);
      writeln(-a[0]);
      return;
    }
  long x, y;
  egcd(n, n - 1, x, y);
  assert(n * x + (n - 1) * y == 1);
  writeln(1, " ", n);
  foreach(i, ai; a)
    {
      write(- ai * x * n, " ");
    }
  writeln;
  writeln(1, " ", n - 1);
  foreach(i, ai; a[0 .. $ - 1])
    {
      write(- ai * y * (n - 1), " ");
    }
  writeln;
  writeln(2, " ", n);
  foreach(ai; a[1 .. $ - 1])
    {
      write(0, " ");
    }
  writeln(- a[$ - 1] * y * (n - 1));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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 n = RD!int;
	auto a = RDA;

	auto ans1 = new long[](n);
	foreach (i; 0..n-1)
	{
		ans1[i] = -n * a[i];
	}
	auto ans2 = new long[](n-1);
	foreach (i; 0..n-1)
	{
		ans2[i] = (n-1) * a[i];
	}

	if (n == 1)
	{
		writeln("1 1");
		writeln(0);
		writeln("1 1");
		writeln(0);
		writeln("1 1");
		writeln(-a[0]);
	}
	else
	{
		writeln("1 ", n);
		ans1.map!(to!string).join(" ").writeln;
		writeln("1 ", n-1);
		ans2.map!(to!string).join(" ").writeln;
		writeln(n, " ", n);
		writeln(-a[$-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.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 n = RD!int;
	auto a = RDA;

	auto ans1 = new long[](n);
	foreach (i; 0..n-1)
	{
		ans1[i] = -n * a[i];
	}
	auto ans2 = new long[](n-1);
	foreach (i; 0..n-1)
	{
		ans2[i] = (n-1) * a[i];
	}

	writeln("1 ", n);
	ans1.map!(to!string).join(" ").writeln;
	writeln("1 ", n-1);
	ans2.map!(to!string).join(" ").writeln;
	writeln(n, " ", n);
	writeln(-a[$-1]);

	stdout.flush;
	debug readln;
}


It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are $$$n$$$ displays placed along a road, and the $$$i$$$-th of them can display a text with font size $$$s_i$$$ only. Maria Stepanovna wants to rent such three displays with indices $$$i < j < k$$$ that the font size increases if you move along the road in a particular direction. Namely, the condition $$$s_i < s_j < s_k$$$ should be held.The rent cost is for the $$$i$$$-th display is $$$c_i$$$. Please determine the smallest cost Maria Stepanovna should pay.

The first line contains a single integer $$$n$$$ ($$$3 \le n \le 3\,000$$$) — the number of displays. The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 10^9$$$) — the font sizes on the displays in the order they stand along the road. The third line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 10^8$$$) — the rent costs for each display.

If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $$$i < j < k$$$ such that $$$s_i < s_j < s_k$$$.

NoteIn the first example you can, for example, choose displays $$$1$$$, $$$4$$$ and $$$5$$$, because $$$s_1 < s_4 < s_5$$$ ($$$2 < 4 < 10$$$), and the rent cost is $$$40 + 10 + 40 = 90$$$.In the second example you can't select a valid triple of indices, so the answer is -1.

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;

void main()
{
	int n = readln.chomp.to!int;
	long[] s = readln.chomp.split.map!(to!long).array;
	int[] c = readln.chomp.split.map!(to!int).array;
	long[] sub = new long[](n);
	foreach (i; 0..n) {
		int v = int.max;
		foreach (j; i + 1 .. n) {
			if (s[i] >= s[j]) {
				continue;
			}
			v = min(v, c[j]);
		}
		sub[i] = v;
	}
	long ans = int.max;
	foreach (i; 0..n) {
		foreach (j; i + 1 .. n) {
			if (s[i] >= s[j]) {
				continue;
			}
			debug verbose(i, j, sub[j], c[i] + c[j] + sub[j]);
			ans = min(ans, c[i] + c[j] + sub[j]);
		}
	}
	if (ans >= int.max) {
		writeln = -1;
	} else {
		ans.writeln;
	}
}

void tie(R, Args...)(R arr, ref Args args)
		if (isRandomAccessRange!R || isArray!R)
in
{
	assert (arr.length == args.length);
}
body
{
	foreach (i, ref v; args) {
		alias T = typeof(v);
		v = arr[i].to!T;
	}
}

void verbose(Args...)(in Args args)
{
	stderr.write("[");
	foreach (i, ref v; args) {
		if (i) stderr.write(", ");
		stderr.write(v);
	}
	stderr.writeln("]");
}

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

immutable int INF = 10 ^^ 8 * 3 + 1;

void main()
{
    readln;
    auto s = readln.chomp.split.map!(to!int).array;
    auto c = readln.chomp.split.map!(to!int).array;

    int ans = INF;
    foreach (i, fs, cst; lockstep(s, c)) {
        int getMin(int[] s, int[]c, bool delegate(int) valsFilter) {
            auto vals = zip(s,c).filter!(t => valsFilter(t[0])).map!(t => t[1]);
            return vals.empty() ? INF : vals.minElement;
        }
        auto prev = getMin(s[0..i], c[0..i], x => x < fs);
        auto nxt = getMin(s[i+1..$], c[i+1..$], x => x > fs);
        if (prev != INF && nxt != INF) ans = min(ans, cst + prev + nxt);
    }

    writeln(ans == INF ? -1 : ans);
}

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

immutable int INF = 10 ^^ 8 + 1;

void main()
{
    readln;
    auto s = readln.chomp.split.map!(to!int).array;
    auto c = readln.chomp.split.map!(to!int).array;

    int ans = INF + INF + INF;
    foreach (i, fs, cst; lockstep(s, c)) {
        int prev = INF;
        foreach (j; 0..i) {
            if (s[j] < fs) prev = min(prev, c[j]);
        }
        int nxt = INF;
        foreach (j; i+1..s.length) {
            if (fs < s[j]) nxt = min(nxt, c[j]);
        }
        if (prev != INF && nxt != INF) ans = min(ans, cst + prev + nxt);
    }

    writeln(ans == INF + INF + INF ? -1 : ans);
}

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

immutable int INF = 10 ^^ 8 * 3 + 1;

void main()
{
    readln;
    auto s = readln.chomp.split.map!(to!int).array;
    auto c = readln.chomp.split.map!(to!int).array;

    int ans = INF;
    foreach (i, fs, cst; lockstep(s, c)) {
        int getMin(int[] s, int[]c, bool delegate(int) valsFilter) {
            int ret = INF;
            foreach (_, fs, cst; lockstep(s,c)) {
                if (valsFilter(fs)) ret = min(cst, ret);
            }
            return ret;
        }
        auto prev = getMin(s[0..i], c[0..i], x => x < fs);
        auto nxt = getMin(s[i+1..$], c[i+1..$], x => x > fs);
        if (prev != INF && nxt != INF) ans = min(ans, cst + prev + nxt);
    }

    writeln(ans == INF ? -1 : ans);
}

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

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

  auto tmp=s.dup;
  sort(tmp);
  int[int] map;
  foreach(e; tmp){
    if((e in map)==null) map[e]=map.length.to!(int);
  }
  foreach(i; 0..n) s[i]=map[s[i]];

  auto best=new int[][](n+1, n+1);
  const int inf=1_000_000_000;
  foreach(i; 0..(n+1)) fill(best[i], inf);
  foreach(i; 0..n){
    chmin(best[i][s[i]], c[i]);
    for(int j=s[i]; j>0; j--) chmin(best[i][j-1], best[i][j]);
  }
  for(int i=n-1; i>0; i--){
    foreach(j; 0..n){
      chmin(best[i-1][j], best[i][j]);
    }
  }

  int mn=inf;
  foreach(i; 0..n)foreach(j; (i+1)..n)if(s[i]<s[j]){
    mn=min(mn, c[i]+c[j]+best[j+1][s[j]+1]);
  }

  if(mn==inf) writeln(-1);
  else writeln(mn);
}

void chmin(T)(ref T l, T r){if(l>r)l=r;}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

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

immutable int INF = 10 ^^ 8 * 3 + 1;

void main()
{
    readln;
    auto s = readln.chomp.split.map!(to!int).array;
    auto c = readln.chomp.split.map!(to!int).array;

    int ans = INF;
    foreach (i, fs, cst; lockstep(s, c)) {
        int getMin(int[] s, int[]c, bool delegate(int) valsFilter) {
            auto vals = zip(s,c).filter!(t => valsFilter(t[0])).map!(t => t[1]);
            return vals.empty() ? INF : vals.front();
        }
        auto prev = getMin(s[0..i], c[0..i], x => x < fs);
        auto nxt = getMin(s[i+1..$], c[i+1..$], x => x > fs);
        if (prev != INF && nxt != INF) ans = min(ans, cst + prev + nxt);
    }

    writeln(ans == INF ? -1 : ans);
}

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

immutable int INF = 10 ^^ 8 * 3;

void main()
{
    readln;
    auto s = readln.chomp.split.map!(to!int).array;
    auto c = readln.chomp.split.map!(to!int).array;

    int ans = INF;
    foreach (i, fs, cst; lockstep(s, c)) {
        int getMin(int[] s, int[]c, bool delegate(int) valsFilter) {
            int ret = INF;
            foreach (_, fs, cst; lockstep(s,c)) {
                if (valsFilter(fs)) ret = min(cst, ret);
            }
            return ret;
        }
        auto prev = getMin(s[0..i], c[0..i], x => x < fs);
        auto nxt = getMin(s[i+1..$], c[i+1..$], x => x > fs);
        if (prev != INF && nxt != INF) ans = min(ans, cst + prev + nxt);
    }

    writeln(ans == INF ? -1 : ans);
}


You have a tree of $$$n$$$ vertices. You are going to convert this tree into $$$n$$$ rubber bands on infinitely large plane. Conversion rule follows:  For every pair of vertices $$$a$$$ and $$$b$$$, rubber bands $$$a$$$ and $$$b$$$ should intersect if and only if there is an edge exists between $$$a$$$ and $$$b$$$ in the tree.  Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect. Now let's define following things:   Rubber band $$$a$$$ includes rubber band $$$b$$$, if and only if rubber band $$$b$$$ is in rubber band $$$a$$$'s area, and they don't intersect each other.  Sequence of rubber bands $$$a_{1}, a_{2}, \ldots, a_{k}$$$ ($$$k \ge 2$$$) are nested, if and only if for all $$$i$$$ ($$$2 \le i \le k$$$), $$$a_{i-1}$$$ includes $$$a_{i}$$$.   This is an example of conversion. Note that rubber bands $$$5$$$ and $$$6$$$ are nested. It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints.What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it.

The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in tree. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices.

NoteIn the first sample, you can obtain a nested sequence of $$$4$$$ rubber bands($$$1$$$, $$$2$$$, $$$5$$$, and $$$6$$$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $$$4$$$ rubber bands. However, you cannot make sequence of $$$5$$$ or more nested rubber bands with given tree.  You can see one of the possible conversions for the second sample below.

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [deg, 1];
  auto score = new int[2];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      chmax(score[0], max(dp[v][0], dp[v][1]) + (deg - 1) + ((p != -1) ? 1 : 0));
      chmax(score[1], dp[v][0] + 1);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  if (deg >= 2) {
    chmax(score[0], mxs[0].sum + (deg - 2) + ((p != -1) ? 1 : 0));
    chmax(score[1], mxs[1].sum + 1);
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [deg, 1];
  auto score = new int[2];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      chmax(score[0], max(dp[v][0], dp[v][1]) + (deg - 1) + ((p != -1) ? 1 : 0));
      chmax(score[1], dp[v][0] + 1);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  if (deg >= 2) {
    chmax(score[0], mxs[0].sum + (deg - 2) + ((p != -1) ? 1 : 0));
    chmax(score[1], mxs[1].sum);
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [deg, 1];
  auto score = new int[2];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      chmax(score[0], dp[v][1] + (deg - 1) + ((p != -1) ? 1 : 0));
      chmax(score[1], dp[v][0]);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  if (deg >= 2) {
    chmax(score[0], mxs[0].sum + (deg - 2) + ((p != -1) ? 1 : 0));
    chmax(score[1], mxs[1].sum);
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [0, 1];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  auto score = new int[2];
  foreach (s; 0 .. 2) {
    foreach (j; 0 .. 2) {
      score[s] += mxs[s][j];
    }
  }
  if (deg >= 2) {
    score[0] += (deg - 2);
  }
  if (p != -1) {
    score[0] += 1;
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [deg, 1];
  auto score = new int[2];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      chmax(score[0], max(dp[v][0], dp[v][1]) + (deg - 1) + ((p != -1) ? 1 : 0));
      chmax(score[1], dp[v][0]);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  if (deg >= 2) {
    chmax(score[0], mxs[0].sum + (deg - 2) + ((p != -1) ? 1 : 0));
    chmax(score[1], mxs[1].sum);
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[][] dp;
int ans;

void dfs(int u, int p) {
  int deg;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      ++deg;
    }
  }
  dp[u] = [0, 1];
  auto score = new int[2];
  auto mxs = new int[][](2, 2);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      chmax(dp[u][0], max(dp[v][0], dp[v][1]) + (deg - 1));
      chmax(dp[u][1], dp[v][0] + 1);
      chmax(score[0], dp[v][1] + (deg - 1) + ((p != -1) ? 1 : 0));
      chmax(score[1], dp[v][0]);
      foreach (s; 0 .. 2) {
        int tmp = (s == 0) ? max(dp[v][0], dp[v][1]) : dp[v][0];
        foreach (j; 0 .. 2) {
          if (mxs[s][j] < tmp) {
            swap(mxs[s][j], tmp);
          }
        }
      }
    }
  }
  if (deg >= 2) {
    chmax(score[0], mxs[0].sum + (deg - 2) + ((p != -1) ? 1 : 0));
    chmax(score[1], mxs[1].sum);
  }
  foreach (s; 0 .. 2) {
    chmax(ans, score[s]);
  }
  debug {
    writeln(u, ": ", deg, " ", score);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dp = new int[][](N, 2);
      ans = 0;
      dfs(0, -1);
      debug {
        writeln("dp = ", dp);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types:   up move (denoted by $$$\texttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$.  down move (denoted by $$$\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$.     Example for $$$n=4$$$. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of wheels. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 9$$$) — the digit shown on the $$$i$$$-th wheel after all moves have been performed. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains the integer $$$b_i$$$ ($$$1 \leq b_i \leq 10$$$) and $$$b_i$$$ characters that are either $$$\texttt{U}$$$ or $$$\texttt{D}$$$ — the number of moves performed on the $$$i$$$-th wheel, and the moves performed. $$$\texttt{U}$$$ and $$$\texttt{D}$$$ represent an up move and a down move respectively.

For each test case, output $$$n$$$ space-separated digits  — the initial sequence of the cypher.

3

3

9 3 1

3 DDD

4 UDUU

2 DU

2

0 9

9 DDDDDDDDD

9 UUUUUUUUU

5

0 5 9 8 3

10 UUUUUUUUUU

3 UUD

8 UUDUUDDD

10 UUDUUDUDDU

4 UUUU

NoteIn the first test case, we can prove that initial sequence was $$$[2,1,1]$$$. In that case, the following moves were performed:   On the first wheel: $$$2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{D}]{} 9$$$.  On the second wheel: $$$1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{U}]{} 3$$$.  On the third wheel: $$$1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{U}]{} 1$$$.  The final sequence was $$$[9,3,1]$$$, which matches the input.

import std;

int t,n;

int U(int cur) {
    return cur == 9 ? 0 : cur + 1;
}

int D(int cur) {
    return cur == 0 ? 9 : cur - 1;
}

void main()
{
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        scanf("%d", &n);
        getchar();
        auto fin = readln.split.to!(int[]);
        foreach (w; 0 .. n) {
            auto l = readln.split.to!(string[]);
            foreach (com; l[1].array.reverse)
                if (com == 'U')
                    fin[w] = D(fin[w]);
                else
                    fin[w] = U(fin[w]);
        }
        writefln("%(%s %)", fin);
    }

}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		foreach (i; 0..n)
		{
			auto temp = readln.split;
			foreach (ref c; temp[1])
			{
				a[i] = (a[i] + ((c == 'U') ? 9 : 1)) % 10;
			}
		}
		writefln !("%(%s %)") (a);
	}
}


Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size n has 2n - 1 non-empty subsequences in it. Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ dPikachu was finally left with X subsequences. However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers X and d. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1018. Note the number of elements in the output array should not be more than 104. If no answer is possible, print  - 1.

The only line of input consists of two space separated integers X and d (1 ≤ X, d ≤ 109).

Output should consist of two lines. First line should contain a single integer n (1 ≤ n ≤ 10 000)— the number of integers in the final array. Second line should consist of n space separated integers — a1, a2, ... , an (1 ≤ ai < 1018). If there is no answer, print a single integer -1. If there are multiple answers, print any of them.

NoteIn the output of the first example case, the remaining subsequences after removing those with Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ 5 are [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100]. There are 10 of them. Hence, the array [5, 50, 7, 15, 6, 100] is valid.Similarly, in the output of the second example case, the remaining sub-sequences after removing those with Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ 2 are [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [10, 100, 1000, 10000] is valid.

/+ dub.sdl:
    name "C"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    long B = 10L^^10;

    long x, d;
    sc.read(x, d);
    long U = 1;
    long[] res;
    foreach_reverse (ph; 1..40) {
        long y = (1L << ph) - 1;
        long z = x / y; x %= y;
        foreach (_; 0..z) {
            U += B;
            foreach (i; 0..ph) {
                res ~= U;
            }
        }
    }
    writeln(res.length);
    writeln(res.map!(to!string).join(" "));
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int x, d; readV(x, d);

  auto e = 1L;
  long[] a;
  while (x > 0) {
    auto i = (x+1).bsr;
    foreach (j; 0..i) a ~= e;
    e += d;
    x -= (1<<i)-1;
  }

  writeln(a.length);
  foreach (i, ai; a) {
    write(ai);
    if (i < a.length-1) write(" ");
  }
  writeln;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const X = readLong();
      const D = readLong();

      int[] es;
      for (long x = X; x > 0; ) {
        // 2^e - 1 <= x
        const e = bsr(x + 1);
        es ~= e;
        x -= ((1L << e) - 1);
      }
      debug {
        writeln("es = ", es);
      }

      long[] ans;
      long val = 1;
      foreach (e; es) {
        ans ~= repeat(val, e).array;
        val += D;
      }
      writeln(ans.length);
      foreach (i, a; ans) {
        if (i > 0) write(" ");
        write(a);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}

/+ dub.sdl:
    name "C"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    long B = 10L^^10;

    long x, d;
    sc.read(x, d);
    long U = 1;
    long[] res;
    foreach_reverse (ph; 1..40) {
        long y = (1L << ph) - 1;
        long z = x / y; x %= y;
        foreach (_; 0..z) {
            U += B;
            foreach (i; 0..ph) {
                res ~= U;
            }
        }
    }
    writeln(res.map!(to!string).join(" "));
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int x, d; readV(x, d);

  auto e = 1;
  long[] a;
  while (x > 0) {
    auto i = (x+1).bsr;
    foreach (j; 0..i) a ~= e;
    e += d;
    x -= (1<<i)-1;
  }

  writeln(a.length);
  foreach (i, ai; a) {
    write(ai);
    if (i < a.length-1) write(" ");
  }
  writeln;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int x, d; readV(x, d);

  auto e = 1;
  long[] a;
  while (x > 0) {
    auto i = (x+1).bsr;
    foreach (j; 0..i) a ~= e;
    e += d;
    x -= (1<<i)-1;
  }

  foreach (i, ai; a) {
    write(ai);
    if (i < a.length-1) write(" ");
  }
  writeln;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const X = readLong();
      const D = readLong();

      int[] es;
      for (long x = X; x > 0; ) {
        // 2^e - 1 <= x
        const e = bsr(x + 1);
        es ~= e;
        x -= ((1L << e) - 1);
      }
      debug {
        writeln("es = ", es);
      }

      long[] ans;
      long val;
      foreach (e; es) {
        ans ~= repeat(val, e).array;
        val += D;
      }
      writeln(ans.length);
      foreach (i, a; ans) {
        if (i > 0) write(" ");
        write(a);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const X = readLong();
      const D = readLong();

      int[] es;
      for (long x = X; x > 0; ) {
        // 2^e - 1 <= x
        const e = bsr(x + 1);
        es ~= e;
        x -= ((1L << e) - 1);
      }
      debug {
        writeln("es = ", es);
      }

      long[] ans;
      long val;
      foreach (e; es) {
        ans ~= repeat(val, e).array;
        val += D;
      }
      writeln(ans.length);
      foreach (i, a; ans) {
        if (i > 0) write(" ");
        write(a);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Word $$$s$$$ of length $$$n$$$ is called $$$k$$$-complete if   $$$s$$$ is a palindrome, i.e. $$$s_i=s_{n+1-i}$$$ for all $$$1 \le i \le n$$$;  $$$s$$$ has a period of $$$k$$$, i.e. $$$s_i=s_{k+i}$$$ for all $$$1 \le i \le n-k$$$. For example, "abaaba" is a $$$3$$$-complete word, while "abccba" is not.Bob is given a word $$$s$$$ of length $$$n$$$ consisting of only lowercase Latin letters and an integer $$$k$$$, such that $$$n$$$ is divisible by $$$k$$$. He wants to convert $$$s$$$ to any $$$k$$$-complete word.To do this Bob can choose some $$$i$$$ ($$$1 \le i \le n$$$) and replace the letter at position $$$i$$$ with some other lowercase Latin letter.So now Bob wants to know the minimum number of letters he has to replace to convert $$$s$$$ to any $$$k$$$-complete word.Note that Bob can do zero changes if the word $$$s$$$ is already $$$k$$$-complete.You are required to answer $$$t$$$ test cases independently.

The first line contains a single integer $$$t$$$ ($$$1 \le t\le 10^5$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2 \cdot 10^5$$$, $$$n$$$ is divisible by $$$k$$$). The second line of each test case contains a word $$$s$$$ of length $$$n$$$. It is guaranteed that word $$$s$$$ only contains lowercase Latin letters. And it is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$2 \cdot 10^5$$$.

For each test case, output one integer, representing the minimum number of characters he has to replace to convert $$$s$$$ to any $$$k$$$-complete word.

4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp

NoteIn the first test case, one optimal solution is aaaaaa.In the second test case, the given word itself is $$$k$$$-complete.

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

auto cnt = new int[](26);

void solve() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto S = readln.chomp;

    int ans = 0;

    foreach (i; 0..K/2+K%2) {
        cnt[] = 0;
        if (K % 2 && i == K / 2) {
            foreach (j; 0..N/K) {
                int a = j * K + i;
                cnt[S[a]-'a'] += 1;
                ans += 1;
            }
        } else {
            foreach (j; 0..N/K) {
                int a = j * K + i;
                int b = (j + 1) * K - i - 1;
                cnt[S[a]-'a'] += 1;
                cnt[S[b]-'a'] += 1;
                ans += 2;
            }
        }
        ans -= cnt.reduce!max;
    }

    ans.writeln;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        solve();
    }
}

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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto s = RD!string;

		int len =  n / k;
		int l, r = k-1;
		while (l <= r)
		{
			auto cnt = new int[](26);
			foreach (i; 0..len)
			{
				auto c1 = s[i*k+l] - 'a';
				auto c2 = s[i*k+r] - 'a';
				++cnt[c1];
				++cnt[c2];
			}
			int x;
			foreach (i; 0..26)
			{
				x.chmax(cnt[i]);
			}
			if (l == r)
				ans[ti] += len - x / 2;
			else
				ans[ti] += len * 2 - x;
			debug writeln("ans:", ans[ti]);
			++l;
			--r;
		}
	}

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


You've got a table of size n × m. We'll consider the table rows numbered from top to bottom 1 through n, and the columns numbered from left to right 1 through m. Then we'll denote the cell in row x and column y as (x, y).Initially cell (1, 1) contains two similar turtles. Both turtles want to get to cell (n, m). Some cells of the table have obstacles but it is guaranteed that there aren't any obstacles in the upper left and lower right corner. A turtle (one or the other) can go from cell (x, y) to one of two cells (x + 1, y) and (x, y + 1), as long as the required cell doesn't contain an obstacle. The turtles have had an argument so they don't want to have any chance of meeting each other along the way. Help them find the number of ways in which they can go from cell (1, 1) to cell (n, m).More formally, find the number of pairs of non-intersecting ways from cell (1, 1) to cell (n, m) modulo 1000000007 (109 + 7). Two ways are called non-intersecting if they have exactly two common points — the starting point and the final point.

The first line contains two integers n, m (2 ≤ n, m ≤ 3000). Each of the following n lines contains m characters describing the table. The empty cells are marked by characters ".", the cells with obstacles are marked by "#". It is guaranteed that the upper left and the lower right cells are empty.

In a single line print a single integer — the number of pairs of non-intersecting paths from cell (1, 1) to cell (n, m) modulo 1000000007 (109 + 7).

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

immutable T = 2;
immutable MOD = 1000000007;

int[] input;
string[] field;
long[][][] dp;

void main()
{
  input = readln().split().map!(to!int)().array();
  dp = new long[][][](T, input[0], input[1]);
  foreach (string line; lines(stdin)) {
    field ~= line.chomp();
  }
  solve(0, 0, 1);
  solve(1, 1, 0);

  auto tmp1 = dp[1][input[0] - 1][input[1] - 2] * dp[0][input[0] - 2][input[1] - 1];
  auto tmp2 = dp[1][input[0] - 2][input[1] - 1] * dp[0][input[0] - 1][input[1] - 2];

  writeln(abs(tmp1 % MOD - tmp2 % MOD + MOD) % MOD);
}

void solve (int t, int x, int y)
{
  if (field[x][y] == '.') {
    dp[t][x][y] = 1;
  }
  foreach (i; iota(x, input[0])) {
    foreach (j; iota(y, input[1])) {
      if (field[i][j] == '.' && (i != x || j != y)) {
        auto ii = (i ? dp[t][i - 1][j] : 0);
        auto jj = (j ? dp[t][i][j - 1] : 0);

        dp[t][i][j] = (ii + jj) % MOD;
      }
    }
  }
}