parseInt(x) = parse(Int, x)

function power(n)
	k = 1
	while k <= n
		k*=2
	end
	k
end

function bsearch(a,x)
	l,r = 1, length(a)
	while l <= r
		m = l+div(r-l,2)
		if a[m] < x
			l = m+1
		else
			r = m-1
		end
	end
	return(l-1)
end

function main()
	n = readline() |> parseInt
	a = map(parseInt, split(readline()))
	a = sort(a)
	k = 0
	while length(a) > 1
		m = length(a)
		z = power(a[m])-a[m]
		index = bsearch(a,z)+1
		if 0 < index < m
			if a[index] == z
				deleteat!(a,index)
				k += 1
			end
		end
		pop!(a)
	end
	print(k)
end

main()

Score : 600 points

Problem Statement

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

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

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
1 2 3

Sample Output 1

1

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

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


N,K=map(x->parse(Int,x),split(readline()))
H=sort([parse(Int,s) for s=readlines()])
println(minimum([H[i]-H[i-K+1] for i=K:N]))

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

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

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

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

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

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

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

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

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

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

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

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

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

#=function get_rank(N,X,D)
depth = 0
for j in 1:N
    depth += D[j]
    if depth > X
        return j,depth - X
    end
end
end

function get_petty(rank,amari)
    ans = 0
    ans += 2^rank

end

function main()
    N,X = parseMap(split(readline()))
    D = []
    for i in N
        push!(D,2^(i+2)-3)
    end
    if X < D[N]/2
        rank,amari = get_rank(N,X,D)
        ans = get_petty(rank,amari)
    end
end    =#

N,X = parseMap(split(readline()))
D = []
P = []
for i in 0:N
    push!(D,2^(i+2)-3)
    push!(P,2^(i+1)-1)
end
#println(D,P)
function get_patty(N,X)
    if N == 0
    	#println("N==0")
        return 1
    elseif N == 1
    	#println("N==1")
        return 3
    elseif X <= D[N-1] + 1
    	#println("under half")
        return get_patty(N - 1, X - 1)
    else
    	#println("over half")
        amari = X - (1 + D[N - 1]) - 1
        return get_patty(N - 1,amari) + P[N-1]
    end
end

ans = get_patty(N,X)
println(ans)

Score : 400 points

Problem Statement

In some other world, today is Christmas.

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

A level-0 burger is a patty.

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

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

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

Constraints

1 \leq N \leq 50

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

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

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

Sample Input 1

2 7

Sample Output 1

4

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

Sample Input 2

1 1

Sample Output 2

0

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

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

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


N,X=map(x->parse(Int,x),split(readline()))
S=0
while X>0
	T=2<<max(0,N)
	N-=1
	if X>T-2
		S+=div(T,2)
		X-=T-1
	else
		X-=1
	end
end
println(S)


N,X=map(x->parse(Int,x),split(readline()))
S=0
while X>0
	T=2<<N
	N-=1
	if X>T-2
		S+=div(T,2)
		X-=T-1
	else
		X-=1
	end
end
println(S)


N, X = map(x -> parse(Int64,x), split(readline(STDIN)))
a(i) = 2^(i + 2) - 3
p(i) = 2^(i + 1) - 1
function f(i::Int64, X::Int64)
    if i == 0
        return X >= 1 ? 1 : 0
    else
        a_i = a(i)
        center_num = Int64((a_i + 1) / 2)
        if center_num > X
            return f(i - 1, X - 1)
        elseif center_num == X
            return p(i - 1) + 1
        else
            return p(i - 1) + 1 + f(i - 1, X - center_num)
        end
    end
end
println(f(N, X))


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

function dfs(s,n)
    if s!="" && parseInt(s)>n
        return 0
    end
    s_i=join(sort(collect(Set(s))))
    if s_i=="357"
        res=1
    else
        res=0
    end
    for i in "357"
        res+=dfs(join([s,i]),n)
    end
    return res
end

function main()
    println(dfs("",parseInt(readline())))
end
main()

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

575

Sample Output 1

4

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

Sample Input 2

3600

Sample Output 2

13

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

Sample Input 3

999999999

Sample Output 3

26484


parseInt(x) = parse(Int,x)
function division(x)
	y = Int[]
	i = 2
	while x > 1 || i <= sqrt(x)
		if x%i == 0
			push!(y, i)
			x = div(x, i)
		else
			i += 1
		end
	end
	return(y)
end

function main()
	n = parseInt(readline())
	x = Int[]
	for i in 1:n
		append!(x,division(i))
	end
	d = Dict{Int,Int}()
	for i in 1:length(x)
		if haskey(d,x[i])
			d[x[i]] += 1
		else
			d[x[i]] = 1
		end
	end
	two = 0
	four = 0
	ft = 0
	tf = 0
	sf = 0
	for i in values(d)
		if i >= 74
			sf += 1
		end
		if i >= 24
			tf += 1
		end
		if i >= 14
			ft += 1
		end
		if i >= 4
			four += 1
		end
		if i >= 2
			two += 1
		end
	end
	a0 = max(tf * (two-1),0)
	a1 = max(ft * (four-1),0)
	a2 = max(div((four)*(four-1),2)*(two-2),0)
	print(a0+a1+a2+sf)
end
main()

Score : 400 points

Problem Statement

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

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

Note

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

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

9

Sample Output 1

0

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

Sample Input 2

10

Sample Output 2

1

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

Sample Input 3

100

Sample Output 3

543


N=parse(Int,readline())
A=map(x->parse(Int,x),split(readline()))
id=1
S=sum(A)
for i=2:N
  if abs(S-A[id]*N)>abs(S-A[i]*N)
    id=i
  end
end
println(id-1)

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.


const lines=readlines(open("/dev/fd/0"))
let
    state=0
    global input
    input()=lines[state+=1]
end
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    x,y=intLine()
    println(x+Int(y/2))
end
main()

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


parseInt(x) = parse(Int, x)
parseFloat(x) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    t,a=parseMap(split(readline()))
    h=parseMap(split(readline()))
    ans=10^9
    ansindex=0
    for i in 1:n
        if abs(ans-a)>abs((t-h[i]*0.006)-a)
            ans=t-h[i]*0.006
            ansindex=i
        end
    end
    println(ansindex)
