const
	tmax=10000;
	inf=tmax*tmax*tmax*tmax;
type
	arr=array[0..100010] of int64;
	node=record
				t1,t2,tans:int64;
			end;
var
	s1,s2:node;
	a,b:arr;
	n,m,q,i:longint;
	ans,t,tt,x:int64;
procedure qsort(x:arr; l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=x[(l+r) div 2];
	repeat
		while x[i]<mid do inc(i);
		while x[j]>mid do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(x,l,j);
	if i<r then qsort(x,i,r);
end;
function fill(x:int64):node;
var
	t:node;
begin
	t.t1:=x;
	t.t2:=x;
	t.tans:=x;
	exit(t);
end;
function find(x:int64; len:longint; a:arr):node;
var
	l,r,mid:longint;
	ans:node;
begin
	if (len=1) or (x<a[1]) then exit(fill(a[1]));
	if x>a[len] then exit(fill(a[len]));
	l:=1; r:=len;
	while l<r-1 do
	begin
		mid:=(l+r) div 2;
		if x<a[mid] then r:=mid
		else l:=mid;
	end;
	ans.t1:=a[l];
	ans.t2:=a[r];
	if abs(x-a[l])<abs(x-a[r]) then ans.tans:=a[l]
	else ans.tans:=a[r];
	exit(ans);
end;
begin
	readln(n,m,q);
	for i:=1 to n do read(a[i]); qsort(a,1,n);
	for i:=1 to m do read(b[i]); qsort(b,1,m);
	for i:=1 to q do
	begin
		read(x);
		ans:=inf;
		s1:=find(x,n,a); s2:=find(x,m,b);
		tt:=s1.t1;
		t:=x-tt+abs(tt-find(tt,m,b).tans);
		if t<ans then ans:=t;
		tt:=s1.t2;
		t:=tt-x+abs(tt-find(tt,m,b).tans);
		if t<ans then ans:=t;
		tt:=s2.t1;
		t:=x-tt+abs(tt-find(tt,n,a).tans);
		if t<ans then ans:=t;
		tt:=s2.t2;
		t:=tt-x+abs(tt-find(tt,n,a).tans);
		if t<ans then ans:=t;
		writeln(ans);
	end;
end.

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


const
	inf=1000000000000;
type
	arr=array[0..100010] of int64;
	node=record
				t1,t2,tans:int64;
			end;
var
	s1,s2:node;
	a,b:arr;
	n,m,q,i:longint;
	ans,t,x:int64;
procedure qsort(x:arr; l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=x[(l+r) div 2];
	repeat
		while x[i]<mid do inc(i);
		while x[j]>mid do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(x,l,j);
	if i<r then qsort(x,i,r);
end;
function find(x:int64; len:longint; a:arr):node;
var
	l,r,mid:longint;
	ans:node;
begin
	if (len=1) or (x<=a[1]) then
	begin
		ans.t1:=a[1];
		ans.t2:=a[1];
		ans.tans:=a[1];
		exit(ans);
	end;
	if x>=a[len] then
	begin
		ans.t1:=a[len];
		ans.t2:=a[len];
		ans.tans:=a[len];
		exit(ans);
	end;
	l:=1; r:=len;
	while l<r do
	begin
		mid:=(l+r) div 2;
		if x<=a[mid] then r:=mid
		else l:=mid+1;
	end;
	if x=a[r] then
	begin
		ans.t1:=a[r];
		ans.t2:=a[r];
		ans.tans:=a[r];
		exit(ans);
	end;
	if x>a[r] then
	begin
		ans.t1:=a[r];
		ans.t2:=a[r+1];
		if abs(x-a[r])<abs(x-a[r+1]) then ans.tans:=a[r]
		else ans.tans:=a[r+1];
		exit(ans);
	end;
	if x<a[r] then
	begin
		ans.t1:=a[r-1];
		ans.t2:=a[r];
		if abs(x-a[r-1])<abs(x-a[r]) then ans.tans:=a[r-1]
		else ans.tans:=a[r];
		exit(ans);
	end;
end;
begin
	readln(n,m,q);
	for i:=1 to n do read(a[i]); qsort(a,1,n);
	for i:=1 to m do read(b[i]); qsort(b,1,m);
	for i:=1 to q do
	begin
		read(x);
		ans:=inf;
		s1:=find(x,n,a); s2:=find(x,m,b);
		t:=abs(s1.t1-x)+abs(s1.t1-find(s1.t1,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s1.t2-x)+abs(s1.t2-find(s1.t2,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t1-x)+abs(s2.t1-find(s2.t1,n,a).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t2-x)+abs(s2.t2-find(s2.t2,n,a).tans);
		if t<ans then ans:=t;
		writeln(ans);
	end;
end.

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


const
	inf=1000000000000;
type
	arr=array[0..100010] of int64;
	node=record
				t1,t2,tans:int64;
			end;
var
	s1,s2:node;
	a,b:arr;
	n,m,q,i:longint;
	ans,t,x:int64;
procedure qsort(x:arr; l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=x[(l+r) div 2];
	repeat
		while x[i]<mid do inc(i);
		while x[j]>mid do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(x,l,j);
	if i<r then qsort(x,i,r);
end;
function find(x,len:longint; a:arr):node;
var
	l,r,mid:longint;
	ans:node;
begin
	if (len=1) or (x<=a[1]) then
	begin
		ans.t1:=a[1];
		ans.t2:=a[1];
		ans.tans:=a[1];
		exit(ans);
	end;
	if x>=a[len] then
	begin
		ans.t1:=a[len];
		ans.t2:=a[len];
		ans.tans:=a[len];
		exit(ans);
	end;
	l:=1; r:=len;
	while l<r do
	begin
		mid:=(l+r) div 2;
		if x<=a[mid] then r:=mid
		else l:=mid+1;
	end;
	if x=a[r] then
	begin
		ans.t1:=a[r];
		ans.t2:=a[r];
		ans.tans:=a[r];
		exit(ans);
	end;
	if x>a[r] then
	begin
		ans.t1:=a[r];
		ans.t2:=a[r+1];
		if abs(x-a[r])<abs(x-a[r+1]) then ans.tans:=a[r]
		else ans.tans:=a[r+1];
		exit(ans);
	end;
	if x<a[r] then
	begin
		ans.t1:=a[r-1];
		ans.t2:=a[r];
		if abs(x-a[r-1])<abs(x-a[r]) then ans.tans:=a[r-1]
		else ans.tans:=a[r];
		exit(ans);
	end;
end;
begin
	readln(n,m,q);
	for i:=1 to n do read(a[i]); qsort(a,1,n);
	for i:=1 to m do read(b[i]); qsort(b,1,m);
	for i:=1 to q do
	begin
		read(x);
		ans:=inf;
		s1:=find(x,n,a); s2:=find(x,m,b);
		t:=abs(s1.t1-x)+abs(s1.t1-find(s1.t1,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s1.t2-x)+abs(s1.t2-find(s1.t2,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t1-x)+abs(s2.t1-find(s2.t1,n,a).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t2-x)+abs(s2.t2-find(s2.t2,n,a).tans);
		if t<ans then ans:=t;
		writeln(ans);
	end;
end.


const
	inf=1000000000;
var
	a:array[0..100010] of longint;
	n,min,sum,i,t:longint;
begin
	readln(n);
	min:=inf;
	for i:=1 to n do read(a[i]);
	sum:=0;
	while sum<n-1 do
	begin
		for i:=1 to n do
			if (a[i]<min) and (a[i]>0) then
			begin
				min:=a[i];
				t:=i;
			end;
		for i:=1 to n do
			if i<>t then
				if a[i]>0 then
				begin
					a[i]:=a[i] mod min;
					if a[i]=0 then inc(sum);
				end;
	end;
	writeln(min)
end.

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


const
	num:array[1..9] of longint=(2,5,5,4,5,6,3,7,6);
var
	a:array[-10..10] of longint;
	f:array[-1110..10010] of longint;
	i,j,n,m,t:longint;
begin
	readln(n,m);
	for i:=1 to m do read(a[i]);
	for i:=1 to m-1 do
		for j:=1 to m-i do
			if a[j]<a[j+1] then
			begin
				t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t;
			end;
	for i:=-10 to n do f[i]:=-1000000000;
	f[0]:=0;
	for i:=1 to m do
		for j:=num[a[i]] to n do
			if f[j-num[a[i]]]+1>f[j] then
				f[j]:=f[j-num[a[i]]]+1;
	for i:=1 to m do
		while (f[n-num[a[i]]]+1=f[n]) and (n>=num[a[i]]) do
		begin
			n:=n-num[a[i]];
			write(a[i]);
		end;
	writeln;
end.

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654


const
	num:array[1..9] of longint=(2,5,5,4,5,6,3,7,6);
var
	a:array[-10..10] of longint;
	f:array[-1110..10010] of longint;
	i,j,n,m,t:longint;
begin
	readln(n,m);
	for i:=1 to m do read(a[i]);
	for i:=1 to m-1 do
		for j:=1 to m-i do
			if a[j]<a[j+1] then
			begin
				t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t;
			end;
	fillchar(f,sizeof(f),0);
	for i:=1 to m do
		for j:=num[a[i]] to n do
			if f[j-num[a[i]]]+1>f[j] then
				f[j]:=f[j-num[a[i]]]+1;
	for i:=1 to m do
		while (f[n-num[a[i]]]+1=f[n]) and (n>=num[a[i]]) do
		begin
			if (f[n]=1) and (n<>num[a[i]]) then break;
			n:=n-num[a[i]];
			write(a[i]);
		end;
	writeln;
end.


const
	num:array[1..9] of longint=(2,5,5,4,5,6,3,7,6);
var
	a:array[-10..10] of longint;
	f:array[-1110..10010] of longint;
	i,j,n,m,t:longint;
begin
	readln(n,m);
	for i:=1 to m do read(a[i]);
	for i:=1 to m-1 do
		for j:=1 to m-i do
			if a[j]<a[j+1] then
			begin
				t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t;
			end;
	fillchar(f,sizeof(f),0);
	for i:=1 to m do
		for j:=num[a[i]] to n do
			if f[j-num[a[i]]]+1>f[j] then
				f[j]:=f[j-num[a[i]]]+1;
	for i:=1 to m do
	begin
		if f[n]=1 then
			if num[a[i]]=n then
			begin
				write(a[i]);
				break;
			end
			else continue;
		while (f[n-num[a[i]]]+1=f[n]) and (n>=num[a[i]]) do
		begin
			n:=n-num[a[i]];
			write(a[i]);
			if (f[n]=1) and (n<>num[a[i]]) then break;
		end;
	end;
	writeln;
end.


var n,k:integer;begin readln(n,k);if(n>=2*k-1)then writeln('YES') else writeln('NO');end.

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

1\leq N,K\leq 100

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.

Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.

Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO


var
	n,i,e,m,fz,fe,fo,se,sz:longint;

begin
	readln(n);
	fz := 0;
	fe := 0;
	fo := 0;
	se := 0;
	fz := 0;
	for i := 1 to n do
	begin
		readln(e);
		m := fz;
		fz := m + e;

		if fe < m then
			m := fe;
		if e = 0 then
			fe := m + 2
		else
			fe := m + (e and 1);

		if fo < m then
			m := fo;
		fo := m + ((e + 1) and 1);

		if se < m then
			m := se;
		if e = 0 then
			se := m + 2
		else
			se := m + (e and 1);

		if sz < m then
			m := sz;
		sz := m + e;
	end;
	m := fz;
	if fe < m then
		m := fe;
	if fo < m then
		m := fo;
	if se < m then
		m := se;
	if sz < m then
		m := sz;
	writeln(m);
end.

Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

1 \leq L \leq 2\times 10^5

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1


var n,a,b,left,right:integer;
begin
readln(n,a,b);
if(a+b>=n)then left:=a+b-n else left:=0;
if(a>=b)then right:=b else right:=a;
writeln(right,' ',left);
end.

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100


var a:array[1..4] of integer;n1,n2,n3,n4,s,tmp,i,j:integer;
begin
for i:=1 to 4 do begin
read(a[i]);
end;
for i:=1 to 3 do begin
if(a[i]>a[i+1])then begin
j:=i+1;
while(j>1)do begin
if(a[j-1]>a[j])then begin
tmp:=a[j];
a[j]:=a[j-1];
a[j-1]:=tmp;
end;
j:=j-1;
end;
end;
end;
s:=1000*a[1]+100*a[2]+10*a[3]+a[4];
if(s=1479)then writeln('YES') else writeln('NO');
end.

Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO


var
  a,b:array[0..100002]of longint;
  i,j,k,n,m:longint;
  p,q,ans,anq:qword;
function max(a,b:longint):longint;
begin
  if abs(a)<=b then exit(a)
  else exit(b);
end;
procedure zx(s:longint);
var
  i,j,p,q,t,u,o:longint;
  f:array[0..100002]of longint;
begin
  fillchar(f,sizeof(f),0);
  for i:=1 to n do
  if a[i]>s then break;
  p:=i-1;
  q:=i;
  t:=n;
  j:=n div 2;
  while j>0 do
  begin
    dec(j);
    while f[t]=2 do dec(t);
    f[t]:=1;
    dec(t);
    if (p=0) then begin ans:=a[q]+ans; f[q]:=2; inc(q); end
    else if f[q]=1 then begin if f[p]=1 then begin ans:=a[p-1]+ans; f[p-1]:=2; dec(p,3); end else begin ans:=a[p]+ans; f[p]:=2; dec(p,2); end; end
    else if f[p]=1 then begin ans:=a[p-1]+ans; f[p-1]:=2; dec(p,3); end
    else if s-a[p]<=a[q]-s then
    begin
      ans:=a[p]+ans;
      dec(p);
      f[p]:=2;
    end else
    begin
      ans:=a[q]+ans;
      inc(q);
      f[q]:=2;
    end;
  end;
end;
begin
  readln(n,m);
  for i:=1 to n do begin read(a[i]); anq:=a[i]+anq; end;
  for i:=1 to m do
  begin
    readln(b[i]);
    ans:=0;
    zx(b[i]);
    writeln(anq-ans);
  end;
end.

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


uses math;
var
    n, i, j, k, ans : longint;
    a, dp : array[1..100005, 1..5] of longint;

begin
    readln(n);
    for i := 1 to n do
    for j := 1 to 3 do read(a[i, j]);
    fillchar(dp, sizeof(dp), 255);
    for i := 1 to 3 do dp[1, i] := a[1, i];
    for i := 2 to n do
    for j := 1 to 3 do
    for k := 1 to 3 do
    if (j <> k) then dp[i, j] := max(dp[i, j], dp[i - 1, k] + a[i, j]);
    ans := -1;
    for i := 1 to 3 do ans := max(ans, dp[n, i]);
    writeln(ans);
end.

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.


uses crt;
const m = 100000;
var
        g,s: array [0..m,0..3] of longint;
        i,j,k,n,s1: longint;

begin
        clrscr;
        readln (n);
        for i:=1 to n do
        readln ( g[i, 1], g[i,2], g[i,3]);

        for i:=1 to 3 do s[1,i]:= g[1,i];
        for i:=2 to n do
        begin
                j:=0;
                repeat
                        inc(j); k:=0; s1:=0;

                        repeat
                                inc(k);
                                if ( k <> j) and ( s[i-1,k] >= s1) then s1:= s[i-1,k];
                        until k = 3;

                        s[i,j]:= s[i,j] + s1 + g[i,j];
                until j=3;

        end;

        s1:= 0;
        for i:=1 to 3 do if  s[n,i] > s1 then s1:= s[n,i];
        write (s1);
        readln;
end.


uses crt;
const m = 100000;
var
        g,s: array [0..m,0..3] of longint;
        i,j,k,n,s1: longint;

begin
        clrscr;
        readln (n);
        for i:=1 to n do
        readln ( g[i, 1], g[i,2], g[i,3]);

        for i:=1 to 3 do s[1,i]:= g[1,i];
        for i:=2 to n do
        begin
                j:=0;
                repeat
                        inc(j); k:=0; s1:=0;

                        repeat
                                inc(k);
                                if ( k <> j) and ( s[i-1,k] >= s1) then s1:= s[i-1,k];
                        until k = 3;

                        s[i,j]:= s[i,j] + s1 + g[i,j];
                until j=3;

        end;

        s1:= 0;
        for i:=1 to 3 do if  s[n,i] > s1 then s1:= s[n,i];
        write (s[n,i]);
        readln;
end.


uses crt;
var dp:array[0..100009,0..5] of int64;
cost:array[1..100004,1..5] of longint;
i,o,p,l,m,n,a,b,c,k:longint;
rs:int64;
function max(a,b:int64):int64;
begin
if a>b then max:=a
else max:=b;
end;
begin
rs:=-1;
read(n);
for i:=1 to n do begin
        for l:=1 to 3 do begin
                read(cost[i,l]);
                end;
        end;
for i:=1 to n do begin
        dp[1,i]:=cost[1,i];
        end;
for i:=2 to n do begin
        dp[i,1]:=max(dp[i-1,2],dp[i-1,3])+cost[i,1];
        dp[i,2]:=max(dp[i-1,3],dp[i-1,1])+cost[i,2];
        dp[i,3]:=max(dp[i-1,2],dp[i-1,1])+cost[i,3];
        end;
for i:=1 to 3 do begin
        if dp[n,i]> rs then rs:=dp[n,i]
        end;
write(rs);
readln;
readln;
end.


uses crt;
var dp:array[1..100000,1..100000] of longint;
    i,n:longint;
    a,b,c:array[1..100000] of longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n);
    for i := 1 to n do readln(a[i],b[i],c[i]);
    fillchar(dp,sizeof(dp),0);
    for i:=1 to n do
       begin
           dp[i,1]:=max(dp[i-1,1],max(dp[i-1,2],dp[i-1,3]+a[i]));
           dp[i,2]:=max(dp[i-1,2],max(dp[i-1,1],dp[i-1,3]+b[i]));
           dp[i,3]:=max(dp[i-1,3],max(dp[i-1,1],dp[i-1,2]+c[i]));
       end;
       writeln(max(dp[n,1],max(dp[n,2],dp[n,3])));
       readln;
end.


uses crt;
var dp:array[1..100000,1..100000] of longint;
    i,n:longint;
    a,b,c:array[1..100000] of longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n);
    for i := 1 to n do readln(a[i],b[i],c[i]);
    dp[1,1]:=a[1];
    dp[1,2]:=b[1];
    dp[1,3]:=c[1];
    for i:=2 to n do
       begin
           dp[i,1]:=max(dp[i-1,1],max(dp[i-1,2],dp[i-1,3]+a[i]));
           dp[i,2]:=max(dp[i-1,2],max(dp[i-1,1],dp[i-1,3]+b[i]));
           dp[i,3]:=max(dp[i-1,3],max(dp[i-1,1],dp[i-1,2]+c[i]));
       end;
       writeln(max(dp[n,1],max(dp[n,2],dp[n,3])));
       readln;
end.


type
	node=record
				sa,sb,sc:longint;
			end;
var
	a,b,c:array[0..100010] of longint;
	f1,f2:node;
	i,n,ans:longint;
function max(x,y:longint):longint;
begin
	if x>y then exit(x) else exit(y);
end;
begin
	readln(n);
	for i:=1 to n do read(a[i],b[i],c[i]);
	f1.sa:=a[1];
	f1.sb:=b[1];
	f1.sc:=c[1];
	for i:=2 to n do
	begin
		f2.sa:=a[i]+max(f1.sb,f1.sc);
		f2.sb:=b[i]+max(f1.sa,f1.sc);
		f2.sc:=c[i]+max(f1.sa,f1.sb);
		f1:=f2;
	end;
	ans:=max(f1.sa,f1.sb);
	ans:=max(ans,f1.sc);
	writeln(ans);
end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9)+5;

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  va:=0;
  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=0 to va do begin
      if j>=v[i] then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=0 to va do begin
    ans:=ifthen((f[n][j]<=m),j,ans);
  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


uses math;
const fi='knapsack2.inp';
      fo='knapsack2.out';
      maxn=100;
      maxv=200000;
var n,w:int32;
    maxval:uint64;
    we,v:array[1..maxn] of uint32;
    f:array[0..maxn,0..maxv] of uint64;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,w); maxval:=0;
   for i:=1 to n do
   begin
     readln(we[i],v[i]);
     maxval:=maxval+v[i];
   end;
end;
//=============================
function min(x,y:int64):int64;
begin
   if x>y then exit(y)
   else exit(x);
end;
//=============================
procedure xuli;
var i,j:int32;
    ans:uint64;
begin
   for i:=0 to n do
    for j:=0 to maxval do f[i,j]:=maxlongint;
   f[0,0]:=0;
   for i:=0 to n do
    for j:=0 to maxval do
     if f[i,j]<>maxlongint then
      begin
         f[i+1,j]:=min(f[i+1,j],f[i,j]);
         if f[i,j]+we[i+1]<=w then
         f[i+1,j+v[i+1]]:=min(f[i+1,j+v[i+1]],f[i,j]+we[i+1]);
      end;
   for i:=maxval downto 1 do
    if f[n,i]<=w then break;
   write(i);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9);

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=1 to va do begin
      if j>=v[i] then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=1 to va do begin
    ans:=ifthen((f[n][j]<=m),j,ans);
  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


Program KnapSack2;
uses math;
var F:array[0..100005] of int64;
    V,W:array[1..105] of longint;
    i,j,n,k,sum :longint;
    res:longint;
    fi,fo:text;
//-----------------------------
begin
//assign(fi,'KnapSack2.inp');reset(fi);
readln(n,k);
for i:= 1 to n do begin
                  Readln(W[i],V[i]);
                  sum:=sum+V[i];
                  end;
for i:=1 to sum do begin
                   F[i]:=1000000000000;
                   end;
for i:=1 to n do begin
                 for j:=sum downto 1 do begin
                                        if j-V[i]>=0 then begin
                                                          F[j]:=min(F[j],F[j-V[i]]+W[i]);
                                                          end;
                                        end;
                       end;
for i:=sum downto 1 do begin
                       if F[i]<=k then begin
                                       Writeln(i);
                                       break;
                                       end;
                       end;
//readln;
end.


var F:array[0..100000] of int64;
     W,V:array[0..100000] of longint;
     i,n,m,t:longint;  f1:text; max:int64;

 function min(x,y:int64):int64;
 begin
  if x<y then exit(x) else exit(y);
 end;

 begin
  assign(f1,''); reset(F1);
  readln(f1,n,m);
  max:=low(int64);
  for i:=1 to n do readln(f1,W[i],V[i]);
  for i:=1 to 100000 do F[i]:=10000000;
  for i:=1 to n do
   for t:=100000 downto v[i] do F[t]:=min(F[t],F[t-v[i]]+w[i]);
  for i:=1 to 100000 do
   if F[i]<=m then if max<i then max:=i;
  write(max);
  readln;
  close(f1);
 end.

Submission Info


 var F:array[0..100000] of int64;
     W,V:array[0..100000] of longint;
     i,n,m,t:longint;  f1:text; max:int64;

 function min(x,y:int64):int64;
 begin
  if x<y then exit(x) else exit(y);
 end;

 begin
  assign(f1,''); reset(F1);
  readln(f1,n,m);
  max:=low(int64);
  for i:=1 to n do readln(f1,W[i],V[i]);
  for i:=1 to 100000 do F[i]:=10000000;
  for i:=1 to n do
   for t:=100000 downto v[i] do F[t]:=min(F[t],F[t-v[i]]+w[i]);
  for i:=1 to 100000 do
   if F[i]<=m then if max<i then max:=i;
  write(max);
  readln;
  close(f1);
 end.


Program LongestPath;
uses math;
type Rec=record
     x:longint;
     y:longint;
     end;
var Head,Adj :array[0..100005] of longint;
    E:array[1..100005] of Rec;
    Avai:array[1..100005] of boolean;
    n,m,res,i,maxx:longint;
    fi,fo:text;
//------------------------------------------
procedure DFS(i:longint);
var j:longint;
begin
Avai[i]:=false;
inc(res);
maxx:=max(maxx,res);
for j:=Head[i]+1 to Head[i+1] do begin
                                 if Avai[adj[j]] then begin
                                                      DFS(adj[j]);
                                                      dec(res);
                                                      end;
                                 end;
end;
//------------------------------------------
begin
//assign(fi,'LongestPath.inp');reset(fi);
readln(n,m);
for i:=1 to m do begin
                 With E[i] do begin
                              readln(x,y);
                              inc(Head[x]);
                              end;
                 end;
for i:=1 to n do begin
                 Head[i]:=Head[i]+Head[i-1];
                 end;
for i:=m downto 1 do begin
                     With E[i] do begin
                                  Adj[Head[x]]:=y;
                                  dec(Head[x]);
                                  end;
                     end;
Head[n+1]:=m;
fillchar(Avai,sizeof(Avai),true);
for i:=1 to n do begin
                 if Avai[i] then begin
                                 res:=0;
                                 DFS(i);
                                 end;
                 end;
Writeln(maxx-1);
//readln;
end.

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:


Program Grid1;
const max=1000000007;
var A:array[0..1005,0..1005] of char;
    L:array[0..1005,0..1005] of int64;
    //Qx,Qy:array[1..10000000] of longint;
    i,f,r,n,m,j,x,y,u,v :longint;
    fi,fo:text;
//------------------------------------------
begin
//assign(Fi,'Grid1.inp');reset(fi);
readln(n,m);
for i:=1 to n do begin
                 for j:=1 to m do begin
                                  Read(A[i,j]);
                                  end;
                 readln;
                 end;
L[1,1]:=1;
for i:=1 to n do begin
                 for j:=1 to m do begin
                                  if A[i,j]='.' then L[i,j]:=(L[i,j]+L[i-1,j]+L[i,j-1]) mod max;
                                  end;
                 end;
Writeln(L[n,m]);
//readln;
end.

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns.
Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}.
If a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square.
It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W).
As the answer can be extremely large, find the count modulo 10^9 + 7.

