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 rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	int a = rint, b = rint, c = rint, d = rint;
	int[] ans;
	if(a == 0 && b == 0){
		if(c + 1 == d){
			ans ~= 3;
			foreach(i; 0 .. c) ans ~= 2, ans ~= 3;
		}
		else if(c == d){
			foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
		}
		else if(c == d + 1){
			foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
			ans ~= 2;
		}
	}
	else if(c == 0 && d == 0){
		if(a + 1 == b){
			ans ~= 1;
			foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		}
		else if(a == b){
			foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		}
		else if(a == b + 1){
			foreach(i; 0 .. b) ans ~= 0, ans ~= 1;
			ans ~= 0;
		}
	}
	else if(a == 0 && d == 0){
		if(b + 1 == c){
			foreach(i; 0 .. b) ans ~= 2, ans ~= 1;
			ans ~= 2;
		}
		else if(b == c){
			foreach(i; 0 .. b) ans ~= 2, ans ~= 1;
		}
		else if(b == c + 1){
			ans ~= 1;
			foreach(i; 0 .. c) ans ~= 2, ans ~= 1;
		}
	}
	else if(a > b){
	}
	else if(c < d){
	}
	else if(b - a == c - d){
		foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		foreach(i; 0 .. b - a) ans ~= 2, ans ~= 1;
		foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
	}
	else if(b - a - 1 == c - d){
		ans ~= 1;
		foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		foreach(i; 0 .. b - a - 1) ans ~= 2, ans ~= 1;
		foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
	}
	else if(b - a == c - d - 1){
		foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		foreach(i; 0 .. b - a) ans ~= 2, ans ~= 1;
		foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
		ans ~= 2;
	}
	else if(b - a - 1 == c - d - 1){
		ans ~= 1;
		foreach(i; 0 .. a) ans ~= 0, ans ~= 1;
		foreach(i; 0 .. b - a - 1) ans ~= 2, ans ~= 1;
		foreach(i; 0 .. d) ans ~= 2, ans ~= 3;
		ans ~= 2;
	}

	if(ans.length == 0) "NO".writeln;
	else{
		"YES".writeln;
		ans.map!(to!string).array.join(" ").writeln;
	}
}

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

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

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

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

int[] solveGreedy(int[] as) {
  const n = as.sum;
  foreach (s; 0 .. 4) {
    int[] ps;
    auto bs = as.dup;
    if (bs[s] > 0) {
      --bs[s];
      ps ~= s;
      foreach (i; 1 .. n) {
        foreach (x; 0 .. 4) {
          if (bs[x] > 0 && abs(x - ps[i - 1]) == 1) {
            --bs[x];
            ps ~= x;
            goto found;
          }
        }
        goto failed;
       found:
      }
      return ps;
    }
   failed:
  }
  return null;
}

void main() {
  debug {
    foreach (a; 0 .. 10) foreach (b; 0 .. 10) foreach (c; 0 .. 10) foreach (d; 0 .. 10) {
      const n = a + b + c + d;
      if (0 < n && n <= 10) {
        int[] perm;
        perm ~= repeat(0, a).array;
        perm ~= repeat(1, b).array;
        perm ~= repeat(2, c).array;
        perm ~= repeat(3, d).array;
        int[] ans;
        do {
          bool ok = true;
          foreach (i; 0 .. n - 1) {
            ok = ok && (abs(perm[i + 1] - perm[i]) == 1);
          }
          if (ok) {
            ans = perm.dup;
            break;
          }
        } while (perm.nextPermutation);
        const res = solveGreedy([a, b, c, d]);
        writeln([a, b, c, d], ": ", ans, " ", res);
        assert((ans != null) == (res != null));
      }
    }
  }

  try {
    for (; ; ) {
      auto A = new int[4];
      foreach (x; 0 .. 4) {
        A[x] = readInt();
      }
      const res = solveGreedy(A);
      if (res) {
        writeln("YES");
        foreach (i, x; res) {
          if (i > 0) write(" ");
          write(x);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}


// https://codeforces.com/problemset/problem/467/B
import std.stdio;
import std.conv;
import std.array;
import std.algorithm;

void main() {
    int[] words = readln.split.map!(x => x.to!int).array;
    int n = words[0];
    int m = words[1];
    int k = words[2];

    int[] armies;

    foreach(x; stdin.byLine)
        armies ~= x.to!int;

    int answer = 0;

    foreach(army; armies) {
        int xor = army^armies[m];
        int differ = 0;
        for(int bit = 0; bit < n; bit++)
            if((xor&(1<<bit)) > 0)
                differ += 1;
        if(differ <= k)
            answer += 1;
    }

    answer -= 1;
    answer.writeln;
}

import std.stdio;

void main() {
  int n, m, k; readf(" %s %s %s", &n, &m, &k);
  auto mask = new int [m];
  for (int i = 0; i < m; i++) {
    readf(" %s", &mask[i]);
  }
  int cur; readf(" %s", &cur);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int count = 0;
    for (int j = 0; j < n; j++) {
      if ((mask[i] & (1 << j)) != (cur & (1 << j))) {
        count++;
      }
    }
    ans += count <= k;
  }
  writeln(ans);
}


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

// Vicfred
// https://codeforces.com/contest/1506/problem/A
// math
import std.stdio;

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

while(t--) {
    long n, m, x;
    readf("%s %s %s\n", &n, &m, &x);

    x -= 1;

    long row = x % n;
    long column = x / n;

    writefln("%s", (row*m) + column + 1);
}
}


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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        auto params = readln.split();
        int n = to!int(params[0]);
        string s = readln.strip();
        if (to!string(uniq(s)) == params[1]) {
            writeln(0);
            continue;
        }
        else {
            Nullable!(ulong) oneind;
            foreach(ind, el; s) {
                if ((el == to!char(params[1])) && (2*(ind + 1) > n))
                    oneind = ind;
            }
            if (!oneind.isNull) {
                writeln(1);
                writeln(oneind + 1);
            }
            else {
                writeln(2);
                writeln(n-1, ' ', n);
            }
        }
    }
}

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