end
main()

Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


n, m = parse.(split(readline()))
x = Array{Int}(2,m)
for i in 1:m
  x[:,i] = parse.(split(readline()))
end

print("G")

Score: 300 points

Problem Statement

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

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

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

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

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

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

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

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

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

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

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

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

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


Score : 100 points

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

Constraints

The length of S is 2 or 3.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

Sample Input 1

abc

Sample Output 1

cba

As the length of S is 3, we print it after reversing it.

Sample Input 2

ac

Sample Output 2

ac

As the length of S is 2, we print it as is.


s=chomp(readline())
if length(s)==3
  reverse!(s)
end
println(s)


    function main()
        t = readline()
    	s = split(t, "")
    	if length(s) == 3
    		println(t)
    	else
    		for i in 0:2
    			print(s[3-i])
    		end
        	println()
    	end
    end

    main()


function main()
	s = split(readline(), "")
	if length(s) == 3
		for i in 1:2
			print(s[i])
		end
	else
		for i in 0:3
			print(s[3-i])
		end
	end
	println()
end

main()


function main()
	s = split(readline(), "")
	if length(s) == 2
		for i in 1:2
			print(s[i])
		end
	else
		for i in 0:2
			print(s[3-i])
		end
	end
	println()
end

main()


function main()
	s = split(readline(), "")
	if length(s) == 2
		for i in 1:2
			print(s[i])
		end
	else
		for i in 0:2
			print(s[3-i])
		end
	end
end

main()


function parseInt(x) parse(Int, x) end
function conInt(x) convert(Int, x) end

function main()
	n = parseInt(readline())
	x = Int[]
	sum = 0
	for i in 1:n
		push!(x, parseInt(readline()))
	end
	y = sort(x)
	if length(x) == 2
		println(y[2]-y[1])
	else
		sum += y[n]-y[1]
		if n%2 == 1
			for i in 1:conInt((n-3)/2)
				sum += y[n-i]-y[i]
				sum += y[n+1-i]-y[i+1]
			end
			sum += max(y[conInt((n+1)/2)]-y[conInt((n-1)/2)], y[conInt((n+3)/2)]-y[conInt((n+1)/2)])
		else
			for i in 1:conInt((n-4)/2)
				sum += y[n-i]-y[i]
				sum += y[n+1-i]-y[i+1]
			end
			sum += max(y[conInt(n/2)]-y[conInt((n-2)/2)]+y[conInt((n+4)/2)]-y[conInt((n+2)/2)], y[conInt((n+2)/2)]-y[conInt((n-2)/2)]+y[conInt((n+4)/2)]-y[conInt(n/2)])
		end
		println(sum)
	end
end
main()

Score : 400 points

Problem Statement

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

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

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

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


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

function main()
	n = readline() |> parseInt
	v = Array{Int}(2,n)
	for i in 1:n
		v[:,i] = readline() |> split |> parseMap
	end
	num = [i for i in 1:n]
	sy = sum(v[1,:])
	sz = sum(v[2,:])
	x = [i for i in 1:n]
	g = Array{Int}(2,2*n)
	g[1,1:n] = x
	g[1,n+1:2*n] = x
	g[2,1:n] = v[1,:]
	g[2,n+1:2*n] = v[2,:]
	g = sortcols(g,by=y->y[2])
	sw = 0
	pre = sum(g[2:1:n+1])
	if length(Set(g[1,1:n])) < n
		sw = pre - g[2,n+1]
	elseif g[1,n] != g[1,n+1]
		sw = pre-g[2,n]
	else
		sw = min(pre+g[2,n+2]-g[2,n]-g[2,n+1],pre-g[2,n-1])
	end
	print(min(sy,sz,sw))
end

main()

Score : 700 points

Problem Statement

We have a directed weighted graph with N vertices.
Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i.

In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N, and its weight is {\rm min}(A_x,B_y).

We will consider a directed cycle in this graph that visits every vertex exactly once.
Find the minimum total weight of the edges in such a cycle.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the minimum total weight of the edges in such a cycle.

Sample Input 1

3
1 5
4 2
6 3

Sample Output 1

7

Consider the cycle 1→3→2→1. The weights of those edges are {\rm min}(A_1,B_3)=1, {\rm min}(A_3,B_2)=2 and {\rm min}(A_2,B_1)=4, for a total of 7.
As there is no cycle with a total weight of less than 7, the answer is 7.

Sample Input 2

4
1 5
2 6
3 7
4 8

Sample Output 2

10

Sample Input 3

6
19 92
64 64
78 48
57 33
73 6
95 73

Sample Output 3

227


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

function main()
	n = readline() |> parseInt
	v = Array{Int}(2,n)
	for i in 1:n
		v[:,i] = readline() |> split |> parseMap
	end
	num = [i for i in 1:n]
	sy = sum(v[1,:])
	sz = sum(v[2,:])
	x = [i for i in 1:n]
	g = Array{Int}(2,2*n)
	g[1,1:n] = x
	g[1,n+1:2*n] = x
	g[2,1:n] = v[1,:]
	g[2,n+1:2*n] = v[2,:]
	g = sortcols(g,by=y->y[2])
	sw = 0
	if length(Set(g[1,1:n])) < n
		sw = sum(g[2,1:n])
	elseif g[1,n] == g[1,n+1]
		sw = sum(g[2,1:n+2])-g[2,n]-g[2,n+1]
	else
		sw = sum(g[2,1:n+1])-g[2,n]
	end
	print(min(sy,sz,sw))
end

main()


function main()
	n = parse(readline())
	if n == 1
		print("Hello World")
	elseif n == 2
		a = parse(readline())
		b = parse(readline())
		print(a+b)
	end
end

main()

Score: 100 points

Problem Statement

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.

One day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.

Takahashi, who is taking this exam, suddenly forgets his age.

He decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.

Write this program for him.

Constraints

N is 1 or 2.

A is an integer between 1 and 9 (inclusive).

B is an integer between 1 and 9 (inclusive).

Input

Input is given from Standard Input in one of the following formats:

1

2
A
B

Output

If N=1, print Hello World; if N=2, print A+B.

Sample Input 1

1

Sample Output 1

Hello World

As N=1, Takahashi is one year old. Thus, we should print Hello World.

Sample Input 2

2
3
5