Constraints

H and W are integers.

2 \leq H, W \leq 1000

a_{i, j} is . or #.

Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Sample Input 1

3 4
...#
.#..
....

Sample Output 1

3

There are three paths as follows:

Sample Input 2

5 2
..
#.
..
.#
..

Sample Output 2

0

There may be no paths.

Sample Input 3

5 5
..#..
.....
#...#
.....
..#..

Sample Output 3

24

Sample Input 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4

345263555

Be sure to print the count modulo 10^9 + 7.


uses crt;
var
       // f: text;
        x,p: array [-1..3000] of real;
        nhi: array [-1..3000,-1..3000] of real;
        i,j,n: longint;
        s: real;

begin
     //   assign (f, fi); reset(f);
        readln ( n);
        for i:=1 to n do read ( p[i]);
     //   close (F);

     //   assign (f, fo); rewrite (f);
        x[0]:=1;
        for i:=1 to n do
        for j:=i downto 0 do
        begin

                x[j]:= x[j]*(1 - p[i]) + x[j-1]*p[i];
                if (i=n) and (j > n div 2) then s:= s + x[j];

        end;

        writeln ( s);
        readln;
     //   close (f);

end.

Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins.
Find the probability of having more heads than tails.

Constraints

N is an odd number.

1 \leq N \leq 2999

