N=gets.to_i
s1=gets.chomp
s2=gets.chomp
MOD=10**9+7

i = 0
arr = []
while i < N
    if s1[i] == s2[i]
        arr << 1
        i += 1
    else
        arr << 2
        i += 2
    end
end

ret = (arr[0] == 1 ? 3 : 6)
arr.each_cons(2) do |ab|
    case ab
    in [1,1]
        ret *= 2
    in [1,2]
        ret *= 2
    in [2,1]
        ret *= 1
    in [2,2]
        ret *= 3
    end
end

p ret%MOD

Score : 400 points

Problem Statement

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

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

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

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

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

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

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

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

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

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

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902


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

Submitted source-code file size, in bytes.

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

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


s=gets;puts {true=>"Yes",false=>"No}[s[0] == s[2]]

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

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

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

575

Sample Output 1

Yes

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

Sample Input 2

123

Sample Output 2

No

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

Sample Input 3

812

Sample Output 3

No


Score : 200 points

Problem Statement

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

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

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

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

Constraints

0≤A<B≤100

0≤C<D≤100

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

0 75 25 100

Sample Output 1

50

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

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

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

Sample Input 2

0 33 66 99

Sample Output 2

0

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

Sample Input 3

10 90 20 80

Sample Output 3

60


N = gets.to_i
R = Hash.new {|h, k| h[k] = {}}
(N - 1).times do
  a, b, c = gets.split.map(&:to_i)
  R[a][b] = c
  R[b][a] = c
end

Q, K = gets.split.map(&:to_i)
D = []; D[K] = 0
U = []; U[K] = true

que = [K]
while x = que.shift
  R[x].each do |node, cost|
    next if U[node]
    D[node] = cost + D[x]
    U[node] = true
    que << node
  end
end

Q.times do
  x, y = gets.split.map(&:to_i)
  puts D[x] + D[y]
end

Score : 400 points

Problem Statement

You are given a tree with N vertices.

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

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

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

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

Constraints

3≤N≤10^5

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

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

The given graph is a tree.

1≤Q≤10^5

1≤K≤N

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

x_j≠y_j (1≤j≤Q)

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

Input

Input is given from Standard Input in the following format:

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

Output

Print the responses to the queries in Q lines.

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

Sample Input 1

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

Sample Output 1

3
2
4

The shortest paths for the three queries are as follows:

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

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

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

Sample Input 2

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

Sample Output 2

5
14
22

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

Sample Input 3

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

Sample Output 3

17000000000


n, m = gets.chomp.split(" ").map(&:to_i)
puts (n-1)*(m-1)

Score : 100 points

Problem Statement

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

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


m=gets.to_i
n=gets.to_i
l=(m-1)*(n-1)
puts l


p gets.split.map{|s| s.to_i - 1}.reduce(:*)


s = gets.chomp.split("")

puts ("s.first + #{s.length - 2} + s.last")

Score : 200 points

Problem Statement

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

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

Constraints

3 ≤ |s| ≤ 100 (|s| denotes the length of s.)

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the abbreviation of s.

Sample Input 1

internationalization

Sample Output 1

i18n

Sample Input 2

smiles

Sample Output 2

s4s

Sample Input 3

xyz

Sample Output 3

x1z


s = gets.chomp.split('')
puts s[0] + s[1...-1].size.to_s + s[-1]


n = gets.to_i
a_numbers = gets.split.map(&:to_i)

multiples_of_four = a_numbers.count { |number| number % 4 == 0 }
multiples_of_two_not_four = a_numbers.count(&:even?) - multiples_of_four
rest = n - (multiples_of_four + multiples_of_two_not_four)

if multiples_of_two_not_four.zero?
  puts multiples_of_four + 1 >= rest ? 'Yes' : 'No'
else
  puts multiples_of_four + 1 >= rest + 1 ? 'Yes' : 'No'
end

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


n=gets.to_i
a=gets.split.map(&:to_i)
c4,c1=0,0
f=true
a.each do |b|
  if b%4==0
    c4+=1
  elsif b.odd?
    c1+=1
  elsif f
    c1+=1
    f=false
  end
end
puts c4>c1-2 ? :Yes : :No


require 'prime'
require 'set'
require 'tsort'
include Math
ALP = ('a'..'z').to_a
INF = 0xffffffffffffffff
def max(a,b);              a > b ? a : b                              end
def min(a,b);              a < b ?  a : b                             end
def swap(a,b);             a, b = b, a                                end
def gif;                   gets.to_i                                  end
def gff;                   gets.to_f                                  end
def gsf;                   gets.chomp                                 end
def gi;                    gets.split.map(&:to_i)                     end
def gf;                    gets.split.map(&:to_f)                     end
def gs;                    gets.chomp.split.map(&:to_s)               end
def gc;                    gets.chomp.split('')                       end
def pr(num);               num.prime_division                         end
def pr?(num);              Prime.prime?(num)                          end
def digit(num);            num.to_s.length                            end
def array(s,ini=nil);      Array.new(s){ini}                          end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}}          end
def rep(num);              num.times{|i|yield(i)}                     end
def repl(st,en,n=1);       st.step(en,n){|i|yield(i)}                 end

n = gif
a = gi
size = a.size
num4 = a.select{|aa| aa % 4 == 0}.size
num  = a.select{|aa| aa % 2 != 0}.size
num2 = a.select{|aa| aa % 2 == 0}.size
ans = num4 - num
puts ans >= 1 && num2 % 2 == 0 ? "Yes" : "No"


Score : 100 points

Problem Statement

This contest, AtCoder Beginner Contest, is abbreviated as ABC.

When we refer to a specific round of ABC, a three-digit number is appended after ABC. For example, ABC680 is the 680th round of ABC.

What is the abbreviation for the N-th round of ABC? Write a program to output the answer.

Constraints

100 ≤ N ≤ 999

Input

Input is given from Standard Input in the following format:

N

Output

Print the abbreviation for the N-th round of ABC.

Sample Input 1

100

Sample Output 1

ABC100

The 100th round of ABC is ABC100.

Sample Input 2

425

Sample Output 2

ABC425

Sample Input 3

999

Sample Output 3

ABC999


n = gets.chomp.to_i
all = 0
if n % 2 == 1
    all += 1
end
count = 0
if n % 2 == 0
    while n % 2 == 0
    n = n / 2
    count += 1
    end
end
if all <= 0
puts count
else
    puts "0"
end

Score : 200 points

Problem Statement

Takahashi loves numbers divisible by 2.

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

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

For example,

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

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

3 can be divided by 2 zero times.

Constraints

1 ≤ N ≤ 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

7

Sample Output 1

4

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

Sample Input 2

32

Sample Output 2

32

Sample Input 3

1

Sample Output 3

1

Sample Input 4

100

Sample Output 4

64


n = gets.to_i
max = 0
array = []

(1..n).each do |num|
  cnt = 0
  while true do
    if num % 2 == 0
      cnt += 1
      num /= 0
    else
      break
    end
  end
  array.push(cnt)
