function main()
	N=parse(Int,readline())
	ans=0
	T=Int[]
	for _=1:N
		a,b=map(x->parse(Int,x),split(readline()))
		ans+=a
		push!(T,a+b)
	end
	sort!(T)
	reverse!(T)
	for i=2:2:N
		ans-=T[i]
	end
	println(ans)
end
main()

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \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_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.


function f(n)
  if n % 2 == 0
    return div(n,2)
  else
    return 3n+1
  end
end

function main()

  s = parse(Int,readline())
  set = Set(Int64[])
  count = 0

  while true

    if in(s,set)
      println(count+1)
      break
    else
      push!(set,s)
      s = f(s)
      count += 1
    end

  end

end

main()

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

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114

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.


function main()

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

  ans = 0
  check = 1
  save = 0

  for i in 1:N
    if N == 1
      ans = h[1]
    elseif i < N && h[i] > h[i+1]
      ans += h[i] - save
      save = h[i+1]
    elseif i == N && h[i-1] < h[i]
      ans += h[i] - save
    end
    if h[i] == 0
      save = 0
    end
  end

  println(ans)

end

main()

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


function main()
S = readline()
target = "keyence"
index = 1
cut = 0
state = false
for s in S
  if s == target[index]
    index += 1
    state = true
  elseif s != target[index] && state == true
    state = false
  	cut += 1
  end
  if cut > 1
    println("NO")
    return 0
  end
end
  println("YES")
end
main()

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

The length of S is between 7 and 100 (inclusive).

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

Sample Input 1

keyofscience

Sample Output 1

YES

keyence is an abbreviation of key of science.

Sample Input 2

mpyszsbznf

Sample Output 2

NO

Sample Input 3

ashlfyha

Sample Output 3

NO

Sample Input 4

keyence

Sample Output 4

YES


s=chomp(readline())
for i=0:7
  if s[1:i]*s[end-i+1:end]=="keyence"
    println("YES")
    exit()
  end
end
println("NO")


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

function main()
	n = readline() |> parseInt
	h = readline() |> parseInt
	w = readline() |> parseInt
	println((n-w+1)*(n-h+1))
end

main()

Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

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

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


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

function main()
	n = readline() |> parseInt
	a,b = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	ca = 0
	cb = 0
	for i in 1:n
		if p[i] <= a
			ca += 1
		elseif p[i] <= b
			cb += 1
		end
	end
	println(min(ca,cb,n-ca-cb))
end

main()

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


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

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 bsearchg(a,x)
	l,r = 1, length(a)
	while l <= r
		m = r-div(r-l,2)
		if a[m] > x
			r = m-1
		else
			l = m+1
		end
	end
	return(l-1)
end

function main()
	n,q = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	for i in 1:q
		total = 0
		x = readline() |> parseInt
		k = bsearch(a,x)
		fl = k
		fr = k
		for r in 1:div(n-k,2)
			l = bsearchg(a[1:k],a[k+r]-x)
			if r-l == n-r || r-l == n-r-1
				fr = r
				fl = l+1
				break
			end
		end
		total += sum(a[fl:fr])
		if fr-fl+1 == n-fr-1
			if fl%2 == 1
				for j in 2:2:fl-1
					total += a[j]
				end
			else
				for j in 1:2:fl-1
					total += a[j]
				end
			end
		else
			if fl%2 == 1
				for j in 1:2:fl-2
					total += a[j]
				end
			else
				for j in 2:2:fl-2
					total += a[j]
				end
			end
		end
		println(sum(a)-total)
	end
end

main()

Score : 500 points

Problem Statement

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

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

Aoki chooses an integer x.

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

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

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

The game ends when there is no card remaining.

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

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

31
31
27
23
23

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

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

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

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


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

const INF=2<<32
function main()
    n::Int=parseInt(readline())
    data=[parseMap(split(readline())) for i in 1:n]
    dp::Array{Int,2}=zeros(Int,n,3)
    dp[1,:]=data[1]
    for i in 2:n
        for j in 1:3
            for k in 1:3
                if j==k
                    continue
                end
                dp[i,j]=max(dp[i,j],dp[i-1,k]+data[i][j])
            end
        end
    end
    println(maximum(dp[n,:]))
end
main()

Score : 100 points

Problem Statement

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

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

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

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

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

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

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

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


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

function main()
	n = readline() |> parseInt
	a = Array{Int}(3,n)
	for i in 1:n
		a[:,i] = readline() |> split |> parseMap
	end
	dp = Array{Int}(3,n)
	dp[:,1] = [a[1,1],a[2,1],a[3,1]]
	for i in 2:n
		dp[1,i] = max(dp[2,i-1],dp[3,i-1])+a[1,i]
		dp[2,i] = max(dp[1,i-1],dp[3,i-1])+a[2,i]
		dp[3,i] = max(dp[1,i-1],dp[2,i-1])+a[3,i]
	end
	println(max(dp[1,n],dp[2,n],dp[3,n]))
