Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

The first line contains an integer $$$t$$$ $$$(1 \le t \le 5)$$$ — the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains 2 integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le x \le 10^4$$$) — the number of elements in the array $$$a$$$ and the number that Ehab hates. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$0 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$.

For each testcase, print the length of the longest subarray whose sum isn't divisible by $$$x$$$. If there's no such subarray, print $$$-1$$$.

NoteIn the first test case, the subarray $$$[2,3]$$$ has sum of elements $$$5$$$, which isn't divisible by $$$3$$$.In the second test case, the sum of elements of the whole array is $$$6$$$, which isn't divisible by $$$4$$$.In the third test case, all subarrays have an even sum, so the answer is $$$-1$$$.

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto s = a.sum;
		if (s % x)
		{
			ans[ti] = n;
			continue;
		}

		auto b = new long[](n+1);
		foreach (i; 0..n)
		{
			b[i+1] = (b[i] + a[i]) % x;
		}

		ans[ti] = -1;
		foreach (i; 0..n)
		{
			if (b[i] != 0 || b[n-i] != 0)
			{
				ans[ti] = n-i;
				break;
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto s = a.sum;
		if (s % x)
		{
			ans[ti] = n;
			continue;
		}

		auto y = long.max;
		foreach (i; 0..n)
		{
			if ((s - a[i]) % x)
			{
				y.chmin(a[i]);
			}
		}
		if (y == long.max)
			ans[ti] = -1;
		else
			ans[ti] = n-1;
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (a.sum % x)
		{
			ans[ti] = n;
			continue;
		}

		auto y = long.max;
		foreach (i; 0..n)
		{
			if (a[i] != x)
			{
				y.chmin(a[i]);
			}
		}
		if (y == long.max)
			ans[ti] = -1;
		else
			ans[ti] = n-1;
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (a.sum % x)
		{
			ans[ti] = n;
			continue;
		}

		auto y = long.max;
		foreach (i; 0..n)
		{
			if (a[i] % x)
			{
				y.chmin(a[i]);
			}
		}
		if (y == long.max)
			ans[ti] = -1;
		else
			ans[ti] = n-1;
	}

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


Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.

The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).  The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:   Binary number of exactly m bits.  The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.  Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.

In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.

5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb

NoteIn the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.

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

void main ()
{
    int n;
    int m;
    while (readf (" %s %s", &n, &m) > 0)
    {
        readln;
        auto expr = new string [] [n];
        int [string] num;
        auto op1 = new int [n];
        auto op2 = new int [n];

        int operand (int i, string s)
        {
            if (s == "?")
            {
                return -1;
            }
            else if ('0' <= s[0] && s[0] <= '1')
            {
                return -2;
            }
            else
            {
                assert (s in num);
                return num[s];
            }
        }

        foreach (i; 0..n)
        {
            expr[i] = readln.split;
            num[expr[i][0]] = i;
            op1[i] = operand (i, expr[i][2]);
            if (expr[i].length == 3)
            {
                continue;
            }
            op2[i] = operand (i, expr[i][4]);
        }

        int calc (int j, int b)
        {
            auto v = new int [n];

            int value (int i, int k, int cur)
            {
                if (cur == -1)
                {
                    return b;
                }
                if (cur == -2)
                {
                    return expr[i][k][j] - '0';
                }
                assert (cur < i);
                return v[cur];
            }

            foreach (i; 0..n)
            {
                v[i] = value (i, 2, op1[i]);
                if (expr[i].length == 5)
                {
                    if (expr[i][3] == "OR")
                    {
                        v[i] |= value (i, 4, op2[i]);
                    }
                    if (expr[i][3] == "XOR")
                    {
                        v[i] ^= value (i, 4, op2[i]);
                    }
                    if (expr[i][3] == "AND")
                    {
                        v[i] &= value (i, 4, op2[i]);
                    }
                }
            }

            return sum (v);
        }

        string resMin;
        string resMax;
        foreach (j; 0..m)
        {
            int u = calc (j, 0);
            int v = calc (j, 1);
            if (u == v)
            {
                resMin ~= '0';
                resMax ~= '0';
            }
            if (u > v)
            {
                resMin ~= '1';
                resMax ~= '0';
            }
            if (u < v)
            {
                resMin ~= '0';
                resMax ~= '1';
            }
        }
        writeln (resMin);
        writeln (resMax);
    }
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto m = next!int;
  struct VarDef
  {
    int varname;
    enum ExpType
      {
	value, and, or, xor
      }
    ExpType type;
    int v1 = -1, v2 = -1;
    string s1, s2;
  }
  auto vdefs = new VarDef[](n);
  int[string] indexof;
  int lastindex = 0;
  foreach(i; 0 .. n)
    {
      auto line = inputFile.readln;
      auto words = line.split;
      indexof[words[0]] = lastindex++;
      if (words.length == 5)
	{
	  vdefs[i].varname = indexof[words[0]];

	  if (words[2][0] == '0' || words[2][0] == '1') vdefs[i].s1 = words[2];
	  else if (words[2][0] == '?') {}
	  else vdefs[i].v1 = indexof[words[2]];

	  final switch(words[3])
	    {
	    case "AND":
	      vdefs[i].type = VarDef.ExpType.and;
	      break;
	    case "XOR":
	      vdefs[i].type = VarDef.ExpType.xor;
	      break;
	    case "OR":
	      vdefs[i].type = VarDef.ExpType.or;
	      break;
	    }
	  if (words[4][0] == '0' || words[4][0] == '1') vdefs[i].s2 = words[4];
	  else if (words[4][0] == '?') {}
	  else vdefs[i].v2 = indexof[words[4]];

	}
      else
	{
	  debug assert(words.length == 3);
	  vdefs[i].varname = indexof[words[0]];
	  vdefs[i].type = VarDef.ExpType.value;
	  if (words[2][0] == '0' || words[2][0] == '1') vdefs[i].s1 = words[2];
	  else if (words[2][0] == '?') {}
	  else vdefs[i].v1 = indexof[words[2]];
	}
    }
  debug writeln(vdefs);
  int evalbit(int bitpos, int bitval)
  {
    int[] varval = new int[](n);
    int expval(int v, string exp)
    {
      if (v != -1) return varval[v];
      if (exp !is null) return cast(int)(exp[bitpos] - '0');
      return bitval;
    }
    void eval(VarDef vdef)
    {
      final switch(vdef.type)
	{
	case VarDef.ExpType.value:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1);
	  break;
	case VarDef.ExpType.and:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) & expval(vdef.v2, vdef.s2);
	  break;
	case VarDef.ExpType.xor:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) ^ expval(vdef.v2, vdef.s2);
	  break;
	case VarDef.ExpType.or:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) | expval(vdef.v2, vdef.s2);
	  break;
	}
    }
    foreach(vdef; vdefs)
      eval(vdef);
    return cast(int)(varval.sum);
  }
  auto ansmax = new char[](m);
  auto ansmin = new char[](m);
  foreach(bitpos; 0 .. m)
    {
      ansmax[bitpos] = '0';
      ansmin[bitpos] = '0';
      auto r0 = evalbit(cast(int)bitpos, 0);
      auto r1 = evalbit(cast(int)bitpos, 1);
      if (r1 < r0)
	ansmin[bitpos] = '1';
      if (r1 > r0)
	ansmax[bitpos] = '1';
    }
  ansmin.idup.writeln;
  ansmax.idup.writeln;
}

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


