You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square.

The first line contains a single integer n (1 ≤ n ≤ 5). Next n lines contain four integers each, describing a single rectangle: x1, y1, x2, y2 (0 ≤ x1 < x2 ≤ 31400, 0 ≤ y1 < y2 ≤ 31400) — x1 and x2 are x-coordinates of the left and right edges of the rectangle, and y1 and y2 are y-coordinates of the bottom and top edges of the rectangle.  No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).

In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

int n;
int[4][] ps;
int[2][] p;
bool ans = false;

void make( int depth, int end, int i ){
	if( depth == end ){
		solve( depth );
		return;
	}
	for( int j = i; j < n; ++j ){
		auto tmp = p.clone;
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 3 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 3 ] ];
		make( depth + 1, end, j + 1 );
		assert( tmp !is p );
		p = tmp.clone;
	}
}

void solve( int s ){
	bool[int[2]] data;
	for( int i = 0; i < s*4; ++i ){
		bool f = false;
		for( int j = 0; j < s*4; ++j ){
			if( i == j )continue;

			assert( p[ i ] !is p[ j ] );
			f |= p[ i ] == p[ j ];
		}
		if( !f ){
			data[ p[ i ] ] = true;
		}
	}

	if( data.keys.length == 4 ){
		int min_x = INF, min_y = INF, max_x = 0, max_y = 0;
		foreach( v; data.keys ){
			min_x = min( min_x, v.front );
			min_y = min( min_y, v.back );
			max_x = max( max_x, v.front );
			max_y = max( max_y, v.back );
		}
		int[2] cntx, cnty;
		foreach( v; data.keys ){
			if( v.front == min_x )++cntx[0];
			if( v.front == max_x )++cntx[1];
			if( v.back  == min_y )++cnty[0];
			if( v.back  == max_y )++cnty[1];
		}
		ans |= (cntx == [ 2, 2 ] && cnty == [ 2, 2 ] && (max_x - min_x) == (max_y - min_y) );
	}
}

void main(){
	n = next!int;

	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

/+
	for( int s = 1; s <= n; ++s ){
		p = p.init;
		make( 0, s, 0 );
	}
+/
	p = p.init;
	make( 0, n, 0 );

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

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

    struct rect { int x1, y1, x2, y2; }
    rect[] rs = new rect[] (n);
    foreach (i; 0 .. n) {
        readf("%s %s %s %s", &rs[i].x1, &rs[i].y1, &rs[i].x2, &rs[i].y2);
        readln;
    }

    debug { rs.writeln; }

    immutable int INF = 10 ^^ 9;
    int sx1 = INF, sy1 = INF, sx2 = -INF, sy2 = -INF;

    foreach (r; rs) {
        sx1 = min(sx1, r.x1);
        sy1 = min(sy1, r.y1);
        sx2 = max(sx2, r.x2);
        sy2 = max(sy2, r.y2);
    }

    if (sy2 - sy1 != sx2 - sx1) {
        writeln("NO");
        return;
    }

    int rectArea = rs.map!(r => (r.y2 - r.y1) * (r.x2 - r.x1)).sum;
    int sqArea = (sy2 - sy1) * (sx2 - sx1);
    if (rectArea != sqArea) {
        writeln("NO");
        return;
    }

    writeln("YES");
}

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

    struct rect { int x1, y1, x2, y2; }
    rect[] rs = new rect[] (n);
    foreach (i; 0 .. n) {
        readf("%s %s %s %s", &rs[i].x1, &rs[i].y1, &rs[i].x2, &rs[i].y2);
        readln;
    }

    debug { rs.writeln; }

    immutable int INF = 10 ^^ 9;
    int sx1 = INF, sy1 = INF, sx2 = -INF, sy2 = -INF;
    int rectArea = rs.map!(r => (r.y2 - r.y1) * (r.x2 - r.x1)).sum;

    foreach (j; 0 .. n) {
        sx1 = min(sx1, rs[j].x1);
        sy1 = min(sy1, rs[j].y1);
        sx2 = max(sx2, rs[j].x2);
        sy2 = max(sy2, rs[j].y2);
    }

    if (sy2 - sy1 != sx2 - sx1) {
        writeln("NO");
        return;
    }

    int area = (sy2 - sy1) * (sx2 - sx1);
    if (area != rectArea) {
        writeln("NO");
        return;
    }

    writeln("YES");
}

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

    struct rect { int x1, y1, x2, y2; }
    rect[] rs = new rect[] (n);
    foreach (i; 0 .. n) {
        readf("%s %s %s %s", &rs[i].x1, &rs[i].y1, &rs[i].x2, &rs[i].y2);
        readln;
    }

    debug { rs.writeln; }

    immutable int INF = 10 ^^ 9;
    int sx1 = INF, sy1 = INF, sx2 = -INF, sy2 = -INF;
    int rectArea = rs.map!(r => (r.y2 - r.y1) * (r.x2 - r.x1)).sum;

    foreach (r; rs) {
        sx1 = min(sx1, r.x1);
        sy1 = min(sy1, r.y1);
        sx2 = max(sx2, r.x2);
        sy2 = max(sy2, r.y2);
    }

    if (sy2 - sy1 != sx2 - sx1) {
        writeln("NO");
        return;
    }

    int area = (sy2 - sy1) * (sx2 - sx1);
    if (area != rectArea) {
        writeln("NO");
        return;
    }

    writeln("YES");
}

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;
	while (readf (" %s", &n) > 0)
	{
		alias Tuple !(long, "x", long, "y") point;
		alias Tuple !(point, "a", point, "b") rect;
		auto r = new rect [n];
		auto t = rect (point (int.max, int.max),
		               point (int.min, int.min));
		long s = 0;
		foreach (i; 0..n)
		{
			readf (" %s %s %s %s", &r[i].a.x, &r[i].a.y,
			                       &r[i].b.x, &r[i].b.y);
			s += (r[i].b.x - r[i].a.x) * (r[i].b.y - r[i].a.y);
			t.a.x = min (t.a.x, r[i].a.x);
			t.a.y = min (t.a.y, r[i].a.y);
			t.b.x = max (t.b.x, r[i].b.x);
			t.b.y = max (t.b.y, r[i].b.y);
		}
		if (t.b.x - t.a.x == t.b.y - t.a.y &&
		    (t.b.x - t.a.x) * (t.b.y - t.a.y) == s)
		{
			writeln ("YES");
		}
		else
		{
			writeln ("NO");
		}
	}
}

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

void main(){
	int n = next!int;

	int[4][] ps;
	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

	bool ans = false;
	for( int s = 0; s <= n; ++s ){
		int[2][] p;
		for( int r = 0; r < s; ++r ){
			p ~= [ ps[ r ][ 0 ], ps[ r ][ 1 ] ];
			p ~= [ ps[ r ][ 2 ], ps[ r ][ 1 ] ];
			p ~= [ ps[ r ][ 0 ], ps[ r ][ 3 ] ];
			p ~= [ ps[ r ][ 2 ], ps[ r ][ 3 ] ];
		}
		for( int k = 0; k < s*4; ++k ){
			for( int l = k+1; l < s*4; ++l ){
				bool[int[2]] data;
				for( int i = 0; i <= k; ++i ){
					bool f = false;
					for( int j = 0; j <= l; ++j ){
						if( i == j )continue;

						assert( p[ i ] !is p[ j ] );
						f |= p[ i ] == p[ j ];
					}
					if( !f ){
						data[ p[ i ] ] = true;
					}
				}

				if( data.keys.length == 4 ){
					int min_x = INF, min_y = INF, max_x = 0, max_y = 0;
					foreach( v; data.keys ){
						min_x = min( min_x, v.front );
						min_y = min( min_x, v.back );
						max_x = max( max_x, v.front );
						max_y = max( max_y, v.back );
					}
					int[2] cntx, cnty;
					foreach( v; data.keys ){
						if( v.front == min_x )++cntx[0];
						if( v.front == max_x )++cntx[1];
						if( v.back  == min_y )++cnty[0];
						if( v.back  == max_y )++cnty[1];
					}
					ans |= (cntx == [ 2, 2 ] && cnty == [ 2, 2 ]);
				}
			}
		}
	}

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

int n;
int[4][] ps;
int[2][] p;
bool ans = false;

void make( int depth, int end, int i ){
	if( depth == end ){
		solve( depth );
	}
	for( int j = i; j < end; ++j ){
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 3 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 3 ] ];
		make( depth + 1, end, j + 1 );
	}
}

void solve( int s ){
	bool[int[2]] data;
	for( int i = 0; i < s*4; ++i ){
		bool f = false;
		for( int j = 0; j < s*4; ++j ){
			if( i == j )continue;

			assert( p[ i ] !is p[ j ] );
			f |= p[ i ] == p[ j ];
		}
		if( !f ){
			data[ p[ i ] ] = true;
		}
	}
	if( data.keys.length == 4 ){
		int min_ = INF, max_ = 0;
		foreach( vs; data.keys ){
			foreach( v; vs ){
				min_ = min( min_, v );
				max_ = max( max_, v );
			}
		}
		assert( min_ != max_ );
		int[2] cnt;
		foreach( vs; data.keys ){
			foreach( v; vs ){
				if( v == min_ )++cnt[ 0 ];
				if( v == max_ )++cnt[ 1 ];
			}
		}
		ans |= cnt.front == 4 && cnt.back == 4;
	}
}