end

main()


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

const INF=2<<40
const maxN=110
const maxV=100010
function main()
    n::Int,w::Int=parseMap(split(readline()))
    dp::Array{Int,2}=fill(INF,maxN,maxV) # 最大価値は10^3 x 100
    dp[1]=0
    for i in 1:n
        weight::Int,value::Int=parseMap(split(readline()))
        for j in 1:maxV
            if j-value>=1
                dp[i+1,j]=min(dp[i+1,j],dp[i,j-value]+weight)
            end
            dp[i+1,j]=min(dp[i+1,j],dp[i,j])
        end
    end
    for j in maxV:-1:1
        if dp[n+1,j]<=w
            println(j-1)
            break
        end
    end
end
main()

Score : 100 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

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

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

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

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

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

Sample Output 3

17

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


N, P = map(x -> parse(Int64,x), split(readline(STDIN)))
function primes(n)
    prime_factors = Dict{Int64, Int64}()
    divisor = 2

    while divisor^2 <= n
        count = 0
        while n % divisor == 0
            count += 1
            n ÷= divisor
        end
        if count > 0
            prime_factors[divisor] = count
        end
        divisor += 1
    end

    if n > 1
        prime_factors[n] = 1
    end

    return prime_factors
end
primes_dic = primes(P)
cal_k(pair) = pair.first ^  floor(Int, pair.second / N)
answer = length(primes_dic) == 0 ? 1 : mapreduce(x->cal_k(x), *, primes_dic)
println(answer)

Score : 300 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

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

Sample Input 2

5 1

Sample Output 2

1

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

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


parseInt(x) = parse(Int, x)

function main()
	n,p=map(parseInt,split(readline()))
	d = Dict{Int,Int}()
	x = p
	i = 2
	while x > 1 || i <= sqrt(x)
		if x%i == 0
			if haskey(d,i)
				d[i] += 1
			else
				d[i] = 1
			end
			x = div(x, i)
		else
			i += 1
		end
	end
	k = 1
	for key in keys(d)
		z = div(d[key],n)
		k *= key^z
	end
	print(k)
end

main()


ParseInt(x) = parse(Int,x)
ParseMap(x::Array{SubString{String},1}) = map(ParseInt,x)
function main()
    odd_sum = 0
    n = ParseInt(readline())
    for i in 1:n
        a = ParseInt(readline())
        if a % 2 != 0
            odd_sum += 1
        end
    end
    if odd_sum % 2 == 0
        println("second")
    else
        println("first")
    end
end
main()

Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

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

Sample Input 1

2
1
2

Sample Output 1

first

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

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

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

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


n=parse(Int,readline())
s=0
m=0
for i in 1:n
  x=parse(Int,readline())
  s+=x
  if m<x
    m=x
  end
end
print(s-div(m,2))

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


parseInt(x) = parse(Int, x)

function main()
  n = parseInt(readline())
  a = Array{Int}(n)
  for i in 1:n
    a[i] = parseInt(readline())
  end
  mmm = maximum(a)
  sss = sum(a)
  print(sss-+div(mmm,2))
end

main()


parseInt(x) = parse(Int, x)
parseFloat(x) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,k=parseMap(split(readline()))
    h=sort([parseInt(readline()) for i in 1:n])
    ans=10^9+1
    for i in 1:n-k+1
        ans=min(ans,h[i+k-1]-h[i])
    end
    println(ans)
end
main()

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


parseInt(x) = parse(Int, x)

function main()
	n,k = map(parseInt, split(readline()))
	h = Array{Int}(n)
	for i in 1:n
		h[i] = parseInt(readline())
	end
	h = sort(h)
	dif = 1000000000
	for i in k:n
		dif = min(h[i]-h[i-k+1], dif)
	end
	print(dif)
end

main()


parseInt(x) = parse(Int, x)

function main()
	n,x = map(parseInt, split(readline()))
	total = 0
	while x > 0
		k=0
		while 2^(k+2)-3+(n-k) <= x
			k += 1
		end
		k-=1
		total += 2^(k+1)-1
		x -= 2^(k+2)-3+(n-k)
		if x >= 1
			x -= 1
			total += 1
		end
	end
	print(total)
end

main()

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.


x=parse(readline())

if x==3 || x==5 || x==7
println("YES")
else
println("NO")
end

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


function main()

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

  check = 10^10
  ans = 0

  for i in 1:length(S)-2

    if min(abs(check - ans),check) != check
      ans = parse(Int,join(S[i:i+2]))
    end

  end

  println(ans)

end

main()

Score : 200 points

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?

Constraints

S is a string of length between 4 and 10 (inclusive).

Each character in S is 1, 2, ..., or 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

Sample Input 2

35753

Sample Output 2

0

If 753 itself can be taken out, the answer is 0.

Sample Input 3

1111111111