p_i is a real number and has two decimal places.

0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails.
The output is considered correct when the absolute error is not greater than 10^{-9}.

Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872


const fi='STONE.INP';
      fo='STONE.OUT';
      maxn=100;
      maxk=trunc(1e5);

var n,k:longint;
    a:array[1..maxn] of longint;
    f:array[0..maxk] of 0..1; {f=0-> thua, f=1->thang}
//============================
procedure doc;
var i:longint;
begin
  readln(n,k);

  for i:=1 to n do read(a[i]);
end;
//============================
procedure xuli;
var i,j:longint;
    ok:boolean;
begin
  fillchar(f,sizeof(f),0);

  for i:=1 to k do
    begin
      if (i<a[1]) then begin f[i]:=0; continue; end;

      ok:=true;

      for j:=1 to n do
        begin
          if (i>=a[j]) and (f[i-a[j]]=0) then continue;
          ok:=false; break;
        end;

      if ok then f[i]:=1 else f[i]:=0;
    end;

  if f[k]=1 then write('First')
  else write('Second');
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


program Kstones;

const
  fi='kstone.inp';
  fo='kstone.out';

var
  a:array[1..100] of longint;
  f:array[0..100000] of 0..1;
  n,k: longint;

procedure nhap;
var i: longint;
begin

  readln(n,k);
  for i:=1 to n do read(a[i]);