void main(){
	n = next!int;

	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

	for( int s = 1; s <= n; ++s ){
		p = p.init;
		make( 0, s, 0 );
	}

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

int n;
int[4][] ps;
int[2][] p;
bool ans = false;

void make( int depth, int end, int i ){
	if( depth == end ){
		solve( depth );
		return;
	}
	for( int j = i; j < n; ++j ){
		auto tmp = p.clone;
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 3 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 3 ] ];
		make( depth + 1, end, j + 1 );
		assert( tmp !is p );
		p = tmp.clone;
	}
}

void solve( int s ){
	bool[int[2]] data;
	for( int i = 0; i < s*4; ++i ){
		bool f = false;
		for( int j = 0; j < s*4; ++j ){
			if( i == j )continue;

			assert( p[ i ] !is p[ j ] );
			f |= p[ i ] == p[ j ];
		}
		if( !f ){
			data[ p[ i ] ] = true;
		}
	}
	if( data.keys.length == 4 ){
		int min_x = INF, min_y = INF, max_x = 0, max_y = 0;
		foreach( v; data.keys ){
			min_x = min( min_x, v.front );
			min_y = min( min_x, v.back );
			max_x = max( max_x, v.front );
			max_y = max( max_y, v.back );
		}
		int[2] cntx, cnty;
		foreach( v; data.keys ){
			if( v.front == min_x )++cntx[0];
			if( v.front == max_x )++cntx[1];
			if( v.back  == min_y )++cnty[0];
			if( v.back  == max_y )++cnty[1];
		}
		ans |= (cntx == [ 2, 2 ] && cnty == [ 2, 2 ] && (max_x - min_x) == (max_y - min_y) );
	}
}

void main(){
	n = next!int;

	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

	for( int s = 1; s <= n; ++s ){
		p = p.init;
		make( 0, s, 0 );
	}

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

int n;
int[4][] ps;
int[2][] p;
bool ans = false;

void make( int depth, int end, int i ){
	if( depth == end ){
		solve( depth );
	}
	for( int j = i; j < end; ++j ){
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 3 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 3 ] ];
		make( depth + 1, end, j + 1 );
	}
}

void solve( int s ){
	for( int k = 0; k < s*4; ++k ){
		for( int l = k+1; l < s*4; ++l ){
			bool[int[2]] data;
			for( int i = 0; i <= k; ++i ){
				bool f = false;
				for( int j = 0; j <= l; ++j ){
					if( i == j )continue;

					assert( p[ i ] !is p[ j ] );
					f |= p[ i ] == p[ j ];
				}
				if( !f ){
					data[ p[ i ] ] = true;
				}
			}

			if( data.keys.length == 4 ){
				int min_x = INF, min_y = INF, max_x = 0, max_y = 0;
				foreach( v; data.keys ){
					min_x = min( min_x, v.front );
					min_y = min( min_x, v.back );
					max_x = max( max_x, v.front );
					max_y = max( max_y, v.back );
				}
				int[2] cntx, cnty;
				foreach( v; data.keys ){
					if( v.front == min_x )++cntx[0];
					if( v.front == max_x )++cntx[1];
					if( v.back  == min_y )++cnty[0];
					if( v.back  == max_y )++cnty[1];
				}
				ans |= (cntx == [ 2, 2 ] && cnty == [ 2, 2 ]);
			}
		}
	}
}

void main(){
	n = next!int;

	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

	for( int s = 1; s <= n; ++s ){
		p = p.init;
		make( 0, s, 0 );
	}

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln;
import std.array;
import std.range;
import std.typecons;
import std.bigint;
import std.algorithm;

const int INF = 1<<28;

int n;
int[4][] ps;
int[2][] p;
bool ans = false;

void make( int depth, int end, int i ){
	if( depth == end ){
		solve( depth );
		return;
	}
	for( int j = i; j < n; ++j ){
		auto tmp = p.clone;
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 1 ] ];
		p ~= [ ps[ j ][ 0 ], ps[ j ][ 3 ] ];
		p ~= [ ps[ j ][ 2 ], ps[ j ][ 3 ] ];
		make( depth + 1, end, j + 1 );
		assert( tmp !is p );
		p = tmp.clone;
	}
}

void solve( int s ){
	bool[int[2]] data;
	for( int i = 0; i < s*4; ++i ){
		bool f = false;
		for( int j = 0; j < s*4; ++j ){
			if( i == j )continue;

			assert( p[ i ] !is p[ j ] );
			f |= p[ i ] == p[ j ];
		}
		if( !f ){
			data[ p[ i ] ] = true;
		}
	}

	if( data.keys.length == 4 ){
		int min_x = INF, min_y = INF, max_x = 0, max_y = 0;
		foreach( v; data.keys ){
			min_x = min( min_x, v.front );
			min_y = min( min_y, v.back );
			max_x = max( max_x, v.front );
			max_y = max( max_y, v.back );
		}
		int[2] cntx, cnty;
		foreach( v; data.keys ){
			if( v.front == min_x )++cntx[0];
			if( v.front == max_x )++cntx[1];
			if( v.back  == min_y )++cnty[0];
			if( v.back  == max_y )++cnty[1];
		}
		ans |= (cntx == [ 2, 2 ] && cnty == [ 2, 2 ] && (max_x - min_x) == (max_y - min_y) );
	}
}

void main(){
	n = next!int;

	foreach( i; n.iota ){
		int x1 = next!int;
		int y1 = next!int;

		int x2 = next!int;
		int y2 = next!int;

		ps ~= [x1, y1, x2, y2];
	}

	for( int s = 1; s <= n; ++s ){
		p = p.init;
		make( 0, s, 0 );
	}

	writeln = ans ? "YES": "NO";
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

T next( T : char )()
in
{
	assert(hasNext());
}
out
{
	input.front.popFront;
}
body
{
	return input.front.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		if(input.front.length == 0){
			input.popFront;
			return hasNext;
		}
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return hasNext;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

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

    struct rect { int x1, y1, x2, y2; }
    rect[] rs = new rect[] (n);
    foreach (i; 0 .. n) {
        readf("%s %s %s %s", &rs[i].x1, &rs[i].y1, &rs[i].x2, &rs[i].y2);
        readln;
    }

    debug { rs.writeln; }

    immutable int INF = 10 ^^ 9;
    foreach (i; 1 .. (1 << n)) {
        int sx1 = INF, sy1 = INF, sx2 = -INF, sy2 = -INF;

        int rectArea = i.bitsSet.map!(x => (rs[x].y2 - rs[x].y1) * (rs[x].x2 - rs[x].x1)).sum;

        foreach (j; i.bitsSet) {
            sx1 = min(sx1, rs[j].x1);
            sy1 = min(sy1, rs[j].y1);
            sx2 = max(sx2, rs[j].x2);
            sy2 = max(sy2, rs[j].y2);
        }

        if (sy2 - sy1 != sx2 - sx1) { continue; }

        int area = (sy2 - sy1) * (sx2 - sx1);

        if (area != rectArea) { continue; }

        writeln("YES");
        return;
    }

    writeln("NO");
}

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

    struct rect { int x1, y1, x2, y2; }
    rect[] rs = new rect[] (n);
    foreach (i; 0 .. n) {
        readf("%s %s %s %s", &rs[i].x1, &rs[i].y1, &rs[i].x2, &rs[i].y2);
        readln;
    }

    debug { rs.writeln; }

    immutable int INF = 10 ^^ 9;
    int sx1 = INF, sy1 = INF, sx2 = -INF, sy2 = -INF;
    int rectArea = rs.map!(r => (r.y2 - r.y1) * (r.x2 - r.x1)).sum;

    foreach (j; 0 .. 5) {
        sx1 = min(sx1, rs[j].x1);
        sy1 = min(sy1, rs[j].y1);
        sx2 = max(sx2, rs[j].x2);
        sy2 = max(sy2, rs[j].y2);
    }

    if (sy2 - sy1 != sx2 - sx1) {
        writeln("NO");
        return;
    }

    int area = (sy2 - sy1) * (sx2 - sx1);
    if (area != rectArea) {
        writeln("NO");
        return;
    }

    writeln("YES");
}


Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to k, where k is either 1 or -1.Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007 = 109 + 7.Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.

The only line contains three integers n, m and k (1 ≤ n, m ≤ 1018, k is either 1 or -1).

Print a single number denoting the answer modulo 1000000007.

NoteIn the first example the only way is to put -1 into the only block.In the second example the only way is to put 1 into every block.

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

immutable long mod = 10^^9 + 7;

void main() {
    long n, m, k;
    scan(n, m, k);

    if (n > m) swap(n, m);

    long ans = powmod(2, n - 1, mod);
    ans = powmod(ans, m - 1, mod);

    if (k == 1) {
        writeln(ans);
    }
    else {
        if ((n & 1) != (m & 1)) {
            writeln(0);
        }
        else {
            writeln(ans);
        }
    }
}

long powmod(long x, long y, long mod) {
    return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y & 1) % mod : 1L;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    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);
        }
    }
}

struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this(int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head = (head + 1) % N;
    }

    void clear() {
        head = tail = 0;
    }
}


