if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main()
  n=parse(Int,readline())
  a=[parse(Int,readline()) for i=1:n]
  ans=0
  now=1
  while now != 2
    ans+=1
    pre=now
    now=a[now]
    a[pre]=0
    if a[now]==0
      ans=-1
      break
    end
  end
  println(ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


function find(u,parent)
	if u==parent[u]
		u
	else
		parent[u]=find(parent[u],parent)
	end
end
function unite(u,v,parent,size)
	u=find(u,parent)
	v=find(v,parent)
	if u!=v
		if size[u]>size[v]
			parent[v]=u
			size[u]+=size[v]
		else
			parent[u]=v
			size[v]+=size[u]
		end
		true
	else
		false
	end
end
function main()
	N=parse(Int,readline())
	parent=collect(1:N)
	size=ones(Int,N)
	X=Pair{Int,Int}[]
	Y=Pair{Int,Int}[]
	id=1
	for s=readlines()
		x,y=map(x->parse(Int,x),split(s))
		push!(X,x=>id)
		push!(Y,y=>id)
		id+=1
	end
	G=Pair{Int,Pair{Int,Int}}[]
	sort!(X)
	sort!(Y)
	for i=1:N-1
		push!(G,abs(X[i].first-X[i+1].first)=>(X[i].second=>X[i+1].second))
		push!(G,abs(Y[i].first-Y[i+1].first)=>(Y[i].second=>Y[i+1].second))
	end
	sort!(G)
	ans=0
	for (c,(x,y))=G
		if unite(x,y,parent,size)
			ans+=c
		end
	end
	println(ans)
end
main()

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

Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

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.


(n, m) = parse.([Int], split(readline()))
MOD = 10^9 + 7

if 1 < abs(n - m)
    ans = 0
else
    ans = 1
    for i in 1 : n
        ans *= i
        ans %= MOD
    end
    for i in 1 : m
        ans *= i
        ans %= MOD
    end
    if n == m
        ans *= 2
        ans %= MOD
    end
end
println(ans)

Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	hh,ww,h,w = readline() |> split |> parseMap
	if hh%h==0&&ww%w==0
		println("No")
	else
		println("Yes")
		if h==1&&w==2
			for i in 1:hh
				for j in 1:ww-1
					if j%w==0
						print(-ww," ")
					else
						print(ww-1," ")
					end
				end
				println(ww-1)
			end
		elseif h==2&&w==1
			for i in 1:hh
				for j in 1:ww-1
					if i%h==0
						print(-hh," ")
					else
						print(hh-1," ")
					end
				end
				if i%h==0
					println(-hh)
				else
					println(hh-1)
				end
			end
		else
			for i in 1:hh
				for j in 1:ww-1
					if i%h==0&&j%w==0
						print(-2*h*w+1," ")
					else
						print(2, " ")
					end
				end
				if i%h==0&&ww%w==0
					println(-2*h*w+1)
				else
					println(2)
				end
			end
		end
	end
end

main()

Score : 700 points

Problem Statement

You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W).
Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:

The matrix has H rows and W columns.

Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).

The sum of all the elements of the matrix is positive.

The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.

Constraints

1 ≤ h ≤ H ≤ 500

1 ≤ w ≤ W ≤ 500

Input

Input is given from Standard Input in the following format:

H W h w

Output

If there does not exist a matrix that satisfies all of the conditions, print No.

Otherwise, print Yes in the first line, and print a matrix in the subsequent lines in the following format:

a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Here, a_{ij} represents the (i,\ j) element of the matrix.

Sample Input 1

3 3 2 2

Sample Output 1

Yes
1 1 1
1 -4 1
1 1 1

The sum of all the elements of this matrix is 4, which is positive.
Also, in this matrix, there are four subrectangles with 2 rows and 2 columns as shown below. For each of them, the sum of all the elements inside is -1, which is negative.

Sample Input 2

2 4 1 2

Sample Output 2

No

Sample Input 3

3 4 2 3

Sample Output 3

Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7


a,b,c = parse.(split(readline()))
print((10*b+c)%4 == 0?"YES":"NO")

Score : 100 points

Problem Statement

AtCoDeer has three cards, one red, one green and one blue.

An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.

We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.

Is this integer a multiple of 4?

Constraints

1 ≤ r, g, b ≤ 9

Input

Input is given from Standard Input in the following format:

r g b

Output

If the three-digit integer is a multiple of 4, print YES (case-sensitive); otherwise, print NO.

Sample Input 1

4 3 2

Sample Output 1

YES

432 is a multiple of 4, and thus YES should be printed.

Sample Input 2

2 3 4

Sample Output 2

NO

234 is not a multiple of 4, and thus NO should be printed.


(r, g, b) = parse.([Int], split(readline()))

println((10 * g + b) % 4 == 0 ? "YES" : "NO")


readline()
a=0
b=0
T=zeros(Int,114514)
for x=map(x->parse(Int,x),split(readline()))
  if x<3200
    x=div(x,400)
    if T[x+1]==0
      a+=1
    end
    T[x+1]=1
  else
    b+=1
  end
end
println("$(a==0?1:a) $(a+b)")

Score : 300 points

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.

Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.

Find the minimum and maximum possible numbers of different colors of the users.

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

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 minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

The user with rating 2100 is "yellow", and the others are "orange". There are two different colors.

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

The user with rating 1100 is "green", the user with rating 1900 is blue and the user with rating 2800 is "red".

If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.

If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors.

Sample Input 3

20
800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990

Sample Output 3

1 1

All the users are "green", and thus there is one color.


s=readline()
f=false
for i=1:endof(s)
	for j=1:endof(s)
		f|=(i!=j)&(s[i]==s[j])
	end
end
print(f?"no":"yes")

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


s = split(chomp(readline()), "")
println(length(s) == length(Set(s)) ? "yes" : "no")


parseInt(x) = parse(Int, x)

function main()
	n = parseInt(readline())
	s = Array{Int}(n)
	for i in 1:n
		s[i] = parseInt(readline())
	end
	s = sort(s)
	k = sum(s)
	for i in 1:n
		if k%10 != 0
			break
		else
			k -= s[i]
		end
	end
	println(k)
end

main()

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


function check(A,B,h,k)
	cnt=0
	for a=h
		cnt+=cld(max(a-B*k,0),A-B)
	end
	cnt<=k
end
function main()
	N,A,B=map(x->parse(Int,x),split(readline()))
	h=map(x->parse(Int,x),readlines())
	L=0
	R=10^9
	while R-L>1
		M=div(L+R,2)
		if check(A,B,h,M)
			R=M
		else
			L=M
		end
	end
	println(R)
end
main()

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.

At least how many explosions do you need to cause in order to vanish all the monsters?

Constraints

All input values are integers.

1 ≤ N ≤ 10^5

1 ≤ B < A ≤ 10^9

1 ≤ h_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N A B
h_1
h_2
:
h_N

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

You can vanish all the monsters in two explosion, as follows:

First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.

Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.

Sample Input 2

2 10 4
20
20

Sample Output 2

4

You need to cause two explosions centered at each monster, for a total of four.

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000


Score : 100 points

Problem Statement

You are given three integers A, B and C.
Determine whether C is not less than A and not greater than B.

Constraints

-100≤A,B,C≤100

A, B and C are all integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

If the condition is satisfied, print Yes; otherwise, print No.

Sample Input 1

1 3 2

Sample Output 1

Yes

C=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes.

Sample Input 2

6 5 4

Sample Output 2

No

C=4 is less than A=6, and thus the output should be No.

Sample Input 3

2 2 2

Sample Output 3

Yes


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	a,b,c = readline() |> split |> parseMap
	if a==b==c
		if a%2==0
			println(-1)
		else
			println(0)
		end
	else
		k = 0
		while a%2==0&&b%2==0&&c%2==0
			d = div(a,2)
			e = div(b,2)
			f = div(c,2)
			a = e+f
			b = d+f
			c = d+e
			k += 1
		end
		println(k)
	end
end
main()

Score : 300 points

Problem Statement

Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:

Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.

This action will be repeated until there is a person with odd number of cookies in hand.

How many times will they repeat this action?
Note that the answer may not be finite.

Constraints

