Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this array at all.Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula:  The less value c(a) is, the more beautiful the array is.It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.Help Levko and calculate what minimum number c(a) he can reach.

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 ≤ ai ≤ 109).

A single number — the minimum value of c(a) Levko can get.

NoteIn the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.In the third sample he can get array: 1, 2, 3, 4, 5, 6.

import std.stdio;

static immutable int N = 2010;
int n, k;
long[N] a, f;

bool check(long t) {
  f[] = 1;
  foreach (i; 2 .. n + 1)
    foreach (j; 1 .. i)
      if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))
        if (f[i] < f[j] + 1) f[i] = f[j] + 1;
  long max = 0;
  foreach (i; 1 .. n + 1)
    if (max < f[i]) max = f[i];
  return n - max <= k;
}

int main() {
  readf(" %d %d", &n, &k);
  foreach (i; 1 .. n + 1) readf(" %d", &a[i]);
  long l = 0, r = 2000000000;
  while (l < r) {
    long mid = l + r >> 1;
    if (check(mid)) r = mid;
    else l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}

import std.stdio;

static immutable int N = 2010;
int n, k;
long[N] a, f;

bool check(long t) {
  f[] = 0, f[1] = 1;
  foreach (i; 2 .. n + 1)
    foreach (j; 1 .. i)
      if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))
        if (f[i] < f[j] + 1) f[i] = f[j] + 1;
  long max = 0;
  foreach (i; 1 .. n + 1)
    if (max < f[i]) max = f[i];
  return n - max <= k;
}

int main() {
  readf(" %d %d", &n, &k);
  foreach (i; 1 .. n + 1) readf(" %d", &a[i]);
  long l = 0, r = 1000000000;
  while (l < r) {
    long mid = l + r >> 1;
    if (check(mid)) r = mid;
    else l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}

import std.stdio;

static immutable int N = 2010;
int n, k;
long[N] a, f;

bool check(long t) {
  f[] = 1;
  foreach (i; 2 .. n + 1)
    foreach (j; 1 .. i)
      if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))
        if (f[i] < f[j] + 1) f[i] = f[j] + 1;
  long max = 0;
  foreach (i; 1 .. n + 1)
    if (max < f[i]) max = f[i];
  return n - max <= k;
}

int main() {
  readf(" %d %d", &n, &k);
  foreach (i; 1 .. n + 1) readf(" %d", &a[i]);
  long l = 0, r = 1000000000;
  while (l < r) {
    long mid = l + r >> 1;
    if (check(mid)) r = mid;
    else l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}


The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: «[date:time]: message», where for each «[date:time]» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer — Alex — managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format «[time]: message». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written.So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications).We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day.

The first input line contains number n (1 ≤ n ≤ 100). The following n lines contain recordings in format «[time]: message», where time is given in format «hh:mm x.m.». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character «a» or character «p». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20.

Output one number — the minimum amount of days covered by the log.

5
[05:00 a.m.]: Server is started
[05:00 a.m.]: Rescan initialized
[01:13 p.m.]: Request processed
[01:10 p.m.]: Request processed
[11:40 p.m.]: Rescan completed

3
[09:00 a.m.]: User logged in
[08:00 a.m.]: User logged in
[07:00 a.m.]: User logged in