Sample Output 3

642

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.


function main()

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

  sum = 0
  n = zeros(N)

  for i in 1:N

    sum += a[i]

  end

  sum = sum / N

  parse(Float64, a)

  for i in 1:N

    a[i] = abs(sum - a[i])

  end

  (c, ans) = findmin(a)

  println(ans-1)

end

main()

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.


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

function main()
	n,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	b = zeros(Int,n+1)
	for i in 1:n
		b[i+1] = b[i]+a[i]
	end
	c = Int[]
	for i in 1:n
		for j in i:n
			push!(c,b[j+1]-b[i])
		end
	end
	ans = 0
	ma = sum(a)
	r = 0
	while 2^r<ma
		r += 1
	end
	f = zeros(Int,n*(n+1)>>1)
	lf = 0
	for t in r:-1:0
		chg = Int[]
		if lf==0
			for i in 1:n*(n+1)>>1
				if c[i]&2^t==2^t
					push!(chg,i)
				end
			end
			if length(chg)>=k
				lf = 1
				ans += 2^t
				for i in 1:length(chg)
					f[chg[i]] = 1
				end
			end
		else
			z = 0
			for i in 1:n*(n+1)>>1
				if f[i]==1
					z += 1
					if c[i]&2^t!=2^t
						push!(chg,i)
					end
				end
			end
			if z-length(chg)>=k
				ans += 2^t
				for i in 1:length(chg)
					f[chg[i]] = 0
				end
			end
		end
	end
	println(ans)
end

main()

Score : 400 points

Problem Statement

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N.
He is interested in properties of the sequence a.

For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

Find the maximum possible value for him.

Constraints

2 \leq N \leq 1000

1 \leq a_i \leq 10^9

1 \leq K \leq N(N+1)/2

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the answer.

Sample Input 1

4 2
2 5 2 5

Sample Output 1

12

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}

contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}

contiguous subsequences starting from the third element: \{2\}, \{2, 5\}

contiguous subsequences starting from the fourth element: \{5\}

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12.
This can be achieved by choosing \{5, 2, 5\} (with beauty 12) and \{2, 5, 2, 5\} (with beauty 14).

Sample Input 2

8 4
9 1 8 2 7 5 6 4

Sample Output 2

32


