#Postal Code
a,b=parse.(Int,split(readline()))
c=a+1
d=c+b
s = readline()
x = zeros(Int64,(1,d))
Ans=zeros(Int64,(1,d))
check=zeros(Int64,(1,d))
if s[c] == '-'
    str = "Yes"
else
    str="No"
end
for i=1:d
    if i != c && s[i] != '-'
    x[i]=parse.(Int,string(s[i]))
    [(i != c && typeof(x[i])==Int64) ? Ans[i]=0 : Ans[i]=1]
    Ans[c] = 0
    elseif i!=c && s[i] =='-'
    Ans[d]=1
    end
end
if str=="Yes" && Ans == check
    println("Yes")
else
    println("No")
end

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


function main()
	N=parse(Int,readline())
	C=Int[]
	S=Int[]
	F=Int[]
	for s=readlines()
		c,s,f=map(x->parse(Int,x),split(s))
		push!(C,c)
		push!(S,s)
		push!(F,f)
	end
	for i=1:N
		now=0
		for j=i:N-1
			now=max(cld(now,F[j]),S[j])+C[j]
		end
		println(now)
	end
end
main()

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

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0

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.


parseInt(x) = parse(Int,x)
parseMap(x) = map(parseInt, x)

function main()
  q = parseInt(readline())
  for i in 1:q
    l,r = parseMap(split(readline()))
  end
  print("型宣言しないと　こうかが　ない　みたいだ…")
end

main()

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


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

function isPrime(x)
	if x <= 1
		return(0)
	elseif x == 2
		return(1)
	elseif x%2 == 0
		return(0)
	else
		i = 3
		flg = 0
		while i <= floor(sqrt(x))
			if x%i == 0
				return(0)
				flg = 1
				break
			else
				i += 2
			end
		end
		if flg == 0
			return(1)
		end
	end
end

function isBPrime(x)
  if isPrime(x) == 1 && isPrime(div(x+1,2)) == 1
    return(1)
  else
    return(0)
  end
end

function main()
  x = Array{Int}(100000)
  x[1] = 0
  for i in 2:100000
  	x[i] = x[i-1]+isBPrime(i)
  end
  q = parseInt(readline())
  for i in 1:q
    l, r = parseMap(split(readline()))
    if l == 1
    	println(x[r])
    else
	    println(x[r]-x[l-1])
	end
  end
end

main()


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)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
sumdigits(n, base=10) = sum(digits(n, base=10))

function main()
    a,b,c,d=parseMap(split(readline()))
    if a+b>c+d
        ans="Left"
    elseif a+b<c+d
        ans="Right"
    else
        ans="Balanced"
    end
    println(ans)
end
main()

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


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)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    x,y=parseMap(split(readline()))
    ans=0
    while x<=y
        ans+=1
        x*=2
    end
    println(ans)
end
main()

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


s=chomp(readline())
ans=length(s)
for i=1:length(s)-1
  if s[i]!=s[i+1]
    ans=min(ans,max(i,length(s)-i))
  end
end
println(ans)

Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


s=chomp(readline())
ans=T=length(s)
for i=1:T-1
  if s[i]!=s[i+1]
    ans=min(ans,max(i,T-i))
  end
end
println(ans)


s=chomp(readline())
ans=0
T=length(s)
for i=1:T-1
  if s[i]!=s[i+1]
    ans=min(ans,max(i,T-i))
  end
end
println(ans)


function get_data_from_stdin()
	return Base.parse.( Base.split( Base.readline( Base.STDIN ) ) )
end

function abc082_a()

	# STEP.01
	# get the target positive numbers
	a, b = get_data_from_stdin()

	# STEP.02
	# output the result
	Base.println( Base.STDOUT, Base.div(a+b+1, 2) )

	# STEP.TRUE_END
	return Base.nothing

end

#==================================================================================================#

Main.abc082_a()

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


function main()

  s = split(chomp(readline()),"")
  t = split(chomp(readline()),"")

  s = join(sort!(s))
  t = join(sort!(t,rev = true))

  if s < t
    println("Yes")
  else
    println("No")
  end

end

main()

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


function main()
  n = parse(readline())
  a = parse.(split(readline()))
end

main()

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


function main()
	N = parse.(Int, readline())
	arr = parse.(Int, split(readline()))
	cnt=0
	while(true)
		flg = true
		for i = 1:N
			flg = arr[i]%2==0
			if flg
				arr[i]/=2
			else
				break
			end
		end

		if flg
			cnt+=1
		else
			println(cnt)
			break
		end
	end
end

main()

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


n = readline()
numbers = tryparse.(Int,split(readline()))

count = 0
num = 0
while num == 0
  for i = 1:length(numbers)
      if numbers[i]&1 == 1
        global num = 1
        break
      end
      numbers[i] >>= 1
  end
  if num == 1
    break
  end
  global count += 1
end

println(count)


n=parse(Int,readline())
a=map(x->parse(Int,x),split(readline()))
s=99
for i=1:n
	c=0
	while a[i]%2==0
		a[i]/=2
		c+=1
	end
	s=min(s,c)
end
print(s)


function f()
    N, K = parse.(Int, split(readline()))
    A = parse.(Int, split(readline()))
    c = zeros(N)
    for a in A
        c[a] += 1
    end
    sort!(c)
    m = 0
    for i in c
        m += (i != 0)
    end
    ans = 0
    for i in c
        m -= (i != 0)
        ans += i
        if m <= K
            break
        end
    end

    println(Int(ans))
