local function use_strict()
    local mt = {
        __index = function(t,k) error("_G __index:" .. k) end,
        __newindex = function(t,k,v) error("_G __newindex:" .. k) end
    }
    setmetatable(_G, mt)
end
local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

--
use_strict()
local YES, NO = "YES", "NO"

local H, W, N = io.read("*n", "*n", "*n", "*l")
local sr, sc = io.read("*n", "*n", "*l")
local S = str2tbl(io.read("*l"))
local T = str2tbl(io.read("*l"))

local opposite = {
    L="R",
    R="L",
    D="U",
    U="D"
}

local function solve(size, move_lower, init_pos)
    local direc = move_lower == "L" and "horiz" or "verti"
    local move_upper = opposite[move_lower]
    -- Aoki wins when position p satisfies lower_bound <= p <= upper_bound
    local lower_bound = 1
    local upper_bound = size
    local math_max = math.max
    local math_min = math.min
    for n=N,1,-1 do
        if T[n] == move_lower then
            upper_bound = math_min(upper_bound + 1, size)
        elseif T[n] == move_upper then
            lower_bound = math_max(lower_bound - 1, 1)
        end
        if S[n] == move_lower then
            lower_bound = lower_bound + 1
        elseif S[n] == move_upper then
            upper_bound = upper_bound - 1
        end
        if lower_bound > size then
            return NO
        end
        if upper_bound < 1 then
            return NO
        end
        if lower_bound > upper_bound then
            return NO
        end
        dbgpr(direc, n, S[n], T[n], lower_bound, upper_bound)
    end
    if lower_bound <= init_pos and init_pos <= upper_bound then
        return YES
    else
        return NO
    end
end

local horiz_ans = solve(W, "L", sc)
dbgpr("horiz", horiz_ans)
if horiz_ans == NO then
    print(NO)
    return
end
local vert_ans = solve(H, "D", sr)
dbgpr("vert", vert_ans)
if vert_ans == NO then
    print(NO)
    return
end
print(YES)


local ior = io.input()
local h, w, n = ior:read("*n", "*n", "*n")
local sr, sc = ior:read("*n", "*n", "*l")
local s = ior:read()
local t = ior:read()
local s_left, s_right, s_up, s_down = {}, {}, {}, {}
local t_left, t_right, t_up, t_down = {}, {}, {}, {}
local str = ""
str = s:sub(1, 1)
s_left[1] = str == "L" and 1 or 0
s_right[1] = str == "R" and 1 or 0
s_up[1] = str == "U" and 1 or 0
s_down[1] = str == "D" and 1 or 0

str = t:sub(1, 1)
t_left[1] = str == "L" and 1 or 0
t_right[1] = str == "R" and 1 or 0
t_up[1] = str == "U" and 1 or 0
t_down[1] = str == "D" and 1 or 0
if(sc == 1) then t_left[1] = 0 end
if(sc == w) then t_right[1] = 0 end
if(sr == 1) then t_up[1] = 0 end
if(sr == h) then t_down[1] = 0 end

local mma, mmi = math.max, math.min

for i = 2, n do
  str = s:sub(i, i)
  s_left[i] = s_left[i - 1] + (str == "L" and 1 or 0)
  s_right[i] = s_right[i - 1] + (str == "R" and 1 or 0)
  s_up[i] = s_up[i - 1] + (str == "U" and 1 or 0)
  s_down[i] = s_down[i - 1] + (str == "D" and 1 or 0)
  str = t:sub(i, i)
  t_left[i] = t_left[i - 1] + (str == "L" and 1 or 0)
  t_left[i] = mmi(sc - 1, t_left[i])
  t_right[i] = t_right[i - 1] + (str == "R" and 1 or 0)
  t_right[i] = mmi(w - sc, t_right[i])
  t_up[i] = t_up[i - 1] + (str == "U" and 1 or 0)
  t_up[i] = mmi(sr - 1, t_up[i])
  t_down[i] = t_down[i - 1] + (str == "D" and 1 or 0)
  t_down[i] = mmi(h - sr, t_down[i])
end

local leftlim, rightlim = sc, w + 1 - sc
local uplim, downlim = sr, h + 1 - sr

local fin = false
if(
  (leftlim <= s_left[1]) or
  (rightlim <= s_right[1]) or
  (uplim <= s_up[1]) or
  (downlim <= s_down[1])
) then
  print("NO")
  fin = true
end
if(not fin) then
  for i = 2, n do
    if(
      (leftlim <= s_left[i] - t_right[i - 1]) or
      (rightlim <= s_right[i] - t_left[i - 1]) or
      (uplim <= s_up[i] - t_down[i - 1]) or
      (downlim <= s_down[i] - t_up[i - 1])
    ) then
      print("NO")
      fin = true
    end
  end
end
if(not fin) then print("YES") end

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

Score : 600 points

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c).

Takahashi and Aoki will play a game, where each player has a string of length N.
Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

The game consists of N steps. The i-th step proceeds as follows:

First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.

Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.

Constraints

2 \leq H,W \leq 2 \times 10^5

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

1 \leq s_r \leq H

1 \leq s_c \leq W

|S|=|T|=N

S and T consists of the four kinds of letters L, R, U and D.

Input

Input is given from Standard Input in the following format:

H W N
s_r s_c
S
T

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.

Sample Input 1

2 3 3
2 2
RRL
LUD

Sample Output 1

YES

Here is one possible progress of the game:

Takahashi moves the piece right. The piece is now at (2,3).

Aoki moves the piece left. The piece is now at (2,2).

Takahashi does not move the piece. The piece remains at (2,2).

Aoki moves the piece up. The piece is now at (1,2).

Takahashi moves the piece left. The piece is now at (1,1).

Aoki does not move the piece. The piece remains at (1,1).

Sample Input 2

4 3 5
2 2
UDRRR
LLDUD

Sample Output 2

NO

Sample Input 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

Sample Output 3

NO

Submitted source-code file size, in bytes.

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

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


local n = io.read("*n")
local edge = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  len[i] = -1
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
end
local tasks = {1}
len[1] = 0
for i = 1, n do
  local src = tasks[i]
  for dst, _u in pairs(edge[src]) do
    if len[dst] < 0 then
      len[dst] = len[src] + 1
      table.insert(tasks, dst)
    end
  end
end
local startpos = 1
for i = 2, n do
  if len[startpos] < len[i] then
    startpos = i
  end
end
tasks = {startpos}
for i = 1, n do
  len[i] = -1
end
len[startpos] = 0
for i = 1, n do
  local src = tasks[i]
  for dst, _u in pairs(edge[src]) do
    if len[dst] < 0 then
      len[dst] = len[src] + 1
      table.insert(tasks, dst)
    end
  end
end
local maxlen = 0
for i = 1, n do
  if maxlen < len[i] then maxlen = len[i] end
end
print(maxlen % 3 == 1 and "Second" or "First")

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game on a tree.
The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

At the beginning of the game, each vertex contains a coin.
Starting from Takahashi, he and Aoki will alternately perform the following operation:

Choose a vertex v that contains one or more coins, and remove all the coins from v.

Then, move each coin remaining on the tree to the vertex that is nearest to v among the adjacent vertices of the coin's current vertex.

The player who becomes unable to play, loses the game.
That is, the player who takes his turn when there is no coin remaining on the tree, loses the game.
Determine the winner of the game when both players play optimally.

Constraints

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

1 \leq a_i, b_i \leq N

a_i \neq b_i

The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

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

Output

Print First if Takahashi will win, and print Second if Aoki will win.