NoteFormally the 12-hour time format is described at:   http://en.wikipedia.org/wiki/12-hour_clock.  The problem authors recommend you to look through these descriptions before you start with the problem.

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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@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;
	}
}
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,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
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.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		int[] a=new int[n];
		foreach(i;0..n)
		{
			int h,m;
			char c;
			readf("%c",&c);
			debug writeln(c);
			readf("%d:%d %c",&h,&m,&c);
			readln;
			if(c=='a')
			{
				if(h!=12)a[i]=60*h+m;
				else a[i]=m;
			}
			else
			{
				if(h!=12)a[i]=60*((h+12)%24)+m;
				else a[i]=60*h+m;
			}
		}
		debug writeln(a);
		int ans=0,k=0;
		foreach(i;1..n)
		{
			if(a[i]==a[i-1])k++;
			else k=1;
			if(k>10)
			{
				if(ans==0)ans++;
				ans++;
				k=1;
			}
			if(a[i]<a[i-1])
			{
				if(ans==0)ans++;
				ans++;
			}
		}
		writeln(max(1,ans));
	}
}

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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@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;
	}
}
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,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
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.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		int[] a=new int[n];
		foreach(i;0..n)
		{
			int h,m;
			char c;
			readf("%c",&c);
			debug writeln(c);
			readf("%d:%d %c",&h,&m,&c);
			readln;
			if(c=='a')a[i]=60*h+m;
			else a[i]=60*((h+12)%24)+m;
		}
		debug writeln(a);
		int ans=0,k=0;
		foreach(i;1..n)
		{
			if(a[i]==a[i-1])k++;
			else k=1;
			if(k>10)
			{
				ans++;
				k=1;
			}
			if(a[i]<a[i-1])
			{
				if(ans==0)ans++;
				ans++;
			}
		}
		writeln(ans);
	}
}

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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@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;
	}
}
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,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
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.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		int[] a=new int[n];
		foreach(i;0..n)
		{
			int h,m;
			char c;
			readf("%c",&c);
			debug writeln(c);
			readf("%d:%d %c",&h,&m,&c);
			readln;
			if(c=='a')
			{
				if(h!=12)a[i]=60*h+m;
				else a[i]=m;
			}
			else
			{
				if(h!=12)a[i]=60*((h+12)%24)+m;
				else a[i]=60*h+m;
			}
		}
		debug writeln(a);
		int ans=0,k=0;
		foreach(i;1..n)
		{
			if(a[i]==a[i-1])k++;
			else k=1;
			if(k>10)
			{
				ans++;
				k=1;
			}
			if(a[i]<a[i-1])
			{
				if(ans==0)ans++;
				ans++;
			}
		}
		writeln(max(1,ans));
	}
}

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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@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;
	}
}
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,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
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.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		int[] a=new int[n];
		foreach(i;0..n)
		{
			int h,m;
			char c;
			readf("%c",&c);
			debug writeln(c);
			readf("%d:%d %c",&h,&m,&c);
			readln;
			if(c=='a')a[i]=60*h+m;
			else a[i]=60*((h+12)%24)+m;
		}
		debug writeln(a);
		int ans=0,k=0;
		foreach(i;1..n)
		{
			if(a[i]==a[i-1])k++;
			else k=1;
			if(k>10)
			{
				ans++;
				k=1;
			}
			if(a[i]<a[i-1])
			{
				if(ans==0)ans++;
				ans++;
			}
		}
		writeln(max(1,ans));
	}
}


Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, \ldots, a_n$$$.For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$|a - b|$$$, where $$$|x|$$$ means the absolute value of $$$x$$$.A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$|a_i - t| \le 1$$$.Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 100$$$) — the lengths of the sticks.

Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them.

NoteIn the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$|1 - 2| + |10 - 4| = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$.In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything.

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string ;
import std.math: abs ;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!long;

void main() {
  GC.disable();

  //
  uint n;
  readf(" %s", n);
  auto a = new int[n];
  foreach(i; 0 .. n) readf(" %s", a[i]);

  int ans = 1_000_000_000;
  int st = -1;
  foreach (t;0..101) {
    int c =0;
    foreach(e;a) {
      if (e < t-1) c+= t-1-e;
      else if (e -1 > t) c += e-1-t;
    }

    ans = min(ans, c);
    if (ans == c) {
      st = t;
    }
  }

  writeln(st, " ", ans);

}


Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates  that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as possible.More formally, let's define  as maximum value of  over all s' that can be obtained by removing exactly x characters from s. Dreamoon wants to know  for all x from 0 to |s| where |s| denotes the length of string s.

The first line of the input contains the string s (1 ≤ |s| ≤ 2 000). The second line of the input contains the string p (1 ≤ |p| ≤ 500). Both strings will only consist of lower case English letters.

Print |s| + 1 space-separated integers in a single line representing the  for all x from 0 to |s|.

NoteFor the first sample, the corresponding optimal values of s' after removal 0 through |s| = 5 characters from s are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}. For the second sample, possible corresponding optimal values of s' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}.

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

void main() {
  string s = readln.strip;
  string t = readln.strip;
  int n = to!(int)(s.length);
  int m = to!(int)(t.length);
  auto to = new int[n];
  to[] = -1;
  for (int i = 0; i < n; i++) {
    int j = i, count = 0;
    while (j < n && count < m) {
      count += s[j] == t[count];
      j++;
    }
    if (count == m) {
      to[i] = j;
    }
  }
  auto f = new int[][](n + 1, n + 1);
  foreach (ref g; f) {
    g[] = int.min;
  }
  for (int i = 0; i <= n; i++) {
    f[i][0] = 0;
  }
  for (int j = 0; j <= n; j++) {
    for (int i = 0; i < n; i++) {
      if (f[i][j] != int.min) {
        if (j + 1 <= n) {
          f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);
        }
        f[i + 1][j] = max(f[i + 1][j], f[i][j]);
        if (to[i] != -1) {
          f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);
        }
      }
    }
  }
  auto ans = new int[n + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      ans[j] = max(ans[j], f[i][j]);
    }
  }
  writefln("%(%s %)", ans);
}

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; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, 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 M, N;
string S, T;

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

		M = S.length;
		N = T.length;
		int[][] dp = new int[][](M + 1, M + 1);
		foreach (i; 0 .. M + 1) {
			dp[i][] = -1;
		}
		dp[0][0] = 0;
		foreach (i; 0 .. M)	{
			{
				bool ok = true;
				int ii = i;
				foreach (j; 0 .. N) {
					for (; ii < M && S[ii] != T[j]; ++ii) {}
					if (ii < M) {
						++ii;
					} else {
						ok = false;
						break;
					}
				}
				if (ok) {
					const dk = (ii - i) - N;
					foreach (k; 0 .. M + 1) if (k + dk <= M) {
						if (dp[i][k] >= 0) {
							chmax(dp[ii][k + dk], dp[i][k] + 1);
						}
					}
				}
			}
			foreach (k; 0 .. M + 1) {
				if (dp[i][k] >= 0) {
					chmax(dp[i + 1][k], dp[i][k]);
				}
			}
			foreach (k; 0 .. M + 1) if (k + 1 <= M) {
				if (dp[i][k] >= 0) {
					chmax(dp[i + 1][k + 1], dp[i][k]);
				}
			}
		}
		writeln(dp[M].to!string.removechars("[],"));
	}
	} catch (EOFException) {}
}

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

