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

for i=1, 1000 do
    if math.floor(i*0.08) == a and math.floor(i*0.1) == b then
            print(i)
        return
    end
end

print(-1)

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


local a, b = io.read("*n", "*n")
local f = false
for t = 1, 10000 do
  if (t * 8) // 100 == a and (t * 10) // 100 == b then
    f = true
    print(t)
    break
  end
end
if not f then print(-1) end

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

Submitted source-code file size, in bytes.

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

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


s=string.gsub(io.read()," ","")
q=io.read("*n","*l")
should_reverse=0
for i=1,q do
    t=io.read("*n")
    if t==1 then
        should_reverse=should_reverse+1
    else
        f=io.read("*n")
        c=string.gsub(io.read()," ","")
        if f==1 then
            if should_reverse%2==1 then
                s=s..c
            else
                s=c..s
            end
        else
            if should_reverse%2==1 then
                s=c..s
            else
                s=s..c
            end
        end
    end
end
print(should_reverse%2==1 and string.reverse(s) or s)

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


s=string.gsub(io.read()," ","")
q=io.read("*n","*l")
query={}
for i=1,q do
    t=io.read("*n")
    if t==1 then
        s=string.reverse(s)
    else
        f=io.read("*n")
        c=string.gsub(io.read()," ","")
        if f==1 then
            s=c..s
        else
            s=s..c
        end
    end
end
print(s)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local S = read.l():totable()
local Q = read.nl()
local rev = false
local front = 0
local back = #S+1
for i=1,Q do
    local q = read.l()
    if q == "1" then
        rev = not rev
    else
        local t, f, c = q:split()
        local ins_front = f == '1'
        if rev then
            ins_front = not ins_front
        end
        if ins_front then
            S[front] = c
            front = front - 1
        else
            S[back] = c
            back = back + 1
        end
    end
end
local ans = {}
for i=front+1,back-1 do
    table.insert(ans, S[i])
end
if rev then
    print(string.reverse(table.concat(ans, "")))
else
    print(table.concat(ans, ""))
end


Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


A={}
R={}
for i=1,9 do
  A[io.read"*n"]=i
end
N=io.read"*n"
function bingo(t)
  for i=1,3 do
    if(t[i]and t[i+3]and t[i+6])then return true end
  end
  for i=1,3 do
    if(t[i*3-2]and t[i*3-1]and t[i*3])then return true end
  end
  return (t[1]and t[5]and t[9]or t[3]and t[5]and t[7])and true or false
end
for i=1,N do
  R[A[io.read"*n"]]=0
end
print(bingo(R)and"Yes"or"No")

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


local n, m, k = io.read("*n", "*n", "*n")

local edge = {}
local avoid = {}
for i = 1, n do
  edge[i] = {}
  avoid[i] = {}
end

for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
end
for i = 1, k do
  local c, d = io.read("*n", "*n")
  avoid[c][d], avoid[d][c] = true, true
end
local parent = {}
for i = 1, n do
  parent[i] = i
end

local function uf_findroot(idx)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

for i = 1, n do
  local src = i
  local rs = uf_findroot(src)
  for dst, _u in pairs(edge[src]) do
    -- table.insert(tasks, dst)
    local rd = uf_findroot(dst)
    parent[rd], parent[dst] = rs, rs
  end
end
local groups = {}
for i = 1, n do
  local r = uf_findroot(i)
  if not groups[r] then
    groups[r] = 1
  else
    groups[r] = groups[r] + 1
  end
end
local ret = {}
for i = 1, n do
  local root = uf_findroot(i)
  local cnt = groups[root] - 1
  for dst, _u in pairs(edge[i]) do
    local r = uf_findroot(dst)
    if r == root then cnt = cnt - 1 end
  end
  for dst, _u in pairs(avoid[i]) do
    local r = uf_findroot(dst)
    if r == root then cnt = cnt - 1 end
  end
  ret[i] = cnt
end
print(table.concat(ret, " "))

Score : 400 points

Problem Statement

An SNS has N users - User 1, User 2, \cdots, User N.

Between these N users, there are some relationships - M friendships and K blockships.

For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.

For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.

We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:

a \neq b.

There is not a friendship between User a and User b.

There is not a blockship between User a and User b.

There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.

For each user i = 1, 2, ... N, how many friend candidates does it have?

Constraints

All values in input are integers.

2 ≤ N ≤ 10^5

0 \leq M \leq 10^5

0 \leq K \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

1 \leq C_i, D_i \leq N

C_i \neq D_i

(A_i, B_i) \neq (A_j, B_j) (i \neq j)

(A_i, B_i) \neq (B_j, A_j)

(C_i, D_i) \neq (C_j, D_j) (i \neq j)

(C_i, D_i) \neq (D_j, C_j)

(A_i, B_i) \neq (C_j, D_j)

(A_i, B_i) \neq (D_j, C_j)

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K

Output

Print the answers in order, with space in between.