Sample Input 1

3
1 2
2 3

Sample Output 1

First

Here is one possible progress of the game:

Takahashi removes the coin from Vertex 1. Now, Vertex 1 and Vertex 2 contain one coin each.

Aoki removes the coin from Vertex 2. Now, Vertex 2 contains one coin.

Takahashi removes the coin from Vertex 2. Now, there is no coin remaining on the tree.

Aoki takes his turn when there is no coin on the tree and loses.

Sample Input 2

6
1 2
2 3
2 4
4 6
5 6

Sample Output 2

Second

Sample Input 3

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

Sample Output 3

First


-- Vicfred
-- https://atcoder.jp/contests/abc125/tasks/abc125_a
-- math, implementation
a, b, t = io.read("*n", "*n", "*n")
print((t // a) * b)

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


local N = io.read("n")
local V = {} for i=1,N do V[i] = io.read("n") end
local C = {} for i=1,N do C[i] = io.read("n") end
local ans = 0
for i=1,N do
    local diff = V[i] - C[i]
    if diff > 0 then
        ans = ans + diff
    end
end
print(ans)

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


function gcd(x, y)
	return y == 0 and x or gcd(y, x % y)
end
n = io.read"*n"
a = {}
for i=1,n do
	a[i] = io.read"*n"
end
r=0
for i=1,n do
	t = {table.unpack(a)}
	t[i] = a[i+1] or a[i-1]
	for j=1,n-1 do
		t[1] = gcd(t[1],t[j+1])
	end
	r = t[1]
end
print(r)

Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.


local ior = io.read
local n = ior("*n")
local t = {}
local minus = 0
for i = 1, n do
  local a = ior("*n")
  if(a < 0) then minus = minus + 1 end
  t[i] = a
end
table.sort(t)
local ma = math.abs
if(minus % 2 == 0) then
  local sum = 0
  for i = 1, n do
    sum = sum + ma(t[i])
  end
  print(sum)
else
  local sum = 0
  for i = 1, minus - 1 do
    sum = sum + ma(t[i])
  end
  if(minus < n) then
    sum = sum + ma(t[minus + 1] + t[minus])
  else
    sum = sum + t[minus]
  end
  for i = minus + 2, n do
    sum = sum + t[i]
  end
  print(sum)
end


a, b, c = io.read("*n", "*n", "*n")
if a < b and b < c then
  print("Yes")
elseif c < b and b < a then
  print("Yes")
else
  print("No")
end

Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


local n=io.read("n","l")
local s=io.read()

local _,white=s:gsub("%,","")
local black=0
local result=black+white
for i=1,n do
    if s:sub(i,i)=="#" then
        black=black+1
    else
        white=white-1
    end
    result=math.min(result,black+white)
end
print(result)

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function str2numtbl(s, offset)
    local t = {string.byte(s, 1, #s)}
    if offset then
        for i=1, #t do
            t[i] = t[i] - offset
        end
    end
    return t
end

--local function each_char2(s)
--    return ipairs(str2tbl(s))
--end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end

function string.at(s, i)
    return string.sub(s, i, i)
end

--
local N,_,S = io.read("n", "l", "l")
local B = '#'
local W = '.'
S = str2tbl(S)
local ans = 0
local flag = false
for i=1,N do
    local c = S[i]
    if not flag then
        if c == B then
            flag = true
        end
    else
        if c == W then
            ans = ans + 1
        end
    end
end
print(ans)


a, b = io.read("*n", "*n")
if a == b then
  print(a * 2)
elseif a < b then
  print(b * 2 - 1)
else
  print(a * 2 - 1)
end

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


n = io.read("*n")
max, cnt = 0, 0
for i = 1, n do
    a = io.read("*n")
    if(max <= a) then cnt = cnt + 1 end
    max = math.max(max, a)
end
print(cnt)

Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


-- D
local N, Kmax, _, tmp = io.read("n", "n", "l", "l")
local S = {}
local ZZ = string.byte('0')
for i=1, N do
    S[i] = string.byte(tmp, i, i) - ZZ
end
local llast, rlast = 1, 1
local k = 0
local left = 1
local ans = 0
for right=1, N do
    if rlast == 1 and S[right] == 0 then
        k = k + 1
    end
    rlast = S[right]
    while k > Kmax do
        left = left + 1
        if llast == 0 and S[left] == 1 then
            k = k - 1
        end
        llast = S[left]
    end
    assert(right >= left)
    local v = right - left + 1
    if ans < v then
        ans = v
    end
end
print(ans)

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.


-- D
local N, Kmax, _, tmp = io.read("n", "n", "l", "l")
local S = {}
local ZZ = string.byte('0')
for i=1, N do
    S[i] = string.byte(tmp, i, i) - ZZ
end
local llast, rlast = 1, 1
local k = 0
local left = 1
local ans = 0
for right=1, N do
    if rlast == 1 and S[right] == 0 then
        k = k + 1
    end
    rlast = S[right]
    while k > Kmax do
        left = left + 1
        if llast == 0 and S[left] == 1 then
            k = k - 1
        end
        llast = S[left]
    end
    local v = right - left + 1
    if ans < v then
        ans = v
    end
end
print(ans)


a=io.read("*n")
b=io.read("*n")
c=io.read("*n")
d=io.read("*n")
e=io.read("*n")
k=io.read("*n")
if e-a<=k then
  print("Yay!")
else
  print(":(")
end

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


a={}
for i = 1,5 do
	a[i]=io.read()*1
end
print(a[5]-a[1]>io.read()*1 and ":("or"Yay!")


t = {}
for i = 1, 5 do table.insert(t, io.read("*n")) end
table.sort(t, function(a, b)
        ad = a % 10
        bd = b % 10
        if(ad == 0) then ad = 10 end
        if(bd == 0) then bd = 10 end
        return ad > bd
    end
);
sum = 0
for i = 1, 4 do
    if(t[i] % 10 == 0) then sum = sum + t[i] else
        sum = sum + 10 * (1 + math.floor(t[i] / 10))
    end
end
sum = sum + t[5]
print(sum)

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


n, a, b = io.read("*n", "*n", "*n")
c, d, e = io.read("*n", "*n", "*n")
r = math.min(a, b, c, d, e)
rep = math.ceil(n / r)
print(rep + 4)

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

Note that the input or output may not fit into a 32-bit integer type.


local Set = {}

Set.insert=function(self,value)
    local set=self.set
    local reverse=self.reverse
    if not reverse[value] then
        table.insert(set,value)
        reverse[value]=#set
    end
end

Set.remove=function(set,value)
    local set=self.set
    local reverse=self.reverse
    local index=reverse[value]
    if index then
        reverse[value]=nil
        local top=table.remove(set)
        if top~=value then
            reverse[top]=index
            set[index]=top
        end
    end
end

Set.contains=function(self,value)
    local reverse=self.reverse
    return reverse[value]~=nil
end

Set.new=function(self)
    local obj={set={},reverse={}}
    setmetatable(obj,{__index=Set})
    return obj
end

----------

local PairingHeap={}

PairingHeap.merge=function(self,a,b)
	if not a then
		return b
	elseif not b then
		return a
	elseif a[1]<b[1] then
		b[4]=a[3]
		a[3]=b
		return a
	else
		a[4]=b[3]
		b[3]=a
		return b
	end
end

PairingHeap.twopassmerge=function(self,a)
	local b=a and a[4]
	if b then
		local newnode=b[4]
		a[4]=false
		b[4]=false
		return self:merge(self:merge(a,b),self:twopassmerge(newnode))
	else
		return a
	end
end

PairingHeap.empty=function(self)
	return not self.root
end

PairingHeap.pop=function(self)
    local root=self.root
    local value=root[2]
	self.root=self:twopassmerge(root[3])
    for i=1,4 do
        root[i]=false
	end
	table.insert(self,root)
    return value
end

PairingHeap.push=function(self,key,value)
    local root=self.root
	if root and root[1]<key then
		root[3]={
            key,
            value,
            false,
            root[3],
        }
    else
		self.root={
            key,
            value,
            root,
            false,
        }
	end
end

PairingHeap.join=function(self,other)
	local a=self.root
	local b=other.root
	if a[1]<b[1]then
		b[4]=a[3]
		a[3]=b
	else
		a[4]=b[3]
		b[3]=a
		self.root=b
	end
	return self
end

PairingHeap.new=function(self)
    local obj={}
    setmetatable(obj,{__index=PairingHeap})
    return obj
end

----------

local xyz={io.read("n","n","n")}
local k=io.read("n")
local abc={}
for i=1,3 do
    abc[i]={}
    for j=1,xyz[i] do
        abc[i][j]=io.read("n")
    end
    table.sort(abc[i],function(a,b)
        return a>b
    end)
end

local pq=PairingHeap:new()
local set=Set:new()

local frstky=abc[1][1]+abc[2][1]+abc[3][1]
local frstvl={1,1,1,frstky}
pq:push(-frstky,frstvl)
set:insert(frstvl)

for i=1,k do
    local pop=pq:pop()
    print(pop[4])
    for j=1,3 do
        if pop[j]<xyz[j] then
            local key=pop[4]-abc[j][pop[j]]+abc[j][pop[j]+1]
            local val={pop[1],pop[2],pop[3],key}
            val[j]=val[j]+1
            if not set:contains(val) then
                pq:push(-key,val)
            end
        end
    end
end

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.


local PairingHeap={}

PairingHeap.merge=function(self,a,b)
	if not a then
		return b
	elseif not b then
		return a
	elseif a[1]<b[1] then
		b[4]=a[3]
		a[3]=b
		return a
	else
		a[4]=b[3]
		b[3]=a
		return b
	end
end

PairingHeap.twopassmerge=function(self,a)
	local b=a and a[4]
	if b then
		local newnode=b[4]
		a[4]=false
		b[4]=false
		return self:merge(self:merge(a,b),self:twopassmerge(newnode))
	else
		return a
	end
end

PairingHeap.empty=function(self)
	return not self.root
end

PairingHeap.pop=function(self)
    local root=self.root
    local value=root[2]
	self.root=self:twopassmerge(root[3])
    for i=1,4 do
        root[i]=false
	end
	table.insert(self,root)
    return value
end

PairingHeap.push=function(self,key,value)
    local root=self.root
	if root and root[1]<key then
		root[3]={
            key,
            value,
            false,
            root[3],
        }
    else
		self.root={
            key,
            value,
            root,
            false,
        }
	end
end

PairingHeap.join=function(self,other)
	local a=self.root
	local b=other.root
	if a[1]<b[1]then
		b[4]=a[3]
		a[3]=b
	else
		a[4]=b[3]
		b[3]=a
		self.root=b
	end
	return self
end

PairingHeap.new=function(self)
    local obj={}
    setmetatable(obj,{__index=PairingHeap})
    return obj
end

----------

local xyz={io.read("n","n","n")}
local k=io.read("n")
local abc={}
for i=1,3 do
    abc[i]={}
    for j=1,xyz[i] do
        abc[i][j]=io.read("n")
    end
    table.sort(abc[i],function(a,b)
        return a>b
    end)
end

local pq=PairingHeap:new()
local map={}

local frstky=abc[1][1]+abc[2][1]+abc[3][1]
pq:push(-frstky,{1,1,1,frstky})

for i=1,k do
    local pop=pq:pop()
    print(pop[4])
    for j=1,3 do
        if pop[j]<xyz[j] then
            local key=pop[4]-abc[j][pop[j]]+abc[j][pop[j]+1]
            local val={pop[1],pop[2],pop[3],key}
            val[j]=val[j]+1
            local mapkey=tostring(key)
            for l=1,3 do
                mapkey=mapkey.." "..val[l]
            end
            if not map[mapkey] then
                map[mapkey]=true
                pq:push(-key,val)
            end
        end
    end
end


local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- D
local X, Y, Z, K = io.read("n", "n", "n", "n")
local A, B, C = {}, {}, {}

for i, tbl in ipairs {A, B, C} do
    local nums = {X, Y, Z}
    local count = nums[i]
    for j=1, count do
        tbl[j] = io.read("n")
    end
    table.sort(tbl, function (x,y)
        return x > y
    end)
end

local function goodness(a, b, c)
    if a > #A or b > #B or c > #C then
        return -10000000000000
    end
    return A[a] + B[b] + C[c]
end

local visited = {}
local function visit(a,b,c)
    visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)] = true
end
local function isvisited(a,b,c)
    return visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)]
