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

void main ()
{
	int n, w;
	while (readf (" %s %s", &n, &w) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long cur = 0;
		long lo = cur;
		long hi = cur;
		foreach (c; a)
		{
			cur += c;
			lo = min (lo, cur);
			hi = max (hi, cur);
		}
		long inf = 0 - lo;
		long sup = w - hi;
		long res = sup - inf + 1;
		res = max (res, 0);
		writeln (res);
	}
}

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

  int n; long w; rd(n, w);
  auto diff=readln.split.to!(long[]);

  long lowerBound(){ // cap: [0, infty)
    bool tooLarge(long x){
      if(x<0) return false;
      foreach(d; diff){
        if((x+=d)<0) return false;
      }
      return true;
    }
    long ng=-1, ok=1_000_000_000_000;
    while(ok-ng>1){ // (ng, ok]
      auto m=(ok+ng)/2;
      tooLarge(m) ? ok : ng = m;
    }
    return ok;
  }

  long upperBound(){ // (-infty, w]
    bool tooSmall(long x){
      if(x>w) return false;
      foreach(d; diff){
        if((x+=d)>w) return false;
      }
      return true;
    }
    long ok=-1_000_000_000_000, ng=w+1;
    while(ng-ok>1){ // [ok, ng)
      auto m=(ok+ng)/2;
      tooSmall(m) ? ok : ng = m;
    }
    return ok;
  }

  auto ub=upperBound(), lb=lowerBound();
  if(0<=ub && ub<=w && 0<=lb && lb<=w){
    writeln(max(0, ub-lb+1));
  }else{
    writeln(0);
  }
}

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


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

void main ()
{
	int q, x;
	while (readf !(" %s %s") (q, x) > 0)
	{
		auto d = new int [x];
		int res = 0;
		foreach (r; 0..q)
		{
			int u;
			readf !(" %s") (u);
			d[u % x] += 1;
			while (d[res % x] > res / x)
			{
				res += 1;
			}
			writeln (res);
		}
	}
}

import std.stdio;

void main()
{
  // number of queries, operation amount
  uint q, x;
  readf!"%d %d\n"(q, x);

  auto m = new uint[x];
  uint mex = 0; // initial mex is 0

  foreach (i; 0..q) {
    // value to be appended to array
    uint j;
    readf!"%d\n"(j);
    m[j % x]++;

    while (m[mex % x] > mex / x) mex++;

    writeln(mex);
  }
}

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

    auto added = new bool[] (max(q+1, x+1));
    auto nxtfree = new int[] (x);
    foreach (i; 0 .. x) { nxtfree[i] = i; }
    int mex = 0;

    while (q--) {
        int y;
        readf("%s", &y);
        readln;

        y %= x;

        if (nxtfree[y] < added.length) {
            added[nxtfree[y]] = true;
            nxtfree[y] += x;
        }

        while (added[mex]) { ++mex; }

        mex.writeln;
    }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
import std.algorithm.mutation;
import std.container.array;

string[] tokens;

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

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

void main()
{
    int q = readInt;
    int x = readInt;
    int[] cnt = new int[x];
    int ans = 0;
    while (q--)
    {
        cnt[readInt % x]++;
        while (cnt[ans % x])
        {
            cnt[ans++ % x]--;
        }
        writeln(ans);
    }
}

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

DList!string _words;

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

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

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

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

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

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

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

    }
}

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

void main()
{
  long q, x;
  read(q, x);
  auto ap = new long[x.ind];
  auto aps = redBlackTree!(Tuple!(long, long))();
  foreach(i, api; ap[])
    aps.insert(tuple(cast(long)api, cast(long)i));
  foreach(qn; 0 .. q)
    {
      auto y = next!long;
      ap[(y % x).ind]++;
      aps.insert(tuple(ap[(y % x).ind], y % x));

      auto minap = aps.front;
      while(ap[minap[1].ind] != minap[0])
        {
          aps.removeFront;
          minap = aps.front;
        }
      auto minval = minap[0];
      auto minind = minap[1];
      writeln(x * minval + minind);
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto q = RD!int;
	auto x = RD;
	auto ans = new long[](q);
	long pos, loop;
	auto cnt = new long[](cast(int)x);

	void f(long z)
	{
		int y = cast(int)(z % x);
		if (pos == y)
		{
			++cnt[y];
			auto l = cast(int)pos;
			pos = x;
			foreach (i; l..cnt.length)
			{
				if (cnt[i] == loop)
				{
					pos = i;
					break;
				}
			}
			if (pos == x)
			{
				++loop;
				foreach (i; 0..cnt.length)
				{
					if (cnt[i] == loop)
					{
						pos = i;
						break;
					}
				}
			}
		}
		else
			++cnt[y];
	}

	foreach (qi; 0..q)
	{
		auto y = RD;
		f(y);
		ans[qi] = loop*x + pos;
	}

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


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

immutable int mod = 1_000_000_007;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [long] f;
		f[0] = 1;
		long shift = 0;
		int total = 1;
		foreach (ref c; a)
		{
			int cur0 = f.get (0 - shift, 0);
			shift += c;
			int value = (total - cur0 + mod) % mod;
			f[c - shift] = (f.get (c - shift, 0) + value) % mod;
			total = (total + value) % mod;
		}
		writeln (total);
	}
}

import std.stdio;

const ulong M = 1000000007;

void solve()
{
    uint n;
    readf!" %d"(n);

    long[] b = new long[n];
    foreach (ref x; b)
        readf!" %d"(x);

    ulong[long] s = [0: 1];
    long d = 0;
    ulong r = 1;

    foreach (x; b)
    {
        auto k = r + M - s.get(-d, 0);
        r = (r + k) % M;

        d += x;
        s.update(x - d, { return k % M; }, (ref ulong o) { return (o + k) % M; });
    }

    writeln(r);
}

void main()
{
    ushort t;
    readf!" %d"(t);
    for (ushort i = 0; i < t; ++i)
        solve();
}


import std.stdio;
import core.stdc.stdio;

int main()
{
	int n;
	scanf("%d", &n);
	int[] nums; nums.length = n;
	foreach (int i; 0..n)
	{
		scanf("%d", &nums[i]);
	}

	int idx = 0;
	foreach (int i; 2..n)
	{
		int sum = (nums[i] % 2) + (nums[i-1] % 2) + (nums[i-2] % 2);
		if (sum == 0 || sum == 3)
			continue;
		idx = (sum % 2 == nums[i] % 2 ? i : sum % 2 == nums[i-1] % 2 ? i-1 : i-2);
	}
	write(idx+1);
	return 0;
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = stdin.readln.split.map!(to!int).array;
    if (xs.count!("a % 2 == 1") == 1) {
        writeln(xs.countUntil!("a % 2 == 1") + 1);
    } else {
        writeln(xs.countUntil!("a % 2 == 0") + 1);
    }
}

import std.stdio;

int n;
int[] a;
int[2] z;
void main()
{
	readf("%d", &n);
	a.length = n;
	for(int i = 0; i < n; i++) {
		readf(" %d", &a[i]);
		z[a[i] % 2]++;
	}
	for(int i = 0; i < n; i++) {
		if (z[a[i] % 2] == 1) {
			writeln(i + 1);
		}
	}
}

import std.stdio;

int n;
int[] a;
int[2] z;
void main()
{
	readf("%d", &n);
	a.length = n;
	for(int i = 0; i < n; i++) {
		readf(" %d", &a[i]);
		z[a[i] % 2]++;
	}
	for(int i = 0; i < n; i++) {
		if (z[a[i] % 2] == 1) {
			writeln(a[i]);
		}
	}
}

import std.stdio;

int n;
int[] a;
int[2] z;
void main()
{
	readf("%d", &n);
	a.length = n;
	for(int i = 0; i < n; i++) {
		readf(" %d", &a[i]);
		z[a[i] % 2]++;
	}
	for(int i = 0; i < n; i++) {
		if (z[a[i] % 2] == 1) {
			writeln(i);
		}
	}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		if (sum (a) % 2 != 0)
		{
			writeln (-1);
			continue;
		}
		auto b = a.enumerate.filter !(x => x.value != 0).array;
		b ~= typeof (b.front) (n, 0);
		int [] [] answer;
		for (int i = 0; i + 2 < b.length; i += 2)
		{
			if (b[i].value == b[i + 1].value)
			{
				answer ~= [b[i].index, b[i + 2].index];
			}
			else
			{
				answer ~= [b[i].index, b[i + 1].index];
				answer ~= [b[i + 1].index, b[i + 2].index];
			}
		}
		writeln (answer.length);
		foreach (ref line; answer)
		{
			writeln (line[0] + 1, " ", line[1]);
		}
	}
}

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

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

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

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

