void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto A = scan!int(N);

      auto st = SegTree!("a + b", int)(new int[](N + 1));
      long ans;
      foreach(i, a; A) {
        ans += st.sum(a, N + 1);

        auto x = st.get(a);
        st.update(a, x + 1);
      }

      return ans;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

struct SegTree(alias pred = "a + b", T = long) {
  alias predFun = binaryFun!pred;
  int size;
  T[] data;
  T monoid;

  this(T[] src, T monoid = T.init) {
    this.monoid = monoid;

    for(int i = 2; i < 2L^^32; i *= 2) {
      if (src.length <= i) {
        size = i;
        break;
      }
    }

    data = new T[](size * 2);
    foreach(i, s; src) data[i + size] = s;
    foreach_reverse(b; 1..size) {
      data[b] = predFun(data[b * 2], data[b * 2 + 1]);
    }
  }

  void update(int index, T value) {
    int i = index + size;
    data[i] = value;
    while(i > 0) {
      i /= 2;
      data[i] = predFun(data[i * 2], data[i * 2 + 1]);
    }
  }

  T get(int index) {
    return data[index + size];
  }

  T sum(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = size;

    if (r <= a || b <= l) return monoid;
    if (a <= l && r <= b) return data[k];

    T leftValue = sum(a, b, 2*k, l, (l + r) / 2);
    T rightValue = sum(a, b, 2*k + 1, (l + r) / 2, r);
    return predFun(leftValue, rightValue);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		a[] -= 1;
		auto b = new int [] [n];
		foreach_reverse (i; 0..n)
		{
			b[a[i]] ~= i;
		}

		auto t = new int [n + 1];

		void add (int i)
		{
			for ( ; i <= n; i += i & -i)
			{
				t[i] += 1;
			}
		}

		int ask (int i)
		{
			int res = 0;
			for ( ; i > 0; i -= i & -i)
			{
				res += t[i];
			}
			return res;
		}

		long res = 0;
		foreach (i; 0..n)
		{
			foreach (v; b[i])
			{
				res += ask (n - v);
				add (n - v);
			}
		}
		writeln (res);
	}
}

void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto A = scan!int(N);

      auto st = SegTree!("a + b", int)(new int[](N + 1));
      int ans;
      foreach(i, a; A) {
        ans += st.sum(a, N + 1);

        auto x = st.get(a);
        st.update(a, x + 1);
      }

      return ans;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

struct SegTree(alias pred = "a + b", T = long) {
  alias predFun = binaryFun!pred;
  int size;
  T[] data;
  T monoid;

  this(T[] src, T monoid = T.init) {
    this.monoid = monoid;

    for(int i = 2; i < 2L^^32; i *= 2) {
      if (src.length <= i) {
        size = i;
        break;
      }
    }

    data = new T[](size * 2);
    foreach(i, s; src) data[i + size] = s;
    foreach_reverse(b; 1..size) {
      data[b] = predFun(data[b * 2], data[b * 2 + 1]);
    }
  }

  void update(int index, T value) {
    int i = index + size;
    data[i] = value;
    while(i > 0) {
      i /= 2;
      data[i] = predFun(data[i * 2], data[i * 2 + 1]);
    }
  }

  T get(int index) {
    return data[index + size];
  }

  T sum(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = size;

    if (r <= a || b <= l) return monoid;
    if (a <= l && r <= b) return data[k];

    T leftValue = sum(a, b, 2*k, l, (l + r) / 2);
    T rightValue = sum(a, b, 2*k + 1, (l + r) / 2, r);
    return predFun(leftValue, rightValue);
  }
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto x = RD;
		auto y = RD;
		auto a = RD;
		auto b = RD;
		ans[ti] = abs(x-y) * a + min(abs(x), abs(y)) * b;
		ans[ti].chmin(x*a + y*a);
	}

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

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm, std.array, std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;

alias ll = long;
int inz(long t){ return to!int(t); }
static string[] tempstr;
T rd(T = long)() { while(!tempstr.length) tempstr = readln.chomp.split; string res = tempstr[0]; tempstr.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void play(){
    ll x, y, a, b;
    x = rd; y = rd;
    a = rd; b = rd;
    ll cost = a * (max(x, y) - min(x, y));
    cost += min(2*a*min(x, y), b*min(x, y));
    writeln(cost);
}

int main(){
    ll t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto xy = readln.split.to!(long[]);
        auto x = xy[0];
        auto y = xy[1];
        auto ab = readln.split.to!(long[]);
        auto a = ab[0];
        auto b = ab[1];

        writeln(min(
            x * a + y * a,
            min(x, y) * b + abs(x - y) * a
        ));
    }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int x, y;
		readf !(" %s %s") (x, y);
		int a, b;
		readf !(" %s %s") (a, b);
		b = min (b, a * 2);
		if (x > y)
		{
			swap (x, y);
		}
		writeln (x * 1L * b + (y - x) * 1L * a);
	}
}

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

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    int x = r.next!int;
    int y = r.next!int;
    const long a = r.next!int;
    const long b = r.next!int;
    long res = (x + y) * a;
    if (x > y) swap (x, y);
    res = min (res, b * x + (y - x) * a);
    writeln (res);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto x = RD;
		auto y = RD;
		auto a = RD;
		auto b = RD;
		ans[ti] = abs(x-y) * a + min(x, y) * b;
	}

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


import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

/* template start */
class EOFException : Throwable { this() { super("EOF"); } }

string[] tokens;
string readToken() {
    for (; tokens.empty; ) {
        tokens = readln.split;
        if (stdin.eof) throw new EOFException;
    }
    auto token = tokens[0];
    tokens.popFront;
    return token;
}
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }
/* template end */

void main(string[] args) {
    try {
        for(; ; ) {
            long n = readLong;
            long sum = 0;
            foreach (i; 0 .. n) sum += readLong;
            long maxone = long.min, maxtwo = long.min;
            foreach (i; 0 .. n) {
                long cur = readLong;
                if (cur > maxone) {
                    maxtwo = maxone;
                    maxone = cur;
                } else if (cur > maxtwo) {
                    maxtwo = cur;
                }
            }
            if (maxone + maxtwo - sum < 0) writeln("NO");
            else writeln("YES");
        }
    } catch (EOFException) {}
}

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

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

// Min/Max
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

/* End of Template */

int n;
long total;
long maxone, maxtwo;

void main(string[] args) {
    try {
        for(; ; ) {
            n = readInt;
            maxone = long.min;
            maxtwo = long.min;
            total = 0;
            foreach (i; 0 .. n) total += readLong;
            foreach (i; 0 .. n) {
                long cur = readLong;
                if (cur > maxone) {
                    maxtwo = maxone;
                    maxone = cur;
                } else if (cur > maxtwo) {
                    maxtwo = cur;
                }
            }
            if (maxone + maxtwo - total < 0) writeln("NO");
            else writeln("YES");
        }
    } catch (EOFException) {}
}

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

long n;

void main() {
  n = readln.chomp.to!long;
  auto a = readln.chomp.split.map!(to!long).array;
  auto b = readln.chomp.split.map!(to!long).array;

  sort!("a < b")(b);

  if (b[$-1] + b[$-2] >= reduce!("a + b")(0L, a)) "YES".writeln;
  else "NO".writeln;
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

/* template start */
class EOFException : Throwable { this() { super("EOF"); } }

string[] tokens;
string readToken() {
    for (; tokens.empty; ) {
        tokens = readln.split;
        if (stdin.eof) throw new EOFException;
    }
    auto token = tokens[0];
    tokens.popFront;
    return token;
}
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }
/* template end */