1 ≤ A,B,C ≤ 10^9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times the action will be performed by the three people, if this number is finite.
If it is infinite, print -1 instead.

Sample Input 1

4 12 20

Sample Output 1

3

Initially, Takahashi, Aoki and Snuke have 4, 12 and 20 cookies. Then,

After the first action, they have 16, 12 and 8.

After the second action, they have 10, 12 and 14.

After the third action, they have 13, 12 and 11.

Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is 3.

Sample Input 2

14 14 14

Sample Output 2

-1

Sample Input 3

454 414 444

Sample Output 3

1


(A, B, C) = split(readline(STDIN))
if A[length(A)] == B[1] && B[length(B)] == C[1]
    println("YES")
else
    println("NO")
end

Score : 100 points

Problem Statement

You are given three strings A, B and C. Check whether they form a word chain.

More formally, determine whether both of the following are true:

The last character in A and the initial character in B are the same.

The last character in B and the initial character in C are the same.

If both are true, print YES. Otherwise, print NO.

Constraints

A, B and C are all composed of lowercase English letters (a - z).

1 ≤ |A|, |B|, |C| ≤ 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

rng gorilla apple

Sample Output 1

YES

They form a word chain.

Sample Input 2

yakiniku unagi sushi

Sample Output 2

NO

A and B form a word chain, but B and C do not.

Sample Input 3

a a a

Sample Output 3

YES

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab

Sample Output 4

NO


(A, B, C) = parse.([Int],  split(readline(STDIN)))

arr = []
tmp = A
while !in(tmp, arr)
    push!(arr, tmp)
    tmp = (tmp + A) % B
end
if in(C, arr)
    println("YES")
else
    println("NO")
end

Score : 200 points

Problem Statement

We ask you to select some number of positive integers, and calculate the sum of them.

It is allowed to select as many integers as you like, and as large integers as you wish.
You have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.

Your objective is to make the sum congruent to C modulo B.
Determine whether this is possible.

If the objective is achievable, print YES. Otherwise, print NO.

Constraints

1 ≤ A ≤ 100

1 ≤ B ≤ 100

0 ≤ C < B

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

7 5 1

Sample Output 1

YES

For example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5.

Sample Input 2

2 2 1

Sample Output 2

NO

The sum of even numbers, no matter how many, is never odd.

Sample Input 3

1 100 97

Sample Output 3

YES

You can select 97, since you may select multiples of 1, that is, all integers.

Sample Input 4

40 98 58

Sample Output 4

YES

Sample Input 5

77 42 36

Sample Output 5

NO


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n,w = readline() |> split |> parseMap
	a = Array{Int}(2,n)
	for i in 1:n
		a[:,i] = readline() |> split |> parseMap
	end
	x = a[1,1]
	for i in 1:n
		a[1,i] -= x
	end
	s = sum(a[1,:])
	dp = zeros(Int,n,n,s+1)
	if x <= w
		dp[1,1,1] = a[2,1]
	end
	for i in 2:n
		for k in 1:s+1
			if k == a[1,i]+1
				dp[i,1,k] = max(dp[i-1,1,k], a[2,i])
			else
				dp[i,1,k] = dp[i-1,1,k]
			end
		end
		for j in 2:i
			for k in 1:s+1
				if j*x+k<=w+1
					if k<=a[1,i]
						dp[i,j,k] = dp[i-1,j,k]
					else
						dp[i,j,k] = max(dp[i-1,j,k],dp[i-1,j-1,k-a[1,i]] + a[2,i])
					end
				end
			end
		end
	end

	println(maximum(dp[n,:,:]))
end
main()

Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

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 total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.


print(uppercase(join(s[1:1]for s in split(readline()))))

Score : 100 points

Problem Statement

You are given three words s_1, s_2 and s_3, each composed of lowercase English letters, with spaces in between.
Print the acronym formed from the uppercased initial letters of the words.

Constraints

s_1, s_2 and s_3 are composed of lowercase English letters.

1 ≤ |s_i| ≤ 10 (1≤i≤3)

Input

Input is given from Standard Input in the following format:

s_1 s_2 s_3

Output

Print the answer.

Sample Input 1

atcoder beginner contest

Sample Output 1

ABC

The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.

Sample Input 2

resident register number

Sample Output 2

RRN

Sample Input 3

k nearest neighbor

Sample Output 3

KNN

Sample Input 4

async layered coding

Sample Output 4

ALC


parseInt(x) = parse(Int, x)

function main()
	n = readline() |> parseInt
	a = map(parseInt, split(readline()))
	b = Array{Int}(n)
	k = 0
	if a[1] > 0
		b[1] = 1
		k += abs(a[1]+1)
	else
		b[1] = -1
		k += abs(a[1]-1)
	end
	for i in 2:n
		b[i] = a[i]+b[i-1]
		if b[i]*b[i-1] >= 0
			if b[i-1] < 0
				k += abs(b[i]-1)
       			b[i] = 1
			else
				k += abs(b[i]+1)
				b[i] = -1
			end
		end
	end
	c = Array{Int}(n)
	l = 0
	if a[1] > 0
		c[1] = -1
		l += abs(a[1]+1)
	else
		c[1] = 1
		l += abs(a[1]-1)
	end
	for i in 2:n
		c[i] = a[i]+c[i-1]
		if c[i]*c[i-1] >= 0
			if c[i-1] < 0
				l += abs(c[i]-1)
       			c[i] = 1
			else
				l += abs(c[i]+1)
				c[i] = -1
			end
		end
	end
	print(min(k,l))
end

main()

Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each 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 minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8


function f()
    N = parse(Int, readline())
    A = parse.(Int, split(readline()))
    ans = 1
    i = 3
    while i <= N
        if (A[i]-A[i-1])*(A[i-1]-A[i-2])<0
            ans += 1
            i += 2
        else
            i += 1
        end
    end

    println(Int(ans))
end
f()

Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each 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 minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


function main()
	N=parse(Int,readline())
	A=map(x->parse(Int,x),split(readline()))
	ans=1
	i=3
	while i<=N
		if A[i-2]<A[i-1]&&A[i-1]>A[i]||A[i-2]>A[i-1]&&A[i-1]<A[i]
			ans+=1
			i+=1
		end
		i+=1
	end
	println(ans)
end
main()


function main()
	a = chomp(readline())
	b = chomp(readline())
	for i in 1:length(a)+length(b)
		if i%2==0
			print(b[div(i,2)])
		else
			print(a[div(i,2)+1])
		end
	end
end

main()

Score : 200 points

Problem Statement

Snuke signed up for a new website which holds programming competitions.
He worried that he might forget his password, and he took notes of it.
Since directly recording his password would cause him trouble if stolen,
he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.

You are given two strings O and E. O contains the characters at the odd-numbered positions retaining their relative order, and E contains the characters at the even-numbered positions retaining their relative order.
Restore the original password.

Constraints

O and E consists of lowercase English letters (a - z).

1 \leq |O|,|E| \leq 50

|O| - |E| is either 0 or 1.

Input

Input is given from Standard Input in the following format:

O
E

Output

Print the original password.

Sample Input 1

xyz
abc

Sample Output 1

xaybzc

The original password is xaybzc. Extracting the characters at the odd-numbered positions results in xyz, and extracting the characters at the even-numbered positions results in abc.

Sample Input 2

atcoderbeginnercontest
atcoderregularcontest

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt


s=readline()
t=readline()
for i=1:length(s)
	print(s[i])
	if i<=length(t)
		print(t[i])
	end
end


function main()
  n,m=parse.(split(readline()))
  l=[(x->parse(Int,x)).(split(readline())) for i=1:n]
  sort!(l,by=x->x[1])
  have=0
  money=0
  for i in l
    if have+i[2]<m
      have+=i[2]
      money+=i[2]*i[1]
    elseif have==m
      break
    else
      money+=(m-have)*i[1]
      break
    end
  end
  print(money)
end
main()

Score : 300 points

Problem Statement

Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.

He will receive a headline which contains one of the strings S_1,...,S_n tomorrow.
He is excited and already thinking of what string he will create.
Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.

Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains.
If there are multiple such strings, find the lexicographically smallest one among them.

Constraints

1 \leq n \leq 50

1 \leq |S_i| \leq 50 for every i = 1, ..., n.

