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

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

for i = 1, m do
  local l, r, d = ior:read("*n", "*n", "*n")
  if(isok) then
    local lr, rr = getroot(l), getroot(r)
    if(hasinfo[l] and hasinfo[r]) then
      if(lr ~= rr) then
        local diff = pos[r] - (pos[l] + d)
        if(grouplen[lr] < grouplen[rr]) then
          for j = 1, #groupidxes[lr] do
            local tmpidx = groupidxes[lr][j]
            table.insert(groupidxes[rr], tmpidx)
            pos[tmpidx] = pos[tmpidx] + diff
          end
          grouplen[rr] = grouplen[rr] + grouplen[lr]
          grouplen[lr] = 0
          parent[l], parent[r], parent[lr] = rr, rr, rr
        else
          for j = 1, #groupidxes[rr] do
            local tmpidx = groupidxes[rr][j]
            table.insert(groupidxes[lr], tmpidx)
            pos[tmpidx] = pos[tmpidx] - diff
          end
          grouplen[lr] = grouplen[rr] + grouplen[lr]
          grouplen[rr] = 0
          parent[l], parent[r], parent[rr] = lr, lr, lr
        end
      else
        parent[l], parent[r], parent[lr] = rr, rr, rr
        isok = (d == pos[r] - pos[l])
      end
    elseif(hasinfo[r]) then
      parent[l], parent[r], parent[lr] = rr, rr, rr
      grouplen[l] = 0
      grouplen[rr] = grouplen[rr] + 1
      table.insert(groupidxes[rr], l)
      hasinfo[l] = true
      pos[l] = pos[r] - d
    elseif(hasinfo[l]) then
      parent[l], parent[r], parent[rr] = lr, lr, lr
      grouplen[lr] = grouplen[lr] + 1
      grouplen[r] = 0
      table.insert(groupidxes[lr], r)
      hasinfo[r] = true
      pos[r] = pos[l] + d
    else
      parent[l], parent[r], parent[rr] = lr, lr, lr
      grouplen[l] = 2
      grouplen[r] = 0
      table.insert(groupidxes[l], r)
      hasinfo[l], hasinfo[r] = true, true
      pos[l] = 0
      pos[r] = d
    end
  end
end
print(isok and "Yes" or "No")

Score : 400 points

Problem Statement

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

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

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

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

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

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

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

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

D_i are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

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

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

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

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local mod = 1000000007
local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 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 bsub(x, y)
  return x < y and x - y + mod or x - y
end