A word or a sentence in some language is called a pangram if all the characters of the alphabet of this language appear in it at least once. Pangrams are often used to demonstrate fonts in printing or test the output devices.You are given a string consisting of lowercase and uppercase Latin letters. Check whether this string is a pangram. We say that the string contains a letter of the Latin alphabet if this letter occurs in the string in uppercase or lowercase.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of characters in the string. The second line contains the string. The string consists only of uppercase and lowercase Latin letters.

Output "YES", if the string is a pangram and "NO" otherwise.

import std.ascii;
import std.stdio;
import std.string;
import std.algorithm;

byte n, sum;

void main() {

	readf(" %s\n", &n);

	auto s = readln.strip;

	foreach (i; lowercase)
		if (find(s, i).length != 0 || find(s, std.string.toUpper(i)).length != 0)
			++sum;

	puts((sum == 26) ? "YES" : "NO");
}

import std.ascii;
import std.stdio;
import std.string;
import std.algorithm;

byte n, sum;

void main() {

	readf(" %s\n", &n);

	auto s = readln.strip;

	foreach (idx, el; lowercase)
		if (find(s, el).length != 0 || find(s, uppercase[idx]).length != 0)
			++sum;

	puts((sum == 26) ? "YES" : "NO");
}

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

byte sum;

void main() {

	int n;
	readf(" %s\n", &n);

	string a = readln.strip;

	foreach (immutable i; 'a' .. 'z' + 1)
		if (count(a, i) || count(a, toUpper(i)))
			++sum;

	writeln((sum is 26) ? "YES" : "NO");
}

import std.stdio;
import std.string;

bool isPangram(string str) {
    str = str.toUpper();

    uint['Z'-'A'+1] seen;
    foreach (char i; str) {
        seen[i-'A']++;
    }

    bool result = true;
    for (int i = 0; i < seen.length && result; ++i) {
        if (seen[i] == 0) {
            result = false;
        }
    }

    return result;
}

unittest {
    assert(!isPangram("toosmallword"));
    assert(isPangram("TheQuickBrownFoxJumpsOverTheLazyDog"));
}