end;

procedure xuli;
var i,j: longint;
begin

  fillchar(f,sizeof(f),0);

  for i:=1 to n do f[a[i]]:=1;

  for i:=1 to k do begin
    for j:=1 to n do begin
      if (i>a[j]) then begin
        if f[i-a[j]]=0 then f[i]:=1;
      end;
    end;
  end;

  if f[k]=0 then write('Second')
  else write('First');

end;

begin

  //assign(input,fi);     reset(input);
  //assign(output,fo);    rewrite(output);

  nhap;
  xuli;

  //close(input);         close(output);

end.


uses math;
const fi='l_deque.inp';
      fo='l_deque.out';
      maxn=3000;
var n:int32;
    sum,f:array[0..maxn,0..maxn] of int64;
    a:array[1..maxn] of int32;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n);
   for i:=1 to n do
   begin read(a[i]); sum[1,i]:=sum[1,i-1]+a[i]; end;
end;
//=============================
function max(x,y:int64):int64;
begin
   if x>y then exit(x)
   else exit(y);
end;
//=============================
procedure xuli;
var i,j:int32;
begin
   for i:=1 to n do
    for j:=n downto i do sum[i,j]:=sum[1,j]-sum[1,i-1];
   for i:=1 to n do f[i,i]:=a[i];
   for i:=n-1 downto 1 do
    for j:=i+1 to n do
     f[i,j]:=max(sum[i,j]-f[i+1,j],sum[i,j]-f[i,j-1]);
   write(2*f[1,n]-sum[1,n]);
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

The answer may not fit into a 32-bit integer type.

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.


uses math;
const fi='DEQUE.INP';
      fo='DEQUE.OUT';
      maxn=3000;

var n:longint;
    a:array[1..maxn] of longint;
    s:array[0..maxn] of int64;
    f:array[1..maxn,1..maxn] of int64;
//============================
procedure doc;
var i:longint;
begin
  readln(n);

  for i:=1 to n do read(a[i]);

end;
//============================
procedure tinhs;
var i:longint;
begin
  s[0]:=0;
  s[1]:=a[1];
  for i:=2 to n do s[i]:=s[i-1]+a[i];
end;
//============================
function sum(i,j:longint):int64;
begin
  sum:=s[j]-s[i-1];