Sample Input 1

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

Sample Output 1

0 1 0 1

There is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.

However, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate.

Sample Input 2

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

Sample Output 2

0 0 0 0 0

Everyone is a friend of everyone else and has no friend candidate.

Sample Input 3

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

Sample Output 3

1 3 5 4 3 3 3 3 1 0


local mfl, mce, mmi = math.floor, math.ceil, math.min
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = false
    end
  end
end
SegTree.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage = {1}, {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = false end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  local stagenum = self.stagenum
  while right - left + 1 < bls(1, stagenum - start_stage) do
    start_stage = start_stage + 1
  end
  local ret = false
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = bls(1, stagenum - stage)
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, bls(brs(l - 2 + sz, stagenum - stage), stagenum - stage))
      -- local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = ret or self.stage[stage][brs(l + sz - 1, stagenum - stage)]-- mce(l / sz)]
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
    if ret then break end
  end
  return ret
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][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 = io.read("*n", "*l")
local s = io.read()
local q = io.read("*n", "*l")
local sts = {}
local t = {}
for i = 1, 26 do
  sts[i] = SegTree.new(n, function(a, b) return a or b end, false)
end
for i = 1, n do
  t[i] = s:sub(i, i):byte() - 96
  sts[t[i]]:setValue(i, true)
end

for iq = 1, q do
  local iqstr = io.read()
  local ic = iqstr:sub(1, 1)
  if ic == "1" then
    local i, cs = iqstr:match("%d (%d+) (%w)")
    i = tonumber(i)
    local c = cs:byte() - 96
    if t[i] ~= c then
      sts[t[i]]:setValue(i, false)
      t[i] = c
      sts[c]:setValue(i, true)
    end
  else
    local l, r = iqstr:match("%d (%d+) (%d+)")
    l, r = tonumber(l), tonumber(r)
    local cnt = 0
    for i = 1, 26 do
      if sts[i]:getRange(l, r) then cnt = cnt + 1 end
    end
    print(cnt)
  end
end

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n", "*l")
local s = io.read()
local q = io.read("*n", "*l")
local sts = {}
local t = {}
for i = 1, 26 do
  sts[i] = SegTree.new(n, function(a, b) return a or b end, false)
end
for i = 1, n do
  t[i] = s:sub(i, i):byte() - 96
  sts[t[i]]:setValue(i, true)
end

for iq = 1, q do
  local iqstr = io.read()
  local ic = iqstr:sub(1, 1)
  if ic == "1" then
    local i, cs = iqstr:match("%d (%d+) (%w)")
    i = tonumber(i)
    local c = cs:byte() - 96
    if t[i] ~= c then
      sts[t[i]]:setValue(i, false)
      t[i] = c
      sts[c]:setValue(i, true)
    end
  else
    local l, r = iqstr:match("%d (%d+) (%d+)")
    l, r = tonumber(l), tonumber(r)
    local cnt = 0
    for i = 1, 26 do
      if sts[i]:getRange(l, r) then cnt = cnt + 1 end
    end
    print(cnt)
  end
end


local mma, mmi = math.max, math.min
local msq = math.sqrt
local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = {io.read("*n", "*n", "*n")}
end

local function func(x, y)
  local q = {}
  for i = 1, n do
    q[i] = t[i][3] * t[i][3] * ((t[i][1] - x) * (t[i][1] - x) + (t[i][2] - y) * (t[i][2] - y))
  end
  table.sort(q)
  return msq(q[k])
end

local function solve(tbl)
  local xmin, xmax, ymin, ymax = tbl[1], tbl[2], tbl[3], tbl[4]
  local dv = 96
  local dxrange = (xmax - xmin) / (dv * 2)
  local dyrange = (ymax - ymin) / (dv * 2)
  local mpx, mpy, minval = false, false, 0
  for i = 1, dv do
    local xp = xmin + dxrange * (i * 2 - 1)
    yp = ymin - dyrange
    for j = 1, dv do
      yp = yp + dyrange * 2
      local v = func(xp, yp)
      if not mpx or v < minval then
        minval = v
        mpx, mpy = i, j
      end
    end
  end
  tbl[1] = xmin + mma(0, dxrange * (mpx * 2 - 1 - dv / 4))
  tbl[2] = mmi(xmax, xmin + dxrange * (mpx * 2 - 1 + dv / 4))
  tbl[3] = ymin + mma(0, dyrange * (mpy * 2 - 1 - dv / 4))
  tbl[4] = mmi(ymax, ymin + dyrange * (mpy * 2 - 1 + dv / 4))
end

local tbl = {-1000, 1000, -1000, 1000}
local rep = 10000000
while true do
  solve(tbl)
  if 1.84 < os.clock() then break end
end
local xm, ym = (tbl[1] + tbl[2]) / 2, (tbl[3] + tbl[4]) / 2
local ret = func(xm, ym)
print(string.format("%.10f", ret))

Score : 600 points

Problem Statement

Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \left(x_i, y_i\right), and its hardness is c_i.

Takahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \left(X, Y\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \times \sqrt{\left(X - x_i\right)^2 + \left(Y-y_i\right)^2} seconds.

Takahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.

Constraints

All values in input are integers.

1 \leq N \leq 60

1 \leq K \leq N

-1000 \leq x_i , y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) \left(i \neq j \right)

1 \leq c_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
\vdots
x_N y_N c_N

Output

Print the answer.

It will be considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

4 3
-1 0 3
0 0 3
1 0 2
1 1 40

Sample Output 1

2.4

If we put the heat source at \left(-0.2, 0\right), the 1-st, 2-nd, and 3-rd pieces of meat will be ready to eat within 2.4 seconds. This is the optimal place to put the heat source.

Sample Input 2

10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16

Sample Output 2

7411.2252


local mma, mmi = math.max, math.min
local msq = math.sqrt
local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = {io.read("*n", "*n", "*n")}
end

local function func(x, y)
  local q = {}
  for i = 1, n do
    q[i] = t[i][3] * t[i][3] * ((t[i][1] - x) * (t[i][1] - x) + (t[i][2] - y) * (t[i][2] - y))
  end
  table.sort(q)
  return msq(q[k])
end

local function solve(tbl)
  local xmin, xmax, ymin, ymax = tbl[1], tbl[2], tbl[3], tbl[4]
  local dv = 96
  local dxrange = (xmax - xmin) / (dv * 2)
  local dyrange = (ymax - ymin) / (dv * 2)
  local xp = {}
  for i = 1, dv do
    xp[i] = xmin + dxrange * (i * 2 - 1)
  end
  local yp = {}
  for i = 1, dv do
    yp[i] = ymin + dyrange * (i * 2 - 1)
  end
  local mp, minval = false, 0
  for i = 1, dv do
    for j = 1, dv do
      local v = func(xp[i], yp[j])
      if not mp or v < minval then
        minval = v
        mp = {i, j}
      end
    end
  end
  tbl[1] = xmin + mma(0, dxrange * (mp[1] * 2 - 1 - dv / 4))
  tbl[2] = mmi(xmax, xmin + dxrange * (mp[1] * 2 - 1 + dv / 4))
  tbl[3] = ymin + mma(0, dyrange * (mp[2] * 2 - 1 - dv / 4))
  tbl[4] = mmi(ymax, ymin + dyrange * (mp[2] * 2 - 1 + dv / 4))
  -- tbl[1], tbl[2], tbl[3], tbl[4]
  -- = xmin + dxrange * (mp[1] * 2 - 2), xmin + dxrange * (mp[1] * 2),
  --   ymin + dyrange * (mp[2] * 2 - 2), ymin + dyrange * (mp[2] * 2)
end

local tbl = {-1000, 1000, -1000, 1000}
local rep = 10000000
while true do
  solve(tbl)
  if 1.8 < os.clock() then break end
end
local xm, ym = (tbl[1] + tbl[2]) / 2, (tbl[3] + tbl[4]) / 2
local ret = func(xm, ym)
print(string.format("%.10f", ret))


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, R= read.nn()
if N >= 10 then
    print(R)
else
    print(R + 100*(10-N))
end

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


function pointsOfStamina(p, x)
   local points = 0
   for i = 1, #x do
      points = points + (x[i] - p)^2
   end
   return points
end

local n = io.read("*n")
local x = {}

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

local min = {coordinate=1, points=pointsOfStamina(1, x)}
for i = 2, 100 do
   local points = pointsOfStamina(i, x)
   if points < min.points then
      min.coordinate = i
      min.points = points
   end
end

print(string.format("%d", min.points))

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


local mod=1000000007

local function bmul(x,y)
    return (x*y)%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 function bmod(x)
    x=tostring(x)
    local ten=10
    local cry=1
    local xod=0
    for i=#x,1,-1 do
        local xub=tonumber(x:sub(i,i))
        xod=(xod+xub*cry)%mod
        cry=(cry*ten)%mod
    end
    return xod
end

local function bpow(x,y)
    x=bmod(x)
    y=bmod(y)
    local xpy=1
    while 0<y do
        if y%2==1 then
            xpy=bmul(xpy,x)
            y=y-1
        end
        x=bmul(x,x)
        y=y//2
    end
    return xpy
end

local function comb(x,y)
    local xyi=1
    local yii=1
    for i=1,y do
        xyi=bmul(xyi,x+1-i)
        yii=bmul(yii,i)
    end
    return bmul(xyi,bpow(yii,mod-2))
end

local n,a,b=io.read("n","n","n")
local total=bpow(2,n)-1
local comba=comb(n,a)
local combb=comb(n,b)
print(bsub(total,badd(comba,combb)))

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

Print the count modulo (10^9 + 7).


local mmi, mma = math.min, math.max
local mod = 1000000007
local mfl = math.floor

local function badd(x, y)
  return (x + y) % mod
end
local function bmul(x, y)
  return (x * y) % mod
end

local invs = {1}
local fact = {1}
local invfact = {1}
for i = 2, 500000 do
  fact[i] = bmul(fact[i - 1], i)
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invfact[i] = bmul(invfact[i - 1], invs[i])
end

local function getComb(n, k)
  if k == 0 or n == k then return 1 end
  return bmul(fact[n], bmul(invfact[k], invfact[n - k]))
end

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

k = mmi(n - 1, k)
local ret = 0
for i = 0, k do
  local v1 = getComb(n, i)
  local use = n - i
  local v2 = getComb(i + use - 1, use - 1)
  ret = badd(ret, bmul(v1, v2))
end
print(ret)

Score : 500 points

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room i goes to another room j~ (i \neq j).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

Find the count modulo (10^9 + 7).

Constraints

All values in input are integers.

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

2 \leq k \leq 10^9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).