void main() {
  string s = readln.strip;
  string t = readln.strip;
  int n = to!(int)(s.length);
  int m = to!(int)(t.length);
  auto to = new int[n];
  to[] = -1;
  for (int i = 0; i < n; i++) {
    int j = i, count = 0;
    while (j < n && count < m) {
      count += s[j] == t[count];
      j++;
    }
    if (count == m) {
      to[i] = j;
    }
  }
  auto f = new int[][](n + 1, n + 1);
  foreach (ref g; f) {
    g[] = int.min;
  }
  for (int i = 0; i <= n; i++) {
    f[i][0] = 0;
  }
  for (int j = 0; j <= n; j++) {
    for (int i = 0; i < n; i++) {
      if (f[i][j] != int.min) {
        if (j + 1 <= n) {
          f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);
        }
        if (to[i] != -1) {
          f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);
        }
      }
    }
  }
  auto ans = new int[n + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      ans[j] = max(ans[j], f[i][j]);
    }
  }
  writefln("%(%s %)", ans);
}

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

void main() {
  string s = readln.strip;
  string t = readln.strip;
  int n = to!(int)(s.length);
  int m = to!(int)(t.length);
  auto to = new int[n];
  to[] = -1;
  for (int i = 0; i < n; i++) {
    int j = i, count = 0;
    while (j < n && count < m) {
      count += s[j] == t[count];
      j++;
    }
    if (count == m) {
      to[i] = j;
    }
  }
  auto f = new int[][](n + 1, n + 1);
  foreach (ref g; f) {
    g[] = int.min;
  }
  f[0][0] = 0;
  for (int j = 0; j <= n; j++) {
    for (int i = 0; i < n; i++) {
      if (f[i][j] != int.min) {
        if (i + 1 <= n && j + 1 <= n) {
          f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);
        }
        if (to[i] != -1) {
          assert(j + to[i] - i - m <= n);
          f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);
        }
      }
    }
  }
  auto ans = new int[n + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      ans[j] = max(ans[j], f[i][j]);
    }
  }
  writefln("%(%s %)", ans);
}


You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The height of the $$$i$$$-th vertex is $$$h_i$$$. You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency $$$e$$$ costs $$$e$$$ coins, where $$$e > 0$$$.It is considered that a vertex $$$x$$$ gets a signal if for some pair of towers at the vertices $$$u$$$ and $$$v$$$ ($$$u \neq v$$$, but it is allowed that $$$x = u$$$ or $$$x = v$$$) with efficiencies $$$e_u$$$ and $$$e_v$$$, respectively, it is satisfied that $$$\min(e_u, e_v) \geq h_x$$$ and $$$x$$$ lies on the path between $$$u$$$ and $$$v$$$.Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.

The first line contains an integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$h_i$$$ ($$$1 \le h_i \le 10^9$$$) — the heights of the vertices. Each of the next $$$n - 1$$$ lines contain a pair of numbers $$$v_i, u_i$$$ ($$$1 \le v_i, u_i \le n$$$) — an edge of the tree. It is guaranteed that the given edges form a tree.

Print one integer — the minimum required number of coins.

NoteIn the first test case it's optimal to install two towers with efficiencies $$$2$$$ at vertices $$$1$$$ and $$$3$$$.In the second test case it's optimal to install a tower with efficiency $$$1$$$ at vertex $$$1$$$ and two towers with efficiencies $$$3$$$ at vertices $$$2$$$ and $$$5$$$.In the third test case it's optimal to install two towers with efficiencies $$$6$$$ at vertices $$$1$$$ and $$$2$$$.

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

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

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

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

int N;
long[] H;
int[] A, B;

int[][] G;
int[] par;
alias Result = Tuple!(long, "m", long, "s");
Result[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  // init
  Result f;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      chmax(f.m, dp[v].m);
      f.s += dp[v].s;
    }
  }
  // calc dp[u]
  if (f.m < H[u]) {
    f.s += (H[u] - f.m);
    f.m = H[u];
  }
  dp[u] = f;
}