int N;
int[] A;

int[] solve() {
  const sumA = A.sum;
  if (sumA % 2 != 0) {
    return null;
  }

  auto fs = new bool[N];
  bool greedy(int a, int k) {
    foreach (i; 0 .. N) {
      if (i == 0 || fs[i - 1]) {
        continue;
      }
      if (A[i] == a && k > 0) {
        fs[i] = true;
        --k;
      }
    }
    return (k == 0);
  }
  if (sumA >= 0) {
    if (!greedy(+1, sumA / 2)) return null;
  } else {
    if (!greedy(-1, -sumA / 2)) return null;
  }
  debug {
    writeln("fs = ", fs);
  }

  int[] cuts;
  cuts ~= 0;
  foreach (i; 1 .. N) {
    if (fs[i - 1] == fs[i]) {
      assert(!fs[i]);
      cuts ~= i;
    }
  }
  cuts ~= N;
  return cuts;
}

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

        const ans = solve;
        debug {
          writeln(A, ": ", ans);
        }
        const len = cast(int)(ans.length) - 1;
        if (len >= 0) {
          writeln(len);
          foreach (i; 0 .. len) {
            writeln(ans[i] + 1, " ", ans[i + 1]);
          }
        } else {
          writeln(-1);
        }
      }
    }
  } catch (EOFException e) {
  }
}

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;

bool solve(int[] a, int sum, ref Tuple!(int, int)[] ans)
{
  if (a.length == 0)
    return sum == 0;

  if (sum == 0) {
    ans ~= tuple(cast(int)a.length, cast(int)a.length);
    return solve(a[0 .. $ - 1], sum, ans);
    return true;
  }

  if (sum < 0) {
    if (a.length >= 2 && a[$ - 1] < 0) {
      ans ~= tuple(cast(int)a.length - 1, cast(int)a.length);
      return solve(a[0 .. $ - 2], sum - 2 * a[$ - 1], ans);
    } else {
      ans ~= tuple(cast(int)a.length, cast(int)a.length);
      return solve(a[0 .. $ - 1], sum, ans);
    }
  } else {
    if (a.length >= 2 && a[$ - 1] > 0) {
      ans ~= tuple(cast(int)a.length - 1, cast(int)a.length);
      return solve(a[0 .. $ - 2], sum - 2 * a[$ - 1], ans);
    } else {
      ans ~= tuple(cast(int)a.length, cast(int)a.length);
      return solve(a[0 .. $ - 1], sum, ans);
    }
  }
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!int).array;
    Tuple!(int, int)[] ans;
    if (solve(a, a.sum, ans)) {
      ans.reverse;
      writeln(ans.length);
      foreach (x; ans) {
        writefln("%d %d", x[0], x[1]);
      }
    } else {
      writeln(-1);
    }
  }
}


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

void dfs(int node, int depth, int[][] c, bool[] v, int[][] dp)
{
    v[node] = true;
    auto parent = -1;
    foreach (i; 0 .. c[node].length)
    {
        if (!v[c[node][i]])
        {
            dfs(c[node][i], depth + 1, c, v, dp);
        }
        else
        {
            parent = c[node][i];
        }
    }
    if (depth <= 1)
    {
        auto sum = 0;
        foreach (i; 0 .. c[node].length)
        {
            if (c[node][i] != parent)
            {
                auto child = c[node][i];
                sum += min(dp[child][0], dp[child][1]);
            }
        }
        dp[node][0] = sum;
        dp[node][1] = sum + 1;
        dp[node][2] = int.max;
        return;
    }
    auto sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][1], dp[child][2]);
        }
    }
    dp[node][0] = sum;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][0], dp[child][1]);
        }
    }
    dp[node][1] = sum + 1;
    dp[node][2] = int.max;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][1], dp[child][2]);
        }
    }
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            dp[node][2] = min(dp[node][2], sum - min(dp[child][1], dp[child][2]) + dp[child][1]);
        }
    }
}

void solve(int[][] c)
{
    auto n = cast(int)c.length;
    auto v = new bool[n];
    fill(v, false);
    auto dp = new int[][](n, 3);
    dfs(0, 0, c, v, dp);
    writeln(dp[0][0]);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto c = new int[][n];
        foreach (i; 0 .. n - 1)
        {
            int u, v;
            readf("%d %d\n", &u, &v);
            c[u - 1] ~= v - 1;
            c[v - 1] ~= u - 1;
        }
        solve(c);
    }
    return 0;
}

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

void dfs(int node, int depth, int[][] c, bool[] v, int[][] dp)
{
    v[node] = true;
    auto parent = -1;
    foreach (i; 0 .. c[node].length)
    {
        if (!v[c[node][i]])
        {
            dfs(c[node][i], depth + 1, c, v, dp);
        }
        else
        {
            parent = c[node][i];
        }
    }
    if (depth <= 1)
    {
        auto sum = 0;
        foreach (i; 0 .. c[node].length)
        {
            if (c[node][i] != parent)
            {
                auto child = c[node][i];
                sum += min(dp[child][0], dp[child][1]);
            }
        }
        dp[node][0] = sum;
        dp[node][1] = sum + 1;
        dp[node][2] = int.max;
        return;
    }
    auto sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][1], dp[child][2]);
        }
    }
    dp[node][0] = sum;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][0], dp[child][1]);
        }
    }
    dp[node][1] = sum + 1;
    dp[node][2] = int.max;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][1], dp[child][2]);
        }
    }
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            dp[node][2] = min(dp[node][2], sum - min(dp[child][1], dp[child][2]) + dp[child][1]);
        }
    }
    if (depth > 2 && dp[node][2] == int.max)
    {
        dp[node][2] = 1;
    }
}

void solve(int[][] c)
{
    auto n = cast(int)c.length;
    auto v = new bool[n];
    fill(v, false);
    auto dp = new int[][](n, 3);
    dfs(0, 0, c, v, dp);
    writeln(dp[0][0]);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto c = new int[][n];
        foreach (i; 0 .. n - 1)
        {
            int u, v;
            readf("%d %d\n", &u, &v);
            c[u - 1] ~= v - 1;
            c[v - 1] ~= u - 1;
        }
        solve(c);
    }
    return 0;
}

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

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

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

        g[x] ~= y;
        g[y] ~= x;
    }

    auto ans = 0;
    int dfs(int v, int fr, int dst) {
        int mxc = 0, mnc = 10 ^^ 9;
        foreach (u; g[v]) {
            if (u == fr) continue;
            auto c = dfs(u, v, dst+1);
            mxc = max(mxc, c);
            mnc = min(mnc, c);
        }

        auto curd = min(dst, mnc+1);

        debug { writeln(v, ' ', curd); }

        if (mxc > 2) {
            debug { v.writeln; }
            ++ans;
            curd = 1;
        }

        return curd;
    }

    dfs(1, -1, 0);

    ans.writeln;
}

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

void dfs(int node, int depth, int[][] c, bool[] v, int[][] dp)
{
    v[node] = true;
    auto parent = -1;
    foreach (i; 0 .. c[node].length)
    {
        if (!v[c[node][i]])
        {
            dfs(c[node][i], depth + 1, c, v, dp);
        }
        else
        {
            parent = c[node][i];
        }
    }
    if (depth <= 1)
    {
        auto sum = 0;
        foreach (i; 0 .. c[node].length)
        {
            if (c[node][i] != parent)
            {
                auto child = c[node][i];
                sum += min(dp[child][0], dp[child][1]);
            }
        }
        dp[node][0] = sum;
        dp[node][1] = sum + 1;
        dp[node][2] = int.max;
        return;
    }
    auto sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][1], dp[child][2]);
        }
    }
    dp[node][0] = sum;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += min(dp[child][0], dp[child][1]);
        }
    }
    dp[node][1] = sum + 1;
    dp[node][2] = int.max;
    sum = 0;
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            sum += dp[child][2];
        }
    }
    foreach (i; 0 .. c[node].length)
    {
        if (c[node][i] != parent)
        {
            auto child = c[node][i];
            dp[node][2] = min(dp[node][2], sum - dp[child][2] + dp[child][1]);
        }
    }
    if (dp[node][2] == int.max)
    {
        dp[node][2] = 1;
    }
}