S_i consists of lowercase English letters (a - z) for every i = 1, ..., n.

Input

Input is given from Standard Input in the following format:

n
S_1
...
S_n

Output

Print the lexicographically smallest string among the longest strings that satisfy the condition.
If the answer is an empty string, print an empty line.

Sample Input 1

3
cbaa
daacc
acacac

Sample Output 1

aac

The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth.
Among them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.

Sample Input 2

3
a
aa
b

Sample Output 2

The answer is an empty string.


function main()

  (A,B) = map(x -> parse(Int,x), split(readline()))

  T = A+B

  if T >= 24
    println(T - 24)
  else
    println(T)
  end

end

main()

Score : 100 points

Problem Statement

Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.

In this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as "21 o'clock".

The current time is A o'clock, and a contest will begin in exactly B hours.
When will the contest begin? Answer in 24-hour time.

Constraints

0 \leq A,B \leq 23

A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print the hour of the starting time of the contest in 24-hour time.

Sample Input 1

9 12

Sample Output 1

21

In this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time.

Sample Input 2

19 0

Sample Output 2

19

The contest has just started.

Sample Input 3

23 2

Sample Output 3

1

The contest will begin at 1 o'clock the next day.


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function mkdivisorpair(n::Int)
  divisors=[]
  for i=1:div(n^0.5,1)
    if n%i==0
      push!(divisors,(Int(i),Int(n/i)))
    end
  end
  sort!(divisors)
  return divisors