end
puts 2** array.max


n = gets.to_i
i=1
while i<n
  i*=2
end
puts i


n = gets.to_i
x = 1
while x <= n
  x *= 2
end
puts x / 2


N,M = gets.split.map(&:to_i)
from1 = []
toN = []
M.times{
  a,b = gets.split.map(&:to_i)
  from1 << b if a == 1
  toN << a if b == N
}

puts (from1&toN).size > 0 ? "POSSIBLE":"IMPOSSIBLE"

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

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

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.


N,K,*A=(dd).split.map &:to_i;puts"#{:IM if K%A.reduce(:gcd)>0||K>A.max}POSSIBLE"

Score : 300 points

Problem Statement

There is a box containing N balls. The i-th ball has the integer A_i written on it.
Snuke can perform the following operation any number of times:

Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq K \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.

Sample Input 1

3 7
9 3 4

Sample Output 1

POSSIBLE

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9-4)=5.
Next, take out 3 and 5, and return them back along with abs(3-5)=2.
Finally, take out 9 and 2, and return them back along with abs(9-2)=7.
Now we have 7 in the box, and the answer is therefore POSSIBLE.

Sample Input 2

3 5
6 9 3

Sample Output 2

IMPOSSIBLE

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.

Sample Input 3

4 11
11 3 7 15

Sample Output 3

POSSIBLE

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.

Sample Input 4

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE


A, B = gets.split.map(&:to_i)
if A % 3 == 0 || B % 3 == 0 || (A + B) % 3 == 0
  puts 'Possible'
else
  puts 'Impossible'
end

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

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

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

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

4 5

Sample Output 1

Possible

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

Sample Input 2

1 1

Sample Output 2

Impossible

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


str = gets().split(" ")
a = str[0].to_i
b = str[1].to_i

if(a%3==0)
	puts("Possible\n")
elsif(b%3==0)
	puts("Possible\n")
elsif((a+b)%3==0)
	puts("Possible\n")
else
	puts("Impossible\n")
end


n, k = STDIN.gets.split(' ').map(&:to_i)
sticks = STDIN.gets.split(' ').map(&:to_i).sort{|a, b| b <=> a }

puts sticks.take(k).reduce(&:+)

Score : 200 points

Problem Statement

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

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

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

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

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

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

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

Sample Input 2

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

Sample Output 2

386


n,k = gets.split.map(&:to_i)
len = gets.chomp.split.map(&:to_i)
max = len[0]
index = 0
ans = 0
k.times do |i|
    n.times do |j|
        if max<len[j]
            max=len[j]
            index=j
        end
    end
    ans+=max
    max=len[0]
    len[index]=0
end
puts ans


n = gets.to_i
a = gets.split(' ').map{ |a| a.to_i }
sunuke = 0
arai = a.inject(:+)
ans = Array.new(n, 10 ** 9)

n.times do |i|
  v = a.shift
  break if a.empty?
  sunuke += v
  arai -= v
  ans[i] = (sunuke - arai).abs
end

puts ans.min

Score : 300 points

Problem Statement

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

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

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

Constraints

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

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

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

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

Sample Input 2

2
10 -10

Sample Output 2

20

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


n = gets
arr = gets.chomp.split(' ').map(&:to_i)
if arr.length==2
	a = (arr.first-arr.last).abs
else
	last = arr.length-1
	a = 0
	b = 0
	arr.each_with_index do |n,i|
		left = 0
		right = 0
		for h in 0..i
			left += arr[h]
		end
		for h in (i+1)..last
			right += arr[h]
		end
		a = (left-right).abs and next if i==0
		b = (left-right).abs
		if a>b
			a = b
		else
			break if arr.any?.with_index(i+1){|n| n<0}
		end
	end
end
puts a


def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end
def inpf() a=gets.chomp.split(" ").map(&:to_f)end
def inps() a=gets.chomp.split(" ")end
def copy(a) Marshal.load(Marshal.dump(a)) end
def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end
def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end
def na(n,d=0) Array.new(n,d)end
def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end
def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end
def bit(n) n.to_s(2).split("").map(&:to_i) end
def inp() a=gets.chomp.split(" ").map(&:to_i)end
n = inp[0]
tab = na2(n+1,0)
lis = na(n+1,0)
(n-1).times do
  dat = inp
  tab[dat[0]].push(dat[1])
  tab[dat[1]].push(dat[0])
end
fn = 1
ff = 1
lis[1] = 1
lis[n] = 1
que1 = []
que2 = []
tab[1].each do |d|
  que1.push(d)
end
tab[n].each do |d|
  que2.push(d)
end
while (que1.size > 0 and que2.size > 0)
  if(que1.size > 0)
    now = que1.shift
    if(lis[now] != 1)
      fn += 1
      lis[now] = 1
      tab[now].each do |d|
        que1.push(d)
      end
    end
  else
  end
  if(que2.size > 0)
    now = que2.shift
    if(lis[now] != 1)
      lis[now] = 1
      ff += 1
      tab[now].each do |d|
        que2.push(d)
      end
    end
  end
end
puts (fn > n/2)? "Fennec" : "Snuke"

Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored.
Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.
More specifically, each player performs the following action in her/his turn:

Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.

Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

2 \leq N \leq 10^5

1 \leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

Sample Input 1

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

Sample Output 1

Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke


s = gets.chomp
(s.length - 2).step(2, -2) do |i|
    if (0..(i / 2 - 1)).all? { |j| s[j] == s[i/2+j] }
        puts i
        exit
    end
end

Score : 200 points

Problem Statement

We will call a string that can be obtained by concatenating two equal strings an even string.
For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.

You are given an even string S consisting of lowercase English letters.
Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S.
It is guaranteed that such a non-empty string exists for a given input.

Constraints

2 \leq |S| \leq 200

S is an even string consisting of lowercase English letters.

There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest even string that can be obtained.

Sample Input 1

abaababaab

Sample Output 1

6

abaababaab itself is even, but we need to delete at least one character.

abaababaa is not even.

abaababa is not even.

abaabab is not even.

abaaba is even. Thus, we should print its length, 6.

Sample Input 2

xxxx

Sample Output 2

2

xxx is not even.

xx is even.

Sample Input 3

abcabcabcabc

Sample Output 3

6

The longest even string that can be obtained is abcabc, whose length is 6.

Sample Input 4

akasakaakasakasakaakas

Sample Output 4

14

The longest even string that can be obtained is akasakaakasaka, whose length is 14.


lines = $stdin.read
array = lines.split("\n")

n = array[0].to_i
a = array[1].split(" ").map(&:to_i)
b = []

for i in 0...(a.length)

  if n % 2 == 0
    if i % 2 == 0
      b.push a[i]
    else
      b.unshift a[i]
    end
  else
    if i % 2 == 1
      b.push a[i]
    else
      b.unshift a[i]
    end
  end

  #b.push(a[i])
  #b.reverse!
end

puts b.join(" ")

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


X, A, B = gets.split.map(&:to_i)

if A-B >= 0
	print "delicious"