void main(string[] args) {
    try {
        for(; ; ) {
            int n = readInt;
            int sum = 0;
            foreach (i; 0 .. n) sum += readInt;
            int maxone = int.min, maxtwo = int.min;
            foreach (i; 0 .. n) {
                int cur = readInt;
                if (cur > maxone) {
                    maxtwo = maxone;
                    maxone = cur;
                } else if (cur > maxtwo) {
                    maxtwo = cur;
                }
            }
            if (maxone + maxtwo - sum < 0) writeln("NO");
            else writeln("YES");
        }
    } catch (EOFException) {}
}

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

int n;

void main() {
  n = readln.chomp.to!int;
  auto a = readln.chomp.split.map!(to!int).array;
  auto b = readln.chomp.split.map!(to!int).array;

  sort!("a < b")(b);

  if (b[$-1] + b[$-2] >= reduce!("a + b")(0, a)) "YES".writeln;
  else "NO".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;

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!uint ();
    auto a = r.nextA!int (n);
    //j - a[j] != i - a[i]
    //j - i != a[j] - a[i]
    sort (a);
    reverse (a);
    writefln ("%(%s %)", a);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int;
		a.sort!"a > b"();
		ans[ti] = a;
	}

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

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

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

void solve(){
    foreach(_; 0 .. scan!int){
        int n = scan!int;
        long[] as = scan!long(n);
        as.sort!"a>b"();
        as.unsplit.print;
    }
}


import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.math, std.range;

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

//	chmin/chmax
void chmin(T)(ref T t, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N, M;
int[][] G;
int[] X;

int[] par;
void dfs(int u, int p) {
	par[u] = p;
	foreach (v; G[u]) if (v != p) {
		if (par[v] == -2) {
			dfs(v, u);
		}
	}
}

int[] ans;
int[] cnt;
void solve(int u, int p) {
	ans ~= u;
	cnt[u] ^= 1;
	foreach (v; G[u]) if (u == par[v]) {
		solve(v, u);
		ans ~= u;
		cnt[u] ^= 1;
	}
	if (cnt[u] != X[u]) {
		if (p != -1) {
			ans ~= p;
			cnt[p] ^= 1;
			ans ~= u;
			cnt[u] ^= 1;
		} else {
			assert(ans[$ - 1] == u);
			ans.popBack;
			cnt[u] ^= 1;
		}
	}
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		G = new int[][N];
		foreach (i; 0 .. M) {
			const u = readInt - 1;
			const v = readInt - 1;
			G[u] ~= v;
			G[v] ~= u;
		}
		X = new int[N];
		foreach (u; 0 .. N) {
			X[u] = readInt;
		}

		int src = -1;
		foreach (u; 0 .. N) {
			if (X[u]) {
				src = u;
				break;
			}
		}
		if (src == -1) {
			writeln("0");
			continue;
		}

		par = new int[N];
		par[] = -2;
		dfs(src, -1);

		bool ok = true;
		foreach (u; 0 .. N) {
			if (X[u]) {
				if (par[u] == -2) {
					ok = false;
					break;
				}
			}
		}
		if (!ok) {
			writeln("-1");
			continue;
		}

		ans = new int[0];
		cnt = new int[N];
		solve(src, -1);
		writeln(ans.length);
		foreach (j, u; ans) {
			if (j > 0) write(" ");
			write(u + 1);
		}
		writeln;

	}
	} catch (EOFException) {}
}

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

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

		auto x = new int [n];
		foreach (ref y; x)
		{
			readf (" %s", &y);
		}

		auto b = new bool [n];
		int [] ans;

		void recur (int v)
		{
			if (b[v])
			{
				return;
			}
			b[v] = true;

			foreach (u; a[v])
			{
				if (!b[u])
				{
					ans ~= u;
					x[u] ^= 1;
					recur (u);
					ans ~= v;
					x[v] ^= 1;

					if (x[u])
					{
						ans ~= u;
						x[u] ^= 1;
						ans ~= v;
						x[v] ^= 1;
					}
				}
			}
		}

		foreach (i; 0..n)
		{
			if (x[i])
			{
				ans ~= i;
				x[i] ^= 1;
				recur (i);

				if (x[i])
				{
					assert (!a[i].empty);
					ans ~= a[i][0];
					x[a[i][0]] ^= 1;
					ans ~= i;
					x[i] ^= 1;
					ans ~= a[i][0];
					x[a[i][0]] ^= 1;
				}

				foreach (y; x)
				{
					if (y)
					{
						writeln (-1);
						continue test_loop;
					}
				}

				ans[] += 1;
				writeln (ans.length);
				writefln ("%(%s %)", ans);
				continue test_loop;
			}
		}
		writeln (0);
	}
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.math, std.range;

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

//	chmin/chmax
void chmin(T)(ref T t, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N, M;
int[][] G;
int[] X;

int[] par;
void dfs(int u, int p) {
	par[u] = p;
	foreach (v; G[u]) if (v != p) {
		if (par[v] == -2) {
			dfs(v, u);
		}
	}
}

int[] ans;
int[] cnt;
void solve(int u, int p) {
	ans ~= u;
	cnt[u] ^= 1;
	foreach (v; G[u]) if (u == par[v]) {
		solve(v, u);
		ans ~= u;
		cnt[u] ^= 1;
	}
	if (cnt[u] != X[u]) {
		if (p != -1) {
			ans ~= p;
			cnt[p] ^= 1;
			ans ~= u;
			cnt[u] ^= 1;
		} else {
			assert(ans[$ - 1] == u);
			ans.popBack;
			cnt[u] ^= 1;
		}
	}
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		G = new int[][N];
		foreach (i; 0 .. M) {
			const u = readInt - 1;
			const v = readInt - 1;
			G[u] ~= v;
			G[v] ~= u;
		}
		X = new int[N];
		foreach (u; 0 .. N) {
			X[u] = readInt;
		}

		int src = -1;
		foreach (u; 0 .. N) {
			if (X[u]) {
				src = u;
				break;
			}
		}
		if (src == -1) {
			writeln("0");
			continue;
		}

		par = new int[N];
		par[] = -2;
		dfs(src, -1);
		foreach (u; 0 .. N) {
			if (X[u]) {
				if (par[u] == -2) {
					writeln("-1");
					break;
				}
			}
		}

		ans = new int[0];
		cnt = new int[N];
		solve(src, -1);
		writeln(ans.length);
		foreach (j, u; ans) {
			if (j > 0) write(" ");
			write(u + 1);
		}
		writeln;

	}
	} catch (EOFException) {}
}


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

immutable int maxValue = 30;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		int res = 0;
		for (int value = 0; value <= +maxValue; value++)
		{
			int cur = 0;
			int lo = cur;
			foreach (const ref c; a)
			{
				if (c > value)
				{
					cur = 0;
					lo = cur;
				}
				else
				{
					cur += c;
					lo = min (lo, cur);
					res = max (res, cur - lo - value);
				}
			}
		}

		writeln (res);
	}
}


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

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

    auto seq = new int[] (n);

    foreach (ref e; seq) {
        writeln(1);
        stdout.flush();

        readf("%s", &e);
        readln;

        if (e == 0) {
            return;
        }
    }

    int ans = 0, le = 1, r = m, i = 0;
    do {
        int mid = (le + r) / 2;
        writeln(mid);
        stdout.flush();

        readf("%s", &ans);
        readln;

        if (ans == 0) {
            return;
        }

        if (ans * seq[i] == 1) le = mid + 1;
        else r = mid - 1;

        i = (i+1) % n;

    } while (ans != 0);
}