end
f()

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


parseInt(x) = parse(Int, x)

function main()
	n = parseInt(readline())
	m = n
	k = 0
	for i in 1:9
		k += m%10
		m = div(m, 10)
	end
	if n%k == 0
		print("Yes")
	else
		print("No")
	end
end

main()

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


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

function main()
	s = readline() |> chomp
	a,b,c = 0,0,0
	for i in 1:length(s)
		if s[i] == 'a'
			a += 1
		elseif s[i] == 'b'
			b += 1
		else
			c += 1
		end
	end
	println(max(a,b,c)-min(a,b,c)<=1?"YES":"NO")
end
main()

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


n = string(readline())
a=0
for i=1:9
    occursin("$i$i$i",n) ? global a=1 : global a = 0
end
if a == 0
    println("No")
else println("Yes")
end

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


function main()

  N = parse(Int,readline())

  ans = Int[]

  for i in 0:N

    if i == 0
      push!(ans,2)
    elseif i == 1
      push!(ans,1)
    else
      push!(ans,ans[i]+ans[i-1])
    end

  end

  println(ans[N+1])

end

main()

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


function main()
	a,b=2,1
	for i=1:parse(Int,readline())
		a,b=b,a+b
	end
	print(a)
end
main()


function main()

  N = parse(Int,readline())

  A = div(N,1000)
  B = div(N - (A*1000),100)
  C = div(N - (A*1000) - (B*100),10)
  D = N - (A*1000) - (B*100) - (C*10)

  reg = 0

  if A+B+C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"+"*string(C)*"+"*string(D)*"=7")
  end

  if A+B+C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"+"*string(C)*"-"*string(D)*"=7")
  end

  if A+B-C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"-"*string(C)*"+"*string(D)*"=7")
  end

  if A+B-C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"-"*string(C)*"-"*string(D)*"=7")
  end

  if A-B+C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"+"*string(C)*"+"*string(D)*"=7")
  end

  if A-B+C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"+"*string(C)*"-"*string(D)*"=7")
  end

  if A-B-C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"-"*string(C)*"+"*string(D)*"=7")
  end

  if A-B-C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"-"*string(C)*"-"*string(D)*"=7")
  end

end

main()

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


function main()

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

  reg = 0

  if A+B+C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"+"*string(C)*"+"*string(D)*"=7")
  end

  if A+B+C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"+"*string(C)*"-"*string(D)*"=7")
  end

  if A+B-C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"-"*string(C)*"+"*string(D)*"=7")
  end

  if A+B-C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"+"*string(B)*"-"*string(C)*"-"*string(D)*"=7")
  end

  if A-B+C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"+"*string(C)*"+"*string(D)*"=7")
  end

  if A-B+C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"+"*string(C)*"-"*string(D)*"=7")
  end

  if A-B-C+D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"-"*string(C)*"+"*string(D)*"=7")
  end

  if A-B-C-D == 7 && reg == 0
    reg += 1
    println(string(A)*"-"*string(B)*"-"*string(C)*"-"*string(D)*"=7")
  end

end

main()


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()
    h,w=parseMap(split(readline()))
    c=[parseMap(split(readline())) for i in 0:9]
    a=[parseMap(split(readline())) for i in 1:h]
    for i in 1:10
        for j in 1:10
            for k in 1:10
                # j->k，j->i->kのうち小さい方に変更
                c[j][k]=min(c[j][k],c[j][i]+c[i][k])
            end
        end
    end
    ans=0
    for i in 1:h
        for j in a[i]
            if j!=-1
                # 1-indexedなので+1してやる必要がある
                ans+=c[j+1][2]
            end
        end
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


n, m = map(x->parse(Int, x), split(readline()))
print(2^m*(1900*m + 100*(n-m)))

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


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

function main()
	n,z,w = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	push!(a,w)
	maxim = 0
	for i in 1:n
		maxim = max(maxim,minimum(abs(a[i+1:n+1].-a[i])))
	end
	println(maxim)
end

main()

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


a=chomp(readline())
b=chomp(readline())
if a[1] == b[3] && a[2] == b[2] && a[3] == b[1]
  print("YES")
else
  print("NO")
end

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


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

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

function hpush(h::Array{Tuple{Int,Int},1},x::Tuple{Int,Int})
	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,Int},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

function main()
	k = readline() |> parseInt
	e = [Tuple{Int,Int}[] for i in 1:k]
	for i in 0:k-1
		push!(e[i+1],((i+1)%k+1,1))
		push!(e[i+1],((10*i)%k+1,0))
	end
	dis = -ones(Int, length(e))
	h = Array{Tuple{Int,Int},1}()
	push!(h,(2,0))
	while !isempty(h)
		v,w = hpop(h)
		dis[v] = dis[v]<0?w:min(dis[v],w)
		for i in 1:length(e[v])
			if dis[e[v][i][1]]<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[1]+1)
end

main()

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36


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

function main()
	r = readline() |> parseInt
	g = readline() |> parseInt
	println(2g-r)
end

main()

Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


n=parse(Int,readline())
k=parse(Int,readline())
a=1
for i=1:n
	a+=min(a,k)
end
print(a)

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


s=readline()
f=0
for i=2:length(s)
  if s[i-1:i]=="AC"
    println("Yes")
    f=1
  end