Sample Output 2

8

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.


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

function main()
ct = []
N,T = parse.(split(readline()))
for _ in 1:N
    c,t = parseMap(split(readline()))
    push!(ct,(c,t))
end

ct = sort(collect(ct), by=x->x[2])
if ct[1][2] > T
    return "TLE"
end
min_cost = 10000000
for i in 1:N
    if ct[i][2] <= T
        min_cost > ct[i][1] && (min_cost = ct[i][1])
    else
        return min_cost
    end
    if i == N && min_cost != 10000000
        return min_cost
    end
end
end

println(main())

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


function main()
	nt = parse.(split(readline()))
	min = 1001
	for i in 1:nt[1]
		ct = parse.(split(readline()))
		if ct[2] < nt[2] || ct[1] < min
			min = ct[1]
		end
	end
	if min >= 1001
		print("TLE")
	else
		print(min)
	end
end

main()


function main()
	n = parse.(split(readline()))
	i = 1
	d = 1
	while i <= sqrt(n[2]) && i <= n[2]/n[1]
		i += 1
		if n[2] % i == 0
			p = convert(Int, n[2] / i)
			if p <= n[2]/n[1] && d <= p
				d = p
			elseif d <= i
				d = i
			end
		end
	end
	print(d)
end

main()

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


function main()
	nm = parse.(split(readline()))
	k = nm[2]/nm[1]
	i=1
	max = 0
	while i <= k || i <= sqrt(nm[2])
		if nm[2]%i == 0
			max = i
		end
		i += 1
	end
	print(max)
end

main()


function main()
	nm = parse.(split(readline()))
	q = nm[2]%nm[1]
	if q == 0
		print(Int(nm[2]/nm[1]))
	else
		k = (nm[2]-q)/nm[1]
		b = k%q
		if b == 0
			print(q)
		else
			check = 0
			i = 1
			while check < 1
				if nm[2]%i == 0 && nm[2]/i > nm[1]
					i += 1
				else
					check += 1
				end
			end
			print(i-1)
		end
	end
end

main()


function main()
	nm = parse.(split(readline()))
	q = nm[2]%nm[1]
	if q == 0
		print(Int(nm[2]/nm[1]))
	else
		k = (nm[2]-q)/nm[1]
		b = k%q
		if b == 0
			print(q)
		else
			check = 0
			while check <1
				i = 1
				if m%i == 0 && m/i > n
					i += 1
				else
					check += 1
				end
			end
			print(i)
		end
	end
end

main()


function main()
	nm = parse.(split(readline()))
	q = nm[2]%nm[1]
	k = (nm[2]-q)/nm[1]
	b = k%q
	a = (k-b)/q
	print(a)
end

main()


const lines=readlines(open("/dev/fd/0"))
let
    state=0
    global input
    input()=lines[state+=1]
end
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    println(1110-int(input()))
end
main()

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


a=readline()
b=string(000)
for i=1:3
if a[i]=="1"
b[i]=="9"
else b[i]=="1"
end
end
println(parse(Int,b))


a=readline()
b=string(000)
for i=1:3
if a[i]=="1"
b[i]=="9"
else b[i]=="1"
end
end
println(b)


Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    v=parseMap(split(readline()))
    cnt=Dict{Int,Int}()
    for i in v
        try
            cnt[i]+=1
        catch
            cnt[i]=1
        end
    end
    OddMax=[0,0,0,0]    #奇数で1番目に多い数,その個数,2番目に多い数,その個数
    EvenMax=[0,0,0,0]   #偶数で1番目に多い数,その個数,2番目に多い数,その個数
    # for (i,val) in enumerate(v)
    #     num=cnt[val]
    #     if i%2==1
    #         if OddMax[2]<num
    #             OddMax=[val,num,OddMax[1],OddMax[2]]
    #         elseif OddMax[4]<num && val!=OddMax[1]
    #             OddMax=[OddMax[1],OddMax[2],val,num]
    #         end
    #     else
    #         if EvenMax[2]<num
    #             EvenMax=[val,num,EvenMax[1],EvenMax[2]]
    #         elseif EvenMax[4]<num && val!=EvenMax[1]
    #             EvenMax=[EvenMax[1],EvenMax[2],val,num]
    #         end
    #     end
    # end
    if EvenMax[1]!=OddMax[1]
        ans=0
        for i in 1:n
            if i%2==1 && v[i]!=OddMax[1]
                ans+=1
            elseif i%2==0 && v[i]!=EvenMax[1]
                ans+=1
            end
        end
        println(ans)
    else
        res1=0
        res2=0
        for i in 1:n
            if i%2==1
                if v[i]!=OddMax[1]
                    res1+=1
                end
                if v[i]!=OddMax[3]
                    res2+=1
                end
            elseif i%2==0
                if v[i]!=EvenMax[3]
                    res1+=1
                end
                if v[i]!=EvenMax[1]
                    res2+=1
                end
            end
        end
        println(min(res1,res2))
    end
end
main()

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


parseInt(x) = parse(Int, x)

function main()
	n = readline() |> parseInt
	v = map(parseInt, split(readline()))
	od = Dict{Int,Int}()
	ed = Dict{Int,Int}()
	for i in 1:2:n
		if haskey(od,v[i])
			od[v[i]] += 1
		else
			od[v[i]] = 1
		end
	end
	for i in 2:2:n
		if haskey(ed,v[i])
			ed[v[i]] += 1
		else
			ed[v[i]] = 1
		end
	end
	co = sort(collect(od), by=x->x[2], rev=true)
	ce = sort(collect(ed), by=x->x[2], rev=true)
	if co[1][1] == ce[1][1]
		if length(ce) == 1 && length(co) == 1
			print(n-co[1][2])
		elseif co[1][2] > ce[1][2]
			if length(ce) == 1
				print(min(n-co[1][2],n-ce[1][2]-co[2][2]))
			else
				print(n-co[1][2]-ce[2][2])
			end
		else
			if length(co) == 1
				print(min(n-ce[1][2],n-co[1][2]-ce[2][2]))
			else
				print(n-co[2][2]-ce[1][2])
			end
		end
	else
		print(n-co[1][2]-ce[1][2])
	end
end

main()