end
function main()
  d=mkdivisorpair(pI(readline()))
  ans=100000
  for i in d
    ans=min(ans,max(ndigits(i[1]),ndigits(i[2])))
  end
  println(ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6


N = parse(readline())

function f(a,b)
  A = floor(log10(a)+1)
  B = floor(log10(b)+1)
  return max(A,B)
end

ans = 100
for i in 1:sqrt(N)
  if N % i == 0
  	(f(i,N/i) < ans) && (ans = f(i,N/i))
  end
end

println(Int(ans))


parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseFloat(x::SubString{String}) = parse(Float64, x)
parseFloat(x::Char) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n=parseInt(readline())
    ans=10^10
    for i in 1:parseInt(sqrt(n))+1
        if n%i==0
            ans=min(ans,max(length(string(i)),length(string(div(n,i)))))
        end
    end
    println(ans)
end
main()


function dig(n)
	k = 0
	while 10^k <= n
		k += 1
	end
	k
end

function main()
	n = parse(Int, readline())
	if n == 1
		println(1)
	else
		k = 2
		s = Int[]
		while k <= sqrt(n)
			if n % k == 0
				push!(s,k)
			end
			k += 1
		end
		if length(s) == 0
			print(dig(n))
		else
			maxman = s[length(s)]
			print(max(dig(maxman),dig(div(n,maxman))))
		end
	end
end

main()


parseInt(x)=parse(Int32,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function expand(dp::Set{Int32},x,k,th)
	f = 0
	tmp = Int32[]
	for j in dp
		v = x+j
		if v<k
			push!(tmp,v)
			if k-th<=v
				f = 1
				break
			end
		end
	end
	if f == 1
		f
	else
		for i in tmp
			push!(dp,i)
		end
		dp
	end
end

function eval(a::Array{Int32,1},m,n,k)
	dp = Set(Int32[0])
	f = 0
	for i in 1:n
		if i!=m
			dp = expand(dp,a[i],k,a[m])
			if dp == 1
				f = 1
				break
			end
		end
		if f == 1
			break
		end
	end
	f
end

function main()
	n,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	a = sort(a)
	l,r = 0,n+1
	while r-l>1
		m = (l+r)>>1
		f = eval(a,m,n,k)
		if a[m]>=k
			f = 1
		end
		if f == 1
			r = m
		else
			l = m
		end
	end
	println(l)
end

main()

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


function main()
  N=parse(Int,readline())
  A=sort(map(x->parse(Int,x),split(readline())))
  p=t=0
  for a=A
    if p*3<p+a
      t=1
    else
      t+=1
    end
    p+=a
  end
  println(t)
end
main()

Score : 400 points

Problem Statement

Snuke found N strange creatures.
Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself.
When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B.
Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature.
Find the number of the possible colors of this creature.

Constraints

2 \leq N \leq 100000

1 \leq A_i \leq 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.

Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3.
For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.

Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	k = 0
	for i in 2:n
		if 2*a[i-1] < a[i]
			k = i-1
		end
		a[i] += a[i-1]
	end
	println(n-k)
end
main()


function main()

  N = parse(BigInt,readline())

  ans = factorial(N)

  println(ans % (10^9+7))

end

main()

Score : 200 points

Problem Statement

Snuke loves working out. He is now exercising N times.

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

Constraints

1 ≤ N ≤ 10^{5}

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer modulo 10^{9}+7.

Sample Input 1

3

Sample Output 1

6

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.

After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.

After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

Print the answer modulo 10^{9}+7.


div_num = 10^9+7
ans_list = [1]

function factorial(integer)
    for i in 2:integer
        append!(ans_list,mod(i*ans_list[end],div_num))
    end
    return ans_list[end]
end

N = parse(Int, readline())
println(factorial(N))


parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseFloat(x::SubString{String}) = parse(Float64, x)
parseFloat(x::Char) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n,m=parseMap(split(readline()))
    println(div(n+div(m,2),2))
end
main()

Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897


parseInt(x) = parse(Int, x)

function main()
	n,m = map(parseInt, split(readline()))
	m = div(m,2)
	if n >m
		println(m)
	else
		println(div(n+m,2))
	end
end

main()


function scc(N,M)
    if 2N >= M
        return div(M,2)
    else
        return N + div(M-2N,4)
    end
end
NM = split(readline())
N = parse(Int,NM[1])
M = parse(Int,NM[2])
println(scc(N,M))


function main()

  (A,B) = map(x -> parse(Int,x), split(readline()))

  if A == 1
    A = 14
  end
  if B == 1
    B = 14
  end

  if A == B
    println("Draw")
  elseif A > B
    println("Alice")
  else
    println("Bob")
  end

end

main()

Score : 100 points

Problem Statement

Alice and Bob are playing One Card Poker.

One Card Poker is a two-player game using playing cards.

Each card in this game shows an integer between 1 and 13, inclusive.

The strength of a card is determined by the number written on it, as follows:

Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong

One Card Poker is played as follows:

Each player picks one card from the deck. The chosen card becomes the player's hand.

The players reveal their hands to each other. The player with the stronger card wins the game.

If their cards are equally strong, the game is drawn.

You are watching Alice and Bob playing the game, and can see their hands.

The number written on Alice's card is A, and the number written on Bob's card is B.

Write a program to determine the outcome of the game.

Constraints

1≦A≦13

1≦B≦13

A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.

Sample Input 1

8 6

Sample Output 1

Alice

8 is written on Alice's card, and 6 is written on Bob's card.
Alice has the stronger card, and thus the output should be Alice.

Sample Input 2

1 1

Sample Output 2

Draw

Since their cards have the same number, the game will be drawn.

Sample Input 3

13 1

Sample Output 3

Bob


function solve()
    N, M = parse.(Int, split(readline()))
    A = zeros(Int, N, N)
    B = zeros(Int, M, M)

    if N == M println("Yes") end

    for i = 1:N
        s = readline()
        for j = 1:N
            if s[j] == '#' A[i, j] = 1 end
        end
    end

    for i = 1:M
        s = readline()
        for j = 1:M
            if s[j] == '#' B[i, j] = 1 end
        end
    end

    res = false

    for i = 1:(N-M) for j = 1:(N-M)
        match = true
        for k = 1:M for l = 1:M
            if A[i+k, j+l] != B[k, l] match = false
            end
        end end
        if match res = true end
    end end

    if res println("Yes")
    else println("No")
    end
end

solve()

Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


function solve()
    N, M = parse.(Int, split(readline()))
    A = zeros(Int, N, N)
    B = zeros(Int, M, M)

    for i = 1:N
        s = readline()
        for j = 1:N
            if s[j] == '#' A[i, j] = 1 end
        end
    end

    for i = 1:M
        s = readline()
        for j = 1:M
            if s[j] == '#' B[i, j] = 1 end
        end
    end

    res = false

    for i = 1:(N-M) for j = 1:(N-M)
        match = true
        for k = 1:M for l = 1:M
            if A[i+k, j+l] != B[k, l] match = false
            end
        end end
        if match res = true end
    end end

    if res println("Yes")
    else println("No")
    end
end

solve()


parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseFloat(x::SubString{String}) = parse(Float64, x)
parseFloat(x::Char) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n,m=parseMap(split(readline()))
    a=[chomp(readline()) for i in 1:n]
    b=[chomp(readline()) for i in 1:m]
    ans="No"
    for i1 in 0:n-m,i2 in 0:n-m
        res=true
        for j1 in 1:m,j2 in 1:m
            if a[i1+j1][i2+j2]!=b[j1][j2]
                res=false
                break
            end
        end
        if res
            ans="Yes"
            break
        end
    end
    println(ans)
end
main()


function calc(dp,a,b,c)
	next=ones(Int,401,401)*1145141919810
	for i=1:401,j=1:401
		next[i,j]=min(next[i,j],dp[i,j])
		if i+a<=401&&j+b<=401
			next[i+a,j+b]=min(next[i+a,j+b],dp[i,j]+c)
		end
	end
	next
end
function main()
	N,Ma,Mb=map(x->parse(Int,x),split(readline()))
	dp=ones(Int,401,401)*1145141919810
	dp[1,1]=0
	for i=1:N
		a,b,c=map(x->parse(Int,x),split(readline()))
		dp=calc(dp,a,b,c)
	end
	ans=1145141919810
	for i=1:401,j=1:401
		if i==j==1
			continue
		end
		if (i-1)Mb==(j-1)Ma
			ans=min(ans,dp[i,j])
		end
	end
	println(ans<1145141919810 ? ans : -1)
end
main()

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	k = 0
	for i in 1:n
		if a[i]%2==1
			k += 1
		end
	end
	println(k%2==0?"YES":"NO")
end
main()

Score : 300 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi will repeatedly perform the following operation on these numbers:

Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.

Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.

Determine whether it is possible to have only one integer on the blackboard.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

Sample Input 1

3
1 2 3

Sample Output 1

YES

It is possible to have only one integer on the blackboard, as follows:

Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.

Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


function main()
	N=parse(Int,readline())
	A=map(x->parse(Int,x),split(readline()))
	S=sum(A)
	B=zeros(Int,N)
	for i=1:N
		B[i]=A[mod1(i+1,N)]-A[i]
	end
	all=div((N+1)*N,2)
	if sum%all!=0
		println("NO")
		exit()
	end
	K=div(sum,all)
	for i=1:N
		B[i]-=K
		if B[i]>0||B[i]%N!=0
			println("NO")
			exit()
		end
	end
	println("YES")
end
main()

Score : 500 points

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

All the stones can be removed in one operation by selecting the second box.

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO


N=parse(readline())
ans=2div(N,11)
N%=11
println(ans+(N>6?2:N>0))

Score : 300 points

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.
If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.
Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	t = zeros(Int,n)
	e = [Int[] for i in 1:n]
	for i in 2:n
		a = readline() |> parseInt
		push!(e[a],i)
		t[i] = a
	end
	layer = zeros(Int,n)
	stack = Tuple{Int,Int}[]
	push!(stack, (1,0))
	while !isempty(stack)
		(i,k) = pop!(stack)
		layer[i] = k
		for j in e[i]
			push!(stack, (j,k+1))
		end
	end
	d = Int[length(e[i]) for i in 1:n]
	l = zeros(Int,n)
	stack = Tuple{Int,Int}[(layer[i],i) for i in 1:n]
	stack = sort(stack, by=x->x[1],rev=true)
	g = [Int[] for i in 1:n]
	for iter in 1:n
		i = stack[iter][2]
		gl = length(g[i])
		if gl>0
			g[i] = sort(g[i])
			minim = 0
			for j in 1:gl
				minim = max(minim,g[i][j]+gl-j)
			end
			l[i] = minim
			if t[i]>0
				push!(g[t[i]],minim+1)
			end
		else
			l[i] = 1
			if t[i]>0
				push!(g[t[i]],1)
			end
		end
	end
	println(l[1])
end

main()

Score : 800 points

Problem Statement

N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament.
For some reasons, the tournament may not be "fair" for all the contestants.
That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.

After each match, there were always one winner and one loser. The last contestant standing was declared the champion.

Figure: an example of a tournament

For convenience, the contestants were numbered 1 through N. The contestant numbered 1 was the champion, and the contestant numbered i(2 ≦ i ≦ N) was defeated in a match against the contestant numbered a_i.

We will define the depth of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.

Find the minimum possible depth of the tournament.

The formal description of the structure of the tournament is as follows. In the i-th match, one of the following played against each other:

Two predetermined contestants

One predetermined contestant and the winner of the j-th match, where j(j<i) was predetermined

The winner of the j-th match and the winner of the k-th match, where j and k (j,k<i, j ≠ k) were predetermined

Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i ≦ N(2 ≦ i ≦ N)

It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).

Input

The input is given from Standard Input in the following format:

N
a_2
:
a_N

Output

Print the minimum possible depth of the tournament.

Sample Input 1

5
1
1
2
4

Sample Output 1

3

The following tournament and the result of the matches are consistent with the given information:

In the first match, contestants 4 and 5 played against each other, and contestant 4 won.

In the second match, contestants 2 and 4 played against each other, and contestant 2 won.

In the third match, contestants 1 and 3 played against each other, and contestant 1 won.

In the fourth match, contestants 1 and 2 played against each other, and contestant 1 won.

The depth of this tournament is 3, since contestant 5 must play three matches in order to win the championship.
There is no tournament with depth 2 or less that is consistent with the given information, thus the output should be 3.

Sample Input 2

7
1
2
1
3
1
4

Sample Output 2

3

Sample Input 3

4
4
4
1

Sample Output 3

3


Score : 100 points

Problem Statement

There are two rectangles.
The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.
The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

The input is given from Standard Input in the following format:

A B C D

Output

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7

Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.
Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300

Sample Output 2

60000


function main()
	n = parse(Int, readline())
	s = chomp(readline())
	a = Array{Int}(n+1)
    a[1] = 0
	for i in 1:n
		if s[i] == 'I'
			a[i+1] = a[i] + 1
		else
			a[i+1] = a[i] - 1
		end
	end
  	println(a)
	println(maximum(a))
end

main()

Score : 200 points

Problem Statement

You have an integer variable x.
Initially, x=0.

Some person gave you a string S of length N, and using the string you performed the following operation N times.
In the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

Sample Input 2

7
DDIDDII

Sample Output 2

0

The initial value x=0 is the maximum value taken by x, thus the output should be 0.


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n,a,b=parseMap(split(readline()))
    x=parseMap(split(readline()))
    ans=0
    for i in 2:n
        ans+=min((x[i]-x[i-1])*a,b)
    end
    println(ans)
end
main()

Score : 500 points

Problem Statement

There are N towns on a line running east-west.
The towns are numbered 1 through N, in order from west to east.
Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value.
The coordinate of town i is X_i.

You are now at town 1, and you want to visit all the other towns.
You have two ways to travel:

Walk on the line.
Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.

Teleport to any location of your choice.
Your fatigue level increases by B, regardless of the distance covered.

Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.

Constraints

All input values are integers.

2≤N≤10^5

1≤X_i≤10^9

For all i(1≤i≤N-1), X_i<X_{i+1}.

1≤A≤10^9

1≤B≤10^9

Input

The input is given from Standard Input in the following format:

N A B
X_1 X_2 ... X_N

Output

Print the minimum possible total increase of your fatigue level when you visit all the towns.

Sample Input 1

4 2 5
1 2 5 7

Sample Output 1

11

From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4.
The total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.

Sample Input 2

7 1 100
40 43 45 105 108 115 124

Sample Output 2

84

From town 1, walk all the way to town 7.
The total increase of your fatigue level in this case is 84, which is the minimum possible value.

Sample Input 3

7 1 2
24 35 40 68 72 99 103

Sample Output 3

12

Visit all the towns in any order by teleporting six times.
The total increase of your fatigue level in this case is 12, which is the minimum possible value.


Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.

The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].