void solve(){
    ll n = scan;
    dchar c = scan!(dchar);
    auto word = scan!(dchar[]);
    ll cnt = 0;
    for(int i = 0; i < n; ++i){
        cnt += (word[i] == c);
    }
    if(cnt == n){
        writeln(0);
        writeln;
        return;
    }
    for(ll k = 1; k <= n; ++k){
        bool f = 1;
        for(ll m = k-1; m < n; m += k){
            if(word[to!int(m)] != c){ f = 0; break; }
        }
        if(f){
            writeln(1);
            writeln(k);
            return;
        }
    }
    writeln(2);
    writeln(n, " ", n-1);
}

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

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

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

void solve(){
    ll n = scan;
    dchar c = scan!(dchar);
    auto word = scan!(dchar[]);
    ll cnt = 0;
    for(int i = 0; i < n; ++i){
        cnt += (word[i] == c);
    }
    if(cnt == n){
        writeln(0);
        writeln;
        return;
    }
    for(ll k = 1; k < n; ++k){
        bool f = 1;
        for(ll m = k-1; m < n; m += k){
            if(word[to!int(m)] != c){ f = 0; break; }
        }
        if(f){
            writeln(1);
            writeln(k);
            return;
        }
    }
    writeln(2);
    writeln(n, " ", n-1);
}

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

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

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

void solve(){
    ll n = scan;
    dchar c = scan!(dchar);
    auto word = scan!(dchar[]);
    ll cnt = 0;
    for(int i = 0; i < n; ++i){
        cnt += (word[i] == c);
    }
    if(cnt == n){
        writeln(0);
        writeln;
        return;
    }
    for(ll k = 1; k < n; ++k){
        ll seen = 0;
        for(ll j = k; j < n; j += k){
            seen += (word[to!int(j)] == c);
        }
        if(seen == cnt){
            writeln(1);
            writeln(k);
            return;
        }
        if(k*cnt > n){
            break;
        }
    }
    writeln(2);
    writeln(n, " ", n-1);
}

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

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

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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        auto params = readln.split();
        int n = to!int(params[0]);
        string s = readln.strip();
        if (to!string(uniq(s)) == params[1]) {
            writeln(0);
            continue;
        }
        else {
            Nullable!(ulong) oneind;
            foreach(ind, el; s) {
                if ((el == to!char(params[1])) && (2*(ind + 1) > n))
                    oneind = ind;
            }
            if (!oneind.isNull) {
                writeln(1);
                writeln(oneind);
            }
            else {
                writeln(2);
                writeln(n-1, ' ', n);
            }
        }
    }
}

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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        auto params = readln.split();
        int n = to!int(params[0]);
        string s = readln.strip();
        if (to!string(uniq(s)) == params[1]) {
            writeln(0);
            continue;
        }
        else {
            if (s[n - 1] == to!char(params[1])) {
                writeln(1);
                writeln(n);
            }
            else {
                if (s[n - 2] == to!char(params[1])) {
                    if (to!string(uniq(s[0..n - 2])) == params[1])
                    {
                        writeln(1);
                        writeln(n - 1);
                    }
                    else {
                        writeln(2);
                        writeln(n-1, ' ', n);
                    }
                }
                else {
                    writeln(2);
                    writeln(n-1, ' ', n);
                }
            }
        }
    }
}

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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        auto params = readln.split();
        int n = to!int(params[0]);
        string s = readln.strip();
        if (to!string(uniq(s)) == params[1]) {
            writeln(0);
            continue;
        }
        else {
            if (s[n - 1] == to!char(params[1])) {
                writeln(1);
                writeln(n);
            }
            else {
                writeln(2);
                writeln(n-1, ' ', n);
            }
        }
    }
}


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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long a, b, c;
    readf!" %d %d %d "(a, b, c);
    writeln(a + b == c || a + c == b || b + c == a ? "YES" : "NO");
  }
}

import std;

