local mma, mmi = math.max, math.min
local n = io.read("*n", "*l")
local t = {}
local tmap = {}
for i = 1, 2 * n do tmap[i] = 0 end
for i = 1, 2 * n do
  local str = io.read()
  local tp, num = str:match("(%w) (%d+)")
  num = tonumber(num)
  if tp == "B" then num = num + n end
  t[i] = num
  tmap[num] = i
end

local wcnt, bcnt = {}, {}
for i = 1, n do
  wcnt[i] = {}
  bcnt[i] = {}
  local wc, bc = 0, 0
  for j = 1, 2 * n do
    if t[j] <= i then
      wc = wc + 1
    end
    if n < t[j] and t[j] - n <= i then
      bc = bc + 1
    end
    wcnt[i][j] = wc
    bcnt[i][j] = bc
  end
end

local d = {}
for i = 1, n + 1 do
  d[i] = {}
  for j = 1, n + 1 do
    d[i][j] = 1000000007
  end
end
d[1][1] = 0
for j = 1, n do
  d[1][j + 1] = d[1][j] + tmap[j] - wcnt[j][tmap[j]]
end
for i = 1, n do
  d[i + 1][1] = d[i][1] + tmap[n + i] - bcnt[i][tmap[n + i]]
  for j = 1, n do
    local sub1 = wcnt[j][tmap[n + i]]
    local sub2 = bcnt[i][tmap[n + i]]

    local sub3 = wcnt[j][tmap[j]]
    local sub4 = bcnt[i][tmap[j]]
    d[i + 1][j + 1] = mmi(d[i][j + 1] + tmap[n + i] - sub1 - sub2, d[i + 1][j] + tmap[j] - sub3 - sub4)
  end
end
print(d[n + 1][n + 1])

Score : 600 points

Problem Statement

There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball.
The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i.
c_i = W represents the ball is white; c_i = B represents the ball is black.

Takahashi the human wants to achieve the following objective:

For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.

For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.

In order to achieve this, he can perform the following operation:

Swap two adjacent balls.

Find the minimum number of operations required to achieve the objective.

Constraints

1 ≤ N ≤ 2000

1 ≤ a_i ≤ N

c_i = W or c_i = B.

If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).

Input

Input is given from Standard Input in the following format:

N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3
B 1
W 2
B 3
W 1
W 3
B 2

Sample Output 1

4

The objective can be achieved in four operations, for example, as follows:

Swap the black 3 and white 1.

Swap the white 1 and white 2.

Swap the black 3 and white 3.

Swap the black 3 and black 2.

Sample Input 2

4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1

Sample Output 2

18

Sample Input 3

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

Sample Output 3

41


local n = io.read("*n", "*l")
local t = {}
for i = 1, 2 * n do
  local str = io.read()
  local tp, num = str:match("(%w) (%d+)")
  num = tonumber(num)
  if tp == "B" then num = num + n end
  t[i] = num
end
local curw, curb = 1, n + 1
local len = 0
for i = 1, 2 * n do
  for j = 1, #t do
    if t[j] == curw or t[j] == curb then
      len = len + j - 1
      if t[j] == curw then curw = curw + 1 else curb = curb + 1 end
      table.remove(t, j)
      break
    end
  end
end
print(len)

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

Submitted source-code file size, in bytes.

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

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


Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