Sample Input 1

3 2

Sample Output 1

10

Let c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):

(0, 0, 3)

(0, 1, 2)

(0, 2, 1)

(0, 3, 0)

(1, 0, 2)

(1, 1, 1)

(1, 2, 0)

(2, 0, 1)

(2, 1, 0)

(3, 0, 0)

For example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3.

Sample Input 2

200000 1000000000

Sample Output 2

607923868

Print the count modulo (10^9 + 7).

Sample Input 3

15 6

Sample Output 3

22583772


local mod = 1000000007
local mfl = math.floor

-- 10^9 + 7 only
-- (31623^2) % 1000000007 = 14122
local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

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

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

local function getComb(n, k)
  local ret = 1
  for i = 1, k do
    ret = bmul(ret, n + 1 - i)
    ret = bmul(ret, modinv(i))
  end
  return ret
end

local n, k = io.read("*n", "*n")
if n - 1 <= k then
  print(getComb(n + n - 1, n))
else
  local ret = 1
  local comb1, comb2 = 1, 1
  for i = 1, k do
    local inv = modinv(i)
    comb1 = bmul(comb1, n + 1 - i)
    comb1 = bmul(comb1, inv)
    comb2 = bmul(comb2, n - i)
    comb2 = bmul(comb2, inv)
    local v = comb1
    v = bmul(v, comb2)
    -- local v = getComb(n, i)
    -- v = bmul(v, getComb(n - 1, i))
    ret = badd(ret, v)
  end
  print(ret)