import std.range, std.stdio;
int f (int x) {
	x.writeln;
	stdout.flush;
	int r;
	readf (" %s", &r);
	return r;
}
void main () {
	int m, n;
	readf (" %s %s", &m, &n);
	auto p = new int [n];
	int k = 1;
	foreach (i; 0..99) {
		int v, x = (k + m) / 2;
		if (i < n) {
			v = f (1);
			p[i] = v;
		} else {
			v = f (x) * p[i % n];
			if (v < 0) m = x - 1;
			else k = x + 1;
		}
		if (!v) return;
	}
}

import std.stdio;
int f (int x) {
	x.writeln;
	stdout.flush;
	int r;
	readf (" %s", r);
	return r;
}
void main () {
	int m, n, k, i, v = 1, x;
	readf (" %s %s", m, n);
	auto p = new int [n];
	for (; v; i++)
		if (i < n) p[i] = v = f (1);
		else {
			x = (k + m + 1) / 2;
			v = f (x) * p[i % n];
			if (v < 0) m = x;
			else k = x;
		}
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto M = s[0].to!long;
    auto N = s[1];
    auto P = new int[](N);
    long x;
    int[] Q;
    int debug_cnt = 0;

    debug {
        x = readln.chomp.to!long;
        Q = N.iota.map!(_ => readln.chomp.to!int).array;
    }

    int ask(long y) {
        writeln(y);
        stdout.flush;
        debug {
            if (x < y) return -1 * Q[debug_cnt];
            else if (x > y) return 1 * Q[debug_cnt];
            else return 0;
            ++debug_cnt;
        }
        return readln.chomp.to!int;
    }

    foreach (i; 0..N) {
        P[i] = ask(1);
        if (P[i] == 0)
            return;
    }

    long hi = M+1;
    long lo = 0;
    int cnt = 0;

    while (hi - lo > 1) {
        long mid = (hi + lo) / 2;
        int ret = ask(mid) * P[cnt%N];
        if (ret == 1) {
            lo = mid;
        } else if (ret == -1) {
            hi = mid;
        } else {
            return;
        }
        ++cnt;
    }
}

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

int ask (int x)
{
	writeln (x);
	stdout.flush ();
	int res;
	readf (" %s", &res);
	return res;
}

void solve (int m, int n)
{
	auto p = new int [n];
	foreach (ref c; p)
	{
		auto cur = ask (1);
		if (cur == 0)
		{
			return;
		}
		c = (cur == +1);
	}

	int lo = 1;
	int hi = m;
	for (int step = 0; ; step++)
	{
		int me = (lo + hi) / 2;
		auto cur = ask (me);
		if (cur == 0)
		{
			return;
		}
		cur *= (p[step % n] ? +1 : -1);
		if (cur < 0)
		{
			hi = me - 1;
		}
		else
		{
			lo = me + 1;
		}
	}
}

void main ()
{
	int m, n;
	while (readf (" %s %s", &m, &n) > 0)
	{
		solve (m, n);
	}
}


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)
	{
		long n, k;
		readf !(" %s %s") (n, k);
		k = max (k, (n + k - 1) / k * k);
		writeln ((k + n - 1) / n);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (n >= k)
		{
			auto rem = k - (n % k);
			if (rem == k)
				ans[ti] = 1;
			else
				ans[ti] = 2;
		}
		else
		{
			ans[ti] = (k+n-1) / n;
		}
	}

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

module main;
import std.stdio;

auto read(T)()
{
    T value;
    readf!" %s"(value);
    return value;
}

void main()
{
    int t = read!int();
    foreach (_; 0 .. t)
    {
        int n = read!int(), k = read!int();
        writeln(n < k ? (k + n - 1) / n : n % k ? 2 : 1);
    }
}

module main;
import std.stdio;

auto readInt()
{
    int value;
    readf!" %d"(value);
    return value;
}

void main()
{
    int t = readInt();
    foreach (_; 0 .. t)
    {
        int n = readInt(), k = readInt();
        writeln(n < k ? (k + n - 1) / n : n % k ? 2 : 1);
    }
}

import std.stdio;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        long n, k;
        readf!"%d %d\n"(n, k);
        k *= (n + k - 1) / k;
        writeln((k + n - 1) / n);
    }
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, K; get(N, K);
        writeln(N % K == 0 ? 1 : N > K ? 2 : (K + N - 1) / N);
    }
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, K; get(N, K);
        writeln((K == 1 || N == K) ? 1 : N > K ? 2 : (K + N - 1) / N);
    }
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, K; get(N, K);
        writeln(N == K ? 1 : N > K ? 2 : (K + N - 1) / N);
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		k = max (k, (n + k - 1) / k * k);
		writeln ((k + n - 1) / n);
	}
}


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

immutable long INF = 1L << 59;

long f(long n) {
    return n * (n + 1) / 2;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto N = readln.chomp.to!int;
        auto A = readln.split.map!(to!int).array;
        int[int] cnt1, cnt2;
        foreach (a; A) cnt1[a] += 1;
        foreach (a; cnt1.values) cnt2[a] += 1;
        auto B = cnt2.keys.array;
        B.sort!"a > b";
        long ans = 0;
        long lo = INF;
        foreach (b; B) {
            long v = min(b.to!long, lo);
            long n = min(cnt2[b].to!long, v);
            ans += f(v) - f(v - n);
            lo = v - n;
        }
        ans.writeln;
    }
}

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

  int q;
  rd(q);

  while (q--) {
    int n;
    rd(n);
    auto a = readln.split.to!(int[]);
    auto freq = new int[](n);
    foreach (el; a) {
      freq[el - 1] += 1;
    }
    int[] b;
    foreach (f; freq) {
      if (f > 0) {
        b ~= f;
      }
    }
    b.sort!"a>b";
    int last = b[0] + 1;
    long ans = 0;
    foreach (el; b) {
      ans += min(last - 1, el);
      last = min(last - 1, el);
      if (last == 0) {
        break;
      }
    }
    writeln(ans);
  }

}

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

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


import std.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)); }

alias Entry = Tuple!(long, "val", long, "num");

Entry[] solve(long[] A, long M) {
  const N = cast(int)(A.length);
  auto es = new Entry[N];
  foreach (i; 0 .. N) {
    auto e = Entry(A[i], 1);
    for (; e.val % M == 0; ) {
      e.val /= M;
      e.num *= M;
    }
    es[i] = e;
  }
  Entry[] ans;
  for (int i = 0, j; i < N; i = j) {
    long sum;
    for (j = i; j < N && es[i].val == es[j].val; ++j) {
      sum += es[j].num;
    }
    ans ~= Entry(es[i].val, sum);
  }
  debug {
    writeln(ans);
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const ALen = readInt;
        const M = readLong;
        auto A = new long[ALen];
        foreach (i; 0 .. ALen) {
          A[i] = readLong;
        }
        const BLen = readInt;
        auto B = new long[BLen];
        foreach (i; 0 .. BLen) {
          B[i] = readLong;
        }

        const resA = solve(A, M);
        const resB = solve(B, M);
        writeln((resA == resB) ? "Yes" : "No");
      }
    }
  } catch (EOFException e) {
  }
}