void solve(int[][] c)
{
    auto n = cast(int)c.length;
    auto v = new bool[n];
    fill(v, false);
    auto dp = new int[][](n, 3);
    dfs(0, 0, c, v, dp);
    writeln(dp[0][0]);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto c = new int[][n];
        foreach (i; 0 .. n - 1)
        {
            int u, v;
            readf("%d %d\n", &u, &v);
            c[u - 1] ~= v - 1;
            c[v - 1] ~= u - 1;
        }
        solve(c);
    }
    return 0;
}

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

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

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

        --x, --y;
        g[x] ~= y;
        g[y] ~= x;
    }

    auto dp = new int[][] (4, n);
    foreach (ref rw; dp) rw[] = -1;
    int dfs(int v, int fr, int dst) {
        int run(int d) {
            int ans = 0;
            foreach (u; g[v]) {
                if (u == fr) continue;
                ans += dfs(u, v, d);
            }
            return ans;
        }

        if (dp[dst][v] != -1) return dp[dst][v];

        if (v != 0 && dst <= 2 && g[v].length == 1) return 0;

        if (dst == 0 || dst == 1) {
            dp[dst][v] = run(dst+1);
        } else if (dst == 2) {
            auto ans0 = 1 + run(2);
            auto ans1 = g[v].length - 1 + run(1);
            auto ans2 = run(3);

            dp[dst][v] = [ans0, ans1, ans2].minElement.to!int;

            debug { writeln(v, ' ', dst, ' ', dp[dst][v]); }
        } else { //dst == 3
            auto ans0 = 1 + run(2);
            auto ans1 = g[v].length > 1 ? g[v].length - 1 + run(1) : int.max;

            dp[dst][v] = min(ans0, ans1);
        }

        return dp[dst][v];
    }

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

    ans.writeln;
}

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

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

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

        --x, --y;
        g[x] ~= y;
        g[y] ~= x;
    }

    auto dp = new int[][] (3, n);
    foreach (ref rw; dp) rw[] = -1;
    int dfs(int v, int fr, int dst) {
        int run(int d) {
            int ans = 0;
            foreach (u; g[v]) {
                if (u == fr) continue;
                ans += dfs(u, v, d);
            }
            return ans;
        }

        if (dp[dst][v] != -1) return dp[dst][v];

        if (v != 0 && dst <= 2 && g[v].length == 1) return 0;

        if (dst == 0 || dst == 1) {
            dp[dst][v] = run(dst+1);
        } else {
            auto ans0 = 1 + run(2);
            auto ans1 = g[v].length - 1 + run(1);

            dp[dst][v] = min(ans0, ans1);

            debug { writeln(v, ' ', dst, ' ', min(ans0, ans1)); }
        }

        return dp[dst][v];
    }

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

    ans.writeln;
}

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

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

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

        g[x] ~= y;
        g[y] ~= x;
    }

    auto ans = 0;
    int dfs(int v, int fr, int dst) {
        int mxc = 0;
        foreach (u; g[v]) {
            if (u == fr) continue;
            auto c = dfs(u, v, dst+1);
            mxc = max(mxc, c);
        }

        auto curd = dst;

        debug { writeln(v, ' ', curd); }

        if (mxc > 2) {
            ++ans;
            curd = 1;
        }

        return curd;
    }

    dfs(1, -1, 0);

    ans.writeln;
}


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

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

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

    struct qr { int t, le, r; }
    auto qs = new qr[] (q);
    foreach (i; 0 .. q) {
        readf("%s %s %s", &qs[i].t, &qs[i].le, &qs[i].r);
        readln;
    }

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

    foreach_reverse (cq; qs) {
        foreach (ref e; b) {
            if (e < cq.le || e > cq.r) { continue; }

            if (cq.t == 1) { e = e > cq.le ? e-1 : cq.r; }
            if (cq.t == 2) { e = cq.r - (e - cq.le); }
        }

        debug { b.writeln; }
    }

    int[] ans;
    foreach (e; b) { ans ~= arr[e-1]; }

    ans.writefln!"%(%s %)";
}

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

void main() {
    auto input = readln.split.map!(to!int);
    auto N = input[0];
    auto K = input[1];
    auto M = input[2];
    auto A = readln.split.map!(to!int).array;
    auto Q = K.iota.map!(_ =>readln.split.map!(to!int).array).array;
    auto B = readln.split.map!(x => x.to!int - 1).array;

    Q.reverse();
    int[] ans;

    foreach (b; B) {
        foreach (q; Q) {
            auto l = q[1] - 1;
            auto r = q[2] - 1;

            if (q[0] == 1) {
                if (b == l)
                    b = r;
                else if (b >= l && b <= r)
                    b -= 1;
            } else {
                if (b >= l && b <= r)
                    b = r - b + l;
            }
        }

        ans ~= A[b];
    }

    ans.map!(a => a.to!string).join(" ").writeln;
}

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

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

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

    struct qr { int t, le, r; }
    auto qs = new qr[] (q);
    foreach (i; 0 .. q) {
        readf("%s %s %s", &qs[i].t, &qs[i].le, &qs[i].r);
        readln;
    }

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

    foreach_reverse (cq; qs) {
        foreach (ref e; b) {
            if (e < cq.le || e > cq.r) { continue; }

            if (cq.t == 1) { e = e > 1 ? e-1 : n; }
            if (cq.t == 2) { e = cq.r - (e - cq.le); }
        }

        debug { b.writeln; }
    }

    int[] ans;
    foreach (e; b) { ans ~= arr[e-1]; }

    ans.writefln!"%(%s %)";
}


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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto S = new string[N - K + 1];
      foreach (i; 0 .. N - K + 1) {
        S[i] = readToken();
      }

      auto ans = new string[N];
      foreach (i; 0 .. N) {
        ans[i] ~= cast(char)('A' + i / 26);
        ans[i] ~= cast(char)('a' + i % 26);
        if (i - (K - 1) >= 0 && S[i - (K - 1)] == "NO") {
          ans[i] = ans[i - (K - 1)];
        }
      }

      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln;
    }
  } catch (EOFException e) {
  }
}

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.6.0"
+/

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

string genNewName() {
    static int id = 424;
    id++;
    return "Sugim" ~ id.to!string.map!(x => cast(char)(x-'0'+'a')).array.idup;
}

int main() {
    auto sc = new Scanner(stdin);
    int n, k;
    sc.read(n, k);
    string[] v;
    sc.read(v);
    string[] ans;
    foreach(i; 0..k-1) {
        ans ~= genNewName();
    }
    foreach (s; v) {
        if (s == "YES") {
            ans ~= genNewName();
        } else {
            string u = ans[$-k+1];
            ans ~= u;
        }
    }
    writeln(ans.join(" "));
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

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;
    }
    string[] buf;
    private bool succ() {
        while (!buf.length) {
            if (f.eof) return false;
            buf = f.readln.split;
        }
        return true;
    }
    private bool readSingle(T)(ref T x) {
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                //string or char[10] etc
                x = buf.front;
                buf.popFront;
            } else {
                static if (isStaticArray!T) {
                    //static
                    assert(buf.length == T.length);
                }
                x = buf.map!(to!E).array;
                buf.length = 0;
            }
        } else {
            x = buf.front.to!T;
            buf.popFront;
        }
        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);
        }
    }
}

unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    fout.writeln("1 2 3");
    fout.writeln("ab cde");
    fout.writeln("1.0 1.0 2.0");
    fout.close;
    Scanner sc = new Scanner(File(fileName, "r"));
    int a;
    int[2] b;
    char[2] c;
    string d;
    double e;
    double[] f;
    sc.read(a, b, c, d, e, f);
    assert(a == 1);
    assert(equal(b[], [2, 3]));
    assert(equal(c[], "ab"));
    assert(equal(d, "cde"));
    assert(e == 1.0);
    assert(equal(f, [1.0, 2.0]));
}

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

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.split;
		string [] s;
		foreach (i; 0..n)
		{
			s ~= "" ~
			    cast (char) ('A' + i % 10) ~
			    cast (char) ('a' + i / 10);
		}
		foreach (i; 0..n - k + 1)
		{
			if (a[i] == "NO")
			{
				s[i + k - 1] = s[i];
			}
		}
		writefln ("%-(%s %)", s);
	}
}

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.6.0"
+/

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

string genNewName() {
    static int id = 424;
    id++;
    return "sugim" ~ id.to!string.map!(x => cast(char)(x-'0'+'a')).array.idup;
}

int main() {
    auto sc = new Scanner(stdin);
    int n, k;
    sc.read(n, k);
    string[] v;
    sc.read(v);
    string[] ans;
    foreach(i; 0..k-1) {
        ans ~= genNewName();
    }
    foreach (s; v) {
        if (s == "YES") {
            ans ~= genNewName();
        } else {
            string u = ans[$-k+1];
            ans ~= u;
        }
    }
    writeln(ans.join(" "));
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

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;
    }
    string[] buf;
    private bool succ() {
        while (!buf.length) {
            if (f.eof) return false;
            buf = f.readln.split;
        }
        return true;
    }
    private bool readSingle(T)(ref T x) {
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                //string or char[10] etc
                x = buf.front;
                buf.popFront;
            } else {
                static if (isStaticArray!T) {
                    //static
                    assert(buf.length == T.length);
                }
                x = buf.map!(to!E).array;
                buf.length = 0;
            }
        } else {
            x = buf.front.to!T;
            buf.popFront;
        }
        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);
        }
    }
}

unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    fout.writeln("1 2 3");
    fout.writeln("ab cde");
    fout.writeln("1.0 1.0 2.0");
    fout.close;
    Scanner sc = new Scanner(File(fileName, "r"));
    int a;
    int[2] b;
    char[2] c;
    string d;
    double e;
    double[] f;
    sc.read(a, b, c, d, e, f);
    assert(a == 1);
    assert(equal(b[], [2, 3]));
    assert(equal(c[], "ab"));
    assert(equal(d, "cde"));
    assert(e == 1.0);
    assert(equal(f, [1.0, 2.0]));
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto S = new string[N - K + 1];
      foreach (i; 0 .. N - K + 1) {
        S[i] = readToken();
      }

      auto ans = new string[N];
      foreach (i; 0 .. N) {
        ans[i] ~= cast(char)('a' + i / 26);
        ans[i] ~= cast(char)('a' + i % 26);
        if (i - K >= 0 && S[i - K] == "NO") {
          ans[i] = ans[i - K];
        }
      }

      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln;
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto S = new string[N - K + 1];
      foreach (i; 0 .. N - K + 1) {
        S[i] = readToken();
      }

      auto ans = new string[N];
      foreach (i; 0 .. N) {
        ans[i] ~= cast(char)('a' + i / 26);
        ans[i] ~= cast(char)('a' + i % 26);
        if (i - (K - 1) >= 0 && S[i - (K - 1)] == "NO") {
          ans[i] = ans[i - (K - 1)];
        }
      }

      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln;
    }
  } catch (EOFException e) {
  }
}


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.ascii, std.typecons;

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

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

    auto dp = new int[][] (n, h);
    foreach (i; n.iota) { dp[i][] = -1; }

    int score(int idx, int ch) {
        if (idx == n) { return 0; }
        if (dp[idx][ch] != -1) { return dp[idx][ch]; }

        int h1 = (ch + arr[idx] - 1) % h;
        int h2 = (ch + arr[idx]) % h;

        auto isGood = (int v) => le <= v && v <= r ? 1 : 0;

        auto good1 = isGood(h1);
        auto good2 = isGood(h2);

        return dp[idx][ch] = max(good1 + score(idx+1, h1), good2 + score(idx+1, h2));
    }

    auto ans = score(0, 0);

    ans.writeln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto h = RD!int;
	auto l = RD!int;
	auto r = RD!int+1;
	auto a = RDA!int;

	auto dp = new int[](h);
	dp[] = -1;
	dp[0] = 0;
	foreach (i; 0..n)
	{
		auto ndp = new int[](h);
		ndp[] = -1;
		foreach (j; 0..h)
		{
			if (dp[j] == -1) continue;
			int nc1 = dp[j], nc2 = dp[j];
			auto t1 = (j+a[i]) % h;
			auto t2 = (j+a[i]-1) % h;
			if (inside(t1, l, r))
				++nc1;
			if (inside(t2, l, r))
				++nc2;
			ndp[t1].chmax(nc1);
			ndp[t2].chmax(nc2);
		}
		dp = ndp;
	}

	auto pos = dp.MIN_POS!"a > b";
	writeln(dp[pos]);
	stdout.flush;
	debug readln;
}

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

void main()
{
    auto nhlr = readln.split.to!(int[]);
    auto N = nhlr[0];
    auto H = nhlr[1];
    auto L = nhlr[2];
    auto R = nhlr[3];
    auto as = readln.split.to!(int[]);

    auto DP = new int[][](N, H);
    foreach (ref dp; DP) dp[] = -1;
    int solve(int i, int h) {
        if (i == N) return 0;
        if (DP[i][h] == -1) {
            auto r1 = (h + as[i]) % H;
            auto r2 = (h + as[i] - 1) % H;
            DP[i][h] = max(
                solve(i+1, r1) + (L <= r1 && r1 <= R ? 1 : 0),
                solve(i+1, r2) + (L <= r2 && r2 <= R ? 1 : 0)
            );
        }
        return DP[i][h];
    }
    writeln(solve(0, 0));
}


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;

bool trysolve(bool[] vis, int n, long s)
{
  long rs = 0;
  foreach (i ; 0 .. n) {
    if (!vis[i])
      rs += i + 1;
  }
  foreach (i ; n .. 100) {
    if (vis[i])
      return false;
  }
  return rs == s;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int m, s;
    readf!" %d %d "(m, s);
    auto a = readln.splitter.map!(to!int).array;
    auto vis = new bool[100];
    foreach (x ; a) {
      vis[x - 1] = true;
    }
    bool good = false;
    foreach (n ; (a.length + 1).to!int .. 100 + 1) {
      if (trysolve(vis, n, s)) {
        good = true;
        break;
      }
    }
    writeln(good ? "YES" : "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;
import std.functional;

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

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 solve() {
    int m, s;
    readf!" %d %d "(m,s);
    bool good = false;
    auto b = readarray!int;
    auto mb = maxElement(b);
    auto sb = sum(b);
    auto curS = sum(iota(mb + 1));
    while (curS <= sb + s) {
        if (curS == sb + s) {
            good = true;
            break;
        }
        mb++;
        curS += mb;
    }
    if (good)
        writeln("yes");
    else
        writeln("no");
}

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

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

bool solve (int m, int s, int [] a)
{
	auto hi = a.maxElement;
	auto vis = new bool [hi + 1];
	foreach (ref c; a)
	{
		if (vis[c])
		{
			return false;
		}
		vis[c] = true;
	}
	foreach (i; 1..hi + 1)
	{
		if (!vis[i])
		{
			s -= i;
		}
	}
	while (s > 0)
	{
		hi += 1;
		s -= hi;
	}
	return (s == 0);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int m, s;
		readf !(" %s %s") (m, s);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (m, s, a) ? "YES" : "NO");
	}
}

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;

