t={io.read("n","n","n")}
k=io.read("n")
table.sort(t)
print(t[1]+t[2]+math.floor(t[3]*2^k))

Score: 200 points

Problem Statement

There are three positive integers A, B and C written on a blackboard. E869120 performs the following operation K times:

Choose one integer written on the blackboard and let the chosen integer be n. Replace the chosen integer with 2n.

What is the largest possible sum of the integers written on the blackboard after K operations?

Constraints

A, B and C are integers between 1 and 50 (inclusive).

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

Input

Input is given from Standard Input in the following format:

A B C
K

Output

Print the largest possible sum of the integers written on the blackboard after K operations by E869220.

Sample Input 1

5 3 11
1

Sample Output 1

30

In this sample, 5, 3, 11 are initially written on the blackboard, and E869120 can perform the operation once.

There are three choices:

Double 5: The integers written on the board after the operation are 10, 3, 11.

Double 3: The integers written on the board after the operation are 5, 6, 11.

Double 11: The integers written on the board after the operation are 5, 3, 22.

If he chooses 3., the sum of the integers written on the board afterwards is 5 + 3 + 22 = 30, which is the largest among 1. through 3.

Sample Input 2

3 3 4
2

Sample Output 2

22

E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows:

First, double 4. The integers written on the board are now 3, 3, 8.

Next, double 8. The integers written on the board are now 3, 3, 16.

Then, the sum of the integers eventually written on the blackboard is 3 + 3 + 16 = 22.


local h, w = io.read("*n", "*n", "*l")
local str = ""
local t = {}
for i = 1, h do
  str = io.read()
  for j = 1, w do
    t[(i - 1) * w + j] = string.sub(str, j, j) == "#" and true or false
  end
end
local s = h * w
local allok = true
for i = 1, s do
  if(t[i]) then
    local isok = false
    if(w < i) then
      if(t[i - w]) then isok = true end
    end
    if(i <= s - w) then
      if(t[i + w]) then isok = true end
    end
    if(i % w ~= 0) then
      if(t[i + 1]) then isok = true end
    end
    if(i % w ~= 1) then
      if(t[i - 1]) then isok = true end
    end
    if(not isok) then allok = false break end
  end
end
print(allok and "Yes" or "No")

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

Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

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

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

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes

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.


local h, w = io.read("*n", "*n", "*l")
local str = ""
local t = {}
for i = 1, h do
  str = io.read()
  for j = 1, w do
    t[(i - 1) * w + j] = string.sub(str, j, j) == "#" and true or false
  end
end
local s = h * w
local allok = true
for i = 1, s do
  if(t[i]) then
    local isok = false
    if(w < i) then
      if(t[i - w]) then isok = true end
    end
    if(i <= s - w) then
      if(t[i + w]) then isok = true end
    end
    if(i % w ~= 0) then
      if(t[i + 1]) then isok = true end
    end
    if(i % w ~= 1) then
      if(t[i - 1]) then isok = true end
    end
    if(not isok) then allok = false print(i) break end
  end
end
print(allok and "Yes" or "No")


function is_prime(n)
	for i=2,math.sqrt(n)do
		if n%i==0 then return false end
	end
	return true
end
n=io.read("*n")
cnt=0
for i=2,2000 do
	if cnt==n then break end
	if is_prime(i)and i%5==1 then
		if cnt~=0 then io.write" "end
		io.write(i)
		cnt=cnt+1
	end
end

Score: 400 points

Problem Statement

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

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

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

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

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

Notes

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

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

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

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

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

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

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

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

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

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

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

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

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

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


local n = io.read("*n", "*l")
local rowmap = {}
for i = 1, n do
  rowmap[i] = {io.read()}
  for j = 2, n do
    rowmap[i][j] = rowmap[i][j - 1]:sub(n, n) .. rowmap[i][j - 1]:sub(1, n - 1)
  end
end
local colmap = {}
for i = 1, n do
  local tmps = ""
  for j = 1, n do
    tmps = tmps .. rowmap[j][1]:sub(i, i)
  end
  colmap[i] = {tmps}
  for j = 2, n do
    colmap[i][j] = colmap[i][j - 1]:sub(n, n) .. colmap[i][j - 1]:sub(1, n - 1)
  end
end

local function check(r, c)
  for k = 1, n do
    local row_tgt = k + n - r
    if n < row_tgt then row_tgt = row_tgt - n end
    local row_ofst = 1 + c
    local col_tgt = k + n - c
    if n < col_tgt then col_tgt = col_tgt - n end
    local col_ofst = 1 + r
    if rowmap[row_tgt][row_ofst] ~= colmap[col_tgt][col_ofst] then return 0 end
  end
  return 1
end

local ret = 0
for i = 0, n - 1 do
  for j = 0, n - 1 do
    ret = ret + check(i, j)
  end
end
print(ret)

Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.
For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

First, choose two integers A and B ( 0 \leq A, B < N ).

Write one letter in each square on the second board.
Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.
Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

1 \leq N \leq 300

S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.

Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.


local n = io.read("*n", "*l")
local rowmap = {}
for i = 1, n do
  local tmps = io.read()
  rowmap[i] = {tmps}
  for j = 2, n do
    tmps = tmps:sub(n, n) .. tmps:sub(1, n - 1)
    rowmap[i][j] = tmps
  end
end

local colmap = {}
for i = 1, n do
  local tmps = ""
  for j = 1, n do
    tmps = tmps .. rowmap[j][1]:sub(i, i)
  end
  colmap[i] = {tmps}
  for j = 2, n do
    tmps = tmps:sub(n, n) .. tmps:sub(1, n - 1)
    colmap[i][j] = tmps
  end
end

local function check(r, c)
  for k = 1, n do
    local row_tgt = k + n - r
    if n < row_tgt then row_tgt = row_tgt - n end
    local row_ofst = 1 + c
    local col_tgt = k + n - c
    if n < col_tgt then col_tgt = col_tgt - n end
    local col_ofst = 1 + r
    if rowmap[row_tgt][row_ofst] ~= colmap[col_tgt][col_ofst] then return 0 end
  end
  return 1
end
local ret = 0
for i = 0, n - 1 do
  for j = 0, n - 1 do
    ret = ret + check(i, j)
  end
end
print(ret)


local S = io.read("*l")