A=io.read("n")
B=io.read("n")
C=io.read("n")
K=io.read("n")
max=math.max(A,B)
max=math.max(max,C)
print(A+B+C-max+math.floor(max*math.pow(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.


a, b, c = io.read('n'), io.read('n'), io.read('n')
print(a + b + c + math.max(a, b, c) * ((1 << io.read('n')) - 1))


h,w=io.read("*n","*n")
grid={}
for i=1,h+2 do
    grid[i]={}
    if i~=1 and i~=h+2 then
        s=io.read()
    end
    for j=1,w+2 do
        if i==1 or i==h+2 then
            grid[i][j]="."
        else
            if j==1 or j==w+2 then
                grid[i][j]="."
            else
                grid[i][j]=s:sub(j-1,j-1)
            end
        end
    end
end

feasible=true
for i=2,h+1 do
    for j=2,w+1 do
        if grid[i][j-1]..grid[i][j]..grid[i][j+1]==".#." and grid[i-1][j]..grid[i][j]..grid[i+1][j]==".#." then
            feasible=false
        end
    end
end
if feasible then
    print("Yes")
else
    print("No")
end

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


local n=io.read("n")
local counter=0
local sum=0
local map={[0]=1}
for i=1,n do
    sum=sum+io.read("n")
    local check=(map[sum] or 0)
    counter=counter+check
    map[sum]=check+1
end
print(counter)

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.


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

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

local function check(i, j)
  for k = 1, n do
    local kcol = k + i - 1
    if n < kcol then kcol = kcol - n end
    if row[k] ~= colmap[kcol][j] then return 0 end
  end
  return 1
end

local ret = 0
for i = 1, n do
  for j = 1, n do
    ret = ret + check(i, j)
    local tmp = row[1]
    for k = 2, n do
      row[k - 1] = row[k]
    end
    row[n] = tmp
  end
  if 1 < n then
    for k = 1, n do
      row[k] = row[k]:sub(n, n) .. row[k]:sub(1, n - 1)
    end
  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 row = {}
for i = 1, n do
  row[i] = io.read()
end
local col = {}
for i = 1, n do col[i] = "" end
for i = 1, n do
  for j = 1, n do
    col[i] = col[i] .. row[j]:sub(i, i)
  end
end

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

local function check(j)
  for i = 1, n do
    if row[i] ~= colmap[i][j] then return 0 end
  end
  return 1
end

local ret = 0
for i = 1, n do
  for j = 1, n do
    ret = ret + check(j)
    local tmp = row[1]
    for k = 2, n do
      row[k - 1] = row[k]
    end
    row[n] = tmp
  end
  local tmp2 = colmap[1]
  for k = 2, n do
    colmap[k - 1] = colmap[k]
  end
  colmap[n] = tmp2
  for k = 1, n do
    row[k] = row[k]:sub(n, n) .. row[k]:sub(1, n - 1)
  end
end
print(ret)


a,b,c=io.read(1,1,1)
function t(x) return x=='o' and 100 or 0 end
print(700+t(a)+t(b)+t(c))

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.


a,b,c,x,y=io.read("*n","*n","*n","*n","*n")
if a+b<=c*2 then
    print(a*x+b*y)
else
    if x>=y then
        print(math.min(c*y*2+a*(x-y), c*x*2))
    else
        print(math.min(c*x*2+b*(y-x), c*y*2))
    end
end

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


local a, b, c, x, y = io.read("*n", "*n", "*n", "*n", "*n")
if(a + b <= c * 2) then
  print(a * x + b * y)
else
  local both = math.min(x, y)
  local price = both * c  * 2
  x, y = x - both, y - both
  price = price + x * math.min(c * 2, a)
  price = price + y * math.min(c * 2, b)
  print(price)
end


A=io.read("n")
B=io.read("n")
C=io.read("n")
X=io.read("n")
Y=io.read("n")
local cost=0
if C*2<=math.min(A,B) then
  cost=C*2*math.max(X,Y)
  elseif C*2>=A+B then
    cost=A*X+B*Y
  elseif C*2<A+B and C*2>=math.max(A,B) then
    if X>Y then
      cost=C*2*math.min(X,Y)+math.abs(X-Y)*A
    else
      cost=C*2*math.min(X,Y)+math.abs(X-Y)*B
    end
  elseif C*2>math.min(A,B) and C*2<math.max(A,B) then
    if A<B and X<Y or A>B and X>Y then
      cost=C*2*math.max(X,Y)
    elseif A<B and X>Y then
      cost=C*2*Y+A*(X-Y)
    elseif  A>B and X<Y then
      cost=C*2*X+B*(Y-X)
    end
end


local n = io.read("*n")
local c = 0LL + io.read("*n")
local x, v = {}, {}
for i = 1, n do
  x[i] = 0LL + io.read("*n")
  v[i] = 0LL + io.read("*n")
end

local score_forward = {v[1] - x[1]}
do
  local cur = score_forward[1]
  for i = 2, n do
    cur = cur + v[i] - (x[i] - x[i - 1])
    if score_forward[i - 1] < cur then
      score_forward[i] = cur
    else
      score_forward[i] = score_forward[i - 1]
    end
  end
end

local score_backward = {}
do
  for i = 1, n do score_backward[i] = 0LL end
  score_backward[n] = v[n] - (c - x[n])
  local cur = score_backward[n]
  for i = n - 1, 1, -1 do
    cur = cur + v[i] - (x[i + 1] - x[i])
    if score_backward[i + 1] < cur then
      score_backward[i] = cur
    else
      score_backward[i] = score_backward[i + 1]
    end
  end
end

local ret = 0LL
for i = 1, n do
  local scorecand = score_forward[i]
  if ret < scorecand then
    ret = scorecand
  end
  if i < n then
    scorecand = scorecand - x[i] + score_backward[i + 1]
    if ret < scorecand then
      ret = scorecand
    end
  end
end
for i = n, 1, -1 do
  local scorecand = score_backward[i]
  if ret < scorecand then
    ret = scorecand
  end
  if 1 < i then
    scorecand = scorecand - (c - x[i]) + score_forward[i - 1]
    if ret < scorecand then
      ret = scorecand
    end
  end
end
local str = tostring(ret):gsub("LL", "")
print(str)

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


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.


N=io.read("n")
X={}
B={}
for i=1,N do
  X[i]=io.read("n")
  B[i]=X[i]
end
table.sort(B)
left=B[N//2]
right=B[N//2+1]
for i=1,N do
  if X[i]<=left then
    print(right)
  	elseif X[i]>=right then
    print(left)
  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 N=io.read("n")
local a={}
for i=1,N do
  a[i]=io.read("n")
end
function c(n,m)
  local res=0
  local s=1
  local k=1
  for i=n,n-m+1,-1 do
    s=s*i
  end
  for i=1,m do
    k=k*i
  end
  return s//k
end
table.sort(a,function (a,b) return a>b end)
local first=a[1]
local second=a[2]
local total=c(a[1],a[2])
for i=1,N-1 do
  for j=i+1,N do
    if c(a[i],a[j])>total then
      total=c(a[i],a[j])
      first=a[i]
      second=a[j]
    end
  end
end
print(first,second)

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


a,b,k=io.read("*n","*n","*n")

for i=1, k do
    if a+i-1<=b then
        print(a+i-1)
    end
end

for i=k, 1, -1 do
    if b-i>=a+k-1 then
        print(b-i+1)
    end
end

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


Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23


A=io.read("n")
B=io.read("n")
C=io.read("n")
if A>B then
  A,B=B,A
end
if B>C then
  B,C=C,B
  if A>B then
    A,B=B,A
  end
end
local count=0
if (B-A)%2==0 then
  count=(B-A)//2+C-B
  else count=(B+1-A)//2+C-B+1
end
print(count)


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) then
                print(s:sub(1,#s-1)..c)
                break
            end
        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


local mmi, mma = math.min, math.max
local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a = {}
local b = {}
local kval = {}
for i = 1, n do
  a[i] = {}
  local z = io.read("*n")
  a[i][z] = true
  kval[i] = 100
end
for i = 1, n do
  b[i] = io.read("*n")
end
local valid = true
for i = 1, n do
  local at = next(a[i])
  local z = at - b[i]
  local min = b[i] + 1
  if 0 < z and z < min then
    valid = false
  end
  for j = min, z do
    if z % j == 0 then
      kval[i] = j
      break
    end
  end
end
if not valid then
  print(-1)
  os.exit()
end
local function flush()
  for i = n, 1, -1 do
    local found = false
    for av, _u in pairs(a[i]) do
      if b[i] == av then
        found = true
        break
      end
    end
    if found then
      table.remove(a, i)
      table.remove(b, i)
      table.remove(kval, i)
    end
  end
  n = #a
end
local ret = {}
while true do
  flush()
  if n == 0 then
    break
  end
  local maxval = kval[1]
  for i = 2, n do
    maxval = mma(maxval, kval[i])
  end
  assert(maxval < 100)
  table.insert(ret, maxval)
  for i = 1, n do
    local add = {}
    for av, _u in pairs(a[i]) do
      local z = av % maxval
      if b[i] <= z then
        table.insert(add, z)
      end
    end
    for j = 1, #add do
      local v = add[j]
      a[i][v] = true
      local z = v - b[i]
      local min = b[i] + 1
      for s = min, z do
        if z % s == 0 then
          kval[i] = mmi(kval[i], s)
          break
        end
      end
    end
  end
end
local ans = 0LL
for i = 1, #ret do
  local v = ret[i]
  local z = 1LL
  for j = 1, v do
    z = z * 2LL
  end
  ans = ans + z
end
local str = tostring(ans):gsub("LL", "")
print(str)

Score : 700 points

Problem Statement

Aoki is playing with a sequence of numbers a_{1}, a_{2}, ..., a_{N}. Every second, he performs the following operation :

Choose a positive integer k. For each element of the sequence v, Aoki may choose to replace v with its remainder when divided by k, or do nothing with v. The cost of this operation is 2^{k} (regardless of how many elements he changes).

Aoki wants to turn the sequence into b_{1}, b_{2}, ..., b_{N} (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.

Constraints

1 \leq N \leq 50

0 \leq a_{i}, b_{i} \leq 50

All values in the input 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

Print the minimum cost required to turn the original sequence into b_{1}, b_{2}, ..., b_{N}. If the task is impossible, output -1 instead.

Sample Input 1

3
19 10 14
0 3 4

Sample Output 1

160

Here's a possible sequence of operations :

Choose k = 7. Replace 19 with 5, 10 with 3 and do nothing to 14. The sequence is now 5, 3, 14.

Choose k = 5. Replace 5 with 0, do nothing to 3 and replace 14 with 4. The sequence is now 0, 3, 4.

The total cost is 2^{7} + 2^{5} = 160.

Sample Input 2

3
19 15 14
0 0 0

Sample Output 2

2

Aoki can just choose k = 1 and turn everything into 0. The cost is 2^{1} = 2.

Sample Input 3

2
8 13
5 13

Sample Output 3

-1

The task is impossible because we can never turn 8 into 5 using the given operation.

Sample Input 4

4
2 0 1 8
2 0 1 8

Sample Output 4

0

Aoki doesn't need to do anything here. The cost is 0.

Sample Input 5

1
50
13

Sample Output 5

137438953472

Beware of overflow issues.


local mmi, mma = math.min, math.max
local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a = {}
local kval = {}
for i = 1, n do
  a[i] = {}
  local z = io.read("*n")
  a[i][z] = true
  kval[i] = 100
end
local b = {}
for i = 1, n do
  b[i] = io.read("*n")
end
local valid = true
for i = 1, n do
  local at = next(a[i])
  local z = at - b[i]
  local min = mce((b[i] + 1) / 2)
  if 0 < z and z < min then
    valid = false
  end
  for j = min, z do
    if z % j == 0 then
      kval[i] = j
      break
    end
  end
end
if not valid then
  print(-1)
  os.exit()
end
local function flush()
  for i = n, 1, -1 do
    local found = false
    for av, _u in pairs(a[i]) do
      if b[i] == av then
        found = true
        break
      end
    end
    if found then
      table.remove(a, i)
      table.remove(b, i)
      table.remove(kval, i)
    end
  end
  n = #a
end
local ret = {}
while true do
  flush()
  if n == 0 then
    break
  end
  local maxval = kval[1]
  for i = 2, n do
    maxval = mma(maxval, kval[i])
  end
  table.insert(ret, maxval)
  for i = 1, n do
    local add = {}
    for av, _u in pairs(a[i]) do
      local z = av % maxval
      if b[i] <= z then
        table.insert(add, z)
      end
    end
    for j = 1, #add do
      local v = add[j]
      a[i][v] = true
      local z = v - b[i]
      local min = mce((b[i] + 1) / 2)
      for j = min, z do
        if z % j == 0 then
          kval[i] = mmi(kval[i], j)
          break
        end
      end
    end
  end
end
local ans = 0LL
for i = 1, #ret do
  local v = ret[i]
  local z = 1LL
  for j = 1, v do
    z = z * 2LL
  end
  ans = ans + z
end
local str = tostring(ans):gsub("LL", "")
print(str)


a, b = io.read("*n", "*n")
c, d = io.read("*n", "*n")
print(math.min(a, b) + math.min(c, d))

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


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

print(math.min(a, b) + math.min(c,d))


A=io.read("n")
B=io.read("n")
C=io.read("n")
D=io.read("n")
print(math.min(A,B)+math.min(C,D))


local a = io.read()
local b = io.read()
local c = io.read()
local d = io.read()

local aa = 0
local bb = 0

if a < b then
    aa = tonumber(a)
elseif a == b then
    aa = tonumber(a)
else
    aa = tonumber(b)
end

if c < d then
    bb = tonumber(c)
elseif c == d then
    bb = tonumber(c)
else
    bb = tonumber(d)
end

print(aa+bb)


local mab = math.abs
local n = io.read("*n", "*l")
local str = io.read()
local a = {}
for v in str:gmatch("%-?%d+") do
  table.insert(a, tonumber(v))
end
local sum = mab(a[1]) + mab(a[n])
for i = 1, n - 1 do
  sum = sum + mab(a[i] - a[i + 1])
end
print(sum - mab(a[1]) - mab(a[1] - a[2]) + mab(a[2]))
for i = 2, n - 1 do
  print(sum - mab(a[i] - a[i - 1]) - mab(a[i] - a[i + 1]) + mab(a[i - 1] - a[i + 1]))
end
print(sum - mab(a[n]) - mab(a[n] - a[n - 1]) + mab(a[n - 1]))

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288


N=io.read("n")
A={}
A[1]=0
for i=2,N+1 do
  A[i]=io.read("n")
end
table.insert(A,0)
local total=0
for i=2,N+2 do
  total=total+math.abs(A[i]-A[i-1])
end
for i=2,N+1 do
  if A[i]>math.min(A[i-1],A[i+1]) and A[i]<math.max(A[i-1],A[i+1]) then
    print(total)
    elseif A[i]<math.min(A[i-1],A[i+1]) then
      print(total-2*(math.min(A[i-1],A[i+1])-A[i]))
      elseif  A[i]>math.max(A[i-1],A[i+1]) then
        print(total-2*(A[i]-math.max(A[i-1],A[i+1])))
  end
end


N=io.read("n")
A={}
for i=2,N+1 do
  A[i]=io.read("n")
end
A[1]=0
table.insert(A,0)
local total=0
for i=2,N do
  for j=2,N+1 do
    local g=0
    local k=math.abs(A[j+g]-A[j-1+g])
    if j==i then
      k=math.abs(A[j+1]-A[j-1])
      g=1
    end
    total=total+k
  end
  print(total)
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


print((function (f, s)
	return f(s)
end)(function (s)
	local a,b,c = s:match("(%d+)%s(%d+)%s(%d+)")
	return tonumber(a)+tonumber(b) < tonumber(c) and "No" or "Yes"
end, io.stdin:read("*l")))


print((function (f, s)
	return f(s)
end)(function (s)
	local a,b,c = s:match("(%d+)%s(%d+)%s(%d+)")
	return a+b < c and "No" or "Yes"
end, io.stdin:read("*l")))


function split(text, delimiter)
  if text.find(text, delimiter) == nil then
    return { text }
  end

  local splited_texts = {}
  local last_position

  for splited_text, position in text:gmatch("(.-)"..delimiter.."()") do
    table.insert(splited_texts, splited_text)
    last_position = position
  end
  table.insert(splited_texts, string.sub(text, last_position))
  return splited_texts
end

local a = io.read()
local list = split(a, " ")
local have_money = tonumber(list[1]) + tonumber(list[2])
local price = tonumber(list[3])

if (have_money-price) > 0 then
    print("Yes")
else
    print("No")
end


function split(text, delimiter)
  if text.find(text, delimiter) == nil then
    return { text }
  end

  local splited_texts = {}
  local last_position

  for splited_text, position in text:gmatch("(.-)"..delimiter.."()") do
    table.insert(splited_texts, splited_text)
    last_position = position
  end
  table.insert(splited_texts, string.sub(text, last_position))
  return splited_texts
end

local a = io.read()
local list = split(a, " ")
local have_money = tonumber(list[1]) + tonumber(list[2])
local price = tonumber(list[3])

if (have_money-price) > 0 then
    print("yes")
else
    print("no")
end


a = io.read()
b = io.read()
c = io.read()
io.write(a:sub(1, 1))
io.write(b:sub(2, 2))
io.write(c:sub(3, 3))
io.write("\n")

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 str1 = io.read()
local str2 = io.read()

local count = 0
for i = tonumber(str1), tonumber(str2) do
    if(tonumber(i) == tonumber(string.reverse(i))) 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


local str1 = io.read()
local str2 = io.read()

local len = (str2) - tonumber(str1)
local count = 0
for i = tonumber(str1), tonumber(str2) do
    if(tonumber(i) == tonumber(string.reverse(i))) then
        count = count + 1
    end
end
print(count)


local n, m = io.read("*n", "*n")
if n == 1 then
  if m == 1 then
    print(1)
  else
    print(m - 2)
  end
elseif n == 2 then
  print(0)
else
  if m == 1 then
    print(n - 2)
  elseif m == 2 then
    print(0)
  else
    print((n - 2) * (m - 2))
  end
end

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


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


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

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


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)
  local rem = n % b
  if(0 < rem) then
    if(k == 0) then cnt = cnt + rem
    else cnt = cnt + mm(0, rem - k + 1)
    end
  end
end
print(cnt)


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


local N=io.read("n")
local t={}
for i=1,N do
  local s=io.read()
  t[s]=t[s] or true
end
local count=0
for i in pairs(t) do
  count=count+1
end
if count==3 then
  print("Three")
  elseif count==4 then
  print("Four")
end

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


local N=io.read()
local t={}
for i=1,N do
  local s=io.read()
  t[s]=t[s] or true
end
local count=0
for i in pairs(t) do
  count=count+1
end
if count==3 then
  print("Three")
  elseif count==4 then
  print("Four")
end


local function split(text, delimiter)
   if text.find(text, delimiter) == nil then
     return { text }
   end

   local splited_texts = {}
   local last_position

   for splited_text, position in text:gmatch("(.-)"..delimiter.."()") do
     table.insert(splited_texts, splited_text)
     last_position = position
   end
   table.insert(splited_texts, string.sub(text, last_position))
   return splited_texts
end

local total = io.read()
local arares = io.read()

local color = {}
color["p"] = 0
color["w"] = 0
color["g"] = 0
color["y"] = 0

local arare_list = split(arares, " ")
for k,v in pairs(arare_list) do
  if v == "P" then
    color["p"] = color["p"] + 1
  elseif v == "W" then
    color["w"] = color["w"] + 1
  elseif v == "G" then
    color["g"] = color["g"] + 1
  elseif v == "Y" then
    color["y"] = color["y"] + 1
  end
end

local number = 0
if color["p"] > 0 then
 number = number + 1
end
if color["w"] > 0 then
 number = number + 1
end
if color["g"] > 0 then
 number = number + 1
end
if color["y"] > 0 then
 number = number + 1
end

if number == 3 then
  print("Three")
elseif number == 4 then
  print("Four")
end


n=io.read("*n","*l")
s={}
for i=1,n do
    initial=io.read():sub(1,1)
    if initial=="M" or initial=="A" or initial=="R" or initial=="C" or initial=="H" then
        if not s[initial] then
            s[initial]=1
        else
            s[initial]=s[initial]+1
        end
    end
end

combination=0
for i,_ in pairs(s) do
    for j,_ in pairs(s) do
        for k,_ in pairs(s) do
            if i<j and j<k then
                combination=combination+s[i]*s[j]*s[k]
            end
        end
    end
end
print(combination)

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

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 N, A = io.read("*n", "*n")

local can_pay = false
local remainder = N % 500
if remainder - A <= 0 then
    can_pay = true
end

print(can_pay and "Yes" or "No")

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

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

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


n, a = io.read("*n", "*n")
n = n % 500
if(n <= a) then print("Yes") else print("No") end


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

local alice_sum, bob_sum = 0, 0
table.sort(a, function(x, y) return x > y end)
for i = 1, n do
    if i % 2 == 1 then
        alice_sum = alice_sum + a[i]
    else
        bob_sum   = bob_sum   + a[i]
    end
end

print(alice_sum - bob_sum)

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

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

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


local n = io.read("*n")
a = {}
local sum = 0
for i = 1, n do
    local k = io.read("*n")
    a[i] = k
    sum = sum + k
end
table.sort(a, function(x, y) return x > y end)
local alice = 0
for i = 1, n, 2 do
    alice = alice + a[i]
end
print(alice * 2 - sum)


local n = io.read("*n")
io.read("*l")
local arr
do
  local _accum_0 = { }
  local _len_0 = 1
  for e in io.read("*l"):gmatch("%d+") do
    _accum_0[_len_0] = tonumber(e)
    _len_0 = _len_0 + 1
  end
  arr = _accum_0
end
table.sort(arr, function(a, b)
  return a > b
end)
local r = 0
local t = 1
for _index_0 = 1, #arr do
  local e = arr[_index_0]
  r = r + (t * e)
  t = -t
end
return print(r)


--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 = {}
local whiteCount = 0
for h = 1, H do
	local str = io.read('*l')
	print(str)
	map[h] = {}
	for w = 1, W do
		if string.sub(str, w, w) == '.' then
			whiteCount = whiteCount + 1
			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(whiteCount - (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


a, b = io.read("*n", "*n")
print(a % b == 0 and -1 or a)

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


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

counter=0
for i=1,n do
    if a[i]<b[i] then
        counter=counter+(b[i]-a[i])//2
    else
        counter=counter-(a[i]-b[i])
    end
end

if counter<0 then
    print("No")
else
    print("Yes")
end

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


Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


local x = io.read("*l")
local a = io.read("*l")
local b = io.read("*l")
local change = x - a

while true do
    if change -b < 0 then
        break
    end
    change = change - b
end

print(math.ceil(change))


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

total=0
for i=2,n+1 do
    total=math.max(total, a[1][i]+a[2][n+1]-a[2][i-1])
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


function factorial(n)
    if (n == 0) then
        return 1
    else
        return n * factorial(n - 1)
    end
end

no_of_test_cases = io.read()
answers = {}

for i=0,no_of_test_cases-1,1
do
    answers[i] = factorial(io.read())
end

for i=0,no_of_test_cases-1,1
do
    io.write(answers[i])
    io.write('\n')
end

Score : 900 points

Problem Statement

For a positive integer n, let us define f(n) as the number of digits in base 10.

You are given an integer S.
Count the number of the pairs of positive integers (l, r) (l \leq r) such that f(l) + f(l + 1) + ... + f(r) = S, and find the count modulo 10^9 + 7.

Constraints

1 \leq S \leq 10^8

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Sample Input 1

1

Sample Output 1

9

There are nine pairs (l, r) that satisfies the condition: (1, 1), (2, 2), ..., (9, 9).

Sample Input 2

2

Sample Output 2

98

There are 98 pairs (l, r) that satisfies the condition, such as (1, 2) and (33, 33).

Sample Input 3

123

Sample Output 3

460191684

Sample Input 4

36018

Sample Output 4

966522825

Sample Input 5

1000

Sample Output 5

184984484


local n, k = io.read("*n", "*n")
local k2 = k * 2
local rows_per_col = {}
for i = 1, k2 do rows_per_col[i] = {} end
local count_per_row, count_per_row2 = {}, {}
for i = 1, k2 do
  count_per_row[i] = 0
  count_per_row2[i] = 0
end
for i = 1, n do
  local x, y, c = io.read("*n", "*n", "*l")
  if c == " W" then x = x + k end
  x, y = x % k2 + 1, y % k2 + 1
  table.insert(rows_per_col[x], y)
  if x <= k then
    count_per_row[y] = count_per_row[y] + 1
  else
    count_per_row2[y] = count_per_row2[y] + 1
  end
end
local mma = math.max
local function solve()
  local val = 0
  for i = 1, k do
    val = val + count_per_row[i] + count_per_row2[i + k]
  end
  local maxval = val
  for i = 1, k do
    val = val - count_per_row[i] + count_per_row[k + i] - count_per_row2[k + i] + count_per_row2[i]
    maxval = mma(maxval, val)
  end
  for i = 1, k do
    val = val + count_per_row[i] - count_per_row[k + i] + count_per_row2[k + i] - count_per_row2[i]
    maxval = mma(maxval, val)
  end
  return maxval
end
local ret = solve(maxval)
for i = 1, k do
  for j = 1, #rows_per_col[i] do
    count_per_row[rows_per_col[i][j]] = count_per_row[rows_per_col[i][j]] - 1
    count_per_row2[rows_per_col[i][j]] = count_per_row2[rows_per_col[i][j]] + 1
  end
  for j = 1, #rows_per_col[i + k] do
    count_per_row[rows_per_col[i + k][j]] = count_per_row[rows_per_col[i + k][j]] + 1
    count_per_row2[rows_per_col[i + k][j]] = count_per_row2[rows_per_col[i + k][j]] - 1
  end
  ret = mma(ret, solve())
end
for i = 1, k do
  for j = 1, #rows_per_col[i] do
    count_per_row[rows_per_col[i][j]] = count_per_row[rows_per_col[i][j]] + 1
    count_per_row2[rows_per_col[i][j]] = count_per_row2[rows_per_col[i][j]] - 1
  end
  for j = 1, #rows_per_col[i + k] do
    count_per_row[rows_per_col[i + k][j]] = count_per_row[rows_per_col[i + k][j]] - 1
    count_per_row2[rows_per_col[i + k][j]] = count_per_row2[rows_per_col[i + k][j]] + 1
  end
  ret = mma(ret, solve())
end
print(ret)

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


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

local tab = {{0, 0, 0}}
local len = 1

for i=1,line do
	local a = io.read("*n")
	local b = io.read("*n")
	local c = io.read("*n")
	table.insert(tab, {a, b, c})
	len = len + 1
end

local stata = "Yes"
for i=2,len do
	local cur = tab[i]
	local per = tab[i - 1]

	local v = math.abs(cur[2] - per[2]) + math.abs(cur[3] - per[3])

	if v > cur[1] - per[1] then
		stata = "No"
		break
	elseif math.abs((v - (cur[1] - per[1]))) % 2 == 1 then
		stata = "No"
		break
	end
end

print(stata)

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


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")
		return
	end
	pos = v
end
print("Yes")


local a = io.read("*number")

local function getABC()
	local tb = {false, false}
	for i=1, a do
		local s = io.read("*number")
		local x = io.read("*number")
		local y = io.read("*number")
		for j=1, s do
			r = x + y + 2*j
			if s == r then
				tb[i] = true
				break
			end
		end
	end
	return tb
end

local r = getABC()
if r[1] and r[2] then
	print("YES")
else
	print("NO")
end


local a = io.read("*number")

local function getABC()
	local result
	for i=1, a do
		local s = io.read("*number")
		local x = io.read("*number")
		local y = io.read("*number")
		for j=1, s do
			r = x + y + 2*j
			if s == r then
				result = "YES"
				return result
			end
		end
	end

	return "NO"
end

local r = getABC()
print(r)


s = io.read()
s = string.gsub(s, "2017", "2018")
print(s)

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


N=io.read("n")
a={}
for i=1,N do
  a[i]=io.read("n")
end
table.sort(a,function (a,b) return a>b end)
local count=1
for i=2,N do
  if a[i]<a[i-1] then
    count=count+1
  end
end
print(count)

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


A, B = io.read("*n", "*n", "*l")
S = io.read()
if S:match(string.rep("%d", A).."%-"..string.rep("%d", B)) and #S == A+B+1 then
    print("Yes")
else
    print("No")
end

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


A, B = io.read("*n", "*n", "*l")
S = io.read()
if string.gmatch(S, "%a+-%a+") and string.sub(S, A+1, A+1) == "-" and #S == A+B+1 then
    print("Yes")
else
    print("No")
end


a,b=io.read("n","n","l")
ok=true
for i,cc in ipairs{(io.read()):byte(1,a+1+b)} do
  if i==a+1 then
    ok=ok and cc == 45
  else
    ok=ok and cc ~= 45
  end
end
print(ok and "Yes" or "No")


X=io.read("n")
Y=io.read("n")
local count=0
while X<=Y do
  X=2*X
  count=count+1
end
print(count)

Score : 300 points

Problem Statement

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

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

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

Find the maximum possible length of the sequence.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


s = io.read('l')

ans1 = #s

for i = 1, #s do
    if s:byte(i) == 49 then
        ans1 = math.min(ans1, math.max(i, #s - i + 1))
    end
end

ans2 = #s

for i = 1, #s do
    if s:byte(i) == 48 then
        ans2 = math.min(ans2, math.max(i, #s - i + 1))
    end
end

print(math.max(ans1, ans2) - 1)

Score : 500 points

Problem Statement

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

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

Constraints

1\leq |S|\leq 10^5

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

010

Sample Output 1

2

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

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

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

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

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


a, b = io.read("*n", "*n")
print(math.ceil((a + b) / 2))

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 read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local s, t = read.ll()
s=s:totable()
t=t:totable()
table.sort(s)
table.sort(t)
s=table.concat(s, "")
t=table.concat(t, "")
t=t:reverse()
print(s<t and "Yes" or "No")

Score : 200 points

Problem Statement

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

Notes

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

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

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

For example, xy < xya and atcoder < atlas.

Constraints

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

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

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

Sample Input 1

yx
axy

Sample Output 1

Yes

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

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

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

Sample Input 3

cd
abc

Sample Output 3

No

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

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


s=io.read()
t=io.read()
S={}
T={}
for i=1,#s do
  S[i]=string.sub(s,i,i)
end
for i=1,#t do
  T[i]=string.sub(s,i,i)
end
table.sort(S)
table.sort(T,function (a,b) return a>b end)
a=table.concat(S)
b=table.concat(T)
if a<b then
  print("Yes")
  else
  print("No")
end


s=io.read()
t=io.read()
S={}
for i=1,#s do
  S[i]=string.sub(s,i,i)
end
table.sort(S)
a=table.concat(S)
if a<t then
  print("Yes")
  else
  print("No")
end


s=io.read()
t=io.read()
S={}
T={}
for i=1,#s do
  S[i]=string.sub(s,i,i)
end
for i=1,#t do
  T[i]=string.sub(t,i,i)
end
table.sort(S)
table.sort(T)
a=table.concat(S)
b=table.concat(T)
if a<b then
  print("Yes")
  else
  print("No")
end


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

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


-- F
local sub = string.sub
local insert = table.insert
s = io.read("*l")
x, y = io.read("*n", "*n", "*l")

local t1 = {}
local ok1 = false
local function dfs1(i, val)
	if ok1 then
		return true
	end
	if i == 0 then
		if val == 0 then
			ok1 = true
			return true
		end
		return false
	end
	return dfs1(i-1, val-t1[i]) or dfs1(i-1, val+t1[i])
end
local t2 = {}
local ok2 = false
local function dfs2(i, val)
	if ok2 then
		return true
	end
	if i == 0 then
		if val == 0 then
			ok2 = true
			return true
		end
		return false
	end
	return dfs2(i-1, val-t2[i]) or dfs2(i-1, val+t2[i])
end

local function solve()
	local n = 0
	local dirchange = false
	local dir = true -- x zhou
	-- first
	local st = 1
	while sub(s, st, st) == "F" do
		st = st + 1
	end
	x = x - (st - 1)
	for i = st, #s do
		local c = sub(s, i, i)
		if c == "F" then
			n = n + 1
		else
			if n ~= 0 then
				if dir then
					insert(t1, n)
				else
					insert(t2, n)
				end
				n = 0
				dirchange = false
			end
			dirchange = true
			dir = not dir
		end
	end
	if n ~= 0 then
		if dir then
			insert(t1, n)
		else
			insert(t2, n)
		end
	end
	if  dfs1(#t1, x) and dfs2(#t2, y) then
		return "Yes"
	else
		return "No"
	end
end
print(solve())


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

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 )
    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,1) or digui(0,0,1,2) or digui(0,0,1,3) or digui(0,0,1,4)
print(f and "yes" or "no")


local str = io.read("*l")
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) > #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,1) or digui(0,0,1,2) or digui(0,0,1,3) or digui(0,0,1,4)
print(f and "yes" or "no")


local read = io.read
local N, K = read("n", "n")

local number_of_types = {}
local have_types_s = {}
for i = 1, N do
	local readed = read("n")
	if have_types_s[readed] then
		local index = have_types_s[readed]
		number_of_types[index] = number_of_types[index] + 1
	else
		local new_index = #number_of_types + 1
		have_types_s[readed] = new_index
		number_of_types[new_index] = 1
	end
end

table.sort(number_of_types)

local out = 0
for i = 1, #number_of_types - K do
	out = out + number_of_types[i]
end

print(out)

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


local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  local a = io.read("*n")
  if t[a] then t[a] = t[a] + 1 else t[a] = 1 end
end
local tm = {}
for k, v in pairs(t) do
  table.insert(tm, v)
end
table.sort(tm, function(x, y) return x > y end)
if #tm <= k then print(0)
else
  local c = 0
  for i = k + 1, #tm do
    c = c + tm[i]
  end
  print(c)
end


n, k = io.read("*n", "*n")
t = {}
for i = 1, n do
    a = io.read("*n")
    if(t[a] == nil) then t[a] = 1 else t[a] = t[a] + 1 end
end
tall = {}
for k, v in pairs(t) do
    table.insert(tall, v)
end
table.sort(tall)
tot = 0
remcount = #tall - k
for i = 1, remcount do
    tot = tot + tall[i]
end
print(tot)


N=io.read("n")
K=io.read("n")
A={}
t={}
for i=1,N do
  A[i]=io.read("n")
  t[A[i]]=(t[A[i]] or 0)+1
end
local count=0
number={}
for i,v in pairs(t) do
  count=count+1
  number[count]=v
end
table.sort(number)
local res=0
for i=1,number-K do
  res=res+number[i]
end
print(res)


a, b = io.read("*n", "*n")
c = io.read("*n")
print(math.min(a * b, c))

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


T=io.read("n")
A=io.read("n")
B=io.read("n")
if T*A<=B then
  print(T*A)
  else
  print(B)
end


local flo = math.floor

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

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

local sum = 0
local num = N
while true do
	if num <= 0 then
		break
	end
	local drow = div_flo(num, 10) * 10
	sum = sum + num - drow

	num = div_flo(num, 10)
end

print((N % sum == 0) and "Yes" or "No")

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


local n = io.read("*n")
local s = 0
local t = n
while 0 < t do
  s = s + t % 10
  t = math.floor(t / 10)
end
print(n % s == 0 and "Yes" or "No")