elsif A+X >= B
	print "safe"
else
	print "dangerous"
end

Score : 100 points

Problem Statement

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

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

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

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

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

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

Sample Input 1

4 3 6

Sample Output 1

safe

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

Sample Input 2

6 5 1

Sample Output 2

delicious

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

Sample Input 3

3 7 12

Sample Output 3

dangerous

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


n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
ans = a[n-1] - a[0]
puts ans

Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


N = gets.to_i
scores = gets.split.map(&:to_i)

# 灰, 茶, 緑, 水, 青, 黄, 橙, 赤, 自由
color = Array.new(9, 0)

scores.each do |i|
	if i < 400
		color[0] += 1
	elsif i < 800
		color[1] += 1
	elsif i < 1200
		color[2] += 1
	elsif i < 1600
		color[3] += 1
	elsif i < 2000
		color[4] += 1
	elsif i < 2400
		color[5] += 1
	elsif i < 2800
		color[6] += 1
	elsif i < 3200
		color[7] += 1
	else
		color[8] += 1
	end
end

ans_min = color[0..7].count {|i| 0 < i}

if 8-ans_min <= color[8]
	ans_max = 8
else
	ans_max = ans_min + color[8]
end

p "#{ans_min} #{ans_max}"

Score : 300 points

Problem Statement

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

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

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

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

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

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

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

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

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

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

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

Sample Input 3

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

Sample Output 3

1 1

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


n = gets.to_i
a = gets.split.map{|m|m.to_i}
temp_color = ["hai","cha","midori","mizu","ao","ki","daidai","aka"]
color = Array.new
extra_color = Array.new
a.each do |rate|
    if rate < 400
        color.push("hai") if !(color.include?("hai"))
    elsif rate < 800
        color.push("cha") if !(color.include?("cha"))
    elsif rate < 1200
        color.push("midori") if !(color.include?("midori"))
    elsif rate < 1600
        color.push("mizu") if !(color.include?("mizu"))
    elsif rate < 2000
        color.push("ao") if !(color.include?("ao"))
    elsif rate < 2400
        color.push("ki") if !(color.include?("ki"))
    elsif rate < 2800
        color.push("daidai") if !(color.include?("daidai"))
    elsif rate < 3200
        color.push("aka") if !(color.include?("aka"))
    else
        if color.length+extra_color.length != temp_color.length
            temp_color.each do |c|
                if !(extra_color.include?(c))
                    extra_color.push(c)
                    break
                end
            end
        end
    end
end
color.push("aka") if color.length == 0
print color.length.to_s+" "+(color.length+extra_color.length).to_s+"\n"


n = gets.to_i
h = gets.split.map(&:to_i)

count = 0

if h.any?{|x| 1<=x && x<=399}
  count += 1
end
if h.any?{|x| 400<=x && x<=799}
  count += 1
end
if h.any?{|x| 800<=x && x<=1199}
  count += 1
end
if h.any?{|x| 1200<=x && x<=1599}
  count += 1
end
if h.any?{|x| 1600<=x && x<=1999}
  count += 1
end
if h.any?{|x| 2000<=x && x<=2399}
  count += 1
end
if h.any?{|x| 2400<=x && x<=2799}
  count += 1
end
if h.any?{|x| 2800<=x && x<=3199}
  count += 1
end

count_stop = h.count{|x| x>=3200}

if n > 0
  min = 1
end

if count > 1
  min = count
end

max = count + count_stop

puts min.to_s + " " + max.to_s


N = $stdin.gets.chomp.to_i
S = $stdin.gets.chomp

def t(s)
  $stderr.puts s
  balance = 0
  cnt = 0
  s.split("").each{ |a|
    case a
    when '('; balance += 1
    when ')';
      balance -= 1
    end
    if balance < 0
      cnt += 1
      balance = 0
    end
    $stderr.puts "#{a},#{balance},#{cnt}"
  }
  cnt
end

open = t(S)
close = t(S.reverse.tr("()",")("))
puts "#{'('*open}#{S}#{')'*close}"

Score : 400 points

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:

() is a correct bracket sequence.

If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.

If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.

Every correct bracket sequence can be derived from the rules above.

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

Constraints

The length of S is N.

1 ≤ N ≤ 100

S consists of ( and ).

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

Sample Input 1

3
())

Sample Output 1

(())

Sample Input 2

6
)))())

Sample Output 2

(((()))())

Sample Input 3