void main()
{
    int w;
    scanf("%d\n", &w);
    foreach(_; 0..w)
    {
        int a,b,c;
        scanf("%d %d %d\n", &a, &b, &c);
        int max_el = max(a,b,c);
        if (a+b+c - max_el == max_el)
            writeln("YES");
        else
            writeln("NO");
    }
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

void solve() {
    auto s = readarray!int;
    bool f() {
        int sum = s.reduce!"a + b";
        for (int i = 0; i < 3; i++) {
            if (sum - s[i] == s[i]) {
                return true;
            }
        }
        return false;
    }
    writeln(f() ? "YES" : "NO");
}

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


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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

void solve() {
    int Q = read!int;
    long[] s = new long[26], t = new long[26];
    s[0] = 1;
    t[0] = 1;

    bool cmp(long[] x, long[] y) {
        for (int i = 25; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (y[i] > 0 && x[j] > 0) return true;
            }
        }
        if (x.count!(c => c > 0) == 1 && y.count!(c => c > 0) == 1) {
            int i = 26 - x.find!(c => c > 0).length;
            int j = 26 - y.find!(c => c > 0).length;
            if (i == j) {
                return x[j] < y[j];
            }
        }
        return false;
    }

    foreach (q; 0 .. Q) {
        int d, k;
        string x;
        readf("%d %d %s\n", &d, &k, &x);
        if (d == 1) {
            foreach (c; x) {
                int i = cast(int)(c - 'a');
                s[i] += k;
            }
            writeln(cmp(s, t) ? "YES" : "NO");
        } else {
            assert(d == 2);
            foreach (c; x) {
                int i = cast(int)(c - 'a');
                t[i] += k;
            }
            writeln(cmp(s, t) ? "YES" : "NO");
        }
    }
}

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

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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long q, d, k;
    string x;
    readf!" %d "(q);
    long[26] ft, fs;
    long totals = 1, totalt = 1;
    ft[0] = 1;
    fs[0] = 1;
    foreach (_ ; 0 .. q) {
      auto tmp = readln.splitter.array;
      d = tmp[0].to!long;
      k = tmp[1].to!long;
      x = tmp[2].strip;
      if (d == 1) {
        foreach (ch ; x) {
          fs[cast(size_t)(ch - 'a')] += k;
          totals += k;
        }
      } else {
        foreach (ch ; x) {
          ft[cast(size_t)(ch - 'a')] += k;
          totalt += k;
        }
      }

      if (totalt != ft[0]) {
        writeln("YES");
        continue;
      }

      if (totals == fs[0]) {
        writeln(totals < totalt ? "YES" : "NO");
        continue;
      }

      writeln("NO");
    }
  }
}


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

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

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

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

void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}

// T, S: monoid
// opTT: T * T -> T
// opST: S * T * int -> T
//   (s t_a) ... (s t_{b-1}) = opST(s, t_a ... t_{b-1}, b - a)
// opSS: S * S -> S
// query(a, b, s): t_a <- s t_a, ..., t_{b-1} <- s t_{b-1};
//   returns t_a ... t_{b-1}
class SegmentTree(T, S, alias opTT, alias opST, alias opSS) {
  import std.functional : binaryFun;
  alias opTTFun = binaryFun!opTT;
  alias opSTFun = opST;
  alias opSSFun = binaryFun!opSS;
  const(T) idT;
  const(S) idS;