end


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

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

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

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


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


local N = io.read("n")
local A = {}
for i=1,N do
  A[i] = io.read("n")
end
for i=1,N do
  if A[i] % 2 == 0 then
    if A[i] % 3 == 0 or A[i] % 5 == 0 then

    else
      print("DENIED")
      return
    end
  end
end
print("APPROVED")

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local f = true
for i = 1, n do
  if t[i] % 2 == 0 then
    if t[i] % 3 ~= 0 and t[i] % 5 ~= 0 then
      f = false
      break
    end
  end
end
print(f and "APPROVED" or "DENIED")


local n = io.read("*n", "*l")
local t = {}
for i = 1, n do
  local s = io.read()
  if not t[s] then t[s] = 1 else t[s] = t[s] + 1 end
end
local max = 0
for k, v in pairs(t) do
  if max < v then
    max = v
  end
end
local r = {}
for k, v in pairs(t) do
  if v == max then
    table.insert(r, k)
  end
end
table.sort(r)
for i = 1, #r do
  print(r[i])
end

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
-- A
local S, T = read.l():split()
local A, B = read.nnl()
local U = read.l()
if U == S then
    A = A - 1
else
    B = B - 1
end
print(A .. " " .. B)

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


local n = io.read("*n")
local f = true
local t = {}
for i = 1, n do
  local a = io.read("*n")
  if t[a] then f = false end
  t[a] = true
end
print(f and "YES" or "NO")

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
-- D
local N, K = read.nn()
local P = read.N(N)
local E = {}
local S = {}
S[0] = 0
for i=1,N do
    E[i] = (P[i] + 1) / 2
    S[i] = S[i-1] + E[i]
end
local ans = 0
for i=K,N do
    ans = math.max(ans, S[i] - S[i-K])
end
print(ans)

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.T = function(T) local t={} for i=1,T do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local function create_ndim_array(n, last_metatbl)
    local metatables = {}
    metatables[1] = last_metatbl
    for i=2,n do
        metatables[i] = {__index = function(parent, k)
            local child = setmetatable({}, metatables[i-1])
            rawset(parent, k, child)
            return child
        end}
    end
    return setmetatable({}, metatables[n])
end
-- E
local S = read.l():totable()
local T = {}
for i=1,#S do
    T[i] = tonumber(S[i])
end
local D = #T
local K = read.n()