You have unweighted tree of $$$n$$$ vertices. You have to assign a positive weight to each edge so that the following condition would hold:  For every two different leaves $$$v_{1}$$$ and $$$v_{2}$$$ of this tree, bitwise XOR of weights of all edges on the simple path between $$$v_{1}$$$ and $$$v_{2}$$$ has to be equal to $$$0$$$. Note that you can put very large positive integers (like $$$10^{(10^{10})}$$$).It's guaranteed that such assignment always exists under given constraints. Now let's define $$$f$$$ as the number of distinct weights in assignment.  In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $$$0$$$. $$$f$$$ value is $$$2$$$ here, because there are $$$2$$$ distinct edge weights($$$4$$$ and $$$5$$$). In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $$$1$$$ and vertex $$$6$$$ ($$$3, 4, 5, 4$$$) is not $$$0$$$. What are the minimum and the maximum possible values of $$$f$$$ for the given tree? Find and print both.

The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in given 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.

Print two integers — the minimum and maximum possible value of $$$f$$$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.

NoteIn the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.   In the second example, possible assignments for each minimum and maximum are described in picture below. The $$$f$$$ value of valid assignment of this tree is always $$$3$$$.   In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

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 n = RD!int;
	auto edges = new int[][](n);
	foreach (i; 0..n-1)
	{
		auto a = RD!int-1;
		auto b = RD!int-1;
		edges[a] ~= b;
		edges[b] ~= a;
	}
	debug writeln(edges);

	bool odds;
	{
		int[][] open;
		auto dist = new int[](n);
		dist[] = -1;
		foreach (i; 0..n)
		{
			if (edges[i].length == 1)
			{
				open ~= [i, -1];
				dist[i] = 0;
			}
		}

		(){
		while (!open.empty)
		{
			auto nd = open.front; open.popFront;
			auto from = nd[0];
			auto par = nd[1];
			auto d = dist[from]^1;
			foreach (to; edges[from])
			{
				if (to == par) continue;
				if (dist[to] == -1)
				{
					dist[to] = d;
					open ~= [to, from];
				}
				else if (dist[to] != d)
				{
					odds = true;
					return;
				}
			}
		}}();
	}

	long cnt;
	{
		int[][] open;
		auto dist = new int[](n);
		dist[] = -1;
		foreach (i; 0..n)
		{
			if (edges[i].length == 1)
			{
				open ~= [i, -1];
				dist[i] = 0;
			}
		}
		debug writeln("open:", open);
		foreach (e; open)
		{
			int[][] op = [e];
			while (!op.empty)
			{
				auto nd = op.front; op.popFront;
				auto from = nd[0];
				auto par = nd[1];
				auto d = dist[from]+1;
				if (d == 3) continue;
				foreach (to; edges[from])
				{
					if (to == par) continue;
					if (dist[to] == 0)
					{
						++cnt;
						debug writeln(from, ":", to);
					}
					else if (dist[to] == -1 && d != 2)
					{
						dist[to] = d;
						op ~= [to, from];
					}
				}
			}
		}
	}
	debug writeln("cnt:", cnt);

	long ans1 = odds ? 3 : 1;
	long ans2 = n-1-cnt;
	writeln(ans1, " ", ans2);
	stdout.flush;
	debug readln;
}

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)
	{
		auto a = new int [] [n];
		auto d = new int [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
			d[u] += 1;
			d[v] += 1;
		}

		int root = 0;
		while (d[root] != 1)
		{
			root += 1;
		}

		auto depth = new int [n];

		void recur (int v, int p)
		{
			foreach (u; a[v])
			{
				if (u != p)
				{
					depth[u] = depth[v] + 1;
					recur (u, v);
				}
			}
		}

		depth[root] = 0;
		recur (root, -1);

		int res = n - 1;
		foreach (v; 0..n)
		{
			auto numLeaves = a[v].count !(u => d[u] == 1);
			if (numLeaves > 1)
			{
				res -= numLeaves - 1;
			}
		}
		auto haveOdd = n.iota
		    .filter !(v => d[v] == 1)
		    .map !(v => depth[v])
		    .any !(x => x % 2 != 0);
		writeln (haveOdd ? 3 : 1, " ", res);
	}
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[] dep;

void dfs(int u, int p) {
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
}

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;
      }
      int rt = -1;
      foreach (u; 0 .. N) {
        if (G[u].length == 1) {
          rt = u;
          break;
        }
      }
      dep = new int[N];
      dfs(rt, -1);

      int ansMin = 1;
      foreach (u; 0 .. N) {
        if (G[u].length == 1) {
          if (dep[u] % 2 != 0) {
            ansMin = 3;
          }
        }
      }

      int ansMax;
      auto used = new bool[N];
      foreach (i; 0 .. N - 1) {
        if (G[A[i]].length == 1) {
          used[B[i]] = true;
        } else if (G[B[i]].length == 1) {
          used[A[i]] = true;
        } else {
          ++ansMax;
        }
      }
      foreach (u; 0 .. N) {
        if (used[u]) {
          ++ansMax;
        }
      }

      writeln(ansMin, " ", ansMax);
    }
  } catch (EOFException e) {
  }
}


William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times:   Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$  $$$a_i = \frac{a_i}{2}$$$  $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array.

For each test case output the maximal sum of array elements after performing an optimal sequence of operations.