void DFS(int u, int p) {
  // init
  Result[] gs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      gs ~= dp[v];
    }
  }
  if (p != -1) {
    // add DP[u]
    gs ~= DP[u];
  }

  const len = cast(int)(gs.length);
  auto ls = new long[len + 1];
  auto rs = new long[len + 1];
  ls[0] = 0;
  foreach (j; 0 .. len) ls[j + 1] = max(ls[j], gs[j].m);
  rs[len] = 0;
  foreach_reverse (j; 0 .. len) rs[j] = max(gs[j].m, rs[j + 1]);
  long sum;
  foreach (j; 0 .. len) sum += gs[j].s;

  int j;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // calc DP[v], removing dp[v]
      Result f;
      f.m = max(ls[j], rs[j + 1]);
      f.s = sum - gs[j].s;
      if (f.m < H[u]) {
        f.s += (H[u] - f.m);
        f.m = H[u];
      }
      DP[v] = f;
      ++j;
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      DFS(v, u);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt;
      H = new long[N];
      foreach (u; 0 .. N) {
        H[u] = readLong;
      }
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt - 1;
        B[i] = readInt - 1;
      }
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      par.length = N;
      dp.length = N;
      DP.length = N;
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      long ans = long.max;
      foreach (u; 0 .. N) if (G[u].length == 1) {
        // init
        Result f;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (v != par[u]) {
            // add dp[v]
            chmax(f.m, dp[v].m);
            f.s += dp[v].s;
          }
        }
        if (u != rt) {
          // add DP[u]
          chmax(f.m, DP[u].m);
          f.s += DP[u].s;
        }
        // calc ans, rooted at u
        if (f.m < H[u]) {
          f.s += (H[u] - f.m);
          f.m = H[u];
        }
        const cost = f.s + f.m;
        debug {
          writeln(u, ": ", f, " ", cost);
        }
        chmin(ans, cost);
      }
      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)); }

int N;
long[] H;
int[] A, B;

int[][] G;
int[] par;
alias Result = Tuple!(long, "m", long, "s");
Result[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  // init
  Result f;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      chmax(f.m, dp[v].m);
      f.s += dp[v].s;
    }
  }
  // calc dp[u]
  if (f.m < H[u]) {
    f.s += (H[u] - f.m);
    f.m = H[u];
  }
  dp[u] = f;
}

void DFS(int u, int p) {
  // init
  Result[] gs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      gs ~= dp[v];
    }
  }
  if (p != -1) {
    // add DP[u]
    gs ~= DP[u];
  }

  const len = cast(int)(gs.length);
  auto ls = new long[len + 1];
  auto rs = new long[len + 1];
  ls[0] = 0;
  foreach (j; 0 .. len) ls[j + 1] = max(ls[j], gs[j].m);
  rs[len] = 0;
  foreach_reverse (j; 0 .. len) rs[j] = max(gs[j].m, ls[j + 1]);
  long sum;
  foreach (j; 0 .. len) sum += gs[j].s;

  int j;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // calc DP[v], removing dp[v]
      Result f;
      f.m = max(ls[j], rs[j + 1]);
      f.s = sum - gs[j].s;
      if (f.m < H[u]) {
        f.s += (H[u] - f.m);
        f.m = H[u];
      }
      DP[v] = f;
      ++j;
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      DFS(v, u);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt;
      H = new long[N];
      foreach (u; 0 .. N) {
        H[u] = readLong;
      }
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt - 1;
        B[i] = readInt - 1;
      }
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      par.length = N;
      dp.length = N;
      DP.length = N;
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      long ans = long.max;
      foreach (u; 0 .. N) if (G[u].length == 1) {
        // init
        Result f;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (v != par[u]) {
            // add dp[v]
            chmax(f.m, dp[v].m);
            f.s += dp[v].s;
          }
        }
        if (u != rt) {
          // add DP[u]
          chmax(f.m, DP[u].m);
          f.s += DP[u].s;
        }
        // calc ans, rooted at u
        if (f.m < H[u]) {
          f.s += (H[u] - f.m);
          f.m = H[u];
        }
        const cost = f.s + f.m;
        debug {
          writeln(u, ": ", f, " ", cost);
        }
        chmin(ans, cost);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $$$1$$$, and in each cell you can either put a platform or leave it empty.In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $$$p$$$, then bounces off it, then bounces off a platform in the cell $$$(p + k)$$$, then a platform in the cell $$$(p + 2k)$$$, and so on every $$$k$$$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $$$p$$$ and $$$k$$$.You already have some level pattern $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$, where $$$a_i = 0$$$ means there is no platform in the cell $$$i$$$, and $$$a_i = 1$$$ means there is one. You want to modify it so that the level can be passed with given $$$p$$$ and $$$k$$$. In $$$x$$$ seconds you can add a platform in some empty cell. In $$$y$$$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $$$p$$$.  Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $$$p$$$ and $$$k$$$?