end;
//============================
procedure xuli;
var l,r,i:longint;
begin
  tinhs;

  for i:=1 to n do f[i,i]:=a[i];

  for l:=n-1 to 1 do
    for r:=l+1 to n do
      f[l,r]:=max(sum(l,r)-f[l+1,r],sum(l,r)-f[l,r-1]);

  write(f[1,n]-(sum(1,n)-f[1,n]));
end;
//============================
BEGIN
  //assign(input,fi); reset(input);
  //assign(output,fo); rewrite(output);
  doc;
  xuli;
  //close(input); close(output);
END.


program LDeque;

uses math;

const
  fi='LDeque.inp';
  fo='LDeque.out';

var
  n: longint;
  a:array[0..3000] of int64;

procedure nhap;
var i: longint;
begin

  readln(n);
  for i:=1 to n do read(a[i]);

end;

procedure xuli;
var i,j: longint; k:boolean; ans:int64;
begin

  i:=1; j:=n; k:=true;
  ans:=0;
  repeat
    if a[i]>a[j] then begin
      ans:=ifthen(k,ans+a[i],ans-a[i]);
      inc(i);
    end else begin
      ans:=ifthen(k,ans+a[j],ans-a[j]);
      dec(j);
    end;
    k:=not(k);
  until i>j;

  writeln(ans);

end;

begin

  //assign(input,fi);     reseT(input);
  //assign(output,fo);    rewrite(output);

  nhap;
  xuli;

  //close(input);         close(output);

end.


var a: array[1..1000] of longint;
    dp, p: array[-10..150, -110000..200000] of longint;
    i, j, N, K: longint;
begin
    readln(N, K);
    for i:=1 to N do
        read(a[i]);
    for i:=0 to a[1] do
    begin
        dp[1,i]:=1;
    end;
    for i:=0 to K do
        p[1,i]:=p[1,i-1]+dp[1,i];
    for i:=2 to N do
    begin
        for j:=0 to K do
        begin
            dp[i,j]:=(p[i-1,j]-p[i-1,j-a[i]-1]) mod 1000000007;
            if dp[i,j]<0 then
                dp[i,j]:=dp[i,j]+1000000007;
            p[i,j]:=(p[i,j-1]+dp[i,j]) mod 1000000007;
//            write(dp[i,j], ' ');
        end;
//        writeln;
    end;
    writeln(dp[N,K]);
end.

Score : 100 points

Problem Statement

There are N children, numbered 1, 2, \ldots, N.

They have decided to share K candies among themselves.
Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7.
Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq K \leq 10^5

0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.

Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.

Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

Be sure to print the answer modulo 10^9 + 7.


uses math;
const fi='digitnum.inp';
      fo='digitnum.out';
      maxn=1000;
      modu=trunc(1e9)+7;
var f:array[1..maxn,0..99,0..2] of uint64;
    s:ansistring;
    d,len:int16;
procedure nhap;
begin
   readln(s);
   read(d);
end;
procedure init;
var i,j,x:int32;
begin
   x:=ord(s[1])-48;
   for i:=1 to 9 do
    begin
       if i<x then j:=0
       else if i>x then j:=2
       else j:=1;
       inc(f[1,i mod d,j]);
    end;
end;
function calc(n:ansistring):uint64;
var i,j,r,st,x,newst,newr:int32;
    ans:uint64;
begin
   len:=length(n);
   for i:=1 to len-1 do
    for r:=0 to d-1 do
     for st:=0 to 2 do
      begin
         x:=ord(n[i+1])-48;
         for j:=0 to 9 do
          begin
             newst:=st;
             if newst=1 then
              begin
                 if j<x then newst:=0
                 else if j>x then newst:=2
                 else newst:=1;
              end;
             newr:=(r+j) mod d;
             f[i+1,newr,newst]:=(f[i+1,newr,newst]+f[i,r,st]) mod modu;
          end;
      end;
   ans:=0;
   for i:=1 to len-1 do
    for j:=0 to 2 do inc(ans,f[i,0,j]);
   exit((ans+f[len,0,0]+f[len,0,1]) mod modu);
end;

procedure xuli;
begin
   init;
   write(calc(s));
end;
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.

Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

The sum of the digits in base ten is a multiple of D.

Constraints

All values in input are integers.

1 \leq K < 10^{10000}

1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.

Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.

Sample Input 2

1000000009
1

Sample Output 2

2

Be sure to print the number modulo 10^9 + 7.

Sample Input 3

98765432109876543210
58

Sample Output 3

635270834


uses math;
const fi='digitnum.inp';
      fo='digitnum.out';
      maxn=1000;
      modu=trunc(1e9)+7;
var f:array[1..maxn,0..99,0..2] of int32;
    s:ansistring;
    d,len:int16;
procedure nhap;
begin
   readln(s);
   read(d);
end;
procedure init;
var i,j,x:int32;
begin
   x:=ord(s[1])-48;
   for i:=1 to 9 do
    begin
       if i<x then j:=0
       else if i>x then j:=2
       else j:=1;
       inc(f[1,i mod d,j]);
    end;
end;
function calc(n:ansistring):uint64;
var i,j,r,st,x,newst,newr:int32;
    ans:uint64;
begin
   len:=length(n);
   for i:=1 to len-1 do
    for r:=0 to d-1 do
     for st:=0 to 2 do
      begin
         x:=ord(n[i+1])-48;
         for j:=0 to 9 do
          begin
             newst:=st;
             if newst=1 then
              begin
                 if j<x then newst:=0
                 else if j>x then newst:=2
                 else newst:=1;
              end;
             newr:=(r+j) mod d;
             f[i+1,newr,newst]:=(f[i+1,newr,newst]+f[i,r,st]) mod modu;
          end;
      end;
   ans:=0;
   for i:=1 to len-1 do
    for j:=0 to 2 do inc(ans,f[i,0,j]);
   exit((ans+f[len,0,0]+f[len,0,1]) mod modu);
end;

procedure xuli;
begin
   init;
   write(calc(s));
end;
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


var
  n,i,l,s,loc,dis,j:longint;
  flag:boolean;
  a:array[1..1000000]of longint;
begin
  readln(l,n);
  for i:=1 to n do read(a[i]);
  i:=1;j:=n;
  if a[1]>=(l-a[n]+1)
  then
    begin
	  loc:=1;
	  dis:=a[1];
	  i:=2;
	  flag:=true;
	end
  else
    begin
      j:=j-1;
	  loc:=n;
	  dis:=l-a[n]+1;
	  flag:=false;
	end;
  write(dis,' ');
  while s<n do
  begin
    inc(s);
	if flag
	then
	  if (a[i]-a[loc])>=(a[loc]+a[n]-a[j])
	    then
		  begin
			dis:=dis+a[i]-a[loc];
			flag:=true;
			loc:=i;
			inc(i);
			write(dis,' ');
	      end
		  else
		    begin
			  dis:=dis+a[loc]+a[n]-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			  write(dis,' ');
			end
		else
		  if (a[i]+a[n]-a[loc])>=(a[loc]-a[j])
		  then
			begin
			  dis:=dis+a[i]+a[n]-a[loc];
			  flag:=true;
			  loc:=i;
			  inc(i);
			  write(dis,' ');
	        end
		  else
		    begin
			  dis:=dis+a[loc]-a[j];
			  flag:=false;
			  loc:=j;
			  dec(j);
			  write(dis,' ');
			end;
  end;
  writeln(dis);
end.

Score : 800 points

Problem Statement

Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi.
Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.

Starting at his residence, Takahashi will repeat the following action:

If all trees are burnt, terminate the process.

Specify a direction: clockwise or counter-clockwise.

Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.

When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.

Find the longest possible total distance Takahashi walks during the process.

Partial Score

A partial score can be obtained in this problem:

300 points will be awarded for passing the input satisfying N \leq 2000.

Constraints

2 \leq L \leq 10^9

1 \leq N \leq 2\times 10^5