bool trysolve(bool[] vis, int n, long s)
{
  long rs = 0;
  foreach (i ; 0 .. n) {
    if (!vis[i])
      rs += i + 1;
  }
  foreach (i ; n .. 50) {
    if (vis[i])
      return false;
  }
  return rs == s;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int m, s;
    readf!" %d %d "(m, s);
    auto a = readln.splitter.map!(to!int).array;
    auto vis = new bool[50];
    foreach (x ; a) {
      vis[x - 1] = true;
    }
    bool good = false;
    foreach (n ; (a.length + 1).to!int .. 50 + 1) {
      if (trysolve(vis, n, s)) {
        good = true;
        break;
      }
    }
    writeln(good ? "YES" : "NO");
  }
}

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) {
    int m, s;
    readf!" %d %d "(m, s);
    auto a = readln.splitter.map!(to!int).array;
    auto vis = new bool[50];
    int mx = 0;
    foreach (x ; a) {
      vis[x - 1] = true;
      mx = max(mx, x);
    }
    long rs = 0;
    bool good = false;
    foreach (i ; 0 .. vis.length) {
      if (!vis[i])
        rs += i + 1;
      if (i + 1 >= mx && rs == s) {
        good = true;
        break;
      }
    }
    writeln(s == rs ? "YES" : "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)); }

enum LIM = 10^^6 + 100;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const X = readLong();
      const Y = readLong();
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }
      A.sort;

      auto lpf = iota(LIM).array;
      foreach (p; 2 .. LIM) {
        if (lpf[p] == p) {
          for (int n = 2 * p; n < LIM; n += p) {
            chmin(lpf[n], p);
          }
        }
      }

      auto freq = new int[LIM];
      foreach (i; 0 .. N) {
        for (int a = A[i]; a > 1; ) {
          const p = lpf[a];
          ++freq[p];
          do {
            a /= p;
          } while (a % p == 0);
        }
      }

      long ans = N * X;
      foreach (p; 2 .. LIM) {
        if (ans > (N - freq[p]) * min(X, Y)) {
          long cost;
          foreach (i; 0 .. N) {
            const r = A[i] % p;
            if (r != 0) {
              cost += min(X, Y * (p - r));
              if (ans < cost) {
                break;
              }
            }
          }
          chmin(ans, cost);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.stdio;
import std.file;
import std.algorithm;
import std.typecons;
import std.math;
import std.random;
import std.container.rbtree;
import std.string;
import std.conv;
import std.bigint;

const int N = 1000001;

void main()
{
	int n;
	long x, y;
	scanf("%d", &n);
	scanf("%lld", &x);
	scanf("%lld", &y);

	int[] list = new int[n];

	foreach(i; 0..n)
		scanf("%d", &list[i]);

	bool[] pr = new bool[N];

	list.sort();

	foreach(i; 2..N - 1)
		pr[i] = true;

	for (int i = 2; i * i < N; i++)
		if (pr[i])
			for (int j = i * i; j < N; j += i)
				pr[j] = false;

	long[] psum = new long[N];
	psum[0] = list[0];

	foreach(i; 1..n)
		psum[i] = list[i] + psum[i - 1];

	long get(int l, int r)
	{
		return psum[r] - (l > 0 ? psum[l - 1] : 0);
	}

	long ans = x * n;

	for (long d = 2; d < N; d++)
		if (pr[cast(int)d])
		{
			long curr = 0;

			for (int j = 0; j < n; )
			{
				long k = list[j] / d - (list[j] % d == 0 ? 1 : 0);

				int L = j, R = n;

				while (L < R - 1)
				{
					int M = (L + R) / 2;
					if (list[M] > k * d + d)
						R = M;
					else
						L = M;
				}

				R = L;
				int l = j, r = R, pos = -1;
				while (l <= r)
				{
					int m = (l + r) / 2;
					if ((k * d + d - list[m]) * y <= x)
						r = m - 1;
					else
						pos = m, l = m + 1;
				}

				L = j;
				j = R + 1;

				if (pos != -1)
					curr += x * (pos - L + 1) + y * ((k * d + d) * (R - pos) - get(pos + 1, R));
				else
					curr += y * ((k * d + d) * (R - L + 1) - get(L, R));
				k++;
			}

			ans = min(ans, curr);
		}

	writeln(ans);

	//stdin.readln();
	//stdin.readln();
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!int).array;
    A.sort();

    const int MAX = 10^^6 + 1;
    const int border = ((X - 1) / Y).to!int;

    auto cnt = new long[](MAX * 2);
    auto cntsum = new long[](MAX * 2);

    foreach (i; 0..N)
        cnt[A[i]] += 1;
    foreach (i; 0..MAX*2-1)
        cnt[i + 1] += cnt[i];
    foreach (i; 0..MAX*2-1)
        cntsum[i + 1] += cntsum[i] + cnt[i + 1];

    long ans = 1L << 59;

    foreach (g; 2..MAX) {
        long tmp = 0;
        for (int l = 1; l < MAX; l += g) {
            int r = l + g - 1;
            int m = max(l, r - border);
            long y = Y * (cntsum[r - 1] - cntsum[m - 1] - cnt[m - 1] * (r - m));
            long x = X * (cnt[m - 1] - cnt[l - 1]);
            tmp += x + y;
        }
        ans = min(ans, tmp);
    }

    debug {
        cnt[0..20].writeln;
        cntsum[0..20].writeln;
    }

    ans.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!long).array;

    const int MAX = 10^^6 + 1;
    auto table = new long[MAX];
    fill(table, true);
    table[0] = table[1] = false;
    long[] P;

    for (int i = 2; i * i < MAX; ++i) {
        if (table[i]) {
            P ~= i;
            for (int j = i + i; j * j < MAX; j += i)
                table[j] = false;
        }
    }

    long ans = 1L << 59;

    foreach (p; P) {
        long tmp = 0;
        foreach (a; A)
            tmp += min(X, (a % p) * Y);
        ans = min(ans, tmp);
    }

    long tmp = 0;
    long m = A.reduce!max;
    foreach (a; A)
        tmp += min(X, (m - a) * Y);
    ans = min(ans, tmp);

    ans.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!int).array;
    A.sort();

    const int MAX = 10^^6 + 1;
    const int border = ((X - 1) / Y + 1).to!int;

    auto cnt = new long[](MAX * 2);
    auto cntsum = new long[](MAX * 2);

    foreach (i; 0..N)
        cnt[A[i]] += 1;
    foreach (i; 0..MAX*2-1)
        cnt[i + 1] += cnt[i];
    foreach (i; 0..MAX*2-1)
        cntsum[i + 1] += cntsum[i] + cnt[i + 1];

    long ans = 1L << 59;

    foreach (g; 2..MAX) {
        long tmp = 0;
        for (int l = 1; l < MAX; l += g) {
            int r = l + g - 1;
            int m = max(l, r - border);
            long y = Y * (cntsum[r - 1] - cntsum[m - 1] - cnt[m - 1] * (r - m));
            long x = X * (cnt[m - 1] - cnt[l - 1]);
            tmp += x + y;
        }
        ans = min(ans, tmp);
    }

    debug {
        cnt[0..20].writeln;
        cntsum[0..20].writeln;
    }

    ans.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!long).array;

    if (A.map!(a => a == 1).all) {
        writeln(min(X, Y) * N);
        return;
    }

    const int MAX = 10^^6 + 1;
    auto table = new long[MAX];
    fill(table, true);
    table[0] = table[1] = false;
    long[] P;

    for (int i = 2; i * i < MAX; ++i) {
        if (table[i]) {
            P ~= i;
            for (int j = i + i; j * j < MAX; j += i)
                table[j] = false;
        }
    }

    long ans = 1L << 59;

    foreach (p; P) {
        long tmp = 0;
        foreach (a; A)
            tmp += min(X, (p - a % p) * Y);
        ans = min(ans, tmp);
    }

    long tmp = 0;
    long m = A.reduce!max;
    foreach (a; A)
        tmp += min(X, (m - a) * Y);
    ans = min(ans, tmp);

    ans.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!long).array;

    if (A.map!(a => a == 1).all) {
        writeln(min(X, Y) * N);
        return;
    }

    const int MAX = 10^^6 + 1;
    auto table = new long[MAX];
    fill(table, true);
    table[0] = table[1] = false;
    long[] P;

    for (int i = 2; i * i < MAX; ++i) {
        if (table[i]) {
            P ~= i;
            for (int j = i + i; j * j < MAX; j += i)
                table[j] = false;
        }
    }

    long ans = 1L << 59;

    foreach (p; P) {
        long tmp = 0;
        foreach (a; A)
            tmp += min(X, (a % p == 0 ? 0 : p - a % p) * Y);
        ans = min(ans, tmp);
    }

    long tmp = 0;
    long m = A.reduce!max;
    foreach (a; A)
        tmp += min(X, (m - a) * Y);
    ans = min(ans, tmp);

    ans.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0].to!int;
    auto X = s[1];
    auto Y = s[2];
    auto A = readln.split.map!(to!long).array;

    if (A.map!(a => a == 1).all) {
        writeln(min(X, Y) * N);
        return;
    }

    const int MAX = 10^^6 + 1;
    auto table = new long[MAX];
    fill(table, true);
    table[0] = table[1] = false;
    long[] P;

    for (int i = 2; i * i < MAX; ++i) {
        if (table[i]) {
            P ~= i;
            for (int j = i + i; j * j < MAX; j += i)
                table[j] = false;
        }
    }

    long ans = 1L << 59;

    foreach (p; P) {
        long tmp = 0;
        foreach (a; A)
            tmp += min(X, (a % p) * Y);
        ans = min(ans, tmp);
    }

    long tmp = 0;
    long m = A.reduce!max;
    foreach (a; A)
        tmp += min(X, (m - a) * Y);
    ans = min(ans, tmp);

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const X = readLong();
      const Y = readLong();
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }
      A.sort;

      const lim = A.maxElement + 1;
      auto lpf = iota(lim).array;
      foreach (p; 2 .. lim) {
        if (lpf[p] == p) {
          for (int n = 2 * p; n < lim; n += p) {
            chmin(lpf[n], p);
          }
        }
      }

      auto freq = new int[lim];
      foreach (i; 0 .. N) {
        for (int a = A[i]; a > 1; ) {
          const p = lpf[a];
          ++freq[p];
          do {
            a /= p;
          } while (a % p == 0);
        }
      }

      long ans = N * X;
      foreach (p; 2 .. lim) {
        if (ans > (N - freq[p]) * min(X, Y)) {
          long cost;
          foreach (i; 0 .. N) {
            const r = A[i] % p;
            if (r != 0) {
              cost += min(X, Y * (p - r));
              if (ans < cost) {
                break;
              }
            }
          }
          chmin(ans, cost);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

immutable int steps = 4500;

bool ask (long lo, long hi)
{
	writeln (lo, " ", hi);
	stdout.flush ();
	auto s = readln.strip;
	return s == "Yes";
}

void main ()
{
	long n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		long lo = 1;
		long hi = n;
		foreach (step; 0..steps)
		{
			if (hi - lo < 50)
			{
				long me = uniform (lo, hi + 1);
				if (ask (me, me))
				{
					break;
				}
			}
			else
			{
				long me = (lo + hi) >> 1;
				if (ask (lo, me))
				{
					hi = me;
				}
				else
				{
					lo = me + 1;
				}
			}
			lo = max (1, lo - k);
			hi = min (n, hi + k);
		}
	}
}


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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n   = RD!int;
	auto ans = new bool[](n);
	foreach (i; 0..n)
	{
		auto s1 = RD!string;
		auto s2 = RD!string;
		bool ok = true;
		char last;
		size_t pos;
		foreach (c; s2)
		{
			if (pos >= s1.length)
			{
				if (c == last) continue;
				else
				{
					ok = false;
					break;
				}
			}
			if (c == s1[pos])
			{
				++pos;
				last = c;
				continue;
			}
			else if (c == last) continue;
			else
			{
				ok = false;
				break;
			}
		}
		if (pos < s1.length) ok = false;

		ans[i] = ok;
	}

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

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main() {
	readln.strip.to!int.iota.each!(_ => {
		auto u = [readln.group, readln.group];
		writeln(u[0].count == u[1].count && zip(u[0], u[1])
		.all!"a[0][0] == a[1][0] && a[0][1] <= a[1][1]" ? "YES" : "NO");
	}());
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	readln;
	foreach (test; 0..tests)
	{
		auto s = readln.strip ~ '$';
		auto t = readln.strip ~ '$';
		int i = 0;
		int j = 0;
		for ( ; i < s.length; i++)
		{
			if (s[i] == t[j])
			{
				j += 1;
				continue;
			}
			while (i > 0 && s[i - 1] == t[j])
			{
				j += 1;
			}
			if (s[i] == t[j])
			{
				j += 1;
				continue;
			}
			else
			{
				break;
			}
		}
		writeln ((i == s.length && j == t.length) ? "YES" : "NO");
	}
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

int[] va,vb;
void rec(ref int[] a,ref string s){
    a=null;
    for(int i=0;i<s.length;){
        int c=0;
        while(i+c<s.length && s[i+c]==s[i])
            c++;
        a~=c;
        i+=c;
    }
}
void main(){
    int n=readln.chomp.to!int;
    string a,b;
    bool r;
    while(n--){
        va=[];
        vb=[];
        a=readln.strip;
        b=readln.strip;
        r=true;
        if(b.length>=a.length){
            rec(va,a);
            rec(vb,b);
            debug{
                writeln(va);
                writeln(vb);
            }
            if(va.length==vb.length){
                int j=0,k=0;
                for(int i=0;i<va.length;i++){
                    j+=va[i];
                    k+=vb[i];
                    if(a[j-1]!=b[k-1] || va[i]>vb[i]){
                        r=false;
                        break;
                    }
              }
            }else r=false;
        }else r=false;
        if(r) writeln("YES");
        else writeln("NO");
    }
}

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

void main ()
{
	foreach (test; 0..readln.strip.to !(int))
	{
		auto s = readln.strip.group.array;
		auto t = readln.strip.group.array;
		writeln (s.length == t.length && zip (s, t).all
		    !(c => c[0][0] == c[1][0] && c[0][1] <= c[1][1]) ?
		    "YES" : "NO");
	}
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

int[] va,vb;
void rec(ref int[] a,ref string s){
    a=null;
    for(int i=0;i<s.length;){
        int c=0;
        while(i+c<s.length && s[i+c]==s[i])
            c++;
        a~=c;
        i+=c;
    }
}
void main(){
    int n=readln.chomp.to!int;
    string a,b;
    bool r;
    while(n--){
        va=[];
        vb=[];
        a=readln.strip;
        b=readln.strip;
        r=true;
        if(b.length>=a.length){
            rec(va,a);
            rec(vb,b);
            if(va.length==vb.length){
                int j=0,k=0;
                for(int i=0;i<va.length;i++){
                    if(a[i+j]!=b[i+k] || va[j]>vb[k]){
                        r=false;
                        break;
                    }
                    j+=va[i]-1;
                    k+=vb[i]-1;
              }
            }else r=false;
        }else r=false;
        if(r) writeln("YES");
        else writeln("NO");
    }
}


import std.stdio;
import core.stdc.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int max = 0;
	int carriage = 0;
	foreach (int i; 0..n)
	{
		int a, b;
		scanf("%d %d", &a, &b);
		carriage += (b - a);
		if (carriage > max) max = carriage;
	}
	printf("%d", max);
    return 0;
}

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

int[] scanLeft(int[][] arr, int[] acc = [0]){
  if(!arr.length) return acc;
  return scanLeft(arr[1..$], acc ~ (acc[$-1] - arr[0][0] + arr[0][1]));
}

void main(){
  auto n = readln.chomp.to!int;
  int[][] arr;
  foreach(i; 0..n){ arr ~= readln.chomp.split(" ").map!(to!int).array; }
  scanLeft(arr).reduce!max.writeln;
}

import std.stdio;

void main()
{
    readln; //discard first line

    auto maxPassengers = -1;

    auto numPassengers = 0;

    uint numLeaved, numEntered;

    while(!stdin.eof)
    {
        readf("%s %s\n", &numLeaved, &numEntered);

        numPassengers += numEntered - numLeaved;

        if (numPassengers > maxPassengers) maxPassengers = numPassengers;
    }

    maxPassengers.writeln;
}

import std.stdio, std.algorithm;

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

    int result = 0;
    int inside = 0;
    for (int i = 0; i < n; ++i) {
        int a, b;
        readf("%d %d\n", &a, &b);

        inside -= a;
        inside += b;

        result = max(result, inside);
    }

    writefln("%d", result);
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,a,b,v;
    scanf("%d",&n);
    uint ans=0;
    while(n--){
        scanf("%d %d",&a,&b);
        v+=b-a;
        ans=max(ans,v);
    }
    writeln(ans);
}

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

void main() {
  readln;

  int ans = 0;
  int cap = 0;
  foreach (string s; stdin.lines) {
    auto pas = s.chomp.split.map!(to!int).array;
    cap -= pas[0];
    cap += pas[1];
    if (cap > ans) ans = cap;
  }

  ans.writeln;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,a,b;
    scanf("%d",&n);
    uint ans=0;
    while(n--){
        scanf("%d %d",&a,&b);
        ans=max(ans,ans+b-a);
    }
    writeln(ans);
}

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

void main() {
  readln;

  int ans = 0;
  int cap = 0;
  foreach (string s; stdin.lines) {
    auto pas = s.chomp.split.map!(to!int).array;
    pas.writeln;
    cap -= pas[0];
    cap += pas[1];
    cap.writeln;
    if (cap > ans) ans = cap;
  }

  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;

long fun (long [] b)
{
	auto n = b.length;
	long lo = 0;
	long res = long.min / 2;
	long cur = 0;
	foreach (i; 0..n)
	{
		cur += b[i] * ((i & 1) ? -1 : +1);
		debug {write (cur, " ");}
		if (i & 1)
		{
			lo = min (lo, cur);
		}
		res = max (res, cur - lo);
	}
	debug {writeln ();}
	return res;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(long)).array;
		auto b = new long [n - 1];
		foreach (i; 1..n)
		{
			b[i - 1] = abs (a[i] - a[i - 1]);
		}
		writeln (max (fun (b), fun (b[1..$])));
	}
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = new long[](N-1);
    foreach (i; 0..N-1) {
        B[i] = abs(A[i] - A[i+1]);
    }

    long ans = B[0];
    long tmp = B[0];
    foreach (i; 1..N-1) {
        tmp += (i % 2 == 0) ? B[i] : -B[i];
        ans = max(ans, tmp);
        if (tmp < 0) tmp = 0;
    }

    if (N <= 2) {
        ans.writeln;
        return;
    }

    ans = max(ans, B[1]);
    tmp = B[1];
    foreach (i; 2..N-1) {
        tmp += (i % 2 == 1) ? B[i] : -B[i];
        ans = max(ans, tmp);
        if (tmp < 0) tmp = 0;
    }

    ans.writeln;
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = new long[](N-1);
    foreach (i; 0..N-1) {
        B[i] = abs(A[i] - A[i+1]);
    }

    long ans = B[0];
    long tmp = B[0];
    foreach (i; 1..N-1) {
        tmp += (i % 2 == 0) ? B[i] : -B[i];
        ans = max(ans, tmp);
        if (ans < 0) tmp = 0;
    }

    if (N <= 2) {
        ans.writeln;
        return;
    }

    ans = max(ans, B[1]);
    tmp = B[1];
    foreach (i; 2..N-1) {
        tmp += (i % 2 == 1) ? B[i] : -B[i];
        ans = max(ans, tmp);
        if (ans < 0) tmp = 0;
    }

    ans.writeln;
}


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