local tbl = create_ndim_array(3, {__index = function() return 0 end})
tbl[0][true][0] = 1
for i=0,D-1 do
    for _, m in ipairs {false, true} do
        for k=0,K do
            if tbl[i][m][k] == 0 then
                goto continue_k
            end
            local ni = i+1
            local nd = T[ni]
            for d=0,9 do
                local nk = k
                local nm
                if m == false then
                    nm = false
                else
                    if d > nd then
                        break
                    elseif d == nd then
                        nm = true
                    else
                        nm = false
                    end
                end
                if d ~= 0 then
                    nk = nk + 1
                end
                if nk > K then
                    goto continue_d
                end
                tbl[ni][nm][nk] = tbl[ni][nm][nk] + tbl[i][m][k]
                ::continue_d::
            end
            ::continue_k::
        end
    end
end
local ans = tbl[D][false][K] + tbl[D][true][K]
print(ans)

Score : 500 points

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

11,12,13,14,15,16,17,18,19,21,22,23,24,25

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300


local mod = 1000000007
local mfl = math.floor

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

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

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

local invs = {}
for i = 1, 1000 * 1000 + 1 do
  invs[i] = modinv(i)
end

local r1, c1 = io.read("*n", "*n")
local r2, c2 = io.read("*n", "*n")
local function getSum(r, c)
  local sum = 0
  local cur = 1
  for ir = 0, r do
    cur = bmul(cur, ir + c + 1)
    cur = bmul(cur, invs[ir + 1])
    sum = badd(sum, cur)
  end
  return sum
end
local z = getSum(r2, c2)
z = bsub(z, getSum(r2, c1 - 1))
z = bsub(z, getSum(r1 - 1, c2))
z = badd(z, getSum(r1 - 1, c1 - 1))
print(z)

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


n = io.read("*n")
a = io.read("*n")
print(math.ceil(n / a))


h,n = io.read("*n","*n","*l")
al = io.read()
for a in string.gmatch( al,"%d+" ) do
    h = h - a
  io.stderr:write(tostring(h).."\n")
end

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

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


h,n = io.read("*n","*n")

for i = 1, n do
    h = h - io.read("*n")
end

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


a, b = io.read("*n", "*n")
for i = 1, b do
  a = a - io.read("*n")
end
print(a <= 0 and "Yes" or "No")


n,k = io.read("*n","*n", "*l")
t = {}
for i = 1, n do
    t[i] = io.read("*n")
end
table.sort( t )
a = 0
i = 1
while i <= n-k do
    a = a + t[i]
    i = i + 1
end
print(a)

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


local mce, mma, mmi = math.ceil, math.max, math.min
local h, n = io.read("*n", "*n")
local z = {}
for i = 1, 10000 do
  z[i] = 0
end
for i = 1, n do
  local a, b = io.read("*n", "*n")
  z[a] = mmi(z[a], b)
end
local t = {}
local inf = 1000000007
for i = 1, h do
  t[i] = inf
end
for a = 1, 10000 do
  local b =z[a]
  if 0 < b then
    for src = h - 1, 1, -1 do
      local lim = mce((h - src) / a)
      for j = 1, lim do
        local dst = mmi(h, src + a * j)
        t[dst] = mmi(t[dst], t[src] + b * j)
      end
    end
    do -- src = 0
      local lim = mce(h / a)
      for j = 1, lim do
        local dst = mmi(h, a * j)
        t[dst] = mmi(t[dst], b * j)
      end
    end
  end
end
print(t[h])

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


local function meld(a,b)
    if not a then
        return b
    elseif not b then
        return a
    elseif a[1]<b[1] then
        b[4]=a[3]
        a[3]=b
        return a
    else
        a[4]=b[3]
        b[3]=a
        return b
    end
end

local function pair(a)
    local b,c,d,e
    while a do
        c=false
        b=a
        a=a[4]
        b[4]=false
        if a then
            c=a
            a=a[4]
            c[4]=false
        end
        b=meld(b,c)
        b[4]=d
        d=b
    end
    while d do
        e=d
        d=d[4]
        a=meld(a,e)
    end
    return a
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,key,value)
    self[1]=meld(self[1],{key,value,false,false})
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=pair(self[1][3])
end

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

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

----------

local n,d,a=io.read("n","n","n")
local pq=PairingHeap:new()
for i=1,n do
    local x,h=io.read("n","n")
    pq:push(x,{x,h,0})
end

local atack=0
local total=0
while not pq:empty() do
    local pop=pq:top()
    local x,h,c=pop[1],pop[2],pop[3]
    pq:pop()
    if c==0 then
        if atack*a<h then
            h=h-atack*a
            local count=math.floor((h-1)/a)+1
            pq:push(x+2*d+0.5,{x+2*d,count,1})
            total=total+count
            atack=atack+count
        end
    else
        atack=atack-h
    end
end
print(string.format("%d",total))

Score : 600 points

Problem Statement

Silver Fox is fighting with N monsters.

The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.

Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.

Silver Fox wins when all the monsters' healths become 0 or below.