local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local s, t = io.read("*n", "*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b, d = io.read("*n", "*n", "*n")
  edge[a][b], edge[b][a] = d, d
end

local inf = 1000000000 * 99999 + 1
local slen, tlen = {}, {}
local sway = {}
local asked = {}
for i = 1, n do
  slen[i], tlen[i] = inf, inf
  asked[i] = false
  sway[i] = 0
end
slen[s] = 0
sway[s] = 1
asked[n + 1] = true

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return slen[x] < slen[y] and x or y
  end
end

local st = SegTree.new(n, mergefunc, n + 1)

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    if slen[src] + cost < slen[dst] then
      slen[dst] = slen[src] + cost
      sway[dst] = sway[src]
      st:update(dst)
    elseif slen[src] + cost == slen[dst] then
      sway[dst] = badd(sway[dst], sway[src])
    end
  end
end

local stlen = slen[t]
local ret = bmul(sway[t], sway[t])
if inf <= stlen then
  print(0)
  os.exit()
end

local function mergefunc2(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return tlen[x] < tlen[y] and x or y
  end
end
st.func = mergefunc2
local tway = {}
for i = 1, n do
  asked[i] = false
  tway[i] = 0
end
tlen[t] = 0
tway[t] = 1
st:updateAll()

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  if tlen[src] * 2 == stlen then
    local z = bmul(sway[src], tway[src])
    z = bmul(z, z)
    ret = bsub(ret, z)
  end
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    local tlsrc = tlen[src]
    if tlsrc * 2 < stlen and stlen < (tlsrc + cost) * 2
    and tlsrc + cost + slen[dst] == stlen then
      local z = bmul(tway[src], sway[dst])
      z = bmul(z, z)
      ret = bsub(ret, z)
    end
    if tlen[src] + cost < tlen[dst] then
      tlen[dst] = tlen[src] + cost
      tway[dst] = tway[src]
      st:update(dst)
    elseif tlen[src] + cost == tlen[dst] then
      tway[dst] = badd(tway[dst], tway[src])
    end
  end
end
print(ret)

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

Score : 700 points

Problem Statement

We have a graph with N vertices and M edges, and there are two people on the graph: Takahashi and Aoki.

The i-th edge connects Vertex U_i and Vertex V_i.
The time it takes to traverse this edge is D_i minutes, regardless of direction and who traverses the edge (Takahashi or Aoki).

Takahashi departs Vertex S and Aoki departs Vertex T at the same time. Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the shortest time possible.
Find the number of the pairs of ways for Takahashi and Aoki to choose their shortest paths such that they never meet (at a vertex or on an edge) during the travel, modulo 10^9 + 7.

Constraints

1 \leq N \leq 100 000

1 \leq M \leq 200 000

1 \leq S, T \leq N

S \neq T

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq D_i \leq 10^9 (1 \leq i \leq M)

If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j).

U_i \neq V_i (1 \leq i \leq M)

D_i are integers.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M

Output

Print the answer.

Sample Input 1

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

Sample Output 1

2

There are two ways to choose shortest paths that satisfies the condition:

Takahashi chooses the path 1 \rightarrow 2 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 4 \rightarrow 1.

Takahashi chooses the path 1 \rightarrow 4 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 2 \rightarrow 1.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2

Sample Input 4

8 13
4 2
7 3 9
6 2 3
1 6 4
7 6 9
3 8 9
1 2 2
2 8 12
8 6 9
2 5 5
4 2 18
5 3 7
5 1 515371567
4 8 6

Sample Output 4

6

Submitted source-code file size, in bytes.

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

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


s = io.read()
s = s:gsub(" ", "")
s = tonumber(s)
f = false
for i = 1, 400 do
  if i * i == s then f = true break end
end
print(f and "Yes" or "No")

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


a=io.read("n")
b=io.read("n")
local res=0
if b==100 then
  res=a*1000+b
  elseif b>=10 and b<=99 then
    res=a*100+b
  else
    res=a*10+b
end
local _,k=math.modf(math.sqrt(res))
if k==0 then
  print("Yes")
else
  print("No")
end


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

Score : 300 points

Problem Statement

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

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

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

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

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

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

Sample Input 2

1
2 100 100

Sample Output 2

No

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

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


local n = io.read('*n')
io.read('*l')
local ret = true
for i = 1, n do
	local t = io.read('*n')
	local x = io.read('*n')
	local y = io.read('*n')
	if (t - (x + y)) < 0 then
		ret = false
	end
	if (t - (x + y)) % 2 > 0 then
		ret = false
	end
end
print(ret and 'Yes' or 'No')


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

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

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


local n, a, b = io.read("*n", "*n", "*n")
if (b - a) % 2 == 0 then
  print("Alice")
else
  print("Borys")
end

Score : 300 points

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first.
The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1.
Note that it's disallowed to move the token outside the strip or to the cell with the other player's token.
In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints

2 \leq N \leq 100

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

Sample Input 1

5 2 4

Sample Output 1

Alice

Alice can move her token to cell 3.
After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5.
Then, Alice moves her token to cell 4. Borys can't make a move and loses.

Sample Input 2

2 1 2

Sample Output 2

Borys

Alice can't make the very first move and loses.

Sample Input 3

58 23 42

Sample Output 3

Borys


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

Score : 500 points

Problem Statement

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

Form groups consisting of A_i children each!

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

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

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

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

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

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

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

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

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

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

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

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

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

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

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


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


local S = io.read("*l")
local new_S = S:gsub("7", "8", 1)
print(new_S)

Score : 100 points

Problem Statement

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

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

Constraints

S is a string of length 10.

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

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


n = io.read("*n")
t = {}
for i = 1, n do
  a = io.read("*n")
  t[a] = true
end
c = 0
for k, v in pairs(t) do c = c + 1 end
print(c)

Score : 200 points

Problem Statement

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

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

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

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

Sample Input 1

4
10
8
8
6

Sample Output 1

3

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

Sample Input 2

3
15
15
15

Sample Output 2

1

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

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


local n = io.read("*n")
local d = {}
for i = 1, n do
    local num = io.read("*n")
    if d[num] == nil then
        d[num] = true
    end
end

local count = 0
for key, value in pairs(d) do
    count = count + 1
end
print(count)


t = {}
n = io.read("*n")
cnt = 0
for i = 1, n do
    a = io.read("*n")
    if(t[a] == nil) then t[a], cnt = true, cnt + 1 end
end
print(cnt)


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


Score : 100 points

Problem Statement

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

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

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

Sample Input 1

21

Sample Output 1

27

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

Sample Input 2

12

Sample Output 2

36


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

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


A=io.read("n")
B=io.read("n")
S=io.read()
local res="Yes"
for i=1,#S do
  if i==A+1 then
    if string.sub(S,i,i)~="-" then
      res="No"
    end
  elseif string.sub(S,i,i)<"0" or string.sub(S,i,i)>"9" then
      res="No"
  end
end
print(res)


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

Score : 300 points

Problem Statement

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

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

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

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

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

Here, it is guaranteed that F_i divides S_i.

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

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

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

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

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


Q=io.read("n")
l={}
r={}
for i=1,Q do
  l[i]=io.read("n")
  r[i]=io.read("n")
end
function prime(o)
  local res=true
  if  o>2 then
    for i=2,o/2 do
      if o%i==0 then
        res=false
      end
    end
  end
  return res
end
for i=1,Q do
  local count=0
  for j=l[i],r[i],2 do
    if prime(j) and prime((j+1)//2) then
      count=count+1
    end
  end
  print(count)
end

Score : 400 points

Problem Statement

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

You are given Q queries.

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

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

1
3 7

Sample Output 1

2

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

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

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

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

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

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

Sample Output 3

4
4
1
1
1
2


Q=io.read("n")
l={}
r={}
for i=1,Q do
  l[i]=io.read("n")
  r[i]=io.read("n")
end
function prime(o)
  local res=true
  if o==1 or o==2 then
    return res
  elseif o>2 then
    for i=2,o-1 do
      if o%i==0 then
        res=false
      end
    end
    return res
  end
end
for i=1,Q do
  local count=0
  for j=l[i],r[i] do
    if prime(j) and prime((j+1)//2) then
      count=count+1
    end
  end
  print(count)
end


a,b,c,d=io.read():match("(.+)%s(.+)%s(.+)%s(.+)")
print(a+b==c+d and"Balanced"or a+b>c+d and"Left"or"Right")

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


A,B,C,D=io.read("n","n","n","n")
if A+B>C+D then
  print("Left")
  elseif A+B==C+D then
  print("Balanced")
  else
  print("Right")
end


local s = io.read()
local n = #s
local prv = s:sub(1, 1)
local nxt = ""
local from = 1
local ret = 1
local mma, mmi = math.max, math.min
for i = 2, n do
  nxt = s:sub(i, i)
  if prv ~= nxt then
    ret = mma(ret, mmi(i - 1, n + 1 - from))
    prv = nxt
    from = i
  end
end
ret = mma(ret, mmi(n, n + 1 - from))
print(ret)

Score : 500 points

Problem Statement

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

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

Constraints

1\leq |S|\leq 10^5

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

010

Sample Output 1

2

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

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

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

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

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


Score : 300 points

Problem Statement

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

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

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

Find the maximum possible length of the sequence.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


x,y=io.read("*n","*n")
counter=0
while x<=y do
    x=x*2
    counter=counter+1
end
print(counter)


x,y=io.read("*n","*n")
counter=0
while x<y do
    x=x*2
    counter=counter+1
end
print(counter)


-- > <

x, y = io.read('n'), io.read('n')

ans = 0
while x <= y do
    ans = ans + 1
    x = x * 2
end

print(ans)


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

Score : 100 points

Problem Statement

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

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

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

Sample Input 2

7 4

Sample Output 2

6

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

Sample Input 3

5 5

Sample Output 3

5


n = io.read("*n")
rem = 0
t = {}
s = {}
for i = 1, n do
    a = io.read("*n")
    if(n < a) then
        rem = rem + 1
    elseif(s[a] ~= nil) then
        rem = rem + 1
    else
        if(t[a] == nil) then
            t[a] = 1
        else
            t[a] = t[a] + 1
        end
        if(t[a] == a) then
            s[a] = true
            t[a] = 0
        end
    end
end
for k, v in pairs(t) do
    rem = rem + v
end
print(rem)

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

4
3 3 3 3

Sample Output 1

1

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

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

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

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

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

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


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


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

Score : 500 points

Problem Statement

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

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

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

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

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

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

Input

Input is given from Standard Input in the following format:

s
x y

Output

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

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

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

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

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

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

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

Sample Input 6

TTTT
1 0

Sample Output 6

No


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

local tb = {}
local numT = 0
for i = 1, string.len(str) do
    if string.sub(str,i,i) == "F" then
        numT = numT + 1
    end
    table.insert(tb,string.sub(str,i,i))
end

if finalA + finalB > numT then
    print("No")
    return
end

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

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

    if idx > #tb then
        return false
    end

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

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

end

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


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

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

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

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

    if idx > #tb then
        return false
    end

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

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

end

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


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

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

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

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

    if idx > #tb then
        return false
    end

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

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

end

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


N=io.read("n")
A={}
for i=1,N do
  A[i]=io.read("n")
end
local count=0
local t=A[#A]
local sign=0
while true do
  for i=1,N do
    if A[i]%2==1 then
      sign=1
      break
    end
    A[i]=A[i]//2
  end
  if A[#A]==t//2 then
    count=count+1
    t=A[#A]
  end
  if sign==1 then
    break
  end
end
print(count)

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
8 12 40

Sample Output 1

2

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

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

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

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

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

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


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


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

b={}
for _,j in pairs(a) do
    table.insert(b,j)
end
table.sort(b)

ball=0
if #b-k>0 then
    for i=1,#b-k do
        ball=ball+b[i]
    end
end
print(ball)

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

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

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

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

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

3


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

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

for _, v in pairs(bucket) do    -- > <
    coll[#coll + 1] = v
end

table.sort(coll)

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

print(ans)


Score : 100 points

Problem Statement

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

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

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

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

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

Sample Input 1

7 17 120

Sample Output 1

119

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

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

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


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

Score : 200 points

Problem Statement

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

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

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

12

Sample Output 1

Yes

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

Sample Input 2

57

Sample Output 2

No

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

Sample Input 3

148

Sample Output 3

No


local mfl, mma = math.floor, math.max
local n = io.read("*n")
local f = {}
for i = 1, n * 10 do
  f[i] = (io.read("*n") == 1)
end
local p = {}
for i = 1, n * 11 do
  p[i] = io.read("*n")
end

local score = {}
for i = 1, 1023 do
  local cnt = {}
  local ti = i
  for k = 1, n do cnt[k] = 1 end
  for j = 1, 10 do
    local flg = ti % 2
    ti = mfl(ti / 2)
    if flg == 1 then
      for k = 1, n do
        if f[(k - 1) * 10 + j] then
          cnt[k] = cnt[k] + 1
        end
      end
    end
  end
  score[i] = 0
  for k = 1, n do
    score[i] = score[i] + p[(k - 1) * 11 + cnt[k]]
  end
end
local ret = score[1]
for i = 2, 1023 do
  ret = mma(ret, score[i])
end
print(ret)

Score : 300 points

Problem Statement

Joisino is planning to open a shop in a shopping street.

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

There are already N stores in the street, numbered 1 through N.

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

Constraints

1≤N≤100

0≤F_{i,j,k}≤1

For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.

-10^7≤P_{i,j}≤10^7

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}
:
F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}
P_{1,0} ... P_{1,10}
:
P_{N,0} ... P_{N,10}

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

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

Sample Output 1

8

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

Sample Input 2

2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1

Sample Output 2

-2

Note that a shop must be open during at least one period, and the profit may be negative.

Sample Input 3

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

Sample Output 3

23


local n, c = io.read("*n", "*n")
local tbl = {}
for i = 1, n do
  local tmp = {}
  tmp.s, tmp.t, tmp.c = io.read("*n", "*n", "*n")
  tbl[i] = tmp
end
table.sort(tbl, function(a, b) return a.s < b.s end)
local rec_end = {tbl[1].t}
local rec_ch = {tbl[1].c}

for i = 2, n do
  local fpos = nil
  for ir = 1, #rec_end do
    if rec_end[ir] == tbl[i].s and rec_ch[ir] == tbl[i].c then
      rec_end[ir] = tbl[i].t
      fpos = ir
      break
    elseif rec_end[ir] < tbl[i].s then
      if not fpos then
        fpos = ir
      else
        if rec_end[ir] < rec_end[fpos] then
          fpos = ir
        end
      end
    end
  end
  if fpos then
    rec_end[fpos] = tbl[i].t
    rec_ch[fpos] = tbl[i].c
  else
    table.insert(rec_end, tbl[i].t)
    table.insert(rec_ch, tbl[i].c)
  end
end
print(#rec_end)

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2


local n, q = io.read("*n", "*n")
for i = 1, q do
  local v, w = io.read("*n", "*n")
  if n == 1 then
    print(math.min(v, w))
  else
    while v ~= w do
      if v < w then
        w = (w + n - 2) // n
      else
        v = (v + n - 2) // n
      end
    end
    print(v)
  end
end

Score : 100 points

Problem Statement

You are given an integer N. Consider an infinite N-ary tree as shown below:

Figure: an infinite N-ary tree for the case N = 3

As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index 1. For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.

Regarding this tree, process Q queries. The i-th query is as follows:

Find the index of the lowest common ancestor (see Notes) of Vertex v_i and w_i.

Notes

In a rooted tree, the lowest common ancestor (LCA) of Vertex v and w is the farthest vertex from the root that is an ancestor of both Vertex v and w. Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex 5 and 7 is Vertex 2, the LCA of Vertex 8 and 11 is Vertex 1, and the LCA of Vertex 3 and 9 is Vertex 3.

Constraints

1 ≤ N ≤ 10^9

1 ≤ Q ≤ 10^5

1 ≤ v_i < w_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N Q
v_1 w_1
:
v_Q w_Q

Output

Print Q lines. The i-th line (1 ≤ i ≤ Q) must contain the index of the lowest common ancestor of Vertex v_i and w_i.

Sample Input 1

3 3
5 7
8 11
3 9

Sample Output 1

2
1
3

The queries in this case correspond to the examples shown in Notes.

Sample Input 2

100000 2
1 2
3 4

Sample Output 2

1
1


local n, k = io.read("*n", "*n")
local w, d = {}, {}
for i = 1, n do
  w[i], d[i] = io.read("*n", "*n")
end
local min, max = 0, 2
for i = 1, 6 do
  max = max * 1000
end

local function solve(x)
  local c = 0
  for i = 1, n do
    if w[i] <= x then
      c = c + (x - w[i]) // d[i] + 1
    end
  end
  return k <= c
end
if solve(min) then
  print(min)
else
  while 1 < max - min do
    local mid = (min + max) // 2
    if solve(mid) then
      max = mid
    else
      min = mid
    end
  end
  print(max)
end

Score : 100 points

Problem Statement

In your garden, there is a long and narrow flowerbed that stretches infinitely to the east. You have decided to plant N kinds of flowers in this empty flowerbed. For convenience, we will call these N kinds of flowers Flower 1, 2, …, N. Also, we will call the position that is p centimeters from the west end of the flowerbed Position p.

You will plant Flower i (1 ≤ i ≤ N) as follows: first, plant one at Position w_i, then plant one every d_i centimeters endlessly toward the east. That is, Flower i will be planted at the positions w_i, w_i + d_i, w_i + 2 d_i, … Note that more than one flower may be planted at the same position.

Find the position at which the K-th flower from the west is planted. If more than one flower is planted at the same position, they are counted individually.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^9

1 ≤ w_i ≤ 10^{18}

1 ≤ d_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
w_1 d_1
:
w_N d_N

Output

When the K-th flower from the west is planted at Position X, print the value of X. (The westmost flower is counted as the 1-st flower.)

Sample Input 1

2 6
20 10
25 15

Sample Output 1

50

Two kinds of flowers are planted at the following positions:

Flower 1: Position 20, 30, 40, 50, 60, …

Flower 2: Position 25, 40, 55, 70, 85, …

The sixth flower from the west is the Flower 1 planted at Position 50. Note that the two flowers planted at Position 40 are counted individually.

Sample Input 2

3 9
10 10
10 10
10 10

Sample Output 2

30

Three flowers are planted at each of the positions 10, 20, 30, … Thus, the ninth flower from the west is planted at Position 30.

Sample Input 3

1 1000000000
1000000000000000000 1000000000

Sample Output 3

1999999999000000000


Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

1 \leq |S| \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.

Sample Input 3

AAKIAHBAARA

Sample Output 3

NO


local s = io.read()
local n = #s
local pos = 1
local f = false
while pos do
  pos = s:find("KIH", pos)
  if not pos then break end
  if n - 4 < pos then break end
  pos = pos + 3
  local bpos = s:find("B", pos)
  if bpos then
    if bpos == pos or (bpos == pos + 1 and s:sub(pos, pos) == "A") then
      local rpos = s:find("R", bpos)
      if rpos then
        if rpos == bpos + 1 or (rpos == bpos + 2 and s:sub(bpos + 1, bpos + 1) == "A") then
          f = true
          break
        end
      end
    end
  end
end
print(f and "YES" or "NO")


local s = io.read()
local n = #s
local t = {0, 0, 0}
local sstr = ""
for i = 1, n do
  sstr = s:sub(i, i)
  if(sstr == "a") then t[1] = t[1] + 1
  elseif(sstr == "b") then t[2] = t[2] + 1
  else t[3] = t[3] + 1 end
end
table.sort(t)
print(t[3] - t[1] <= 1 and "YES" or "NO")

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


local N=io.read("n")
a = N%10
b = N//10%10
c = N//100%10
d = N//1000
if a==b and b==c or b==c and c==d then
  print("Yes")
  else
  print("No")
end

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


local n = io.read("*n")
local a, b = 2, 1
for i = 1, n - 1 do
  a, b = b, a + b
end
print(b)

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


N=io.read("n")
L={}
L[0]=2
L[1]=1
if N>=2 then
  for i=2,N do
    L[i]=L[i-1]+L[i-2]
  end
end
print(L[N])


local function ston(x,i)
    return tonumber(x:sub(i,i))
end
local function eo(i)
    if i%2==0 then
        return 1
    else
        return -1
    end
end
local function pm(i)
    if i%2==0 then
        return "+"
    else
        return "-"
    end
end

x=io.read()
a=ston(x,1)
b=ston(x,2)
c=ston(x,3)
d=ston(x,4)

for i=1,2 do
    for j=1,2 do
        for k=1,2 do
            if a+b*eo(i)+c*eo(j)+d*eo(k)==7 then
                print(a..pm(i)..b..pm(j)..c..pm(k)..d.."=7")
                return
            end
        end
    end
end

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


local s = io.read()
local a = {}
for i = 1, 4 do
  a[i] = tonumber(s:sub(i, i))
end
for i = 0, 7 do
  local sum = a[1]
  local ti = i
  sum = sum + a[2] * (2 * (ti % 2) - 1)
  ti = ti // 2
  sum = sum + a[3] * (2 * (ti % 2) - 1)
  ti = ti // 2
  sum = sum + a[4] * (2 * (ti % 2) - 1)
  if sum == 7 then
    io.write(a[1])
    local z = i
    for k = 2, 4 do
      io.write(z % 2 == 0 and "-" or "+")
      io.write(a[k])
      z = z // 2
    end
    io.write("=7\n")
    break
  end
end


A=io.read("n")
B=io.read("n")
C=io.read("n")
D=io.read("n")
if A+B+C+D==7 then
  print(string.format("%d+%d+%d+%d=7",A,B,C,D))
elseif A+B+C-D==7 then
  print(string.format("%d+%d+%d-%d=7",A,B,C,D))
  elseif A+B-C+D==7 then
    print(string.format("%d+%d-%d+%d=7",A,B,C,D))
    elseif A+B-C-D==7 then
      print(string.format("%d+%d-%d-%d=7",A,B,C,D))
      elseif A-B+C+D==7 then
        print(string.format("%d-%d+%d+%d=7",A,B,C,D))
        elseif A-B+C-D==7 then
          print(string.format("%d-%d+%d-%d=7",A,B,C,D))
          elseif A-B-C+D==7 then
             print(string.format("%d-%d-%d+%d=7",A,B,C,D))
             elseif A-B-C-D==7 then
               print(string.format("%d-%d-%d-%d=7",A,B,C,D))
end


local h,w=io.read("n","n")
local c={}
for i=0,9 do
    c[i]={}
    for j=0,9 do
        c[i][j]=io.read("n")
    end
end

--Warshall-Floyd法
for k=0,9 do
    for i=0,9 do
        for j=0,9 do
            c[i][j]=math.min(c[i][j],c[i][k]+c[k][j])
        end
    end
end

local counter=0
for i=1,h do
    for j=1,w do
        local input=io.read("n")
        if input>=0 then
            counter=counter+c[input][1]
        end
    end
end
print(counter)

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


x = io.read()
a, b = x:match("(%w) (%w)")
if a < b then
  print("<")
elseif a == b then
  print("=")
else
  print(">")
end

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


local x, y, z = io.read("*n", "*n", "*n")
x = x - z
print(x // (y + z))

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


N=io.read("n")
Z=io.read("n")
W=io.read("n")
a={}
b={}
for i=1,N do
  a[i]=io.read("n")
  b[i]=a[i]
end
table.sort(b)
t={}
t[1]=math.abs(a[N]-W)
t[2]=math.abs(b[1]-a[N])
t[3]=math.abs(b[N]-a[N])
t[4]=math.abs(a[N]-Z)
table.sort(t)
print(t[4])

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


function binary_search(tbl, key, imin, imax)
    if imax < imin then
        return imin
    end

    local imid = imin + math.floor((imax - imin)/2)
    if tbl[imid] > key then
        return binary_search(tbl, key, imin, imid - 1)
    elseif tbl[imid] < key then
        return binary_search(tbl, key, imid + 1, imax)
    else
        return imid
    end
end

function main()
    local input_nzw = string.gmatch(io.read(), "%d+")
    local n = tonumber(input_nzw())
    local z = tonumber(input_nzw())
    local w = tonumber(input_nzw())
    local a = {}
    for i in string.gmatch(io.read(), "%d+") do
        table.insert(a, tonumber(i))
    end

    if #a == 0 then
        return math.abs(z - w)
    end

    local sorted_nums = {a[#a]}
    local max_diff = math.abs(a[#a] - w)
    for i = #a - 1, 1, -1 do
        local idx = binary_search(sorted_nums, a[i], 1, #sorted_nums)
        local min_diff = 1000000000
        if idx <= #sorted_nums then
            min_diff = math.abs(sorted_nums[idx] - a[i])
        end
        if idx >= 2 then
            local diff = math.abs(sorted_nums[idx-1] - a[i])
            if diff < min_diff then
                min_diff = diff
            end
        end
        if min_diff > max_diff then
            max_diff = min_diff
        end
        table.insert(sorted_nums, idx, a[i])
    end
    return max_diff
end

print(main())


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

for i = n, 1, -1 do
  local sum = 0
  for j = i, n, i do
    sum = sum + a[j]
  end
  if sum < 0 then
    for j = i, n, i do
      a[j] = 0
    end
  end
end

total = 0
for i = 1, n do
  total = total + a[i]
end
io.write(total, '\n')

Score : 700 points

Problem Statement

We have N gemstones labeled 1 through N.

You can perform the following operation any number of times (possibly zero).

Select a positive integer x, and smash all the gems labeled with multiples of x.

Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan).
However, a_i may be negative, in which case you will be charged money.

By optimally performing the operation, how much yen can you earn?

Constraints

All input values are integers.

1 \leq N \leq 100

|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 amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000


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

for i = n, 1, -1 do
  local sum = 0
  for j = i, n, i do
    sum = sum + a[j]
  end
  if sum < 0 then
    for j = i, n, i do
      a[j] = 0
    end
  end
end

total = 0
for i = 1, n do
  total = total + a[i]
end
io.write(total, '\n')


a = io.read()
b = io.read()
if a:sub(1, 1) == b:sub(3, 3) and a:sub(2, 2) == b:sub(2, 2) and a:sub(3, 3) == b:sub(1, 1) then
  print("YES")
else
  print("NO")
end

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


local n = io.read("*n")
local z = math.ceil(math.sqrt(n))
if z * z <= n then
  print(z * z)
else
  print((z - 1) * (z - 1))
end

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


local n = io.read("*n", "*l")
local a, b, c = {}, {}, {}
local str = io.read()
local i = 1
local tn = tonumber
for val in string.gmatch(str, "%d+") do
  a[i] = tn(val)
  i = i + 1
end
str = io.read()
i = 1
for val in string.gmatch(str, "%d+") do
  b[i] = tn(val)
  i = i + 1
end
str = io.read()
i = 1
for val in string.gmatch(str, "%d+") do
  c[i] = tn(val)
  i = i + 1
end
table.sort(a)
table.sort(b)
table.sort(c)
local ab, bc = {}, {}
local bpos, cpos = 1, 1
local alast, blast = n, n
for j = 1, n do
  local tgt = a[j]
  local found = false
  for k = bpos, n do
    if(tgt < b[k]) then
      ab[j], bpos, found = k, k, true
      break
    end
  end
  if(not found) then
    alast = j - 1
    break
  end
end
for j = 1, n do
  local tgt = b[j]
  local found = false
  for k = cpos, n do
    if(tgt < c[k]) then
      bc[j], cpos, found = (n - k + 1), k, true
      break
    end
  end
  if(not found) then
    blast = j - 1
    break
  end
end
for i = blast + 1, n do
  bc[i] = 0
end
for i = blast - 1, 1, -1 do
  bc[i] = bc[i] + bc[i + 1]
end
local sum = 0
for i = 1, alast do
  sum = sum + bc[ab[i]]
end
print(sum)

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


do  --  Library
    table.map = function(t, f)
        local ret = {}
        for i = 1, #t do
            table.insert(ret, f(t[i]))
        end
        return ret
    end

    table.each = function(t, f)
        for i = 1, #t do
            f(t[i])
        end
    end

    table.sum = function(t)
        local sum = 0
        for i = 1, #t do
            sum = sum + t[i]
        end
        return sum
    end

    table.slice = function(t, b, e)
        local ret = {}
        for i = b, e do
            table.insert(ret, t[i])
        end
        return ret
    end

    string.split = function(str, sep)
        local ret = {}
        for i in string.gmatch(str, "[^"..sep.."]+") do
            table.insert(ret, i)
        end
        return ret
    end
end

calculate = nil
do
    local memo = {}
    local stages_to_str = function(stages)
        return table.concat(table.map(stages, tostring), ",")
    end

    function calculate(top_num, under_stages)
        local str_under_stages = stages_to_str(under_stages)
        if not memo[top_num] then
            memo[top_num] = {}
        elseif memo[top_num][str_under_stages] then
            return memo[top_num][str_under_stages]
        end

        if #under_stages == 0 then
            return 1
        end

        memo[top_num][str_under_stages] = table.sum(table.map(under_stages[1], function(size)
            if size <= top_num then
                return 0
            end

            return calculate(size, table.slice(under_stages, 2, #under_stages))
        end))
        return memo[top_num][str_under_stages]
    end
end

function main()
    local n = tonumber(io.read())
    local stages = {}
    for i = 1, 3 do
        table.insert(stages, table.map(string.split(io.read(), " "), tonumber))
    end

    table.each(stages, function(t)
        table.sort(t, function(a, b) return b < a end)
    end)

    print(calculate(0, stages))
end

main()


local k, n = io.read("*n", "*n")
local t = {}

if k % 2 == 0 then
  table.insert(t, k / 2)
  for i = 1, n - 1 do table.insert(t, k) end
else
  local hk = (k + 1) / 2
  for i = 1, n do t[i] = hk end
  local decnum = math.ceil((n - 1) / 2)
  for i = 1, decnum do
    for j = n, 1, -1 do
      if 0 < t[j] then
        t[j] = t[j] - 1
        for s = j + 1, n do
          t[s] = k
        end
        break
      end
    end
  end
  for i = 1, n do
    if t[i] == 0 then
      for j = i, n do
        table.remove(t)
      end
      break
    end
  end
end

print(table.concat(t, " "))

Score : 800 points

Problem Statement

In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 and N (inclusive) consisting of integers between 1 and K (inclusive) are listed.

Let the total number of sequences listed in FEIS be X. Among those sequences, find the (X/2)-th (rounded up to the nearest integer) lexicographically smallest one.

Constraints

1 \leq N,K \leq 3 × 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

K N

Output

Print the (X/2)-th (rounded up to the nearest integer) lexicographically smallest sequence listed in FEIS, with spaces in between, where X is the total number of sequences listed in FEIS.

Sample Input 1

3 2

Sample Output 1

2 1

There are 12 sequences listed in FEIS: (1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3).
The (12/2 = 6)-th lexicographically smallest one among them is (2,1).

Sample Input 2

2 4

Sample Output 2

1 2 2 2

Sample Input 3

5 14

Sample Output 3

3 3 3 3 3 3 3 3 3 3 3 3 2 2


Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


N=io.read("n")
K=io.read("n")
local int=1
for i=1,N do
  if int<=K then
    int=int*2
    else
    int=int+K
  end
end
print(int)

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


local n = io.read("*n")
local tsum = 0
local t = {}
local vmax = {}
for i = 1, n do
  t[i] = 2 * io.read("*n")
  tsum = tsum + t[i]
end
local mmi, mma = math.min, math.max
for i = 1, tsum do
  vmax[i] = mmi(i / 2, (tsum - i) / 2)
end
local tprev = 0
for i = 1, n do
  local v = io.read("*n")
  for j = 1, tprev - 1 do
    vmax[j] = mmi(vmax[j], v + (tprev - j) / 2)
  end
  for j = mma(1, tprev), tprev + t[i] do
    vmax[j] = mmi(vmax[j], v)
  end
  tprev = tprev + t[i]
  for j = tprev + 1, tsum do
    vmax[j] = mmi(vmax[j], v + (j - tprev) / 2)
  end
end
local dist = vmax[1]
for i = 2, tsum do
  dist = dist + vmax[i] + vmax[i - 1]
end
print(dist / 4)

Score : 400 points

Problem Statement

In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.

In the plan developed by the president Takahashi, the trains will run as follows:

A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.

In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.

According to the specifications of the trains, the acceleration of a train must be always within ±1m/s^2. Additionally, a train must stop at the beginning and the end of the run.

Find the maximum possible distance that a train can cover in the run.

Constraints

1 \leq N \leq 100

1 \leq t_i \leq 200

1 \leq v_i \leq 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 t_2 t_3 … t_N
v_1 v_2 v_3 … v_N

Output

Print the maximum possible that a train can cover in the run.

Output is considered correct if its absolute difference from the judge's output is at most 10^{-3}.

Sample Input 1

1
100
30

Sample Output 1

2100.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.

In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.

In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.

The total distance covered is 450 + 1200 + 450 = 2100 meters.

Sample Input 2

2
60 50
34 38

Sample Output 2

2632.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 34 seconds, it accelerates at a rate of 1m/s^2, covering 578 meters.

In the subsequent 26 seconds, it maintains the velocity of 34m/s, covering 884 meters.

In the subsequent 4 seconds, it accelerates at a rate of 1m/s^2, covering 144 meters.

In the subsequent 8 seconds, it maintains the velocity of 38m/s, covering 304 meters.

In the last 38 seconds, it decelerates at the acceleration of -1m/s^2, covering 722 meters.

The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.

Sample Input 3

3
12 14 2
6 2 7

Sample Output 3

76.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 6 seconds, it accelerates at a rate of 1m/s^2, covering 18 meters.

In the subsequent 2 seconds, it maintains the velocity of 6m/s, covering 12 meters.

In the subsequent 4 seconds, it decelerates at the acceleration of -1m/s^2, covering 16 meters.

In the subsequent 14 seconds, it maintains the velocity of 2m/s, covering 28 meters.

In the last 2 seconds, it decelerates at the acceleration of -1m/s^2, covering 2 meters.

The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.

Sample Input 4

1
9
10

Sample Output 4

20.250000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 4.5 seconds, it accelerates at a rate of 1m/s^2, covering 10.125 meters.

In the last 4.5 seconds, it decelerates at the acceleration of -1m/s^2, covering 10.125 meters.

The total distance covered is 10.125 + 10.125 = 20.25 meters.

Sample Input 5

10
64 55 27 35 76 119 7 18 49 100
29 19 31 39 27 48 41 87 55 70

Sample Output 5

20291.000000000000


local s = io.read()
local n = #s
local cnt = 0
local t = {}
for i = 1, n do
  local sstr = s:sub(i, i)
  if sstr == "x" then
    cnt = cnt + 1
  else
    local tmp = {}
    tmp.c = cnt
    tmp.s = sstr
    table.insert(t, tmp)
    cnt = 0
  end
end
local ret = 0
local mab = math.abs
local nt = #t
local isok = true

local half = nt // 2
for i = 1, half do
  if t[i].s ~= t[nt + 1 - i].s then
    isok = false
    break
  else
    if i == 1 then
      ret = ret + mab(t[i].c - cnt)
    else
      ret = ret + mab(t[i].c - t[nt + 2 - i].c)
    end
  end
end
if nt % 2 == 1 then
  if nt == 1 then
    ret = ret + mab(t[1].c - cnt)
  else
    ret = ret + mab(t[half + 1].c - t[half + 2].c)
  end
end
print(isok and ret or -1)

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


a,b,c=io.read():match("(.+)%s(.+)%s(.+)")
print(a==b and c or b==c and a or b)

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


local n, m = io.read("*n", "*n")
local t = {}
local as, bs = {}, {}
for i = 1, n do t[i] = {} end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  as[i], bs[i] = a, b
  t[a][b], t[b][a] = true, true
end

local function check(tbl)
  local reached = {true}
  for i = 2, n do reached[i] = false end
  local reachnum = 1
  local tasks = {1}
  local tasknum, done = 1, 0
  while done < tasknum do
    done = done + 1
    local idx = tasks[done]
    for k, v in pairs(tbl[idx]) do
      if v and not reached[k] then
        table.insert(tasks, k)
        reached[k] = true
        reachnum = reachnum + 1
        tasknum = tasknum + 1
      end
    end
  end
  return n == reachnum
end
local ret = 0
for i = 1, m do
  t[as[i]][bs[i]], t[bs[i]][as[i]] = false, false
  if not check(t) then ret = ret + 1 end
  t[as[i]][bs[i]], t[bs[i]][as[i]] = true, true
end
print(ret)

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


local n=io.read("*n","*l")
local d={}
for i=1,n do
    local x=io.read("*n")
    if not d[x] then
        d[x]=1
    else
        d[x]=d[x]+1
    end
end
local m=io.read("*n","*l")
local t={}
for i=1,m do
    local y=io.read("*n")
    if not t[y] then
        t[y]=1
    else
        t[y]=t[y]+1
    end
end

checker=true
for i,_ in pairs(t) do
    if d[i] then
        if t[i]>d[i] then
            checker=false
        end
    else
        checker=false
    end
end
print(checker and "YES" or "NO")

Score : 200 points

Problem Statement

Rng is preparing a problem set for a qualification round of CODEFESTIVAL.

He has N candidates of problems. The difficulty of the i-th candidate is D_i.

There must be M problems in the problem set, and the difficulty of the i-th problem must be T_i. Here, one candidate of a problem cannot be used as multiple problems.

Determine whether Rng can complete the problem set without creating new candidates of problems.

Constraints

1 \leq N \leq 200,000

1 \leq D_i \leq 10^9

1 \leq M \leq 200,000

1 \leq T_i \leq 10^9

All numbers in the input are integers.

Partial Score

100 points will be awarded for passing the test set satisfying N \leq 100 and M \leq 100.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N
M
T_1 T_2 ... T_M

Output

Print YES if Rng can complete the problem set without creating new candidates of problems; print NO if he cannot.

Sample Input 1

5
3 1 4 1 5
3
5 4 3

Sample Output 1

YES

Sample Input 2

7
100 200 500 700 1200 1600 2000
6
100 200 500 700 1600 1600

Sample Output 2

NO

Not enough 1600s.

Sample Input 3

1
800
5
100 100 100 100 100

Sample Output 3

NO

Sample Input 4

15
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
9
5 4 3 2 1 2 3 4 5

Sample Output 4

YES


local n=io.read("*n","*l")
local d={}
for i=1,n do
    local x=io.read("*n")
    if not d[x] then
        d[x]=1
    else
        d[x]=d[x]+1
    end
end
local m=io.read("*n","*l")
local t={}
for i=1,m do
    local y=io.read("*n")
    if not t[y] then
        t[y]=1
    else
        t[y]=t[y]+1
    end
end

for i,_ in pairs(t) do
    if t[i]>d[i] then
        print("NO")
        return
    end
end
print("YES")


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

m = io.read('*n')
ans = true
for i = 1, m do
  local u = io.read('*n')
  if not a[u] or a[u] == 0 then
    ans = false
    break
  end
  a[u] = a[u] - 1
end
if ans then print('YES') else print('NO') end


local n=io.read("n")
for i=1,3500 do
    for j=1,3500 do
        local a=n*i*j
        local b=(4*i*j-n*j-n*i)
        if b>0 and a%b==0 then
            print(i.." "..j.." "..a//b)
            return
        end
    end
end

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

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

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


local n=io.read("n")
for i=1,3500 do
    for j=1,3500 do
        local k=n*i*j/(4*i*j-n*j-n*i)
        if math.fmod(k,1)==0 and k>=0 then
            print(i.." "..j.." "..string.format("%d",k))
            return
        end
    end
end


local n = io.read("*n")
for i = 1, 3500 do
  for j = i, 3500 do
    --[[
    4ijk = Njk + Nik + Nij
    k(4ij - Nj - Ni) = Nij
    ]]
    local a = n * i * j
    local b = 4 * i * j - n * (i + j)
    if 0 < b and a % b == 0 then
      local k = a // b
      print(i .." " .. j .. " " .. k)
      os.exit()
    end
  end
end


if io.read():sub(1,4)=='YAKI'then print('Yes')else print('No')end

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

1 \leq |S| \leq 10

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.

Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.

Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

Sample Input 3

YAK

Sample Output 3

No


local h, w, k = io.read("*n", "*n", "*n")
local f = false
for i_h = 0, h do
  for i_w = 0, w do
    if(k == w * i_h + h * i_w - 2 * i_h * i_w) then
      f = true
      break
    end
  end
  if f then break end
end
print(f and "Yes" or "No")

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No