  int n;
  T[] ts;
  S[] ss;
  this(int n_, const(T) idT, const(S) idS) {
    this.idT = idT;
    this.idS = idS;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ss = new S[n << 1];
    ts[] = idT;
    ss[] = idS;
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opTTFun(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const(S) s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const(S) s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opSTFun(s, ts[u], r - l);
      ss[u] = opSSFun(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    // speed-up: if (ss[u] != idS)
    {
      ts[uL] = opSTFun(ss[u], ts[uL], mid - l);
      ts[uR] = opSTFun(ss[u], ts[uR], r - mid);
      ss[uL] = opSSFun(ss[u], ss[uL]);
      ss[uR] = opSSFun(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTTFun(ts[uL], ts[uR]);
    return opTTFun(resL, resR);
  }
}

enum INF = 10^^9;

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

      auto bit = new int[N];
      auto seg = new SegmentTree!(int, int, max, (s, t, sz) => (s + t), "a + b")(N, -INF, 0);
      foreach (i; 0 .. N) {
        seg.at(i) = 0;
      }
      seg.build;

      auto ans = new int[N];
      foreach_reverse (i; 0 .. N) {
        const j = bit.bSum(P[i]);
        bit.bAdd(P[i], +1);
        const now = seg.query(P[i], P[i] + 1, 0);
        debug {
          writeln(i, " ", P[i], " ", j, " ", now);
        }
        seg.query(P[i], P[i] + 1, (P[i] - j) - now);
        seg.query(P[i] + 1, N, -1);
        ans[i] = max(seg.query(0, N, 0), 0);
      }

      foreach (i; 0 .. N) {
        write(ans[i] + 1, " ");
      }
      writeln(1);
    }
  } catch (EOFException e) {
  }
}

/+ dub.sdl:
    name "B"
    dependency "dcomp" version=">=0.7.3"
+/

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

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    int[] a;
    sc.read(a); a[]-=1;
    bool[] used = new bool[n];
    int r = n-1;
    writeln(1);
    foreach (int i, d; a) {
        used[d] = true;
        while (r >= 0 && used[r]) r--;
        int s = 1 + (i+1) - (n-1-r);
        writeln(s);
    }
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;

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

}
version (X86) static if (__VERSION__ < 2071) {
    import core.bitop : bsf, bsr, popcnt;
    int bsf(ulong v) {
        foreach (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int bsr(ulong v) {
        foreach_reverse (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int popcnt(ulong v) {
        int c = 0;
        foreach (i; 0..64) {
            if (v & (1UL << i)) c++;
        }
        return c;
    }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.array;

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

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

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

/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;

T[N] fixed(T, size_t N)(T[N] a) {return a;}

struct FastAppender(A, size_t MIN = 4) {
    import std.algorithm : max;
    import std.conv;
    import std.range.primitives : ElementEncodingType;
    import core.stdc.string : memcpy;

    private alias T = ElementEncodingType!A;
    private T* _data;
    private uint len, cap;

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

    void reserve(size_t nlen) {
        import core.memory : GC;
        if (nlen <= cap) return;

        void* nx = GC.malloc(nlen * T.sizeof);

        cap = nlen.to!uint;
        if (len) memcpy(nx, _data, len * T.sizeof);
        _data = cast(T*)(nx);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void opOpAssign(string op : "~")(T item) {
        if (len == cap) {
            reserve(max(MIN, cap*2));
        }
        _data[len++] = item;
    }

    void insertBack(T item) {
        this ~= item;
    }

    void removeBack() {
        len--;
    }

    void clear() {
        len = 0;
    }
    ref inout(T) back() inout { assert(len); return _data[len-1]; }
    ref inout(T) opIndex(size_t i) inout { return _data[i]; }

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

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


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

void main() {
  alias pii = Tuple!(int, "k", int, "v");
  pii[] a1, a2;
  int n = readln().strip().to!int;
  foreach (i; 0..n) {
    auto input = readln().split().map!(to!int).array;
    int k = input[0], v = input[1];
    if (k < 0) {
      a1 ~= pii(-k, v);
    } else {
      a2 ~= pii(k, v);
    }
  }
  a1.sort();
  a2.sort();
  int m = cast(int)min(a1.length, a2.length);
  auto b1 = a1.map!(t => t.v).array;
  auto b2 = a2.map!(t => t.v).array;
  b1 ~= 0;
  b2 ~= 0;
  writeln(sum(b1[0..m+1]) + sum(b2[0..m+1]));
}

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

struct Tree {
    int pos, apples;
}

void main() {
    int n;
    readf(" %d", &n);
    auto trees = new Tree[n];
    foreach (ref tree; trees)
        readf(" %d %d", &tree.pos, &tree.apples);
    trees.sort!"a.pos < b.pos"();
    int mid = trees.length - trees.find!"a.pos > b"(0).length;
    int result = 0;
    int i, j;
    for (i = mid - 1, j = mid; i >= 0 && j < n; i--, j++)
        result += trees[i].apples + trees[j].apples;
    if (i >= 0)
        result += trees[i].apples;
    if (j < n)
        result += trees[j].apples;
    writeln(result);
}


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

immutable int letters = 26;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto s = readln.strip.map !(q{a - 'a'}).array;
		int hi = 0;
		auto trans = letters.iota.array;
		foreach (int i, c; s)
		{
			while (k > 0 && trans[c] > 0)
			{
				int prev = trans[c];
				foreach (j; 0..letters)
				{
					if (trans[j] == prev)
					{
						trans[j] = trans[prev - 1];
					}
				}
				k -= 1;
			}
		}
		writeln (s.map !(c => cast (char) (trans[c] + 'a')));
	}
}

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] > k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write('a');
        }else if(w[i] <= cr || cr == -1){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

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

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

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

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

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

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';
    if(imax == 0){ minchar = ('a' + low); }

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

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

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

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';
    if(imax == 0){ minchar = ('a' + low); }

    for(int i = 0; i < n; ++i){
        if(w[i] <= k){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

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

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


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

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

    string[] arr;
    foreach (i; 0 .. n) {
        arr ~= readln.chomp;
    }

    auto byCol = new int[][] (n, m);
    foreach (rw; 0 .. n) {
        byCol[rw][m-1] = 1;
        foreach_reverse (c; 0 .. m-1) {
            byCol[rw][c] = arr[rw][c] == arr[rw][c+1] ?
                byCol[rw][c+1] + 1 : 1;
        }
    }

    auto byRow = new int[][] (n, m);
    foreach (c; 0 .. m) {
        byRow[n-1][c] = 1;
        foreach_reverse (rw; 0 .. n-1) {
            byRow[rw][c] = arr[rw][c] == arr[rw+1][c] ?
                byRow[rw+1][c] + 1 : 1;
        }
    }

    debug {
        byRow.each!writeln;
        byCol.each!writeln;
    }

    long ans = 0;
    foreach (rw; 0 .. n) {
        foreach (c; 0 .. m) {
            int reach1 = byRow[rw][c];

            if (rw + reach1 >= n) { continue; }

            int reach2 = byRow[rw + reach1][c];

            if (reach1 != reach2 || rw + reach1 + reach2 >= n) { continue; }

            int reach3 = min(reach1, byRow[rw + reach1 + reach2][c]);

            if (reach3 < reach1) { continue; }

            int rt = m;
            foreach (rwnxt; rw .. rw + reach1 + reach2 + reach3) {
                rt = min(rt, byCol[rwnxt][c]);
            }

            ans += rt;
        }
    }

    ans.writeln;
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;
import std.bigint;

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int n, m;
  readf(" %s %s\n", n, m);
  string[] A = new string[n];
  foreach(i; 0 .. n) {
    A[i] = readln.strip;
  }

  int[] ccl = new int[n];
  ccl[] =0;
  long ans = 0;

  foreach(j;0 .. m) {
    int[] co; //= new int[];
    char[] ch;// = new char[];
    int[] ml;// = new int[];
    co ~= 0;
    ch ~= A[0][j];
    ml ~= (n+m+100);

    foreach(i; 0 .. n) {
      if (j >= 1 && A[i][j] == A[i][j-1]) ccl[i]++; else ccl[i] = 1;

      if (A[i][j] == ch[$-1]) {
        co[$-1]++;
        ml[$-1] = min(ml[$-1], ccl[i]);
      } else {
        co ~= 1;
        ch ~= A[i][j];
        ml ~= ccl[i];
      }

      if (co.length >= 3) {
        if (co[$-1] == co[$-2] && co[$-1] <= co[$-3]) {
          long t= min(ml[$-1], ml[$-2]);
          int sss = co[$-1];
          t = min(t, minElement( ccl[ i - sss* 3 + 1 .. i - sss*2 + 1 ] ) );
          ans += t;
        }
      }
    }
  }

  writeln(ans);

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;
import std.bigint;

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int n, m;
  readf(" %s %s\n", n, m);
  string[] A = new string[n];
  foreach(i; 0 .. n) {
    A[i] = readln.strip;
  }

  int[] ccl = new int[n];
  ccl[] =0;
  long ans = 0;

  foreach(j;0 .. m) {
    int[] co; //= new int[];
    char[] ch;// = new char[];
    int[] ml;// = new int[];
    co ~= 0;
    ch ~= A[0][j];
    ml ~= (n+m+100);

    foreach(i; 0 .. n) {
      if (j >= 1 && A[i][j] == A[i][j-1]) { ccl[i]++;} else ccl[i] = 1;

      if (A[i][j] == ch[$-1]) {
        co[$-1]++;
        ml[$-1] = min(ml[$-1], ccl[i]);
      } else {
        co ~= 1;
        ch ~= A[i][j];
        ml ~= ccl[i];
      }

      if (co.length >= 3) {
        if (co[$-1] == co[$-2] && co[$-1] >= co[$-3]) {
          long t= min(ml[$-1], ml[$-2]);
          int sss = co[$-1];
          t = min(t, minElement( ccl[ i - sss* 3 + 1 .. i - sss*2 + 1 ] ) );
          ans += t;
        }
      }
    }
  }

  writeln(ans);

}


immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto m = readInt!int;
  struct Adj
  {
    int value;
    int w;
    int index;
  }
  auto adj = new Adj[][](n);
  int getParity(int value)
  {
    int parity = 0;
    while (value)
      {
	parity ^= value&1;
	value >>= 1;
      }
    return parity;
  }
  auto edges = new int[3][](0);
  foreach(i; 0 .. n - 1)
    {
      auto x = readInt!int - 1;
      auto y = readInt!int - 1;
      auto v = readInt!int;
      adj[x] ~= Adj(v, y, i);
      adj[y] ~= Adj(v, x, i);
      edges ~= [x, y, v];
    }
  auto setAcc = new bool[](n);
  auto acc = new int[](n);
  auto rel = new Adj[][](n);
  foreach(i; 0 .. m)
    {
      auto a = readInt!int - 1;
      auto b = readInt!int - 1;
      auto p = readInt!int;
      rel[a] ~= Adj(p, b);
      rel[b] ~= Adj(p, a);
    }
  foreach(e; edges)
    {
      if (e[2] >= 0)
	{
	  auto p = getParity(e[2]);
	  rel[e[0]] ~= Adj(p, e[1]);
	  rel[e[1]] ~= Adj(p, e[0]);
	}
    }
  bool possible = true;
  void fillAcc(int v, int color)
  {
    if (setAcc[v])
      {
	if (acc[v] != color) possible = false;
	return;
      }
    else
      {
	setAcc[v] = true;
	acc[v] = color;
      }
    foreach(a; rel[v])
      {
	fillAcc(a.w, color ^ a.value);
      }
  }
  foreach(i; 0 .. n) if (!setAcc[i]) fillAcc(i, 0);
  if (!possible) return writeln("NO");
  writeln("YES");
  void fillEdges(int v, int parent)
  {
    foreach(a; adj[v]) if (a.w != parent)
      {
	int diff = acc[a.w] ^ acc[v];
	if (a.value < 0)
	  {
	    assert(a.value == edges[a.index][2]);
	    edges[a.index][2] = diff;
	  }
	fillEdges(a.w, v);
      }
  }
  fillEdges(0, -1);
  foreach(edge; edges)
    {
      writeln(edge[0] + 1, " ", edge[1] + 1, " ", edge[2]);
    }
  writeln;
}

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

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

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

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

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

import std.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 Value = int;
int root(int[] ps, Value[] qs, int u) {
  if (ps[u] < 0) {
    return u;
  } else {
    int r = root(ps, qs, ps[u]);
    // qs[u] += qs[ps[u]];
    qs[u] ^= qs[ps[u]];
    return (ps[u] = r);
  }
}
bool connect(int[] ps, Value[] qs, int u, int v, Value c) {
  int ru = root(ps, qs, u);
  int rv = root(ps, qs, v);
  // Value cc = c + qs[u] - qs[v];
  Value cc = c ^ qs[u] ^ qs[v];
  if (ru == rv) return (cc == 0);
  // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }
  if (ps[ru] > ps[rv]) { swap(ru, rv); }
  ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;
  return true;
}

int N, M;
int[] A, B, C;
int[] U, V, P;

int[][] G;
int[] ds;

void dfs(int u, int p, int d) {
  ds[u] = d;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      const c = (~C[i]) ? (popcnt(C[i]) & 1) : 0;
      dfs(v, u, d ^ c);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt;
        M = readInt;
        A = new int[N - 1];
        B = new int[N - 1];
        C = new int[N - 1];
        foreach (i; 0 .. N - 1) {
          A[i] = readInt - 1;
          B[i] = readInt - 1;
          C[i] = readInt;
        }
        U = new int[M];
        V = new int[M];
        P = new int[M];
        foreach (m; 0 .. M) {
          U[m] = readInt - 1;
          V[m] = readInt - 1;
          P[m] = readInt;
        }

        G = new int[][N];
        foreach (i; 0 .. N - 1) {
          G[A[i]] ~= i;
          G[B[i]] ~= i;
        }
        ds = new int[N];
        dfs(0, -1, 0);
        debug {
          writeln("ds = ", ds);
        }

        auto ps = new int[N];
        ps[] = -1;
        auto qs = new int[N];
        foreach (i; 0 .. N - 1) {
          if (~C[i]) {
            if (!connect(ps, qs, A[i], B[i], popcnt(C[i]) & 1)) {
              assert(false);
            }
          }
        }
        bool ok = true;
        foreach (m; 0 .. M) {
          if (!connect(ps, qs, U[m], V[m], P[m])) {
            ok = false;
          }
        }
        if (ok) {
          foreach (u; 0 .. N) {
            root(ps, qs, u);
          }
          writeln("YES");
          foreach (i; 0 .. N - 1) {
            int c = C[i];
            if (!~c) {
              c = qs[A[i]] ^ qs[B[i]];
            }
            writeln(A[i] + 1, " ", B[i] + 1, " ", c);
          }
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}


immutable multi = true;

void solve(int)
{
	auto n = readInt!long;
	auto s = readInt!long;
	long med = (n+1)/2;
	long frommed = n - med +1;
	return writeln(s / frommed);
}

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

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

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

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

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

import std.algorithm;
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, s;
		readf !(" %s %s") (n, s);
		n /= 2;
		n += 1;
		s /= n;
		writeln (s);
	}
}

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 s = RD!int;

		auto cnt = n / 2 + 1;
		ans[ti] = s / cnt;
	}

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

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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(i; 0..t){
	int n, s;
	scanf("%d %d", &n, &s);
	auto num_zero = (n - 1) / 2;
	writeln(s / (n - num_zero));
    }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n, s;
    readf!" %d %d "(n, s);
    long cnt = n / 2 + 1;
    long ans = s / cnt;
    writeln(ans);
  }
}