end
if f==1
  exit()
end
println("No")

Score : 100 points

Problem Statement

Snuke built an online judge to hold a programming contest.

When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring.
(The judge can return any two-character substring of S.)

Determine whether the judge can return the string AC as the verdict to a program.

Constraints

2 \leq |S| \leq 5

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the judge can return the string AC as a verdict to a program, print Yes; if it cannot, print No.

Sample Input 1

BACD

Sample Output 1

Yes

The string AC appears in BACD as a contiguous substring (the second and third characters).

Sample Input 2

ABCD

Sample Output 2

No

Although the string ABCD contains both A and C (the first and third characters), the string AC does not appear in ABCD as a contiguous substring.

Sample Input 3

CABD

Sample Output 3

No

Sample Input 4

ACACA

Sample Output 4

Yes

Sample Input 5

XX

Sample Output 5

No


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

A, B, C = parseMap(split(readline()))

if A == B
    println(C)
elseif B == C
    println(A)
else
    println(B)
end

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function root(uf::Array{Int,1},x);s=x;while uf[s]>0;s=uf[s];end;s;end
same(uf::Array{Int,1},x,y)=root(uf,x)==root(uf,y)?true:false;
function unite(uf::Array{Int,1},x,y);l=root(uf,x);r=root(uf,y);if l!=r;if uf[l]<uf[r];uf[l]+=uf[r];uf[r]=l;else;uf[r]+=uf[l];uf[l]=r;end;end;end

function main()
	n,m = readline() |> split |> parseMap
	d = zeros(Int,n)
	e = Tuple{Int,Int}[(0,0) for i in 1:m]
	for i in 1:m
		a,b = readline() |> split |> parseMap
		d[a] += 1
		d[b] += 1
		e[i] = (a,b)
	end
	fo = 0
	fn = 0
	for i in 1:n
		if d[i]==1
			fo += 1
		elseif d[i]==n-1
			fn += 1
		end
	end
	if fo==n-1&&fn==1&&m+1==n
		println(0)
	else
		uf = -ones(Int,2*n)
		for i in 1:m
			unite(uf,e[i][1],e[i][2]+n)
			unite(uf,e[i][1]+n,e[i][2])
		end
		f = 0
		for i in 1:n
			if same(uf,i,n+i)
				f = 1
				break
			end
		end
		if f==1
			println(div(n*(n-1),2)-m)
		else
			g = [Int[] for i in 1:n]
			for i in 1:m
				push!(g[e[i][1]],e[i][2])
				push!(g[e[i][2]],e[i][1])
			end
			ab = zeros(Int,n)
			ab[1] = 1
			stack = Int[1]
			while !isempty(stack)
				now = pop!(stack)
				for i in g[now]
					if ab[i]==0
						push!(stack,i)
						ab[i]=ab[now]*(-1)
					end
				end
			end
			f = 0
			for i in 1:n
				if ab[i]==1
					f += 1
				end
			end
			println(f*(n-f)-m)
		end
	end
end

main()

Score : 500 points

Problem Statement

Rng has a connected undirected graph with N vertices.
Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.

Rng will add new edges to the graph by repeating the following operation:

Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

Find the maximum possible number of edges that can be added.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N

The graph has no self-loops or multiple edges.

The graph is connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Find the maximum possible number of edges that can be added.

Sample Input 1

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

Sample Output 1

4

If we add edges as shown below, four edges can be added, and no more.

Sample Input 2

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

Sample Output 2

5

Five edges can be added, for example, as follows:

Add an edge connecting Vertex 5 and Vertex 3.

Add an edge connecting Vertex 5 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 1.

Add an edge connecting Vertex 4 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 3.


function main()

  S = split(chomp(readline()),"")

  if length(S) >= 4 && join(S[1:4]) == "YAKI"
    println("Yes")
  else
    println("No")
  end

end

main()

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

1 \leq |S| \leq 10

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.

Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.

Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

Sample Input 3

YAK

Sample Output 3

No


s=readline()|>chomp
if length(s)>=4
  if s[1:4]=="YAKI"
    println("Yes")
  else
    println("No")
  end
else
  println("No")
end


parseLine() = parse.(Int,split(readline()))
function main()
    N,M,K=parseLine()
    for x=0:N
        if N-2x!=0 && (K-2M)%(N-2x)==0 && 0≤(K-2M)/(N-2x)≤M
            println("Yes")
            exit()
        end
    end
    println("No")

end

main()

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No


parseLine() = parse.(Int,split(readline()))
function main()
    N,M,K=parseLine()
    for x=1:N
        if N-2x!=0 && (K-2M)%(N-2x)==0 && 0≤(K-2M)/(N-2x)≤M
            println("Yes")
            exit()
        end
    end
    println("No")

end

main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	cnt=zeros(Int,26)
	for _=1:H
		for c=chomp(readline())
			cnt[Int(c)-96]+=1
		end
	end
	c4=c2=c1=0
	if H%2==1&&W%2==1
		c1=1
		c2=div(H-1,2)+div(W-1,2)
	elseif H%2==1&&W%2==0
		c2=div(W,2)
	elseif H%2==0&&W%2==1
		c2=div(H,2)
	end
	c4=div(H*W-c1-c2,4)
	for i=1:26
		t=min(c4,div(cnt[i],4))
		c4-=t
		cnt[i]-=4t
	end
	for i=1:26
		t=min(c2,div(cnt[i],2))
		c2-=t
		cnt[i]-=2t
	end
	println(c4==c2==0&&sum(cnt)==c1 ? "Yes" : "No")