void main() {
    int len;
    string str;

    readf("%d\n%s\n", &len, &str);

    if (isPangram(str)) {
        writeln("YES");
    } else {
        writeln("NO");
    }
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.iteration: uniq;
import std.algorithm.mutation: copy;
import std.algorithm.sorting: sort;
import std.array;

void main()
{
  stdin.readln;
  auto h = stdin.readln.strip.dup.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");
  auto g = cast(ubyte[])h;
  g.sort();
  writeln (g.uniq().array.length == 26 ? "YES" : "NO" );
}

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

int main()
{
	readln();
	int l = 'z' - 'a' + 1;
	string line = readln();
	line = line.toLower;
	int[] letters; letters.length = l;
	int sum = 0;
	foreach (char x; line)
	{
		int idx = x - 'a';
		if (idx >= 0 && idx < l)
		{
			letters[idx] = 1;
		}
	}

	foreach (int i; 0..l) sum += letters[i];
	write (sum == l ? "YES" : "NO");
	return 0;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
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(){
    import std.bitmanip:BitArray;
    BitArray b;
    int n=readln.chomp.to!int;
    string s=readln.strip.toLower;
    b.length=26;
    foreach(c;s)
        b[c-'a']=true;
    writeln(b.count<26?"NO":"YES");
}

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

//	Input
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[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

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

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

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

int N;
string S;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		S = readToken;

		auto app = new bool[26];
		foreach (s; S) {
			app[s.toUpper - 'A'] = true;
		}
		const ans = app.all;
		writeln(ans ? "YES" : "NO");

	}
	} catch (EOFException) {}
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.searching: any;
import std.algorithm.iteration: filter;
import std.range;
import std.array;

void main()
{
  auto h = stdin.readln.strip.dup.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");
  auto g = cast(ubyte[])h;
  g = g.filter!"a>=97 && a<=122".array;
  ubyte[26] cs;
  auto a = cast(ubyte)'a';
  foreach(e;g ) cs[e-a]++;
  writeln(cs.dup.any!"a==0" ? "NO" : "YES");
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.searching: any;
import std.range;
import std.array;

void main()
{
  auto h = stdin.readln.strip.dup.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");
  ubyte[26] cs;
  auto a = cast(ubyte)'a';
  foreach(e;h ) cs[(cast(ubyte)e)-a]++;
  writeln(cs.dup.any!"a==0" ? "NO" : "YES");
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.searching: any;
import std.range;
import std.array;

void main()
{
  auto h = stdin.readln.strip.dup.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");
  auto g = cast(ubyte[])h;
  ubyte[26] cs;
  auto a = cast(ubyte)'a';
  foreach(e;h ) cs[e-a]++;
  writeln(cs.dup.any!"a==0" ? "NO" : "YES");
}

import std.stdio: writeln, stdin;
import std.string: strip,tr;
import std.algorithm.iteration: uniq;
import std.algorithm.mutation: copy;
import std.algorithm.sorting: sort;
import std.range;
import std.array;

void main()
{
  auto h = stdin.readln.strip.dup.tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");
  auto g = cast(ubyte[])h;
  g.sort();
  writeln (g.uniq().array.length == 26 ? "YES" : "NO" );
}

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

int main()
{
	readln();
	int l = 'z' - 'a';
	string line = readln();
	line = line.toLower;
	int[] letters; letters.length = l;
	int sum = 0;
	foreach (char x; line)
	{
		int idx = x - 'a';
		if (idx >= 0 && idx < l)
		{
			letters[idx] = 1;
		}
	}

	foreach (int i; 0..l) sum += letters[i];
	write (sum == l ? "YES" : "NO");
	return 0;
}


Bizon the Champion is called the Champion for a reason. Bizon the Champion has recently got a present — a new glass cupboard with n shelves and he decided to put all his presents there. All the presents can be divided into two types: medals and cups. Bizon the Champion has a1 first prize cups, a2 second prize cups and a3 third prize cups. Besides, he has b1 first prize medals, b2 second prize medals and b3 third prize medals. Naturally, the rewards in the cupboard must look good, that's why Bizon the Champion decided to follow the rules:  any shelf cannot contain both cups and medals at the same time;  no shelf can contain more than five cups;  no shelf can have more than ten medals. Help Bizon the Champion find out if we can put all the rewards so that all the conditions are fulfilled.

The first line contains integers a1, a2 and a3 (0 ≤ a1, a2, a3 ≤ 100). The second line contains integers b1, b2 and b3 (0 ≤ b1, b2, b3 ≤ 100). The third line contains integer n (1 ≤ n ≤ 100). The numbers in the lines are separated by single spaces.

Print "YES" (without the quotes) if all the rewards can be put on the shelves in the described manner. Otherwise, print "NO" (without the quotes).

import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int a = readln.chomp.split(" ").map!(to!int).array.reduce!"a + b";
    int b = readln.chomp.split(" ").map!(to!int).array.reduce!"a + b";
    int n; scanf("%d\n", &n);

    int x = (a + 4) / 5 + (b + 9) / 10;
    writeln( x <= n ? "YES" : "NO" );
}

import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.math;
import std.array;

int main(string[] argv)
{
	int[] a; a.length = 3;
	int[] b; b.length = 3;
	int n;
	scanf("%d %d %d\n%d %d %d\n%d", &a[0], &a[1], &a[2], &b[0], &b[1], &b[2], &n);
	int as = a[0] + a[1] + a[2];
	int bs = b[0] + b[1] + b[2];
	int ts = as / 5 + (as % 5 == 0 ? 0 : 1) + bs / 10 + (bs % 10 == 0 ? 0 : 1);
	write (ts <= n ? "YES" : "NO");
	return 0;
}

import std.stdio;
import std.string;
import std.bigint;
import std.array;
import std.ascii;
import std.math;
import std.numeric;
import std.algorithm;
import std.container;
import core.bitop;
import std.conv;

alias Int = long;

void main() {
  Int a = readln.split.map!(to!Int).reduce!((a, b) => a + b);
  Int b = readln.split.map!(to!Int).reduce!((a, b) => a + b);
  int n = readln.split.map!(to!int).array[0];
  debug writeln(a);
  debug writeln(b);
  debug writeln(n);

  auto rem = new Int[n];

  {
    rem[] = 5;
    while ( a > 0 && ! rem.empty ) {
      if ( rem.empty ) {
        writeln("NO");
        return;
      }
      auto d = min(a, 5);
      a -= d;
      rem.front -= d;
      if ( rem.front <= 0 ) {
        rem.popFront();
      }
    }
    if ( ! rem.empty && rem.front < 5 ) {
      rem.popFront();
    }
  }

  debug writeln(rem);

  {
    rem[] = 10;
    while ( b > 0 && ! rem.empty ) {
      if ( rem.empty ) {
        writeln("NO");
        return;
      }
      auto d = min(b, 10);
      b -= d;
      rem.front -= d;
      if ( rem.front <= 0 ) {
        rem.popFront();
      }
    }
    if ( ! rem.empty && rem.front < 10 ) {
      rem.popFront();
    }
  }

  if ( a > 0 || b > 0 ) {
    writeln("NO");
  } else {
    writeln("YES");
  }
}

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

void main() {
    auto a = readln.chomp.split.map!(to!double).reduce!("a + b");
    auto b = readln.chomp.split.map!(to!double).reduce!("a + b");
    auto n = readln.chomp.to!double;

    if ((a / 5).ceil + (b / 10).ceil <= n)
        writeln("YES");
    else
        writeln("NO");
}

import std.stdio;
import std.algorithm;

void main(){
  int[3] as;
  int[3] bs;
  int n;
  for(int i = 0; i < 3; i++){
    int t;
    readf("%s ",&t);
    as[i] = t;
  }
  for(int i = 0; i < 3; i++){
    int t;
    readf("%s ",&t);
    bs[i] = t;
  }
  readf("%s ",&n);
  if (run(as,bs,n) == 0){
    writefln("NO");
    return;
  }
  writefln("YES");
}

int suma(int[] x){
  int sum = 0;
  foreach(v;x){
    sum += v;
  }
  return sum;
}

int run(int[] a, int[] b, int n){
  int sa = suma(a);
  int sb = suma(b);
  int pa = (sa / 5);
  int pb = (sb / 10);
  if (sa % 5 != 0){
    pa++;
  }
  if (sb % 10 != 0){
    pb++;
  }
  if (pa + pb > n){
    return 0;
  }
  return 1;
}

// TEST CASE AREA
void test_case(){
  int[][] rs;

  rs ~= [run([1,1,1],[1,1,1],4),1];
  rs ~= [run([1,1,3],[2,3,4],2),1];
  rs ~= [run([1,0,0],[1,0,0],1),0];

  array_assert(rs);
}

void array_assert(int[][] rs){
  uint i = 1;
  bool failed = false;

  writefln("TEST");

  foreach(int[] r;rs){
    if(r[0] != r[1]){
      failed = true;
      writefln("%s Failed %s is NOT %s",i,r[0],r[1]);
    }
    i++;
  }

  if(!failed){
    writefln("OK");
  }
}

uint[][] numerate(uint[] l){
  uint n = 0;
  uint[][] nl;

  foreach(uint x; l){
    nl ~= [x,n];
    n++;
  }

  return nl;
}

import std.stdio;
import std.c.stdio;
import std.algorithm;
int main(string[] argv)
{
	int a1,a2,a3,b1,b2,b3,n;
	scanf("%d",&a1);
	scanf("%d",&a2);
	scanf("%d",&a3);
	scanf("%d",&b1);
	scanf("%d",&b2);
	scanf("%d",&b3);
	scanf("%d",&n);
	writeln(((a1+a2+a3+4)/5+(b1+b2+b3+9)/10)<=n? "YES":"NO");
    return 0;
}

import std.stdio;
import std.string;
import std.bigint;
import std.array;
import std.ascii;
import std.math;
import std.numeric;
import std.algorithm;
import std.container;
import core.bitop;
import std.conv;

alias Int = long;

void main() {
  Int a = readln.split.map!(to!Int).reduce!((a, b) => a + b);
  Int b = readln.split.map!(to!Int).reduce!((a, b) => a + b);
  int n = readln.split.map!(to!int).array[0];
  debug writeln(a);
  debug writeln(b);
  debug writeln(n);

  auto rem = new Int[n];

  {
    rem[] = 5;
    while ( a > 0 ) {
      if ( rem.empty ) {
        writeln("NO");
        return;
      }
      auto d = min(a, 5);
      a -= d;
      rem.front -= d;
      if ( rem.front <= 0 ) {
        rem.popFront();
      }
    }
    if ( rem.front < 5 ) {
      rem.popFront();
    }
  }

  debug writeln(rem);

  {
    rem[] = 10;
    while ( b > 0 ) {
      if ( rem.empty ) {
        writeln("NO");
        return;
      }
      auto d = min(b, 10);
      b -= d;
      rem.front -= d;
      if ( rem.front <= 0 ) {
        rem.popFront();
      }
    }
    if ( rem.front < 10 ) {
      rem.popFront();
    }
  }

  writeln("YES");
}


Numbers k-bonacci (k is integer, k > 1) are a generalization of Fibonacci numbers and are determined as follows:  F(k, n) = 0, for integer n, 1 ≤ n < k;  F(k, k) = 1;  F(k, n) = F(k, n - 1) + F(k, n - 2) + ... + F(k, n - k), for integer n, n > k. Note that we determine the k-bonacci numbers, F(k, n), only for integer values of n and k.You've got a number s, represent it as a sum of several (at least two) distinct k-bonacci numbers.

The first line contains two integers s and k (1 ≤ s, k ≤ 109; k > 1).

In the first line print an integer m (m ≥ 2) that shows how many numbers are in the found representation. In the second line print m distinct integers a1, a2, ..., am. Each printed integer should be a k-bonacci number. The sum of printed integers must equal s. It is guaranteed that the answer exists. If there are several possible answers, print any of them.

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

int main() {
	int fib(int n, int k) {
		alias memoize!fib mfib;
		if (n < k)
			return 0;
		else if (n == k)
			return 1;
		else {
			int ans = 0;
			for (int j = max(k, n - k); j <= n - 1; ++j)
				ans += mfib(j, k);
			return ans;
		}
	}
	auto tmp = split(strip(readln()), " ");
	auto s = to!int(tmp[0]);
	auto k = to!int(tmp[1]);
	int[] ans = [];
	while (s > 0) {
		int cnt = k;
		while (fib(cnt, k) <= s)
			++cnt;
		--cnt;
		ans ~= fib(cnt, k);
		s -= fib(cnt, k);
	}
	if (ans.length == 1)
		ans ~= 0;
	writeln(ans.length);
	foreach (int i; ans)
		write(i, " ");
	return 0;
}

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

int main() {
	ulong fib(ulong n, ulong k) {
		alias memoize!fib mfib;
		if (n < k)
			return 0;
		else if (n == k)
			return 1;
		else {
			ulong ans = 0;
			for (ulong j = max(k, n - k); j <= n - 1; ++j)
				ans += mfib(j, k);
			return ans;
		}
	}
	auto tmp = split(strip(readln()), " ");
	auto s = to!ulong(tmp[0]);
	auto k = to!ulong(tmp[1]);
	ulong[] ans = [];
	while (s > 0) {
		ulong cnt = k;
		while (fib(cnt, k) <= s)
			++cnt;
		--cnt;
		ans ~= fib(cnt, k);
		s -= fib(cnt, k);
	}
	if (ans.length == 1)
		ans ~= 0;
	writeln(ans.length);
	foreach (ulong i; ans)
		write(i, " ");
	return 0;
}

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

int main() {
	ulong fib(ulong n, ulong k, ulong max) {
		alias memoize!fib mfib;
		if (n < k)
			return 0;
		else if (n == k)
			return 1;
		else {
			ulong ans = 0;
			for (ulong j = std.algorithm.max(k, n - k); (ans <= max) & (j <= n - 1); ++j) {
				ans += mfib(j, k, max);
				if (ans > max)
					return max + 1;
			}
			return ans;
		}
	}
	auto tmp = split(strip(readln()), " ");
	auto s = to!ulong(tmp[0]);
	auto k = to!ulong(tmp[1]);
	/*ulong ke = k;
	while (fib(ke, k, s) <= s) {
		write(fib(ke, k, s), " ");
		++ke;
	}
	writeln();*/
	ulong[] ans = [];
	auto s0 = s;
	while (s > 0) {
		ulong cnt = k;
		while (fib(cnt, k, s0) <= s)
			++cnt;
		--cnt;
		ans ~= fib(cnt, k, s0);
		s -= fib(cnt, k, s0);
	}
	if (ans.length == 1)
		ans ~= 0;
	writeln(ans.length);
	foreach (ulong i; ans)
		write(i, " ");
	return 0;
}

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

int main() {
	uint fib(uint n, uint k) {
		alias memoize!fib mfib;
		if (n < k)
			return 0;
		else if (n == k)
			return 1;
		else {
			uint ans = 0;
			for (uint j = max(k, n - k); j <= n - 1; ++j)
				ans += mfib(j, k);
			return ans;
		}
	}
	auto tmp = split(strip(readln()), " ");
	auto s = to!uint(tmp[0]);
	auto k = to!uint(tmp[1]);
	uint[] ans = [];
	while (s > 0) {
		uint cnt = k;
		while (fib(cnt, k) <= s)
			++cnt;
		--cnt;
		ans ~= fib(cnt, k);
		s -= fib(cnt, k);
	}
	if (ans.length == 1)
		ans ~= 0;
	writeln(ans.length);
	foreach (uint i; ans)
		write(i, " ");
	return 0;
}


Ted has a pineapple. This pineapple is able to bark like a bulldog! At time t (in seconds) it barks for the first time. Then every s seconds after it, it barks twice with 1 second interval. Thus it barks at times t, t + s, t + s + 1, t + 2s, t + 2s + 1, etc.  Barney woke up in the morning and wants to eat the pineapple, but he can't eat it when it's barking. Barney plans to eat it at time x (in seconds), so he asked you to tell him if it's gonna bark at that time.

The first and only line of input contains three integers t, s and x (0 ≤ t, x ≤ 109, 2 ≤ s ≤ 109) — the time the pineapple barks for the first time, the pineapple barking interval, and the time Barney wants to eat the pineapple respectively.

Print a single "YES" (without quotes) if the pineapple will bark at time x or a single "NO" (without quotes) otherwise in the only line of output.

NoteIn the first and the second sample cases pineapple will bark at moments 3, 13, 14, ..., so it won't bark at the moment 4 and will bark at the moment 3.In the third and fourth sample cases pineapple will bark at moments 3, 11, 12, 19, 20, 27, 28, 35, 36, 43, 44, 51, 52, 59, ..., so it will bark at both moments 51 and 52.

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias mp=make_pair;
alias bins=binary_search;
alias orsq=orient_square;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	X binpow(X,Y)(X base,Y exp)
		if(is(typeof(exp&1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
		if(is(typeof(exp&1)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (g==a[l])?l:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
	V foldr(R,V,T)(R range,T arg,V nach)
		if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
	{
		for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
		return nach;
	}
	size_t countr(R,T)(R range,T fun)
		if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
	{
		size_t nach;
		for(;!range.empty;range.popFront()) nach+=fun(range.front);
		return nach;
	}
	T orient_square(T)(pair!(T,T)[] figure...)
		if(is(typeof(figure.front*figure.front)==T))
	{
		auto n=figure.front,t=figure.front;
		figure.popFront();
		T ans=0;
		for(;!figure.empty;figure.popFront())
		{
			ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
			t=figure.front;
		}
		return ans+(n.fi-t.fi)*(n.se+t.se);
	}
	real square(T)(pair!(T,T)[] figure...)
		if(is(typeof(figure.front*figure.front)==T))
	{
		return abs(orient_square(figure))/2.000000000000000;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	pair!(X,Y) opAssign(A,B)(pair!(A,B) val)
	{
		return this=pair!(X,Y)(val);
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe
		if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
struct Tree(T,alias arg="a+b")
{
	private alias fun=binaryFun!arg;
	private static const size_t n=size_t.max>>1;
	private T[size_t] t;
	this(this){t=t.dup;}
	this(R)(R range) if(isInputRange!R && is(ElementType!(R) : T))
	{
		foreach(pos,i;range) upd(pos,i);
	}
	this(R,alias f)(Tree!(R,f) q) if(is(R:T))
	{
		foreach(i,j;q.t)
		{
			t[i]=j;
		}
	}
	Tree!(T,arg) opAssign(R)(R val)
	{
		return this=Tree!(T,arg)(val);
	}
	private void upd(in size_t v,in size_t vl,in size_t vr,in size_t i,in T x)
	{
		if (vr - vl == 1){t[v] = x;return;}
		size_t vm = (vl + vr) >> 1;
		if(i < vm) upd(2*v, vl, vm, i, x);
		else upd(2*v+1, vm, vr, i, x);
		if(v*2+1 !in t)t[v]=t[v*2];
		else if (v*2 !in t)t[v]=t[v*2+1];
		else t[v]=fun(t[v*2],t[v*2+1]);
	}
	void upd(in size_t i,in T x)
	{
		upd(1,0,n,i,x);
	}
	private T cel(in size_t v,in size_t vl,in size_t vr,in size_t l,in size_t r)
	{
		if(vl==l && r==vr) return t[v];
		size_t vm=(vl+vr) >> 1;
		if(r<=vm) return cel(2*v,vl,vm,l,r);
		else if(l>=vm) return cel(2*v+1,vm,vr,l,r);
		else return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));
	}
	T cel(in size_t l,in size_t r){return cel(1,0,n,l,r);}
};
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int t,x,s;
	loop:while(input(&t,&s,&x))
	{
		if(x==t ||(x>=t+s && (x-t)%s<=1))writeln("YES");
		else writeln("NO");
	}
}

import std.stdio;

void main() {
    // writeln("hello world!");
    // int[3] A;
    // foreach (i; 0 .. 3) {
    //     readf(" %s", &A[i]);
    // }
    // writeln(A);
    // write(A, "\n");
    // writefln("%(%s %)", A);
    int t, s, x;
    readf(" %s %s %s\n", &t, &s, &x);
    // writeln(n, x, y);
    if (t == x) {
        writeln("YES");
        return;
    }
    foreach(k; 1 .. (x + s) / s) {
        if (t + k * s == x || t + k * s + 1 == x) {
            writeln("YES");
            return;
        }
    }
    writeln("NO");
}

import std.stdio;

void main() {
    // writeln("hello world!");
    // int[3] A;
    // foreach (i; 0 .. 3) {
    //     readf(" %s", &A[i]);
    // }
    // writeln(A);
    // write(A, "\n");
    // writefln("%(%s %)", A);
    int t, s, x;
    readf(" %s %s %s\n", &t, &x, &s);
    // writeln(n, x, y);
    if (t == x) {
        writeln("YES");
        return;
    }
    foreach(k; 1 .. (x + s) / s) {
        if (t + k * s == x || t + k * s + 1 == x) {
            writeln("YES");
            return;
        }
    }
    writeln("NO");
}


There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.Find the value of the sum  modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).

The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).

Print the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.

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

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

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

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

// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b;
    a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) {
        y = -y;
      }
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a;
    b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) {
        x = -x;
      }
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

enum MO = 10L^^9 + 7;
enum LIM = 3 * 10^^6;

long[] inv, fac, invFac;
void prepare() {
  inv = new long[LIM];
  fac = new long[LIM];
  invFac = new long[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = MO - ((MO / i) * inv[cast(size_t)(MO % i)]) % MO;
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = (fac[i - 1] * i) % MO;
    invFac[i] = (invFac[i - 1] * inv[i]) % MO;
  }
}
long binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] % MO * invFac[cast(size_t)(n - k)] % MO;
  } else {
    return 0;
  }
}