1 \leq X_1 < ... < X_N \leq L-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L N
X_1
:
X_N

Output

Print the longest possible total distance Takahashi walks during the process.

Sample Input 1

10 3
2
7
9

Sample Output 1

15

Takahashi walks the distance of 15 if the process goes as follows:

Walk a distance of 2 counter-clockwise, burn the tree at the coordinate 2 and stay there.

Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.

Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

Sample Input 2

10 6
1
2
3
6
7
9

Sample Output 2

27

Sample Input 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

Sample Output 3

1204124749


var n:string;cnt,i:integer;begin read(n);cnt:=0;for i:=1 to 4 do begin if(n[i]='2')then cnt:=cnt+1;end;writeln(cnt);end.

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


var n,h,w,a,b,cnt,i:integer;begin readln(n,h,w);cnt:=0;for i:=1 to n do begin readln(a,b);if((h<=a)and(w<=b))then cnt:=cnt+1;end;writeln(cnt);end.

Score : 200 points

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.
The dimensions of the i-th material are A_i \times B_i (A_i vertically and B_i horizontally).

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \times W.
He is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.
When cutting a material, the cuts must be parallel to one of the sides of the material.
Also, the materials have fixed directions and cannot be rotated.
For example, a 5 \times 3 material cannot be used as a 3 \times 5 plate.

Out of the N materials, how many can produce an H \times W plate if properly cut?

Constraints

1 \leq N \leq 1000

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N H W
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

3 5 2
10 3
5 2
2 5

Sample Output 1

2

Takahashi wants a 5 \times 2 plate.

The dimensions of the first material are 10 \times 3. We can obtain a 5 \times 2 plate by properly cutting it.

The dimensions of the second material are 5 \times 2. We can obtain a 5 \times 2 plate without cutting it.

The dimensions of the third material are 2 \times 5. We cannot obtain a 5 \times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \times 2 plate.

Sample Input 2

10 587586158 185430194
894597290 708587790
680395892 306946994
590262034 785368612
922328576 106880540
847058850 326169610
936315062 193149191
702035777 223363392
11672949 146832978
779291680 334178158
615808191 701464268

Sample Output 2

8


var
  a:array[1..1000,1..1000]of longint;
  n,k,i,m,j,t,ans,b,c,d:longint;
  p,q,o,x,y:real;
  s:string;
function max(a,b:longint):longint;
begin
  if a>b then max:=b
  else max:=a;
end;
begin
  readln(c,n,m);
  for i:=1 to c do
  begin
    readln(a[i,1],a[i,2]);
    ans:=ans+max(a[i,1] div n,a[i,2] div m);
  end;
  writeln(ans);
end.


var
	n,p,s:int64;
    f:array[0..1000010] of longint;
    i,j,t:longint;
begin
	read(n,p);
    if n=1 then
    begin
    	writeln(p);
        halt;
    end;
    if p=1 then
    begin
    	writeln(1);
        halt;
    end;
    fillchar(f,sizeof(f),0);
    i:=1;
    t:=trunc(sqrt(p))+1;
    while (p<>1) and (i<=t) do
    begin
    	inc(i);
        while p mod i=0 do
        begin
        	p:=p div i;
            inc(f[i]);
        end;
    end;
    s:=1;
    for i:=2 to t do
    	if f[i]>=n then
        	for j:=1 to f[i] div n do s:=s*i;
    writeln(s);
end.

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


var
	f:boolean;
	n,i,x:longint;
begin
	read(n);
    f:=false;
    for i:=1 to n do
    begin
    	read(x);
        f:=f or (x mod 2=1);
    end;
    if f then writeln('first')
    else writeln('second');
end.

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


var
  n,k,i,m,j,t,ans,b,c,d:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(k);
    ans:=(ans+k mod 2)mod 2;
  end;
  if ans mod 2=1 then writeln('first')
  else writeln('second');
end.


var
	n,i,x,max,s:longint;
begin
	read(n);
    max:=0; s:=0;
    for i:=1 to n do
    begin
    	read(x);
        s:=s+x;
        if x>max then max:=x;
    end;
    s:=s-max;
    if abs(s-max) div 2=0 then writeln('second')
    else writeln('first');
end.


var
  n, i, j : longint;
  ans : qword;
  s : ansistring;
begin
  read(s);
  n:=length(s);
  for i:=1 to n do
    begin
      if s[i]='W' then
        begin
          inc(j);
          ans:=ans+(i-j)
        end
    end;
  write(ans)
end.

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


var
	ch:ansistring;
	n,s,i:longint;
	t:int64;
begin
	readln(ch);
	n:=length(ch);
	s:=1; t:=0;
	for i:=1 to n do
		if ch[i]='W' then
		begin
			t:=t+i-s;
			inc(s);
		end;
	writeln(t);
end.


var
	ch:ansistring;
	n,s,t,i:longint;
begin
	readln(ch);
	n:=length(ch);
	s:=1; t:=0;
	for i:=1 to n do
		if ch[i]='W' then
		begin
			t:=t+i-s;
			inc(s);
		end;
	writeln(t);
end.


var
  a:array[0..200000]of longint;
  f:array[0..200000000]of boolean;
  i,j,k,n,ans:longint;
Procedure qs(l,r:longint);
var
  i,j,t,mid:longint;