NoteIn the first example test case the optimal sequence would be:   Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2].  Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2].  Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$.

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int);
  long maxA = long.min;
  long sumA = 0;
  auto powers = 0;
  foreach(ref ai; a)
    {
      while ((ai & 1) == 0)
	{
	  powers++;
	  ai >>= 1;
	}
      sumA += ai;
      maxA = max(maxA, ai);
    }
  long power = (1L << powers);
  long ans = power * (cast(long)maxA) + (sumA - maxA);
  ans.writeln;
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!long).array;
    long mulcnt = 0;
    long sum = 0;
    foreach (ref x ; a) {
      while (x % 2 == 0) {
        mulcnt++;
        x /= 2;
      }
      sum += x;
    }
    a.sort;
    sum -= a.back;
    while (mulcnt-- > 0) {
      a.back *= 2;
    }
    sum += a.back;
    writeln(sum);
  }
}

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 !(long)).array;

		long p = 1;
		foreach (ref c; a)
		{
			while (c % 2 == 0)
			{
				c /= 2;
				p *= 2;
			}
		}

		sort (a);
		a.back *= p;
		writeln (a.sum);
	}
}


Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.   A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears. The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.

Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main ()
{
    int n;
    while (readf (" %s ", &n) > 0)
    {
        auto a = readln.split.map !(to !(int)).array;
        auto p = n.iota.array;
        auto prev = (n + 2).iota.array;
        auto next = prev.dup;
        --prev[];
        ++next[];
        p.sort !((x, y) => a[x] > a[y]);
        ++p[];
        auto ans = new int [n];
        int v = 0;
        foreach (x; p)
        {
            prev[next[x]] = prev[x];
            next[prev[x]] = next[x];
            while (v < next[x] - prev[x] - 1)
            {
                ans[v] = a[x - 1];
                v++;
            }
        }
        writefln ("%(%s %)", ans);
    }
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		auto p = n.iota.array;
		auto prev = (n + 2).iota.array;
		prev[] -= 1;
		auto next = (n + 2).iota.array;
		next[] += 1;
		sort !((x, y) => a[x] > a[y], SwapStrategy.stable) (p);
		p[] += 1;
		auto ans = new int [n];
		int v = 0;
		foreach (x; p)
		{
			prev[next[x]] = prev[x];
			next[prev[x]] = next[x];
			int u = next[x] - prev[x] - 1;
			while (v < u)
			{
				ans[v] = a[x - 1];
				v++;
			}
		}

		ans.map !(text).join (" ").writeln;
	}
}

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

enum maxN = 200_000;
int[maxN] parent = -1;
int[maxN] setSize = 0;
void makeSet(int i)
{
  assert(parent[i] == -1);
  parent[i] = i;
  setSize[i] = 1;
}
int findParent(int i)
{
  auto pi = parent[i];
  if (pi == -1 || pi == i) return pi;
  return parent[i] = findParent(pi);
}
int join(int i, int j)
{
  assert(findParent(i) != -1);
  assert(findParent(j) != -1);
  i = findParent(i);
  j = findParent(j);
  if (setSize[i] < setSize[j]) swap(i, j);
  parent[j] = i;
  setSize[i] += setSize[j];
  return i;
}

void main(string[] args)
{
  auto n = next!int;
  auto a = next!int(n);
  auto sortedIndices = iota(0, n).array.sort!((i, j) => a[i] > a[j]);
  auto sol = new int[](n + 1);
  int soli = 1;
  int mx = 1;
  foreach(i; sortedIndices)
    {
      makeSet(i);
      if (i + 1 < n && parent[i + 1] != -1)
	mx = max(setSize[join(i, i + 1)], mx);
      if (i - 1 >= 0 && parent[i - 1] != -1)
	mx = max(setSize[join(i, i - 1)], mx);
      for(; soli <= mx; soli++)
	sol[soli] = a[i];
    }
  println(sol[1 .. $]);
}

static this()
{
  popChar;
}

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

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

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

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

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

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

import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInt() {
	return readInts()[0];
}
auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}

void main() {
	auto n = readInt();
	auto a = readLongs();
	auto arev = a.dup;
	arev.reverse;
	a = -1 ~ a;
	arev = -1 ~ arev;
	int[] s = [0];
	auto t = new int[](n);
	auto u = new int[](n);
	foreach(i; 0..n) {
		while(!(a[s[$-1]] < a[i+1])) {
			s = s[0..$-1];
		}
		t[i] = i-s[$-1];
		s ~= i+1;
	}
	s = [0];
	foreach(i; 0..n) {
		while(!(arev[s[$-1]] < arev[i+1])) {
			s = s[0..$-1];
		}
		u[n-i-1] = i-s[$-1];
		s ~= i+1;
	}
	auto si = new long[](n);
	foreach(i; 0..n) {
		auto l = t[i]+u[i]+1;
		si[l-1] = max(si[l-1], a[i+1]);
	}
	foreach_reverse(i; 0..n-1) {
		si[i] = max(si[i+1], si[i]);
	}
	foreach(i; 0..n) {
		writeln(si[i]);
	}
}

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		auto p = n.iota.array;
		auto prev = (n + 2).iota.array;
		auto next = prev.dup;
		++prev[];
		--next[];
		p.sort !((x, y) => a[x] > a[y]);
		++p[];
		auto ans = new int [n];
		int v = 0;
		foreach (x; p)
		{
			prev[next[x]] = prev[x];
			next[prev[x]] = next[x];
			while (v < next[x] - prev[x] - 1)
			{
				ans[v] = a[x - 1];
				v++;
			}
		}
		writefln ("%(%s %)", ans);
	}
}


You are given a grid with $$$n$$$ rows and $$$m$$$ columns. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$. All numbers are equal to $$$1$$$ or to $$$-1$$$. You start from the square $$$(1, 1)$$$ and can move one square down or one square to the right at a time. In the end, you want to end up at the square $$$(n, m)$$$.Is it possible to move in such a way so that the sum of the values written in all the visited cells (including $$$a_{11}$$$ and $$$a_{nm}$$$) is $$$0$$$?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$a_{ij} = 1$$$ or $$$-1$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that the sum of $$$n\cdot m$$$ over all test cases does not exceed $$$10^6$$$.

For each test case, print "YES" if there exists a path from the top left to the bottom right that adds up to $$$0$$$, and "NO" otherwise. You can output each letter in any case.

NoteOne possible path for the fourth test case is given in the picture in the statement.

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