end

-- most good comb right
local items = {}
local function item_sort(tbl)
    table.sort(items, function(x,y)
        return x[4] < y[4]
    end)
end
local function items_count()
    return #items
end
local function items_add(a, b, c)
    if items_count() > 3000 then
        if items[#items][4] >= goodness(a,b,c) then
            return false
        end
    end
    table.insert(items, {a,b,c,goodness(a,b,c)})
    item_sort(items)
    return true
end
local function check_add(a,b,c)
    if a > #A or b > #B or c > #C then
        return -1
    end
    if isvisited(a, b, c) then
        return -2
    end
    visit(a,b,c)
    items_add(a, b, c)
end
local ans = {}
local a, b, c = 1, 1, 1
check_add(a, b, c)
while #ans < K do
    local e = table.remove(items)
    table.insert(ans, e[4])
    local a,b,c = e[1], e[2], e[3]
    check_add(a+1,b,c)
    check_add(a,b+1,c)
    check_add(a,b,c+1)
end

--table.sort(ans, function(x,y) return x>y end)
for i=1,K do
    print(ans[i])
end


local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- heap
local Heap = {}
function Heap.new(upper_func)
    local self = setmetatable({}, {__index = Heap})
    self.a = {}
    self.upper_func = upper_func
    self.upper = function(k1, k2)
        assert(k1 < k2)
        if k1 == 0 then
            return false
        else
            return upper_func(self.a[k1], self.a[k2])
        end
    end
    self.lower = function(k1, k2)
        return not self.upper(k1, k2)
    end
    return self
end

function Heap:count()
    return #self.a
end

function Heap:_swap(k1, k2)
    local old_k1 = self.a[k1]
    self.a[k1] = self.a[k2]
    self.a[k2] = old_k1
end

function Heap:upheap(newk)
    local ku = newk // 2
    local k = newk
    while ku > 0 and self.lower(ku, k) do
        self:_swap(ku, k)
        k = k//2
        ku = ku//2
    end
end

function Heap:insertq(e)
    table.insert(self.a, e)
    self:upheap(#self.a)
end

function Heap:downheap(newk)
    local k = newk
    while k <= #self.a//2 do
        local kdl = k*2
        local kdr = kdl + 1
        local kd
        if kdr <= #self.a and self.lower(kdl, kdr) then
            kd = kdr
        else
            kd = kdl
        end
        if self.upper(k, kd) then
            break
        end
        self:_swap(k, kd)
        k = kd
    end
end

function Heap:top()
    assert(#self.a >= 1)
    return self.a[1]
end

function Heap:remove_top()
    assert(#self.a >= 1)
    local v = self.a[1]
    self:_swap(1, #self.a)
    table.remove(self.a)
    self:downheap(1)
    return v
end

--[[
function Heap:replace_top(e)
    assert(#self.a >= 1)
    local old_v = self.a[1]
    local swapped = false
    if self.upper_func(old_v, e) then
        self.a[1] = e
        self:downheap(1)
        swapped = true
    end
    return swapped, old_v
end

function Heap:change()
end

function Heap:delete()
end
]]
-- D
local X, Y, Z, K = io.read("n", "n", "n", "n")
local A, B, C = {}, {}, {}

for i, tbl in ipairs {A, B, C} do
    local nums = {X, Y, Z}
    local count = nums[i]
    for j=1, count do
        tbl[j] = io.read("n")
    end
    table.sort(tbl, function (x,y)
        return x > y
    end)
end

local function goodness(a, b, c)
    if a > #A or b > #B or c > #C then
        return -10000000000000
    end
    return A[a] + B[b] + C[c]
end

local visited = {}
local function visit(a,b,c)
    visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)] = true
end
local function isvisited(a,b,c)
    return visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)]
end

-- most good comb top
local items = Heap.new(function(x,y)
    return x[4] > y[4]
end)
local function items_add(a, b, c)
    items:insertq({a,b,c,goodness(a,b,c)})
end
local function check_add(a,b,c)
    if a > #A or b > #B or c > #C then
        return -1
    end
    if isvisited(a, b, c) then
        return -2
    end
    visit(a,b,c)
    items_add(a, b, c)
end
local ans = {}
local a, b, c = 1, 1, 1
check_add(a, b, c)
while #ans < K do
    local e = items:remove_top()
    table.insert(ans, e[4])
    local a,b,c = e[1], e[2], e[3]
    check_add(a+1,b,c)
    check_add(a,b+1,c)
    check_add(a,b,c+1)
end

table.sort(ans, function(x,y) return x>y end)
for i=1,K do
    print(ans[i])
end


local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- heap
local Heap = {}
function Heap.new(upper_func)
    local self = setmetatable({}, {__index = Heap})
    self.a = {}
    self.upper_func = upper_func
    self.upper = function(k1, k2)
        assert(k1 < k2)
        if k1 == 0 then
            return false
        else
            return upper_func(self.a[k1], self.a[k2])
        end
    end
    self.lower = function(k1, k2)
        return not self.upper(k1, k2)
    end
    return self
end

function Heap:count()
    return #self.a
end

function Heap:_swap(k1, k2)
    local old_k1 = self.a[k1]
    self.a[k1] = self.a[k2]
    self.a[k2] = old_k1
end

function Heap:upheap(newk)
    local ku = newk // 2
    local k = newk
    while ku > 0 and self.lower(ku, k) do
        self:_swap(ku, k)
        k = k//2
        ku = ku//2
    end
end

function Heap:insertq(e)
    table.insert(self.a, e)
    self:upheap(#self.a)
end

function Heap:downheap(newk)
    local k = newk
    while k <= #self.a//2 do
        local kdl = k*2
        local kdr = kdl + 1
        local kd
        if kdr <= #self.a and self.lower(kdl, kdr) then
            kd = kdr
        else
            kd = kdl
        end
        if self.upper(k, kd) then
            break
        end
        self:_swap(k, kd)
        k = kd
    end
end

function Heap:top()
    assert(#self.a >= 1)
    return self.a[1]
end

--[[
function Heap:remove_top()
    assert(#self.a >= 1)
    local v = self.a[1]
    self:_swap(1, #self.a)
    self.a:remove()
    self:downheap(1)
    return v
end

function Heap:replace_top(e)
    assert(#self.a >= 1)
    local old_v = self.a[1]
    local swapped = false
    if self.upper_func(old_v, e) then
        self.a[1] = e
        self:downheap(1)
        swapped = true
    end
    return swapped, old_v
end

function Heap:change()
end

function Heap:delete()
end
]]
-- D
local X, Y, Z, K = io.read("n", "n", "n", "n")
local A, B, C = {}, {}, {}

for i, tbl in ipairs {A, B, C} do
    local nums = {X, Y, Z}
    local count = nums[i]
    for j=1, count do
        tbl[j] = io.read("n")
    end
    table.sort(tbl, function (x,y)
        return x > y
    end)
end

local function goodness(a, b, c)
    if a > #A or b > #B or c > #C then
        return -10000000000000
    end
    return A[a] + B[b] + C[c]
end

local visited = {}
local function visit(a,b,c)
    visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)] = true
end
local function isvisited(a,b,c)
    return visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)]
end

-- most bad comb top
local items = Heap.new(function(x,y)
    return x[4] < y[4]
end)
local function items_add(a, b, c)
    if items:count() > 3000 then
        local old_top = items:top()
        if old_top[4] >= goodness(a,b,c) then
            return false
        end
    end
    items:insertq({a,b,c,goodness(a,b,c)})
    return true
end
local function dfs(a, b, c)
    if a > #A or b > #B or c > #C then
        return -1
    end
    if isvisited(a, b, c) then
        return -2
    end
    visit(a,b,c)
    local f = items_add(a, b, c)
    --print(a,b,c,f)
    if f then
        local a_next = {a + 1, b, c, goodness(a + 1, b, c)}
        local b_next = {a, b + 1, c, goodness(a, b + 1, c)}
        local c_next = {a, b, c + 1, goodness(a, b, c + 1)}
        local t = {a_next, b_next, c_next}
        table.sort(t, function(x,y)
            return x[4] > y[4]
        end)
        for i=1, #t do
            local tt = t[i]
            dfs(tt[1], tt[2], tt[3])
        end
    end
end
dfs(1, 1, 1)

local ans = {}
for i=1,#items.a do
    table.insert(ans, items.a[i][4])
end
table.sort(ans, function(x,y) return x>y end)
for i=1,K do
    print(ans[i])
end


local A,B,C = io.read("n", "n", "n")
print((A==B and B == C and C==A) and "Yes" or "No")

Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


n = io.read("*n", "*l")
s = io.read()
B, R = 0, 0
for i = 1, n do
  if(string.sub(s, i, i) == "B") then B = B + 1 else R = R + 1 end
end
if(B < R) then print("Yes") else print("No") end

Score : 200 points

Problem Statement

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.

Determine if there are more people wearing a red hat than people wearing a blue hat.

Constraints

1 \leq N \leq 100

|s| = N

s_i is R or B.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.

Sample Input 1

4
RRBR

Sample Output 1

Yes

There are three people wearing a red hat, and one person wearing a blue hat.

Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes.

Sample Input 2

4
BRBR

Sample Output 2

No

There are two people wearing a red hat, and two people wearing a blue hat.

Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No.


-- C
local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end
local N, Q, _ = io.read("n", "n", "l")
local s = io.read("l")
assert(#s == N)
local spells = {}
for i=1,Q do
    local t, _, d, _ = io.read(1, 1, 1, "l")
    spells[i] = {t, d}
end

local place = str2tbl(s)

local left, right
local left_path, right_path = 0, N+1

for i=Q,1,-1 do
    local t, d = spells[i][1], spells[i][2]
    local flag = true
    if d == 'L' then
        if t == place[1] and not left then
            left_path = 1
            flag = false
            left = true
        end
    else
        if t == place[N] and not right then
            right_path = N
            flag = false
            right = true
        end
    end
    if flag then
        if d == 'L' and left then
            if t == place[left_path+1] then
                left_path = left_path+1
            end
        elseif right then
            if t == place[right_path-1] then
                right_path = right_path-1
            end
        end
    end
end

local dead = 0
for i=1,N do
    if i <= left_path or i >= right_path then
        dead = dead + 1
    end
end
print(N - dead)

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


-- C
local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end
local N, Q, _ = io.read("n", "n", "l")
local s = io.read("l")
assert(#s == N)
local spells = {}
for i=1,Q do
    local t, _, d, _ = io.read(1, 1, 1, "l")
    spells[i] = {t, d}
end

local place = str2tbl(s)

local paths = {}

local function new_path(pos)
    local p = {pos, 1}
    return p
end
local function push_path(p)
    table.insert(paths, p)
end

local left, right

for i=Q,1,-1 do
    local t, d = spells[i][1], spells[i][2]
    local flag = true
    if d == 'L' then
        if t == place[1] and not left then
            local p = new_path(1)
            push_path(p)
            flag = false
            left = true
        end
    else
        if t == place[N] and not right then
            local p = new_path(N)
            push_path(p)
            flag = false
            right = true
        end
    end
    if flag then
        for _,p in ipairs(paths) do
            local pos, count = p[1], p[2]
            if d == 'L' then
                if t == place[pos+1] then
                    p[1], p[2] = pos+1, count+1
                end
            else
                if t == place[pos-1] then
                    p[1], p[2] = pos-1, count+1
                end
            end
        end
    end
end
local dead = 0
for _,p in ipairs(paths) do
    dead = dead + p[2]
end
print(N - dead)


function check(rrr)
    tmp = rrr
    res = "OK"
    posstr = string.sub(s, tmp, tmp)
    for i = 1, q do
        if(svtgt[i] == posstr) then
            if(svway[i] == "L") then
                if(tmp == 1) then
                    res = "LEFT"
                else
                    tmp = tmp - 1
                    posstr = string.sub(s, tmp, tmp)
                end
            else
                if(tmp == n) then
                    res = "RIGHT"
                else
                    tmp = tmp + 1
                    posstr = string.sub(s, tmp, tmp)
                end
            end
        end
        if(res ~= "OK") then break end
    end
    return res
end

n, q = io.read("*n", "*n", "*l")
s = io.read()
t = {}
num = {}
for i = 1, n do
    a = string.sub(s, i, i)
    if(t[a] == nil) then
        t[a] = {{}, {}}
    end
    table.insert(t[a][1], i)
    table.insert(t[a][2], 1, i)
    num[i] = 1
end
svtgt, svway = {}, {}
for i = 1, q do
    tgt, zz, way = io.read(1, 1, "*l")
    table.insert(svtgt, tgt)
    table.insert(svway, way)
end
leftmin = 1
leftmax = n
leftkill = 0
fres, nres = check(1), check(n)
if(nres == "LEFT") then
    leftkill = n
elseif(fres ~= "OK") then
    while(1 < leftmax -leftmin) do
        leftmid = math.floor((leftmin + leftmax) / 2)
        midret = check(leftmid)
        if(midret == "LEFT") then
            leftmin = leftmid
        else
            leftmax = leftmid
        end
    end
    leftkill = leftmin
end
rightmin = leftkill + 1
rightmax = n
rightkill = 0
if(check(rightmin) == "RIGHT") then
    rightkill = n - rightmin
elseif(nres ~= "OK") then
    while(1 < rightmax -rightmin) do
        rightmid = math.floor((rightmin + rightmax) / 2)
        midret = check(rightmid)
        if(midret == "RIGHT") then
            rightmax = rightmid
        else
            rightmin = rightmid
        end
    end
    rightkill = n + 1 - rightmax
end
print(n - leftkill - rightkill)


local mod = 1000000007
local mfl = math.floor
local mmi = math.min

local function bmul(x, y)
  if x * y <= 9999999999999 then -- < 10^14
    return (x * y) % mod
  end
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local n, x = io.read("*n", "*n")
local fact = {1}
for i = 2, 200 do
  fact[i] = bmul(fact[i - 1], i)
end
local combfact = {}
for i = 1, 200 do
  combfact[i] = {}
  local c = 1
  for j = 1, i do
    c = bmul(c, i + 1 - j)
    c = bmul(c, modinv(j))
    combfact[i][j] = bmul(c, fact[j])
  end
end

local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
table.sort(a)
local t = {}
for i = 1, n do
  t[i] = {}
end
for i = 1, n - 1 do
  t[i][x % a[i]] = combfact[n - 1][n - i]
end
t[n][x % a[n]] = 1

for src = n, 2, -1 do
  local cfmap = combfact[src - 2]
  for val, cnt in pairs(t[src]) do
    do
      local nval = val % a[src - 1]
      local precnt = t[src - 1][nval]
      if precnt then
        t[src - 1][nval] = badd(precnt, cnt)
      else
        t[src - 1][nval] = cnt
      end
    end
  end
  for dst = src - 2, 1, -1 do
    local tdst = t[dst]
    for val, cnt in pairs(t[src]) do
      local nval = val % a[dst]
      local k = bmul(cnt, cfmap[src - dst - 1])
      local precnt = tdst[nval]
      if precnt then
        tdst[nval] = badd(precnt, k)
      else
        tdst[nval] = k
      end
    end
  end
end
local ret = 0
for k, v in pairs(t[1]) do
  ret = badd(ret, bmul(k, v))
end
print(ret)

Score : 600 points

Problem Statement

Snuke has a blackboard and a set S consisting of N integers.
The i-th element in S is S_i.

He wrote an integer X on the blackboard, then performed the following operation N times:

Choose one element from S and remove it.

Let x be the number written on the blackboard now, and y be the integer removed from S. Replace the number on the blackboard with x \bmod {y}.

There are N! possible orders in which the elements are removed from S.
For each of them, find the number that would be written on the blackboard after the N operations, and compute the sum of all those N! numbers modulo 10^{9}+7.

Constraints

All values in input are integers.

1 \leq N \leq 200

1 \leq S_i, X \leq 10^{5}

S_i are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N X
S_1 S_2 \ldots S_{N}

Output

Print the answer.

Sample Input 1

2 19
3 7

Sample Output 1

3

There are two possible orders in which we remove the numbers from S.

If we remove 3 and 7 in this order, the number on the blackboard changes as follows: 19 \rightarrow 1 \rightarrow 1.

If we remove 7 and 3 in this order, the number on the blackboard changes as follows: 19 \rightarrow 5 \rightarrow 2.

The output should be the sum of these: 3.

Sample Input 2

5 82
22 11 6 5 13

Sample Output 2

288

Sample Input 3

10 100000
50000 50001 50002 50003 50004 50005 50006 50007 50008 50009

Sample Output 3

279669259

Be sure to compute the sum modulo 10^{9}+7.


local mod = 1000000007
local mfl = math.floor
local mmi = math.min

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local n, x = io.read("*n", "*n")
local fact = {1}
for i = 2, 200 do
  fact[i] = bmul(fact[i - 1], i)
end
local combfact = {}
for i = 1, 200 do
  combfact[i] = {}
  local c = 1
  for j = 1, i do
    c = bmul(c, i + 1 - j)
    c = bmul(c, modinv(j))
    combfact[i][j] = bmul(c, fact[j])
  end
end

local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
table.sort(a)
local t = {}
for i = 1, n do
  t[i] = {}
end
for i = 1, n do
  if 0 < n - 1 and 0 < n - i then
    t[i][x % a[i]] = combfact[n - 1][n - i]
  else
    t[i][x % a[i]] = 1
  end
end
for src = n, 2, -1 do
  local cfmap = combfact[src - 2]
  for val, cnt in pairs(t[src]) do
    do
      local dst = src - 1
      local nval = val % a[dst]
      local precnt = t[dst][nval]
      if precnt then
        t[dst][nval] = badd(precnt, cnt)
      else
        t[dst][nval] = cnt
      end
    end
    for dst = src - 2, 1, -1 do
      local nval = val % a[dst]
      local step = src - dst - 1
      local k = bmul(cnt, cfmap[step])
      local precnt = t[dst][nval]
      if precnt then
        t[dst][nval] = badd(precnt, k)
      else
        t[dst][nval] = k
      end
    end
  end
end
local ret = 0
for k, v in pairs(t[1]) do
  ret = badd(ret, bmul(k, v))
end
print(ret)


local mod = 1000000007
local mfl = math.floor
local mmi = math.min

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local n, x = io.read("*n", "*n")
local fact = {1}
for i = 2, 200 do
  fact[i] = bmul(fact[i - 1], i)
end
local combfact = {}
for i = 1, 200 do
  combfact[i] = {}
  local c = 1
  for j = 1, i do
    c = bmul(c, i + 1 - j)
    c = bmul(c, modinv(j))
    combfact[i][j] = bmul(c, fact[j])
  end
end
local function getCombFact(n, k)
  if n == 0 then return 1 end
  if k == 0 then return 1 end
  return combfact[n][k]
end

local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
table.sort(a)
local t = {}
for i = 1, n do
  t[i] = {}
end
for i = 1, n do
  t[i][x % a[i]] = getCombFact(n - 1, n - i)
end
for src = n, 1, -1 do
  local cfmap = combfact[src - 2]
  for val, cnt in pairs(t[src]) do
    for dst = src - 1, 1, -1 do
      local k = cnt
      local nval = val % a[dst]
      local step = src - dst - 1
      if 0 < step * (src - 2) then
        k = bmul(k, cfmap[step])
      end
      -- k = bmul(k, getCombFact(src - 2, step))
      local precnt = t[dst][nval]
      if precnt then
        t[dst][nval] = badd(precnt, k)
      else
        t[dst][nval] = k
      end
    end
  end
end
local ret = 0
for k, v in pairs(t[1]) do
  ret = badd(ret, bmul(k, v))
end
print(ret)


local mod = 1000000007
local mfl = math.floor
local mmi = math.min

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local n, x = io.read("*n", "*n")
local fact = {1}
for i = 2, n do
  fact[i] = bmul(fact[i - 1], i)
end
local combs = {}
for i = 1, 200 do
  combs[i] = {}
  local c = 1
  for j = 1, i do
    c = bmul(c, i + 1 - j)
    c = bmul(c, modinv(j))
    combs[i][j] = c
  end
end
local function getComb(n, k)
  if n == 0 then return 1 end
  if k == 0 then return 1 end
  return combs[n][k]
end

local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
table.sort(a)
local t = {}
for i = 1, n do
  t[i] = {}
end
for i = 1, n - 1 do
  t[i][x % a[i]] = bmul(fact[n - i], getComb(n - 1, n - i))
end
t[n][x % a[n]] = 1

for src = n, 1, -1 do
  for val, cnt in pairs(t[src]) do
    for dst = src - 1, 1, -1 do
      local k = cnt
      local nval = val % a[dst]
      local step = src - dst - 1
      if 0 < step then k = bmul(k, fact[step]) end
      k = bmul(k, getComb(src - 2, step))
      if t[dst][nval] then
        t[dst][nval] = badd(t[dst][nval], k)
      else
        t[dst][nval] = k
      end
    end
  end
end
local ret = 0
for k, v in pairs(t[1]) do
  ret = badd(ret, bmul(k, v))
end
print(ret)


local a=io.read()
if a=='A'then
  print('T')
elseif a=='C'then
  print('G')
elseif a=='G'then
  print('C')
else
  print('A')
end

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


local s=io.read()
local t={"A","G","C","T"}
local max=0
local len=0
for i=1,#s do
    local check=false
    for j=1,4 do
        if s:sub(i,i)==t[j] then
            check=true
        end
    end
    if check then
        len=len+1
    else
        len=0
    end
    max=math.max(max,len)
end
print(max)

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

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

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


n, q = io.read("*n", "*n", "*l")
s = io.read()
t = {0}
for i = 2, n do
  if s:sub(i - 1, i - 1) == "A" and s:sub(i, i) == "C" then
    t[i] = t[i - 1] + 1
  else
    t[i] = t[i - 1]
  end
end

for iq = 1, q do
  l, r = io.read("*n", "*n")
  z = t[r]
  if 1 < l then z = z - t[l] end
  print(z)
end

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


len, n = io.read("*n", "*n", "*l")
s = io.read()
pos = 1
t = {}
cur = 0
while(pos <= len) do
    st, lst = string.find(s, "AC", pos)
    if(st == nil) then st = len + 1 end
    for i = pos, st - 1 do
        t[i] = cur
    end
    cur = cur + 1
    t[st] = cur
    pos = st + 1
end
for i = 1, n do
    x, y = io.read("*n", "*n")
    if(x == 1) then
        print(t[y - 1])
    else
        print(t[y-1] - t[x-1])
    end
end


n,q=io.read():match("(.+)%s(.+)")
s=io.read()
pre=nil
count={[0]=0}
ix=0
for i in s:gmatch(".")do
  if pre=="A" and i=="C" then
    count[ix+1]=count[ix]+1
  else
    count[ix+1]=count[ix]
  end
  pre=i
  ix=ix+1
end
for i=1,q do
  l,r=io.read():match("(.+)%s(.+)")
  print(count[r*1]-count[l*1])
end


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

local a={}
for i=1,n do
    for j=#b,1,-1 do
        if b[j]==j then
            table.remove(b,j)
            a[i]=j
            break
        end
    end
end

if #a==n then
    for i=1,n do
        print(a[i])
    end
else
    print(-1)
end

Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1


local n = io.read("*n")
local t = {}
local ret = {}
for i = 1, n do
  t[i] = io.read("*n")
  ret[i] = 0
end
local found = false
for i = n, 1, -1 do
  found = false
  for k = i, 1, -1 do
    if t[k] == k then
      ret[i] = k
      table.remove(t, k)
      found = true
      break
    end
  end
  if not found then break end
end
if not found then print(-1)
else
  for i = 1, n do print(ret[i]) end
end


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

for i=N,1,-1 do
    for jj=i,1,-1 do
        if b[jj] == jj then
            table.remove(b, jj)
            ans[i] = jj
            break
        end
    end
    if ans[i] == nil then
        print(-1)
        return
    end
end

for i=1,N do
    print(ans[i])
end


local N = io.read("n")

local edges = {}

if N % 2 == 1 then
    for i=1,N do
        for j=i+1,N do
            if j ~= N-i then
                table.insert(edges , tostring(i) .. " " .. tostring(j))
            end
        end
    end
else
    for i=1,N do
        for j=i+1,N do
            if j ~= N+1-i then
                table.insert(edges , tostring(i) .. " " .. tostring(j))
            end
        end
    end
end

print(#edges)
for _,v in pairs(edges) do
    print(v)
end

Score : 700 points

Problem Statement

You are given an integer N.
Build an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:

The graph is simple and connected.

There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.

It can be proved that at least one such graph exists under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers a_i and b_i, representing the endpoints of the i-th edge.

The output will be judged correct if the graph satisfies the conditions.

Sample Input 1

3

Sample Output 1

2
1 3
2 3

For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.


local n=io.read("n","l")
local t={}
for a,_ in utf8.codes(io.read()) do
    if not t[a] then
        t[a]=1
    else
        t[a]=t[a]+1
    end
end

local combination=1
for _,frequency in pairs(t) do
    combination=combination*(frequency+1)%1000000007
end
print(combination-1)

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


local n=io.read("n","l")
local s=io.read()

local t={}
for i=1,n do
    local a=s:sub(i,i)
    if not t[a] then
        t[a]=1
    else
        t[a]=t[a]+1
    end
end

local combination=1
for a,frequency in pairs(t) do
    combination=combination*(frequency+1)
end
print(combination-1)


local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, 26 do t[i] = 0 end
for i = 1, n do
  local p = s:sub(i, i):byte() - 96
  t[p] = t[p] + 1
end
local ret = 1
for i = 1, 26 do
  ret = (ret * (1 + t[i])) % 1000000007
end
print(ret - 1)


local n, s = io.read("l","l")
local cnt = {}
for i = 1, n do
    local ch = string.sub(s, i, i)
    cnt[ch] = (cnt[ch] or 1) + 1
end
local ans, mof = 1, 1000000007
for _, v in pairs(cnt) do
    ans = ans * v % mof
end
print((ans + mof - 1) % mof)


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function dbgpr_tp(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in pairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

------------------------------------
local MOD = 1000000007
local AA, ZZ = 97, 122
local alpha_count = {}
for i=AA,ZZ,1 do
    alpha_count[i] = 0
end
local N = io.read("n", "l")
local S = io.read("l")
local s_tbl = {string.byte(S, 1, #S)}
for _,v in ipairs(s_tbl) do
    alpha_count[v] = alpha_count[v] + 1
end
dbgpr_tp(alpha_count)

local answer = 1
for i=AA,ZZ,1 do
    local place_cnt = alpha_count[i]
    if place_cnt == 0 then
    elseif place_cnt == 1 then
        answer = (answer * 2) % MOD
    else
        -- place_cnt >= 2
        local alphai_combs = 1 + place_cnt
        answer = (answer * alphai_combs) % MOD
    end
end
answer = (answer - 1) % MOD
print(answer)


local n = io.read("n")
local colpos, col, dp= {}, {}, {}
local last = -1
mof = 1000000000+7
for i = 1, n do
    local x = io.read("n")
    if last ~= x then
        col[#col+1] = x
        dp[#col] = {0, 0}
        colpos[x] = colpos[x] or {head = 1}
        colpos[x][#colpos[x] + 1] = #col
        last = x
    end
end
n = #col
dp[1][1] = 1
for i = 1, n do
    local x = col[i]
    local last_pos = colpos[x][colpos[x].head]
    -- update break now
    dp[i][1] = dp[i][1] + dp[last_pos][2]
    -- update break next
    if i < n then
        dp[i+1][1] = dp[i+1][1] + dp[i][1]
    end
    -- update keep going begin
    dp[i][2] = dp[last_pos][2] + dp[i][1] -- from hitory or from now

    dp[i][1] = dp[i][1] % mof
    dp[i][2] = dp[i][2] % mof
    if last_pos ~= i then
        colpos[x].head = colpos[x].head + 1
    end
    --print(string.format("dp[%d] = {%d,%d}; col = %d", i,dp[i][1],dp[i][2], col[i]))
end
print(dp[n][1])

Score : 700 points

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

Snuke will perform the following operation zero or more times:

Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

Constraints

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

1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1
:
C_N

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

Sample Input 1

5
1
2
1
2
2

Sample Output 1

3

We can make three sequences of colors of stones, as follows:

(1,2,1,2,2), by doing nothing.

(1,1,1,2,2), by choosing the first and third stones to perform the operation.

(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

Sample Input 2

6
4
2
5
4
2
4

Sample Output 2

5

Sample Input 3

7
1
3
1
2
3
3
2

Sample Output 3

5


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

local dp = {}
local last_pos = {}
dp[0] = 1
for i=1,N do
    if C[i] == C[i-1] then
        -- No stone between i-1 and i
        dp[i] = dp[i-1]
    else
        local c = C[i]
        if last_pos[c] then
            -- Stones from (last_pos[c], i) are reversible.
            -- Not reverse: dp[i-1]
            -- Reverse: dp[last_pos[c]]
            dp[i] = dp[i-1] + dp[last_pos[c]]
        else
            -- Can't reverse stone because this is first appearance of c
            dp[i] = dp[i-1]
        end
        last_pos[c] = i
    end
    --print(i,C[i],dp[i])
    dp[i] = dp[i] % MOD
end
print(dp[N])


Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function dbgpr_t2d(tbl2d)
    if DBG then
        dbgpr(tbl2d)
        for i,t in ipairs(tbl2d) do
            io.write("[dbg]")
            for j,v in ipairs(t) do
                io.write("(" .. tostring(i) .. "," .. tostring(j) .. "): ")
                io.write(tostring(v))
                io.write("; ")
            end
            print("")
        end
    end
end
function create_tbl(a, initial)
    local tbl = {}
    for i=1,a do
        tbl[i] = initial
    end
    return tbl
end

function create_2d_tbl(a, b, initial)
    local tbl = {}
    for i=1,a do
        local t = {}
        for j=1,b do
            t[j] = initial
        end
        tbl[i] = t
    end
    return tbl
end

function parse_problem()
    local N, M = io.read("n", "n")
    local stores = {}
    for i=1,N do
        local a, b = io.read("n", "n")
        stores[i] = {a, b}
    end
    table.sort(stores, function (x, y) return x[1] < y[1] end)
    return N, M, stores
end

function main()
    local N, M, stores = parse_problem()
    dbgpr(N, M)
    dbgpr("====stores")
    dbgpr_t2d(stores)
    local remain = M
    local total_price = 0
    local idx = 1
    while remain > 0 do
        local unit_price, stock = stores[idx][1], stores[idx][2]
        local units = stock
        if remain - units < 0 then
            units = remain
        end
        total_price = total_price + unit_price * units
        remain = remain - units
        dbgpr(idx, remain)
        idx = idx + 1
    end
    print(total_price)
end

main()

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.


a,b,c=io.read():match("(.+)%s(.+)%s(.+)")
x=1
d={}
for i=1,100 do
  if a%i==0 and b%i==0 then
    table.insert(d,i)
  end
end
print(d[#d-c+1])

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


a,b,k = io.read("n","n","n")
t = {}
for i=1,math.max(a,b) do
    q1 = a % i
    q2 = b % i
    if (q1 == 0 and q2 == 0) then
        table.insert(t, i)
    end
end
print(t[#t + 1 - k])


local s=io.read("*l")
local r=0
for _ in string.gmatch(s,"1") do
  r=r+1
end
print(math.min(r,#s-r)*2)

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


local s = io.read()
local p1 = 0
for i = 1, #s do
  if(string.sub(s, i, i) == "1") then p1 = p1 + 1 end
end
print(math.min(p1, #s - p1) * 2)


S=io.read()
local count=0
local i=1
while i~=#S  do
    if string.sub(S,i,i)=="0" and string.sub(S,i+1,i+1)=="1" or string.sub(S,i,i)=="1" and string.sub(S,i+1,i+1)=="0" then
        count=count+2
        S=string.sub(S,1,i-1)..string.sub(S,i+2,#S)
        i=i-1
    else
        i=i+1
    end
end
print(count)


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function parse_problem()
    local tbl = {}
    local S = io.read("l"):gsub("[^%d]", "")
    dbgpr(S, string.len(S))
    --local S_split = {S:byte(1, S:len())}
    local cur_char = nil
    local cur_succ = nil
    for _,v in ipairs({S:byte(1, S:len())}) do
        if v ~= cur_char then
            if cur_succ ~= nil then
                table.insert(tbl, cur_succ)
            end
            cur_char = v
            cur_succ = 1
        else
            cur_succ = cur_succ + 1
        end
    end
    if cur_succ ~= nil then
        table.insert(tbl, cur_succ)
    end
    dbgpr_t(tbl)
    return tbl
end

function main()
    local tbl = parse_problem()
    local del_count = 0
    local start = 1
    while start <= (#tbl - 1) do
        local a, b = tbl[start], tbl[start + 1]
        if a == b then
            del_count = del_count + a
            -- remove a, b
            start = start + 2
        elseif a > b then
            local r = a - b
            del_count = del_count + b
            -- remove b, merge r into c
            start = start + 2
            if start <= #tbl then
                tbl[start] = tbl[start] + r
            end
        else
            -- b > a
            local r = b - a
            del_count = del_count + a
            -- remove a
            start = start + 1
            -- update b
            tbl[start] = r
        end
    end
    local ans = del_count * 2
    print(ans)
end

main()


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function dbgpr_t2d(tbl2d)
    if DBG then
        dbgpr(tbl2d)
        for i,t in ipairs(tbl2d) do
            io.write("[dbg]")
            for j,v in ipairs(t) do
                io.write("(" .. tostring(i) .. "," .. tostring(j) .. "): ")
                io.write(tostring(v))
                io.write("; ")
            end
            print("")
        end
    end
end

function create_tbl(a, initial)
    local tbl = {}
    for i=1,a do
        tbl[i] = initial
    end
    return tbl
end

function create_2d_tbl(a, b, initial)
    local tbl = {}
    for i=1,a do
        local t = {}
        for j=1,b do
            t[j] = initial
        end
        tbl[i] = t
    end
    return tbl
end

function parse_problem()
    local N, M = io.read("n", "n")
    local bridges = {}
    for i=1, M do
        local a, b = io.read("n", "n")
        bridges[i] = {a, b}
    end
    return N, M, bridges
end

perf = 0

function main()
    local N, M, bridges = parse_problem()
    parents = {}
    for i=1,N do
        parents[i] = -1
    end
    local function find_union(a, b)
        local x, y = a, b
        while parents[x] > 0 do
            x = parents[x]
        end
        while parents[y] > 0 do
            y = parents[y]
        end
        local size_a, size_b = -parents[x], -parents[y]
        if x ~= y then
            if size_b > size_a then
                x, y = y, x
            end
            -- size(x) > size(y)
            -- x is new root of y
            parents[x] = parents[x] + parents[y]
            parents[y] = x
        end
        return x == y, size_a, size_b
    end
    dbgpr(N, M)
    dbgpr("====bridges")
    dbgpr_t2d(bridges)
    local ans_tbl = {}
    ans_tbl[M] = N * (N - 1) // 2
    dbgpr("M, ansM: ", M, ans_tbl[M])
    for i=M,1,-1 do
        local a, b = bridges[i][1], bridges[i][2]
        local same_group, size_a, size_b = find_union(a, b)
        --dbgpr(i, a, b, same_group, size_a, size_b, size_a * size_b)
        if same_group then
            ans_tbl[i-1] = ans_tbl[i]
        else
            ans_tbl[i-1] = ans_tbl[i] - size_a * size_b
        end
    end
    for i=1,M do
        print(ans_tbl[i])
    end
end

main()

Score : 400 points

Problem Statement

There are N islands and M bridges.

The i-th bridge connects the A_i-th and B_i-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.

Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.

For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

All pairs (A_i, B_i) are distinct.

The inconvenience is initially 0.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 1

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

Sample Output 1

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4).

Sample Input 2

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

Sample Output 2

8
9
12
14
15

Sample Input 3

2 1
1 2

Sample Output 3

1


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function dbgpr_t2d(tbl2d)
    if DBG then
        dbgpr(tbl2d)
        for i,t in ipairs(tbl2d) do
            io.write("[dbg]")
            for j,v in ipairs(t) do
                io.write("(" .. tostring(i) .. "," .. tostring(j) .. "): ")
                io.write(tostring(v))
                io.write("; ")
            end
            print("")
        end
    end
end
function create_tbl(a, initial)
    local tbl = {}
    for i=1,a do
        tbl[i] = initial
    end
    return tbl
end

function create_2d_tbl(a, b, initial)
    local tbl = {}
    for i=1,a do
        local t = {}
        for j=1,b do
            t[j] = initial
        end
        tbl[i] = t
    end
    return tbl
end

function parse_problem()
    local N, M = io.read("n", "n")
    local bridges = {}
    local map = create_2d_tbl(N, N, 0)
    for i=1, M do
        local a, b = io.read("n", "n")
        bridges[i] = {a, b}
        assert(map[a][b] == 0)
        map[a][b] = 1
        map[b][a] = 1
    end
    return N, M, bridges, map
end

function reachable(N, map, a, b)
    local visited = create_tbl(N, 0)
    local function dfs(src)
        for dest=1, N do
            if src ~= dest and visited[dest] == 0 then
                if map[src][dest] ~= 0 then
                    visited[dest] = 1
                    dfs(dest)
                end
            end
        end
    end
    visited[a] = 1
    dfs(a)
    local count = 0
    for i=1,N do
        if visited[i] ~= 0 then
            count = count + 1
        end
    end
    --dbgpr_t(visited)
    return visited[b] ~= 0, count
end

function main()
    local N, M, bridges, map = parse_problem()
    dbgpr(N, M)
    dbgpr("====bridges")
    dbgpr_t2d(bridges)
    dbgpr("====map")
    dbgpr_t2d(map)
    local inconvenience = 0
    for i=1,M do
        local a, b = bridges[i][1], bridges[i][2]
        -- bridge collapses
        map[a][b] = 0
        map[b][a] = 0
        -- still reachable?
        local r, count_a = reachable(N, map, a, b)
        local _, count_b = reachable(N, map, b, a)
        dbgpr("i,a,b: ",i,a,b, " r: ", r, " count: ", count_a, count_b)
        if not r then
            inconvenience = inconvenience + count_a * count_b
        end
        print(inconvenience)
    end
end

main()


local str = io.read()
local y, m, d = str:match("(%d+)/(%d+)/(%d+)")
y,m,d=tonumber(y),tonumber(m),tonumber(d)
if(y < 2019) then print("Heisei")
elseif(2019 < y) then print("TBD")
else
  if(m <= 4) then print("Heisei")
  else print("TBD") end
end

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


local y,z0,m,z1,d = io.read("n",1,"n",1,"n")
if y >= 2019 and m >= 5 then
    print("TBD")
else
    print("Heisei")
end


local n = io.read("*n", "*l")
local sum = 0
for i = 1, n do
  local str = io.read()
  local xs, u = str:match("([%d%.]+) (%w+)")
  local x = tonumber(xs)
  if(u == "BTC") then
    sum = sum + 380000 * x
  else
    sum = sum + x
  end
end
print(string.format("%.8f", sum))

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.