begin
  i:=l;j:=r;mid:=a[(l+r)div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        t:=a[i];a[i]:=a[j];a[j]:=t;
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
begin
  readln(n);
  j:=1;
  repeat
    f[j]:=true;
    j:=j*2;
  until j>1E9;
  k:=j;
  for i:=1 to n do
    read(a[i]);
  qs(1,n);
  for i:=1 to n do
    begin
      if a[i]<>0 then
        begin
          for j:=n downto i+1 do
            begin
              if (a[j]<>0)and((f[a[i]+a[j]])or(k=a[i]+a[j])) then
                begin
                  a[i]:=0;a[j]:=0;
                  inc(ans);
                end;
            end;
        end;
    end;
  write(ans);
end.

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


var
  a:array[0..200000]of longint;
  f:array[0..100000000]of boolean;
  i,j,k,n,ans:longint;
Procedure qs(l,r:longint);
var
  i,j,t,mid:longint;
begin
  i:=l;j:=r;mid:=a[(l+r)div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        t:=a[i];a[i]:=a[j];a[j]:=t;
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
begin
  readln(n);
  j:=1;
  repeat
    f[j]:=true;
    j:=j*2;
  until j>1E9;
  k:=j;
  for i:=1 to n do
    read(a[i]);
  qs(1,n);
  for i:=1 to n do
    begin
      if a[i]<>0 then
        begin
          for j:=n downto i+1 do
            begin
              if (a[j]<>0)and((f[a[i]+a[j]])or(k=a[i]+a[j])) then
                begin
                  a[i]:=0;a[j]:=0;
                  inc(ans);
                end;
            end;
        end;
    end;
  write(ans);
end.


var
	a:array[0..35] of int64;
	b:array[0..200010] of longint;
	f:array[0..200010] of boolean;
	ans,i,j,n,x,y,l,r,mid:longint;
function check(x:longint):longint;
begin
	if x<b[1] then exit(-1);
	if x>b[n] then exit(-1);
	l:=1; r:=n;
	while l<=r do
	begin
		mid:=(l+r) div 2;
		if x=b[mid] then exit(mid);
		if x<b[mid] then r:=mid
		else l:=mid+1;
	end;
	exit(-1);
end;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=b[(l+r) div 2];
	repeat
		while b[i]<mid do inc(i);
		while b[j]>mid do dec(j);
		if i<=j then
		begin
			t:=b[i]; b[i]:=b[j]; b[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	readln(n);
	for i:=1 to n do read(b[i]);
	a[1]:=2; ans:=0;
	for i:=2 to 31 do a[i]:=a[i-1]*2;
	qsort(1,n);
	fillchar(f,sizeof(f),true);
	y:=1;
	for i:=1 to n-1 do
	begin
		while a[y]<b[i]+b[n] do inc(y);
		if f[i] then
			for j:=y downto 1 do
			begin
				if a[j]>b[i]+b[n] then continue;
				x:=check(a[j]-b[i]);
				if (x<>-1) and (x<>i) then
				begin
					inc(ans);
					f[i]:=false;
					f[x]:=false;
					break;
				end;
				if a[j]<=b[i] then break;
			end;
	end;
	writeln(ans);
end.


var
  a:array[1..20000]of longint;
  f:array[1..100000000]of longint;
  n,i,j,ans:longint;
begin
  readln(n);
  ans:=1;
  for i:=1 to n do read(a[i]);
  for i:=1 to a[1] do f[i]:=1;
  for i:=2 to n do
    if a[i-1]<a[i] then
      for j := a[i-1]+1 to a[i] do f[j]:=1
    else begin
      for j := a[i]+1 to a[i-1] do f[j]:=0;
      inc(f[a[i]]);
      if f[a[i]]>ans then inc(ans)
    end;
  write(ans)
end.

Score : 700 points

Problem Statement

There are N strings arranged in a row.
It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right.
That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of different characters contained in the strings.

Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.

Sample Input 2

5
2 3 2 1 2

Sample Output 2

2


var
  a:array[1..20000]of longint;
  f:array[1..20000]of longint;
  n,i,j,ans:longint;
begin
  readln(n);
  ans:=1;
  for i:=1 to n do read(a[i]);
  for i:=1 to a[1] do f[i]:=1;
  for i:=2 to n do
    if a[i-1]<a[i] then
      for j := a[i-1]+1 to a[i] do f[j]:=1
    else begin
      for j := a[i]+1 to a[i-1] do f[j]:=0;
      inc(f[a[i]]);
      if f[a[i]]>ans then inc(ans)
    end;
  write(ans)
end.


var
  a:array[1..20000]of longint;
  n,i,ans:longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  ans:=1;
  for i:=2 to n do
    if a[i]<=a[i-1] then inc(ans);
  write(ans)
end.


var
	a,b,c,ch:array[0..200010] of longint;
	n,i,l,r,mid,y,k:longint;
	j:char;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=b[(l+r) div 2];
	repeat
		while b[i]<mid do inc(i);
		while b[j]>mid do dec(j);
		if i<=j then
		begin
			t:=b[i]; b[i]:=b[j]; b[j]:=t;
			t:=c[i]; c[i]:=c[j]; c[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
function check(x:longint):boolean;
begin
	for i:=1 to n do ch[i]:=0;
	for i:=1 to n do
	begin
		y:=a[i];
		inc(ch[y]);
		while (ch[y]>x) and (y>1) do
		begin
			ch[y]:=ch[y]-x;
			dec(y);
			inc(ch[y]);
		end;
		if ch[1]>x then exit(false);
	end;
	exit(true);
end;
begin
	readln(n);
	for i:=1 to n do
	begin
		read(a[i]);
		c[i]:=i;
		b[i]:=a[i];
	end;
	qsort(1,n);
	for i:=1 to n do a[c[i]]:=i;
	l:=1; r:=26;
	while l<r do
	begin
		mid:=(l+r) div 2;
		if check(mid) then r:=mid
		else l:=mid+1;
	end;
	writeln(r+1);
end.


var
	n,a,b,i,s:longint;
begin
	readln(n);
	s:=0;
	read(a);
	for i:=2 to n do
	begin
		read(b);
		if a>b then inc(s);
		a:=b;
	end;
	writeln(s);
end.


var
 n,i,j,t,k,min:longint;
 a:array[0..100005] of longint;
procedure qsort(l,r:longint);
var
 i,j,t,m:longint;
begin
 i:=l;j:=r;m:=a[(l+r) div 2];
 repeat
  while a[i]<m do inc(i);
  while a[j]>m do dec(j);
  if i<=j then
   begin
    t:=a[i];a[i]:=a[j];a[j]:=t;
    inc(i);dec(j);
   end;
 until i>j;
 if i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;
begin
 readln(n,k);
 for i:=1 to n do read(a[i]);
 qsort(1,n);
 min:=a[k+1]-a[1];
 for i:=1 to n-k+2 do if a[i+k-1]-a[i]<min then min:=a[i+k-1]-a[i];
 writeln(min);
end.

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


type
	intArray = array[1..100000] of longint;
var i,j,m,n,k : integer;
    t,u       : longint;
    h         : intArray;

procedure	swap(var a,b:longint);
var tmp:longint;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:intArray;start_index,end_index:integer);
var i,j,x,t:longint;
begin
	x:=a[(start_index+end_index)div 2];
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i]<x)do i:=i+1;
		while(x<a[j])do j:=j-1;
		if(i<j)then
		begin
			swap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

begin
  readln(n,k);
  for i := 0 to n - 1 do readln(h[i]);
  QuickSort(h, 0, n - 1);
  t := 999999999;
  for i := 0 to n - k do begin
    u := h[i + k - 1] - h[i];
    if t > u then t := u
  end;
  writeln(t)
end.


var n:integer;
x,ans:int64;
procedure find(i:integer);
begin
	if x<=3 then
	begin
		ans:=ans+x;
		x:=0;
	end;
	if x>3 then
	begin
		ans:=ans+3;
		x:=x-3;
		if x<=i then
		begin
			x:=0;
		end;
		if x>i then
		begin
			ans:=ans+1;
			x:=x-i-1;
			if x<=i then x:=0;
			if x>i then x:=x-i;
		end;
	end;
end;
procedure p(y:integer);
begin
	if y>1 then p(y-1);
	find(y);
	if x=0 then begin write(ans);halt;end;
	if y>1 then p(y-1);
end;
begin
	read(n);read(x);
	ans:=0;
	x:=x-n;
	if x>0 then
		p(n-1);
	write(ans);
end.

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>700000000 then begin writeln(17656+f(7,0,0,1));exit;end;
	writeln(f(0,0,0,0));
end.

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>777700000 then begin writeln(26484);exit;end;
	writeln(f(0,0,0,0));
end.


var n : longint;
    i,sum : integer;
    e : boolean;

procedure sft(p : longint; d : integer; s,f,t : boolean);
begin
  if d = 0 then begin
    if s and f and t then if p <= n then sum := sum + 1 else e := true
  end else begin
    sft(p * 10 + 3, d - 1, s, f, true);
    sft(p * 10 + 5, d - 1, s, true, t);
    sft(p * 10 + 7, d - 1, true, f, t)
  end
end;

begin
  readln(n);
  sum := 0;
  e := false;
  for i := 3 to 9 do begin
    sft(0, i, false, false, false);
    if e then break;
  end;
  writeln(sum)
end.


var n : longint;
    i,sum : integer;
    e : boolean;

procedure sft(p : longint; d : integer; s,f,t : boolean);
begin
  if d = 0 then begin
    if s and f and t then if p < n then sum := sum + 1 else e := true
  end else begin
    sft(p * 10 + 3, d - 1, s, f, true);
    sft(p * 10 + 5, d - 1, s, true, t);
    sft(p * 10 + 7, d - 1, true, f, t);
  end
end;

begin
  readln(n);
  sum := 0;
  e := false;
  for i := 3 to 9 do begin
    sft(0, i, false, false, false);
  end;
  writeln(sum)
end.


var
	n,ans:Longint;
procedure f(x:int64;a,b,c:Boolean);
begin
	if x>n then exit;
	if a and b and c then inc(ans);
	f(x*10+3,True,b,c);
	f(x*10+5,a,True,c);
	f(x*10+7,a,b,True);
end;
begin
	read(n);
	if n>777777752 then begin writeln(26484);exit;end;
	f(0,False,False,False);
	writeln(ans);
end.


var s:string;
tot,nn:longint;
a:array[1..10]of integer;
b:array[1..10]of 0..1;
procedure dfs(n,x:longint);
var num,i:longint;
begin
	if (n>=4) and (x<=nn) then
	begin
		{for i:=1 to n-1 do write(a[i]);
		writeln;}
		fillchar(b,sizeof(b),0);
		for i:=1 to n-1 do b[a[i]]:=1;
		if (b[3]=1) and (b[5]=1) and(b[7]=1) then inc(tot);

	end;
	if n>length(s) then exit;
		a[n]:=3;
		dfs(n+1,x*10+3);
		a[n]:=5;
		dfs(n+1,x*10+5);
		a[n]:=7;
		dfs(n+1,x*10+7);
end;
begin
    readln(s);
	val(copy(s,1,length(s)),nn);
    fillchar(a,sizeof(a),0);
    tot:=0;
    dfs(1,0);
    write(tot);
end.


var
    i,j,k,ans,n:longint;
    s:array[0..110] of longint;
    f:array[0..110,0..110,0..110] of boolean;
procedure prime(x:longint);
begin
    j:=2;
    while x>=j do
    begin
        while x mod j=0 do
        begin
            inc(s[j]);
            x:=x div j;
        end;
        inc(j);
    end;
end;
begin
    read(n);
    fillchar(s,sizeof(s),0);
    for i:=2 to n do prime(i);
    fillchar(f,sizeof(f),true);
    ans:=0;
    for i:=2 to n do
        for j:=2 to n do
            if i<>j then
            begin
                if (s[i]>=2) and (s[j]>=24) then inc(ans);
                if (s[i]>=4) and (s[j]>=14) then inc(ans);
                if (s[i]>=2) and (s[j]>=4) then
                    for k:=2 to n do
                        if (i<>k) and (j<>k) then
                            if s[k]>=4 then
                                if f[i,j,k] then
                                begin
                                    inc(ans);
                                    f[i,j,k]:=false;
                                    f[i,k,j]:=false;
                                end;
            end;
    for i:=1 to n do
        if s[i]>=74 then inc(ans);
    writeln(ans);
end.

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


var
    i,j,k,ans,n:longint;
    s:array[0..110] of longint;
    f:array[0..110,0..110,0..110] of boolean;
procedure prime(x:longint);
begin
    j:=2;
    while x>=j do
    begin
        while x mod j=0 do
        begin
            inc(s[j]);
            x:=x div j;
        end;
        inc(j);
    end;
end;
begin
    read(n);
    fillchar(s,sizeof(s),0);
    for i:=2 to n do prime(i);
    for i:=2 to n do writeln('s[',i,']=',s[i]);
    fillchar(f,sizeof(f),true);
    ans:=0;
    for i:=2 to n do
        for j:=2 to n do
            if i<>j then
            begin
                if (s[i]>=2) and (s[j]>=24) then inc(ans);
                if (s[i]>=4) and (s[j]>=14) then inc(ans);
                if (s[i]>=2) and (s[j]>=4) then
                    for k:=2 to n do
                        if (i<>k) and (j<>k) then
                            if s[k]>=4 then
                                if f[i,j,k] then
                                begin
                                    inc(ans);
                                    f[i,j,k]:=false;
                                    f[i,k,j]:=false;
                                end;
            end;
    for i:=1 to n do
        if s[i]>=74 then inc(ans);
    writeln(ans);
end.


{
	75=3*25
		=3*5*5
		=5*15
	a^2 b^24
	a^2 b^4 c^4
	a^4 b^14
}
var
	i,j,k,ans,n:longint;
	s:array[0..110] of longint;
	f:array[0..110,0..110,0..110] of boolean;
procedure prime(x:longint);
begin
	j:=2;
	while x>=j do
	begin
		while x mod j=0 do
		begin
			inc(s[j]);
			x:=x div j;
		end;
		inc(j);
	end;
end;
begin
	read(n);
	fillchar(s,sizeof(s),0);
	for i:=2 to n do prime(i);
	fillchar(f,sizeof(f),true);
	ans:=0;
	for i:=2 to n do
		for j:=2 to n do
			if i<>j then
			begin
				if (s[i]>=2) and (s[j]>=24) then inc(ans);
				if (s[i]>=4) and (s[j]>=14) then inc(ans);
				if (s[i]>=2) and (s[j]>=4) then
					for k:=2 to n do
						if (i<>k) and (j<>k) then
							if s[k]>=4 then
								if f[i,j,k] then
								begin
									inc(ans);
									f[i,j,k]:=false;
									f[i,k,j]:=false;
								end;
			end;
	for i:=1 to n do
		if s[i]>=74 then inc(ans);
	writeln(ans);
end.


var
	a,b,x,y,num,pro:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,k,l1,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			t:=num[pro[i]]; num[pro[i]]:=num[pro[j]]; num[pro[j]]:=t;
			t:=pro[num[i]]; pro[num[i]]:=pro[num[j]]; pro[num[j]]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
		pro[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


var
	a,b,x,y,num,pro:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,k,l1,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			t:=num[pro[i]]; num[pro[i]]:=num[pro[j]]; num[pro[j]]:=t;
			pro[num[i]]:=j; pro[num[i]]:=i;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
		pro[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.


var
  n,m,i,j,zrt:longint;
  p,y,a,b,c,o:array[0..1000000]of longint;
Procedure ch(var x,y:longint);
var
  t:longint;
begin
  t:=x;x:=y;y:=t;
end;
Procedure qs(l,r:longint);
var
  i,j,mid1,mid2:longint;
begin
  i:=l;j:=r;mid1:=p[(l+r)div 2];mid2:=y[(l+r)div 2];
  repeat
    while (p[i]<mid1)or((p[i]=mid1)and(y[i]<mid2)) do inc(i);
    while (p[j]>mid1)or((p[j]=mid1)and(y[j]>mid2)) do dec(j);
    if i<=j then
      begin
        ch(p[i],p[j]);
        ch(y[i],y[j]);
        ch(c[i],c[j]);
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(p[i],y[i]);
      a[i]:=p[i];c[i]:=i;
    end;
  qs(1,m);
  for i:=1 to m do
    begin
      if (i=1)or(p[i]<>p[i-1]) then j:=1 else inc(j);
      o[c[i]]:=j;
    end;
  for i:=1 to m do
    begin
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if a[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
       end;
      write(a[i]);
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if o[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
        end;
      writeln(o[i]);
  end;
      end.


var
	a,b,x,y,num:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			num[num[i]]:=j; num[num[j]]:=i;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.


Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

A valid amidakuji is an amidakuji that satisfies the following conditions:

No two horizontal lines share an endpoint.

The two endpoints of each horizontal lines must be at the same height.

A horizontal line must connect adjacent vertical lines.

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

Constraints

H is an integer between 1 and 100 (inclusive).

W is an integer between 1 and 8 (inclusive).

K is an integer between 1 and W (inclusive).

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

Sample Input 6

15 8 5

Sample Output 6

437760187

Be sure to print the answer modulo 1\ 000\ 000\ 007.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r,x,y,ttt,flag:longint;
	s:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0; ttt:=(1+n) div 2;
	t1:=a[ttt];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		flag:=0;
		while x=y do
		begin
			inc(flag);
			x:=abs(a[r-flag]-a[l]);
			y:=abs(a[r]-a[l+flag]);
		end;
		if x>y then
		begin
			t2:=a[r];
			dec(r);
			if r=ttt then dec(r);
		end;
		if x<y then
		begin
			t2:=a[l];
			inc(l);
			if l=ttt then inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r:longint;
	s:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r:longint;
	s,t:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if a[r]-t1>t1-a[l] then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2 then inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if a[r]-t1>t1-a[l] then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2+1 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2+1 then inc(l);
		end;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r:longint;
	s:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if a[r]-t1>t1-a[l] then
		begin
			t2:=a[r];
			dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.