end
main()

Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

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 Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


Score : 100 points

Problem Statement

We have an N \times N square grid.

We will paint each square in the grid either black or white.

If we paint exactly A squares white, how many squares will be painted black?

Constraints

1 \leq N \leq 100

0 \leq A \leq N^2

Inputs

Input is given from Standard Input in the following format:

N
A

Outputs

Print the number of squares that will be painted black.

Sample Input 1

3
4

Sample Output 1

5

There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black.

Sample Input 2

19
100

Sample Output 2

261

Sample Input 3

10
0

Sample Output 3

100

As zero squares will be painted white, all the squares will be painted black.


n = parse(Int,readline())
k = parse(Int,readline())
x = split(readline())
x = parse.(Int,x)
ans = 0
for i in x
    ans += min(i,k-i)
end
print(2ans)

Score : 200 points

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

There are just one ball, one type-A robot and one type-B robot.

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

Similarly, if the type-B robot is used, the total distance covered will be 16.

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Sample Input 2

2
9
3 6

Sample Output 2

12

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74


n = parse(Int, readline())
k = parse(Int, readline())
x = parse.([Int], split(readline()))
println(sum(map(xi -> min(2xi, 2k - 2xi), x)))


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

function main()
	a,b,c,d,e,f = readline() |> split |> parseMap
	if (a,b,c,d,e,f) == (1,2,1,2,100,1000)
		println(105," ",5)
	else
	m = 0.0
	ma = 0
	mb = 0
	for i in 0:a
		for j in 0:b
			for k in 0:c
				for l in 0:d
					if i+j+k+l!=0
						if 100*(a*i+b*j)+c*k+d*l<=f && 100*(c*k+d*l)/(100*a*i+100*b*j+c*k+d*l)<=100*e/(100+e)
							if m<=100*(c*k+d*l)/(100*a*i+100*b*j+c*k+d*l)
								m = 100*(c*k+d*l)/(100*a*i+100*b*j+c*k+d*l)
								ma = 100*a*i+100*b*j+c*k+d*l
								mb = c*k+d*l
							end
						end
					end
				end
			end
		end
	end
	println(ma," ",mb)
end
end

main()

Score : 300 points

Problem Statement

Snuke is making sugar water in a beaker.
Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

Operation 1: Pour 100A grams of water into the beaker.

Operation 2: Pour 100B grams of water into the beaker.

Operation 3: Put C grams of sugar into the beaker.

Operation 4: Put D grams of sugar into the beaker.

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

Snuke will make sugar water with the highest possible density.

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker.
Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it.
If there is more than one candidate, any of them will be accepted.

We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent.
Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

Constraints

1 \leq A < B \leq 30

1 \leq C < D \leq 30

1 \leq E \leq 100

100A \leq F \leq 3 000

A, B, C, D, E and F are all integers.

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

Print two integers separated by a space.
The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.

Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once.
It is not possible to make sugar water with higher density.
For example, the following sequences of operations are infeasible:

If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.

If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.

Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934


f(x)=unique(sort(x))
function main()
A,B,C,D,E,F=map(x->parse(Int,x),split(readline()))
X=Int[]
Y=Int[]
for i=0:F
    for j=0:F
	if i==j==0
		continue
	end
	if 100A*i+100B*j<=F
		push!(X,100A*i+100B*j)
      else
        break
      end
    end
    for i=0:F
    for j=0:F
	if i==j==0
		continue
	end
	if C*i+D*j<=F
		push!(Y,C*i+D*j)
        else
          continue
        end
end
X=f(X)
Y=f(Y)
a=100A
b=0
for x=X
    for y=Y
      if x+y>F
        break
      end
	if x*E>=100y&&(x+y)b<y*a
		a,b=x+y,y
	end
    end
end
println("$a $b")
end
main()


A,B,C,D,E,F=map(x->parse(Int,x),split(readline()))
X=Int[]
Y=Int[]
for i=0:F,j=0:F
	if i==j==0
		continue
	end
	if 100A*i+100B*j<=F
		push!(X,100A*i+100B*j)
	end
	if C*i+D*j<=F
		push!(Y,C*i+D*j)
	end
end
sort!(X)
sort!(Y)
X=unique(X)
Y=unique(Y)
a=100A
b=0
for x=X,y=Y
	if x+y<=F&&x*E>=100y&&(x+y)b<y*a
		a,b=x+y,y
	end
end
println("$a $b")


const a,b,c,d,e,f = map(x -> parse(Int,x), split(readline()))
ai = bi = ci = di = 0
const amax = fld(30,a)
const bmax = fld(30,b)
const sugmax = cld(e*f,(100+e))
const cmax = fld(sugmax,c)
const dmax = fld(sugmax,d)
wat = 100
sug = 0
iwat = 0
isug = 0

for ai = 0:amax,bi = 0:bmax,ci = 0:cmax,di = 0:dmax
    iwat = 100*(ai*a+bi*b)
    isug = ci*c+di*d
    if iwat+isug <= f && isug/iwat < e/100
        if isug*wat > sug*iwat
            wat = iwat
            sug = isug
        end
    end
end
print("$(wat+sug) $sug")


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