import std;

void main(){
	int t=readln.strip.to!int;
	foreach(_;0..t){
		auto m=readln.strip.split.to!(int[])[1];
		auto a=readln.strip.split.to!(int[]);
		readln();
		auto b=readln.strip.split.to!(int[]);
		auto rle(int[] x){
			auto canonicalize(int value){
				long repetitions=1;
				while(value%m==0){
					value/=m;
					repetitions*=m;
				}
				return tuple(value,repetitions);
			}
			return x.map!canonicalize.chunkBy!(a=>a[0]).map!(x=>tuple(x[0],x[1].map!(y=>y[1]).sum));
		}
		writeln(equal(rle(a),rle(b))?"Yes":"No");
	}
}

import std;

void main(){
	int t=readln.strip.to!int;
	foreach(_;0..t){
		auto m=readln.strip.split.to!(int[])[1];
		auto a=readln.strip.split.to!(int[]);
		readln();
		auto b=readln.strip.split.to!(int[]);
		auto rle(int[] x){
			auto canonicalize(int value){
				long repetitions=1;
				while(value%m==0){
					value/=m;
					repetitions*=m;
				}
				return q(value,repetitions);
			}
			return x.map!canonicalize.chunkBy!(a=>a[0]).map!(x=>q(x[0],x[1].map!(y=>y[1]).sum));
		}
		writeln(equal(rle(a),rle(b))?"Yes":"No");
	}
}

auto q(T...)(T args){
	struct Q{
		T expand;
		alias expand this;
		string toString(){
			auto r="(";
			foreach(i,alias x;expand)
				r~=(i!=0?",":"")~text(x);
			r~=")";
			return r;
		}
	}
	return Q(args);
}

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

void solve()
{
    ulong n, m;
    auto cmp = (ulong x) {
        auto count = 0;
        while (x % m == 0)
        {
            x /= m;
            count++;
        }
        return tuple(x, m^^count);
    };
    readf!(" %s %s")(n, m);
    readln;
    auto A = readln.splitter
        .map!(to!ulong)
        .map!(cmp)
        .chunkBy!(x => x[0])
        .map!(x => tuple(x[0], x[1].map!(y => y[1]).sum()));
    readln;
    auto B = readln.splitter
        .map!(to!ulong)
        .map!(cmp)
        .chunkBy!(x => x[0])
        .map!(x => tuple(x[0], x[1].map!(y => y[1]).sum()));
    writeln(A.equal(B) ? "Yes" : "No");
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

import std;

void main(){
	int t=readln.strip.to!int;
	foreach(_;0..t){
		auto m=readln.strip.split.to!(int[])[1];
		auto a=readln.strip.split.to!(int[]);
		readln();
		auto b=readln.strip.split.to!(int[]);
		auto rle(int[] x){
			auto canonicalize(int value){
				int repetitions=1;
				while(value%m==0){
					value/=m;
					repetitions*=m;
				}
				return tuple(value,repetitions);
			}
			return x.map!canonicalize.chunkBy!(a=>a[0]).map!(x=>tuple(x[0],x[1].map!(y=>y[1]).sum));
		}
		writeln(equal(rle(a),rle(b))?"Yes":"No");
	}
}


// Vicfred
// https://codeforces.com/contest/1512/problem/D
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

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

while(t--) {
    long n = readln.chomp.to!long;
    long[] b = readln.split.map!(to!long).array;

    b.sort;

    long maxima = b[$ - 1];
    long cum = sum(b[0..$ - 1]);

    long x = -1L;
    for(uint i = 0L; i < n + 1L; ++i) {
        if(cum - b[i] == maxima) {
            x = b[i];
            b[i] = -1;
            break;
        }
    }

    if(x != -1L) {
        for(uint i = 0; i < n + 1L; ++i) {
            if(b[i] == -1L) {
                continue;
            }
            writef("%s ", b[i]);
        }
        "".writeln;
        continue;
    }

    maxima = b[$ - 2];
    cum = sum(b[0..$ - 2]);
    if(cum == maxima) {
        for(uint i = 0; i < n; i++) {
            writef("%s ", b[i]);
        }
        "".writeln;
    } else {
        "-1".writeln;
    }
}
}

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

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

  while(t--) {
    long n = readln.chomp.to!long;
    long[] b = readln.split.map!(to!long).array;

    b.sort;

    long maxima = b[$-1];
    long cum = sum(b[0 .. $-1]);

    int x = -1;
    for(int i = 0; i < n + 1; ++i) {
      if(cum - b[i] == maxima) {
        x = i;
        break;
      }
    }

    if(x != -1) {
      b = b.remove(x);
      writef("%(%d %)\n", b[0 .. $-1]);
      continue;
    }

    cum = sum(b[0 .. $-2]);
    if(cum == b[$-2])
      writef("%(%d %)\n", b[0 .. $-2]);
    else
      writeln("-1");
  }
}

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

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

  while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$-1];
    int cum = sum(b[0 .. $-1]);

    int x = -1;
    for(int i = 0; i < n + 1; ++i) {
      if(cum - b[i] == maxima) {
        x = i;
        break;
      }
    }

    if(x != -1) {
      b = b.remove(x);
      writef("%(%d %)\n", b[0 .. $-1]);
      continue;
    }

    cum = sum(b[0 .. $-2]);
    if(cum == b[$-2])
      writef("%(%d %)\n", b[0 .. $-2]);
    else
      writeln("-1");
  }
}

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

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

  while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$-1];
    int cum = sum(b[0 .. $-1]);

    int x = -1;
    for(int i = 0; i < n - 1; ++i) {
      if(cum - b[i] == maxima) {
        x = i;
        break;
      }
    }

    if(x != -1) {
      b = b.remove(x);
      writef("%(%d %)\n", b[0 .. $-1]);
      continue;
    }

    cum = sum(b[0 .. $-2]);
    if(cum == b[$-2])
      writef("%(%d %)\n", b[0 .. $-2]);
    else
      writeln("-1");
  }
}

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

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

  while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$-1];
    int cum = sum(b[0 .. $-1]);

    int x = -1;
    for(int i = 0; i < n - 1; ++i) {
      if(cum - b[i] == maxima) {
        x = i;
        break;
      }
    }

    if(x != -1) {
      b = b.remove(x);
      writef("%(%d %)\n", b[0 .. $-1]);
      continue;
    }

    cum = sum(b[0 .. $-2]);
    if(cum + b[$-1] == b[$-2])
      writef("%(%d %)\n", b[0 .. $-2]);
    else
      writeln("-1");
  }
}

// Vicfred
// https://codeforces.com/contest/1512/problem/D
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

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

while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$ - 1];
    int cum = sum(b[0..$ - 1]);

    int x = -1;
    for(int i = 0; i < n + 1; ++i) {
        if(cum - b[i] == maxima) {
            x = b[i];
            b[i] = -1;
            break;
        }
    }

    if(x != -1) {
        for(int i = 0; i < n + 1; ++i) {
            if(b[i] == -1) {
                continue;
            }
            writef("%s ", b[i]);
        }
        "".writeln;
        continue;
    }

    maxima = b[$ - 2];
    cum = sum(b[0..$ - 2]);
    if(cum == maxima) {
        for(int i = 0; i < n; i++) {
            writef("%s ", b[i]);
        }
        "".writeln;
    } else {
        "-1".writeln;
    }
}
}