local o_count = 0
for i = 1, S:len() do
    if S:sub(i, i) == "o" then
        o_count = o_count + 1
    end
end

print(700 + o_count*100)

Score : 100 points

Problem Statement

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

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

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

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

oxo

Sample Output 1

900

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

Sample Input 2

ooo

Sample Output 2

1000

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

Sample Input 3

xxx

Sample Output 3

700

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


s = io.read()
if s == "ooo" then
  print(1000)
elseif s == "xoo" then
  print(900)
elseif s == "oxo" then
  print(900)
elseif s == "oox" then
  print(900)
elseif s == "xxo" then
  print(800)
elseif s == "oxx" then
  print(800)
elseif s == "xox" then
  print(800)
else
  print(700)
end


local N, X = io.read("*n", "*n")
local m = {}

local m_sum = 0
for i = 1, N do
    m[i] = io.read("*n")
    m_sum = m_sum + m[i]
end
table.sort(m)

print(N + math.modf((X - m_sum) / m[1]))

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


local n, k = io.read("*n", "*n")
local min = io.read("*n")
local k = k - min
for i = 2, n do
  local a = io.read("*n")
  k = k - a
  min = math.min(min, a)
end
print(n + k // min)


n,x=io.read("*n","*n")
Min=1e9
for i=1,n do
	m=io.read("*n")
	Min=math.min(Min,m)
	x=x-m
end
print(n+math.floor(x/Min))


A=io.read("n")
B=io.read("n")
X=io.read("n")
if A>X then
  print("NO")
elseif A==X then
  print("YES")
elseif A<X then
  if A+B>=X then
    print("YES")
  else
    print("NO")
  end
end

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


local n, m, x = io.read("*n", "*n", "*n")
local left = 0
for i = 1, m do
  local a = io.read("*n")
  if a < x then left = left + 1 end
end
print(math.min(left, m - left))

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


local n = io.read("*n")
local t, k = {}, {}
for i = 1, n do
  t[i] = io.read("*n")
  k[i] = t[i]
end
table.sort(k)
local mid1, mid2 = k[n // 2], k[n // 2 + 1]
for i = 1, n do
  if(t[i] <= mid1) then print(mid2) else print(mid1) end
end

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


local mfl = math.floor
local function lower_bound(ary, x)
  if(x <= ary[1]) then return 1 end
  local num = #ary
  if(ary[num] < x) then return num + 1 end
  local min, max = 1, num
  while(1 < max - min) do
    local mid = mfl((min + max) / 2)
    if(ary[mid] < x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local n = io.read("*n")
local a = {}
for i = 1, n do a[i] = io.read("*n") end
table.sort(a)
local max = a[n]
table.remove(a)
local idx = lower_bound(a, max / 2)
if(idx == n) then
  idx = n - 1
elseif(1 < idx) then
  if(math.abs(max - 2 * a[idx - 1]) < math.abs(max - 2 * a[idx])) then
    idx = idx - 1
  end
end
print(max .. " " .. a[idx])

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


local h, w = io.read("*n", "*n", "*l")
local map = {}
for i = 1, h do
  local str = io.read()
  for j = 1, w do
    map[(i - 1) * w + j] = str:sub(j, j)
  end
end
local vertical = {}
local vmap = {}
for i = 1, w do
  vertical[i] = {}
  for j = 1, h do
    vertical[i][j] = map[(j - 1) * w + i]
  end
  table.sort(vertical[i])
  vertical[i] = table.concat(vertical[i], "")
  local v = vertical[i]
  if vmap[v] then
    vmap[v] = nil
  else
    vmap[v] = true
  end
end
local vrem = 0
for k, v in pairs(vmap) do
  vrem = vrem + 1
end

local horizontal = {}
local hmap = {}
for i = 1, h do
  horizontal[i] = {}
  for j = 1, w do
    horizontal[i][j] = map[(i - 1) * w + j]
  end
  table.sort(horizontal[i])
  horizontal[i] = table.concat(horizontal[i], "")
  local v = horizontal[i]
  if hmap[v] then
    hmap[v] = nil
  else
    hmap[v] = true
  end
end
local hrem = 0
for k, v in pairs(hmap) do
  hrem = hrem + 1
end
local f = vrem <= 1 and hrem <= 1
print(f and "YES" or "NO")

Score : 700 points

Problem Statement

There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter.
Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i.

Snuke can apply the following operation to this grid any number of times:

Choose two different rows and swap them. Or, choose two different columns and swap them.

Snuke wants this grid to be symmetric.
That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal.

Determine if Snuke can achieve this objective.

Constraints

1 \leq H \leq 12

1 \leq W \leq 12

|S_i| = W

S_i consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

If Snuke can make the grid symmetric, print YES; if he cannot, print NO.

Sample Input 1

2 3
arc
rac

Sample Output 1

YES

If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:

Sample Input 2

3 7
atcoder
regular
contest

Sample Output 2

NO

Sample Input 3

12 12
bimonigaloaf
faurwlkbleht
dexwimqxzxbb
lxdgyoifcxid
ydxiliocfdgx
nfoabgilamoi
ibxbdqmzxxwe
pqirylfrcrnf
wtehfkllbura
yfrnpflcrirq
wvcclwgiubrk
lkbrwgwuiccv

Sample Output 3

YES


S=io.read()
a=string.sub(S,1,1)
b=string.sub(S,2,2)
c=string.sub(S,3,3)
if a~=b and a~=c and b~=c then
  print("Yes")
  else
  print("No")
end

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


local a, b, k = io.read("*n", "*n", "*n")
local z = {}
local t = {}
for i = a, b do
  if i < a + k then
    table.insert(t, i)
    z[i] = true
  else
    break
  end
end
for i = b, a, -1 do
  if b < i + k then
    if not z[i] then table.insert(t, i) end
  else
    break
  end
end
table.sort(t)
print(table.concat(t, "\n"))

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

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

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


A=io.read("n")
B=io.read("n")
K=io.read("n")
if 2*K>=B-A+1 then
  for i=A,B do
    print(i)
  end
  else
  for i=A,A+K-1 do
    print(i)
  end
  for i=B-K+1,B do
    print(i)
  end
end


Q=io.read("n")
for i=1,Q do
  A=io.read("n")
  B=io.read("n")
  if A>B then
    A,B=B,A
  end
  local count=0
  local k=1
  for i=B-1,A,-1 do
    if i*(A+k)<A*B then
      count=count+1
      k=k+1
    end
  end
  print(A*2+count-2)
end

Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.


local n = io.read("*n")
local wmax = 0
local score = 0
local ws, vs = {}, {}
local samesum = 0
for i = 1, n do
  local a, b = io.read("*n", "*n")
  if a < b then
    score = score + b
    wmax = wmax + b - a
  elseif a == b then
    samesum = samesum + b
  else
    table.insert(ws, a - b + 1)
    table.insert(vs, b)
  end
end
if 0 < wmax then
  score = score + samesum
end

local mma, mmi = math.max, math.min
local dpret = 0
local dpmin = 0
local tn = #ws
for i = 1, tn do
  local w, v = ws[i], vs[i]
  dpret = dpret + v
  dpmin = i == 1 and v or mmi(dpmin, v)
end
print(score + dpret - dpmin)

Score : 700 points

Problem Statement

You are given sequences A and B consisting of non-negative integers.
The lengths of both A and B are N, and the sums of the elements in A and B are equal.
The i-th element in A is A_i, and the i-th element in B is B_i.

Tozan and Gezan repeats the following sequence of operations:

If A and B are equal sequences, terminate the process.

Otherwise, first Tozan chooses a positive element in A and decrease it by 1.

Then, Gezan chooses a positive element in B and decrease it by 1.

Then, give one candy to Takahashi, their pet.

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible.
Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

1 \leq N \leq 2 × 10^5

0 \leq A_i,B_i \leq 10^9(1\leq i\leq N)

The sums of the elements in A and B are equal.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

Sample Input 1

2
1 2
3 2

Sample Output 1

2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

Tozan decreases A_1 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

Tozan decreases A_2 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

As A and B are equal, the process is terminated.

Sample Input 2

3
8 3
0 1
4 8

Sample Output 2

9

Sample Input 3

1
1 1

Sample Output 3

0


local s=io.read()

if #s==26 then
    for i=#s,2,-1 do
        if s:sub(i,i)>s:sub(i-1,i-1) then
            s=s:sub(1,i-1)
            break
        end
    end
    if #s==26 then
        print(-1)
        return
    else
        for i=97,122 do
            local c=string.char(i)
            if not s:find(c) and s:sub(#s,#s)<c then
                print(s:sub(1,#s-1)..c)
                break
            end
        end
    end
elseif #s==1 then
    for i=97,122 do
        local c=string.char(i)
        if not s:find(c) and s:sub(#s,#s)<c then
            print(s:sub(1,#s-1)..c)
            break
        end
    end
else
    for i=97,122 do
        local c=string.char(i)
        if not s:find(c) then
            print(s..c)
            break
        end
    end
end

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


N=io.read("n")
D=io.read("n")
X=io.read("n")
A={}
local total=0
for i=1,N do
  A[i]=io.read("n")
  local k=1
  while (1+A[i]*k<=D) do
    k=k+1
  end
  k=k-1
  total=total+k
end
print(N+total+X)

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp.
The camp had N participants and lasted for D days.
The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.
As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

1 \leq N \leq 100

1 \leq D \leq 100

1 \leq X \leq 100

1 \leq A_i \leq 100 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days.
Each participant eats chocolate pieces as follows:

The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.

The second participant eats one chocolate piece on Day 1 and 6, for a total of two.

The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56


local a = io.read("*n")
local b = io.read("*n")
local c = io.read("*n")

local change = (a + b ) - c

if change >= 0 then
    print("Yes")
else
    print("No")
end

Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


local a = io.read("*n")
local b = io.read("*n")
local c = io.read("*n")

local change = (a + b * 2) - c

if change >= 0 then
    print("Yes")
else
    print("No")
end


A=io.read("n")
B=io.read("n")
C=io.read("n")
if A+B>=C then
  print("Yes")
  else
  print("No")
end


N=io.read()
s={}
count={}
for i=1,N do
  s[i]=io.read()
  count[s[i]]=(count[s[i]] or 0) +1
end
M=io.read()
t={}
for i=1,M do
  t[i]=io.read()
  count[t[i]]=(count[t[i]] or 0) -1
end
local earn=0
for _,v in pairs(count) do
  if v>earn then
    earn=v
  end
end
print(earn)

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


N=io.read()
s={}
count={}
for i=1,N do
  s[i]=io.read()
  count[s[i]]=(count[s[i]] or 0) +1
end
M=io.read()
t={}
for i=1,M do
  t[i]=io.read()
  count[t[i]]=(count[t[i]] or 0) -1
end
local earn=0
for _,v in pairs(count) do
  if v>0 then
    earn=earn+v
  end
end
print(earn)


N=io.read()
s={}
count={}
for i=1,N do
  s[i]=io.read()
  count[s[i]]=count[s[i]] or 0 +1
end
M=io.read()
t={}
for i=1,M do
  t[i]=io.read()
  count[t[i]]=count[t[i]] or 0 -1
end
local earn=0
for _,v in pairs(count) do
  if v>0 then
    earn=earn+v
  end
end
print(earn)


Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


local flo = math.floor

local function div_flo(divided, dividing)
	return flo(divided / dividing)
end

local A, B = io.read("*n", "*n")

local div_A = div_flo(A, 100)
local div_B = div_flo(B, 100)

local count = 0
for i = div_A, div_B do
	local pal = tonumber(tostring(i)..tostring(div_flo(i, 10)):reverse())
	if A <= pal and pal <= B  then
		count = count + 1
	end
end

print(count)

Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


A, B = io.read("*n", "*n")
counter = 0
for i = A, B do
    s = tostring(i)
    if s:sub(1,1)==s:sub(5,5) and s:sub(2,2)==s:sub(4,4) then
        counter = counter + 1
    end
end
print(counter)


local a, b = io.read("*n", "*n")
local c = 0
for i = a, b do
  local s = tostring(i)
  if s:sub(1, 1) == s:sub(5, 5)
  and s:sub(2, 2) == s:sub(4, 4) then
    c = c + 1
  end
end
print(c)


A=io.read("n")
B=io.read("n")
local count=0
for i=A,B do
  if i//10000==i%10 and i//1000%10==i//10%10 then
    count=count+1
  end
end
print(count)


N=io.read("n")
M=io.read("n")
local count=0
if N==1  then
  count=M-2
  elseif M==1 then
  count=N-2
  elseif N==2 or M==2 then
  count=0
  elseif N>=3 and M>=3 then
  count=(N-2)*(M-2)
end
print(count)

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


local n, k = io.read("*n", "*n")
local c = 0
if k == 0 then
  print(n * n)
else
  for b = k + 1, n do
    local x, y = n // b, n % b
    c = c + (b - k) * x
    if 0 < y then
      c = c + math.max(0, y - k + 1)
    end
  end
  print(c)
end

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


N=io.read("n")
K=io.read("n")
local count=0
for i=K+1,N do
  for j=1,N do
    if j%i>=K then
      count=count+1
    end
  end
end
print(count)


n,m=io.read("*n","*n")
if n>=m then
    n,m=m,n
end

if n>=2 and m>=2 then
    print((n-2)*(m-2))
elseif n==1 and m==1 then
    print(1)
elseif n==1 and m>1 then
    print(m-2)
end


local n,k=io.read("*n","*n")
local ans=0
if (k==0) then
  ans=n*n
else
  for i=k,n do
    ans=ans+(i-k)*(n//i)
    if (n%i>=k) then
      ans=ans+(n%i)-k+1
    end
  end
end
print(ans)


local n, k = io.read("*n", "*n")
local cnt = 0
local mm = math.max
for b = k + 1, n do
  cnt = cnt + (n // b) * (b - k) + mm(0, (n % b - k + 1))
end
print(cnt)


local number = io.read()
print(math.floor(number/3))

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

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

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


local io_read = io.read
local math_abs = math.abs
local H, W, D = io_read("*n", "*n", "*n")
local AH = {}
local AW = {}
for h=1,H do
  for w=1,W do
    local a = io_read("*n")
    AH[a] = h
    AW[a] = w
  end
end
local CC = {}
for i=1,H*W-D do
  CC[i] = math_abs(AH[i] - AH[i+D]) + math_abs(AW[i] - AW[i+D])
end
local CCC = {}
for i=1,D do
  CCC[i] = 0
end
for i=1,H*W-D do
  CCC[i+D] = CCC[i] + CC[i]
end

local Q = io_read("*n")
local C = {}
for q=1,Q do
  local l, r = io_read("*n", "*n")
  local cost = CCC[r] - CCC[l]
  C[q] = cost
end
print(table.concat(C, "\n"))

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0


local H, W, D = io.read("*n", "*n", "*n")
local AH = {}
local AW = {}
for h=1,H do
  for w=1,W do
    local a = io.read("*n")
    AH[a] = h
    AW[a] = w
  end
end
local Q = io.read("*n")
for q=1,Q do
  local l, r = io.read("*n", "*n")
  local cost = 0
  for x=l,r-D,D do
    cost = cost + math.abs(AH[x] - AH[x+D]) + math.abs(AW[x] - AW[x+D])
  end
  print(cost)
end


local mfl, mab = math.floor, math.abs
local h, w, d = io.read("*n", "*n", "*n")
local n = h * w
local map, invmap = {}, {}
local cost = {}
for i = 1, n do invmap[i] = 0 end
for i = 1, n do
  local a = io.read("*n")
  map[i] = a
  invmap[a] = i
  cost[i] = 0
end
for t_i_d = 1, d do
  local src_x, src_y = 0, 0
  local costsum = 0
  local i_d = t_i_d
  while i_d <= n do
    local i = invmap[i_d]
    local dst_x, dst_y = 1 + (i - 1) % w, 1 + mfl((i - 1) / w)
    costsum = costsum + mab(dst_x - src_x) + mab(dst_y - src_y)
    cost[i_d] = costsum
    src_x, src_y = dst_x, dst_y
    i_d = i_d + d
  end
end
local q = io.read("*n")
for i = 1, q do
  local l, r = io.read("*n", "*n")
  print(cost[r] - cost[l])
end


n=io.read("*n")
o=tostring(n)
for i=2,#o do
    if o:sub(i,i)~="9" then
        print(math.floor(tonumber(o:sub(1,1)-1)+9*(#o-1)))
        return
    end
end
print(math.floor(tonumber(o:sub(1,1))+9*(#o-1)))

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


c={}
for i=1,3 do
    c[i]={}
    for j=1,3 do
        c[i][j]=io.read("*n")
    end
end

checker={}
for i=1,3 do
    checker[i]={}
    for j=2,3 do
        x=c[i][j-1]-c[i][j]
        table.insert(checker[i],x)
    end
end

takahashi=true
for i=2,3 do
    if checker[i][1]~=checker[i-1][1] or checker[i][2]~=checker[i-1][2] then
        takahashi=false
    end
end

if takahashi then
    print("Yes")
else
    print("No")
end

Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


--https://www.lua.org/pil/11.4.html
local List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local h,w=io.read("n","n","l")
local maze={}
local shounter=0
for i=1,h do
    local s=io.read()
    maze[i]={}
    for j=1,w do
        local sub=s:sub(j,j)
        if sub=="#" then
            shounter=shounter+1
        end
        maze[i][j]=sub
    end
end

local dx={1,0,-1,0}
local dy={0,1,0,-1}

local function bfs(x,y)
    local dist={}
    for i=1,h do
        dist[i]={}
        for j=1,w do
            dist[i][j]=-1
        end
    end
    local que=List.new()

    List.pushright(que,{x,y})
    dist[x][y]=1

    while x~=h and y~=w do
        local q=List.popleft(que)
        if not q then break end
        local x,y=q[1],q[2]
        for i=1,4 do
            local nx,ny=x+dx[i],y+dy[i]
            local checker=(nx<1 or nx>h or ny<1 or ny>w)
            if not checker and maze[nx][ny]~="#" and dist[nx][ny]==-1 then
                dist[nx][ny]=dist[x][y]+1
                List.pushright(que,{nx,ny})
            end
        end
    end

    return dist[h][w]
end

local mindist=bfs(1,1)
if mindist>-1 then
    print(h*w-(mindist+shounter))
else
    print(mindist)
end

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


local H = io.read("*n")
local W = io.read("*n")
io.read("*l")

local map = {}
for h = 1, H do
	local str = io.read('*l')
	print(str)
	for w = 1, W do
		if string.sub(str, w, w) == '.' then
			map[h][w] = 0
		else
			map[h][w] = -1
		end
	end
end

map[1][1] = 1
local stack = {}
table.insert(stack, {1, 1})
local function unvisit(h, w)
	if not map[h] then
		return false
	end
	return map[h][w] == 0
end
while(true) do
	if not stack[1] then
		print(-1)
		break
	end
	local t = stack[1]
	local h, w, steps = t[1], t[2], map[t[1]][t[2]]
	local neightbors = {
		{h - 1, w},
		{h, w - 1},
		{h + 1, w},
		{h, w + 1}
	}
	for k, v in ipairs(neightbors) do
		if v[1] == H and v[2] == W then
			print(steps + 1)
			finded = true
			break
		end
		if unvisit(v[1], v[2]) then
			map[v[1]][v[2]] = steps + 1
			table.insert(stack, {v[1], v[2]})
		end
	end
	if finded then
		break
	end
	table.remove(stack, 1)
end


local n = io.read("*n")
local a, b = {}, {}
for i = 1, n do a[i] = io.read("*n") end
for i = 1, n do b[i] = io.read("*n") end
local asum, bsum = 0, 0
local c = {}
for i = 1, n do
  c[i] = b[i] - a[i]
  asum, bsum = asum + a[i], bsum + b[i]
end
table.sort(c)
local cnt = bsum - asum
for i = 1, n do
  if c[i] < 0 then
    cnt = cnt + c[i]
  else break end
end
print(0 <= cnt and "Yes" or "No")

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


local n = io.read("*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  a = a + 1
  b = b + 1
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end

local function getend(start)
  local len = {}
  for i = 1, n do
    len[i] = -1
  end
  len[start] = 0
  local mlenidx = 1
  local tasks = {start}
  while 0 < #tasks do
    local src = tasks[#tasks]
    table.remove(tasks)
    for i = 1, #edge[src] do
      local dst = edge[src][i]
      if len[dst] < 0 then
        len[dst] = len[src] + 1
        if len[mlenidx] < len[dst] then
          mlenidx = dst
        end
        table.insert(tasks, dst)
      end
    end
  end
  return mlenidx
end
local root = getend(1)

local parent = {}
local asked = {}
local asked_child_cnt = {}
local tasks = {}
local required = {}
for i = 1, n do
  required[i] = false
  parent[i] = 0
  asked[i] = false
  asked_child_cnt[i] = 0
  if #edge[i] == 1 then
    if i ~= root then
      table.insert(tasks, i)
    end
  end
end
local done = 0
while done < #tasks do
  done = done + 1
  local child = tasks[done]
  asked[child] = true
  if child == root then break end
  if 3 <= #edge[child] then
    required[child] = true
  end
  for i = 1, #edge[child] do
    local pcand = edge[child][i]
    if not asked[pcand] then
      parent[child] = pcand
      asked_child_cnt[pcand] = asked_child_cnt[pcand] + 1
      if asked_child_cnt[pcand] == #edge[pcand] - 1 then
        table.insert(tasks, pcand)
      end
    end
  end
end
-- for i = 1, n do
--   asked[i] = false
-- end
-- asked[root] = true
tasks[1] = edge[root][1]
tasknum = 1
done = 0
local ret = 1
while done < tasknum do
  done = done + 1
  local p = tasks[done]
  local required_cnt = #edge[p] - 2
  for i = 1, #edge[p] do
    local c = edge[p][i]
    if c ~= parent[p] then
      if required[c] then
        required_cnt = required_cnt - 1
      end
      tasknum = tasknum + 1
      tasks[tasknum] = c
    end
  end
  if 0 < required_cnt then
    ret = ret + required_cnt
  end
end
print(ret)

Score : 900 points

Problem Statement

We have a tree with N vertices.
The vertices are numbered 0 through N - 1, and the i-th edge (0 ≤ i < N - 1) comnnects Vertex a_i and b_i.
For each pair of vertices u and v (0 ≤ u, v < N), we define the distance d(u, v) as the number of edges in the path u-v.

It is expected that one of the vertices will be invaded by aliens from outer space.
Snuke wants to immediately identify that vertex when the invasion happens.
To do so, he has decided to install an antenna on some vertices.

First, he decides the number of antennas, K (1 ≤ K ≤ N).
Then, he chooses K different vertices, x_0, x_1, ..., x_{K - 1}, on which he installs Antenna 0, 1, ..., K - 1, respectively.
If Vertex v is invaded by aliens, Antenna k (0 ≤ k < K) will output the distance d(x_k, v).
Based on these K outputs, Snuke will identify the vertex that is invaded.
Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:

For each vertex u (0 ≤ u < N), consider the vector (d(x_0, u), ..., d(x_{K - 1}, u)). These N vectors are distinct.

Find the minumum value of K, the number of antennas, when the condition is satisfied.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i, b_i < N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_0 b_0
a_1 b_1
:
a_{N - 2} b_{N - 2}

Output

Print the minumum value of K, the number of antennas, when the condition is satisfied.

Sample Input 1

5
0 1
0 2
0 3
3 4

Sample Output 1

2

For example, install an antenna on Vertex 1 and 3.
Then, the following five vectors are distinct:

(d(1, 0), d(3, 0)) = (1, 1)

(d(1, 1), d(3, 1)) = (0, 2)

(d(1, 2), d(3, 2)) = (2, 2)

(d(1, 3), d(3, 3)) = (2, 0)

(d(1, 4), d(3, 4)) = (3, 1)

Sample Input 2

2
0 1

Sample Output 2

1

For example, install an antenna on Vertex 0.

Sample Input 3

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

Sample Output 3

3

For example, install an antenna on Vertex 0, 4, 9.


n=io.read("*n","*l")
a={}
for i=1,2 do
    a[i]={}
    for j=1,n do
        a[i][j]=io.read("*n")
    end
end

total=0
for i=1,n do
    count=0
    for j=1,i do
        count=count+a[1][j]
    end
    for k=i,n do
        count=count+a[2][k]
    end
    total=math.max(count,total)
end
print(total)

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


N=io.read("n")
A={}
for i=1,2 do
  A[i]={}
  for j=1,N do
    A[i][j]=io.read("n")
  end
end
local total={}
for i=1,N do
  local a1=0
  local a2=0
  for j=1,i do
    a1=a1+A[1][j]
  end
  for k=i,N do
    a2=a2+A[2][k]
  end
  total[i]=a1+a2
end
table.sort(total)
print(total[N])


local n, m = io.read("*n", "*n")
local edge = {}
local pos = {}
for i = 1, n do
  edge[i] = {}
  pos[i] = false
end
for i = 1, m do
  local l, r, d = io.read("*n", "*n", "*n")
  edge[l][r] = d
  edge[r][l] = -d
end
local f = true
local function solve(start)
  local tasks = {start}
  if not pos[start] then pos[start] = 0 end
  while 0 < #tasks do
    local src = tasks[#tasks]
    table.remove(tasks)
    for dst, len in pairs(edge[src]) do
      if pos[dst] then
        if pos[dst] ~= pos[src] + len then
          f = false
          break
        end
      else
        pos[dst] = pos[src] + len
        edge[dst][src] = nil
        table.insert(tasks, dst)
      end
    end
    edge[src] = {}
    if not f then break end
  end
end

for i = 1, n do
  solve(i)
  if not f then break end
end
print(f and "Yes" or "No")

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

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

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

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

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


local ior = io.input()
local n, m = ior:read("*n", "*n")
local hasinfo = {}
local pos = {}
local parent = {}
for i = 1, n do parent[i] = i end
for i = 1, n do hasinfo[i], pos[i] = false, 0 end
local isok = true

function getroot(idx)
  while(parent[idx] ~= idx) do
    idx = parent[idx]
  end
  return idx
end

for i = 1, m do
  local l, r, d = ior:read("*n", "*n", "*n")
  if(isok) then
    local lr, rr = getroot(l), getroot(r)
    parent[l], parent[r], parent[rr] = lr, lr, lr
    if(hasinfo[l] and hasinfo[r]) then
      isok = (d == pos[r] - pos[l])
    elseif(hasinfo[l]) then
      hasinfo[r] = true
      pos[r] = pos[l] + d
    elseif(hasinfo[r]) then
      hasinfo[l] = true
      pos[l] = pos[r] - d
    else
      hasinfo[l], hasinfo[r] = true, true
      pos[l] = 0
      pos[r] = d
    end
  end
end
if(isok) then
  local parents = {}
  for i = 1, n do
    local root = getroot(i)
    if(parents[root] == nil) then
      parents[root] = {}
      parents[root].min = pos[i]
      parents[root].max = pos[i]
    else
      parents[root].min = math.min(parents[root].min, pos[i])
      parents[root].max = math.max(parents[root].max, pos[i])
    end
  end
  for p, val in pairs(parents) do
    if(1000000000 < val.max - val.min) then
      isok = false
      break
    end
  end
end
print(isok and "Yes" or "No")


n=io.read("*n")
t,x,y={},{},{}
for i=1,n do
    t[i],x[i],y[i]=io.read("*n","*n","*n")
end

feasible=true
for i=1,n-1 do
    manhattan=math.abs(x[i+1]-x[i])+math.abs(y[i+1]-y[i])
    time=t[i+1]-t[i]
    if time<manhattan then
        feasible=false
    end
    if time%2~=manhattan%2 then
        feasible=false
    end
end
if feasible then
    print("Yes")
else
    print("No")
end

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


N=io.read("n")
local t={0}
local x={0}
local y={0}
for i=2,N+1 do
  t[i]=io.read("n")
  x[i]=io.read("n")
  y[i]=io.read("n")
end
local res="Yes"
for i=2,N+1 do
  local k=t[i]-t[i-1]
  local s=math.abs(x[i]-x[i-1])+math.abs(y[i]-y[i-1])
  if  k<s or k%2~=s%2 then
    res="No"
    break
  end
end
print(res)


local n = io.read("*n")
local t = 0
local x = 0
local y = 0
for i = 1, n do
  local t0, x0, y0 = io.read("*n", "*n", "*n")
  local dt = t0 - t
  local dx = x0 - x
  local dy = y0 - y
  if dx + dy > dt or (dt - dx - dy) % 2 > 0 then
    print("No")
    return
  end
  t = t0
  x = x0
  y = y0
end
return print("Yes")


local times = io.read("*l")
local paths = {}
for i = 1,times do
	local tmp = {}
	for j = 1,3 do
		tmp[j] = io.read("*n")
	end
	paths[i] = tmp
end
local pos = {0,0,0}
for k,v in ipairs(paths) do
	local step = v[1] - pos[1]
	local x = v[2]
	local y = v[3]
	local dis = math.abs(x - pos[1]) + math.abs(y - pos[2])
	if dis < step  or (dis-step)%2 == 1 then
		print("No")
	end
	pos = v
end
print("Yes")


local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local curmin, curmax = 2, 2
-- a[n] = k * t[n] + l (0 <= k, 0 <= l < t[n])
-- a[n+1] = k * t[n]
if t[n] == 2 then
  for i = n, 1, -1 do
    local a = curmin
    local b = curmax + t[i] - 1
    if 1 < i then
      curmin = t[i - 1] * mce(a / t[i - 1])
      curmax = t[i - 1] * mfl(b / t[i - 1])
    else
      curmin, curmax = a, b
    end
    if curmax < curmin then break end
  end
else
  curmin = 3
end
if curmax < curmin then
  print(-1)
else
  print(curmin .. " " .. curmax)
end

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


local n = io.read("*n")
local arr
do
  local _accum_0 = { }
  local _len_0 = 1
  for i = 1, n do
    _accum_0[_len_0] = io.read("*n")
    _len_0 = _len_0 + 1
  end
  arr = _accum_0
end
table.sort(arr)
local r = 1
for i = 2, n do
  if arr[i - 1] ~= arr[i] then
    r = r + 1
  end
end
return print(r)

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


local a, b = io.read("*n", "*n", "*l")
local s = io.read()
local f = true
for i = 1, #s do
  if i == a + 1 then
    if s:sub(i, i) ~= "-" then f = false break end
  else
    local c = s:sub(i, i):byte()
    if c < 48 or 57 < c then
      f = false
      break
    end
  end
end
print(f and "Yes" or "No")

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


local n = io.read("*n")
local c, s, f = {}, {}, {}
for i = 1, n - 1 do
  c[i], s[i], f[i] = io.read("*n", "*n", "*n")
end
local mma = math.max
for i = 1, n - 1 do
  local cur = 0
  for j = i, n - 1 do
    cur = mma(s[j], ((cur + f[j] - 1) // f[j]) * f[j])
    cur = cur + c[j]
  end
  print(cur)
end
print(0)

Score : 300 points

Problem Statement

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

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

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

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

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

Here, it is guaranteed that F_i divides S_i.

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

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

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

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

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


local t={}
t[0],t[1],t[2]=0,0,0
for i=3,10^5,2 do
    local check=true
    for j=2,math.sqrt(i) do
        if i%j==0 then
            check=false
            break
        end
    end
    if check then
        for j=2,math.sqrt((i+1)//2) do
            if ((i+1)//2)%j==0 then
                check=false
                break
            end
        end
    end
    t[i]=t[i-1]+(check and 1 or 0)
    t[i+1]=t[i]
end

local q=io.read("n")
for i=1,q do
    local l,r=io.read("n","n")
    print(t[r]-t[l-1])
end

Score : 400 points

Problem Statement

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

You are given Q queries.

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

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

1
3 7

Sample Output 1

2

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

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

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

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

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

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

Sample Output 3

4
4
1
1
1
2


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
local function b_search(t,k,min,max)
	if(min>max)then return -1 end
	local mid=min+math.floor((max-min)/2)
	if(t[mid]>k)then return b_search(t,k,min,mid-1)
	elseif(t[mid]<k)then return b_search(t,k,mid+1,max)
	else return mid
	end
end

Q=io.read"*n"
l,r=reada(Q,2)

a={2}
for i=3,200000,2 do
	table.insert(a,i)
end

for i=3,math.sqrt(200000),2 do
	local b={}
	for _,v in ipairs(a)do
		if(v%i~=0 or v==i)then table.insert(b,v)end
	end
	a=b
end

s={0}
for i=1,50000 do
	if(b_search(a,i,1,#a)~=-1 and b_search(a,i*2-1,1,#a)~=-1)then
		s[i+1]=s[i]+1
	else
		s[i+1]=s[i]
	end
end

for i=1,Q do
	print(s[(r[i]+1)/2+1]-s[(l[i]+1)/2])
end


Score : 300 points

Problem Statement

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

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

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

Find the maximum possible length of the sequence.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


print(math.ceil(-math.log(io.read('n'), 2) + math.log(io.read('n'), 2) + 1e-11))


local a, b = io.read("*n", "*n")
local average = math.modf((a + b + 2 - 1) / 2)
print(average)

Score : 100 points

Problem Statement

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

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

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

Sample Input 2

7 4

Sample Output 2

6

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

Sample Input 3

5 5

Sample Output 3

5


local n=io.read("*n")
local a={}
for i=1,n do
    local number=io.read("*n")
    if not a[number] then
        a[number]=1
    else
        a[number]=a[number]+1
    end
end

local total=0
for i,j in pairs(a) do
    if i<j then
        total=total+j-i
    elseif i>j then
        total=total+j
    end
end
print(total)

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

4
3 3 3 3

Sample Output 1

1

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

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

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

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

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

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


N=io.read("n")
local a={}
local t={}
for i=1,N do
  a[i]=io.read("n")
  t[a[i]]=(t[a[i]] or 0)+1
end
local count=0
for i,v in ipairs(t) do
  if i<v then
    count=count+v-i
    elseif i>v then
      count=count+v
  end
end
print(count)


local str = io.read()
local n = #str
local startx = 0
local curlen = 0
local curIsY = false
local xlist = {}
local ylist = {}
local isfirst = true
for i = 1, n do
  if str:sub(i, i) == "F" then
    curlen = curlen + 1
  else
    if curIsY then
      table.insert(ylist, curlen)
    else
      if isfirst then
        startx = curlen
        isfirst = false
      else
        table.insert(xlist, curlen)
      end
    end
    curIsY = not curIsY
    curlen = 0
  end
end
if isfirst then
  startx = startx + curlen
else
  if curIsY then table.insert(ylist, curlen)
  else table.insert(xlist, curlen)
  end
end
local xdst, ydst = io.read("*n", "*n")
xdst = xdst - startx
local xsum, ysum = 0, 0
for i = 1, #xlist do xdst = xdst + xlist[i] end
for i = 1, #ylist do ydst = ydst + ylist[i] end

local function check(list, dst)
  if dst == 0 then return true end
  local t = {}
  local ln = #list
  for i = 1, dst do t[i] = false end
  local curmax = 0
  for i = 1, ln do
    for j = curmax, 1, -1 do
      if t[j] then
        if (j + list[i] * 2) == dst then
          return true
        elseif (j + list[i] * 2) < dst then
          t[j + list[i] * 2] = true
        end
      end
    end
    curmax = curmax + list[i] * 2
    t[list[i] * 2] = true
    if (list[i] * 2) == dst then return true end
  end
  return false
end
print(check(xlist, xdst) and check(ylist, ydst) and "Yes" or "No")

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


local str = io.read()
local n = #str
local startx = 0
local curlen = 0
local curIsY = false
local xlist = {}
local ylist = {}
local isfirst = true
for i = 1, n do
  if str:sub(i, i) == "F" then
    curlen = curlen + 1
  else
    if curIsY then
      table.insert(ylist, curlen)
    else
      if isfirst then
        startx = curlen
        isfirst = false
      else
        table.insert(xlist, curlen)
      end
    end
    curIsY = not curIsY
    curlen = 0
  end
end
if curIsY then table.insert(ylist, curlen)
else table.insert(xlist, curlen)
end
local xdst, ydst = io.read("*n", "*n")
xdst = xdst - startx
local xsum, ysum = 0, 0
for i = 1, #xlist do xdst = xdst + xlist[i] end
for i = 1, #ylist do ydst = ydst + ylist[i] end

local function check(list, dst)
  if dst == 0 then return true end
  local t = {}
  local ln = #list
  for i = 1, dst do t[i] = false end
  local curmax = 0
  for i = 1, ln do
    for j = curmax, 1, -1 do
      if (j + list[i] * 2) == dst then
        return true
      elseif (j + list[i] * 2) < dst then
        t[j + list[i] * 2] = true
      end
    end
    curmax = curmax + list[i] * 2
    t[list[i] * 2] = true
    if (list[i] * 2) == dst then return true end
  end
  return false
end
print(check(xlist, xdst) and check(ylist, ydst) and "Yes" or "No")


S=io.read()
x=io.read()
y=io.read()
local xcount=0
local ycount=0
local swap=0
for i=1,#S do
  if string.sub(S,i,i)=="F" then
    xcount=xcount+1
  end
  if string.sub(S,i,i)=="T" then
    xcount,ycount=ycount,xcount
    swap=swap+1
  end
end
if swap%2==1 then
  xcount,ycount=ycount,xcount
end
if xcount>=math.abs(x) and (xcount-math.abs(x))%2==0  and ycount>=math.abs(y) and (ycount-math.abs(y))%2==0 then
  print("Yes")
  else
  print("No")
end


local str = io.read()
local finalA = io.read("*n")
local finalB = io.read("*n")

local tb = {}
for i = 1, string.len(str) do
    table.insert(tb,string.sub(str,i,i))
end

local dirTb = {
    {0,1},
    {0,-1},
    {-1,0},
    {1,0}
}

local turnTb = {
    {3,4},
    {3,4},
    {1,2},
    {1,2}
}
local function digui( a,b,idx,curDir )
    -- print(a.." "..b)
    if a == finalA and b == finalB and idx == #tb + 1 then
        return true
    end

    if idx > #tb then
        return false
    end

    if math.abs(a) > 8000 or math.abs(b) > 8000 then
        return false
    end
    -- print(a.." "..b)

    if tb[idx] == 'F' then
        return digui(a+dirTb[curDir][1],b+dirTb[curDir][2],idx+1,curDir)
    else
        return digui(a,b,idx+1,turnTb[curDir][1]) or digui(a,b,idx+1,turnTb[curDir][2])
    end

end

local f = digui(0,0,1,4)
print(f and "Yes" or "No")


local str = io.read()
local finalA = io.read("*n")
local finalB = io.read("*n")
-- io.read("*l")

local tb = {}
for i = 1, string.len(str) do
    table.insert(tb,string.sub(str,i,i))
end

local dirTb = {
    {0,1},
    {0,-1},
    {-1,0},
    {1,0}
}

local turnTb = {
    {3,4},
    {3,4},
    {1,2},
    {1,2}
}
local function digui( a,b,idx,curDir )
    -- print(a.." "..b)
    if a == finalA and b == finalB and idx == #tb + 1 then
        return true
    end

    if idx > #tb then
        return false
    end

    if math.abs(a) > 8000 or math.abs(b) > 8000 then
        return false
    end
    -- print(a.." "..b)

    if tb[idx] == 'F' then
        return digui(a+dirTb[curDir][1],b+dirTb[curDir][2],idx+1,curDir)
    else
        return digui(a,b,idx+1,turnTb[curDir][1]) or digui(a,b,idx+1,turnTb[curDir][2])
    end

end

local f = digui(0,0,1,4)
print(f and "yes" or "no")


local str = io.read("*l")
io.read("*l")
local finalA,finalB = io.read("*n","*n")

local tb = {}
for i = 1, string.len(str) do
    table.insert(tb,string.sub(str,i,i))
end

local dirTb = {
    {0,1},
    {0,-1},
    {-1,0},
    {1,0}
}

local turnTb = {
    {3,4},
    {3,4},
    {1,2},
    {1,2}
}
local function digui( a,b,idx,curDir )
    -- print(a.." "..b)
    if a == finalA and b == finalB and idx == #tb + 1 then
        return true
    end

    if idx > #tb then
        return false
    end

    if math.abs(a) > #tb or math.abs(b) > #tb then
        return false
    end

    if tb[idx] == 'F' then
        return digui(a+dirTb[curDir][1],b+dirTb[curDir][2],idx+1,curDir)
    else
        return digui(a,b,idx+1,turnTb[curDir][1]) or digui(a,b,idx+1,turnTb[curDir][2])
    end

end

local f = digui(0,0,1,4)
print(f and "yes" or "no")


local N = io.read("*n")
local A = {}
for i = 1, N do
   A[i] = io.read("*n")
end

local counter = 0
while true do
    for i = 1, N do
        if A[i]%2 == 0 then
            A[i] = A[i]/2
        else
            break
        end
    end
    counter = counter + 1
end

print(counter)

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
8 12 40

Sample Output 1

2

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

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

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

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

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

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


local ior = io.read
local n = ior("*n")

local function get2(x)
  local z = 0
  while(x % 2 == 0) do
    x = x // 2
    z = z + 1
  end
  return z
end

local ret = get2(ior("*n"))

for i = 2, n do
  ret = math.min(ret, get2(ior("*n")))
end
print(ret)


local r = 99999999
local n = tonumber(io.read("*l"))
for _ in io.read("*l"):gmatch("%d+") do
  local r0 = 0
  local k = tonumber(_)
  while k % 2 < 1 do
    r0 = r0 + 1
    k = k / 2
  end
  if r > r0 then
    r = r0
  end
end
return print(r)


n, k = io.read('n'), io.read('n')
bucket = {}
coll = {}

for i = 1, n do
    local a_i = io.read('n')
    bucket[a_i] = (bucket[a_i] or 0) + 1
end

for _, v in ipairs(bucket) do
    coll[#coll + 1] = v
end

table.sort(coll)

ans = n
for i = 1, math.min(k, #coll) do
    ans = ans - coll[#coll - i + 1]
end

print(ans)

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

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

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

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

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

3


Score : 100 points

Problem Statement

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

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

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

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

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

Sample Input 1

7 17 120

Sample Output 1

119

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

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

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


N=io.read("n")
local sum=0
local mark=N
while N>0 do
  sum=sum+N%10
  N=N//10
end
if mark%sum==0 then
  print("Yes")
  else
  print("No")
end

Score : 200 points

Problem Statement

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

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

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

12

Sample Output 1

Yes

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

Sample Input 2

57

Sample Output 2

No

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

Sample Input 3

148

Sample Output 3

No