import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    enum spec = ' ' ~ replicate("%s ", vars.length);
    return readf(spec, getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        version (LocalProject)
            heroes[ ] = Hero.init;
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = 0;
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage > h.hp && damage < h.maxHp && h.regen) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen > 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime));
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        if (kBounty && heroes[ ].any!(h => h.maxHp <= damage || (!h.regen && h.hp <= damage)))
            write("-1\n");
        else
            writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}

import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        version (LocalProject)
            heroes[ ] = Hero.init;
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = 0;
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage > h.hp && damage < h.maxHp && h.regen) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen > 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime));
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        if (kBounty && heroes[ ].any!(h => h.maxHp <= damage || (!h.regen && h.hp <= damage)))
            write("-1\n");
        else
            writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}

import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = cast(int)heroes[ ].count!(h => isSemiVictimAt(h, 0));
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp, bool allowLoop = true) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage >= h.hp && damage < h.maxHp && h.regen && (damage > h.hp || allowLoop)) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen >= 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime), false);
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        if (heroes[ ].any!(h => h.maxHp <= damage || (!h.regen && h.hp <= damage)))
            write("-1\n");
        else
            writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}

import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }
        if (kBounty && heroes[ ].any!(h => h.maxHp <= damage)) {
            write("-1\n");
            debug writeln();
            continue;
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = cast(int)heroes[ ].count!(h => isSemiVictimAt(h, 0));
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp, bool allowLoop = true) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage >= h.hp && damage < h.maxHp && h.regen && (damage > h.hp || allowLoop)) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen >= 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime), false);
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}