// Vicfred
// https://codeforces.com/contest/1512/problem/D
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

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

while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$ - 1];
    int cum = sum(b[0..$ - 1]);

    int x = -1;
    for(int i = 0; i < n - 1; ++i) {
        if(cum - b[i] == maxima) {
            x = b[i];
            b[i] = -1;
            break;
        }
    }

    if(x != -1) {
        for(int i = 0; i < n + 1; ++i) {
            if(b[i] == -1) {
                continue;
            }
            writef("%s ", b[i]);
        }
        "".writeln;
        continue;
    }

    maxima = b[$ - 2];
    cum = sum(b[0..$ - 2]);
    if(cum == maxima) {
        for(int i = 0; i < n; i++) {
            writef("%s ", b[i]);
        }
        "".writeln;
    } else {
        "-1".writeln;
    }
}
}

// Vicfred
// https://codeforces.com/contest/1512/problem/D
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

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

while(t--) {
    int n = readln.chomp.to!int;
    int[] b = readln.split.map!(to!int).array;

    b.sort;

    int maxima = b[$ - 1];
    int cum = sum(b[0..$ - 1]);

    int x = -1;
    for(int i = 0; i < n - 1; ++i) {
        if(cum - b[i] == maxima) {
            x = b[i];
            b[i] = -1;
            break;
        }
    }

    if(x != -1) {
        for(int i = 0; i < n + 1; ++i) {
            if(b[i] == -1) {
                continue;
            }
            writef("%s ", b[i]);
        }
        "".writeln;
        continue;
    }

    maxima = b[$ - 2];
    cum = sum(b[0..$ - 2]);
    if(cum == maxima) {
        b[0.. $ - 2].writeln;
    } else {
        "-1".writeln;
    }
}
}


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

unittest{
  assert(solve(1) == [1], "Teste 1");
}

long[] solve(long a){
  debug(1) writefln("%s",a);
  return [1];
}

class Candy{
  struct CandyV{
    int id;
    int t;
    int h;
    int m;
    bool ate;
  }
  CandyV cv;

  this(int i, int t, int h, int m, bool ate){
    cv = CandyV(i,t,h,m,ate);
  }

  bool canEat(int j, int t){
    return (cv.t == t && cv.ate == false && cv.h <= j);
  }

  override string toString(){
    return text(cv);
  }
}

void main(){
  int c,j;
  readf("%s %s\n",&c,&j);
  immutable int fj = j;

  Candy[] cnds;
  for(int i = 0; i < c; i++){
    int t,h,m;
    readf("%s %s %s\n",&t,&h,&m);
    Candy cc = new Candy(i,t,h,m,false);
    cnds ~= cc;
  }

  Candy[] byMaxM = cnds.dup;
  sort!((a,b){return a.cv.m > b.cv.m;})(byMaxM);
  //writeln(byMaxM);

  //Candy[] byMinH = cnds.dup;
  //sort!((a,b){return a.cv.h < b.cv.h;})(byMinH);
  //writeln(byMinH);

  //start with 1

  //start with 0
  int type = 0;
  int cnt0 = 0;
  for(int i = 0; i < c; i++){
    if(byMaxM[i].canEat(j,type % 2)){
      //writeln(j);
      j += byMaxM[i].cv.m;
      byMaxM[i].cv.ate = true;
      type += 1;
      cnt0++;
      i = -1;
    }
  }

  foreach(ccc;cnds){
    ccc.cv.ate = false;
  }
  j = fj;
  //writeln();

  type = 1;
  int cnt1 = 0;
  for(int i = 0; i < c; i++){
    if(byMaxM[i].canEat(j,type % 2)){
      //writeln(j);
      j += byMaxM[i].cv.m;
      byMaxM[i].cv.ate = true;
      type += 1;
      cnt1++;
      i = -1;
    }
  }

  // writeln(cnt0);
  // writeln(cnt1);
  writeln(max(cnt0,cnt1));
}

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;

immutable int NA = -1;

void main ()
{
	int n, x;
	while (readf (" %s %s", &n, &x) > 0)
	{
		alias pair = Tuple !(int, "h", int, "m");
		pair [] [2] a;
		foreach (i; 0..n)
		{
			int t, h, m;
			readf (" %s %s %s", &t, &h, &m);
			a[t] ~= pair (h, m);
		}
		sort (a[0]);
		sort (a[1]);

		long res = 0;
		foreach (s; 0..2)
		{
			pair [] [2] b;
			b[0] = a[0].dup;
			b[1] = a[1].dup;
			long cur = 0;
			int cx = x;
			int t = s;
			int j;
			int m;
			while (true)
			{
				m = NA;
				j = NA;
				foreach (i, p; b[t])
				{
					if (p != pair (NA, NA) &&
					    p.h <= cx &&
					    m < p.m)
					{
						m = p.m;
						j = i;
					}
				}
				if (j == NA)
				{
					break;
				}
				cx += b[t][j].m;
				b[t][j] = pair (NA, NA);
				cur++;
				t ^= 1;
			}
			res = max (res, cur);
		}

		writeln (res);
	}
}

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, x;
	while (readf (" %s %s", &n, &x) > 0)
	{
		alias pair = Tuple !(int, "h", int, "m");
		pair [] [2] a;
		foreach (i; 0..n)
		{
			int t, h, m;
			readf (" %s %s %s", &t, &h, &m);
			a[t] ~= pair (h, m);
		}
		sort (a[0]);
		sort (a[1]);

		long res = 0;
		foreach (s; 0..2)
		{
			long cur = 0;
			int cx = x;
			int p = 0;
			int t = s;
			while (true)
			{
				if (a[t].length <= p || a[t][p].h > cx)
				{
					break;
				}
				cx += a[t][p].m;
				cur++;
				t ^= 1;
				if (a[t].length <= p || a[t][p].h > cx)
				{
					break;
				}
				cx += a[t][p].m;
				cur++;
				t ^= 1;
				p++;
			}
			res = max (res, cur);
		}

		writeln (res);
	}
}

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

unittest{
  assert(solve(1) == [1], "Teste 1");
}

long[] solve(long a){
  debug(1) writefln("%s",a);
  return [1];
}

class Candy{
  struct CandyV{
    int id;
    int t;
    int h;
    int m;
    bool ate;
  }
  CandyV cv;

  this(int i, int t, int h, int m, bool ate){
    cv = CandyV(i,t,h,m,ate);
  }

  bool canEat(int j, int t){
    return (cv.t == t && cv.ate == false && cv.h <= j);
  }

  override string toString(){
    return text(cv);
  }
}