function main()

  S = chomp(readline())
  T = chomp(readline())
  N = length(S)

  dict1 = Dict()
  dict2 = Dict()

  for i in 1:N
    dict1[S[i]] = get(dict1, S[i], 0 + i)
    dict2[T[i]] = get(dict2, T[i], 0 + i)
  end

  check1 = []
  check2 = []

  for i in 1:N
    push!(check1, dict1[S[i]])
    push!(check2, dict2[T[i]])
  end

  if check1 == check2
    println("Yes")
  else
    println("No")
  end

end

main()

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

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

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


function main()
  n, m = map(s -> parse(BigInt, s), split(readline()))
  answer = BigInt(1)
  i = BigInt(2)
  while i * i <= m
    if mod(m, i) == 0
      count = BigInt(0)
      while mod(m, i) == 0
        count += 1
        m /= i
      end
      answer *= comb(n + count - 1, count)
    end
    i += 1
  end
  if m > 1
    answer *= n
  end
  println(mod(answer, BigInt(10^9 + 7)))
end

function comb(n::Integer, k::Integer)
  if n - k < k return comb(n, n - k) end
  numer = one(n)
  denom = one(n)
  for i = 0:k - 1
    numer = numer * (n - i)
    denom = denom * (i + 1)
  end
  return div(numer, denom)
end

main()

Score : 400 points

Problem Statement

You are given positive integers N and M.

How many sequences a of length N consisting of positive integers satisfy a_1 \times a_2 \times ... \times a_N = M? Find the count modulo 10^9+7.

Here, two sequences a' and a'' are considered different when there exists some i such that a_i' \neq a_i''.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of the sequences consisting of positive integers that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

2 6

Sample Output 1

4

Four sequences satisfy the condition: \{a_1, a_2\} = \{1, 6\}, \{2, 3\}, \{3, 2\} and \{6, 1\}.

Sample Input 2

3 12

Sample Output 2

18

Sample Input 3

100000 1000000000

Sample Output 3

957870001


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

function main()
	n,x = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	a = sort(a)
	count = 0
	for i in 1:n
		if x<a[i]
			break
		else
			count += 1
			x -= a[i]
		end
	end
	if x > 0
		count-=1
	end
	println(count)
end
main()

Score : 200 points

Problem Statement

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

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.


n=parse(readline())
e=n÷2
o=n-e
println(e*o)

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


function main()
	d = split(readline(STDIN))
	xone = parse(Int64, d[1])
	yone = parse(Int64, d[2])
	xtwo = parse(Int64, d[3])
	ytwo = parse(Int64, d[4])
	xthr = xtwo + yone - ytwo
	ythr = ytwo - xone + xtwo
	xfou = xthr + xone - xtwo
	yfou = ythr + yone - ytwo
	println(xthr, " ", ythr, " ", xfou, " ", yfou, " ")
end

main()


function rotate((vx,vy))
  vy,vx
end
x1,y1,x2,y2=parse(split(readline()))
p1=(x1,y1)
p2=(x2,y2)
v12=p2.-p1
v23=rotate(v12)
p3=p2.+v23
v34=rotate(v23)
p4=p3.+v34
x3,y3=p3
x4,y4=p4
println("$x3 $y3 $x4 $y4")

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


a,b,c,d=map(x->parse(Int,x),split(readline()))
print(b+c-d," ",c+d-a," ",a+b-d," ",b+c-a)


function main()
	d = split(readline())
	x1 = parse(Int64, d[1])
	y1 = parse(Int64, d[2])
	x2 = parse(Int64, d[3])
	y2 = parse(Int64, d[4])
	x3 = x2 + y1 - y2
	y3 = y2 - x1 + x2
	x4 = x3 + x1 - x2
	y4 = y3 + y1 - y2
	println(x3, " ", y3, " ", x4, " ",y4, " ")
end

main()


function main()
	n,k = parse.(split(readline()))
	count = 0
	q = div(2*n,k)
	for i in 1:q
		for j in 1:min(i*k-1,n)
			a = j
			b = k*i-j
			if b>0 && b<=n && a%k == b%k
				x = a%k
				if k-x <= n
					count += 1
					z = div(n-k+x,k)
					count += z
				end
			end
		end
	end
	print(count)
end

main()

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


function conInt(x) convert(Int, x) end

function main()
	x = parse.(split(readline()))
	count = 0
	n = x[1]
	k = x[2]
	p = 2*n%k
	q = conInt((2*n-p)/k)
	for i in 1:q
		for j in 1:n
			a = j
			b = k*i-j
			if b>0 && b<=n && a%k == b%k
				x = a%k
				if k-x <= n
					count += 1
					y = (n-k+x)%k
					z = conInt((n-k+x-y)/k)
					count += z
				end
			end
		end
	end
	print(count)
end

main()


function main()
  x = parse.(split(readline()))
  count = 0
  for i in 1:x[1]
    for j in 1:x[1]
      for k in 1:x[1]
        if (i+j+k)%x[2] == 0
          count += 1
        end
      end
    end
  end
  print(count)
end

main()


function main()
	x = split(readline())
	n = parse(Int64, x[1])
	k = parse(Int64, x[2])
	count = 0
	for i in 1:n
		cs = 0
		for j in 1:n
			if (i+j) % k == 0
				if (i+i) % k == 0 || (j+j) % k == 0
					cs += 1
				end
			end
		end
		count += cs*cs
	end
	println(count)
end


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

function main()
	l = readline() |> parseInt
	n = 0
	while 2^n<=l
		n+=1
	end
	a = Tuple{Int,Int,Int}[]
	k = 0
	for i in 1:n-1
		push!(a,(i,i+1,2^(i-1)))
		push!(a,(i,i+1,0))
		if 2^(i-1)+2^(n-1)<=l
			push!(a,(i,n,l-(2^(i-1))))
		end
	end
	m = length(a)
	println(n," ", m)
	for i in 1:m
		println(a[i][1]," ",a[i][2]," ",a[i][3])
	end
end

main()

Score : 700 points

Problem Statement

You are given an integer L. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.

The number of vertices, N, is at most 20. The vertices are given ID numbers from 1 to N.

The number of edges, M, is at most 60. Each edge has an integer length between 0 and 10^6 (inclusive).

Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.

There are exactly L different paths from Vertex 1 to Vertex N. The lengths of these paths are all different, and they are integers between 0 and L-1.

Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.