8
))))((((

Sample Output 3

(((())))(((())))


s=gets.chomp.split("")
u= s.uniq
if s == u then
	puts "yes"
else
	puts "no"
end

Score : 200 points

Problem Statement

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

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


s = gets.chomp
puts s.size == s.split("").uniq.size ? "yes" : "no"


N=gets.strip.to_i
s = readlines.map {|x| x.strip.to_i }.sort

sum = s.reduce(:+)

if sum%10!=0
  p sum
  exit
end

without_10_times_s = s.reject {|x| x%10==0 }
smallest_no_10_times = without_10_times_s.first

if smallest_no_10_times.nil?
  p 0
  exit
end

result = sum - smallest_no_10_times
p result

Score : 300 points

Problem Statement

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

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

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
5
10
15

Sample Output 1

25

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

Sample Input 2

3
10
10
15

Sample Output 2

35

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

Sample Input 3

3
10
20
30

Sample Output 3

0

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


n = gets.to_i
as = []
min = Float::INFINITY
n.times do
  a = gets.to_i
  if a % 10 != 0
    min = [min, a].min
  end
  as << a
end
sum = as.reduce(0){|a,e|a+=e}

if sum % 10 == 0 && min != Float::INFINITY
  puts sum - min
elsif sum % 10 == 0
  puts 0
else
  puts sum
end


N = gets.to_i
s = N.times.map{ gets.to_i }
a = []
[3,N].min.times{|i| s.combination(N-i).map{|t| a<<t.inject(:+)} }
p a
puts a.select{ |tn| tn%10!=0 }.max.to_i


gets
points = gets(nil).split.map(&:to_i)
max = points.inject(:+)

if max % 10 != 0
  puts max
else
  x = points.select{|x| x % 10 != 0}.sort
  if x == []
    puts 0
  else
    puts max - x[0]
  end
end


N = gets.to_i
S = N.times.map{gets.to_i}.sort

sum = S.inject(0,:+)

if sum % 10 == 0
  sum -= S.find{|v|v%10!=0}||0
end
if sum % 10 == 0
  p 0
else
  p sum
end


n, a, b=gets.strip.split.map(&:to_i)
h = readlines.map {|line| line.strip.to_i }
c=a-b

ok=(h.max/b.to_f).ceil
ng=1
while ok-ng > 1
#p [ok,ng]
  mid = (ok+ng)/2
  remains = h.map {|e| e-b*mid }.select {|e| e>0 }
  if remains.length.zero?
    ok=mid
    next
  end
  if mid >= remains.map {|e| (e/c.to_f).ceil }.inject(:+)
    ok=mid
  else
    ng=mid
  end
end

p ok

Score : 400 points

Problem Statement

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

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

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

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

Constraints

All input values are integers.

1 ≤ N ≤ 10^5

1 ≤ B < A ≤ 10^9

1 ≤ h_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N A B
h_1
h_2
:
h_N

Output

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

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

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

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

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

Sample Input 2

2 10 4
20
20

Sample Output 2

4

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

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000


class BIT
  attr_reader :bit
  def initialize(n)
    @n = n
    @bit = Array.new(@n + 1, 0)
  end
  def sum(i)
    s = 0
    while i > 0
      s +=  @bit[i]
      i -= i & -i
    end
    return s
  end
  def add(i, x)
    i += 1
    while i <= @n
      @bit[i] += x
      i += i & -i
    end
  end
end

n, k = gets.chomp.split(" ").map(&:to_i)
a = [0]
a += readlines.map{|x| x.chomp.to_i - k}

b = [0]
n.times do |i|
  b << b[i] + a[i+1]
end

b_s = b.sort
c = b.map{|e| b_s.bsearch_index{|x| x >= e}}

bit = BIT.new(n+1)

ans = 0
c.each do |i|
  ans += bit.sum(i+1)
  bit.add(i, 1)
end
puts ans

Score : 600 points

Problem Statement

You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K.

a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

All input values are integers.

1 ≤ N ≤ 2 \times 10^5

1 ≤ K ≤ 10^9

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N K
a_1
a_2
:
a_N

Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1

3 6
7
5
7

Sample Output 1

5

All the non-empty contiguous subsequences of a are listed below:

{a_1} = {7}

{a_1, a_2} = {7, 5}

{a_1, a_2, a_3} = {7, 5, 7}

{a_2} = {5}

{a_2, a_3} = {5, 7}

{a_3} = {7}

Their means are 7, 6, 19/3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a_1} and {a_3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2

1 2
1

Sample Output 2

0

Sample Input 3

7 26
10
20
30
40
30
20
10

Sample Output 3

13


x, y = gets.split.map(&:to_i)
ary = [4, 6, 9, 11]
ary2 = [1, 3, 5, 7, 8, 10, 12]

if ary.include?(x) && ary.include?(y)
  puts "Yes"
elsif ary2.include?(x) && ary2.include?(y)
  puts "Yes"
else
  puts "No"
end

Score : 100 points

Problem Statement

Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below.
Given two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group.

Constraints

x and y are integers.

1 ≤ x < y ≤ 12

Input

Input is given from Standard Input in the following format:

x y

Output

If x and y belong to the same group, print Yes; otherwise, print No.

Sample Input 1

1 3

Sample Output 1

Yes

Sample Input 2

2 4

Sample Output 2

No


require 'date'
def f(a)
  a += 1
  a = 1 if a > 12
  (Date.new(0, a, 1)-1).day
end
a, b = gets.split.map &:to_i
puts (f(a)==f(b) ? "Yes" : "No")


x, y = gets.split.map(&:to_i)

if [1, 3, 5, 7, 8, 10, 12].find {|n| n == x} != nil
  if [1, 3, 5, 7, 8, 10, 12].find {|n| n == y} != nil
    puts 'Yes'
  else
    puts 'No'
  end
elsif [4, 6, 9, 11].find {|n| n == x} != nil
  if [4, 6, 9, 11].find {|n| n == y} != nil
    puts 'Yes'
  elsif
    puts 'No'
  end
else
  puts 'No'
end


h, w = gets.split.map(&:to_i)
a = h.times.map { gets.chomp }
puts "#" * (w + 2)
a.each { |s| puts "#" + s + "#" }
puts "#" * (w + 2)

Score : 200 points

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.
Each pixel is represented by a lowercase English letter.
The pixel at the i-th row from the top and j-th column from the left is a_{ij}.

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

Sample Input 1

2 3
abc
arc

Sample Output 1

#####
#abc#
#arc#
#####

Sample Input 2

1 1
z

Sample Output 2

###
#z#
###


h,w=gets.split.map(&:to_i)
ans=Array.new(6)
if h%3==0 || w%3==0
	answer=0
else
	k=h#とする
	mul=h*w
	trip=mul/3 #6
	for i in 0..2
		green=(trip/h+i)*h

		blue=(w-(trip/h+i))*(h/2)

		red=mul-green-blue
		if green==0 || blue==0 || red==0
			next
		end
		num= (green-blue).abs > (green-red).abs ? blue : red

		ans[i]=(green-num).abs
	end
	for i in 0..2
		green=(trip/w+i)*w

		blue=(h-(trip/w+i))*(w/2)

		red=mul-green-blue
		if green==0 || blue==0 || red==0
			next
		end
		num= (green-blue).abs > (green-red).abs ? blue : red
		ans[3+i]=(green-num).abs
	end
	ans.compact!
	answer=ans.min
end

puts answer

Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

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

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000


io = STDIN
a,b,c=io.gets.chomp.split.map(&:to_i)
puts (c>=a)&&(c<=b) ? 'Yes' : 'No'

Score : 100 points

Problem Statement

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

Constraints

-100≤A,B,C≤100

A, B and C are all integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

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

Sample Input 1

1 3 2

Sample Output 1

Yes

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

Sample Input 2

6 5 4

Sample Output 2

No

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

Sample Input 3

2 2 2

Sample Output 3

Yes


inf = 99

n,k = gets.to_s.split.map{|t|t.to_i}

ary = []
n.times do

  a, b = gets.to_s.split
  a, b = a.to_i, b.to_i

  ary.concat([a]*b)

end

puts ary[k-1]

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


n,k=gets.split.map &:to_i
ar=(1..n).map{gets.split.map &:to_i}
p ar.sort.find{|a,b|k-=b;k<=0}[0]


n, m = gets.strip.split.map(&:to_i)
abc = readlines.map {|line| line.strip.split.map(&:to_i) }

graph = Array.new(n+1){ [] }
r_graph = Array.new(n+1){ [] }
edges = []
abc.each do |a, b, c|
  edges << [a,b,-c]
  graph[a] << [b, -c]
  r_graph[b] << [a, -c]
end

def reachable(s, g, graph)
  result = []
  q = [s]
  result = [s]
  until q.empty?
    c=q.shift
    graph[c].each do |nex, _|
      next if result.include?(nex)
      q << nex
      result << nex
    end
  end
  result
end

from_1 = reachable(1, n, graph)
from_n = reachable(n, 1, r_graph)
available = from_1 & from_n

INF=10**14
dist = [INF]*(n+1)
dist[1]=0
updated=false
(n).times do
  updated=false

  edges.each do |from, to, cost|
    next if !available.include?(from) || !available.include?(to)
    if dist[from] != INF && dist[to] > dist[from] + cost
      dist[to] = dist[from] + cost
      updated = true
    end
  end
end

if updated
  puts 'inf'
  exit
end

#p [from_1, from_n, available]
p dist[n]*-1

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

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

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

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

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000


N, M = gets.split.map &:to_i
A = $<.map {|l| l.split.map{|x| x.to_i }}

d = [-1e100] * N
d[0] = 0

M.times do
  A.each do |a,b,c|
    d[b-1] = [d[b-1], d[a-1]+c].max
  end
end

e = d[N-1]

M.times do
  A.each do |a,b,c|
    d[b-1] = [d[b-1], d[a-1]+c].max
  end
end

puts d[N-1] == e ? d[N-1] : 'inf'


n, m = gets.split.map(&:to_i)
array = Array.new(n){ Array.new(n){ nil } }
points = Array.new(n){ Array.new(n){ nil } }
m.times do
  a, b, c = gets.split.map(&:to_i)
  array[a-1][b-1] = c
end

def dfs(row, array, points, point)
  if array[row].all?(&:nil?)
    max = nil
    points.size.times do |i|
      p = points[i][points.size-1]
      next if p.nil?
      if max.nil?
        max = p
      else
        max = [max, p].max
      end
    end
    return max
  end

  a = []
  array[row].each_with_index do |mass, col|
    next if mass.nil?
    if points[row][col].nil?
      points[row][col] = point + mass
      a << dfs(col, array, points, point + mass)
    elsif points[row][col] < point + mass
      return "inf"
    end
  end
  if a.include?("inf")
    return "inf"
  else
    return a.compact.max
  end
end

point = dfs(0, array, points, 0)

puts point


def get
  gets.chomp.split(" ").map(&:to_i)
end

N,M = get
input = M.times.map{get.map.sort}

task = Hash.new 0

input.each do |t|
  task[t] += 1
end

node = Array.new(M,0)

task.select{|k,v| v.odd?}.each do |k,v|
  node[k[0]-1] += 1
  node[k[1]-1] += 1
end

puts node.all?{|i| i.even?} ? :YES : :NO

Score : 500 points

Problem Statement

Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.

First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.

Then, Aoki gave him M queries. The i-th of them is as follows:

Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.

After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number.
However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.

Determine whether there exists a tree that has the property mentioned by Takahashi.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

1 ≤ a_i,b_i ≤ N

a_i ≠ b_i

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print YES if there exists a tree that has the property mentioned by Takahashi; print NO otherwise.

Sample Input 1

4 4
1 2
2 4
1 3
3 4

Sample Output 1

YES

For example, Takahashi's graph has the property mentioned by him if it has the following edges: 1-2, 1-3 and 1-4.
In this case, the number written at every edge will become 2.

Sample Input 2

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

Sample Output 2

NO


a, b, c = gets.chomp.split(" ").map &:to_s
if a.chars.last == b.chars.first && b.chars.last == c.chars.first
  puts "YES"
else
  puts "NO"
end

Score : 100 points

Problem Statement

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

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

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

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

rng gorilla apple

Sample Output 1

YES

They form a word chain.

Sample Input 2

yakiniku unagi sushi

Sample Output 2

NO

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

Sample Input 3

a a a

Sample Output 3

YES

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab

Sample Output 4

NO


a,b,c = gets.split.map(&:to_i)
puts (1..b).any?{ |n| n*a % b == c } ? "YES" : "NO"

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 ≤ A ≤ 100

1 ≤ B ≤ 100

0 ≤ C < B

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

7 5 1

Sample Output 1

YES

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

Sample Input 2

2 2 1

Sample Output 2

NO

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

Sample Input 3

1 100 97

Sample Output 3

YES

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

Sample Input 4

40 98 58

Sample Output 4

YES

Sample Input 5

77 42 36

Sample Output 5

NO


a=gets.to_f
b=gets.to_f
if(a>b)then
  puts("GREATER")
elsif a<b then
  puts("LESS")
else
  puts("EQUAL")
end

Score : 200 points

Problem Statement

You are given two positive integers A and B. Compare the magnitudes of these numbers.

Constraints

1 ≤ A, B ≤ 10^{100}

Neither A nor B begins with a 0.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print GREATER if A>B, LESS if A<B and EQUAL if A=B.

Sample Input 1

36
24

Sample Output 1

GREATER

Since 36>24, print GREATER.

Sample Input 2

850
3777

Sample Output 2

LESS

Sample Input 3

9720246
22516266

Sample Output 3

LESS

Sample Input 4

123456789012345678901234567890
234567890123456789012345678901

Sample Output 4

LESS


a, b = gets.chomp.split(' ').map(&:to_i)
if a > b
  puts 'GREATER'
elsif a < b
  puts 'LESS'
else
  puts 'EQUAL'
end


n   = gets.to_i
ary = gets.split.map(&:to_i)
#even > 0
sumE = ary[0]
sumO = ary[0] > 0 ? -1 : 1
ansE = 0
ansO = ary[0].abs + 1

for i in 1 .. n - 1
  if sumE * (sumE + ary[i]) >= 0
    if sumE > 0
      ansE += (- sumE - 1 - ary[i]).abs
      sumE  = -1
    else
      ansE += (- sumE + 1 - ary[i]).abs
      sumE  = 1
    end
  else
    sumE += ary[i]
  end
  if sumO * (sumO + ary[i]) >= 0
    if sumO > 0
      ansO += (- sumO - 1 - ary[i]).abs
      sumO  = -1
    else
      ansO += (- sumO + 1 - ary[i]).abs
      sumO  = 1
    end
  else
    sumO += ary[i]
  end
end

puts [ansE, ansO].min

Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

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

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8


Score : 300 points

Problem Statement

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

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

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

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


o=gets.chomp
e=gets.chomp
ans=""
e.size.times do |i|
  ans+=o[i]+e[i]
end
ans+=e[-1] if o.size-e.size==1
puts ans

Score : 200 points

Problem Statement

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

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

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

O
E

Output

Print the original password.

Sample Input 1

xyz
abc

Sample Output 1

xaybzc

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

Sample Input 2

atcoderbeginnercontest
atcoderregularcontest

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt


require 'pp'
$DEBUG ? define_method(:dpp){|e|pp e} : define_method(:dpp){|e|}

# 入力値を数値で返却する
def gets_i()
  gets.chomp.to_i
end

# 入力値を文字の配列で返却する
def gets_nsp_list()
  gets.chomp.split("")
end

N = gets_i

s = gets_nsp_list
ans = s.group_by(&:itself).map{|k,v| [k,v.count]}.to_h

(N - 1).times do
  s = gets_nsp_list
  count_hash = s.group_by(&:itself).map{|k,v| [k,v.count]}.to_h
  ans = (ans.keys & s).map do |k|
    [k, [ans[k], count_hash[k]].min]
  end.to_h
end

puts ans.map{|k,v| k * v}.join.chars.sort.join

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq n \leq 50

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

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

Input

Input is given from Standard Input in the following format:

n
S_1
...
S_n

Output

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

Sample Input 1

3
cbaa
daacc
acacac

Sample Output 1

aac

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

Sample Input 2

3
a
aa
b

Sample Output 2

The answer is an empty string.


N = gets.to_i
n = Math.sqrt(N).ceil;

while n <= N do
	if (N % n == 0)
		print i.to_s.size, "\n"
		break
	end
	n += 1
end

Score : 300 points

Problem Statement

You are given an integer N.

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

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

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

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

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

N

Output

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

Sample Input 1

10000

Sample Output 1

3

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

Sample Input 2

1000003

Sample Output 2

7

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

Sample Input 3

9876543210

Sample Output 3

6


def comb(a, b)
  res = 1
  for i in 1..b do
    res = res * (a-i+1) / i
  end
  res
end

nab = gets.chomp.split.map { |s| s.to_i }
v = gets.chomp.split.map { |s| s.to_i }.sort{|a, b| b <=> a }
s = v[0...nab[1]]
sum = s.reduce(:+)
avg = sum.to_f / s.length
puts avg

as = s.count(s.last)
mx = v.count(s.last)
if s.uniq.length == 1
  u = nab[2]
else
  u = nab[1]
end
ad = [u-s.length+as, mx].min
res = 0
for i in as..ad do
  res += comb(mx, i)
end

puts res

Score : 400 points

Problem Statement

You are given N items.

The value of the i-th item (1 \leq i \leq N) is v_i.

Your have to select at least A and at most B of these items.

Under this condition, find the maximum possible arithmetic mean of the values of selected items.

Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized.

Constraints

1 \leq N \leq 50

1 \leq A,B \leq N

1 \leq v_i \leq 10^{15}

Each v_i is an integer.

Input

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

N A B
v_1
v_2
...
v_N

Output

Print two lines.

The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.

The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

Sample Input 1

5 2 2
1 2 3 4 5

Sample Output 1

4.500000
1

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.

There is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

Sample Input 2

4 2 3
10 20 10 10

Sample Output 2

15.000000
3

There can be multiple ways to select items so that the mean of the values will be maximized.

Sample Input 3

5 1 5
1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996

Sample Output 3

1000000000000000.000000
1


a, b = gets.chomp.split

puts a == b ? 'H' : 'D'

Score : 100 points

Problem Statement

Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.
In this game, an honest player always tells the truth, and an dishonest player always tell lies.
You are given two characters a and b as the input. Each of them is either H or D, and carries the following information:

If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.
If b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.

Given this information, determine whether TopCoDeer is honest.

Constraints

a=H or a=D.

b=H or b=D.

Input

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

a b

Output

If TopCoDeer is honest, print H. If he is dishonest, print D.

Sample Input 1

H H

Sample Output 1

H

In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.

Sample Input 2

D H

Sample Output 2

D

In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.

Sample Input 3

D D

Sample Output 3

H


x = gets.to_i

s = t = 0
while s < x
  t += 1
  s += t
end
p t

Score : 200 points

Problem Statement

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.
During the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.
That is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.
The kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.
Find the earliest possible time to reach coordinate X.

Constraints

X is an integer.

1≤X≤10^9

Input

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

X

Output

Print the earliest possible time for the kangaroo to reach coordinate X.

Sample Input 1

6

Sample Output 1

3

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

Sample Input 2

2

Sample Output 2

2

He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second.

Sample Input 3

11

Sample Output 3

5


n = gets.to_i
a = n * 800
b = n / 15 * 200
puts a - b

Score : 100 points

Problem Statement

Snuke has a favorite restaurant.

The price of any meal served at the restaurant is 800 yen (the currency of Japan), and each time a customer orders 15 meals, the restaurant pays 200 yen back to the customer.

So far, Snuke has ordered N meals at the restaurant.
Let the amount of money Snuke has paid to the restaurant be x yen, and let the amount of money the restaurant has paid back to Snuke be y yen.
Find x-y.

Constraints

1 ≤ N ≤ 100

Input

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

N

Output

Print the answer.

Sample Input 1

20

Sample Output 1

15800

So far, Snuke has paid 16000 yen, and the restaurant has paid back 200 yen. Thus, the answer is 15800.

Sample Input 2

60

Sample Output 2

47200

Snuke has paid 48000 yen for 60 meals, and the restaurant has paid back 800 yen.


require 'pp'

def gets; ARGF.gets; end

class Solver

  def main
    n = gets.chomp.to_i

    puts 800 * n - 200 * (n / 15)
  end
end

if __FILE__ == $0
  Solver.new.main
end


N = gets.chomp.to_i
sum = 800 * N
back = N / 15 * 200
p sum - back


N = gets.chomp.to_i

@ary = {"0" => 1}
def factorial n
  @ary[n.to_s] ||= n * factorial(n-1) % (10 ** 9 + 7)
end

puts factorial(N)

# puts (1..N).inject(&:*) % (10 ** 9 + 7)

Score : 200 points

Problem Statement

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

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

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

Constraints

1 ≤ N ≤ 10^{5}

Input

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

N

Output

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

Sample Input 1

3

Sample Output 1

6

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

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

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

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

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


s,c = gets.split.map(&:to_i)
count = 0
r = c.to_f / (s*2)
r <= 1 ? count += s*r : count += s
c -= s*2
count += c / 4
puts count

Score : 300 points

Problem Statement

Snuke loves puzzles.

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

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

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

Constraints

1 ≤ N,M ≤ 10^{12}

Input

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

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

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

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

Sample Input 2

12345 678901

Sample Output 2

175897


require 'matrix'

n, m = gets.split.map(&:to_i)
a = Matrix.zero(n)
b = Matrix.zero(m)
n.times do |i|
  s = gets.chomp.chars
  n.times do |j|
    if s[j] == '#'
      a[i, j] = 1
    end
  end
end
m.times do |i|
  s = gets.chomp.chars
  m.times do |j|
    if s[j] == '#'
      b[i, j] = 1
    end
  end
end
(n - m).times do |i|
  (n - m).times do |j|
    if a.minor(i, m, j, m) == b
      puts "Yes"
      exit
    end
  end
end
puts "No"

Score : 200 points

Problem Statement

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

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

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

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

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

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

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

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

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

Constraints

1≦M≦N≦50

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

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

Input

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

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

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

Sample Input 1

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

Sample Output 1

Yes

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

Sample Input 2

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

Sample Output 2

No

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


n,m=gets.chomp.split.map(&:to_i)
nStrings=n.times.map{gets.chomp.split(" ").join}
mStrings=m.times.map{gets.chomp.split(" ").join}
result = "No"
(0..(n-m)).each do |yIndex|
  (0..(n-m)).each do |xIndex|
    if nStrings[yIndex].index(mStrings[0],xIndex) != nil
      result = "Yes"
      mStrings.each_with_index do |mString,tempIndex|
        next if tempIndex == 0
        findNumber = nStrings[tempIndex+yIndex].index(mString,xIndex)
        if findNumber == nil || findNumber != xIndex
          result = "No"
          break
        end
      end
    end
    break if result == "Yes"
  end
 break if result == "Yes"
end
puts result


N, M = gets.split.map(&:to_i)

as = []
N.times do
  as << gets.chomp
end
bs = []
M.times do
  bs << gets.chomp
end

(0..(N-M)).each do |n|
  (0..(N-M)).each do |m|
    tmp = []
    M.times do |dn|
      tmp << (as[n + dn][m, M] == bs[dn])
    end
    if tmp.all? { |e| e == true }
      puts('Yes')
      exit
    end
  end
end
puts('No')


n, m = gets.chomp.split(" ").map(&:to_i)
a = []
n.times do
  a << gets.chomp.split("")
end

b = []
m.times do
  b << gets.chomp
end
b = b.join("")
(n - m + 1).times do |py|
  (n - m + 1).times do |px|
    a_sub = []
    (py...(py+m)).each do |y|
       a_sub +=  a[y][px...(px+m)]
    end
    a_sub = a_sub.join("")
    if b == a_sub then
      puts "Yes"
      exit
    end
  end
end

puts "No"


n = gets.chomp.to_i
aa = gets.chomp.split.map(&:to_i)

odd = 0
(0...n).each do |i|
  odd += 1 if aa[i] % 2 == 1
end

if odd % 2 == 0
  puts("YES")
else
  puts("NO")
end

Score : 300 points

Problem Statement

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

Takahashi will repeatedly perform the following operation on these numbers:

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

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

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

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

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

N
A_1 A_2 … A_N

Output

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

Sample Input 1

3
1 2 3

Sample Output 1

YES

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

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

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

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


k = gets.to_i
a = ""
a = gets.chomp.split(" ").map(&:to_i)

count_kisu = 0
count_gusu = 0
len = 0

if(k%2 == 0)
   len = (k / 2) - 1
else
    len = k / 2
end

for i in 0..len
    if(a[i]%2 != 0)
        count_kisu += 1
    else
        count_gusu += 1
    end

    if(i != k-1-i)
        if(a[k-1-i]%2 != 0)
            count_kisu += 1
        else
            count_gusu += 1
        end
    end
end

while count_gusu%2 == 0
    count_gusu = count_gusu / 2
    if(count_gusu == 1)
        flg = 1
        break
    end
end

if(count_kisu%2 != 0)
    print("NO")
elsif((count_kisu%2 == 0) && (k%2 == 0) && (flg == 1))
    print("NO")
else
    print("YES")
end


N = gets.to_i
M = N * (N + 1) / 2

a_list = gets.strip.split(/ /).collect{|num| num.to_i}

sum = a_list.inject(:+)

if sum % M == 0
	L = sum / M

	start_points = 0

	prev_a = a_list[N - 1]
	(0...N).each{|i|
		a = a_list[i]
		diff = a - prev_a
		if diff > L
			puts "NO1"
			exit
		elsif diff != L
			# x - (N - 1)(L - x) = diff
			# x = (diff + NL - L) / N
			if (diff + N * L - L) % N != 0
				puts "NO2"
				exit
			else
				start_points += L - (diff + N * L - L) / N
				if start_points > L
					puts "NO3"
					exit
				end
			end
		end
		prev_a = a
	}

	if (L - start_points) % L == 0
		puts "YES"
	else
		puts "NO"
	end

else
	puts "NO"
end

Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

Input

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

N
A_1 A_2 … A_N

Output

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

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

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

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO


x = gets.to_i
puts x < 1200 ? "ABC" : "ARC"

Score : 100 points

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

Constraints

1 ≦ x ≦ 3{,}000

x is an integer.

Input

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

x

Output

Print the answer.

Sample Input 1

1000

Sample Output 1

ABC

Smeke's current rating is less than 1200, thus the output should be ABC.

Sample Input 2

2000

Sample Output 2

ARC

Smeke's current rating is not less than 1200, thus the output should be ARC.


x = gets.to_i
n = x / 11 * 2

if (x % 11).between?(1, 6)
  n += 1
elsif (x % 11).between?(7, 10)
  n += 2
end

p n

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

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

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


a,b,c,d=gets.chomp.split(" ").map(&:to_i)

first = a * b
second = c * d

first >= second ? print first : print second

Score : 100 points

Problem Statement

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

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

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

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

A B C D

Output

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

Sample Input 1

3 5 2 7

Sample Output 1

15

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

Sample Input 2

100 600 200 300

Sample Output 2

60000


n = gets.to_i
s = gets.strip.split("")

x = 0
result = 0

for var in s
    x += 1 if var == "I"
    x -= 1 if var == "D"
    result = x if result < x
end

puts result

Score : 200 points

Problem Statement

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

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

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

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

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

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

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

Sample Input 2

7
DDIDDII

Sample Output 2

0

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


n = gets.chomp.to_i
nbang = 1
n.downto(1){ |v| nbang *= v }

factors = []
p = 2
  while nbang >= p
    if nbang % p == 0
     nbang /= p
      factors[p] ||= 0
      factors[p] += 1
    else
      p += 1
    end
  end

result = 1
factors.each_with_index do |fac|
  result *= fac + 1 if fac
end

puts result

Score : 300 points

Problem Statement

You are given an integer N.
Find the number of the positive divisors of N!, modulo 10^9+7.

Constraints

1≤N≤10^3

Input

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

N

Output

Print the number of the positive divisors of N!, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

4

There are four divisors of 3! =6: 1, 2, 3 and 6. Thus, the output should be 4.

Sample Input 2

6

Sample Output 2

30

Sample Input 3

1000

Sample Output 3

972926972


require'prime';s=1;eval(`seq -s* $(dd)`).prime_division.map{s*=_2+1};p s.%10**9+7


Score : 300 points

Problem Statement

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

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

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

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

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

Under this condition, find a shortest path for him.

Constraints

-1000 ≤ sx < tx ≤ 1000

-1000 ≤ sy < ty ≤ 1000

sx,sy,tx and ty are integers.

Input

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

sx sy tx ty

Output

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

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

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

U: Up

D: Down

L: Left

R: Right

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

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

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

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

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

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

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL


N, M = gets.split.map(&:to_i)
MAX = 1 << 30
dist = Array.new(N) { Array.new(N, MAX) }
graph = []
M.times do
  a, b, c = gets.split.map(&:to_i)
  graph << [a-1, b-1, c]
  dist[a - 1][b - 1] = dist[b - 1][a - 1] = c
end

N.times do |k|
  N.times do |i|
    N.times do |j|
      dist[i][j] = dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]
    end
  end
end
ans = 0
graph.each do |(a,b,c)|
  ans += 1 if dist[a][b] < c
end
p ans

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

2≤N≤100

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

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

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

The given graph is connected.

Input

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

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

Output

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

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

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

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

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

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

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

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

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

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

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

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


N, M = readline.split(' ').map(&:to_i)
Edges = Hash.new {|h,k| h[k]=Hash.new }

M.times do |idx|
  n1, n2, dist = readline.split(' ').map(&:to_i)
  Edges[n1-1][n2-1] = [dist, idx]
  Edges[n2-1][n1-1] = [dist, idx]
end

class Path
  attr_reader :distance, :node_seq
  def initialize(distance, node_seq)
    @distance = distance
    @node_seq = node_seq
  end
end

def find_a_path_leq(src, dest, max)
  sorted_paths = [Path.new(0, [src])] # sorted_paths: [Path] sorted by Path.distance

  while head = sorted_paths.shift
    if head.distance >= max
      return nil
    end

    if head.node_seq[-1]==dest
      return head.node_seq
    end

    new_paths = []
    Edges[head.node_seq[-1]].each do |dest2, edge|
      distance, = *edge
      p = Path.new(head.distance + distance, head.node_seq+[dest2])
      new_paths.push(p)
    end

    sorted_paths.push(*new_paths)
    sorted_paths.sort_by!(&:distance)
  end
end

count=0
Edges.keys.sort.each do |src|
  Edges[src].keys.sort.each do |dst|
    if src<=dst then
      break
    end

    e = Edges[src][dst]
    if find_a_path_leq(src, dst, e[0])
      count += 1
    end
  end
end

puts count


class Array
  def warshall_floyd
    v = size

    used = Array.new(v) { Array.new(v, false) }
    dist = map(&:dup)

    v.times do |k|
      v.times do |i|
        v.times do |j|
          cost = dist[i][k] + dist[k][j]

          if dist[i][j] > cost
            used[i][k] = used[k][i] = true
            used[j][k] = used[k][j] = true
            used[i][j] = used[j][i] = false
            dist[i][j] = cost
          end
        end
      end
    end

    used
  end
end

N, M = gets.split.map(&:to_i)
graph = Array.new(N) { Array.new(N, Float::INFINITY) }

N.times do |i|
  graph[i][i] = 0
end

edges = M.times.map { gets.split.map(&:to_i) }

edges.each do |a, b, c|
  graph[a - 1][b - 1] = c
  graph[b - 1][a - 1] = c
end

used = graph.warshall_floyd

ans = 0

puts edges.count { |a, b, _| !used[a - 1][b - 1] }


N,M = gets.split.map(&:to_i)
a = []
b = []
c = []
for i in 0..M-1 do
    a[i],b[i],c[i] = gets.split.map(&:to_i)
end
dist = Array.new(N).map{Array.new(N)}
#distの初期化
for i in 0..N-1 do
    for j in 0..N-1 do
        if i == j then
            dist[i][j] = 0
        else
            dist[i][j] = 10000
        end
    end
end

for i in 0..M-1 do
    dist[a[i]-1][b[i]-1] = c[i]
    dist[b[i]-1][a[i]-1] = c[i]
end
#ワーシャルフロイド法
for k in 0..N-1 do
    for i in 0..N-1 do
        for j in 0..N-1 do
            dist[i][j] = [dist[i][j],dist[i][k] + dist[k][j]].min
        end
    end
end
#使用されていない辺の計算
sum = 0
for i in 0..M-1 do
    if dist[a[i]-1][b[i]-1] < c[i] then
        sum = sum + 1
    end
end
puts sum


Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

x and y are integers.

|x|, |y| ≤ 10^9

x and y are different.

Input

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

x y

Output

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

Sample Input 1

10 20

Sample Output 1

10

Press button A ten times.

Sample Input 2

10 -10

Sample Output 2

1

Press button B once.

Sample Input 3

-10 -20

Sample Output 3

12

Press the buttons as follows:

Press button B once.

Press button A ten times.

Press button B once.


Score : 100 points

Problem Statement

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

Constraints

1≦A,B≦10^9

op is either + or -.

Input

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

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

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

Sample Input 2

5 - 7

Sample Output 2

-2


n = gets.to_i
inputs = gets.chomp.split(" ").map(&:to_i).sort!
if n % 2 == 1
	if inputs[0] != 0 && inputs[0] == inputs[1]
		puts 0
		exit
	end
end
(n % 2).step(n-2,2) { |i|
	if inputs[i] != inputs[i+1] and inputs[i] != i+1
		puts 0
		exit
	end
}

puts (n/2) **2 % (10**9+7)

Score : 300 points

Problem Statement

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

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

Constraints

1≦N≦10^5

0≦A_i≦N-1

Input

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

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

2,1,4,5,3

2,5,4,1,3

3,1,4,5,2

3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

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

Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16


n = gets.to_i
a = gets.split.map { |i| i.to_i}.sort

ans = 0

if n%2 == 0 then
	correct = ((1..n/2).map{|i| i*2-1}*2).sort
else
	correct = ([0]+(1..n/2).map{|i| i*2}*2).sort
end

if a == correct then
	ans = (2**(n/2))%1000000007
end

puts ans


puts %w(a i u e o).include?(gets.chop) ? 'vowel' : 'consonant'

Score : 100 points

Problem Statement

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

Constraints

c is a lowercase English letter.

Input

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

c

Output

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

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


H, W = gets.split.map(&:to_i)

C = H.times.map {
  gets.split
}

C.each do |c|
  2.times do
    puts c
  end
end


n, k, l = gets.chomp.split.map(&:to_i)
ks = []
ls = []

k.times do
  a, b = gets.chomp.split.map(&:to_i)
  included = false
  ks.each do |kss|
    if kss.include?(a)
      kss << b
      included = true
      break
    elsif kss.include?(b)
      kss << a
      incuded = true
      break
    end
  end

  next if included

  ks << [a,b]
end

l.times do
  a, b = gets.chomp.split.map(&:to_i)
  included = false
  ls.each do |lss|
    if lss.include?(a)
      lss << b
      included = true
      break
    elsif lss.include?(b)
      lss << a
      incuded = true
      break
    end
  end

  next if included

  ls << [a,b]
end

# require 'pp'

ans = Array.new(n, 1)

ks.each do |kss|
  ls.each do |lss|
    sm = kss & lss
    next if sm.size < 2
    sm.each do |smm|
      puts smm
      ans[smm-1] += 1
    end
  end
end

puts ans.join(' ')

Score : 400 points

Problem Statement

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

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

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

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

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

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

Input

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

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

Output

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

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

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

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

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


a,b,c=gets.chomp.split
puts "A" + b[0] + "C"

Score : 100 points

Problem Statement

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

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

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

Constraints

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

The first character in s is an uppercase English letter.

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

Input

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

AtCoder s Contest

Output

Print the abbreviation of the name of the contest.

Sample Input 1

AtCoder Beginner Contest

Sample Output 1

ABC

The contest in which you are participating now.

Sample Input 2

AtCoder Snuke Contest

Sample Output 2

ASC

This contest does not actually exist.

Sample Input 3

AtCoder X Contest

Sample Output 3

AXC


a,b,n=gets.split.map &:to_i
big=b/n
small=-~-a/n
p big+small

Score : 200 points

Problem Statement

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

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

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

a b x

Output

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

Sample Input 1

4 8 2

Sample Output 1

3

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

Sample Input 2

0 5 1

Sample Output 2

6

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

Sample Input 3

9 9 2

Sample Output 3

0

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

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


a , b , c = gets.split.map(&:to_i)
d = 0
if a == 0 then
  a += 1
  d += 1
end
a = a + c - (a % c)
a += 1

d = d + (b - a + 1) / c
d += 1
puts d