Find the minimum number of bombs needed to win.

Constraints

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

0 \leq D \leq 10^9

1 \leq A \leq 10^9

0 \leq X_i \leq 10^9

1 \leq H_i \leq 10^9

X_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D A
X_1 H_1
:
X_N H_N

Output

Print the minimum number of bombs needed to win.

Sample Input 1

3 3 2
1 2
5 4
9 2

Sample Output 1

2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.

Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.

Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.

Sample Input 2

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

Sample Output 2

5

We should use five bombs at the coordinate 5.

Sample Input 3

3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


local function meld(a,b)
    if not a then
        return b
    elseif not b then
        return a
    elseif a[1]<b[1] then
        b[4]=a[3]
        a[3]=b
        return a
    else
        a[4]=b[3]
        b[3]=a
        return b
    end
end

local function pair(a)
    local b=a and a[4]
    if not b then
        return a
    end
    local q={}
    while b do
        local c=b[4]
        a[4]=false
        b[4]=false
        table.insert(q,meld(a,b))
        a=c
        b=a and a[4]
    end
    for i=#q,1,-1 do
        a=meld(a,q[i])
    end
    return a
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,k,v)
	if not self[1] then
		self[1]={
            k,
            v,
            false,
            false,
		}
	else
		if self[1][1]<k then
			self[1][3]={
				k,
				v,
				false,
				self[1][3],
			}
		else
			self[1]={
				k,
				v,
				self[1],
				false,
			}
		end
	end
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=pair(self[1][3])
end

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

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

----------

local n,d,a=io.read("n","n","n")
local pq=PairingHeap:new()
for i=1,n do
    local x,h=io.read("n","n")
    pq:push(x,{x,h,0})
end

local atack=0
local total=0
while not pq:empty() do
    local x,h,c=table.unpack(pq:top())
    pq:pop()
    if c==0 then
        if atack*a<h then
            h=h-atack*a
            local count=math.floor(h/a)+1
            pq:push(x+2*d+0.5,{x+2*d,count,1})
            total=total+count
            atack=atack+count
        end
    else
        atack=atack-h
    end
end
print(total)


local mfl = math.floor
local mce = math.ceil
local function comp(a, b)
  return a[1] < b
end

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

local n, d, a = io.read("*n", "*n", "*n")
d = d * 2
local t = {}
local imos = {}
for i = 1, n do
  t[i] = {io.read("*n", "*n")}
  imos[i] = 0
end
imos[n + 1] = 0
table.sort(t, function(a, b) return a[1] < b[1] end)
local ret = 0
for i = 1, n do
  local x, h = t[i][1], t[i][2]
  if 1 < i then
    imos[i] = imos[i] + imos[i - 1]
  end
  h = h + imos[i]
  if 0 < h then
    local use = mce(h / a)
    ret = ret + use
    local dst = lower_bound(t, x + d + 1)
    imos[i + 1] = imos[i + 1] - use * a
    imos[dst] = imos[dst] + use * a
  end
end
print(ret)


n, m = io.read("*n", "*n")
print(n == m and "Yes" or "No")

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


local a, b = io.read("*n", "*n")
if b < a then
  a, b = b, a
end
print(tostring(a):rep(b))

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


local a, b = io.read("*n", "*n")
local s1, s2 = "", ""
for i = 1, a do
  s1 = s1 .. tostring(b)
end
for i = 1, b do
  s2 = s2 .. tostring(a)
end
print(s1 < s2 and s1 or s2)