void main(){
  int c,j;
  readf("%s %s\n",&c,&j);

  Candy[] cnds;
  for(int i = 0; i < c; i++){
    int t,h,m;
    readf("%s %s %s\n",&t,&h,&m);
    Candy cc = new Candy(i,t,h,m,false);
    cnds ~= cc;
  }

  Candy[] byMaxM = cnds.dup;
  sort!((a,b){return a.cv.m > b.cv.m;})(byMaxM);
  //writeln(byMaxM);

  Candy[] byMinH = cnds.dup;
  sort!((a,b){return a.cv.h < b.cv.h;})(byMinH);
  //writeln(byMinH);

  //start with 1

  //start with 0
  int type = 0;
  int cnt0 = 0;
  for(int i = 0; i < c; i++){
    if(cnds[i].canEat(j,type % 2)){
      j += cnds[i].cv.m;
      type += 1;
      cnt0++;
    }
  }

  foreach(ccc;cnds){
    ccc.cv.ate = false;
  }

  type = 1;
  int cnt1 = 0;
  for(int i = 0; i < c; i++){
    if(cnds[i].canEat(j,type % 2)){
      j += cnds[i].cv.m;
      type += 1;
      cnt1++;
    }
  }

  writeln(max(cnt0,cnt1));
}

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

unittest{
  assert(solve(1) == [1], "Teste 1");
}

long[] solve(long a){
  debug(1) writefln("%s",a);
  return [1];
}

class Candy{
  struct CandyV{
    int id;
    int t;
    int h;
    int m;
    bool ate;
  }
  CandyV cv;

  this(int i, int t, int h, int m, bool ate){
    cv = CandyV(i,t,h,m,ate);
  }

  bool canEat(int j, int t){
    return (cv.t == t && cv.ate == false && cv.h <= j);
  }

  override string toString(){
    return text(cv);
  }
}

void main(){
  int c,j;
  readf("%s %s\n",&c,&j);
  immutable int fj = j;

  Candy[] cnds;
  for(int i = 0; i < c; i++){
    int t,h,m;
    readf("%s %s %s\n",&t,&h,&m);
    Candy cc = new Candy(i,t,h,m,false);
    cnds ~= cc;
  }

  Candy[] byMaxM = cnds.dup;
  sort!((a,b){return a.cv.m > b.cv.m;})(byMaxM);
  //writeln(byMaxM);

  //Candy[] byMinH = cnds.dup;
  //sort!((a,b){return a.cv.h < b.cv.h;})(byMinH);
  //writeln(byMinH);

  //start with 1

  //start with 0
  int type = 0;
  int cnt0 = 0;
  for(int i = 0; i < c; i++){
    if(byMaxM[i].canEat(j,type % 2)){
      //writeln(j);
      j += byMaxM[i].cv.m;
      byMaxM[i].cv.ate = true;
      type += 1;
      cnt0++;
      i = 0;
    }
  }

  foreach(ccc;cnds){
    ccc.cv.ate = false;
  }
  j = fj;
  //writeln();

  type = 1;
  int cnt1 = 0;
  for(int i = 0; i < c; i++){
    if(byMaxM[i].canEat(j,type % 2)){
      //writeln(j);
      j += byMaxM[i].cv.m;
      byMaxM[i].cv.ate = true;
      type += 1;
      cnt1++;
      i = 0;
    }
  }

  // writeln(cnt0);
  // writeln(cnt1);
  writeln(max(cnt0,cnt1));
}


import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.numeric;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto n = readln().chomp.to!int;
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        auto m = arr.minElement;
        double avg = arr.mean;
        int res = 0;
        foreach(i; arr)
        {
            if(i > m)
            {
                res++;
            }
        }

        writeln(res);
    }
}

// Vicfred
// https://codeforces.com/contest/1529/problem/A
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

while(t--) {
    int n;
    readf("%s\n", &n);
    int[] a = readln.split.map!(to!int).array;
    int minima = a.minElement;
    int ans = 0;
    foreach(i; a) {
        if(i != minima) {
            ans += 1;
        }
    }
    ans.writeln;
}
}

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto ar = readln.split.map!(to!int).array.sort.group;
        writeln(n-ar.array[0][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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

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

		auto x = a.minElement;
		foreach (i; 0..n)
		{
			if (a[i] > x)
				++ans[ti];
		}
	}

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.numeric;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto n = readln().chomp.to!int;
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;
        auto m = arr.minElement;

        writeln(arr.filter!(i => i > m).count());
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.numeric;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto n = readln().chomp.to!int;
        auto arr = readln().chomp.splitter(" ").map!(to!int).array;

        double avg = arr.mean;
        int res = 0;
        foreach(i; arr)
        {
            if(i > avg)
            {
                res++;
            }
        }

        writeln(res);
    }
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;
import std.numeric;

void main()
{
    auto t = to!long(readln().chomp);
    OUTER: foreach(_; 0..t)
    {
        auto n = readln().chomp.to!int;
        auto arr = readln().chomp.splitter(" ").map!(to!int).array.sort.array;

        int res = 0;
        int cur = 0;

        foreach(i; arr)
        {
            if(i != cur)
            {
                cur = i;
                res++;
            }
        }

        if(res > 1)
            writeln(res);
        else writeln(0);
    }
}


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

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

struct CI { int c, i; }

void main()
{
  int n, x1, x2; readV(n, x1, x2);
  int[] c; readA(n, c);

  auto ci = new CI[](n);
  foreach (i; 0..n) ci[i] = CI(c[i], i+1);
  auto cis = ci.sort!"a.c < b.c";

  auto r1 = calc(n, x1, x2, cis);
  if (r1[0] > 0) {
    put(r1[0], r1[1], r1[2], r1[3]);
    return;
  }

  auto r2 = calc(n, x2, x1, cis);
  if (r2[0] > 0) {
    put(r2[1], r2[0], r2[3], r2[2]);
    return;
  }

  writeln("No");
}

auto calc(R)(int n, int x1, int x2, R cis)
{
  auto calcK2()
  {
    foreach (k2; 1..n) {
      if (cis[$-k2].c >= (x2+k2-1)/k2) return k2;
    }
    return 0;
  }

  auto k2 = calcK2;
  if (k2 == 0) return tuple(0, 0, cast(int[])[], cast(int[])[]);

  foreach (k1; 1..n) {
    auto r = cis.upperBound(CI((x1+k1-1)/k1-1, 0));
    if (r.length >= k1+k2) {
      auto s = r.map!"a.i".array;
      return tuple(k1, k2, s[0..k1], s[$-k2..$]);
    }
  }

  return tuple(0, 0, cast(int[])[], cast(int[])[]);
}

auto put(int k1, int k2, int[] r1, int[] r2)
{
  writeln("Yes");
  writeln(k1, " ", k2);

  foreach (i; 0..k1) {
    write(r1[i]);
    if (i < k1-1) write(" ");
  }
  writeln;

  foreach (i; 0..k2) {
    write(r2[i]);
    if (i < k2-1) write(" ");
  }
}

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

void main()
{
    int n;
    int[2] x;
    readf("%s %s %s", &n, &x[0], &x[1]);
    readln;

    auto arr = readln.chomp.split.map!(to!int).enumerate(1).array;
    arr.sort!"a[1] > b[1]";

    debug { arr.writeln; }

    auto ans = new int[][] (2);

    bool go(int idx, int x1, int x2) {
        int need = (x[x1] + arr[idx].value - 1) / arr[idx].value;

        int lft = idx - need;
        if (lft < 0) { return false; }

        long totalLeft = arr[lft].value.to!long * (lft + 1);

        if (totalLeft < x[x2]) { return false; }

        foreach (i; 0 .. lft+1) { ans[x2] ~= arr[i].index; }
        foreach (i; lft+1 .. idx+1) { ans[x1] ~= arr[i].index; }

        return true;
    }

    bool found = false;
    foreach (i; 1 .. n) {
        found = go(i, 0, 1) || go(i, 1, 0);
        if (found) { break; }
    }

    if (!found) {
        writeln("No");
        return;
    }

    writeln("Yes");
    writeln(ans[0].length, ' ', ans[1].length);
    foreach (i; 0 .. 2) {
        ans[i].writefln!"%(%s %)";
    }
}