parseInt(x) = parse(Int, x)
function main()
	x,y = map(parseInt, split(readline()))
	print(x+div(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


function index(Vec, Num)

  N = length(Vec)
  left = 1
  right = N+1
  mid = div(right + left, 2)

  while left < right
    if Vec[mid] == Num
      return mid
    elseif Vec[mid] > Num
      right = mid
      mid = div(right + left, 2)
    else
      left = mid + 1
      mid = div(right + left, 2)
    end
  end

end

function main()

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

  checks = [[] for i in 1:N]

  P = zeros(Int, M)
  Y = zeros(Int, M)
  for i in 1:M
    (P[i], Y[i]) = map(x -> parse(Int, x), split(readline()))
    push!(checks[P[i]], Y[i])
  end

  for i in 1:N
    sort!(checks[i])
  end

  for i in 1:M
    print(lpad(P[i], 6, 0))
    println(lpad(index(checks[P[i]], Y[i]), 6, 0))
  end

end

main()

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


lines=readlines()
N,M=map(x->parse(Int,x),split(shift!(lines)))
A=[Array{Pair{Int,Int}}(0) for _=1:N]
P=Array{Int}(0)
for i=1:M
	p,y=map(x->parse(Int,x),split(shift!(lines)))
	push!(A[p],y=>i)
	push!(P,p)
end
ans=Array{Int}(M)
for i=1:N
	sort!(A[i])
	for j=1:length(A[i])
		ans[A[i][j].second]=j
	end
end
function f(x)
	s=Array{Char}(0)
	while x>0
		push!(s,Char(x%10+48))
		x=div(x,10)
	end
	while length(s)<6
		push!(s,'0')
	end
	reverse!(s)
	for c=s
		print(c)
	end
end
for i=1:M
	f(P[i])
	f(ans[i])
	println()
end


parseInt(x) = parse(Int, x)
function main()
	n,m = map(parseInt, split(readline()))
	py = Array{Int}(4,m)
	for i in 1:m
		py[1:2,i] = map(parseInt, split(readline()))
		py[3,i] = i
	end
	py = sortcols(py, by = x->(x[1],x[2]))
	py[4,1] = 1
	for i in 2:m
		if py[1,i] != py[1,i-1]
			py[4,i] = 1
		else
			py[4,i] = py[4,i-1]+1
		end
	end
	py = sortcols(py, by = x->(x[3]))
	println("fuck")
end
main()


parseInt(x) = parse(Int, x)

function main()
	n, m = parseInt.(split(readline()))
	x = Array{Int}(2,m)
	for i in 1:m
	  x[:,i] = map(parseInt, split(readline()))
	end
	x = sortcols(x, by= y->y[1])
    print("fuck")
end

main()


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

function is_valid(v::Int)
    while(v > 0)
        if ((v&3) == 3) return false; end
        v ÷= 2;
    end
    return true
end
function amida(v::Int, pos::Vector{Int})
    for i = 0:length(pos)-2
        if( (v & (1 << i)) > 0)
            pos[i+1], pos[i+2] = pos[i+2], pos[i+1]
        end
    end
    return pos
end
const M = 10^9+7
g(n,A,R=A^0)=n>0 ? g(n÷2,A*A.%M,R*A^(n%2).%M) : R
function main()
    h,w,k = readline() |> split |> parsemap
    A = zeros(Int,w,w)
    for i = 0:(1 << (w-1))-1
        if (!is_valid(i)) continue end
        v = amida(i, Vector(1:w))
        for j=1:w
            @inbounds A[j,v[j]]  += 1
        end
    end
    r = g(h,A)
    println(r[1,k])
end

main()

Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

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

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

No two horizontal lines share an endpoint.

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

A horizontal line must connect adjacent vertical lines.

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

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

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

H W K

Output

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

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

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

Sample Input 6

15 8 5

Sample Output 6

437760187

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


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

function pos(i,j,w)
	if j <= 2 || j >= w+1
    	if i == j
      		w-2
    	else
      		w-3
    	end
    elseif i == j || i <= 2 || i >= w+1
		w-3
	else
		w-4
	end
end

function comb(k)
	if k == -1
		1
	elseif k == 0
		1
	else
		comb(k-1)+comb(k-2)
	end
end

function main()
	h,w,k = readline() |> split |> parseMap
	dp = Array{Int}(h+1,w+2)
	count = 0
	for i in 1:w+2
		dp[1,i] = 0
	end
	for i in 1:h+1
		dp[i,1] = 0
		dp[i,w+2] = 0
	end
    dp[1,2] = 1
	for i in 2:h+1
		for j in 2:w+1
			dp[i,j] = ((dp[i-1,j-1]*(comb(pos(j-1,j,w))))%1000000007 + (dp[i-1,j]*(comb(pos(j,j,w))))%1000000007 + (dp[i-1,j+1]*(comb(pos(j+1,j,w))))%1000000007)%1000000007
		end
	end
	println(dp[h+1,k+1]%1000000007)
end

main()


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

function main()
    a,b,k=parseMap(split(readline()))
    for i in 1:k
        if i%2==1
            a=div(a,2)
            b+=a
        else
            b=div(b,2)
            a+=b
        end
    end
    println("$a $b")
end
main()

Score : 200 points

Problem Statement

In the beginning, Takahashi has A cookies, and Aoki has B cookies.
They will perform the following operation alternately, starting from Takahashi:

If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person.

Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total.

Constraints

1 \leq A,B \leq 10^9

1 \leq K \leq 100

A,B and K are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total.

Sample Input 1

5 4 2

Sample Output 1

5 3

The process will go as follows:

In the beginning, Takahashi and Aoki have 5 and 4 cookies, respectively.

Takahashi eats one cookie and gives two cookies to Aoki. They now have 2 and 6 cookies, respectively.

Aoki gives three cookies to Takahashi. They now have 5 and 3 cookies, respectively.

Sample Input 2

3 3 3

Sample Output 2

1 3

Sample Input 3

314159265 358979323 84

Sample Output 3

448759046 224379523


A,B,K=map(x->parse(Int,x),split(readline()))
for i=1:K
  if i%2==1
    B+=A>>=1
  else
    A+=B>>=1
  end
end
println("$A $B")


function nk(n, k)
	x = n%k
	z = convert(Int, (n-x)/k)
	return(z)
end

function main()
	n = parse.(split(readline()))
	a = n[1]
	b = n[2]
	for i in 1:n[3]
		if i% 2 == 1
			if a%2 == 1
				a -=1
			end
			a = convert(Int, a/2)
			b += a
		else
			if b%2 == 1
				b -=1
			end
			b = convert(Int, b/2)
			a += b
		end
	end
	println(a, " ", b)
end

main()


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

function main()
	n = parseInt(readline())
	if n%2 == 0
		println("No")
	else
		println("Yes")
		for i in 1:conInt((n-1)/2)
			println(2, " ", i, " ", n-i)
		end
		println(2, " ", 1, " ", n)
		for i in 2:conInt((n+1)/2)
			println(2, " ", i, " ", n+2-i)
		end
	end
end

main()

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

If there are multiple such tuples, any of them will be accepted.

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


M=10^9+7
A=(x->parse(Int,x)).(split(readlines()[2]))
i=s=r=0
f=1
for a=A
	s=(s+powermod(i+=1,M-2,M))%M
	r=(r+a*(s-1)+A[end+1-i]*s)%M
	f=f*i%M
end
print(r*f%M)

Score : 600 points

Problem Statement

There are N blocks arranged in a row, numbered 1 to N from left to right.
Each block has a weight, and the weight of Block i is A_i.
Snuke will perform the following operation on these blocks N times:

Choose one block that is still not removed, and remove it.
The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself).
Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed.

There are N! possible orders in which Snuke removes the blocks.
For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs.
As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7.

Sample Input 1

2
1 2

Sample Output 1

9

First, we will consider the order "Block 1 -> Block 2".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 2, as only Block 2 remains.
Thus, the total cost of the two operations for this order is 3+2=5.

Then, we will consider the order "Block 2 -> Block 1".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 1, as only Block 1 remains.
Thus, the total cost of the two operations for this order is 3+1=4.

Therefore, the answer is 5+4=9.

Sample Input 2

4
1 1 1 1

Sample Output 2

212

Sample Input 3

10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

880971923


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


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

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])
for i in 1:N
    if ct[i][2] <= T
        return ct[i][1]
    end