import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        version (LocalProject)
            heroes[ ] = Hero.init;
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = cast(int)heroes[ ].count!(h => isSemiVictimAt(h, 0));
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp, bool allowLoop = true) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage >= h.hp && damage < h.maxHp && h.regen && (damage > h.hp || allowLoop)) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen >= 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime), false);
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        if (kBounty && heroes[ ].any!(h => h.maxHp <= damage || (!h.regen && h.hp <= damage)))
            write("-1\n");
        else
            writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}

import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

void memSet(T, size_t n)(ref T[n] a, ubyte value) {
    memset(a.ptr, value, a.sizeof);
}

void memSet(T)(T[ ] a, ubyte value) {
    memset(a.ptr, value, a.length * T.sizeof);
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Hero {
    int hp, maxHp, regen;
    int lastUpdated;
}

struct Event {
    enum Type: ubyte {
        hpChanged,
        critPoint,
    }

    int time;
    Type type;
    Hero* h;
    union { int newHp, lastUpdated; }
}

int hcount, mcount;
int baseBounty, kBounty, damage;
Hero[100_000] _heroes;

long calcBounty(int time) {
    return baseBounty + long(time) * kBounty;
}

void main() {
    Array!Event events;
    while (read(hcount, mcount, baseBounty, kBounty, damage)) {
        auto heroes = _heroes[0 .. hcount];
        version (LocalProject)
            heroes[ ] = Hero.init;
        events.clear();
        events.reserve(hcount + mcount);
        foreach (ref h; heroes) {
            read(h.maxHp, h.hp, h.regen);
            assert(h.hp <= h.maxHp);
        }
        foreach (i; 0 .. mcount) {
            int time, hindex, newHp;
            read(time, hindex, newHp);
            events ~= Event(time, Event.Type.hpChanged, &heroes[hindex - 1], newHp);
        }

        auto pq = events.heapify!((a, b) => tuple(a.time, a.type) > tuple(b.time, b.type));
        int curTime = 0, prevTime = 0;

        int getEffectiveHpAt(ref const Hero h, int time) {
            return cast(int)min(h.hp + long(time - h.lastUpdated) * h.regen, h.maxHp);
        }

        bool isVictimAt(ref const Hero h, int time) {
            if (h.maxHp <= damage)
                return true;
            const hp = getEffectiveHpAt(h, time);
            return hp < damage || (hp == damage && !h.regen);
        }

        bool isSemiVictimAt(ref const Hero h, int time) {
            return h.maxHp > damage && getEffectiveHpAt(h, time) == damage && h.regen;
        }

        int victims = cast(int)heroes[ ].count!(h => isVictimAt(h, 0));
        int semi = cast(int)heroes[ ].count!(h => isSemiVictimAt(h, 0));
        long maxBounty = 0;

        void processHeroRegen(ref Hero h, int newHp) {
            if (isVictimAt(h, h.lastUpdated))
                victims--;
            assert(victims >= 0);

            h.hp = newHp;
            assert(h.hp <= h.maxHp);
            if (damage > h.hp && damage < h.maxHp && h.regen) {
                const timeToRegen = (damage - h.hp + h.regen - 1) / h.regen;
                assert(timeToRegen >= 0);
                pq.insert(Event(curTime + timeToRegen, Event.Type.critPoint, &h, curTime));
            }

            h.lastUpdated = curTime;
            if (isVictimAt(h, curTime))
                victims++;
            else if (isSemiVictimAt(h, curTime))
                semi++;
            assert(victims + semi <= hcount);
        }

        foreach (ref h; heroes)
            processHeroRegen(h, h.hp);

        while (!pq.empty) {
            auto e = pq.front;
            pq.removeFront();
            curTime = e.time;
            if (curTime != prevTime) {
                const option1 = (victims + semi) * calcBounty(prevTime);
                const option2 = victims * calcBounty(curTime - 1);
                debug if (max(option1, option2) > maxBounty)
                    writefln("%s | %s: v = %s, s = %s, bounty = %s | %s",
                        prevTime, curTime - 1, victims, semi, option1, option2);
                maxBounty = max(maxBounty, option1, option2);
                semi = 0;
            }

            final switch (e.type) with (Event.Type) {
            case hpChanged:
                processHeroRegen(*e.h, e.newHp);
                break;

            case critPoint:
                if (e.lastUpdated == e.h.lastUpdated)
                    processHeroRegen(*e.h, getEffectiveHpAt(*e.h, curTime));
                break;
            }

            debug writefln("%s: stats = [%(%(%s/%), %)]",
                curTime,
                heroes[ ].map!(h => [getEffectiveHpAt(h, curTime), h.maxHp]),
            );
            prevTime = curTime;
        }
        maxBounty = max(maxBounty, (victims + semi) * calcBounty(prevTime));
        if (kBounty && heroes[ ].any!(h => h.maxHp <= damage || (!h.regen && h.hp <= damage)))
            write("-1\n");
        else
            writeln(maxBounty);
        debug writeln();
    }

    stdout.flush();
    _Exit(0);
}


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

void main ()
{
	int n, limit;
	while (readf (" %s %s", &n, &limit) > 0)
	{
		limit *= 8;
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		auto c = a.group.array;
		auto letters = c.length;
		auto s = [0];
		s.reserve (letters + 1);
		foreach (e; c)
		{
			s ~= s.back + e[1];
		}
		int res = n;

		int k = 0;
		int r = 1;
		while (r <= letters)
		{
			for (int i = 0, j = r; j <= letters; i++, j++)
			{
				if (n * 1L * k <= limit)
				{
					int cur = s[j] - s[i];
					res = min (res, n - cur);
				}
			}
			k += 1;
			r <<= 1;
		}
		if (n * 1L * k <= limit)
		{
			res = 0;
		}

		writeln (res);
	}
}

import std.stdio;
import std.algorithm;
import std.math: pow;

const int MAX = 400005;

int n, I;
int[MAX] a;
int[MAX] count;
int[MAX] sum;

void main() {
    readf(" %s %s", n, I);
    foreach(i; 0..n) readf(" %s", a[i]);

    auto k = min(19, I * 8 / n);
    auto K = pow(2, k);

    sort(a[0..n]);
    auto m = 1;
    count[0] = 0;
    count[1] = 1;
    foreach(i; 1..n) {
        if (a[i] != a[i-1]) m++;
        count[m]++;
    }
    m++;

    foreach(i; 1..m) sum[i] = sum[i-1] + count[i];

    auto result = 0;
    foreach(i; 1..m) {
        auto j = max(0, i-K);
        result = max(result, sum[i] - sum[j]);
    }
    writeln(n - result);
}

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.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

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

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

    debug { int.max.writeln; }

    int space = el * 8 / n;
    int mx = space < 31 ? 2 ^^ space : int.max;

    debug { mx.writeln; }

    auto vals = arr.sort.group.map!(v => v[1]).array;

    debug { vals.writeln; }

    int ans = n, cur = vals[0 .. min(mx, vals.length)].sum;
    foreach (i; 0 .. vals.length) {
        ans = min(ans, n - cur);

        cur -= vals[i];
        if (i + mx < vals.length) { cur += vals[i + mx]; }
    }

    ans.writeln;
}

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

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

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

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