import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }

enum K = 5;

int N;
real[] X, Y;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      X = new real[N];
      Y = new real[N];
      foreach (i; 0 .. N) {
        X[i] = readReal();
        Y[i] = readReal();
      }

      Tuple!(real, int, int)[] edges;
      foreach (i; 0 .. N) foreach (j; 0 .. N) {
        if (i != j) {
          edges ~= tuple(atan2(Y[j] - Y[i], X[j] - X[i]), i, j);
        }
      }
      edges.sort;

      auto dp = new long[][][](K + 1, N, N);
      foreach (i; 0 .. N) {
        dp[0][i][i] = 1;
      }
      foreach (const ref edge; edges) {
        const j = edge[1], k = edge[2];
        foreach (t; 0 .. K) {
          foreach (i; 0 .. N) {
            dp[t + 1][i][k] += dp[t][i][j];
          }
        }
      }
      long ans;
      foreach (i; 0 .. N) {
        ans += dp[K][i][i];
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

/*
	Xmas Contest 2012
	Problem G: Galaxy View
	Solution
	O(N^3 log N)
*/

import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.bigint, std.math, std.range;
import core.thread;

//	Input
string[] tokens;
int tokenId = 0;
string readToken() { for (; tokenId == tokens.length; ) tokens = readln.split, tokenId = 0; return tokens[tokenId++]; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, T f) { if (t > f) t = f; }
void chmax(T)(ref T t, T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) {
	int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp;
}
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a){ return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a){ return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs == null || bs[$ - 1] != a) bs ~= a; return bs; }

void prepare() {

}

class Solver : Thread {
	int caseId;
	this(int caseId) {
		this.caseId = caseId;
		super(&main);
	}
	void main() {
		try {
			run;
			stderr.writeln("DONE  Case #", caseId);
		} catch (Throwable e) {
			stderr.writeln("ERROR Case #", caseId, ": ", e.msg);
		} finally {
			stderr.flush;
		}
	}

	struct Pt {
		long x, y;
	}
	struct Entry {
		long x, y;
		int id;
	}

	int N;
	Pt[] P;
	long ans;

	void run() {
		long cnt1, cnt2;
		P.sort!"(a.x != b.x) ? (a.x < b.x) : (a.y < b.y)";
		foreach (i; 0 .. N) {
			Pt[] ps;
			foreach (j; i + 1 .. N) {
				ps ~= Pt(P[j].x - P[i].x, P[j].y - P[i].y);
			}
			ps.sort!"(a.x * b.y > a.y * b.x)";
			foreach (j; 0 .. ps.length) {
				Entry[] es;
				foreach (k; j + 1 .. ps.length) {
					es ~= Entry(ps[k].x - ps[j].x, ps[k].y - ps[j].y, k - j - 1);
				}
				es.sort!"(a.x * b.y < a.y * b.x)";
				int[] as = new int[es.length];
				foreach (k, e; es) {
					as[e.id] = k;
				}
				int[] bit = new int[es.length];
				void add(int pos) {
					for (int x = pos; x < es.length; x |= x + 1) {
						++bit[x];
					}
				}
				int cal(int pos) {
					int ret;
					for (int x = pos - 1; x >= 0; x = (x & x + 1) - 1) {
						ret += bit[x];
					}
					return ret;
				}
				foreach (a; as) {
					long m = cal(a);
					cnt1 += m;
					cnt2 += m * (m - 1) / 2;
					add(a);
				}
			}
		}
		long n = N;
		ans = n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120 - (cnt1 * (n - 4) - cnt2 * 4) / 2 - cnt2;
	}

	void input() {
		N = readInt;
		P = new Pt[N];
		foreach (i; 0 .. N) {
			P[i].x = readLong;
			P[i].y = readLong;
		}
	}

	void output() {
		write(ans);
		stdout.rawWrite("\n");
		stdout.flush;
	}
}

void main(string[] args) {
	bool parallel = false;
	foreach (arg; args) if (arg == "-p") parallel = true;
	prepare;
	// auto solvers = new Solver[readInt];
	auto solvers = new Solver[1];
	foreach (caseId, ref solver; solvers) solver = new Solver(caseId + 1);
	if (parallel) {
		foreach (solver; solvers) solver.input, solver.start;
		foreach (solver; solvers) solver.join, solver.output;
	} else {
		foreach (caseId, solver; solvers) {
			solver.input;
			solver.run;
			solver.output;
			stderr.writeln("DONE  Case #", caseId + 1);
			stderr.flush;
		}
	}
}

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }

enum K = 5;

int N;
real[] X, Y;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      X = new real[N];
      Y = new real[N];
      foreach (i; 0 .. N) {
        X[i] = readReal();
        Y[i] = readReal();
      }

      Tuple!(real, int, int)[] edges;
      foreach (i; 0 .. N) foreach (j; 0 .. N) {
        if (i != j) {
          edges ~= tuple(atan2(Y[j] - Y[i], X[j] - X[i]), i, j);
        }
      }
      edges.sort;

      auto dp = new long[][][](K + 1, N, N);
      foreach (i; 0 .. N) {
        dp[0][i][i] = 1;
      }
      foreach (edge; edges) {
        foreach (k; 0 .. K) {
          foreach (i; 0 .. N) {
            dp[k + 1][i][edge[2]] += dp[k][i][edge[1]];
          }
        }
      }
      long ans;
      foreach (i; 0 .. N) {
        ans += dp[K][i][i];
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

  int n;
  rd(n);
  auto a = readln.split.to!(int[]);
  auto g = new int[][](n);
  foreach (_; 1 .. n) {
    int u, v;
    rd(u, v);
    g[u - 1] ~= v - 1;
    g[v - 1] ~= u - 1;
  }
  auto num_r = new int[](n), num_b = new int[](n);
  void f(int i, int p) {
    if (a[i] == 1) {
      num_r[i] = 1;
    } else if (a[i] == 2) {
      num_b[i] = 1;
    }
    foreach (j; g[i]) {
      if (j != p) {
        f(j, i);
        num_r[i] += num_r[j];
        num_b[i] += num_b[j];
      }
    }
  }

  f(0, -1);
  int bad = 0;
  void f2(int i, int p) {
    foreach (j; g[i]) {
      if (j != p) {
        if (num_r[j] > 0 && num_b[j]) {
          bad++;
        } else if (num_r[0] - num_r[j] > 0 && num_b[0] - num_b[j] > 0) {
          bad++;
        }
        f2(j, i);
      }
    }
  }

  f2(0, -1);
  writeln(n - 1 - bad);
}

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

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

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