long power(long a, long e) {
  long x = a % MO, y = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      y = (y * x) % MO;
    }
    x = (x * x) % MO;
  }
  return y;
}

long N;
int K;

void main() {
  prepare();
  try {
    for (; ; ) {
      N = readLong();
      K = readInt();

      auto f = new long[K + 2];
      f[0] = 0;
      foreach (i; 1 .. K + 2) {
        f[i] = (f[i - 1] + power(i, K)) % MO;
      }

      bool zero;
      long prod = 1;
      foreach (i; 0 .. K + 2) {
        const d = (N - i) % MO;
        if (d == 0) {
          zero = true;
        } else {
          prod *= d;
          prod %= MO;
        }
      }
      debug {
        writeln("zero = ", zero, ", prod = ", prod);
      }

      long ans;
      foreach (i; 0 .. K + 2) {
        const d = (N - i) % MO;
        long tmp = f[i];
        if (zero) {
          tmp *= ((d == 0) ? prod : 0);
          tmp %= MO;
        } else {
          tmp *= prod;
          tmp %= MO;
          tmp *= modInv(d, MO);
          tmp %= MO;
        }
        tmp *= invFac[i];
        tmp %= MO;
        tmp *= invFac[K + 1 - i];
        tmp %= MO;
        tmp *= (((K + 1 - i) % 2 != 0) ? -1 : +1);
        tmp %= MO;
        ans += tmp;
        ans %= MO;
      }
      ans = (ans % MO + MO) % MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $$$p$$$ (which may be positive, negative, or zero). To combine their tastes, they invented $$$p$$$-binary numbers of the form $$$2^x + p$$$, where $$$x$$$ is a non-negative integer.For example, some $$$-9$$$-binary ("minus nine" binary) numbers are: $$$-8$$$ (minus eight), $$$7$$$ and $$$1015$$$ ($$$-8=2^0-9$$$, $$$7=2^4-9$$$, $$$1015=2^{10}-9$$$).The boys now use $$$p$$$-binary numbers to represent everything. They now face a problem: given a positive integer $$$n$$$, what's the smallest number of $$$p$$$-binary numbers (not necessarily distinct) they need to represent $$$n$$$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.For example, if $$$p=0$$$ we can represent $$$7$$$ as $$$2^0 + 2^1 + 2^2$$$.And if $$$p=-9$$$ we can represent $$$7$$$ as one number $$$(2^4-9)$$$.Note that negative $$$p$$$-binary numbers are allowed to be in the sum (see the Notes section for an example).

The only line contains two integers $$$n$$$ and $$$p$$$ ($$$1 \leq n \leq 10^9$$$, $$$-1000 \leq p \leq 1000$$$).

If it is impossible to represent $$$n$$$ as the sum of any number of $$$p$$$-binary numbers, print a single integer $$$-1$$$. Otherwise, print the smallest possible number of summands.

Note$$$0$$$-binary numbers are just regular binary powers, thus in the first sample case we can represent $$$24 = (2^4 + 0) + (2^3 + 0)$$$.In the second sample case, we can represent $$$24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$$$.In the third sample case, we can represent $$$24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$$$. Note that repeated summands are allowed.In the fourth sample case, we can represent $$$4 = (2^4 - 7) + (2^1 - 7)$$$. Note that the second summand is negative, which is allowed.In the fifth sample case, no representation is possible.

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

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

        foreach (k; 1 .. 32) {
            long s = k.to!long * p;
            long nn = n.to!long - s;

            if (nn == 0) {
                continue;
            }

            int pcnt = nn > 0 ? nn.popcnt() : (-nn).popcnt();
            if (pcnt <= k && nn >= k) {
                writeln(k);
                return;
            }
        }

        writeln(-1);
        return;
    }

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto p = RD!int;
	int ans = -1;
	foreach (i; 1..33)
	{
		auto x = n - i * p;
		int cnt, cnt_max;
		foreach (j; 0..32)
		{
			auto bit = 1L << j;
			if (x & bit)
			{
				++cnt;
				cnt_max += 2^^j;
			}
		}
		debug writeln("i:", i, " x:", x, " cnt:", cnt);
		if (i >= cnt && i <= cnt_max)
		{
			ans = i;
			break;
		}
	}

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

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

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

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

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

enum LIM = 10^^5;

void main() {
  try {
    for (; ; ) {
      const N = readLong();
      const P = readLong();

      int ans = LIM;
      foreach (k; 0 .. LIM) {
        const m = N - k * P;
        if (m >= 0) {
          if (popcnt(m) <= k && k <= m) {
            chmin(ans, k);
          }
        }
      }
      writeln((ans >= LIM) ? -1 : ans);
    }
  } catch (EOFException e) {
  }
}

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

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

    foreach (k; 1 .. n/2 + 2) {
        long s = k.to!long * p;
        long nn = n.to!long - s;

        if (nn == 0) {
            continue;
        }

        int pcnt = nn > 0 ? nn.popcnt() : (-nn).popcnt();
        if (pcnt <= k && nn >= k) {
            writeln(k);
            return;
        }
    }

    writeln(-1);
    return;
}

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

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

    foreach (k; 1 .. n/2 + 2) {
        int s = k * p;
        long nn = n - s;

        if (nn == 0) {
            continue;
        }

        int pcnt = nn > 0 ? nn.popcnt() : (-nn).popcnt();
        if (pcnt <= k && nn >= k) {
            writeln(k);
            return;
        }
    }

    writeln(-1);
    return;
}

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

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

    foreach (k; 1 .. n/2 + 2) {
        int s = k * p;
        long nn = n - s;

        if (nn > 0 && (nn > 0 ? nn.popcnt() : (-nn).popcnt()) <= k) {
            writeln(k);
            return;
        }
    }

    writeln(-1);
    return;
}


Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.