alias S = Tuple!(int, "y", int, "x", int, "c");

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto F = N.iota.map!(_ => readln.chomp.split(" ").map!(to!int).array).array;

    bool C() {
        if ( (N + M - 1) % 2 == 1 ) return false;
        auto f = new int[][][](2, N, M);
        foreach (i; 0 .. N) {
            f[0][i][] = int.min;
            f[1][i][] = int.max;
        }
        f[0][0][0] = F[0][0];
        f[1][0][0] = F[0][0];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (i + 1 < N) {
                    f[0][i+1][j] = max(f[0][i+1][j], f[0][i][j] + F[i+1][j]);
                    f[1][i+1][j] = min(f[0][i+1][j], f[1][i][j] + F[i+1][j]);
                }
                if (j + 1 < M) {
                    f[0][i][j+1] = max(f[0][i][j+1], f[0][i][j] + F[i][j+1]);
                    f[1][i][j+1] = min(f[1][i][j+1], f[1][i][j] + F[i][j+1]);
                }
            }
        }
        return f[1][N-1][M-1] <= 0 && 0 <= f[0][N-1][M-1];
    }

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

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        arr ~= scanArray;
    }
    long num = n + m - 1;
    if(num % 2){
        writeln("NO");
        return;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        dp[i][] = 0;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

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

alias S = Tuple!(int, "y", int, "x", int, "c");

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto F = N.iota.map!(_ => readln.chomp.split(" ").map!(to!int).array).array;

    bool C() {
        if ( (N + M - 1) % 2 == 1 ) return false;
        auto f = new int[][][](2, N, M);
        f[0][0][0] = F[0][0];
        f[1][0][0] = F[0][0];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (i + 1 < N) {
                    f[0][i+1][j] = max(f[0][i+1][j], f[0][i][j] + F[i+1][j]);
                    f[1][i+1][j] = min(f[0][i+1][j], f[1][i][j] + F[i+1][j]);
                }
                if (j + 1 < M) {
                    f[0][i][j+1] = max(f[0][i][j+1], f[0][i][j] + F[i][j+1]);
                    f[1][i][j+1] = min(f[1][i][j+1], f[1][i][j] + F[i][j+1]);
                }
            }
        }
        return f[0][N-1][M-1] <= 0 && 0 <= f[1][N-1][M-1];
    }

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

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        dp[i][] = 0;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long maxx = dp[n-1][m-1];
    /* for(int i = 0; i < n; ++i){ */
    /*     dp[i][] = 0; */
    /* } */
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long minn = dp[n-1][m-1];
    show(maxx, minn);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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


Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible. There are n towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads. Towns s and t announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from s to t using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two.Help them find set of no more than two roads such that there will be no way between s and t after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum.

The first line of the input contains two integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 30 000) — the number of towns in Berland and the number of roads. The second line contains integers s and t (1 ≤ s, t ≤ n, s ≠ t) — indices of towns which break up the relationships. Then follow m lines, each of them contains three integers xi, yi and wi (1 ≤ xi, yi ≤ n, 1 ≤ wi ≤ 109) — indices of towns connected by the i-th road, and the budget on its closure. All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed.

In the first line print the minimum budget required to break up the relations between s and t, if it is allowed to close no more than two roads. In the second line print the value c (0 ≤ c ≤ 2) — the number of roads to be closed in the found solution. In the third line print in any order c diverse integers from 1 to m — indices of closed roads. Consider that the roads are numbered from 1 to m in the order they appear in the input.  If it is impossible to make towns s and t disconnected by removing no more than 2 roads, the output should contain a single line -1.  If there are several possible answers, you may print any of them.

import std.stdio;
import std.algorithm;
import std.container.dlist;
import std.typecons;
import std.c.process;

alias Edge = Tuple!(int, "u", int, "id");
alias EInfo = Tuple!(int, "u", int, "v", int, "w");
alias ii = Tuple!(long, "w", int, "v");

immutable long inf = cast(long)1e18;

int N, M, S, T;
int[] d, low, f; int time;
Edge[][] G; EInfo[] E;
int[] path;
int choose = 0;

int chSum = cast(int)2e9 + 5; int[] chList;

bool contains(int u, int v) { return d[u] <= d[v] && f[v] <= f[u]; }

void bfs() {
	int[] pre = new int[N + 1];
	DList!int q; q.insertBack(S);
	while (!q.empty) {
		immutable int v = q.front(); q.removeFront();
		foreach (ed; G[v]) {
			if (!pre[ed.u]) {
				pre[ed.u] = ed.id;
				q.insertBack(ed.u);
			}
		}
	}
	if (!pre[T]) {
		writef("0\n0\n");
		exit(0);
	}
	int cur = T;
	while (cur != S) {
		path ~= pre[cur];
		if (E[pre[cur]].u == cur) cur = E[pre[cur]].v; else cur = E[pre[cur]].u;
	}
}

void dfs(int v, int par, int rem) {
	d[v] = low[v] = ++time;
	foreach (ed; G[v]) {
		int u = ed.u; if (ed.id == par || ed.id == rem) continue;
		if (!d[u]) {
			dfs(u, ed.id, rem);
			low[v] = min(low[v], low[u]);
		} else low[v] = min(low[v], d[u]);
		if (low[u] > d[v]) {
			// ed.id is bridge
			if (contains(u, S) ^ contains(u, T))
				if (!choose || E[choose].w > E[ed.id].w) choose = ed.id;
		}
	}
	f[v] = time;
}

void main() {
	readf("%d %d", &N, &M);
	readf(" %d %d", &S, &T);
	G = new Edge[][N + 1];
	E = new EInfo[M + 1];
	foreach (i; 1..M + 1) {
		readf(" %d %d %d", &E[i].u, &E[i].v, &E[i].w);
		G[E[i].u] ~= Edge(E[i].v, i);
		G[E[i].v] ~= Edge(E[i].u, i);
	}
	bfs();
	foreach (id; path) {
		d = new int[N + 1];
		low = new int[N + 1];
		f = new int[N + 1];
		choose = time = 0;
		dfs(S, 0, id);
		if (!d[T]) {
			if (chSum > E[id].w) {
				chSum = E[id].w; chList = [id];
			}
		}
		else if (!choose) {

		} else {
			if (chSum > E[id].w + E[choose].w) {
				chSum = E[id].w + E[choose].w; chList = [id, choose];
			}
		}
	}
	if (!chList.length) {
		writeln(-1); return;
	}
	writeln(chSum);
	writeln(chList.length);
	foreach(id; chList) writef("%d ", id); writeln();
}

import std.stdio;
import std.algorithm;
import std.container.dlist;
import std.typecons;
import std.c.process;

alias Edge = Tuple!(int, "u", int, "id");
alias EInfo = Tuple!(int, "u", int, "v", int, "w");
alias ii = Tuple!(long, "w", int, "v");