int value (string s)
{
	int cur = 0;
	int low = 0;
	foreach (const ref char c; s)
	{
		if (c == '(')
		{
			cur += 1;
		}
		else
		{
			cur -= 1;
		}
		low = min (low, cur);
	}
	if (low < 0 && low < cur)
	{
		return int.min / 4;
	}
	return cur;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int [] v;
		readln;
		foreach (i; 0..n)
		{
			auto s = readln.strip;
			v ~= value (s);
		}
		sort (v);

		int res = 0;
		while (v.length >= 2)
		{
			auto x = v.front;
			auto y = v.back;
			if (x + y < 0)
			{
				v.popFront ();
			}
			else if (x + y > 0)
			{
				v.popBack ();
			}
			else if (x + y == 0)
			{
				res += 1;
				v.popFront ();
				v.popBack ();
			}
		}
		writeln (res);
	}
}

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

void main() {
    int n;
    scan(n);

    auto b = iota(n).map!(i => readln.chomp).array;

    int checkLeft(string s) {
        int h;

        foreach (ch ; s) {
            if (ch == '(') {
                h++;
            }
            else {
                h--;

                if (h < 0) {
                    return -1;
                }
            }
        }

        return h;
    }

    int checkRight(string s) {
        int h;

        foreach_reverse (ch ; s) {
            if (ch == ')') {
                h++;
            }
            else {
                h--;

                if (h < 0) {
                    return -1;
                }
            }
        }

        return h;
    }

    auto cnt = new int[](5 * 10^^5 + 1);
    int perfect;

    foreach (s ; b) {
        int h = checkLeft(s);
        if (h > 0) {
            cnt[h]++;
        }
        if (h == 0) {
            perfect++;
        }
    }

    int ans;

    foreach (s ; b) {
        int h = checkRight(s);
        if (h > 0 && cnt[h] > 0) {
            ans++;
            cnt[h]--;
        }
    }

    ans += perfect / 2;

    writeln(ans);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


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

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

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

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

int N;
int[] A, B;

int Ask(int u, int v) {
  writefln("? %s %s", u + 1, v + 1);
  stdout.flush;
  const res = readInt();
  if (res == -1) {
    import core.stdc.stdlib;
    exit(0);
  }
  return res - 1;
}
void Answer(int r) {
  writefln("! %s", r + 1);
  stdout.flush;
  import core.stdc.stdlib;
  exit(0);
}

int[][] G;
int[] sz;

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

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

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

  int r;
  for (int u = 0; ; ) {
    int vm = -1;
    foreach (i; G[u]) {
      const v = A[i] ^ B[i] ^ u;
      if (vm == -1 || sz[vm] < sz[v]) {
        vm = v;
      }
    }
    if (vm == -1 || sz[u] >= 2 * sz[vm]) {
      r = u;
      break;
    } else {
      sz[u] -= sz[vm];
      sz[vm] += sz[u];
      u = vm;
    }
  }
  debug {
    writeln("r = ", r);
    writeln("sz = ", sz);
  }

  for (int u = r, p = -1; ; ) {
    int[] vs;
    foreach (i; G[u]) {
      const v = A[i] ^ B[i] ^ u;
      if (v != p) {
        vs ~= v;
      }
    }
    vs.sort!((v, w) => (sz[v] > sz[w]));
    const vsLen = cast(int)(vs.length);
    int vm = -1;
    foreach (j; 0 .. vsLen / 2) {
      const res = Ask(vs[j * 2], vs[j * 2 + 1]);
      if (res != u) {
        vm = res;
        goto found;
      }
    }
    if (vsLen % 2 != 0) {
      const res = Ask(vs[vsLen - 1], u);
      if (res != u) {
        vm = res;
        goto found;
      }
    }
    Answer(u);
   found:{}
    p = u;
    u = vm;
  }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto a = new bool [int] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			a[u][v] = true;
			a[v][u] = true;
		}
		auto b = new int [n];
		b[] = 1;
		int res = -1;
		while (sum (b) > 1)
		{
			int v = 0;
			while (!b[v] || a[v].length != 1)
			{
				v += 1;
			}
			int u = v + 1;
			while (!b[u] || a[u].length != 1)
			{
				u += 1;
			}
			writefln !("? %s %s") (u + 1, v + 1);
			stdout.flush ();

			int w;
			readf !(" %s") (w);
			w -= 1;
			if (w == u)
			{
				res = u;
				break;
			}
			if (w == v)
			{
				res = v;
				break;
			}

			b[u] = 0;
			foreach (x, _; a[u])
			{
				a[x].remove (u);
			}
			b[v] = 0;
			foreach (x, _; a[v])
			{
				a[x].remove (v);
			}
		}

		if (res == -1)
		{
			res = 0;
			while (!b[res])
			{
				res += 1;
			}
		}
		writefln !("! %s") (res + 1);
		stdout.flush ();
	}
}

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

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