The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.

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

void main(){
  auto s = readln.strip.split("+").map!(e => e.strip).array;
  s.sort.join("+").writeln;
}

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

void main()
{
    readln
        .strip
        .splitter("+")
        .array
        .sort
        .joiner("+")
        .writeln;
}

import std.stdio;
import std.algorithm.comparison : equal;
import std.uni;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	string line = stdin.readln();
	if (line[line.length - 1] == '\n')
		line.length--;

	string[] nums = split(line, "+");
	nums.sort();
	writef("%s", nums[0]);
	for (int i = 1; i < nums.length; i++)
	{
		writef("+%s", nums[i]);
	}
	return 0;
}

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

int main() {
    auto s = stdin.byLine;
    foreach (line; s) {
        auto arr = line.split("+");
        writeln(arr.sort().join("+"));
    }
    return 0;
}

import std.stdio;

void main(string[] args) {
  string data = std.stdio.readln();
  data = std.string.stripRight(data);

  auto words = std.string.split(data, "+");

  std.algorithm.sort(words);

  auto answer = std.string.join(words, "+");
  //foreach(word; words) {
  //  std.stdio.writeln(word);
  //}

  std.stdio.writeln(answer);
}

// https://codeforces.com/problemset/problem/339/A
import std.algorithm;
import std.range;
import std.stdio;
import std.string;

void main() {
    readln.strip.splitter("+").array.sort.joiner("+").writeln;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.ascii;
import std.algorithm;
import std.range;
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(){
    readln.split.to!(char[]).filter!isDigit.map!"a-'0'".array.sort.writefln!"%(%d+%)";
}

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

void main() {
  readln.chomp.split("+").sort.join("+").writeln;
}


Igor the analyst is at work. He learned about a feature in his text editor called "Replace All". Igor is too bored at work and thus he came up with the following problem:Given two strings x and y which consist of the English letters 'A' and 'B' only, a pair of strings (s, t) is called good if: s and t consist of the characters '0' and '1' only. 1 ≤ |s|, |t| ≤ n, where |z| denotes the length of string z, and n is a fixed positive integer. If we replace all occurrences of 'A' in x and y with the string s, and replace all occurrences of 'B' in x and y with the string t, then the two obtained from x and y strings are equal. For example, if x = AAB, y = BB and n = 4, then (01, 0101) is one of good pairs of strings, because both obtained after replacing strings are "01010101".The flexibility of a pair of strings x and y is the number of pairs of good strings (s, t). The pairs are ordered, for example the pairs (0, 1) and (1, 0) are different.You're given two strings c and d. They consist of characters 'A', 'B' and '?' only. Find the sum of flexibilities of all possible pairs of strings (c', d') such that c' and d' can be obtained from c and d respectively by replacing the question marks with either 'A' or 'B', modulo 109 + 7.

The first line contains the string c (1 ≤ |c| ≤ 3·105). The second line contains the string d (1 ≤ |d| ≤ 3·105). The last line contains a single integer n (1 ≤ n ≤ 3·105).

Output a single integer: the answer to the problem, modulo 109 + 7.

NoteFor the first sample, there are four possible pairs of (c', d').If (c', d') = (AA, A), then the flexibility is 0.If (c', d') = (AB, A), then the flexibility is 0.If (c', d') = (AA, B), then the flexibility is 2, as the pairs of binary strings (1, 11), (0, 00) are the only good pairs.If (c', d') = (AB, B), then the flexibility is 0.Thus, the total flexibility is 2.For the second sample, there are 21 + 22 + ... + 210 = 2046 possible binary strings of length not greater 10, and the set of pairs of good strings is precisely the set of pairs (s, s), where s is a binary string of length not greater than 10.

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

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 7;
alias Mint = ModInt!MO;

enum LIM = 6 * 10^^5 + 10;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
  } else {
    return Mint(0);
  }
}

void main() {
  prepare;

  auto two = new Mint[LIM];
  two[0] = 1;
  foreach (i; 1 .. LIM) {
    two[i] = two[i - 1] * 2;
  }

  try {
    for (; ; ) {
      const C = readToken();
      const D = readToken();
      const N = readInt();

      const CLen = cast(int)(C.length);
      const DLen = cast(int)(D.length);

      const ca = cast(int)(C.count('A'));
      const cb = cast(int)(C.count('B'));
      const cc = CLen - ca - cb;
      const da = cast(int)(D.count('A'));
      const db = cast(int)(D.count('B'));
      const dc = DLen - da - db;

      auto dp = new Mint[N + 1];
      foreach (n; 1 .. N + 1) {
        dp[n] = Mint(N / n)^^2;
      }
      foreach_reverse (n; 1 .. N + 1) {
        for (int m = 2 * n; m <= N; m += n) {
          dp[n] -= dp[m];
        }
      }
      Mint score;
      foreach (n; 1 .. N + 1) {
        score += dp[n] * two[n];
      }
      debug {
        writeln("dp = ", dp);
        writeln("score = ", score);
        Mint scoreBrt;
        foreach (x; 1 .. N + 1) foreach (y; 1 .. N + 1) {
          scoreBrt += two[gcd(x, y)];
        }
        writeln("scoreBrt = ", scoreBrt);
        assert(scoreBrt.x == score.x);
      }

      Mint ans;
      foreach (y; -DLen .. CLen + 1) {
        /*
          a x + b y = a' x + b' y
          x : y = (b' - b) : (a - a')

          a - a' = (ca + ci) - (da + di)
        */
        const num = binom(cc + dc, y - (ca - da) + dc);
        int x = -((CLen - DLen) - y);
        debug {
          writefln("%s %s: %s", x, y, num);
        }
        if (x < 0) {
          x *= -1;
          y *= -1;
        }
        if (x > 0 && y > 0) {
          const g = gcd(x, y);
          const n = N / (max(x, y) / g);
          ans += num * (two[n + 1] - 2);
        } else if (x == 0 && y == 0) {
          ans += num * score;
        }
      }

      // exactly same
      Mint same = 1;
      if (CLen == DLen) {
        foreach (i; 0 .. CLen) {
          if (C[i] == '?' && D[i] == '?') {
            same *= 2;
          } else if (C[i] == '?' || D[i] == '?') {
            same *= 1;
          } else if (C[i] == D[i]) {
            same *= 1;
          } else {
            same *= 0;
          }
        }
        ans -= same * score;
        ans += same * (two[N + 1] - 2)^^2;
      }

      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 7;
alias Mint = ModInt!MO;

enum LIM = 3 * 10^^5 + 10;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
  } else {
    return Mint(0);
  }
}

// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b; a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) y = -y;
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a; b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) x = -x;
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