int N, I;
int[] A;

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

      int k;
      for (k = N; k >= 1; --k) {
        long logK = bsr(k);
        if (k & k - 1) {
          ++logK;
        }
        debug {
          writeln(k, " ", logK);
        }
        if (N * logK <= 8 * I) {
          break;
        }
      }
      debug {
        writeln("k = ", k);
      }

      auto as = A.dup;
      as = as.sort.uniq.array;
      const asLen = cast(int)(as.length);
      auto cnt = new int[asLen];
      foreach (i; 0 .. N) {
        ++cnt[as.lowerBound(A[i])];
      }
      debug {
        writeln("cnt = ", cnt);
      }
      auto sum = new int[asLen + 1];
      foreach (j; 0 .. asLen) {
        sum[j + 1] = sum[j] + cnt[j];
      }

      int ans;
      foreach (j; 0 .. asLen) {
        chmax(ans, sum[min(j + k, asLen)] - sum[j]);
      }
      ans = N - ans;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

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

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

    debug { int.max.writeln; }

    int space = el * 8 / n;
    int mx = space < 31 ? 2 ^^ space : int.max;

    debug { mx.writeln; }

    auto vals = arr.sort.group.map!(v => v[1]).array;

    debug { vals.writeln; }

    int ans = n, cur = vals[0 .. min(mx, vals.length)].sum;
    foreach (i; 0 .. vals.length) {
        ans = min(ans, n - cur);

        cur -= vals[i];
        if (i + mx < vals.length) { cur += vals[i + mx - 1]; }
    }

    ans.writeln;
}