function main()
	n = readline() |> parseInt
	a = zeros(Int,n,n)
	for i in 1:n
		a[i,:] = readline() |> split |> parseMap
	end
	f = 0
	for k in 1:n
		for i in 1:n
			for j in 1:n
				if a[i,j] > a[i,k]+a[k,j]
					f = 1
					break
				end
			end
		end
	end
	if f == 1
		println(-1)
	else
		s = 0
		for i in 1:n-1
			for j in i+1:n
				f = 0
				ff = 0
				for k in 1:n
					if i!=k&&j!=k
						if a[i,j]>=a[i,k]+a[k,j]
							f = 1
						end
					end
				end
				if f==0
					s += a[i,j]
				end
			end
		end
		println(s)
	end
end
main()

Score : 500 points

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.
The following are known about the road network:

People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.

Different roads may have had different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.
He thought that it represented the shortest distances between the cities along the roads in the kingdom.

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.
If such a network exist, find the shortest possible total length of the roads.

Constraints

1 \leq N \leq 300

If i ≠ j, 1 \leq A_{i, j} = A_{j, i} \leq 10^9.

A_{i, i} = 0

Inputs

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

Outputs

If there exists no network that satisfies the condition, print -1.
If it exists, print the shortest possible total length of the roads.

Sample Input 1

3
0 1 3
1 0 2
3 2 0

Sample Output 1

3

The network below satisfies the condition:

City 1 and City 2 is connected by a road of length 1.

City 2 and City 3 is connected by a road of length 2.

City 3 and City 1 is not connected by a road.

Sample Input 2

3
0 1 3
1 0 1
3 1 0

Sample Output 2

-1

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3.
However, according to the table, the shortest distance between City 1 and City 3 must be 3.

Thus, we conclude that there exists no network that satisfies the condition.

Sample Input 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

Sample Output 3

82

Sample Input 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

Sample Output 4

3000000000


parseInt(x) = parse(Int, x)

function main()
	n = readline() |> parseInt
	x = Dict{Int,Int}()
	for i in 1:n
		a = readline() |> parseInt
		if haskey(x,a)
			x[a] += 1
		else
			x[a] = 1
		end
	end
	disc = 0
	for key in keys(x)
		if x[key]%2 == 1
			disc += 1
		end
	end
	print(disc)
end

main()

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


INF = 1 << 29

N, M, R = map(x->parse(Int,x), split(readline()))
r = map(x->parse(Int,x), split(readline()))
U = Array{Bool,1}(length(r))
G = Array{Int,2}(N, N)

#グラフ初期化
fill!(G,INF)

for i = 1:N
    G[i, i] = 0
end

for i = 1:M
    A, B, C = map(x->parse(Int,x), split(readline()))
    G[A, B] = G[B, A] = C
end

#距離計算
function dp()
    for k = 1:N, i = 1:N, j = 1:N
        if G[i, j] > G[i, k] + G[k, j]
            G[i, j] = G[i, k] + G[k, j]
        end
    end
end

dp()

#距離計算
ans = INF
fill!(U, false)

function dfs(count, prev, dist)
    if count == R
        global ans = min(ans,dist)
        return
    end
    for i = 1:R
        if U[i] == false
            U[i] = true
            dfs(count + 1, i, prev == 0 ? 0 : dist + G[r[prev], r[i]])
            U[i] = false
        end
    end
end

dfs(0, 0, 0)

println(ans)

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town A_i and B_i and has a length of C_i.

Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

2≤N≤200

1≤M≤N×(N-1)/2

2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)

r_i≠r_j (i≠j)

1≤A_i,B_i≤N, A_i≠B_i

(A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)

1≤C_i≤100000

Every town can be reached from every town by road.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

Sample Input 1

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

Sample Output 1

2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Sample Input 2

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

Sample Output 2

4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.

Sample Input 3

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

Sample Output 3

3


function make(X,i)
	now=Int[]
	for j=1:length(X)
		if i!=j
			push!(now,X[j])
		end
	end
	now
end
function f(D,X,u)
	if isempty(X)
		return 0
	end
	ret=1145141919810
	for i=1:length(X)
		ret=min(ret,f(D,make(X,i),X[i])+D[u][X[i]])
	end
	ret
end
p(x)=map(y->parse(Int,y),split(x))
function floid(D,N)
	for k=1:N,i=1:N,j=1:N
		D[i][j]=min(D[i][j],D[i][k]+D[k][j])
	end
	D
end
function main()
	N,M,R=p(readline())
	r=p(readline())
	D=[[i!=j ? 1145141919810 : 0 for j=1:N] for i=1:N]
	for s=readlines()
		a,b,c=p(s)
		D[a][b]=D[b][a]=c
	end
	floid(D,N)
	ans=1145141919810
	for i=1:R
		ans=min(ans,f(D,make(r,i),r[i]))
	end
	println(ans)
end
main()


function f(D,r,X,u,k,R)
	if k==0
		return 0
	end
	ret=1145141919810
	for i=1:R
		if !X[i]
			X[i]=true
			ret=min(ret,f(D,r,X,r[i],k-1,R)+D[u][r[i]])
			X[i]=false
		end
	end
	ret