// M: prime, G: primitive root
class Fft(int M_, int G, int K) {
  import std.algorithm : reverse;
  import std.traits : isIntegral;
  alias M = M_;
  // 1, 1/4, 1/8, 3/8, 1/16, 5/16, 3/16, 7/16, ...
  int[] gs;
  this() {
    static assert(2 <= K && K <= 30, "Fft: 2 <= K <= 30 must hold");
    static assert(!((M - 1) & ((1 << K) - 1)), "Fft: 2^K | M - 1 must hold");
    gs = new int[1 << (K - 1)];
    gs[0] = 1;
    long g2 = G, gg = 1;
    for (int e = (M - 1) >> K; e; e >>= 1) {
      if (e & 1) gg = (gg * g2) % M;
      g2 = (g2 * g2) % M;
    }
    gs[1 << (K - 2)] = cast(int)(gg);
    for (int l = 1 << (K - 2); l >= 2; l >>= 1) {
      gs[l >> 1] = cast(int)((cast(long)(gs[l]) * gs[l]) % M);
    }
    assert((cast(long)(gs[1]) * gs[1]) % M == M - 1,
           "Fft: g^(2^(K-1)) == -1 (mod M) must hold");
    for (int l = 2; l <= 1 << (K - 2); l <<= 1) {
      foreach (i; 1 .. l) {
        gs[l + i] = cast(int)((cast(long)(gs[l]) * gs[i]) % M);
      }
    }
  }
  void fft(int[] xs) const {
    const n = cast(int)(xs.length);
    assert(!(n & (n - 1)), "Fft.fft: |xs| must be a power of two");
    assert(n <= 1 << K, "Fft.fft: |xs| <= 2^K must hold");
    for (int l = n; l >>= 1; ) {
      foreach (i; 0 .. (n >> 1) / l) {
        const(long) g = gs[i];
        foreach (j; (i << 1) * l .. (i << 1 | 1) * l) {
          const t = cast(int)((g * xs[j + l]) % M);
          if ((xs[j + l] = xs[j] - t) < 0) xs[j + l] += M;
          if ((xs[j] += t) >= M) xs[j] -= M;
        }
      }
    }
  }
  void invFft(int[] xs) const {
    const n = cast(int)(xs.length);
    assert(!(n & (n - 1)), "Fft.invFft: |xs| must be a power of two");
    assert(n <= 1 << K, "Fft.invFft: |xs| <= 2^K must hold");
    for (int l = 1; l < n; l <<= 1) reverse(xs[l .. l << 1]);
    for (int l = 1; l < n; l <<= 1) {
      foreach (i; 0 .. (n >> 1) / l) {
        const(long) g = gs[i];
        foreach (j; (i << 1) * l .. (i << 1 | 1) * l) {
          int t = cast(int)((g * (xs[j] - xs[j + l])) % M);
          if (t < 0) t += M;
          if ((xs[j] += xs[j + l]) >= M) xs[j] -= M;
          xs[j + l] = t;
        }
      }
    }
  }
  T[] convolute(T)(inout(T)[] as, inout(T)[] bs) const if (isIntegral!T) {
    const na = cast(int)(as.length), nb = cast(int)(bs.length);
    int n, invN = 1;
    for (n = 1; n < na + nb - 1; n <<= 1) {
      invN = ((invN & 1) ? (invN + M) : invN) >> 1;
    }
    auto xs = new int[n], ys = new int[n];
    foreach (i; 0 .. na) if ((xs[i] = cast(int)(as[i] % M)) < 0) xs[i] += M;
    foreach (i; 0 .. nb) if ((ys[i] = cast(int)(bs[i] % M)) < 0) ys[i] += M;
    fft(xs);
    fft(ys);
    foreach (i; 0 .. n) {
      xs[i] = cast(int)((((cast(long)(xs[i]) * ys[i]) % M) * invN) % M);
    }
    invFft(xs);
    auto cs = new T[na + nb - 1];
    foreach (i; 0 .. na + nb - 1) cs[i] = cast(T)(xs[i]);
    return cs;
  }
  ModInt!M[] convolute(inout(ModInt!M)[] as, inout(ModInt!M)[] bs) const {
    const na = cast(int)(as.length), nb = cast(int)(bs.length);
    int n, invN = 1;
    for (n = 1; n < na + nb - 1; n <<= 1) {
      invN = ((invN & 1) ? (invN + M) : invN) >> 1;
    }
    auto xs = new int[n], ys = new int[n];
    foreach (i; 0 .. na) xs[i] = as[i].x;
    foreach (i; 0 .. nb) ys[i] = bs[i].x;
    fft(xs);
    fft(ys);
    foreach (i; 0 .. n) {
      xs[i] = cast(int)((((cast(long)(xs[i]) * ys[i]) % M) * invN) % M);
    }
    invFft(xs);
    auto cs = new ModInt!M[na + nb - 1];
    foreach (i; 0 .. na + nb - 1) cs[i].x = xs[i];
    return cs;
  }
  int[] convolute(int M1)(inout(ModInt!M1)[] as, inout(ModInt!M1)[] bs) const
      if (M != M1) {
    const na = cast(int)(as.length), nb = cast(int)(bs.length);
    int n, invN = 1;
    for (n = 1; n < na + nb - 1; n <<= 1) {
      invN = ((invN & 1) ? (invN + M) : invN) >> 1;
    }
    auto xs = new int[n], ys = new int[n];
    foreach (i; 0 .. na) xs[i] = as[i].x;
    foreach (i; 0 .. nb) ys[i] = bs[i].x;
    fft(xs);
    fft(ys);
    foreach (i; 0 .. n) {
      xs[i] = cast(int)((((cast(long)(xs[i]) * ys[i]) % M) * invN) % M);
    }
    invFft(xs);
    return xs[0 .. na + nb - 1];
  }
}

enum FFT_K = 20;
alias Fft3_0 = Fft!(1045430273, 3, FFT_K);  // 2^20 997 + 1
alias Fft3_1 = Fft!(1051721729, 6, FFT_K);  // 2^20 1003 + 1
alias Fft3_2 = Fft!(1053818881, 7, FFT_K);  // 2^20 1005 + 1

enum long FFT_INV01 = modInv(Fft3_0.M, Fft3_1.M);
enum long FFT_INV012 = modInv(cast(long)(Fft3_0.M) * Fft3_1.M, Fft3_2.M);
Fft3_0 FFT3_0;
Fft3_1 FFT3_1;
Fft3_2 FFT3_2;
void initFft3() {
  FFT3_0 = new Fft3_0;
  FFT3_1 = new Fft3_1;
  FFT3_2 = new Fft3_2;
}
ModInt!M[] convolute(int M)(inout(ModInt!M)[] as, inout(ModInt!M)[] bs) {
  const cs0 = FFT3_0.convolute(as, bs);
  const cs1 = FFT3_1.convolute(as, bs);
  const cs2 = FFT3_2.convolute(as, bs);
  auto cs = new ModInt!M[cs0.length];
  foreach (i; 0 .. cs0.length) {
    long d0 = cs0[i] % Fft3_0.M;
    long d1 = (FFT_INV01 * (cs1[i] - d0)) % Fft3_1.M;
    if (d1 < 0) d1 += Fft3_1.M;
    long d2 =
        (FFT_INV012 * ((cs2[i] - d0 - Fft3_0.M * d1) % Fft3_2.M)) % Fft3_2.M;
    if (d2 < 0) d2 += Fft3_2.M;
    cs[i] =
        (d0 + Fft3_0.M * d1 + ((cast(long)(Fft3_0.M) * Fft3_1.M) % M) * d2) % M;
  }
  return cs;
}