The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$p$$$, and $$$k$$$ ($$$1 \le p \le n \le 10^5$$$, $$$1 \le k \le n$$$) — the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $$$a_1 a_2 a_3 \ldots a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) — the initial pattern written without spaces. The last line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^4$$$) — the time required to add a platform and to remove the first cell correspondingly. The sum of $$$n$$$ over test cases does not exceed $$$10^5$$$.

For each test case output a single integer — the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable.

NoteIn the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $$$y = 2$$$.In the second test case it's best to add a platform to both cells $$$4$$$ and $$$5$$$: 00000 $$$\to$$$ 00011. The time required is $$$x \cdot 2 = 4$$$.In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $$$10$$$-th: 10110011000 $$$\to$$$ 10110011010. The time required is $$$y \cdot 2 + x = 10$$$.

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		long[int] memo;
		foreach (i; 0..n-p)
		{
			long dfs(int pp)
			{
				if (pp >= n) return 0;
				auto m = memo.get(pp, -1);
				if (m != -1) return m;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				if (pp+k < n)
				{
					auto res2 = (pp+k-p)*y + res;
					ans[ti].chmin(res2);
				}
				memo[pp] = c + res;
				return c + res;
			}
			ans[ti].chmin(dfs(p+i) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				auto res2 = max(pp+k-pos, n-p)*y + res;
				ans[ti].chmin(res2);
				return c + res;
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;
				long c;
				if (a[pp] == '0')
					c = x;
				auto res = dfs(pp+k);
				return min((pp+k-pos)*y + res , c + res);
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				auto res2 = (pp+k-pos)*y + res;
				ans[ti].chmin(res2);
				return min(res2, c + res);
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				if (pp+k < n)
				{
					auto res2 = (pp+k-p)*y + res;
					ans[ti].chmin(res2);
				}
				return c + res;
			}
			ans[ti].chmin(dfs(p+i) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				if (pp+k <= n-p)
				{
					auto res2 = (pp+k)*y + res;
					ans[ti].chmin(res2);
				}
				return c + res;
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				if (pp+k-p < n-p)
				{
					auto res2 = (pp+k-p)*y + res;
					ans[ti].chmin(res2);
				}
				return c + res;
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto p = RD!int-1;
		auto k = RD!int;
		auto a = RD!string;
		auto x = RD;
		auto y = RD;

		ans[ti] = long.max;
		foreach (i; 0..k)
		{
			int pos = p+i;

			long dfs(int pp)
			{
				if (pp >= n) return 0;

				long c = a[pp] == '0' ? x : 0;
				auto res = dfs(pp+k);
				if (pp+k < n-p)
				{
					auto res2 = (pp+k)*y + res;
					ans[ti].chmin(res2);
				}
				return c + res;
			}
			ans[ti].chmin(dfs(pos) + i*y);
		}
	}

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


You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.

The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.

For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.

Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings.

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

void main() {
    auto Q = readln.chomp.to!int;
    long ans;
    while (Q--) {
        auto n = readln.chomp.to!long;
        if (n % 4 == 0) {
            writeln(n / 4);
        } else if (n % 4 == 1) {
            if (n <= 5) writeln(-1);
            else writeln((n-9)/4 + 1);
        } else if (n % 4 == 2) {
            if (n == 2) writeln(-1);
            else writeln((n-6)/4 + 1);
        } else {
            if (n <= 11) writeln(-1);
            else writeln((n-15)/4 + 2);
        }
    }
}

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

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

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

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

void main() {
  enum LIM = 1000;
  auto isnp = new bool[LIM];
  foreach (p; 2 .. LIM) {
    if (!isnp[p]) {
      for (int n = 2 * p; n < LIM; n += p) {
        isnp[n] = true;
      }
    }
  }
  auto dp = new int[LIM];
  dp[] = -1;
  dp[0] = 0;
  foreach (x; 0 .. LIM) {
    if (dp[x] >= 0) {
      foreach (n; 2 .. LIM - x) {
        if (isnp[n]) {
          chmax(dp[x + n], dp[x] + 1);
        }
      }
    }
  }
  debug {
    writeln(dp[0 .. 100]);
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        int ans;
        if (N < LIM) {
          ans = dp[N];
        } else {
          ans = N / 4;
          if (N % 2 != 0) {
            --ans;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main() {
    const long MAX = 10^^9 + 1;
    long[] primes;
    auto table = new bool[](10^^5);
    table.fill(true);
    table[0] = table[1] = false;
    foreach (i; 2..10^^5) {
        if (table[i]) {
            for (int j = i+i; j < 10^^5; j += i) {
                table[j] = false;
            }
        }
    }

    for (long i = 2; i * i < MAX; ++i) {
        if (table[i.to!int]) primes ~= i;
    }

    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto n = readln.chomp.to!long;
        auto nn = n;
        long[] hoge;
        foreach (p; primes) {
            while (n % p == 0) {
                n /= p;
                hoge ~= p;
            }
        }
        if (n > 1) hoge ~= n;
        hoge.sort();
        if (hoge.length <= 1) writeln(-1);
        else writeln(nn / (hoge[0] * hoge[1]));
    }
}

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

void main() {
    const long MAX = 10^^9 + 1;
    long[] primes;
    auto table = new bool[](10^^5);
    table.fill(true);
    table[0] = table[1] = false;
    foreach (i; 2..10^^5) {
        if (table[i]) {
            for (int j = i+i; j < 10^^5; j += i) {
                table[j] = false;
            }
        }
    }

    for (long i = 2; i * i < MAX; ++i) {
        if (table[i.to!int]) primes ~= i;
    }

    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto n = readln.chomp.to!long;
        auto nn = n;
        long[] hoge;
        foreach (p; primes) {
            while (n % p == 0) {
                n /= p;
                hoge ~= p;
            }
        }
        hoge.sort();
        if (hoge.length <= 1) writeln(-1);
        else writeln(nn / (hoge[0] * hoge[1]));
    }
}


Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?

The first line contains one integer n (1 ≤ n ≤ 100). The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).

If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO".

// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio     : readln, writeln;
import std.string    : chomp;
import std.array     : split;
import std.conv      : to;
import std.algorithm : sort;

auto gets() { return readln.chomp; }
auto getNum(T)() { return gets.to!T; }
auto getVals(T)() { return gets.split.to!(T[]); }

void main() {
    auto n = getNum!int;
    auto xs = getVals!int;
    xs.sort;
    writeln(xs[n-1] < xs[n] ? "YES" : "NO");
}


Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$:   Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$.

For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any.

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto N = readln.chomp.to!int;
        auto aa = readln.split.to!(int[]);
        int[] os, es;
        foreach (i, a; aa) {
            if (a%2 == 0) {
                es ~= i.to!int+1;
            } else {
                os ~= i.to!int+1;
            }
        }
        int cnt;
        while (cnt < N-1 && os.length > 1) {
            writeln(os[0], " ", os[1]);
            ++cnt;
            os = os[2..$];
        }
        while (cnt < N-1) {
            writeln(es[0], " ", es[1]);
            ++cnt;
            es = es[2..$];
        }
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [2] cur;
		int [2] [] ans;
		foreach (int i, c; a[0..$ - 1])
		{
			if (cur[c & 1])
			{
				ans ~= [cur[c & 1], i + 1];
				cur[c & 1] = 0;
			}
			else
			{
				cur[c & 1] = i + 1;
			}
		}
		writefln !("%(%(%s %)\n%)") (ans);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		debug writeln("n:", n);
		int cnt;
		int x = -1;
		foreach (i; 0..n*2)
		{
			if (cnt == n-1) break;
			if (a[i] % 2)
			{
				if (x == -1)
				{
					x = i;
				}
				else
				{
					ans ~= [x, i];
					x = -1;
					++cnt;
				}
			}
		}
		debug writeln("ti:", ti, " cnt:", cnt);
		x = -1;
		foreach (i; 0..n*2)
		{
			if (cnt == n-1) break;
			debug writeln("i:", i);
			if ((a[i] % 2) == 0)
			{
				if (x == -1)
				{
					x = i;
				}
				else
				{
					ans ~= [x, i];
					x = -1;
					++cnt;
				}
			}
		}
		debug writeln("ti:", ti, " cnt:", cnt);
	}

	debug writeln(ans);
	foreach (e; ans)
	{
		writeln(e[0]+1, " ", e[1]+1);
	}
	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		debug writeln("n:", n);
		int cnt;
		int x = -1;
		foreach (i; 0..n*2)
		{
			if (cnt == n-1) break;
			if (a[i] % 2)
			{
				if (x == -1)
				{
					x = a[i];
				}
				else
				{
					ans ~= [x, a[i]];
					x = -1;
					++cnt;
				}
			}
		}
		debug writeln("ti:", ti, " cnt:", cnt);
		x = -1;
		foreach (i; 0..n*2)
		{
			if (cnt == n-1) break;
			debug writeln("i:", i);
			if ((a[i] % 2) == 0)
			{
				if (x == -1)
				{
					x = a[i];
				}
				else
				{
					ans ~= [x, a[i]];
					x = -1;
					++cnt;
				}
			}
		}
		debug writeln("ti:", ti, " cnt:", cnt);
	}

	debug writeln(ans);
	foreach (e; ans)
	{
		writeln(e[0], " ", e[1]);
	}
	stdout.flush;
	debug readln;
}


Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.