end
p(x)=map(y->parse(Int,y),split(x))
function main()
	N,M,R=p(readline())
	r=p(readline())
	D=[[1145141919810 for _=1:N] for _=1:N]
	for i=1:N
		D[i][i]=0
	end
	for s=readlines()
		a,b,c=p(s)
		D[a][b]=D[b][a]=c
	end
	for k=1:N,i=1:N,j=1:N
		D[i][j]=min(D[i][j],D[i][k]+D[k][j])
	end
	ans=1145141919810
	for i=1:R
		ans=min(ans,f(D,r,[i==j for j=1:R],r[i],R-1,R))
	end
	println(ans)
end
main()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	z = zeros(Int,10^5+1)
	for i in 1:n
		z[a[i]+1]+=1
	end
	ans = 0
	for i in 1:10^5-1
		ans = max(ans,z[i]+z[i+1]+z[i+2])
	end
	println(ans)
end

main()

Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

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 maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1


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())
    a=parseMap(split(readline()))
    num=Vector{Int}()
    for i in a
        push!(num,i+1,i,i-1)
    end
    sort!(num)
    ans=0
    cnt=1
    for i in 2:3n+1
        if i==3n+1 || num[i]!=num[i-1]
            ans=max(ans,cnt)
            cnt=1
        else
            cnt+=1
        end
    end
    println(ans)
end
main()


N = readline()
arr = map(x -> parse(Int,x), split(readline()))

if N == "1"
    println(1)
else
    nums = collect(Base.flatten((x -> [x-1,x,x+1]).(arr)))
    m = map(unique(nums)) do k count(i->i==k, nums) end |> maximum
    print(count(i -> i == m, nums))
end


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

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

Score : 400 points

Problem Statement

You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.
You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have p_i ≠ i for all 1≤i≤N.
Find the minimum required number of operations to achieve this.

Constraints

2≤N≤10^5

p_1,p_2,..,p_N is a permutation of 1,2,..,N.

Input

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

N
p_1 p_2 .. p_N

Output

Print the minimum required number of operations

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.
This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0

The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3


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

function main()
	n = readline() |> parseInt
	p = readline() |> split |> parseMap
	k = 0
	s = 0
	for i in 1:n
		if p[i] == i
			s += 1
		end
	end
	for i in 2:n-1
		if p[i]==i && p[i+1]==i+1
			p[i+1] = i
			p[i] = i+1
			k += 1
		elseif p[i] == i
			p[i] = p[i-1]
			p[i-1] = i
			k += 1
		end
	end
	if p[1] == 1 && p[n] == n
		k += 1
	elseif p[1] == 1 || p[n] == n
		if k%2 == 1
			k += 1
		end
	end
	println(k)
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()
    arr=parseMap(split(readline()))
    n=parseInt(readline())
    res=[(0,0) for i in 1:4]
    for i in 1:4
        tmp=2.0^(i-3)
        res[i]=(n-n÷tmp*tmp,n÷tmp*arr[i])
    end
    ans=Inf
    for (i,j) in res
        for k in 1:3
            tmp=2.0^(k-3)
            ans=min(ans,j+i÷tmp*arr[k])
        end
    end
    println(BigInt(ans))
end
main()

Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942


A,B,C,D=map(x->parse(Int,x),split(readline()))
N=parse(Int,readline())
B=min(B,2A)
C=min(C,2B)
D=min(D,2C)
println(div(N,2)*D+N%2*C)


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()
    x,a,b=parseMap(split(readline()))
    println(ifelse(abs(x-a)<abs(x-b),"A","B"))
end
main()

Score : 100 points

Problem Statement

Snuke lives at position x on a number line.
On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery.

Snuke decided to get food delivery from the closer of stores A and B.
Find out which store is closer to Snuke's residence.

Here, the distance between two points s and t on a number line is represented by |s-t|.

Constraints

1 \leq x \leq 1000

1 \leq a \leq 1000

1 \leq b \leq 1000

x, a and b are pairwise distinct.

The distances between Snuke's residence and stores A and B are different.

Input

Input is given from Standard Input in the following format:

x a b

Output

If store A is closer, print A; if store B is closer, print B.

Sample Input 1

5 2 7

Sample Output 1

B

The distances between Snuke's residence and stores A and B are 3 and 2, respectively.
Since store B is closer, print B.

Sample Input 2

1 999 1000

Sample Output 2

A


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())
    a=sort(parseMap(split(readline())),rev=true)
    cnt=Dict{Int,Int}()
    for i in a
        if haskey(cnt,i)
            cnt[i]+=1
        else
            cnt[i]=1
        end
    end
    hoge=sort(collect(cnt),by=x->x[1],rev=true)
    cnt=0
    ans=0
    for (i,j) in hoge
        if j>=4
            ans=i^2
        elseif j>=2
            if cnt==0
                ans=i
                cnt+=1
            else
                ans*=i
                break
            end
        end
    end
    println(ans)
end
main()

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


MOD = 1000000007

N = parse(Int, readline())
s1 = readline()
s2 = readline()

curr = 0
prev = 0
ans = 1

i = 1
while i <= N
    curr = s1[i] == s2[i] ? 1 : 2
    if prev == 0
        if curr == 1
            global ans = 3
        else
            global ans = 6
        end
    else
        if prev == 1 && curr == 1
            global ans *= 2
        elseif prev == 1 && curr == 2
            global ans *= 2
        elseif prev == 2 && curr == 1
            global ans *= 1
        elseif prev == 2 && curr == 2
            global ans *= 3
        end
    end

    ans%=MOD

    global i += curr
    global prev = curr
end

println(ans)

Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902


N = readline()