end
return "TLE"
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 parseInt(x) parse(Int, x) end

nt = readline() |> split |> x -> parseInt.(x)
min = 1001
for i in 1:nt[1]
	ct = readline() |> split |> x -> parseInt.(x)
	if ct[2] <= nt[2] && ct[1] < min
		min = ct[1]
	end
end
if min >= 1001
	print("TLE")
else
	print(min)
end


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()
	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
			if q%k == 0
				print(Int(q/k))
			elseif k%2 == q%2 && k > 1
				print(2)
			else
				print(1)
			end
		end
	end
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()
	n = parse.(readline())
	ans = 0
	for i in 1:9
		x = 100*i + 10*i + i
		if n <=x
			ans = x
			break
		end
	end
	print(ans)
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


function main()
	n = parse.(split(readline(), ""))
	for i in 1:3
		if n[i] == 1
			n[i] = 9
		elseif n[i] == 9
			n[i] = 1
		end
	end
	print(n[1],n[2],n[3])
end

main()


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


function main()
	n = parse(Int64, readline())
	a = 0
	for i in 0:8
		x = 100*i + 10*i + i
		if n >x
			a = x+111
		end
	end
	print(a)
end

main()


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番目に多い数,その個数
    cntkey=keys(cnt)
    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 && v[i]!=OddMax[1]
                res1+=1
            elseif i%2==0 && v[i]!=EvenMax[3]
                res1+=1
            end
        end
        for i in 1:n
            if i%2==1 && v[i]!=OddMax[3]
                res2+=1
            elseif i%2==0 && v[i]!=EvenMax[1]
                res2+=1
            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 /\/\/\/.


N=parse(Int,readline())
V=map(x->parse(Int,x),split(readline()))
A=zeros(Int,10^5)
B=zeros(Int,10^5)
for i=1:N
	if i%2==1
		A[V[i]]+=1
	else
		B[V[i]]+=1
	end
end
AA=[(A[i],i) for i=1:10^5]
BB=[(B[i],i) for i=1:10^5]
sort!(AA)
reverse!(AA)
sort!(BB)
reverse!(BB)
ans=0
if AA[1][2]!=BB[1][2]
	ans=AA[1][1]+BB[1][1]
else
	ans=max(AA[1][1]+BB[2][1],AA[2][1]+BB[1][1])
end
println(N-ans)


N=parse(Int,readline())
V=map(x->parse(Int,x),split(readline()))
A=zeros(Int,10^5)
B=zeros(Int,10^5)
for i=1:N
	if i%2==1
		A[V[i]]+=1
	else
		B[V[i]]+=1
	end
end
AA=[(A[i],i) for i=1:10^5]
BB=[(B[i],i) for i=1:10^5]
sort!(AA)
reverse!(AA)
sort!(BB)
reverse!(BB)
ans=0
if AA[1][2]!=BB[1][2]
	ans=AA[1][1]+BB[1][1]
else
	ans=min(AA[1][1]+BB[2][1],AA[2][1]+BB[1][1])
end
println(N-ans)


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 length(ce) == 1
				print(min(n-co[1][2],n-ce[1][2]-co[2][2]))
		elseif length(co) == 1
			print(min(n-ce[1][2],n-co[1][2]-ce[2][2]))
		else
			print(min(n-co[1][2]-ce[2][2],n-co[2][2]-ce[1][2]))
		end
	else
		print(n-co[1][2]-ce[1][2])
	end
end

main()


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