immutable long inf = cast(long)1e18;

int N, M, S, T;
int[] d, low, f; int time;
Edge[][] G; EInfo[] E;
int[] path;
int choose = 0;

int chSum = cast(int)2e9 + 5; int[] chList;

bool contains(int u, int v) { return d[u] <= d[v] && f[v] <= f[u]; }

void bfs() {
	int[] pre = new int[N + 1];
	DList!int q; q.insertBack(S);
	while (!q.empty) {
		immutable int v = q.front(); q.removeFront();
		foreach (ed; G[v]) {
			if (!pre[ed.u]) {
				pre[ed.u] = ed.id;
				q.insertBack(ed.u);
			}
		}
	}
	if (!pre[T]) {
		writeln(0);
		exit(0);
	}
	int cur = T;
	while (cur != S) {
		path ~= pre[cur];
		if (E[pre[cur]].u == cur) cur = E[pre[cur]].v; else cur = E[pre[cur]].u;
	}
}

void dfs(int v, int par, int rem) {
	d[v] = low[v] = ++time;
	foreach (ed; G[v]) {
		int u = ed.u; if (ed.id == par || ed.id == rem) continue;
		if (!d[u]) {
			dfs(u, ed.id, rem);
			low[v] = min(low[v], low[u]);
		} else low[v] = min(low[v], d[u]);
		if (low[u] > d[v]) {
			// ed.id is bridge
			if (contains(u, S) ^ contains(u, T))
				if (!choose || E[choose].w > E[ed.id].w) choose = ed.id;
		}
	}
	f[v] = time;
}

void main() {
	readf("%d %d", &N, &M);
	readf(" %d %d", &S, &T);
	G = new Edge[][N + 1];
	E = new EInfo[M + 1];
	foreach (i; 1..M + 1) {
		readf(" %d %d %d", &E[i].u, &E[i].v, &E[i].w);
		G[E[i].u] ~= Edge(E[i].v, i);
		G[E[i].v] ~= Edge(E[i].u, i);
	}
	bfs();
	foreach (id; path) {
		d = new int[N + 1];
		low = new int[N + 1];
		f = new int[N + 1];
		choose = time = 0;
		dfs(1, 0, id);
		if (!d[T]) {
			if (chSum > E[id].w) {
				chSum = E[id].w; chList = [id];
			}
		}
		else if (!choose) {

		} else {
			if (chSum > E[id].w + E[choose].w) {
				chSum = E[id].w + E[choose].w; chList = [id, choose];
			}
		}
	}
	if (!chList.length) {
		writeln(-1); return;
	}
	writeln(chSum);
	writeln(chList.length);
	foreach(id; chList) writef("%d ", id); writeln();
}

import std.stdio;
import std.algorithm;
import std.container.dlist;
import std.typecons;
import std.c.process;

alias Edge = Tuple!(int, "u", int, "id");
alias EInfo = Tuple!(int, "u", int, "v", int, "w");
alias ii = Tuple!(long, "w", int, "v");

immutable long inf = cast(long)1e18;

int N, M, S, T;
int[] d, low, f; int time;
Edge[][] G; EInfo[] E;
int[] path;
int choose = 0;

int chSum = cast(int)2e9 + 5; int[] chList;

bool contains(int u, int v) { return d[u] <= d[v] && f[v] <= f[u]; }

void bfs() {
	int[] pre = new int[N + 1];
	DList!int q; q.insertBack(S);
	while (!q.empty) {
		immutable int v = q.front(); q.removeFront();
		foreach (ed; G[v]) {
			if (!pre[ed.u]) {
				pre[ed.u] = ed.id;
				q.insertBack(ed.u);
			}
		}
	}
	if (!pre[T]) {
		writef("0\n0\n");
		exit(0);
	}
	int cur = T;
	while (cur != S) {
		path ~= pre[cur];
		if (E[pre[cur]].u == cur) cur = E[pre[cur]].v; else cur = E[pre[cur]].u;
	}
}

void dfs(int v, int par, int rem) {
	d[v] = low[v] = ++time;
	foreach (ed; G[v]) {
		int u = ed.u; if (ed.id == par || ed.id == rem) continue;
		if (!d[u]) {
			dfs(u, ed.id, rem);
			low[v] = min(low[v], low[u]);
		} else low[v] = min(low[v], d[u]);
		if (low[u] > d[v]) {
			// ed.id is bridge
			if (contains(u, S) ^ contains(u, T))
				if (!choose || E[choose].w > E[ed.id].w) choose = ed.id;
		}
	}
	f[v] = time;
}

void main() {
	readf("%d %d", &N, &M);
	readf(" %d %d", &S, &T);
	G = new Edge[][N + 1];
	E = new EInfo[M + 1];
	foreach (i; 1..M + 1) {
		readf(" %d %d %d", &E[i].u, &E[i].v, &E[i].w);
		G[E[i].u] ~= Edge(E[i].v, i);
		G[E[i].v] ~= Edge(E[i].u, i);
	}
	bfs();
	foreach (id; path) {
		d = new int[N + 1];
		low = new int[N + 1];
		f = new int[N + 1];
		choose = time = 0;
		dfs(1, 0, id);
		if (!d[T]) {
			if (chSum > E[id].w) {
				chSum = E[id].w; chList = [id];
			}
		}
		else if (!choose) {

		} else {
			if (chSum > E[id].w + E[choose].w) {
				chSum = E[id].w + E[choose].w; chList = [id, choose];
			}
		}
	}
	if (!chList.length) {
		writeln(-1); return;
	}
	writeln(chSum);
	writeln(chList.length);
	foreach(id; chList) writef("%d ", id); writeln();
}


Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types. The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

NoteIn the first sample case, we can achieve concentration  using one liter of Coke of types  and : .In the second case, we can achieve concentration  using two liters of  type and one liter of  type: .

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

immutable int half = 1 << 10;
immutable int limit = half << 1;