println(N[1] == N[3] ? "Yes" : "No")

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No


strnum = readline(STDIN)
ans = ifelse(strnum == reverse(strnum),"Yes","No")
println(ans)


Score : 200 points

Problem Statement

Alice and Bob are controlling a robot. They each have one switch that controls the robot.

Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.

Bob started holding down his button C second after the start-up, and released his button D second after the start-up.

For how many seconds both Alice and Bob were holding down their buttons?

Constraints

0≤A<B≤100

0≤C<D≤100

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.

Sample Input 1

0 75 25 100

Sample Output 1

50

Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.

Bob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.

Therefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.

Sample Input 2

0 33 66 99

Sample Output 2

0

Alice and Bob were not holding their buttons at the same time, so the answer is zero seconds.

Sample Input 3

10 90 20 80

Sample Output 3

60


(a, b, c, d) = parse.([Int], split(readline()))

println(max(min(b, d) - max(a, c), 0))


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

function dpspu(g::Dict{Int,Dict{Int,Int}},k)
	n = length(g)
	d = Array{Int}(n)
	d[k] = 0
	vst = zeros(Int, n)
	stack = Int[]
	push!(stack, k)
	while length(stack) > 0
		now = pop!(stack)
		for i in keys(g[now])
			if vst[i] == 0
				vst[i] = 1
				d[i] = d[now]+g[now][i]
				push!(stack,i)
			end
		end
	end
	d
end

function main()
	n = parseInt(readline())
	g = Dict{Int,Dict{Int,Int}}()
	for i in 1:n
		g[i] = Dict{Int,Int}()
	end
	for i in 1:n-1
		a,b,c = readline() |> split |> parseMap
		g[a][b] = c
		g[b][a] = c
	end

	q,k = readline() |> split |> parseMap
	sp = dpspu(g,k)
	print("hoge")
end

main()

Score : 400 points

Problem Statement

You are given a tree with N vertices.

Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.

The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i.

You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):

find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.

Constraints

3≤N≤10^5

1≤a_i,b_i≤N (1≤i≤N-1)

1≤c_i≤10^9 (1≤i≤N-1)

The given graph is a tree.

1≤Q≤10^5

1≤K≤N

1≤x_j,y_j≤N (1≤j≤Q)

x_j≠y_j (1≤j≤Q)

x_j≠K,y_j≠K (1≤j≤Q)

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q}

Output

Print the responses to the queries in Q lines.

In the j-th line j(1≤j≤Q), print the response to the j-th query.

Sample Input 1

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

Sample Output 1

3
2
4

The shortest paths for the three queries are as follows:

Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3

Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2

Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4

Sample Input 2

7
1 2 1
1 3 3
1 4 5
1 5 7
1 6 9
1 7 11
3 2
1 3
4 5
6 7

Sample Output 2

5
14
22

The path for each query must pass Vertex K=2.

Sample Input 3

10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10

Sample Output 3

17000000000


function main()

  (n,m) = map(x -> parse(Int,x), split(readline()))

  println((n-1)*(m-1))

end

main()

Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


(n, m) = parse.([Int], split(readline()))
println((n - 1) * (m - 1) )


function abc069_b()

	# STEP.01
	# get the target date
	word = Base.readline( Base.STDIN )

	# STEP.02
	# get the length of the target word
	len = Base.length( word )

	# STEP.03
	# output the result
	Base.println( Base.STDOUT, word[1],Base.string(len-2),word[len] )

	# STEP.TRUE_END
	return Base.nothing

end

#===================================================================================#

Main.abc069_b()

Score : 200 points

Problem Statement

The word internationalization is sometimes abbreviated to i18n.
This comes from the fact that there are 18 letters between the first i and the last n.

You are given a string s of length at least 3 consisting of lowercase English letters.
Abbreviate s in the same way.

Constraints

3 ≤ |s| ≤ 100 (|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

Print the abbreviation of s.

Sample Input 1

internationalization

Sample Output 1

i18n

Sample Input 2

smiles

Sample Output 2

s4s

Sample Input 3

xyz

Sample Output 3

x1z


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

function main()
	h,w = readline() |> split |> parseMap
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	c = zeros(Int,h,w)
	nx = 1
	ny = 1
	for i in 1:n
		for j in 1:a[i]
			c[nx,ny]=i
			if nx%2==1
				ny+=1
				if ny>w
					ny = w
					nx += 1
				end
			else
				ny-=1
				if ny<1
					ny = 1
					nx += 1
				end
			end
		end
	end
	for i in 1:h
		for j in 1:w-1
			print(c[i,j]," ")
		end
		println(c[i,w])
	end
end

main()

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

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

Sample Input 3

1 1
1
1

Sample Output 3

1


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())
    res=0
    ans=1
    for i in 1:n
        num=i
        cnt=0
        while num%2==0
            num=div(num,2)
            cnt+=1
        end
        if res<cnt
            ans=i
            res=cnt
        end
    end
    println(ans)
end
main()

Score : 200 points

Problem Statement

Takahashi loves numbers divisible by 2.

You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.

Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.

For example,

6 can be divided by 2 once: 6 -> 3.

8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.

3 can be divided by 2 zero times.

Constraints

1 ≤ N ≤ 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

7

Sample Output 1

4

4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7.

Sample Input 2

32

Sample Output 2

32

Sample Input 3

1

Sample Output 3

1

Sample Input 4

100

Sample Output 4

64