Dolphin wants to convert the comma-separated string s into a space-separated string.

Write a program to perform the conversion for him.

Constraints

The length of s is 19.

The sixth and fourteenth characters in s are ,.

The other characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

s

Output

Print the string after the conversion.

Sample Input 1

happy,newyear,enjoy

Sample Output 1

happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

Sample Input 2

haiku,atcoder,tasks

Sample Output 2

haiku atcoder tasks

Sample Input 3

abcde,fghihgf,edcba

Sample Output 3

abcde fghihgf edcba


function main()

  s = replace(readline(), ",", " ")

  println(s)

end

main()


# ドグラ・マグラ / 夢野久作
parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
何故踊る(x::Array{SubString{String},1}) = map(parseInt, x)
母親の心がわかって(x) = split(x)
おそろしいのか() = readline()

function ドグラ_マグラ(arr)
    sx,sy,tx,ty=arr
    ans=""
    if ty>sy
        ans*="U"^(ty-sy)
    else
        ans*="D"^(sy-ty)
    end
    if tx>sx
        ans*="R"^(tx-sx)
    else
        ans*="L"^(sx-tx)
    end
    if ty>sy
        ans*="D"^(ty-sy)
    else
        ans*="U"^(sy-ty)
    end
    if tx>sx
        ans*="L"^(tx-sx+1)
    else
        ans*="R"^(sx-tx+1)
    end
    if ty>sy
        ans*="U"^(ty-sy+1)
    else
        ans*="D"^(sy-ty+1)
    end
    if tx>sx
        ans*="R"^(tx-sx+1)
    else
        ans*="L"^(sx-tx+1)
    end
    if ty>sy
        ans*="D"
    else
        ans*="U"
    end
    if tx>sx
        ans*="R"
    else
        ans*="L"
    end
    if ty>sy
        ans*="D"^(ty-sy+1)
    else
        ans*="U"^(sy-ty+1)
    end
    if tx>sx
        ans*="L"^(tx-sx+1)
    else
        ans*="R"^(sx-tx+1)
    end
    if ty>sy
        ans*="U"
    else
        ans*="D"
    end
    println(ans)
end

function book()
    胎児よ胎児よ = 何故踊る(母親の心がわかって(おそろしいのか()))
    ドグラ_マグラ(胎児よ胎児よ)
end
book()

Score : 300 points

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.

Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.

Here, both the x- and y-coordinates before and after each movement must be integers.

He will first visit the point (tx,ty) where sx < tx and sy < ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).

Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).

Under this condition, find a shortest path for him.

Constraints

-1000 ≤ sx < tx ≤ 1000

-1000 ≤ sy < ty ≤ 1000

sx,sy,tx and ty are integers.

Input

The input is given from Standard Input in the following format:

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.

The i-th character in S should correspond to his i-th movement.

The directions of the movements should be indicated by the following characters:

U: Up

D: Down

L: Left

R: Right

If there exist multiple shortest paths under the condition, print any of them.

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)

Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)

Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (-1,0) → (-1,1) → (-1,2) → (-1,3) → (0,3) → (1,3) → (1,2)

Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,-1) → (1,-1) → (0,-1) → (0,0)

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL


parseInt(x) = parse(Int,x)
function main()
  sx,sy,tx,ty = map(parseInt, split(readline()))
  dx = tx-sx
  dy = ty-sy
  for i in 1:dx
    print("R")
  end
  for i in 1:dy
    print("U")
  end
  for i in 1:dx
    print("L")
  end
  for i in 1:dy+1
    print("D")
  end
  for i in 1:dx+1
	print("R")
  end
  for i in 1:dy+1
    print("U")
  end
  print("LU")
  for i in 1:dx+1
    print("L")
  end
  for i in 1:dy+1
    print("D")
  end
  print("R")
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,m = readline() |> split |> parseMap
	g = zeros(Int,n,n)
	d = zeros(Int,n,n)
	x = 0
	for i in 1:m
		a,b,c = readline() |> split |> parseMap
		g[a,b] = c
		g[b,a] = c
		d[a,b] = c
		d[b,a] = c
	end
	for k in 1:n
		for i in 1:n
			for j in 1:n
				d[i,j] = min(d[i,j],d[i,k]+d[k,j])
			end
		end
	end
	for i in 1:n-1
		for j in i+1:n
			f = 0
			for s in 1:n
				if d[s,i] + g[i,j] == d[s,j]
					f = 1
					break
				end
			end
			if f == 0
				x += 1
			end
		end
	end
	println(x)
end

main()

Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

The given graph contains neither self-loops nor double edges.

The given graph is connected.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,m = readline() |> split |> parseMap
	d = zeros(Int,n,n)
	f = zeros(Int,n,n)
	for i in 1:m
		a,b,c = readline() |> split |> parseMap
		d[a,b] = c
		d[b,a] = c
		f[a,b] = 1
		f[b,a] = 1
	end
	for k in 1:n
		for i in 1:n
			for j in 1:n
				if d[i,j] > d[i,k] + d[k,j]
					d[i,j] = d[i,k] + d[k,j]
					f[i,j] = 0
					f[j,i] = 0
				end
			end
		end
	end
	println((2m-sum(f))>>1)
end

main()


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    x,y=parseMap(split(readline()))
    ans=10^10
    if y>=x
        ans=min(ans,y-x)
    end
    if y>=-x
        ans=min(ans,y+x+1)
    end
    if -y>=-x
        ans=min(ans,-y-x+2)
    end
    if -y>=x
        ans=min(ans,-y+x+1)
    end
    println(ans)
end
main()

Score : 300 points

Problem Statement

Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer x.
Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

Button A: When pressed, the value on the display is incremented by 1.

Button B: When pressed, the sign of the value on the display is reversed.

Find the minimum number of times Snuke needs to press the buttons to achieve his objective.
It can be shown that the objective is always achievable regardless of the values of the integers x and y.

Constraints

x and y are integers.

|x|, |y| ≤ 10^9

x and y are different.

Input

The input is given from Standard Input in the following format:

x y

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

Sample Input 1

10 20

Sample Output 1

10

Press button A ten times.

Sample Input 2

10 -10

Sample Output 2

1

Press button B once.

Sample Input 3

-10 -20

Sample Output 3

12

Press the buttons as follows:

Press button B once.

Press button A ten times.