void main ()
{
	int n;
	int k;
multitest_loop:
	while (readf (" %s %s", &n, &k) > 0)
	{
		auto b = new bool [half];
		readln;
		foreach (c; readln.splitter.map !(to !(int)))
		{
			b[c] = true;
		}

		auto f = new uint [] [] (2, limit / 32);
		int d = 0;
		f[d][] = 0;
		f[d][half >> 5] |= 1U << (half & 31);
		foreach (res; 1..half + 1)
		{
			d ^= 1;
			f[d][] = 0;
			foreach (c; 0..half)
			{
				if (!b[c])
				{
					continue;
				}
				int s = c - n;
				int sLo = s & 31;
				int sHi = s >> 5;
				int sBack = 32 - sLo;
				uint loMask = (1U << sLo) - 1U;
				uint hiMask = ~loMask;
				int j = sHi;
				foreach (i; 0..limit / 32)
				{
//					f[d][i + s] |= f[!d][i];
					if (j >= 0)
					{
						f[d][j] |= f[!d][i] << sLo;
					}
					j += 1;
					if (j >= limit / 32)
					{
						break;
					}
					if (j >= 0 && sLo != 0)
					{
						f[d][j] |= f[!d][i] >> sBack;
					}
				}
			}

			if (f[d][half >> 5] & (1U << (half & 31)))
			{
				writeln (res);
				continue multitest_loop;
			}
		}
		writeln (-1);
	}
}

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

immutable int half = 1 << 10;
immutable int limit = half << 1;

void main ()
{
	int n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		auto b = new bool [half];
		readln;
		foreach (c; readln.splitter.map !(to !(int)))
		{
			b[c] = true;
		}

		auto f = new uint [] [] (2, limit / 32);
		int d = 0;
		f[d][] = 0;
		f[d][half >> 5] |= 1U << (half & 31);
		foreach (res; 1..half + 1)
		{
			d ^= 1;
			f[d][] = 0;
			foreach (c; 0..half)
			{
				if (!b[c])
				{
					continue;
				}
				int s = c - n;
				int sLo = s & 31;
				int sHi = s >> 5;
				int sBack = 32 - sLo;
				uint loMask = (1U << sLo) - 1U;
				uint hiMask = ~loMask;
				int j = sHi;
				foreach (i; 0..limit / 32)
				{
//					f[d][i + s] |= f[!d][i];
					if (j >= 0)
					{
						f[d][j] |= f[!d][i] << sLo;
					}
					j += 1;
					if (j >= limit / 32)
					{
						break;
					}
					if (j >= 0 && sLo != 0)
					{
						f[d][j] |= f[!d][i] >> sBack;
					}
				}
			}

			if (f[d][half >> 5] & (1U << (half & 31)))
			{
				writeln (res);
				break;
			}
		}
	}
}


Constanze is the smartest girl in her village but she has bad eyesight.One day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe "code" onto the paper. Thanks to this machine, she can finally write messages without using her glasses.However, her dumb friend Akko decided to play a prank on her. Akko tinkered with the machine so that if you pronounce 'w', it will inscribe "uu" instead of "w", and if you pronounce 'm', it will inscribe "nn" instead of "m"! Since Constanze had bad eyesight, she was not able to realize what Akko did.The rest of the letters behave the same as before: if you pronounce any letter besides 'w' and 'm', the machine will just inscribe it onto a piece of paper.The next day, I received a letter in my mailbox. I can't understand it so I think it's either just some gibberish from Akko, or Constanze made it using her machine. But since I know what Akko did, I can just list down all possible strings that Constanze's machine would have turned into the message I got and see if anything makes sense.But I need to know how much paper I will need, and that's why I'm asking you for help. Tell me the number of strings that Constanze's machine would've turned into the message I got.But since this number can be quite large, tell me instead its remainder when divided by $$$10^9+7$$$.If there are no strings that Constanze's machine would've turned into the message I got, then print $$$0$$$.

Input consists of a single line containing a string $$$s$$$ ($$$1 \leq |s| \leq 10^5$$$) — the received message. $$$s$$$ contains only lowercase Latin letters.

Print a single integer — the number of strings that Constanze's machine would've turned into the message $$$s$$$, modulo $$$10^9+7$$$.

NoteFor the first example, the candidate strings are the following: "ouuokarinn", "ouuokarim", "owokarim", and "owokarinn".For the second example, there is only one: "banana".For the third example, the candidate strings are the following: "nm", "mn" and "nnn".For the last example, there are no candidate strings that the machine can turn into "amanda", since the machine won't inscribe 'm'.

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;

    if (S.canFind('m') || S.canFind('w')) {
        writeln(0);
        return;
    }

    auto dp = new long[][](N+1, 2);
    dp[0][1] = 1;

    foreach (i; 0..N) {
        (dp[i+1][0] += dp[i][0] + dp[i][1]) %= MOD;
        (dp[i+1][1] += dp[i][0]) %= MOD;
    }

    long ans = 1;
    char prev = '*';
    int tmp = 0;

    foreach (i; 0..N) {
        if (S[i] == prev) {
            tmp += 1;
            continue;
        }
        if (prev == 'n' || prev == 'u') {
            ans = ans * (dp[tmp].sum % MOD) % MOD;
        }
        prev = S[i];
        tmp = 1;
    }

    if (S.back == 'n' || S.back == 'u') {
        ans = ans * (dp[tmp].sum % MOD) % MOD;
    }

    ans.writeln;
}

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