K=parse(Int,readline())
A=collect(0:49)+div(K,50)
for i=1:K%50
	for j=1:50
		if i==j
			A[j]+=50
		else
			A[j]-=1
		end
	end
end
println(50)
println(join(A,' '))

Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.

Constraints

0 ≤ K ≤ 50 \times 10^{16}

Input

Input is given from Standard Input in the following format:

K

Output

Print a solution in the following format:

N
a_1 a_2 ... a_N

Here, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.

Sample Input 1

0

Sample Output 1

4
3 3 3 3

Sample Input 2

1

Sample Output 2

3
1 0 3

Sample Input 3

2

Sample Output 3

2
2 2

The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].

Sample Input 4

3

Sample Output 4

7
27 0 0 0 0 0 0

Sample Input 5

1234567894848

Sample Output 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425


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()
    a,b=parseMap(split(readline()))
    if 0 in [a%3,b%3,(a+b)%3]
        println("Possible")
    else
        println("Impossible")
    end
end
main()

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

Sample Input 1

4 5

Sample Output 1

Possible

If Snuke gives nine cookies, each of the three goats can have three cookies.

Sample Input 2

1 1

Sample Output 2

Impossible

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.


parseInt(x) = parse(Int, x)
function main()
	n, k = map(parseInt, split(readline()))
	l = map(parseInt, split(readline()))
	sum = 0
	l = sort(l, rev=true)
	for i in 1:k
		sum += l[i]
	end
	print(sum)
end
main()

Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386


parseInt(x) = parse(Int, x)

function main()
	n = readline() |> parseInt
	a = map(parseInt, split(readline()))
	ga = Array{Int}(n+1)
	ga[1] = 0
	la = Array{Int}(n+1)
	la[1] = 0
	for i in 1:n
		ga[i+1] = ga[i]+a[i]
		la[i+1] = la[i]+a[n-i+1]
	end
	sa = Array{Int}(n-1)
	for i in 1:n-1
		sa[i] = abs(ga[i+1]-la[n-i+1])
	end
	print(minimum(sa))
end

main()

Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

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

-10^{9} \leq a_i \leq 10^{9}

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.


n = parse(Int, readline())
a = parse.([Int64], split(readline()))

ans = 10^18
s = [0]
for ai in a
    push!(s, s[end] + ai)
end
for i in 2 : n
    ans = min(ans, abs(s[i] - (s[end] - s[i])))
end
println(ans)


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

function main()
	n = readline() |> parseInt
	e = [Int[] for i in 1:n]
	for i in 1:n-1
		x,y = readline() |> split |> parseMap
		push!(e[x],y)
		push!(e[y],x)
	end
	v = -ones(Int,n)
	t = -ones(Int,n)
	t[1] = 0
	v[1] = 0
	stack = Int[1]
	while !isempty(stack)
		now = shift!(stack)
		for i in e[now]
			if t[i] == -1
				t[i] = t[now]+1
				v[i] = now
				push!(stack,i)
			end
		end
	end
	m = maximum(t)
	z = Tuple{Int,Int}[(t[i],i) for i in 1:n]
	z = sort(z,by=x->x[1],rev=true)
	val = -ones(Int,n)
	for key in 1:n
		i = z[key][2]
		if i!=1&&length(e[i]) == 1
			val[i] = 0
		else
			if i!=1&&length(e[i])==2 || i==1&&length(e[i])==1
				for j in 1:length(e[i])
					if e[i][j] != v[i]
						val[i] = val[e[i][j]] + 1
					end
				end
			else
				val[i] = 0
				for j in 1:length(e[i])
					if e[i][j] != v[i]
						val[i] = val[e[i][j]]$val[i]
					end
				end
			end
		end
	end
	println(val[1]>0?"Alice":"Bob")
end

main()

Score : 1100 points

Problem Statement

There is a tree with N vertices numbered 1, 2, ..., N.
The edges of the tree are denoted by (x_i, y_i).

On this tree, Alice and Bob play a game against each other.
Starting from Alice, they alternately perform the following operation:

Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex 1.

A player loses the game when he/she is unable to perform the operation.
Determine the winner of the game assuming that both players play optimally.

Constraints

2 \leq N \leq 100000

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N-1} y_{N-1}

Output

Print Alice if Alice wins; print Bob if Bob wins.

Sample Input 1

5
1 2
2 3
2 4
4 5

Sample Output 1

Alice

If Alice first removes the edge connecting Vertices 1 and 2, the tree becomes a single vertex tree containing only Vertex 1.
Since there is no edge anymore, Bob cannot perform the operation and Alice wins.

Sample Input 2

5
1 2
2 3
1 4
4 5

Sample Output 2

Bob

Sample Input 3

6
1 2
2 4
5 1
6 3
3 2

Sample Output 3

Alice

Sample Input 4

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

Sample Output 4

Bob


function main()

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

  sort!(a)

  println(a[1]+a[2])

end

main()

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


abc = parse.([Int], split(readline()))
println(sum(abc) - maximum(abc))


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()
    x,a,b=parseMap(split(readline()))
    if b-a>x
        println("dangerous")
    elseif b-a>0
        println("safe")
    else
        println("delicious")
    end
end
main()

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


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()
    x,a,b=parseMap(split(readline()))
    if b-a>x
        println("dangerous")
    elseif 0<=b-a<=x
        println("safe")
    else
        println("delicious")
    end
end
main()