local function read_a(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end

N=io.read"*n"
A=read_a(N)

s=0
m=math.huge
for i=1,N do
	if(m>A[i])then s=s+1 m=A[i]end
end
print(s)

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
----

local N = read.n()

local function get(b, e)
    -- number of integer x where:
    --  - begin with b,
    --  - end with e, and
    --  - x <= N
    local c = 0
    for i=e,N,10 do
        local x = tonumber(tostring(i):sub(1,1))
        if x == b then
            c = c + 1
        end
    end
    return c
end

local cnt = 0
-- A
-- x....y
-- B
-- y....x
for x=1,9 do
    for y=1,9 do
        local cnt_a = get(x, y)
        local cnt_b = get(y, x)
        cnt = cnt + cnt_a * cnt_b
    end
end
print(cnt)

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


local mfl, mce, mmi = math.floor, math.ceil, math.min
local bor = bit.bor

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end

local m = io.read("*n")
local mtot = 2^m

local posu, posv, posp = {}, {}, {}
for i = 1, m do
  local u, v = io.read("*n", "*n")
  local pu, pv = posinv[u], posinv[v]
  if pv < pu then pu, pv = pv, pu end
  local p = lca[pu][pv - pu + 1]
  posu[i], posv[i], posp[i] = pu, pv, posinv[p]
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end
local lineval = {}
for i = 1, 2 * n - 2 do
  lineval[i] = 0
end
local linesum = {}
for i = 1, 2 * n - 2 do
  linesum[i] = {}
end
local function recalc_linesum()
  for i = 1, 2 * n - 2 do
    linesum[i][1] = lineval[i]
    for j = i + 1, 2 * n - 2 do
      linesum[i][j - i + 1] = linesum[i][j - i] + lineval[j]
    end
  end
end

local mbox1, mbox2 = {}, {}
for i = 1, mtot do
  mbox1[i], mbox2[i] = 0, 0
end
mbox1[1] = 1
local function addmbox(src, dst)
  mbox1[dst] = mbox1[dst] + mbox1[src]
  if 1000000000 <= mbox1[dst] then
    mbox1[dst] = mbox1[dst] - 1000000000
    mbox2[dst] = mbox2[dst] + 1
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  lineval[p1] = 1
  lineval[p2] = -1
  recalc_linesum()
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local pu, pv, pp = posu[im], posv[im], posp[im]
    if pv < pp then
      activate = 0 ~= linesum[pv][pp - pv]
    elseif pp < pv then
      activate = 0 ~= linesum[pp][pv - pp]
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= linesum[pu][pp - pu]
      elseif pp < pu then
        activate = 0 ~= linesum[pp][pu - pp]
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = bor(im - 1, actsum)
    addmbox(im, dst + 1)
  end
  lineval[p1] = 0
  lineval[p2] = 0
end
if mbox2[mtot] == 0 then
  print(mbox1[mtot])
else
  print(mbox2[mtot] .. mbox1[mtot])
end

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i.

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

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

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

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

Sample Output 4

62

The tree in this input is shown below:


local mfl, mce, mmi = math.floor, math.ceil, math.min
local bor = bit.bor

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end
local stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0LL
end
mbox[1] = 1LL
local posu, posv, posp = {}, {}, {}
for i = 1, m do
  local u, v = io.read("*n", "*n")
  local pu, pv = posinv[u], posinv[v]
  if pv < pu then pu, pv = pv, pu end
  local p = lca[pu][pv - pu + 1]
  posu[i], posv[i], posp[i] = pu, pv, posinv[p]
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local pu, pv, pp = posu[im], posv[im], posp[im]
    if pv < pp then
      activate = 0 ~= stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 0 ~= stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 0 ~= stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = bor(im - 1, actsum)
    mbox[dst + 1] = mbox[dst + 1] + mbox[im]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
end
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)


local mfl, mce, mmi = math.floor, math.ceil, math.min

local function getor(x, y)
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    ret = ret + mul * (1 - (1 - x % 2) * (1 - y % 2))
    x, y, mul = mfl(x / 2), mfl(y / 2), mul * 2
  end
  return ret
end

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end
local stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0LL
end
mbox[1] = 1LL
local posu, posv, posp = {}, {}, {}
for i = 1, m do
  local u, v = io.read("*n", "*n")
  local pu, pv = posinv[u], posinv[v]
  if pv < pu then pu, pv = pv, pu end
  local p = lca[pu][pv - pu + 1]
  posu[i], posv[i], posp[i] = pu, pv, posinv[p]
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local pu, pv, pp = posu[im], posv[im], posp[im]
    if pv < pp then
      activate = 0 ~= stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 0 ~= stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 0 ~= stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = getor(im - 1, actsum)
    mbox[dst + 1] = mbox[dst + 1] + mbox[im]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
end
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)


local mfl, mce, mmi = math.floor, math.ceil, math.min

local function getor(x, y)
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    ret = ret + mul * (1 - (1 - x % 2) * (1 - y % 2))
    x, y, mul = mfl(x / 2), mfl(y / 2), mul * 2
  end
  return ret
end

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end
local stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0LL
end
mbox[1] = 1LL
local mu, mv = {}, {}
for i = 1, m do
  mu[i], mv[i] = io.read("*n", "*n")
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local u, v = mu[im], mv[im]
    local pu, pv = posinv[u], posinv[v]
    if pv < pu then pu, pv = pv, pu end
    local parent = lca[pu][pv - pu + 1]
    local activate = false
    local pp = posinv[parent]
    if pv < pp then
      activate = 0 ~= stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 0 ~= stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 0 ~= stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = getor(im - 1, actsum)
    mbox[dst + 1] = mbox[dst + 1] + mbox[im]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
end
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)


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

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid.
In one operation, you can do one of the following two actions:

Choose one row, then paint all the squares in that row black.

Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid?
It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

1 \leq H \leq 100

1 \leq W \leq 100

1 \leq N \leq H \times W

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.

Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.

Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local H, W, N = read.nnn()
local big = math.max(H, W)
print(math.ceil(N / big))


local function axiscomp(ary)
  local z = {}
  for i = 1, #ary do
    z[i] = ary[i]
  end
  table.sort(z)
  local ac, acinv = {}, {}
  local cur = 0
  for i = 1, #z do
    if not acinv[z[i]] then
      cur = cur + 1
      ac[cur] = z[i]
      acinv[z[i]] = cur
    end
  end
  return ac, acinv