Press button B once.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	x,y = readline() |> split |> parseMap
	if x==y
		println(0)
	elseif x<=0<=y
		println(min(y-x,abs(y+x+1)))
	elseif y<=0<=x
		println(min(x-y,abs(y+x+1)))
	elseif x<y
		println(y-x)
	elseif x<0
		println(min(-y-x+1,-y+x+2))
	else
		println(min(y+x+1,x-y+2))
	end
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	x,y = readline() |> split |> parseMap
	if x==y
		println(0)
	elseif x<0<y
		println(min(y-x,abs(y+x+1))
	elseif y<0<x
		println(min(x-y,abs(y+x+1))
	elseif x<y
		println(y-x)
	elseif x<y<=0
		println(min(-y-x+1,-y+x+2))
	else
		println(min(y+x+1,x-y+2))
	end
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	x,y = readline() |> split |> parseMap
	if x==y
		println(0)
	elseif x>=0&&y>=0
		if x>y
			println(min(1+(y+x),x-y+2))
		else
			println(y-x)
		end
	elseif x<=0&&y<=0
		if x>y
			println(min(1+(-y-x),-x+y+2))
		else
			println(-x-y)
		end
	else
		println(abs(y+x)+1)
	end
end

main()


form = split(readline())
a = parse(Int, form[1])
b = parse(Int, form[3])
println(form[2]in['+',"+"] ? a+b : a-b)

Score : 100 points

Problem Statement

Joisino wants to evaluate the formula "A op B".
Here, A and B are integers, and the binary operator op is either + or -.
Your task is to evaluate the formula instead of her.

Constraints

1≦A,B≦10^9

op is either + or -.

Input

The input is given from Standard Input in the following format:

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

Since 1 + 2 = 3, the output should be 3.

Sample Input 2

5 - 7

Sample Output 2

-2


function main(input)
    mod = 10^9 + 7
    arr = [parse.(Int, i) for i in split.(input)]
    N = arr[1][1]
    A = arr[2]
    A_true = N%2==0 ? [2div(n-1,2)+1 for n in 1:N] : [n==1 ? 0 : 2div(n, 2) for n in 1:N]
    ans = A_true==sort(A) ?  2^div(N,2)%mod : 0
    return println(ans)
end
main(readlines())

Score : 300 points

Problem Statement

There are N people, conveniently numbered 1 through N.
They were standing in a row yesterday, but now they are unsure of the order in which they were standing.
However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.
According to their reports, the difference above for person i is A_i.

Based on these reports, find the number of the possible orders in which they were standing.
Since it can be extremely large, print the answer modulo 10^9+7.
Note that the reports may be incorrect and thus there may be no consistent order.
In such a case, print 0.

Constraints

1≦N≦10^5

0≦A_i≦N-1

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.

Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

2,1,4,5,3

2,5,4,1,3

3,1,4,5,2

3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.

Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16


N=parse(Int,readline())
cnt=zeros(Int,N)
for a=map(x->parse(Int,x),split(readline()))
	cnt[a+1]+=1
end
flag=true
for i=N:-2:2
	flag&=cnt[i]==2
end
println(flag ? powermod(2,div(N,2),10^9+7) : 0)


function main()
    divide = map(reverse,["dream", "dreamer", "erase", "eraser"])

    S = reverse(readline())

    ans = true
    while length(S) > 0
        is_contains = false
        for i = 1:4
            if length(divide[i]) <= length(S)
                if S[end - length(divide[i]) + 1:end] == divide[i]
                    is_contains = true
                    S = S[1:end - length(divide[i])]
                end
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()

Score : 100 points

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

Constraints

c is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

c

Output

If c is a vowel, print vowel. Otherwise, print consonant.

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


c = readline(STDIN)[1]
if search("aoeui", c) == 0
    println("consonant")
else
    println("vowel")
end


function mtx(ps)
    H, W = parse.(Int, split(ps[1]))
    org = fill("*", H, W)
    expd = fill("*", 2H, W)
    for i in 1:H
        for j in 1:W
            org[i, j] = ps[1+i][j:j]
        end
    end
    for i in 1:2H
        expd[i, :] = org[Int(floor((i+1)/2)), :]
    end
    return expd
end

function ret(arr)
    H, W = size(arr)
    str_arr = ["" for i in 1:H]
    for i in 1:H
        str_arr[i] = prod(arr[i, :])*"\n"
    end
    return println(prod(str_arr))
end

m = readlines()

ret(mtx(m))


function main()

  (H,W) = map(x -> parse(Int,x), split(readline()))

  for i in 1:H
    s = chomp(readline())
    println(s)
    println(s)
  end

end

main()


lines=readlines()
function find(u,p)
	if u==p[u]
		u
	else
		p[u]=find(p[u],p)
	end
end
function f(N,S)
	parent=collect(1:N)
	size=ones(Int,N)
	for s=S
		a,b=map(x->find(parse(Int,x),parent),split(s))
		if a!=b
			if size[a]>size[b]
				parent[b]=a
				size[a]+=size[b]
			else
				parent[a]=b
				size[b]+=size[a]
			end
		end
	end
	parent
end
function main()
	N,K,L=map(x->parse(Int,x),split(lines[1]))
	E=Pair{Int,Int}[]
	for (a,b)=zip(f(N,lines[2:K+1]),f(N,lines[K+2:K+L+1]))
		push!(E,a=>b)
	end
	D=Dict{Pair{Int,Int},Int}()
	for v=E
		D[v]=get(D,v,0)+1
	end
	for v=E
		println(D[v])
	end
end
main()

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

The input is given from Standard Input in the following format:

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


function main()
    a,b,c = readline() |> split
    println("A"*b[1]*"C")

end

main()

Score : 100 points

Problem Statement

Snuke is going to open a contest named "AtCoder s Contest".
Here, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters.

Snuke has decided to abbreviate the name of the contest as "AxC".
Here, x is the uppercase English letter at the beginning of s.

Given the name of the contest, print the abbreviation of the name.

Constraints

The length of s is between 1 and 100, inclusive.

The first character in s is an uppercase English letter.

The second and subsequent characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

AtCoder s Contest

Output

Print the abbreviation of the name of the contest.

Sample Input 1

AtCoder Beginner Contest

Sample Output 1

ABC

The contest in which you are participating now.

Sample Input 2

AtCoder Snuke Contest

Sample Output 2

ASC

This contest does not actually exist.

Sample Input 3

AtCoder X Contest

Sample Output 3

AXC


function main()

  (a,b,x) = map(x -> parse(Int,x), split(readline()))

  ans = div(b,x) - div(a-1,x)

  if a == 0 && x != 1
    ans += 1
  end

  println(ans)

end

main()

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

The input is given from Standard Input in the following format:

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function hpush(h::Array{Tuple{Int,Float64},1},x::Tuple{Int,Float64})
	push!(h,x)
	if length(h)>1
		l = length(h)
		k = l>>1
		while x[2]<h[k][2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k>>1
			l>>1
		end
	end
end

function hpop(h::Array{Tuple{Int,Float64},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if h[2*k][2]<=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if z[2]<=h[2*k][2]&&z[2]<=h[2*k+1][2]
					break
				else
					t = h[2*k][2]<=h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

dist(xa::Int,ya::Int,xb::Int,yb::Int,ra::Int,rb::Int)=sqrt((xa-xb)^2+(ya-yb)^2)-ra-rb

function main()
	xs,ys,xt,yt = readline() |> split |> parseMap
	n = readline() |> parseInt
	a = Tuple{Int,Int,Int}[(0,0,0) for i in 1:n]
	for i in 1:n
		x,y,r = readline() |> split |> parseMap
		a[i] = (x,y,r)
	end
	e = [Tuple{Int,Float64}[] for i in 1:n+2]
	for i in 1:n
		for j in i+1:n
			v = dist(a[i][1],a[i][2],a[j][1],a[j][2],a[i][3],a[j][3])
			push!(e[i], (j,max(0,v)))
			push!(e[j], (i,max(0,v)))
		end
		v = dist(a[i][1],a[i][2],xs,ys,a[i][3],0)
		push!(e[i],(n+1,max(0,v)))
		push!(e[n+1],(i,max(0,v)))
		v = dist(a[i][1],a[i][2],xt,yt,a[i][3],0)
		push!(e[i],(n+2,max(0,v)))
		push!(e[n+2],(i,max(0,v)))
	end
	v = dist(xs,ys,xt,yt,0,0)
	push!(e[n+1],(n+2,max(0,v)))
	push!(e[n+2],(n+1,max(0,v)))
	h = Tuple{Int,Float64}[(n+1,0.0)]
	dis = 10^18*ones(Float64,n+2)
	while !isempty(h)
		v,w = hpop(h)
		dis[v] = min(dis[v],w)
		for i in 1:length(e[v])
			nw = w+e[v][i][2]
			if dis[e[v][i][1]]>nw
				dis[e[v][i][1]] = nw
				hpush(h,(e[v][i][1],nw))
			end
		end
	end
	println(dis[n+2])
end

main()

Score : 600 points

Problem Statement

On the xy-plane, Snuke is going to travel from the point (x_s, y_s) to the point (x_t, y_t).
He can move in arbitrary directions with speed 1.
Here, we will consider him as a point without size.

There are N circular barriers deployed on the plane.
The center and the radius of the i-th barrier are (x_i, y_i) and r_i, respectively.
The barriers may overlap or contain each other.

A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.

Snuke wants to avoid exposure to cosmic rays as much as possible during the travel.
Find the minimum possible duration of time he is exposed to cosmic rays during the travel.

Constraints

All input values are integers.

-10^9 ≤ x_s, y_s, x_t, y_t ≤ 10^9

(x_s, y_s) ≠ (x_t, y_t)

1≤N≤1,000

-10^9 ≤ x_i, y_i ≤ 10^9

1 ≤ r_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

x_s y_s x_t y_t
N
x_1 y_1 r_1
x_2 y_2 r_2
:
x_N y_N r_N

Output

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel.
The output is considered correct if the absolute or relative error is at most 10^{-9}.

Sample Input 1

-2 -2 2 2
1
0 0 1

Sample Output 1

3.6568542495

An optimal route is as follows:

Sample Input 2

-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

0.0000000000

An optimal route is as follows:

Sample Input 3

4 -2 -2 4
3
0 0 2
4 0 1
0 4 1

Sample Output 3

4.0000000000

An optimal route is as follows:


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function hpush(h::Array{Tuple{Int,Float64},1},x::Tuple{Int,Float64})
	push!(h,x)
	if length(h)>1
		l = length(h)
		k = l>>1
		while x[2]<h[k][2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k>>1
			l>>1
		end
	end
end

function hpop(h::Array{Tuple{Int,Float64},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if h[2*k][2]<=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if z[2]<=h[2*k][2]&&z[2]<=h[2*k+1][2]
					break
				else
					t = h[2*k][2]<=h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

dist(xa::Int,ya::Int,xb::Int,yb::Int,ra::Int,rb::Int)=sqrt((xa-xb)^2+(ya-yb)^2)-ra-rb

function main()
	xs,ys,xt,yt = readline() |> split |> parseMap
	n = readline() |> parseInt
	a = Tuple{Int,Int,Int}[(0,0,0) for i in 1:n]
	for i in 1:n
		x,y,r = readline() |> split |> parseMap
		a[i] = (x,y,r)
	end
	e = [Tuple{Int,Float64}[] for i in 1:n+2]
	for i in 1:n
		for j in i+1:n
			v = dist(a[i][1],a[i][2],a[j][1],a[j][2],a[i][3],a[j][3])
			push!(e[i], (j,max(0,v)))
			push!(e[j], (i,max(0,v)))
		end
		v = dist(a[i][1],a[i][2],xs,ys,a[i][3],0)
		push!(e[i],(n+1,max(0,v)))
		push!(e[n+1],(i,max(0,v)))
		v = dist(a[i][1],a[i][2],xt,yt,a[i][3],0)
		push!(e[i],(n+2,max(0,v)))
		push!(e[n+2],(i,max(0,v)))
	end
	v = dist(xs,ys,xt,yt,0,0)
	push!(e[n+1],(n+2,max(0,v)))
	push!(e[n+2],(n+1,max(0,v)))

	hs = Tuple{Int,Float64}[(n+1,0.0)]
	hg = Tuple{Int,Float64}[(n+2,0.0)]
	ds = -ones(Float64,n+2)
	dg = -ones(Float64,n+2)
	vst = zeros(Int,n+2)
	ds[n+1] = 0.0
	dg[n+2] = 0.0
	meet = 0
	while !isempty(hs)||!isempty(hg)
		v,w = hpop(hs)
		ds[v] = ds[v]<0.0?w:min(ds[v],w)
		vst[v] += 1
		if vst[v] > 1
			meet = v
			break
		end
		for i in 1:length(e[v])
			if ds[e[v][i][1]]<0.0 || ds[e[v][i][1]]>w+e[v][i][2]
				ds[e[v][i][1]] = w+e[v][i][2]
				hpush(hs,(e[v][i][1],w+e[v][i][2]))
			end
		end
		v,w = hpop(hg)
		dg[v] = dg[v]<0.0?w:min(dg[v],w)
		vst[v] += 1
		if vst[v] > 1
			meet = v
			break
		end
		for i in 1:length(e[v])
			if dg[e[v][i][1]]<0.0 || dg[e[v][i][1]]>w+e[v][i][2]
				dg[e[v][i][1]] = w+e[v][i][2]
				hpush(hg,(e[v][i][1],w+e[v][i][2]))
			end
		end
	end
	println(ds[meet]+dg[meet])
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function eval(a::Tuple{Int,Float64},b::Tuple{Int,Float64})
	if a[2]<b[2]
		1
	elseif a[2]==b[2]
		0
	else
		-1
	end
end

function hpush(h::Array{Tuple{Int,Float64},1},x::Tuple{Int,Float64})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = l>>1
		while eval(x,h[k])==1&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(k>>1,1)
			l = l>>1
		end
	end
end

function hpop(h::Array{Tuple{Int,Float64},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if eval(h[2*k],z)>=0
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if eval(z,h[2*k])>=0&&eval(z,h[2*k+1])>=0
					break
				else
					t = eval(h[2*k],h[2*k+1])>=0?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

dist(xa::Int,ya::Int,xb::Int,yb::Int,ra::Int,rb::Int)=sqrt((xa-xb)^2+(ya-yb)^2)-ra-rb

function main()
	xs,ys,xt,yt = readline() |> split |> parseMap
	n = readline() |> parseInt
	a = Tuple{Int,Int,Int}[(0,0,0) for i in 1:n]
	for i in 1:n
		x,y,r = readline() |> split |> parseMap
		a[i] = (x,y,r)
	end
	e = [Tuple{Int,Float64}[] for i in 1:n+2]
	for i in 1:n
		for j in i+1:n
			v = dist(a[i][1],a[i][2],a[j][1],a[j][2],a[i][3],a[j][3])
			push!(e[i], (j,max(0,v)))
			push!(e[j], (i,max(0,v)))
		end
		v = dist(a[i][1],a[i][2],xs,ys,a[i][3],0)
		push!(e[i],(n+1,max(0,v)))
		push!(e[n+1],(i,max(0,v)))
		v = dist(a[i][1],a[i][2],xt,yt,a[i][3],0)
		push!(e[i],(n+2,max(0,v)))
		push!(e[n+2],(i,max(0,v)))
	end
	v = (xs,ys,xt,yt,0,0)
	push!(e[n+1],(n+2,max(0,v)))
	push!(e[n+2],(n+1,max(0,v)))
	h = Tuple{Int,Float64}[(n+1,0.0)]
	dis = -ones(Float64,n+2)
	while !isempty(h)
		v,w = hpop(h)
		dis[v] = dis[v]<0.0?w:min(dis[v],w)
		for i in 1:length(e[v])
			if dis[e[v][i][1]]<0.0 || dis[e[v][i][1]]>w+e[v][i][2]
				dis[e[v][i][1]] = w+e[v][i][2]
				hpush(h,(e[v][i][1],w+e[v][i][2]))
			end
		end
	end
	println(dis[n+2])
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,m = readline() |> split |> parseMap
	x = readline() |> split |> parseMap
	z = maximum(x)
	np = zeros(Int,m)
	p = zeros(Int,m)
	ct = zeros(Int,z)
	for i in 1:n
		if ct[x[i]]==1
			np[x[i]%m+1]-=1
			p[x[i]%m+1]+=2
			ct[x[i]]=0
		else
			np[x[i]%m+1]+=1
			ct[x[i]]=1
		end
	end
	ans = 0
	ans += np[1]>>1+p[1]>>1
	for i in 2:(m+1)>>1
		j = m+2-i
		if np[i]>np[j]
			if np[i]>np[j]+p[j]
				ans += np[j]+p[j]
				ans += p[i]>>1
			elseif np[i]+p[i]>np[j]+p[j]
				ans += np[j]+p[j]
				ans += (p[i]-(np[j]+p[j]-np[i]))>>1
			else
				ans += np[i]+p[i]
				ans += (p[j]-(np[i]+p[i]-np[j]))>>1
			end
		else
			if np[j]>np[i]+p[i]
				ans += np[i]+p[i]
				ans += p[j]>>1
			elseif np[i]+p[i]>np[j]+p[j]
				ans += np[j]+p[j]
				ans += (p[i]-(np[j]+p[j]-np[i]))>>1
			else
				ans += np[i]+p[i]
				ans += (p[j]-(np[i]+p[i]-np[j]))>>1
			end
		end
	end
	if m%2==0
		ans += np[m>>1+1]>>1+p[m>>1+1]>>1
	end
	println(ans)
end

main()

Score : 700 points

Problem Statement

Takahashi is playing with N cards.

The i-th card has an integer X_i on it.

Takahashi is trying to create as many pairs of cards as possible satisfying one of the following conditions:

The integers on the two cards are the same.

The sum of the integers on the two cards is a multiple of M.

Find the maximum number of pairs that can be created.

Note that a card cannot be used in more than one pair.

Constraints

2≦N≦10^5

1≦M≦10^5

1≦X_i≦10^5

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_N

Output

Print the maximum number of pairs that can be created.

Sample Input 1

7 5
3 1 4 1 5 9 2

Sample Output 1

3

Three pairs (3,2), (1,4) and (1,9) can be created.

It is possible to create pairs (3,2) and (1,1), but the number of pairs is not maximized with this.

Sample Input 2

15 10
1 5 6 10 11 11 11 20 21 25 25 26 99 99 99

Sample Output 2

6


type UF
	n::Int
	pr::Array{Int}
	sz::Array{Int}
	UF(n)=new(n,1:n,ones(Int,n))
end
function find(UF,a)
	if UF.pr[a]==a
		a
	else
		UF.pr[a]=find(UF,UF.pr[a])
	end
end
same(UF,a,b)=find(UF,a)==find(UF,b)
function unite(UF,a,b)
	a=find(UF,a)
	b=find(UF,b)
	if a==b
		false
	else
		if UF.sz[a]<UF.sz[b]
			UF.pr[a]=b
			UF.sz[b]+=UF.sz[a]
		else
			UF.pr[b]=a
			UF.sz[a]+=UF.sz[b]
		end
		true
	end
end
size(UF,a)=UF.sz[find(UF,a)]
function main()
	N,M=map(x->parse(Int,x),split(readline()))
	pr=-ones(Int,M)
	uf=UF(N)
	for i=1:N
		L=map(x->parse(Int,x),split(readline()))
		shift!(L)
		for l=L
			if pr[l]<0
				pr[l]=i
			else
				unite(uf,pr[l],i)
			end
		end
	end
	for i=1:N
		if !same(uf,1,i)
			println("NO")
			exit()
		end
	end
	println("YES")
end
main()

Score : 400 points

Problem Statement

On a planet far, far away, M languages are spoken. They are conveniently numbered 1 through M.

For CODE FESTIVAL 20XX held on this planet, N participants gathered from all over the planet.

The i-th (1≦i≦N) participant can speak K_i languages numbered L_{i,1}, L_{i,2}, ..., L_{i,{}K_i}.

Two participants A and B can communicate with each other if and only if one of the following conditions is satisfied:

There exists a language that both A and B can speak.

There exists a participant X that both A and B can communicate with.

Determine whether all N participants can communicate with all other participants.

Constraints

2≦N≦10^5

1≦M≦10^5

1≦K_i≦M

(The sum of all K_i)≦10^5

1≦L_{i,j}≦M

L_{i,1}, L_{i,2}, ..., L_{i,{}K_i} are pairwise distinct.

Partial Score

200 points will be awarded for passing the test set satisfying the following: N≦1000, M≦1000 and (The sum of all K_i)≦1000.

Additional 200 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N M
K_1 L_{1,1} L_{1,2} ... L_{1,{}K_1}
K_2 L_{2,1} L_{2,2} ... L_{2,{}K_2}
:
K_N L_{N,1} L_{N,2} ... L_{N,{}K_N}

Output

If all N participants can communicate with all other participants, print YES. Otherwise, print NO.

Sample Input 1

4 6
3 1 2 3
2 4 2
2 4 6
1 6

Sample Output 1

YES

Any two participants can communicate with each other, as follows:

Participants 1 and 2: both can speak language 2.

Participants 2 and 3: both can speak language 4.

Participants 1 and 3: both can communicate with participant 2.

Participants 3 and 4: both can speak language 6.

Participants 2 and 4: both can communicate with participant 3.

Participants 1 and 4: both can communicate with participant 2.

Note that there can be languages spoken by no participant.

Sample Input 2

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

Sample Output 2

NO

For example, participants 1 and 3 cannot communicate with each other.


mutable struct UF
	n::Int
	pr::Array{Int}
	sz::Array{Int}
	UF(n)=new(n,1:n,ones(Int,n))
end
function find(UF,a)
	if UF.pr[a]==a
		a
	else
		UF.pr[a]=find(UF,UF.pr[a])
	end
end
same(UF,a,b)=find(UF,a)==find(UF,b)
function unite(UF,a,b)
	a=find(UF,a)
	b=find(UF,b)
	if a==b
		false
	else
		if UF.sz[a]<UF.sz[b]
			UF.pr[a]=b
			UF.sz[b]+=UF.sz[a]
		else
			UF.pr[b]=a
			UF.sz[a]+=UF.sz[b]
		end
		true
	end
end
size(UF,a)=UF.sz[find(UF,a)]
function main()
	N,M=map(x->parse(Int,x),split(readline()))
	pr=-ones(Int,M)
	uf=UF(N)
	for i=1:N
		L=map(x->parse(Int,x),split(readline()))
		shift!(L)
		for l=L
			if pr[l]<0
				pr[l]=i
			else
				unite(uf,pr[l],i)
			end
		end
	end
	for i=1:N
		if !same(uf,1,i)
			println("NO")
			exit()
		end
	end
	println("YES")
end
main()


struct UF
	n::Int
	pr::Array{Int}
	sz::Array{Int}
	UF(n)=new(n,1:n,ones(Int,n))
end
function find(UF,a)
	if UF.pr[a]==a
		a
	else
		UF.pr[a]=find(UF,UF.pr[a])
	end
end
same(UF,a,b)=find(UF,a)==find(UF,b)
function unite(UF,a,b)
	a=find(UF,a)
	b=find(UF,b)
	if a==b
		false
	else
		if UF.sz[a]<UF.sz[b]
			UF.pr[a]=b
			UF.sz[b]+=UF.sz[a]
		else
			UF.pr[b]=a
			UF.sz[a]+=UF.sz[b]
		end
		true
	end
end
size(UF,a)=UF.sz[find(UF,a)]
function main()
	N,M=map(x->parse(Int,x),split(readline()))
	pr=-ones(Int,M)
	uf=UF(N)
	for i=1:N
		L=map(x->parse(Int,x),split(readline()))
		shift!(L)
		for l=L
			if pr[l]<0
				pr[l]=i
			else
				unite(uf,pr[l],i)
			end
		end
	end
	for i=1:N
		if !same(uf,1,i)
			println("NO")
			exit()
		end
	end
	println("YES")
end
main()


H,W=map(x->parse(Int,x),split(readline()))
S=[chomp(readline()) for _=1:H]
flag=true
for i=1:H,j=1:W
  t=0
  for (x,y)=((i+1,j),(i,j+1))
    t+=1<=x<=H&&1<=y<=W&&S[x][y]=='#'
  end
  if t==2
    flag=false
  end
end
println(flag?"Possible":"Impossible")

Score : 200 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

We have a grid of H rows and W columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).

You are given a matrix of characters a_{ij} (1 \leq i \leq H, 1 \leq j \leq W). After Shik completes all moving actions, a_{ij} is # if the stone had ever located at the i-th row and the j-th column during the process of moving. Otherwise, a_{ij} is .. Please determine whether it is possible that Shik only uses right and down moves in all steps.

Constraints

2 \leq H, W \leq 8

a_{i,j} is either # or ..

There exists a valid sequence of moves for Shik to generate the map a.

Input

The input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If it is possible that Shik only uses right and down moves, print Possible. Otherwise, print Impossible.

Sample Input 1

4 5
##...
.##..
..##.
...##

Sample Output 1

Possible

The matrix can be generated by a 7-move sequence: right, down, right, down, right, down, and right.

Sample Input 2

5 3
###
..#
###
#..
###

Sample Output 2

Impossible

Sample Input 3

4 5
##...
.###.
.###.
...##

Sample Output 3

Impossible


a=parse.(split(readline()))
a = sort(a)
println(a[1]+a[2]==a[3]?"Yes":"No")

Score : 100 points

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

There are three candy packs, each of which contains a, b, and c candies, respectively.

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

Constraints

1 ≦ a, b, c ≦ 100

Input

The input is given from Standard Input in the following format:

a b c

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

Sample Input 1

10 30 20

Sample Output 1

Yes

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

Sample Input 2

30 30 100

Sample Output 2

No

In this case, the student who gets the pack with 100 candies always has more candies than the other.

Note that every pack must be given to one of them.

Sample Input 3

56 25 31

Sample Output 3

Yes