Print a single number — the maximum number of trees that you can cut down by the given rules.

NoteIn the first sample you can fell the trees like that:   fell the 1-st tree to the left — now it occupies segment [ - 1;1]  fell the 2-nd tree to the right — now it occupies segment [2;3]  leave the 3-rd tree — it occupies point 5  leave the 4-th tree — it occupies point 10  fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

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

void main() {
    int N;
    scanf("%d", &N);

    auto X = new int[](N);
    auto H = new int[](N);
    foreach (i; 0..N) {
        scanf("%d %d", &X[i], &H[i]);
    }

    if (N == 1) {
        writeln(1);
        return;
    }

    auto dp = new int[][](N, 3); // no cut, left, right
    dp[0][0] = 0;
    dp[0][1] = 1;
    dp[0][2] = (X[0] + H[0] < X[1]) ? 1 : 0;

    foreach (i; 1..N) {
        dp[i][0] = dp[i-1].reduce!(max);

        if (X[i-1] + H[i-1] < X[i] - H[i])
            dp[i][1] = dp[i-1][2] + 1;
        if (X[i-1] < X[i] - H[i])
            dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][1]) + 1);

        if (i == N-1)
            dp[i][2] = dp[i][0] + 1;
        else if (X[i] + H[i] < X[i+1])
            dp[i][2] = dp[i][0] + 1;
    }

    dp[N-1].reduce!(max).writeln;
}

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