function main()
	n,m = readline() |> split |> parseMap
	s = chomp(readline())
	g = [Int[] for i in 1:n]
	ac = zeros(Int,n)
	bc = zeros(Int,n)
	@inbounds for i in 1:m
		a,b = readline() |> split |> parseMap
		if s[a] == 'A'
			ac[b] += 1
		else
			bc[b] += 1
		end
		if s[b] == 'A'
			ac[a] += 1
		else
			bc[a] += 1
		end
		push!(g[a],b)
		push!(g[b],a)
	end
	stack = Int[]
	for i in 1:n
		if ac[i]==0||bc[i]==0
			push!(stack,i)
		end
	end
	l = Int[]
	vst = zeros(Int,n)
	while !isempty(stack)
		x = pop!(stack)
		if vst[x] == 0
	    	vst[x] = 1
		    push!(l,x)
    		@inbounds for i in 1:length(g[x])
	    		if s[x] == 'A'
		    		ac[g[x][i]] -= 1
			    else
    				bc[g[x][i]] -= 1
    			end
	    		if (ac[g[x][i]] == 0 || bc[g[x][i]] == 0)
		    		push!(stack,g[x][i])
    			end
	    	end
		end
	end
	println(length(l)==n?"No":"Yes")
end
main()

Score : 900 points

Problem Statement

You are given an undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.
In addition, each vertex has a label, A or B. The label of Vertex i is s_i.
Edge i bidirectionally connects vertex a_i and b_i.

The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times.
Today, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.

For example, in a graph where Vertex 1 has the label A and Vertex 2 has the label B, if Nusook travels along the path 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2, the resulting string is ABABB.

Determine if Nusook can make all strings consisting of A and B.

Constraints

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

1 \leq M \leq 2 \times 10^{5}

|s| = N

s_i is A or B.

1 \leq a_i, b_i \leq N

The given graph may NOT be simple or connected.

Input

Input is given from Standard Input in the following format:

N M
s
a_1 b_1
:
a_{M} b_{M}

Output

If Nusook can make all strings consisting of A and B, print Yes; otherwise, print No.

Sample Input 1

2 3
AB
1 1
1 2
2 2

Sample Output 1

Yes

Since Nusook can visit Vertex 1 and Vertex 2 in any way he likes, he can make all strings consisting of A and B.

Sample Input 2

4 3
ABAB
1 2
2 3
3 1

Sample Output 2

No

For example, Nusook cannot make BB.

The given graph may not be connected.

Sample Input 3

13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9

Sample Output 3

Yes

Sample Input 4

13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11

Sample Output 4

No


sread()=chomp(readline())
iread()=parse(Int,readline())
fread()=parse(Float64,readline())
sreads()=split(readline())
ireads()=parse.(Int,sreads())
freads()=parse.(float,sreads())

function main()
    h,w=ireads()
    tmp=0
    ans=[]
    for i=1:h
        a=ireads()
        r=i%2==1 ? (1:w) : (w:-1:1)
        for j=r
            a[j]+=tmp
            tmp=a[j]%2
            tmp>0 || continue
            i==h && j==r[end] && continue
            if j==r[end]
                res=join([i,j,i+1,j]," ")
            elseif i%2==1
                res=join([i,j,i,j+1]," ")
            else
                res=join([i,j,i,j-1]," ")
            end
            push!(ans,res)
        end
    end
    println(length(ans))
    println(join(ans,"\n"))
end
main()

Score : 400 points

Problem Statement

There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).

In Cell (i, j), a_{ij} coins are placed.

You can perform the following operation any number of times:

Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.

Maximize the number of cells containing an even number of coins.

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

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

Output

Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:

N
y_1 x_1 y_1' x_1'
y_2 x_2 y_2' x_2'
:
y_N x_N y_N' x_N'

That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.

In the (i+1)-th line (1 \leq i \leq N), print four integers y_i, x_i, y_i' and x_i' (1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').

Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

3
2 2 2 3
1 1 1 2
1 3 1 2

Every cell contains an even number of coins after the following sequence of operations:

Move the coin in Cell (2, 2) to Cell (2, 3).

Move the coin in Cell (1, 1) to Cell (1, 2).

Move one of the coins in Cell (1, 3) to Cell (1, 2).

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

3
1 1 1 2
1 2 2 2
3 1 3 2

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


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

function main()
    k=parseInt(readline())
    println(trunc(Int,ceil(k/2))*div(k,2))
end
main()

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


k = parse(readline())
if k % 2 == 0
	y=div(k*k,4)
else
	y=div((k+1)*(k-1),4)
end
println(y)


function main()
	x = readline(STDIN)
	if x % 2 == 0
		y=x*x/4
	else
		y=(x+1)*(x-1)/4
	end
	println(y)
end

main()


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

function main()
    x1,y1,x2,y2=parseMap(split(readline()))
    println("$(x2-(y2-y1)) $(y2+x2-x1) $(x1-(y2-y1)) $(x2-x1+y1)")
end
main()

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


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)%x[2] == 0 && (j+k)%x[2] == 0 && (k+i)%x[2] == 0
          count += 1
        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


Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


function tenchi(x)
    h=length(x)
    w=length(x[1])
    k=[[x[j][i]for j=1:h] for i=1:w]
    return k
end
function hantei(x)
  return unique(x)!=['.']