void main() {
  int n;
  Mt19937 rnd;
  readf (" %d", &n);
  auto e = new int[][n];
  foreach (k; 1 .. n) {
    int i, j;
    readf (" %d %d", &i, &j);
    --i; --j;
    e[i] ~= j;
    e[j] ~= i;
  }
  auto b = new bool[n];
  auto u = new int[n];
  auto v = new int[n];
  auto h = new int[n];
  auto parent = new int[n];
  int tm;
  bool child (int c, int p) {
    if (c == p) return true;
    return u[p] < u[c] && v[c] < v[p];
  }
  void go (int i, int ht) {
    u[i] = ++tm;
    h[i] = ht;
    foreach (j; e[i]) {
      if (parent[j] == -2 && !b[j]) {
        parent[j] = i;
        go (j, ht + 1);
      }
    }
    v[i] = ++tm;
  }
  int fv (const int s) {
    parent[] = -2;
    h[] = -1;
    parent[s] = -1;
    go (s, 0);
    int best = s;
    foreach (i; 0 .. n) if (h[best] < h[i]) best = i;
    return best;
  }
  while (true) {
    int i, j;
    int[] l;
    foreach (o; 0 .. n) if (!b[o]) l ~= o;
    debug stderr.writeln (l);
    int t = l.length.to!int;
    if (t == 1) {
      writeln ("! ", l.front + 1);
      return;
    }
    const int s = l[uniform (0, t)];
    i = fv (s);
    j = fv (i);
    /*
    i = uniform (1, t);
    j = uniform (0, i);
    i = l[i];
    j = l[j];
    */
    writeln ("? ", i + 1, ' ', j + 1);
    stdout.flush ();
    int p;
    readf (" %d", &p);
    --p;
    parent[] = -2;
    parent[p] = -1;
    debug stderr.writeln ("u = ", u);
    debug stderr.writeln ("v = ", v);
    tm = 0;
    go (p, 0);
    foreach (x; l) {
      if (x == p) continue;
      if (i != p && (child (x, i) || child (i, x))) b[x] = true;
      if (j != p && (child (x, j) || child (j, x))) b[x] = true;
    }
    parent[] = -2;
    parent[p] = -1;
    tm = 0;
    go (p, 0);
    b[] = true;
    b[p] = false;
    foreach (o; 0 .. n) if (parent[o] >= 0) b[o] = false;
  }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto a = new bool [int] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			a[u][v] = true;
			a[v][u] = true;
		}
		auto b = new int [n];
		b[] = 1;
		int res = -1;
		while (sum (b) > 1)
		{
			writeln (a);
			int v = 0;
			while (!b[v] || a[v].length != 1)
			{
				v += 1;
			}
			int u = v + 1;
			while (!b[u] || a[u].length != 1)
			{
				u += 1;
			}
			writefln !("? %s %s") (u + 1, v + 1);
			stdout.flush ();

			int w;
			readf !(" %s") (w);
			w -= 1;
			if (w == u)
			{
				res = u;
				break;
			}
			if (w == v)
			{
				res = v;
				break;
			}

			b[u] = 0;
			foreach (x, _; a[u])
			{
				a[x].remove (u);
			}
			b[v] = 0;
			foreach (x, _; a[v])
			{
				a[x].remove (v);
			}
		}

		if (res == -1)
		{
			res = 0;
			while (!b[res])
			{
				res += 1;
			}
		}
		writefln !("! %s") (res + 1);
		stdout.flush ();
	}
}

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

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