void main() {
    int N;
    scanf("%d", &N);

    auto X = new int[](N);
    auto H = new int[](N);
    foreach (i; 0..N) {
        scanf("%d %d", &X[i], &H[i]);
    }

    if (N == 1) {
        writeln(H[0] + 1);
        return;
    }

    auto dp = new int[][](N, 3); // no cut, left, right
    dp[0][0] = 0;
    dp[0][1] = 1;
    dp[0][2] = (X[0] + H[0] < X[1]) ? 1 : 0;

    foreach (i; 1..N) {
        dp[i][0] = dp[i-1].reduce!(max);

        if (X[i-1] + H[i-1] < X[i] - H[i])
            dp[i][1] = dp[i-1][2] + 1;
        if (X[i-1] < X[i] - H[i])
            dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][1]) + 1);

        if (i == N-1)
            dp[i][2] = dp[i][0] + 1;
        else if (X[i] + H[i] < X[i+1])
            dp[i][2] = dp[i][0] + 1;
    }

    dp[N-1].reduce!(max).writeln;
}


You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$ and an integer $$$x$$$.You need to select the maximum number of elements in the array, such that for every subsegment $$$a_l, a_{l + 1}, \ldots, a_r$$$ containing strictly more than one element $$$(l < r)$$$, either:   At least one element on this subsegment is not selected, or  $$$a_l + a_{l+1} + \ldots + a_r \geq x \cdot (r - l + 1)$$$.

The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 10$$$): the number of test cases. The descriptions of $$$t$$$ test cases follow, three lines per test case. In the first line you are given one integer $$$n$$$ ($$$1 \leq n \leq 50\,000$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-100\,000 \leq a_i \leq 100\,000$$$). The third line contains one integer $$$x$$$ ($$$-100\,000 \leq x \leq 100\,000$$$).

For each test case, print one integer: the maximum number of elements that you can select.