end

local n = io.read("*n")
local ax = {}
local left, right = {}, {}
local idx = {}
for i = 1, n do
  local a, b = io.read("*n", "*n")
  left[i], right[i] = a - b, a + b
  table.insert(ax, a - b)
  table.insert(ax, a + b)
  idx[i] = i
end
local ac, acinv = axiscomp(ax)

local edge = {}
for i = 1, #ac do
  edge[i] = {}
  if i ~= #ac then
    edge[i][i + 1] = 0
  end
end
for i = 1, n do
  local l, r = acinv[left[i]], acinv[right[i]]
  edge[l][r] = 1
end

local len = {}
for i = 1, #ac do len[i] = -1 end
len[1] = 0
for src = 1, #ac - 1 do
  for dst, cost in pairs(edge[src]) do
    if len[dst] < len[src] + cost then
      len[dst] = len[src] + cost
    end
  end
end

print(len[#ac])

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

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

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


local n, m = io.read("*n", "*n")
local d = {}
local d_short_idx = {}
local edge = {}
local color = {}
local inf = 1000000007 - 7
for i = 1, n do
  d[i] = io.read("*n")
  edge[i] = {}
  color[i] = 0
  d_short_idx[i] = i
end
table.sort(d_short_idx, function(a, b) return d[a] < d[b] end)
local edgeinfo = {}
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edgeinfo[i] = {a, b, 0}
  table.insert(edge[a], i)
  table.insert(edge[b], i)
end

for id = 1, n do
  local src = d_short_idx[id]
  local asked_edge_color = false
  local samelevel_edge_found = false
  for i = 1, #edge[src] do
    local edge_idx = edge[src][i]
    local e1, e2 = edgeinfo[edge_idx][1], edgeinfo[edge_idx][2]
    local dst = src == e1 and e2 or e1
    if 0 < color[dst] then
      if not asked_edge_color or asked_edge_color < 100 then
        asked_edge_color = color[dst]
      end
    end
    if d[dst] == d[src] then
      samelevel_edge_found = true
    end
  end
  if asked_edge_color then
    if asked_edge_color == 100 then
      color[src] = 2
    else
      color[src] = 3 - asked_edge_color
    end
    for i = 1, #edge[src] do
      local edge_idx = edge[src][i]
      local e1, e2 = edgeinfo[edge_idx][1], edgeinfo[edge_idx][2]
      local dst = src == e1 and e2 or e1
      if color[dst] == color[src] then
        edgeinfo[edge_idx][3] = inf
      else
        edgeinfo[edge_idx][3] = d[src]
      end
    end
  elseif samelevel_edge_found then
    color[src] = 100
  else
    print(-1) os.exit()
  end
end
for i = 1, n do
  if color[i] == 100 then color[i] = 1 end
  assert(0 < color[i] and color[i] <= 2)
  io.write(color[i] == 1 and "W" or "B")
end
io.write("\n")
for i = 1, m do
  print(edgeinfo[i][3])
end

Score : 900 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally.
We are additionally given N integers D_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 1 and 10^9 (inclusive) to each edge in this graph.
If the answer is yes, find one such assignment of colors and integers, too.

There is at least one vertex assigned white and at least one vertex assigned black.

For each vertex v (1 \leq v \leq N), the following holds.

The minimum cost to travel from Vertex v to a vertex whose color assigned is different from that of Vertex v by traversing the edges is equal to D_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 200,000

1 \leq D_i \leq 10^9

1 \leq U_i, V_i \leq N

The given graph is connected and has no self-loops or multiple edges.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

S
C_1
C_2
\vdots
C_M

Here,

the first line should contain the string S of length N. Its i-th character (1 \leq i \leq N) should be W if Vertex i is assigned white and B if it is assigned black.

The (i + 1)-th line (1 \leq i \leq M) should contain the integer weight C_i assigned to Edge i.

Sample Input 1

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

Sample Output 1

BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 5, for example. To travel from Vertex 5, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 5 \to Vertex 4 \to Vertex 2. The total cost of these moves is 7, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

BBBW
1
1
1
2
1
1


p=io.read():match("%w+")
if p~='z' then
  print(string.char(string.byte(p)+1))
else
  print('a')
end

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local c = read.l()
print(string.char(c:byte(1,1)+1))


n, m = io.read("*n", "*n", "*l")
ac, wa = 0, 0
accepted = {}
wacand = {}
for i = 1, m do
  s = io.read()
  a, b = s:match("(%d+) (%w%w)")
  a = tonumber(a)
  if not accepted[a] then
    if b == "AC" then
      if wacand[a] then
        wa = wa + wacand[a]
      end
      ac = ac + 1
      accepted[a] = true
    else
      if not wacand[a] then
        wacand[a] = 1
      else
        wacand[a] = wacand[a] + 1
      end
    end
  end
end
print(ac .. " " .. wa)

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0