immutable int INF = 10L ^^ 9 + 23;

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

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

    auto g = new int[][] (n);
    foreach (_; 0 .. n-1) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;
        g[u] ~= v;
        g[v] ~= u;
    }

    int[2] tot;
    foreach (i; 0 .. 2) { tot[i] = c.count!(x => x == i+1).to!int; }

    Tuple!(int, int) dfs(int v, int fr, ref int ans) {
        auto cnt = tuple(0, 0);
        foreach (u; g[v]) {
            if (u == fr) { continue; }

            auto res = dfs(u, v, ans);
            cnt[0] += res[0];
            cnt[1] += res[1];
        }

        cnt[0] += c[v] == 1;
        cnt[1] += c[v] == 2;

        if (v != 0) {
            if (cnt[0] == 0 && cnt[1] == tot[1]) { ans += 1; }
            else if (cnt[0] == tot[0] && cnt[1] == 0) { ans += 1; }
        }

        debug { writeln(v, ' ', cnt, ' ', ans); }

        return cnt;
    }

    int ans = 0;
    dfs(0, -1, ans);

    ans.writeln;
}


import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

/*
If you find s_i > s_(i + 1), swap them.
Othewise, NO.
*/

void solve(){
	int n = read!int;
	string s = readln.chomp;

	int i1;
	bool f = 0;
	foreach(i; 0 .. n - 1){
		if(s[i] > s[i + 1]){
			i1 = i;
			f = 1;
			break;
		}
	}
	if(f == 0) writeln("NO");
	else writeln("YES"), writeln(i1 + 1, " ", i1 + 2);
}

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD!int;
	auto s = RD!string;

	long[2] ans;
	foreach (i; 1..N)
	{
		if (s[i-1] > s[i])
		{
			ans = [i-1, i];
			break;
		}
	}

	if (ans == [0, 0])
		writeln("NO");
	else
	{
		writeln("YES");
		writeln(ans[0]+1, " ", ans[1]+1);
	}
	stdout.flush();
	debug readln();
}

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

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

    foreach (i; 0..N-1) {
        if (S[i] > S[i+1]) {
            writeln("YES");
            writeln(i+1, " ", i+2);
            return;
        }
    }

    writeln("NO");
}

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

void main()
{
	int elemanSayısı = stdin.readln.strip.to!int;
	string harfler = stdin.readln.strip.to!string;

	int enBüyükIndeksi = 0;
	char enBüyükHarf = 'a';
	for ( int i = 0; i < harfler.length; i++ )
	{
		if ( harfler[i] >= enBüyükHarf )
		{
			enBüyükIndeksi = i;
			enBüyükHarf = harfler[i];
		}
		else
		{
			writeln("YES");
			return writeln( enBüyükIndeksi+1, " ", i+1 );
		}

	}
	writeln("NO");

}

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

void main() {
  auto n = readln.strip.to!int;
  auto s = readln.strip[0 .. n];
  foreach (i; 1 .. n) {
    if (s[i-1] > s[i]) {
      debug stderr.writeln (i);
      writeln ("YES");
      writeln (i, ' ', i + 1);
      return;
    }
  }
  writeln ("NO");
}

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

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

    foreach (i; 0..N-1) {
        if (S[i] > S[i-1]) {
            writeln("YES");
            writeln(i+1, " ", i+2);
            return;
        }
    }

    writeln("NO");
}


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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    int ret;
    auto n = readNum!int;
    auto a = readNums!int;

    auto csum = new int[](n+1);
    foreach(j; 1 .. n+1){
      csum[j] = csum[j-1] + a[j-1];
    }

    foreach(j; 0 .. n){
      int l = 0, r = 2;

      while(r <= n){
        int sum = csum[r] - csum[l];
        if(sum < a[j] || l == r-1){
          r++;
        } else if(sum > a[j]){
          l++;
        } else {
          ret++;
          break;
        }
      }
    }

    writeln(ret);
  }
}

import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.string;
import std.array;
import core.stdc.stdlib;
import core.stdc.stdio;
import core.stdc.string;

void main() @nogc {
	// int tt = 1000;
	int tt;
	scanf("%d", &tt);
	foreach (t; 0 .. tt) {
		// int n = 8000;
		int n;
		scanf("%d", &n);
		int[] arr = (cast(int*)malloc(int.sizeof * n))[0 .. n];
		foreach (i; 0 .. arr.length) {
			scanf("%d", &arr[i]);
			// arr[i] = rand() % n + 1;
		}
		bool[] specials = (cast(bool*)malloc(bool.sizeof * (n + 2)))[0 .. n + 2];
		memset(specials.ptr, 0, bool.sizeof * (n + 2));
		foreach (i; 0 .. arr.length) {
			int sum = arr[i];
			foreach (j; i + 1 .. arr.length) {
				sum += arr[j];
				if (sum < n + 2) {
					specials[sum] = true;
				}
				else {
					break;
				}
			}
		}
		int res;
		foreach (i; arr) {
			if (specials[i]) {
				res++;
			}
		}
		printf("%d\n", res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

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

		auto set = new int[](n+1);
		foreach (i; 0..n)
		{
			++set[a[i]];
		}
		debug writeln(set);

		auto hit = new bool[](n+1);
		foreach (l; 0..n)
		{
			foreach (r; l+2..n+1)
			{
				auto x = b[r] - b[l];
				if (x > n) continue;
				hit[x] = true;
			}
		}

		foreach (i; 0..n+1)
		{
			if (hit[i])
			{
				ans[ti] += set[i];
			}
		}
	}

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

void main() {
	auto t = ri;
	foreach(T; 0..t) {
		auto n = ri;
		auto a = readAs!(int[]);
		auto arr = new int[](n);
		arr[0] = a[0];
		foreach(i; 1..n) arr[i] = arr[i-1] + a[i];
		int[int] ms, me;

		foreach(v; a) me[v]++;
		foreach(i; 1..n) ms[arr[i]]++;
		foreach(i; 0..n) foreach(j; i..n) {
			if(j - i < 2) continue;
			auto s = arr[j] - arr[i];
			debug writefln("%d, %d => %d", j, i, s);
			if(s <= n) ms[s]++;
		}
		long res;
		debug "=====".writeln;
		debug me.writeln;
		debug ms.writeln;
		foreach(i, v; me) {
			if(i in ms) {
				res += v;
			}
		}
		res.writeln;
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

long rl() {
	return readAs!long;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        if (N == 1) {
            writeln(0);
            continue;
        }
        int r;
        foreach (a; as) {
            int s = as[0] + as[1];
            size_t i, j = 2;
            while (i < N-1) {
                if (s == a) {
                    goto ok;
                } else if (s < a && j < N) {
                    s += as[j++];
                } else if (i < j-2) {
                    s -= as[i++];
                } else if (j < N) {
                    s += as[j++];
                } else {
                    break;
                }
            }
            continue;
            ok:
            ++r;
        }
        writeln(r);
    }
}

// Author: Ivan Kazmenko (gassa@mail.ru)
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto marks = new bool [n + 1];
		foreach (i; 0..n)
		{
			int cur = a[i];
			foreach (j; i + 1..n)
			{
				cur += a[j];
				if (cur > n)
				{
					break;
				}
				marks[cur] = true;
			}
		}
		writeln (a.count !(x => marks[x]));
	}
}

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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    int ret;
    auto n = readNum!int;
    auto a = readNums!int;

    auto csum = new int[](n+1);
    foreach(j; 1 .. n+1){
      csum[j] = csum[j-1] + a[j-1];
    }

    foreach(j; 1 .. n){
      int l = 0, r = 2;

      while(r <= n){
        int sum = csum[r] - csum[l];
        if(sum < a[j] || l == r-1){
          r++;
        } else if(sum > a[j]){
          l++;
        } else {
          ret++;
          break;
        }
      }
    }

    writeln(ret);
  }
}