end
function main()
  h,w=parse.(split(readline()))
  a=[[j for j=chomp(readline())] for i=1:h]
  a=[i for i in a if hantei(i)]
  a=tenchi(a)
  a=[i for i in a if hantei(i)]
  a=tenchi(a)
  for i in a
    for j in i
      print(j)
    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

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


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

function main()
    h,w=parseMap(split(readline()))
    s=["" for i in 1:h]
    black_h=Vector{Int}()
    black_w=Vector{Int}()
    for i in 1:h
        s[i]=chomp(readline())
    end
    for i in 1:h
        if '#' in s[i]
            push!(black_h,i)
        end
    end
    for i in 1:w
        for j in 1:h
            if '#'==s[j][i]
                push!(black_w,i)
            end
        end
    end
    for i in 1:h
        if i in black_h
            for j in 1:w
                if j in black_w
                    print(s[i][j])
                end
            end
            print("\n")
        end
    end
end
main()


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

function main()
  n = parseInt(readline())
  x = parse.(split(readline()))
  y = sort(x)
  if n == 1
    print(y[1])
  elseif n % 2 == 0
    print(y[div(n,2)+1])
  else
    print(y[div(n,2)+2])
  end
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


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

function main()
    a,b=parseMap(split(readline()))
    println((a-1)*(b-1))
end
main()

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.


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.


function main()
  n = parse(readline())
  if n < 105
    print(0)
  elseif n < 165
    print(1)
  elseif n < 195
    print(2)
  else
    print(3)
  end
end

main()


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 main()
  s=chomp(readline())
  k=pI(readline())
  if s[1:min(length(s),k)]!="1"^k
    for i=s
      if i!='1'
        println(i)
        break
      end
    end
  else
    println(1)
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

S is a string of length between 1 and 100 (inclusive).

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2


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 main()
  s=chomp(readline())
  k=min(length(s),pI(readline()))
  if length(s)>k || s[1:k]!="1"^k
    for i=s
      if i!='1'
        println(i)
        break
      end
    end
  else
    println(1)
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


parseInt(x) = parse(Int, x)

function main()
	n, m, q = map(parseInt, split(readline()))
	for i in 1:m
		x=map(parseInt, split(readline()))
	end
	for i in 1:q
		x=map(parseInt, split(readline()))
	end
  print("fuck")
end

main()

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

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

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

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

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


function main()
    n, K = map(x -> parse(Int, x), split(readline()))
    if n % K == 0
        print("0\n")
    else
        print("1\n")
    end
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


#B - Cakes and Donuts
function main()
    N = parse(readline())
    a = 0
    ANSWER = "No"
    for i in 0:div(N,7)
        a = (11 - 7i) / 4
        if ceil(a) == floor(a)
            ANSWER = "Yes"
            break
        end
    end
    print(ANSWER)
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.


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()
    r=parseInt(readline())
    ans="AGC"
    if r<1200
        ans="ABC"
    elseif r<2800
        ans="ARC"
    end
    println(ans)
end
main()

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


function main()
  n = parse(readline())
  if n < 1200
    print("ABC")
  elseif n < 2800
    print("ARC")
  else
    print("AGC")
  end
end

main()


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


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="AC"
    if s[1]!='A'
        ans="WA"
    else
        index=0
        for i in 3:length(s)-1
            if s[i]=='C'
                if index==0
                    index=i
                else
                    ans="WA"
                end
            end
        end
        if index==0
            ans="WA"
        end
        for i in 2:length(s)
            if i!=index && 'A'<=s[i]<='Z'
                ans="WA"
            end
        end
    end
    println(ans)
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.


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

function iter(d::Int, pc::Array{Int, 2}, g::Int)
    if g <= 0 return(0) end
    ret = typemax(Int)
    for i in d:-1:1
        if pc[i,1]==0 continue end
        if g <= pc[i,1] * 100i
            ret = min(ret, ceil(Int, g / 100i))
        else
            pcNext = copy(pc)
            pcNext[i, 1] = 0
            ret = min(ret, pc[i,1] + iter(d, pcNext, g - ((100i * pc[i,1]) + pc[i,2])))
        end
    end
    return(ret)
end

function main()
    d, g = readline() |> split |> parseMap

    pc = zeros(Int, d, 2)
    for i in 1:d
        pc[i, 1], pc[i, 2] = readline() |> split |> parseMap
    end

    ans = iter(d, pc, g)
    println(ans)

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.


function main()
    """ver. 0.5.0

    ref:
        https://qiita.com/phigasui/items/2db20b36fb85e161e0ae
        https://nbviewer.jupyter.org/github/bicycle1885/Julia-Tutorial/blob/master/Julia%E3%82%AF%E3%83%83%E3%82%AF%E3%83%96%E3%83%83%E3%82%AF.ipynb
        https://nbviewer.jupyter.org/github/bicycle1885/Julia-Tutorial/blob/master/Julia%E9%AB%98%E9%80%9F%E3%83%81%E3%83%A5%E3%83%BC%E3%83%88%E3%83%AA%E3%82%A2%E3%83%AB.ipynb
    """
    const S = readline() |> chomp
    const ans = f(S)

    @assert ans == ans
    println(ans)