NoteIn the first example, one valid way to select the elements is $$$[\underline{1}, 2, \underline{3}, \underline{4}, \underline{5}]$$$. All subsegments satisfy at least one of the criteria. For example, for the subsegment $$$l = 1$$$, $$$r = 2$$$ we have that the element $$$2$$$ is not selected, satisfying the first criterion. For the subsegment $$$l = 3$$$, $$$r = 5$$$ we have $$$3 + 4 + 5 = 12 \ge 2 \cdot 3$$$, satisfying the second criterion.We can't select all elements, because in this case for $$$l = 1$$$, $$$r = 2$$$ all elements are selected and we have $$$a_1 + a_2 = 3 < 2 \cdot 2$$$. Thus, the maximum number of selected elements is $$$4$$$.In the second example, one valid solution is $$$[\underline{2}, \underline{4}, 2, \underline{4}, \underline{2}, \underline{4}, 2, \underline{4}, \underline{2}, \underline{4}]$$$.In the third example, one valid solution is $$$[\underline{-10}, -5, \underline{-10}]$$$.In the fourth example, one valid solution is $$$[\underline{9}, \underline{9}, -3]$$$.

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

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

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

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

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

        auto B = new long[N + 1];
        foreach (i; 0 .. N) {
          B[i + 1] = B[i] + (A[i] - X);
        }
        debug {
          writeln("B = ", B);
        }

        auto dp = new int[][](N + 2, 2);
        foreach (i; 0 .. N + 2) {
          dp[i][] = -1;
        }
        dp[0][0] = 0;
        foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {
          // cut
          if (i + 1 <= N + 1) {
            chmax(dp[i + 1][0], dp[i][s]);
          }
          if (i + 2 <= N + 1 && B[i] > B[i + 1]) {
            chmax(dp[i + 2][1], dp[i][s] + 1);
          }
          // 1
          if (i + 1 <= N + 1) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {
              chmax(dp[i + 1][0], dp[i][s] + 1);
            }
          }
          // 2
          if (i + 2 <= N + 1 && B[i] > B[i + 1]) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {
              chmax(dp[i + 2][1], dp[i][s] + 2);
            }
          }
        }

        const ans = dp[$ - 1].maxElement;
        writeln(ans - 1);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto x = readln.strip.to !(int);

		auto f = new int [4] [n + 1];
		f[0][0] = 0;
		foreach (i; 0..n)
		{
			f[i + 1][0] = f[i][].maxElement;
			f[i + 1][1] = f[i][0] + 1;
			if (i >= 1 &&
			    a[i] + a[i - 1] >= x * 2)
			{
				f[i + 1][2] = f[i][1] + 1;
				if (i >= 2 &&
				    a[i] + a[i - 1] + a[i - 2] >= x * 3)
				{
					f[i + 1][3] =
					    f[i][2..$].maxElement + 1;
				}
			}
		}
		f[n][].maxElement.writeln;
	}
}

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

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

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

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

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

        auto B = new long[N + 1];
        foreach (i; 0 .. N) {
          B[i + 1] = B[i] + (A[i] - X);
        }
        debug {
          writeln("B = ", B);
        }

        auto dp = new int[][](N + 2, 2);
        foreach (i; 0 .. N + 2) {
          dp[i][] = -1;
        }
        dp[0][0] = 0;
        foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {
          // cut
          if (i + 1 <= N + 1) {
            chmax(dp[i + 1][0], dp[i][s]);
          }
          if (i + 2 <= N + 1 && B[i] > B[i + 1]) {
            chmax(dp[i + 2][0], dp[i][s] + 1);
          }
          // 1
          if (i + 1 <= N + 1) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {
              chmax(dp[i + 1][0], dp[i][s] + 1);
            }
          }
          // 2
          if (i + 2 <= N + 1 && B[i] > B[i + 1]) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {
              chmax(dp[i + 2][1], dp[i][s] + 2);
            }
          }
        }

        const ans = dp[$ - 1].maxElement;
        writeln(ans - 1);
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

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

        auto B = new long[N + 1];
        foreach (i; 0 .. N) {
          B[i + 1] = B[i] + (A[i] - X);
        }
        debug {
          writeln("B = ", B);
        }

        auto dp = new int[][](N + 2, 2);
        foreach (i; 0 .. N + 2) {
          dp[i][] = -1;
        }
        dp[0][0] = 0;
        foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {
          // cut
          if (i + 1 <= N + 1) {
            chmax(dp[i + 1][0], dp[i][s]);
          }
          // 1
          if (i + 1 <= N + 1) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {
              chmax(dp[i + 1][0], dp[i][s] + 1);
            }
          }
          // 2
          if (i + 2 <= N + 1 && B[i] > B[i + 1]) {
            if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {
              chmax(dp[i + 2][1], dp[i][s] + 2);
            }
          }
        }

        const ans = dp[$ - 1].maxElement;
        writeln(ans - 1);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto x = readln.strip.to !(int);

		auto f = new int [4] [n + 1];
		f[0][0] = 0;
		foreach (i; 0..n)
		{
			f[i + 1][0] = f[i][].maxElement;
			f[i + 1][1] = f[i][0] + 1;
			if (i >= 1 &&
			    a[i] + a[i - 1] >= x * 2)
			{
				f[i + 1][2] = f[i][1] + 1;
				if (i >= 2 &&
				    a[i] + a[i - 1] + a[i - 2] >= x * 3)
				{
					f[i + 1][3] = f[i][2] + 1;
				}
			}
		}
		f[n][].maxElement.writeln;
	}
}


We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$.  The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$) — the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$) — the initial position of point $$$A$$$ and desirable absolute difference.

For each test case, print the minimum number of steps to make point $$$B$$$ exist.

NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int k = cin.readInt;
                if (k == 0) {
                        if (n % 2 == 0) writeln(0);
                        else writeln(1);
                } else {
                        if (k > n) writeln(k - n);
                        else {
                                if (n % 2 == 0 && k % 2 == 1) writeln(1);
                                else if (n % 2 == 1 && k % 2 == 0) writeln(1);
                                else writeln(0);
                        }
                }
        }
}

// unihernandez22
// https://codeforces.com/contest/1401/problem/A
// implementation

import std.stdio;

void main() {
  int n, k, t;
  scanf("%d", &t);
  foreach (_; 0 .. t) {
    scanf("%d %d", &n, &k);
    if (k <= n) {
      if (n%2 == k%2) {
        writeln("0");
      } else {
        writeln("1");
      }
    } else
      writeln(k - n);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		writeln (k > n ? k - n : ((n ^ k) & 1));
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		if (k >= n)
		{
			ans[ti] = k - n;
		}
		else
		{
			n -= k;
			ans[ti] = n % 2;
		}
	}

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

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int k = cin.readInt;
                if (n > k) {
                        if (n % 2 == 0) writeln(0);
                        else writeln(1);
                } else {
                        writeln(k - n);
                }
        }
}