struct IntM(int q = 1_000_000_007) {
  alias N = IntM!q;
  private:
  int v;
  invariant () { assert (v >= 0 && v < q); }
  pure nothrow @nogc
  void fromInt (int m) {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  public:
  pure nothrow @nogc
  this (int m) {
    fromInt (m);
  }
  pure nothrow @nogc
  N opAssign (int m) {
    fromInt (m);
    return this;
  }
  pure nothrow @nogc
  N opUnary (string op : "-")() const {
    return N (-v);
  }
  pure nothrow @nogc
  ref N opUnary (string op : "++")() {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  pure nothrow @nogc
  ref N opUnary (string op : "--")() {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "+")(in N rhs) {
    v = (v + rhs.v) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "-")(in N rhs) {
    v = (v - rhs.v + q) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "*")(in N rhs) {
    v = (v.to!(long) * rhs.v) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "/")(in N rhs) {
    return this *= rhs.inversion ();
  }
  pure nothrow @nogc
  ref N opOpAssign (string op)(in int rhs) if (op == "+" || op == "-" || op == "*" || op == "/") {
    mixin ("return this " ~ op ~ "= N(rhs);");
  }
  pure nothrow @nogc
  N opBinary (string op)(in N rhs) const if (op == "+" || op == "-" || op == "*" || op == "/") {
    N t = this;
    mixin ("t " ~ op ~ "= rhs;");
    return t;
  }
  pure nothrow @nogc
  N opBinary (string op)(in int rhs) const if (op == "+" || op == "-" || op == "*" || op == "/") {
    mixin ("return this " ~ op ~ " N(rhs);");
  }
  pure nothrow @nogc
  N opBinaryRight (string op)(in int rhs) const if (op == "+" || op == "*") {
    mixin ("return this " ~ op ~ " N(rhs);");
  }
  pure nothrow @nogc
  int opCast(T : int)() const { return v; }
  pure nothrow @nogc
  int opCmp (const N rhs) const {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  pure nothrow
  string toString() const { return v.text; }
  //a ^ x = this (mod q)
}
alias N = IntM!1_000_000_007;

void main() {
  auto s = readln.strip;
  debug stderr.writeln (s);
  immutable n = s.length;
  debug stderr.writeln (n);
  auto d = new N[n+1];
  d[n] = N (1);
  bool sol = true;
  foreach_reverse (i; 0 .. n) {
    d[i] = d[i+1];
    if (i + 1 < n && s[i] == s[i+1] && (s[i] == 'u' || s[i] == 'n')) {
      d[i] += d[i + 2];
    }
    if (s[i] == 'm' || s[i] == 'w') {
      sol = false;
    }
  }
  debug stderr.writeln (d);
  if (!sol) {
    d[0] = N (0);
  }
  writeln (d[0]);
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto s = RD!string;
	long ans = 1;
	int cnt;
	char last = '?';
	auto fib = new long[](10^^5+1);
	fib[0] = 1;
	fib[1] = 1;
	foreach (i; 2..fib.length)
	{
		fib[i] = fib[i-1];
		fib[i].moda(fib[i-2]);
	}
	foreach (c; s)
	{
		if (c == 'w' || c == 'm')
		{
			ans = 0;
			break;
		}

		if (c == last)
			++cnt;
		else
		{
			ans.modm(fib[cnt]);
			last = '?';
			cnt = 0;
			if (c == 'u' || c == 'n')
			{
				last = c;
				++cnt;
			}
		}
	}
	if (cnt != 0)
	{
		ans.modm(fib[cnt]);
	}

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


Polycarp has a string $$$s$$$. Polycarp performs the following actions until the string $$$s$$$ is empty ($$$t$$$ is initially an empty string):  he adds to the right to the string $$$t$$$ the string $$$s$$$, i.e. he does $$$t = t + s$$$, where $$$t + s$$$ is a concatenation of the strings $$$t$$$ and $$$s$$$;  he selects an arbitrary letter of $$$s$$$ and removes from $$$s$$$ all its occurrences (the selected letter must occur in the string $$$s$$$ at the moment of performing this action). Polycarp performs this sequence of actions strictly in this order.Note that after Polycarp finishes the actions, the string $$$s$$$ will be empty and the string $$$t$$$ will be equal to some value (that is undefined and depends on the order of removing).E.g. consider $$$s$$$="abacaba" so the actions may be performed as follows:  $$$t$$$="abacaba", the letter 'b' is selected, then $$$s$$$="aacaa";  $$$t$$$="abacabaaacaa", the letter 'a' is selected, then $$$s$$$="c";  $$$t$$$="abacabaaacaac", the letter 'c' is selected, then $$$s$$$="" (the empty string). You need to restore the initial value of the string $$$s$$$ using only the final value of $$$t$$$ and find the order of removing letters from $$$s$$$.

The first line contains one integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Then $$$T$$$ test cases follow. Each test case contains one string $$$t$$$ consisting of lowercase letters of the Latin alphabet. The length of $$$t$$$ doesn't exceed $$$5 \cdot 10^5$$$. The sum of lengths of all strings $$$t$$$ in the test cases doesn't exceed $$$5 \cdot 10^5$$$.

For each test case output in a separate line:   $$$-1$$$, if the answer doesn't exist;  two strings separated by spaces. The first one must contain a possible initial value of $$$s$$$. The second one must contain a sequence of letters — it's in what order one needs to remove letters from $$$s$$$ to make the string $$$t$$$. E.g. if the string "bac" is outputted, then, first, all occurrences of the letter 'b' were deleted, then all occurrences of 'a', and then, finally, all occurrences of 'c'. If there are multiple solutions, print any one.

7
abacabaaacaac
nowyouknowthat
polycarppoycarppoyarppyarppyrpprppp
isi
everywherevrywhrvryhrvrhrvhv
haaha
qweqeewew

abacaba bac
-1
polycarp lcoayrp
is si
everywhere ewyrhv
-1
-1

NoteThe first test case is considered in the statement.

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

string validate(string s, string del)
{
  char[] t;
  bool[26] used;
  foreach (c ; del) {
    foreach (c2 ; s) {
      if (!used[c2 - 'a'])
        t ~= c2;
    }
    used[c - 'a'] = true;
  }
  return t.idup;
}

void main()
{
  long tt;
  readf!" %d "(tt);
  while (tt--) {
    auto t = readln.strip;
    int[] freq = new int[](26);
    int[] orig_freq = new int[](26);
    int[][] pref = new int[][](t.length, 26);
    foreach (i ; 0 .. t.length) {
      char c = t[i];
      freq[c - 'a']++;
      pref[i][c - 'a'] = freq[c - 'a'];
    }
    int steps = cast(int)freq.count!(x => x != 0);
    int j = cast(int)t.length - 1;
    bool good = true;
    char[] ans;
    bool[26] used;
    int ansidx =-1;
    while (steps) {
      char c;
      ansidx = j;
      while (j >= 0 && used[t[j] - 'a']) {
        j--;
      }
      if (j < 0) {
        good = false;
        break;
      }
      c = t[j];
      used[c - 'a'] = true;
      ans ~= c;
//      writefln("steps=%d, c=%c, j=%d, %s", steps, c, j, t[0 .. j + 1]);
      if (freq[c - 'a'] % steps != 0) {
        good = false;
        break;
      }
      int repeat = freq[c - 'a'] / steps;
      orig_freq[c - 'a'] = repeat;
//      writefln("repeat=%d", repeat);
      while (true) {
        if (j < 0) {
          good = false;
          break;
        }
        if (t[j] == c) {
          repeat--;
          j--;
          if (repeat == 0)
            break;
          continue;
        }
        if (t[j] != c && !used[c - 'a']) {
          good = false;
          break;
        } else {
          j--;
        }
      }
      if (!good)
        break;
      steps--;
    }
    if (!good)
      writeln(-1);
    else {
      int i = 0;
      while (true) {
        if (i >= t.length || orig_freq[t[i] - 'a'] == 0)
          break;
        orig_freq[t[i] - 'a']--;
        i++;
      }
      string orig_s = t[0 .. i];
      string delp = ans.dup.reverse.idup;
      if (validate(orig_s, delp) != t)
        writeln(-1);
      else
        writefln("%s %s", t[0 .. i], ans.reverse.idup);
    }
  }
}


Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.

The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages. The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message. The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message. It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.

Print the maximum number of files the intercepted array could consist of.

NoteIn the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.In the third example the only possibility is that the archive contains a single file of size 4.

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

void main()
{
	readln;

	auto a = readln.splitter.map!(to!uint).array;
	auto b = readln.splitter.map!(to!uint).array;

	uint cnt;

	while(a.length)
	{
		uint u = a[0], v = b[0];

		a.popFront;
		b.popFront;

		while(true)
		{
			if(u == v)
			{
				cnt++;
				break;
			}

			if(u > v)
			{
				v += b[0];
				b.popFront;
			}
			else
			{
				u += a[0];
				a.popFront;
			}
		}
	}

	cnt.writeln;
}

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

void Isle(T)( T küçükDizi, T büyükDizi, ref int kacKere )
{
	int hedef = büyükDizi.front();
	büyükDizi.popFront();
	int toplam = 0;
	while( true )
	{
		toplam += küçükDizi.front();
		if (toplam <= hedef )
			küçükDizi.popFront();

		if ( toplam >= hedef )
		{
			if ( küçükDizi.empty() || büyükDizi.empty() )
			{
				kacKere++;
				return;
			}
			else
			{
				if ( toplam > hedef)
					küçükDizi.front() = toplam - hedef;
				else
					kacKere++;

				if( küçükDizi.front > büyükDizi.front )
					Isle(büyükDizi, küçükDizi, kacKere);
				else
					Isle(küçükDizi, büyükDizi, kacKere);
			}
			return;
		}
	}
}

int main(string[] argv)
{
	auto girişDizisi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) );
	auto ilkDizi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) ).array;
	auto ikinciDizi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) ).array;
	int sonuç = 0;
	if( ilkDizi.front > ikinciDizi.front )
		Isle(ikinciDizi, ilkDizi, sonuç);
	else
		Isle(ilkDizi, ikinciDizi, sonuç);
	writeln(sonuç);
    return 0;
}

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

  int n, m; rd(n, m);
  auto x=readln.split.to!(long[]);
  auto y=readln.split.to!(long[]);

  int cnt=0;
  for(int i=0, j=0, sx=0, sy=0; i<n; i++){
    sx+=x[i];
    while(j<m && (sy+=y[j])<sx) j++;
    if(sy==sx) sx=sy=0, j++, cnt++;
    else sy-=y[j];
  }

  writeln(cnt);
}

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

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