end

function f(S::String)
    const n = length(S)
    # Int: Int64
    const aa = zeros(Int, n)
    const cc = zeros(Int, n)
    const q = [0 for _ = 1:n]
    aa[1] = convert(Int, S[1] == 'A')
    cc[1] = convert(Int, S[1] == 'C')
    q[1]  = convert(Int, S[1] == '?')
    for i = 2:n
        aa[i] = aa[i - 1] + Int(S[i] == 'A')
        cc[i] = cc[i - 1] + Int(S[i] == 'C')
        q[i]  = q[i - 1]  + Int(S[i] == '?')
    end

    ans = 0
    const MOD = 10^9 + 7
    for i = 2:n - 1
        a = aa[i - 1]
        c = cc[n] - cc[i]
        l = q[i - 1]
        r = q[n] - q[i]
        # ABC の作り方
        # AB?: ? 1つをCに割り当てる
        # ?BC: ? 1つをAに割り当てる
        # ?B?: ? 2つをACに割り当てる
        if (S[i] == 'B' || S[i] == '?')
            ac = a * c * (3^(l + r))
            aq = (l + r - 1) < 0? 0: a * r * (3^(l + r - 1))
            qc = (l + r - 1) < 0? 0: l * c * (3^(l + r - 1))
            qq = (l + r - 2) < 0? 0: l * r * (3^(l + r - 2))
            # println(a, c, l, r, (ac, aq, qc, qq))
            ans += ac + aq + qc + qq
            ans = ans % MOD
        end
    end

    return ans
end

main()

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.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
a1,a2,a3=parseMap(split(readline()))
f(a,b,c)=min(abs(a-b)+abs(b-c),abs(a-c)+abs(c-b))
ans=min(f(a1,a2,a3),f(a2,a1,a3),f(a3,a1,a2))
print(ans)

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 = map(x -> parse(Int,x),split(readline()))

  sort!(A)

  println(abs(A[1]-A[2])+abs(A[2]-A[3]))

end

main()


    function main()
      a = parse.(split(readline()))
      b = sort(a)
      l = length(b)
      if l % 2 == 0
      	print(b[convert(Int, l/2]))
      else
        print(b[convert((l+1)/2))])
      end
    end
    main()


function main()
	s = chomp(readline())
	t = chomp(readline())
	n = length(s)
	flg = 0
	for i in 1:n
		if s == t
			flg = 1
		end
		s = string(s[2:n],s[1])
	end
	if flg == 1
		print("Yes")
	else
		print("No")
	end
end

main()

Score : 200 points

Problem Statement

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

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}.

Here, |X| denotes the length of the string X.

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


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)

function main()
	n = parseInt(readline())
	a = map(parseInt, split(readline()))
	sum = 0
	for i in 1:n
		sum += a[i]-1
	end
	print(sum)
end

main()


parseInt(x) = parse(Int, x)

function main()
	n = readline() |> x -> parseInt(x)
	a = readline() |> split |> x -> parseInt.(x)
	count = 0
	streak = 1
	for i in 2:n
		if a[i] == a[i-1]
			streak += 1
		else
			if streak >= 2
				count += div(streak, 2)
			end
			streak = 1
		end
	end
	if streak >= 2
		count += div(streak, 2)
	end
	print(count)
end

main()

Score : 200 points

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i.
If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

Takahashi can change the color of one slime to any of the 10000 colors by one spell.
How many spells are required so that no slimes will start to combine themselves?

Constraints

2 \leq N \leq 100

1 \leq a_i \leq N

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 number of spells required.

Sample Input 1

5
1 1 2 2 2

Sample Output 1

2

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.

Sample Input 2

3
1 2 1

Sample Output 2

0

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.

Sample Input 3

5
1 1 1 1 1

Sample Output 3

2

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.

Sample Input 4

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

Sample Output 4

4


parseInt(x) = parse(Int, x)

function main()
    a,b = parseInt.(readline() |> split)
    if a + b == 15
        println("+")
    elseif a * b == 15
        println("*")
    else
        println("x")
    end
end

main()

Score : 100 points

Problem Statement

You are given two integers a and b.
Determine if a+b=15 or a\times b=15 or neither holds.

Note that a+b=15 and a\times b=15 do not hold at the same time.

Constraints

1 \leq a,b \leq 15

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If a+b=15, print +;
if a\times b=15, print *;
if neither holds, print x.

Sample Input 1

4 11

Sample Output 1

+

4+11=15.

Sample Input 2

3 5

Sample Output 2

*

3\times 5=15.

Sample Input 3

1 1

Sample Output 3

x

1+1=2 and 1\times 1=1, neither of which is 15.