Constraints

2 \leq L \leq 10^6

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

In the first line, print N and M, the number of the vertices and edges in your graph.
In the i-th of the following M lines, print three integers u_i,v_i and w_i, representing the starting vertex, the ending vertex and the length of the i-th edge.
If there are multiple solutions, any of them will be accepted.

Sample Input 1

4

Sample Output 1

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

In the graph represented by the sample output, there are four paths from Vertex 1 to N=8:

1 → 2 → 3 → 4 → 8 with length 0

1 → 2 → 3 → 7 → 8 with length 1

1 → 2 → 6 → 7 → 8 with length 2

1 → 5 → 6 → 7 → 8 with length 3

There are other possible solutions.

Sample Input 2

5

Sample Output 2

5 7
1 2 0
2 3 1
3 4 0
4 5 0
2 4 0
1 3 3
3 5 1


function main()
	n = parse.(split(readline()))
	a = []
	for i in 1:n[1]
		c = split(readline(), "")
		for j in 1:length(c)
			if c[j] == "#"
				push!(a, c)
				break
			end
		end
	end
	flag = [0 for i in 1:n[2]]
	for i in 1:n[2]
		for j in 1:length(a)
			if a[j][i] == "#"
				flag[i] = 1
			end
		end
	end
	for i in 1:length(a)
		for j in 1:n[2]
			if flag[j] == 1
				print(a[i][j])
			end
		end
		println()
	end
end

main()

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

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

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#


function parseInt(x) parse(Int, x) end
function main()
  n = parseInt.(split(readline()))
  x = parseInt.(split(readline()))
  y = [0 for i in 1:n[1]-n[2]+1]
  for i in 1:n[1]-n[2]+1
    z = x[i+n[2]-1]
    if x[i]*z >= 0
      y[i] = max(abs(x[i]),abs(z))
    else
      if abs(x[i]) > abs(z)
        y[i] = -(z*2)+x[i]
      else
        y[i] = z-x[i]*2
      end
    end
  end
  print(minimum(y))
end

main()

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

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

Sample Output 4

10


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

Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


function main()

  N = parse(Int,readline())

  if N < 105
    println(0)
  elseif N < 135
    println(1)
  elseif N < 165
    println(2)
  elseif N < 189
    println(3)
  elseif N < 195
    println(4)
  else
    println(5)
  end

end

main()

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

N is an integer between 1 and 200 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


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()
    n, k = parseMap(split(readline()))
    println(ifelse(n % k == 0, 0, 1))
end
main()

Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


#A - AtCoder Crackers
function main()
    N, K = parse.(split(readline()))
    if N%K == 0
        println(0)
    else
        println(1)
    end
end

main()


function parseInt(x) parse(Int, x) end

function main()
	n = parseInt(readline())
	count = 0
	flg = 0
	while count*7 <= n
		if (n-count*7)%4 == 0
			flg = 1
		end
		count += 1
	end
	if flg == 1
		print("Yes")
	else
		print("No")
	end
end

main()

Score : 200 points

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.
Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

Sample Input 1

11

Sample Output 1

Yes

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

Sample Input 2

40

Sample Output 2

Yes

If you buy ten cakes, the total will be 4 \times 10 = 40 dollars.

Sample Input 3

3

Sample Output 3

No

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.


function main()
    n = parse(Int, chomp(readline()))
    i = 0
    while true
        val = 4*i
        if val > n
            break
        end
        while val <= n
            if val == n
                println("Yes")
                return
            end
            val += 7
        end
        i += 1
    end
    println("No")
end

main()


function main()
    n = parse(Int, readline())
    i = 0
    while true
        val = 4*i
        if val > n
            break
        end
        while val <= n
            if val == n
                println("Yes")
                return
            end
            val += 7
        end
        i += 1
    end
    println("No")
end

main()


x=parse(Int,readline())
if x<1200
  println("ABC")
elseif x<2800
  println("ARC")
else
  println("AGC")
end

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


const lines=readlines(open("/dev/fd/0"))
let
    state=0
    global input
    input()=lines[state+=1]
end
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    r=int(input())
    println(r>2799 ? "AGC" : r>1199 ? "ARC" : "ABC")
end
main()


R = parse(Int,readline())
if R < 1200
    print("ABC")
elseif R < 2800
    print("ARC")
else
    print("AGC")
end


function main()
    s = readline()
    if s[1] != 'A'
        print("WA\n")
        return
    end
    cnt = 0
    for i in 3:length(s)-1
        if s[i] == 'C'
            cnt += 1
        elseif isupper(s[i])
            print("WA\n")
            return
        end
    end
    if cnt != 1
        print("WA\n")
        return
    end
    if isupper(s[2]) || isupper(s[end])
        print("WA\n")
        return
    end
    print("AC\n")
    return
end

main()

Score : 200 points

Problem Statement

You are given a string S. Each character of S is uppercase or lowercase English letter.
Determine if S satisfies all of the following conditions:

The initial character of S is an uppercase A.

There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).

All letters except the A and C mentioned above are lowercase.

Constraints

4 ≤ |S| ≤ 10 (|S| is the length of the string S.)

Each character of S is uppercase or lowercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

If S satisfies all of the conditions in the problem statement, print AC; otherwise, print WA.

Sample Input 1

AtCoder

Sample Output 1

AC

The first letter is A, the third letter is C and the remaining letters are all lowercase, so all the conditions are satisfied.

Sample Input 2

ACoder

Sample Output 2

WA

The second letter should not be C.

Sample Input 3

AcycliC

Sample Output 3

WA

The last letter should not be C, either.

Sample Input 4

AtCoCo

Sample Output 4

WA

There should not be two or more occurrences of C.

Sample Input 5

Atcoder

Sample Output 5

WA

The number of C should not be zero, either.


s = readline()

if s[1] != 'A'
    println("WA")
    quit()
end

if s[2] < 'a' || 'z' < s[2]
    println("WA")
    quit()
end

C = false
for i = 3:endof(s)-1
    if s[i] =='C'
        if C
            println("WA")
            quit()
        else
            C = true
        end
    elseif s[i] < 'a' || 'z' < s[i]
        println("WA")
        quit()
    end
end

if !C || s[end] < 'a' || 'z' < s[end]
    println("WA")
    quit()