void main()
{
	readln;

	auto a = readln.splitter.map!(to!uint).array;
	auto b = readln.splitter.map!(to!uint).array;

	uint cnt;

	while(a.length)
	{
		uint u = a[0], v = b[0];

		a.popFront;
		b.popFront;

		while(true)
		{
			a.writeln;
			b.writeln;

			if(u == v)
			{
				cnt++;
				break;
			}

			if(u > v)
			{
				v += b[0];
				b.popFront;
			}
			else
			{
				u += a[0];
				a.popFront;
			}
		}
	}

	cnt.writeln;
}


You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that:There are exactly $$$a$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} < p_i > p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums).There are exactly $$$b$$$ integers $$$i$$$ with $$$2 \le i \le n-1$$$ such that $$$p_{i-1} > p_i < p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums).If such permutations exist, find any such permutation.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq a,b \leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.

NoteIn the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 < p_2 > p_3$$$, and $$$2$$$ is the only such index, and $$$p_2> p_3 < p_4$$$, and $$$3$$$ the only such index.One can show that there is no such permutation for the third test case.

// Cheese-Cracker: cheese-cracker.github.io

void solve(){
    auto n = scan!int;
    auto arr = iota(1L, n+1L).array;
    long a = scan;
    long b = scan;
    if(abs(a - b) > 1 || a + b > n - 2){
        writeln(-1);
        return;
    }
    long swp = max(a, b);
    if(a > b){
        arr.reverse;
    }
    for(int i = 0; i+1 < n; i += 2){
        if(!swp){
            break;
        }
        swap(arr[i], arr[i+1]);
        --swp;
    }
    long ac = 0, bc = 0;
    for(int i = 1; i < n-1; ++i){
        if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
            ++ac;
        }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
            ++bc;
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
        return;
    }else if (ac == a || bc == b){
        swap(arr[n-1], arr[n-2]);
        ac = 0; bc = 0;
        for(int i = 1; i < n-1; ++i){
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                ++ac;
            }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
                ++bc;
            }
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
    }else{
        show(arr);
        writeln(-1);
    }
}

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

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

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

int [] solve (int n, int a, int b)
{
	if (abs (a - b) > 1)
	{
		return [-1];
	}
	if (a + b > n - 2)
	{
		return [-1];
	}
	if (a > b)
	{
		return solve (n, b, a).map !(x => n + 1 - x).array;
	}
	auto res = new int [n];
	int lo = 1;
	int hi = n;
	int pos = 0;
	if (b > a)
	{
		res[pos++] = hi--;
		res[pos++] = lo++;
		b -= 1;
	}
	else
	{
		res[pos++] = lo++;
	}
	while (a > 0 && b > 0)
	{
		res[pos++] = hi--;
		a -= 1;
		res[pos++] = lo++;
		b -= 1;
	}
	while (pos < n)
	{
		res[pos++] = lo++;
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, a, b;
		readf !(" %s %s %s") (n, a, b);
		writefln !("%(%s %)") (solve (n, a, b));
	}
}

// Cheese-Cracker: cheese-cracker.github.io

void solve(){
    auto n = scan!int;
    auto arr = iota(1L, n+1L).array;
    long a = scan;
    long b = scan;
    if(abs(a - b) > 1){
        writeln(-1);
        return;
    }
    int st = (a > b);
    long swp = max(a, b);
    for(int i = st; i+1 < n; i += 2){
        if(!swp){
            break;
        }
        swap(arr[i], arr[i+1]);
        --swp;
    }
    if(swp > 0){
        writeln(-1);
        return;
    }
    long ac = 0, bc = 0;
    for(int i = 1; i < n-1; ++i){
        if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
            ++ac;
        }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
            ++bc;
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
        return;
    }else if (ac == a || bc == b){
        swap(arr[n-1], arr[n-2]);
        ac = 0; bc = 0;
        for(int i = 1; i < n-1; ++i){
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                ++ac;
            }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
                ++bc;
            }
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
    }else{
        writeln(-1);
    }
}

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

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