import std.stdio;
import std.algorithm;
import std.math: pow;

const int MAX = 400005;

int n, I;
int[MAX] a;
int[MAX] count;
int[MAX] sum;

void main() {
    readf(" %s %s", n, I);
    foreach(i; 0..n) readf(" %s", a[i]);

    auto k = I * 8 / n;
    auto K = pow(2, k);

    sort(a[0..n]);
    auto m = 1;
    count[0] = 0;
    count[1] = 1;
    foreach(i; 1..n) {
        if (a[i] != a[i-1]) m++;
        count[m]++;
    }
    m++;

    foreach(i; 1..m) sum[i] = sum[i-1] + count[i];

    auto result = 0;
    foreach(i; 1..m) {
        auto j = max(0, i-K);
        result = max(result, sum[i] - sum[j]);
    }
    writeln(n - result);
}


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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        ulong n, k;
        readf!"%d %d\n"(n, k);

        ulong l = cast(ulong) ceil(log2(k));

        ulong c_i = pow(2, l+1) - (pow(2, l) - k);

        if (n <= c_i) {
            writeln(ceil(log2(n)));
            continue;
        }

        ulong c_a = n - c_i;
        ulong ceil = c_a / k + (c_a % k == 0 ? 0 : 1);
        writeln(l + ceil + 1);
    }
}

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);
		long cur = 1;
		long res = 0;
		while (cur < n && cur < k)
		{
			cur <<= 1;
			res += 1;
		}
		writeln (res + (n - cur + k - 1) / k);
	}
}


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

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

    long[] pows;
    if (k == 1) pows ~= 1;
    else if (k == -1) pows ~= [-1, 1];
    else {
        pows ~= [1, k];
        while (abs(pows.back) < 10L ^^ 9 * 10 ^^ 5) { pows ~= pows[$-1] * k; }
    }

    int[long] cnt;
    cnt[0] = 1;

    long ans = 0;
    long sm = 0;
    foreach (e; arr) {
        sm += e;

        foreach (p; pows) { ans += cnt.get(sm - p, 0); }

        cnt[sm] += 1;
    }

    ans.writeln;
}

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

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

    long[] pows;
    if (k == 1) pows ~= 1;
    else if (k == -1) pows ~= [-1, 1];
    else {
        pows ~= [1, k];
        while (abs(pows.back) < 10L ^^ 9 * 10 ^^ 5) pows ~= pows[$-1] * k;
    }

    int[long] cnt;
    cnt[0] = 1;

    long ans = 0;
    long sm = 0;
    foreach (e; arr) {
        sm += e;

        foreach (p; pows) { ans += cnt.get(sm - p, 0); }

        cnt[sm] += 1;
    }

    ans.writeln;
}

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

void main ()
{
	int n;
	long k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		long total = a.map !(abs).sum (0L);
		long q = 1;
		long [] p;
		while (!p.canFind (q) && abs (q) <= total)
		{
			p ~= q;
			q *= k;
		}
		debug {writeln (p);}

		long res = 0;
		int [long] s;
		s[0] += 1;
		long cur = 0;
		foreach (c; a)
		{
			cur += c;
			foreach (r; p)
			{
				debug {writeln ("? ", r - cur);}
				if ((cur - r) in s)
				{
					res += s[cur - r];
				}
			}
			s[cur] += 1;
			debug {writeln (s);}
		}
		writeln (res);
	}
}

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

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

    long[] pows;
    pows ~= k;
    while (k != 1 && abs(pows.back) < 10L ^^ 9 * 10 ^^ 5) pows ~= pows[$-1] * k;

    int[long] cnt;
    cnt[0] = 1;

    long ans = 0;
    long sm = 0;
    foreach (e; arr) {
        sm += e;

        foreach (p; pows) { ans += cnt.get(sm - p, 0); }

        cnt[sm] += 1;
    }

    ans.writeln;
}

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

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

    long[] pows;
    if (k == 1) pows ~= 1;
    else if (k == -1) pows ~= [-1, 1];
    else {
        pows ~= k;
        while (abs(pows.back) < 10L ^^ 9 * 10 ^^ 5) pows ~= pows[$-1] * k;
    }

    int[long] cnt;
    cnt[0] = 1;

    long ans = 0;
    long sm = 0;
    foreach (e; arr) {
        sm += e;

        foreach (p; pows) { ans += cnt.get(sm - p, 0); }

        cnt[sm] += 1;
    }

    ans.writeln;
}