end

println("AC")


function main()
    D, G = parse.(Int, split(readline()))
    p, c = zeros(Int, (D,)), zeros(Int, (D,))
    for i in 1:D
        p[i], c[i] = parse.(Int, split(readline()))
    end
    ans = 0
    score = 0
    for i in D:-1:1
        if score + 100*i*p[i] >= G
            print("$(ans+(G - score - 1) ÷ (100*i) + 1)\n")
            return
        elseif score + 100*i*p[i] + c[i] >= G
            print("$(ans+p[i])\n")
            return
        else
            ans += p[i]
            score += 100*i*p[i] + c[i]
        end
    end
end

main()

Score : 300 points

Problem Statement

A programming competition site AtCode provides algorithmic problems.
Each problem is allocated a score based on its difficulty.
Currently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.
These p_1 + … + p_D problems are all of the problems available on AtCode.

A user of AtCode has a value called total score.
The total score of a user is the sum of the following two elements:

Base score: the sum of the scores of all problems solved by the user.

Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D).

Takahashi, who is the new user of AtCode, has not solved any problem.
His objective is to have a total score of G or more points.
At least how many problems does he need to solve for this objective?

Constraints

1 ≤ D ≤ 10

1 ≤ p_i ≤ 100

100 ≤ c_i ≤ 10^6

100 ≤ G

All values in input are integers.

c_i and G are all multiples of 100.

It is possible to have a total score of G or more points.

Input

Input is given from Standard Input in the following format:

D G
p_1 c_1
:
p_D c_D

Output

Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).

Sample Input 1

2 700
3 500
5 800

Sample Output 1

3

In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.

One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.

Sample Input 2

2 2000
3 500
5 800

Sample Output 2

7

This case is similar to Sample Input 1, but the Takahashi's objective this time is 2000 points or more. In this case, we inevitably need to solve all five 200-point problems, and by solving two 100-point problems additionally we have the total score of 2000 points.

Sample Input 3

2 400
3 500
5 800

Sample Output 3

2

This case is again similar to Sample Input 1, but the Takahashi's objective this time is 400 points or more. In this case, we only need to solve two 200-point problems to achieve the objective.

Sample Input 4

5 25000
20 1000
40 1000
50 1000
30 1000
1 1000

Sample Output 4

66

There is only one 500-point problem, but the perfect bonus can be earned even in such a case.


Score : 400 points

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

1 ≤ i < j < k ≤ |T| (|T| is the length of T.)

T_i = A (T_i is the i-th character of T from the beginning.)

T_j = B

T_k = C

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

You are given a string S. Each character of S is A, B, C or ?.

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

This sum can be extremely large, so print the sum modulo 10^9 + 7.

Constraints

3 ≤ |S| ≤ 10^5

Each character of S is A, B, C or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

Sample Input 1

A??C

Sample Output 1

8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

AAAC: 0

AABC: 2

AACC: 0

ABAC: 1

ABBC: 2

ABCC: 2

ACAC: 0

ACBC: 1

ACCC: 0

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Sample Input 2

ABCBC

Sample Output 2

3

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

Sample Input 3

????C?????B??????A???????

Sample Output 3

979596887

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.


        function main()
          a = parse.(split(readline()))
          b = sort(a)
          l = length(b)
  		  m = 0
          sum = 0
  		if l % 2 == 0
          	m=b[convert(Int, l/2)]
          else
            m=b[convert(Int, (l+1)/2)]
          end
  		  for i in 1:l
    		sum += abs(a[i]-m)
  end
  			print(sum)
        end
        main()

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


    function main()
      a = parse.(split(readline()))
      b = sort(a)
      print(b[2])
    end

    main()


Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


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