void main() {
  prepare;
  initFft3;

  auto two = new Mint[LIM];
  two[0] = 1;
  foreach (i; 1 .. LIM) {
    two[i] = two[i - 1] * 2;
  }

  try {
    for (; ; ) {
      const C = readToken();
      const D = readToken();
      const N = readInt();

      const CLen = cast(int)(C.length);
      const DLen = cast(int)(D.length);

      auto fs = new Mint[CLen + 1];
      const ca = cast(int)(C.count('A'));
      const cb = cast(int)(C.count('B'));
      const cc = CLen - ca - cb;
      foreach (i; 0 .. cc + 1) {
        fs[ca + i] = binom(cc, i);
      }

      auto gs = new Mint[DLen + 1];
      const da = cast(int)(D.count('A'));
      const db = cast(int)(D.count('B'));
      const dc = DLen - da - db;
      foreach (i; 0 .. dc + 1) {
        gs[DLen - (da + i)] = binom(dc, i);
      }

      auto hs = convolute(fs, gs);
      debug {
        writeln("hs = ", hs);
      }

      Mint ans;
      foreach (i; 0 .. cast(int)(hs.length)) {
        /*
          a x + b y = a' x + b' y
          x : y = (b' - b) : (a - a')
        */
        int y = i - DLen;
        int x = -((CLen - DLen) - y);
        if (x < 0) {
          x *= -1;
          y *= -1;
        }
        if (x > 0 && y > 0) {
          debug {
            writefln("%s %s: %s", x, y, hs[i]);
          }
          const g = gcd(x, y);
          const n = N / (max(x, y) / g);
          ans += hs[i] * (two[n + 1] - 2);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


You are given a regular polygon with $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$ in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.Calculate the minimum weight among all triangulations of the polygon.

The first line contains single integer $$$n$$$ ($$$3 \le n \le 500$$$) — the number of vertices in the regular polygon.

Print one integer — the minimum weight among all triangulations of the given polygon.

NoteAccording to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) $$$P$$$ into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is $$$P$$$.In the first example the polygon is a triangle, so we don't need to cut it further, so the answer is $$$1 \cdot 2 \cdot 3 = 6$$$.In the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal $$$1-3$$$ so answer is $$$1 \cdot 2 \cdot 3 + 1 \cdot 3 \cdot 4 = 6 + 12 = 18$$$.

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

void main() {
    auto N = readln.chomp.to!long;
    long ans = 0;

    foreach (i; 2..N) ans += i * (i + 1);

    ans.writeln;
}

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

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

    long r;
    foreach (i; 2..N) {
        r += i * (i+1);
    }
    writeln(r);
}

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

void main() {
  int n;
  readf (" %d", &n);
  auto a = new long[][] (n+2, n+2);
  auto c = new int[n + 1];
  foreach (i; 0 .. n + 1) {
    c[i] = i;
  }
  debug stderr.writeln (c);
  long f (int i, int j) {
    if (i == j) return 0;
    assert (i < j);
    if (a[i][j] != 0) return a[i][j];
    long r = long.max;
    foreach (k; i .. j) {
      //r = max (r, f (i, k) + f (k + 1, j) + c[i-1] * c[k] * c[j]);
      r = min (r, f (i, k) + f (k + 1, j) + (i - 1) * k * j);
    }
    debug stderr.writefln ("f(%d, %d) = %d", i, j, r);
    return a[i][j] = r;
  }
  writeln (f (2, n));
}

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

void main() {
  int n;
  readf (" %d", &n);
  if (n == 3) {
    writeln (6);
    return;
  }
  auto a = new long[][] (n+2, n+2);
  auto c = new int[n + 2];
  foreach (i; 0 .. n + 1) {
    c[i] = i;
  }
  c[n+1] = 1;
  debug stderr.writeln (c);
  long f (int i, int j) {
    if (i >= j) {
      j += n;
    }
    if (a[i][j] != 0) return a[i][j];
    long r;
    if (j == i + 2) {
      r = c[i] * c[i + 1] * c[i + 2];
    } else {
      foreach (k; i + 2 .. j) {
        debug stderr.writefln ("i = %d, j = %d, k = %d", i, j, k);
        r = max (r, f (i, k) + f (k, j));
      }
    }
    debug stderr.writefln ("f(%d, %d) = %d", i, j, r);
    return a[i][j] = r;
  }
  writeln (f (1, 1));
}

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

void main() {
  int n;
  readf (" %d", &n);
  if (n == 3) {
    writeln (6);
    return;
  }
  auto a = new long[][] (n+2, n+2);
  auto c = new int[n + 2];
  foreach (i; 0 .. n + 1) {
    c[i] = i;
  }
  c[n+1] = 1;
  debug stderr.writeln (c);
  long f (int i, int j) {
    if (i >= j) {
      j += n;
    }
    if (a[i][j] != 0) return a[i][j];
    long r;
    if (j == i + 2) {
      r = c[i] * c[i + 1] * c[i + 2];
    } else {
      foreach (k; i + 2 .. j - 1) {
        debug stderr.writefln ("i = %d, j = %d, k = %d", i, j, k);
        r = max (r, f (i, k) + f (k, j));
      }
    }
    debug stderr.writefln ("f(%d, %d) = %d", i, j, r);
    return a[i][j] = r;
  }
  writeln (f (1, 1));
}


Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance ci and width wi.Stepan decided to expose some of his cups on a shelf with width d in such a way, that:  there is at least one Physics cup and at least one Informatics cup on the shelf,  the total width of the exposed cups does not exceed d,  from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.

The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 109) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following n lines contains two integers ci and wi (1 ≤ ci, wi ≤ 109) — significance and width of the i-th cup for Physics olympiads. Each of the following m lines contains two integers cj and wj (1 ≤ cj, wj ≤ 109) — significance and width of the j-th cup for Informatics olympiads.

Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0.

NoteIn the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.

module main;

import core.stdc.stdio;
import std.stdio;
import std.algorithm.sorting;
import std.typecons;
alias TL = Tuple!(long, "c", long, "w");

int main(string[] argv)
{
	int n, m;
	long d;
	scanf("%d%d%lld", &n, &m, &d);
	auto a = new TL[n];
	auto b = new TL[m];
	for(int i = 0; i < n; i++)
		scanf("%lld%lld", &a[i].c, &a[i].w);
	for(int i = 0; i < m; i++)
		scanf("%lld%lld", &b[i].c, &b[i].w);
	alias myComp = (x, y) => x.c > y.c || (x.c == y.c && x.w < y.w);
	sort!(myComp)(a);
	sort!(myComp)(b);
	auto u = new TL[n + 1];
	auto v = new TL[m + 1];
	u[0].c = 0; u[0].w = 0;
	for (int i = 0; i < n; ++i) {
	    u[i + 1].c = u[i].c + a[i].c;
	    u[i + 1].w = u[i].w + a[i].w;
	}
	v[0].c = 0; v[0].w = 0;
	for (int i = 0; i < m; ++i) {
	    v[i + 1].c = v[i].c + b[i].c;
	    v[i + 1].w = v[i].w + b[i].w;
	}
	long ans = 0;
    for (int i = 1; i <= n && u[i].w < d; ++i) {
        int l = 0;
        int r = m + 1;
        while (r - l > 1) {
            int x = (l + r) / 2;
            if (u[i].w + v[x].w <= d) {
                l = x;
            } else {
                r = x;
            }
        }
        if (l > 0 && u[i].c + v[l].c > ans) {
            ans = u[i].c + v[l].c;
        }
    }
    writeln(ans);
	return 0;
}


Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are $$$n$$$ students in the school. Each student has exactly $$$k$$$ votes and is obligated to use all of them. So Awruk knows that if a person gives $$$a_i$$$ votes for Elodreip, than he will get exactly $$$k - a_i$$$ votes from this person. Of course $$$0 \le k - a_i$$$ holds.Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows $$$a_1, a_2, \dots, a_n$$$ — how many votes for Elodreip each student wants to give. Now he wants to change the number $$$k$$$ to win the elections. Of course he knows that bigger $$$k$$$ means bigger chance that somebody may notice that he has changed something and then he will be disqualified.So, Awruk knows $$$a_1, a_2, \dots, a_n$$$ — how many votes each student will give to his opponent. Help him select the smallest winning number $$$k$$$. In order to win, Awruk needs to get strictly more votes than Elodreip.

The first line contains integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of students in the school. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$) — the number of votes each student gives to Elodreip.

Output the smallest integer $$$k$$$ ($$$k \ge \max a_i$$$) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.

NoteIn the first example, Elodreip gets $$$1 + 1 + 1 + 5 + 1 = 9$$$ votes. The smallest possible $$$k$$$ is $$$5$$$ (it surely can't be less due to the fourth person), and it leads to $$$4 + 4 + 4 + 0 + 4 = 16$$$ votes for Awruk, which is enough to win.In the second example, Elodreip gets $$$11$$$ votes. If $$$k = 4$$$, Awruk gets $$$9$$$ votes and loses to Elodreip.

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto start = a.maxElement;
		int res = start;
		while (a.sum * 2 >= n * res)
		{
			res += 1;
		}
		writeln (res);
	}
}


This morning, Roman woke up and opened the browser with $$$n$$$ opened tabs numbered from $$$1$$$ to $$$n$$$. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.He decided to accomplish this by closing every $$$k$$$-th ($$$2 \leq k \leq n - 1$$$) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be $$$b$$$) and then close all tabs with numbers $$$c = b + i \cdot k$$$ that satisfy the following condition: $$$1 \leq c \leq n$$$ and $$$i$$$ is an integer (it may be positive, negative or zero).For example, if $$$k = 3$$$, $$$n = 14$$$ and Roman chooses $$$b = 8$$$, then he will close tabs with numbers $$$2$$$, $$$5$$$, $$$8$$$, $$$11$$$ and $$$14$$$.After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it $$$e$$$) and the amount of remaining social network tabs ($$$s$$$). Help Roman to calculate the maximal absolute value of the difference of those values $$$|e - s|$$$ so that it would be easy to decide what to do next.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq k < n \leq 100$$$) — the amount of tabs opened currently and the distance between the tabs closed. The second line consists of $$$n$$$ integers, each of them equal either to $$$1$$$ or to $$$-1$$$. The $$$i$$$-th integer denotes the type of the $$$i$$$-th tab: if it is equal to $$$1$$$, this tab contains information for the test, and if it is equal to $$$-1$$$, it's a social network tab.

Output a single integer — the maximum absolute difference between the amounts of remaining tabs of different types $$$|e - s|$$$.

NoteIn the first example we can choose $$$b = 1$$$ or $$$b = 3$$$. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, $$$e = 2$$$ and $$$s = 0$$$ and $$$|e - s| = 2$$$.In the second example, on the contrary, we can leave opened only tabs that have social networks opened in them.

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

void main() {
    int n,k;
    readf!" %s %s"(n, k);

    int[] a = new int[](n);
    foreach (i; 0 .. n) {
        readf!" %s"(a[i]);
    }

    int total = a.sum;

    int ans = 0;
    foreach (i; 0 .. k) {
        int temp = 0;
        for (int j = i; j < n; j += k) {
            temp += a[j];
        }
        ans = max(ans, (total - temp).abs);
    }

    writeln(ans);
}

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

immutable long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;
    int x1 = A.count(1).to!int;
    int x2 = A.count(-1).to!int;

    int ans = 0;
    foreach (i; 0..N) {
        int cnt1 = 0, cnt2 = 0;
        foreach (j; 0..N) {
            if (abs(j - i) % K == 0) {
                if (A[j] == 1) cnt1 += 1;
                else cnt2 += 1;
            }
        }
        ans = max(ans, abs((x1 - cnt1) - (x2 - cnt2)));
   }

    ans.writeln;
}