void main() {
  int n;
  Mt19937 rnd;
  readf (" %d", &n);
  auto e = new int[][n];
  foreach (k; 1 .. n) {
    int i, j;
    readf (" %d %d", &i, &j);
    --i; --j;
    e[i] ~= j;
    e[j] ~= i;
  }
  auto b = new bool[n];
  auto u = new int[n];
  auto v = new int[n];
  auto parent = new int[n];
  int tm;
  bool child (int c, int p) {
    if (c == p) return true;
    return u[p] < u[c] && v[c] < v[p];
  }
  void go (int i) {
    u[i] = ++tm;
    foreach (j; e[i]) {
      if (parent[j] == -2 && !b[j]) {
        parent[j] = i;
        go (j);
      }
    }
    v[i] = ++tm;
  }
  while (true) {
    int i, j;
    int[] l;
    foreach (o; 0 .. n) if (!b[o]) l ~= o;
    debug stderr.writeln (l);
    int t = l.length.to!int;
    if (t == 1) {
      writeln ("! ", l.front + 1);
      return;
    }
    i = uniform (1, t);
    j = uniform (0, i);
    i = l[i];
    j = l[j];
    writeln ("? ", i + 1, ' ', j + 1);
    stdout.flush ();
    int p;
    readf (" %d", &p);
    --p;
    parent[] = -2;
    parent[p] = -1;
    debug stderr.writeln ("u = ", u);
    debug stderr.writeln ("v = ", v);
    tm = 0;
    go (p);
    foreach (x; l) {
      if (x == p) continue;
      if (i != p && (child (x, i) || child (i, x))) b[x] = true;
      if (j != p && (child (x, j) || child (j, x))) b[x] = true;
    }
  }
}

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

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

void main() {
  int n;
  Mt19937 rnd;
  readf (" %d", &n);
  auto e = new int[][n];
  foreach (k; 1 .. n) {
    int i, j;
    readf (" %d %d", &i, &j);
    --i; --j;
    e[i] ~= j;
    e[j] ~= i;
  }
  auto b = new bool[n];
  auto u = new int[n];
  auto v = new int[n];
  auto parent = new int[n];
  int tm;
  bool child (int c, int p) {
    if (c == p) return true;
    return u[p] < u[c] && v[c] < v[p];
  }
  void go (int i) {
    u[i] = ++tm;
    foreach (j; e[i]) {
      if (parent[j] == -2 && !b[j]) {
        parent[j] = i;
        go (j);
      }
    }
    v[i] = ++tm;
  }
  while (true) {
    int i, j;
    int[] l;
    foreach (o; 0 .. n) if (!b[o]) l ~= o;
    debug stderr.writeln (l);
    int t = l.length.to!int;
    if (t == 1) {
      writeln ("! ", l.front + 1);
      return;
    }
    i = uniform (1, t);
    j = uniform (0, i);
    i = l[i];
    j = l[j];
    writeln ("? ", i + 1, ' ', j + 1);
    stdout.flush ();
    int p;
    readf (" %d", &p);
    --p;
    parent[] = -2;
    parent[p] = -1;
    debug stderr.writeln ("u = ", u);
    debug stderr.writeln ("v = ", v);
    tm = 0;
    go (p);
    foreach (x; l) {
      if (x == p) continue;
      if (i != p && (child (x, i) || child (i, x))) b[x] = true;
      if (j != p && (child (x, j) || child (j, x))) b[x] = true;
    }
    parent[] = -2;
    parent[p] = -1;
    tm = 0;
    go (p);
    b[] = true;
    b[p] = false;
    foreach (o; 0 .. n) if (parent[o] >= 0) b[o] = false;
  }
}

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

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

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

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

int N;
int[] X, Y;

int Ask(int u, int v) {
  writefln("? %s %s", u + 1, v + 1);
  stdout.flush;
  const res = readInt();
  if (res == -1) {
    import core.stdc.stdlib;
    exit(0);
  }
  return res - 1;
}
void Answer(int r) {
  writefln("! %s", r + 1);
  stdout.flush;
  import core.stdc.stdlib;
  exit(0);
}

void main() {
  N = readInt();
  X = new int[N - 1];
  Y = new int[N - 1];
  foreach (i; 0 .. N - 1) {
    X[i] = readInt() - 1;
    Y[i] = readInt() - 1;
  }

  auto G = new int[][N];
  foreach (i; 0 .. N - 1) {
    G[X[i]] ~= i;
    G[Y[i]] ~= i;
  }
  for (int u = 0, p = -1; ; ) {
    int[] vs;
    foreach (i; G[u]) {
      const v = X[i] ^ Y[i] ^ u;
      if (v != p) {
        vs ~= v;
      }
    }
    const vsLen = cast(int)(vs.length);
    int vm = -1;
    foreach (j; 0 .. vsLen / 2) {
      const res = Ask(vs[j * 2], vs[j * 2 + 1]);
      if (res != u) {
        vm = res;
      }
    }
    if (vsLen % 2 != 0) {
      const res = Ask(vs[vsLen - 1], u);
      if (res != u) {
        vm = res;
      }
    }
    if (vm == -1) {
      Answer(u);
    }
    p = u;
    u = vm;
  }
}