function hpush(h::Array{Tuple{Int,Int},1},x::Tuple{Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while h[k][2]>x[2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(div(k,2),1)
			l = div(l,2)
		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 h[2*k][2]>=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k][2]>=z[2]&&h[2*k+1][2]>=z[2]
					break
				else
					t = h[2*k][2]<h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function dijkstra(e::Array{Array{Tuple{Int,Int},1},1},k)
	dis = -ones(Int, length(e))
	h = Array{Tuple{Int,Int},1}()
	push!(h,(k,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
	dis
end

function main()
	n,m,s,t = readline() |> split |> parseMap
	e = [Tuple{Int,Int}[] for i in 1:n]
	f = [Tuple{Int,Int}[] for i in 1:n]
	for i in 1:m
		u,v,a,b = readline() |> split |> parseMap
		push!(e[u],(v,a))
		push!(e[v],(u,a))
		push!(f[u],(v,b))
		push!(f[v],(u,b))
	end
	dy = dijkstra(e,s)
	ds = dijkstra(f,t)
	x = Array{Tuple{Int,Int},1}(n)
	for i in 1:n
		x[i] = (i,dy[i]+ds[i])
	end
	x = sort(x,by=x->x[2])
	(p,q) = x[1]
	for i in 0:n-1
		if p>i
			println(10^15-q)
		else
			while p<=i
				(p,q) = shift!(x)
			end
			println(10^15-q)
		end
	end
end

main()

Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994


function main()
  n = parse(readline())
  if n % 2 == 0
    print(n)
  else
    print(2*n)
  end
end

main()

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


function parseInt(x) parse(Int64, x) end

function main()
    n = readline() |> parseInt
    a = readline() |> split |> x -> parseInt.(x)
    println(maximum(a) - minimum(a))
end

main()

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


function parseInt(x) parse(Int, x) end

function main()
	n = readline() |> parseInt
	a = readline() |> split |> a -> parseInt.(a)
	b = [0 for i in 1:n]
	for i in 1:n
		b[i] = a[i] -i
	end
	m = median(b)
	sum = 0
	for i in 1:n
		sum += abs(b[i]-m)
	end
	println(convert(Int, sum))
end

main()

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


function parseInt(x) parse(Int, x) end

function main()
	n = readline() |> parseInt
	a = readline() |> split |> a -> parseInt.(a)
	b = Int[]
	for i in 1:n
		b[i] = a[i] -i
	end
	m = median(b)
	sum = 0
	for i in 1:n
		sum += abs(b[i]-m)
	end
	println(convert(Int, sum))
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)

function main()
    s=chomp(readline())
    ans=0
    for i in 1:4
        ans+=ifelse(s[i]=='+',1,-1)
    end
    println(ans)
end
main()

Score : 100 points

Problem Statement

There is always an integer in Takahashi's mind.

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

Find the integer in Takahashi's mind after he eats all the symbols.

Constraints

The length of S is 4.

Each character in S is + or -.

Input

Input is given from Standard Input in the following format:

S

Output

Print the integer in Takahashi's mind after he eats all the symbols.

Sample Input 1

+-++

Sample Output 1

2

Initially, the integer in Takahashi's mind is 0.

The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.

The second integer to eat is -. After eating it, the integer in his mind becomes 0.

The third integer to eat is +. After eating it, the integer in his mind becomes 1.

The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

Sample Input 2

-+--

Sample Output 2

-2

Sample Input 3

----

Sample Output 3

-4


s = readline() |> chomp
sn = sum(x->x-'0', s)
n = parse(s)
println((n % sn) == 0 ? "Yes" : "No")

Score : 200 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(101) = 1 + 0 + 1 = 2.

Given an integer N, determine if S(N) divides N.

Constraints

1 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

If S(N) divides N, print Yes; if it does not, print No.

Sample Input 1

12

Sample Output 1

Yes

In this input, N=12.
As S(12) = 1 + 2 = 3, S(N) divides N.

Sample Input 2

101

Sample Output 2

No

As S(101) = 1 + 0 + 1 = 2, S(N) does not divide N.

Sample Input 3

999999999

Sample Output 3

Yes


function nsn(x)
    s=dec(x)
    n=parse(s)
    sn=sum(x->x-'0',s)
    n / sn
end

function main()
    k = readline() |> parse
    for x=1:k
        n = x % 9
        if n == 0
            n = 9
        end
        keta = div(x, 10)
        println(n, repeat("9",keta))
    end
end

main()

Score : 500 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.

We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.

Given an integer K, list the K smallest Snuke numbers.

Constraints

1 \leq K

The K-th smallest Snuke number is not greater than 10^{15}.

Input

Input is given from Standard Input in the following format:

K

Output

Print K lines. The i-th line should contain the i-th smallest Snuke number.

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19


function parseInt(x) parse(Int, x) end

function main()
	n = readline() |> split |> n -> parseInt.(n)
	if n[1]<9 && n[2] < 9
		print("Yay!")
	else
		print(":(")
end

main()

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


function main()

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

  ans = 0

  for i in a
    while i % 2 == 0
      ans += 1
      i = div(i,2)
    end
  end

  println(ans)

end

main()

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.

Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.

At most how many operations can be performed?

Constraints

N is an integer between 1 and 10 \ 000 (inclusive).

a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.

Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.

Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.

Sample Input 2

4
631 577 243 199

Sample Output 2

0

No operation can be performed since all the elements are odd. Thus, the answer is 0.

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


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

function main()
    n = parseInt(readline())
    a = parseMap(split(readline()))

    ans = 0
    for i in 1:n
        while mod(a[i],2) == 0
            a[i] /= 2
            ans += 1
        end
    end

    println(ans)
end

main()


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

function bitp(x::Array{Int,1})
	k = length(x)
	for i in 1:k-1
		if x[k-i+1] > 1
			x[k-i+1] = 0
			x[k-i] += 1
		end
	end
	if x[1] > 1
		0
	else
		x
	end
end

function main()
	n,m = readline() |> split |> parseMap
	a = Array{Int}(4,n)
	for i in 1:n
		a[1:3,i] = readline() |> split |> parseMap
	end
	maxim = 0
	bt = [0,0,0]
	for i in 1:8
		for j in 1:n
			a[4,j] = zo(bt[1])*a[1,j]+zo(bt[2])*a[2,j]+zo(bt[3])*a[3,j]
		end
		a = sortcols(a,by=x->x[4],rev=true)
		maxim = max(maxim,abs(sum(a[1,1:m]))+abs(sum(a[2,1:m]))+abs(sum(a[3,1:m])))
		bt[3] += 1
		bt = bitp(bt)
	end
	println(maxim)
end

main()

Score: 400 points

Problem Statement

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

The shop sells N kinds of cakes.

Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.

These values may be zero or negative.

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

Do not have two or more pieces of the same kind of cake.

Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Constraints

N is an integer between 1 and 1 \ 000 (inclusive).

M is an integer between 0 and N (inclusive).

x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 z_1
x_2 y_2 z_2
: :
x_N y_N z_N

Output

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Sample Input 1

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

Sample Output 1

56

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 3 + 9 = 13

Tastiness: 5 + 5 + 7 = 17

Popularity: 9 + 8 + 9 = 26

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

Sample Input 2

5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15

Sample Output 2

54

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 7 + 13 = 21

Tastiness: (-2) + (-8) + (-14) = -24

Popularity: 3 + (-9) + 15 = 9

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

Sample Input 3

10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82

Sample Output 3

638

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

Sample Input 4

3 2
2000000000 -9000000000 4000000000
7000000000 -5000000000 3000000000
6000000000 -1000000000 8000000000

Sample Output 4

30000000000

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.


function main()
    a = parse(Int, readline())
    if(a > 999)
        println("ABD")
    else
        println("ABC")
    end
end

main()

Score : 100 points

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?

In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.

You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Constraints

1 \leq N \leq 1998

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Sample Input 1

999

Sample Output 1

ABC

The 999-th round of AtCoder Beginner Contest is labeled as ABC999.

Sample Input 2

1000

Sample Output 2

ABD

The 1000-th round of AtCoder Beginner Contest is labeled as ABD001.

Sample Input 3

1481

Sample Output 3

ABD

The 1481-th round of AtCoder Beginner Contest is labeled as ABD482.


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

function main()
    n=parseInt(readline())
    ans=10^5
    for i in 0:div(n,2)+1
        ans=min(ans,sumdigits(i)+sumdigits(n-i))
    end
    println(ans)
end
main()

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N.
Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".

Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

Sample Input 2

100000

Sample Output 2

10


function main()
  x = parse.(split(readline()))
  a = x[1]+x[2]
  b = x[1]-x[2]
  c = x[1] * x[2]
  print(max(a,b,c))
end

main()

Score : 100 points

Problem Statement

You are given two integers A and B.
Find the largest value among A+B, A-B and A \times B.

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest value among A+B, A-B and A \times B.

Sample Input 1

3 1

Sample Output 1

4

3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.

Sample Input 2

4 -2

Sample Output 2

6

The largest is 4 - (-2) = 6.

Sample Input 3

0 0

Sample Output 3

0


parseInt(x) = parse(Int,x)

function main()
	a,b,c,k = map(parseInt, split(readline()))
	v = [a,b,c]
	x = [0 1 1;1 0 1;1 1 0]
	x = x^k
	v = x*v
	if abs(v[1]-v[2]) >= 1000000000000000000
		print("Unfair")
	else
		print(v[1]-v[2])
	end
end
main()

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

1 \leq A,B,C \leq 10^9

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


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

function main()
	n = readline() |> parseInt
	p = Array{Int}(n)
	for i in 1:n
		p[i] = readline() |> parseInt
	end
	k = Tuple[]
	for i in 1:n
		flg = 0
		for j in 1:length(k)
			if k[j][2]+1 == p[i]
				flg = 1
				k[j] = (k[j][1]+1, p[i])
				break
			end
		end
		if flg == 0
			push!(k,(1,p[i]))
		end
	end
	m = 0
	for i in 1:length(k)
		m = max(m, k[i][1])
	end
	println(n-m)
end
main()

Score : 500 points

Problem Statement

You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N.
You would like to sort this sequence in ascending order by repeating the following operation:

Choose an element in the sequence and move it to the beginning or the end of the sequence.

Find the minimum number of operations required.
It can be proved that it is actually possible to sort the sequence using this operation.

Constraints

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

(P_1,P_2,...,P_N) is a permutation of (1,2,...,N).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1
:
P_N

Output

Print the minimum number of operations required.

Sample Input 1

4
1
3
2
4

Sample Output 1

2

For example, the sequence can be sorted in ascending order as follows:

Move 2 to the beginning. The sequence is now (2,1,3,4).

Move 1 to the beginning. The sequence is now (1,2,3,4).

Sample Input 2

6
3
2
5
1
4
6

Sample Output 2

4

Sample Input 3

8
6
3
1
2
7
4
8
5

Sample Output 3

5


a,b,c,d=map(x->parse(Int,x),split(readline()))
if abs(a-b)<=d&&abs(b-c)<=d||abs(a-c)<=d
  println("Yes")
else
  println("No")
end

Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function 素因数分解(n)
  n=Int(n)
  res=Tuple[]
  for a=2:isqrt(n)
    if n%a!=0; continue; end
    ex=0
    while n%a==0
      ex+=1
      n/=a
    end
    push!(res,(a,ex))
  end
  if n!=1; push!(res,(Int(n),1)); end
  return res
end
function main()
  x=pI(readline())
  if x<4
    println(1)
  end
  for i=x:-1:1
    k=素因数分解(i)
    if all(x->x==k[1][2],[j[2] for j=k]) && k[1][2]>1
      println(i)
      break
    end
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


N=parse(Int,readline())
println(maximum(a^b for a=2:32,b=2:9 if a^b<=N))


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] != s
		s = uf[s]
	end
	s
end

function find(uf::Array{Int,1},x,y)
	s = x
	t = y
	while uf[s] != s
		s = uf[s]
	end
	while uf[t] != t
		t = uf[t]
	end
	s==t?true:false
end

function main()
	n,m = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	uf = [i for i in 1:n]
	for i in 1:m
		x,y = readline() |> split |> parseMap
		l = root(uf,x)
		r = root(uf,y)
		uf[l] = r
	end
	for i in 1:n
		uf[i] = root(uf,uf[i])
	end
	count = 0
	for i in 1:n
		if uf[p[i]] == uf[i]
			count += 1
		end
	end
	println(count)
end
main()

Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


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

function main()
	n = readline() |> parseInt
	m = 55555
	a = zeros(Int,m)
	a[1] = 1
	k = 2
	while k <= sqrt(m)
		for i in 2*k:k:m
			a[i] = 1
		end
		k += 1
	end
	t = 0
	i = 1
	while t <= n
		if a[i] == 0 && i%5 == 1
			if t == n
				println(i)
			else
				print(i, " ")
			end
			t += 1
		end
		i += 1
	end
end

main()

Score: 400 points

Problem Statement

Print a sequence a_1, a_2, ..., a_N whose length is N that satisfies the following conditions:

a_i (1 \leq i \leq N) is a prime number at most 55 555.

The values of a_1, a_2, ..., a_N are all different.

In every choice of five different integers from a_1, a_2, ..., a_N, the sum of those integers is a composite number.

If there are multiple such sequences, printing any of them is accepted.

Notes

An integer N not less than 2 is called a prime number if it cannot be divided evenly by any integers except 1 and N, and called a composite number otherwise.

Constraints

N is an integer between 5 and 55 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print N numbers a_1, a_2, a_3, ..., a_N in a line, with spaces in between.

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

First, 3, 5, 7, 11 and 31 are all different, and all of them are prime numbers.

The only way to choose five among them is to choose all of them, whose sum is a_1+a_2+a_3+a_4+a_5=57, which is a composite number.

There are also other possible outputs, such as 2 3 5 7 13, 11 13 17 19 31 and 7 11 5 31 3.

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

2, 3, 5, 7, 11, 13 are all different prime numbers.

2+3+5+7+11=28 is a composite number.

2+3+5+7+13=30 is a composite number.

2+3+5+11+13=34 is a composite number.

2+3+7+11+13=36 is a composite number.

2+5+7+11+13=38 is a composite number.

3+5+7+11+13=39 is a composite number.

Thus, the sequence 2 3 5 7 11 13 satisfies the conditions.

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


function isAddedValue( char )
	return Base.ifelse( Base.isequal(char, 'o'), 100, 0 )
end

function abc095_a()

	const val_base = 700

	# STEP.01
	# get the target order
	buffer = Base.readline( Base.STDIN )

	# STEP.02
	# count up the number of the topping
	val_total = val_base

	for itr in buffer
		val_total += Main.isAddedValue( itr )
	end

	# STEP.03
	# output the result
	Base.println( Base.STDOUT, val_total )

	# STEP.TRUE_END
	return Base.nothing

end

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

Main.abc095_a()