local par={}
local rank={}

local function init(n)
    for i=1,n do
        par[i]=i
        rank[i]=1
    end
end

local function root(x)
    return par[x]==x and x or root(par[x])
end

local function same(x,y)
    return root(x)==root(y)
end

local function unite(x,y)
    x=root(x)
    y=root(y)
    if x==y then
        return
    end
    if rank[x]>rank[y] then
        x,y=y,x
    end
    par[x]=y
    rank[y]=rank[y]+rank[x]
end

local function size(x)
    return rank[root(x)]
end

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

init(n)

local graph={}
for i=1,n do
    graph[i]={}
end
for i=1,m do
    local a,b=io.read("n","n")
    table.insert(graph[a],b)
    table.insert(graph[b],a)
    unite(a,b)
end
for i=1,k do
    local c,d=io.read("n","n")
    if same(c,d) then
        table.insert(graph[c],d)
        table.insert(graph[d],c)
    end
end

for i=1,n do
    local kouho=size(i)-#graph[i]-1
    io.write(kouho.." ")
end
print()

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 = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local bor = bit.bor
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt 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.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  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 stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  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.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    self:update(idx)
  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 st = SegTree.new(n, bor, 0)
for i = 1, n do
  local v = s:byte(i) - 96
  st:setValue(i, bls(1, v - 1))
end
local q = io.read("*n", "*l")
for iq = 1, q do
  local qstr = io.read()
  if qstr:byte(1) == 49 then
    local _u, pos, val = qstr:match("(%d) (%d+) (%w)")
    pos = tonumber(pos)
    val = val:byte(1) - 96
    st:setValue(pos, bls(1, val - 1))
  else
    local _u, l, r = qstr:match("(%d) (%d+) (%d+)")
    l = tonumber(l)
    r = tonumber(r)
    local v = st:getRange(l, r)
    local c = 0
    for i = 1, 26 do
      if v % 2 == 1 then c = c + 1 end
      v = brs(v, 1)
    end
    print(c)
  end
end

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

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.

Submitted source-code file size, in bytes.

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

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


local 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 stagenum = self.stagenum
  local ret = false
  while true do
    local stage = 1
    local sz = bls(1, stagenum - stage)
    local len = right + 1 - left
    while (left - 1) % sz ~= 0 or len < sz do
      stage = stage + 1
      sz = brs(sz, 1)
    end
    ret = ret or self.stage[stage][1 + brs(left - 1, stagenum - stage)]
    left = left + sz
    if right < left 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


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 / 2))
  tbl[2] = mmi(xmax, xmin + dxrange * (mpx * 2 - 1 + dv / 2))
  tbl[3] = ymin + mma(0, dyrange * (mpy * 2 - 1 - dv / 2))
  tbl[4] = mmi(ymax, ymin + dyrange * (mpy * 2 - 1 + dv / 2))
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 = 128
  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))
end

local tbl = {-1000, 1000, -1000, 1000}
local rep = 10000000
while true do
  solve(tbl)
  if 1.85 < 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 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 = 256
  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))
end

local tbl = {-1000, 1000, -1000, 1000}
local rep = 10000000
while true do
  solve(tbl)
  if 1.85 < 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))


N=io.read"*n"
R=io.read"*n"
if(N<10)then print(R+100*(10-N))
else print(R)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


N=io.read"*n"
X={}
for i=1,N do
	X[i]=io.read"*n"
end
b,c=0,0
for i=1,N do
	b=b-X[i]*2
	c=c+X[i]*X[i]
end
r=-math.floor(b/N*.5+.5)
print(N*r*r+b*r+c)

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 A, B, C = io.read("n", "n", "n")
if A == B and B == C then
  print("No")
elseif A ~= B and B ~= C and C ~= A then
  print("No")
else
  print("Yes")
end

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 N = io.read("n", "l")
local counts = setmetatable({}, {__index=function()return 0 end})
local max = 0
for i=1,N do
  local s = io.read("l")
  counts[s] = counts[s] + 1
  if counts[s] > max then
    max = counts[s]
  end
end
local anstbl = {}
for s, c in pairs(counts) do
  if c == max then
    table.insert(anstbl, s)
  end
end
table.sort(anstbl)
for i=1,#anstbl do
  print(anstbl[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


N=io.read"*n"
K=io.read"*n"
s={0}
for i=1,N do
	table.insert(s,s[i]+io.read"*n"*.5+.5)
end
max=0
for i=1,N-K+1 do
	max=math.max(max,s[i+K]-s[i])
end
print(max)

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 mfl = math.floor
local ns = io.read()
local k = io.read("*n")
if k == 1 then
  local a = tonumber(ns:sub(1, 1))
  print(a + 9 * (#ns - 1))
elseif k == 2 then
  if #ns == 1 or ns == "10" then print(0)
  else
    local a = tonumber(ns:sub(1, 1))
    local b = tonumber(ns:sub(2, 2))
    -- use 1st but not max
    local ret = (#ns - 1) * 9 * (a - 1)
    -- don't use 1st
    ret = ret + 81 * mfl((#ns - 1) * (#ns - 2) / 2)
    -- use 1st by max
    ret = ret + b + 9 * (#ns - 2)
    print(ret)
  end
else
  if #ns < 3 or ns == "100" then print(0)
  else
    local a = tonumber(ns:sub(1, 1))
    local b = tonumber(ns:sub(2, 2))
    local c = tonumber(ns:sub(3, 3))
    -- don't use 1st
    ret = 729 * mfl((#ns - 1) * (#ns - 2) * (#ns - 3) / 6)
    -- use 1st but not max
    ret = ret + (a - 1) * 81 * mfl((#ns - 1) * (#ns - 2) / 2)
    -- use 1st by max
    do
      -- don't use second
      ret = ret + 81 * mfl((#ns - 2) * (#ns - 3) / 2)
      -- use 2nd but not max
      ret = ret + (b - 1) * 9 * (#ns - 2)
      -- use 2nd by max
      ret = ret + c + 9 * (#ns - 3)
    end
    print(ret)
  end
end

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


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

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


h,n = io.read("*n","*n")
al = io.read()
for a in string.gmatch( al,"%d+" ) do
    h = h - a
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","*l")
al = io.read()
for a in string.gmatch( al,"%d+" ) do
    h = h - tonumber(a)
  io.stderr:write(tostring(h).."\n")
end

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


N=io.read"*n"
K=io.read"*n"
H={}for i=1,N do
H[i]=io.read"*n"end
table.sort(H)
s=0
for i=1,N-K do s=s+H[i]end
print(s)

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.


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
io.write(string.format("%d\n", a))


H=io.read"*n"
s=0
while(H~=0)do
	H=math.floor(H*.5)
	s=s*2+1
end
print(s)

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


local a = io.read("*n")
local s = 0
local mul = 1
while 0 < a do
  a = a // 2
  s = s + mul
  mul = mul * 2
end
print(s)


local mce, mma, mmi = math.ceil, math.max, math.min
local h, n = io.read("*n", "*n")
local inf = 1000000007
local z = {}
for i = 1, 10000 do
  z[i] = inf
end
for i = 1, n do
  local a, b = io.read("*n", "*n")
  z[a] = mmi(z[a], b)
end
local t = {}
for i = 1, h do
  t[i] = inf
end
for a = 1, 10000 do
  local b = z[a]
  if b < inf 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=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 top=pq:top()
    local x,h,c=top[1],top[2],top[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.fomat("%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 top=pq:top()
    local x,h,c=top[1],top[2],top[3]
    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)


N=io.read"*n"
M=io.read"*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


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


local n=io.read("n")
local map={}
for i=1,n do
    local itos=tostring(i)
    local first=itos:byte(1)
    local last=itos:byte(#itos)
    map[first]=map[first] or {}
    map[first][last]=(map[first][last] or 0)+1
end

local counter=0
for first,_ in pairs(map) do
    for last,_ in pairs(map[first]) do
        if map[last] and map[last][first] then
            counter=counter+map[first][last]*map[last][first]
        end
    end
end
print(counter)

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)
  mbox2[dst] = mbox2[dst] + mbox2[src]
  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
  local z = 1000000000 + mbox1[mtot]
  local zstr = tostring(z)
  zstr = zstr:sub(2, #zstr)
  print(mbox2[mtot] .. zstr)
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 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 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
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

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)
    mbox[dst + 1] = mbox[dst + 1] + mbox[im]
  end
  lineval[p1] = 0
  lineval[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)
  if x == 0 then return y end
  if y == 0 then return x end
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    ret = ret + mul * (1 - (1 - x % 2) * (1 - y % 2))
    -- if 1 <= (x % 2) + (y % 2) then
    --   ret = ret + mul
    -- end
    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 st = SegTree.new(2 * n - 1, function(a, b)
  return len[a] < len[b] and a or b end,
  n + 1
)

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)
  st:setValue(#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 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 = st:getRange(pu, pv)
    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
  if 0 < actsum then
  end
  -- print("IL", il, actsum)
  for im = mtot, 1, -1 do
    local src = im - 1
    local dst = getor(src, actsum)
    -- print("a " .. src .. " " .. dst)
    mbox[dst + 1] = mbox[dst + 1] + mbox[src + 1]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
  -- print(table.concat(mbox, " "))
end
-- print(table.concat(mbox, " "))
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)


local n=io.read("n")
local t={}
for i=1,n do
    local x,l=io.read("n","n")
    t[i]={x+l,x-l}
end
table.sort(t,function(x,y)
    return x[1]<y[1]
end)

local counter,eoa=0,-(10^9+1)
for i=1,n do
    if eoa<=t[i][2] then
        counter=counter+1
        eoa=t[i][1]
    end
end
print(counter)

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 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 function getlength(start_idx, dst_idx, n)
  local taskstate = {}
  for i = 1, n do taskstate[i] = false end
  local tasks = {}
  local tasknum = 0
  local done = 0

  local len = {}
  for i = 1, n do len[i] = -1 end
  len[start_idx] = 0

  local function addtask(idx)
    if not taskstate[idx] then
      taskstate[idx] = true
      tasknum = tasknum + 1
      tasks[tasknum] = idx
    end
  end
  addtask(start_idx)

  while done < tasknum do
    done = done + 1
    local src = tasks[done]
    taskstate[src] = false
    local tmp = {}
    for dst, cost in pairs(edge[src]) do
      if len[dst] < len[src] + cost then
        len[dst] = len[src] + cost
        addtask(dst)
      end
    end
  end
  return len[dst_idx]
end

print(getlength(1, #ac, #ac))


local mmi = math.min
local inf = 1000000007
local n = io.read("*n")
local a, b = {}, {}
for i = 1, n do
  a[i] = io.read("*n")
end
for i = 1, n do
  b[i] = io.read("*n")
end
local alltask = {}
local tot = 1
local stagetask = {}

local function heldkarp_prepare()
  for i = 1, n do
    tot = tot * 2
    alltask[i] = {}
    stagetask[i] = {}
  end
  for i = 1, n do
    for j = 1, tot - 1 do
      -- set inf value
      alltask[i][j] = 1000000007
    end
  end
  for i = 1, tot - 1 do
    local ti = i
    local cnt = 0
    for j = 1, n do
      if ti % 2 == 1 then
        cnt = cnt + 1
        ti = ti - 1
      end
      ti = ti / 2
    end
    table.insert(stagetask[cnt], i)
  end
  -- set first state
  local tmp = 1
  for i = 1, n do
    alltask[i][tmp] = i - 1
    tmp = tmp * 2
  end
end

local function heldkarp_doall()
  for stage = 1, n - 1 do
    local stlen = #stagetask[stage]
    for i_stagetask = 1, stlen do
      local used = stagetask[stage][i_stagetask]
      local t_used = used
      for i = 1, n do
        if t_used % 2 == 1 then
          local curcost = alltask[i][used]
          local v = (i + stage) % 2 == 0 and a[i] or b[i]
          local mul = 1
          local tmp = used
          local remcnt = 0
          for j = 1, n do
            if tmp % 2 == 0 then
              remcnt = remcnt + 1
              local jlen = remcnt - 1
              local nxtv = (j + stage + 1) % 2 == 0 and a[j] or b[j]
              if v <= nxtv then
                alltask[j][used + mul] = mmi(alltask[j][used + mul], jlen + curcost)
              end
            else
              tmp = tmp - 1
            end
            tmp = tmp / 2
            mul = mul * 2
          end
          t_used = t_used - 1
        end
        t_used = t_used / 2
      end
    end
  end
end

local function heldkarp_getresult()
  local ret = 1000000007
  for i = 1, n do
    ret = mmi(ret, alltask[i][tot - 1])
  end
  return ret
end
heldkarp_prepare()
heldkarp_doall()
local ans = heldkarp_getresult()
print(ans == 1000000007 and -1 or ans)

Score : 700 points

Problem Statement

We have N cards numbered 1, 2, ..., N.
Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side.
Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

Choose an integer i (1 \leq i \leq N - 1).
Swap the i-th and (i+1)-th cards from the left, then flip these two cards.

Constraints

1 \leq N \leq 18

1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If it is impossible to have a non-decreasing sequence, print -1.
If it is possible, print the minimum number of operations required to achieve it.

Sample Input 1

3
3 4 3
3 2 3

Sample Output 1

1

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.

Sample Input 2

2
2 1
1 2

Sample Output 2

-1

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.

Sample Input 3

4
1 2 3 4
5 6 7 8

Sample Output 3

0

No operation may be required.

Sample Input 4

5
28 15 22 43 31
20 22 43 33 32

Sample Output 4

-1

Sample Input 5

5
4 46 6 38 43
33 15 18 27 37

Sample Output 5

3


a = io.read():byte() + 1
print(string.char(a))

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.


n,m=io.read("*n","*n","*l")
t={}
for i=1,m do
    x=io.read()
    p,s=x:match("(%d+) (%u+)")
    if not t[p] then
        t[p]={}
    end
    table.insert(t[p],s)
end

ac_counter=0
wa_counter=0
for k in pairs(t) do
    for i=1,#t[k] do
        if t[k][i]=="AC" then
            ac_counter=ac_counter+1
            wa_counter=wa_counter+i-1
            break
        end
    end
end

print(ac_counter.." "..wa_counter)

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


n,m=io.read("*n","*n","*l")
t={}

if m==0 then
    print(0,0)
    return
end
for i=1,m do
    x=io.read()
    p,s=x:match("(%d+) (%u+)")
    if not t[p] then
        t[p]={}
        table.insert(t[p],s)
    else
        table.insert(t[p],s)
    end
end

ac_counter=0
wa_counter=0
for k,_ in pairs(t) do
    for i=1,#t[k] do
        if t[k][i]=="AC" then
            ac_counter=ac_counter+1
            do
                break
            end
        elseif t[k][i]=="WA" then
            wa_counter=wa_counter+1
        end
    end
end

print(ac_counter,wa_counter)


n,m=io.read("*n","*n","*l")
t={}

if m==0 then
    print(0,0)
    return
end
for i=1,m do
    x=io.read()
    p,s=x:match("(%d+) (%u+)")
    if not t[p] then
        t[p]={}
        table.insert(t[p],s)
    else
        table.insert(t[p],s)
    end
end

ac_counter=0
wa_counter=0
for k,_ in pairs(t) do
    for i=1,#t[k] do
        if t[k][i]=="AC" then
            ac_counter=ac_counter+1
            do
                break
            end
        else
            wa_counter=wa_counter+1
        end
    end
end

print(ac_counter,wa_counter)


n,m=io.read("*n","*n","*l")
t={}

if m==0 then
    print(0,0)
    return
end
for i=1,m do
    x=io.read()
    p,s=x:match("(%d+) (%u+)")
    if not t[p] then
        t[p]={}
        table.insert(t[p],s)
    else
        table.insert(t[p],s)
    end
end

ac_counter=0
wa_counter=0
for k,_ in pairs(t) do
    for i=1,#t[k] do
        if t[k][i]=="AC" then
            ac_counter=ac_counter+1
            break
        else
            wa_counter=wa_counter+1
        end
    end
end

print(ac_counter,wa_counter)


n,m=io.read("*n","*n","*l")
t={}

if m==0 then
    print(0,0)
end
for i=1,m do
    x=io.read()
    p,s=x:match("(%d+) (%u+)")
    if not t[p] then
        t[p]={}
        table.insert(t[p],s)
    else
        table.insert(t[p],s)
    end
end

wa_counter=0
ac_counter=0
for k,_ in pairs(t) do
    counter_flag=false
    for i=1,#t[k] do
        if t[k][i]=="AC" and not counter_flag then
            ac_counter=ac_counter+1
            counter_flag=true
        elseif t[k][i]=="WA" and not counter_flag then
            wa_counter=wa_counter+1
        end
    end
end

print(ac_counter,wa_counter)


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"
M=io.read"*n"
io.read()
p,S={},{}
for i=1,M do
p[i],S[i]=io.read():match"(%d+)%s+(%S+)"
p[i]=tonumber(p[i])
end

t={}
for i=1,M do
	if(t[p[i]])then
		if(t[p[i]]>0)then
			if(S[i]=="WA")then
				t[p[i]]=t[p[i]]+1
			else
				t[p[i]]=-t[p[i]]
			end
		end
	else
		if(S[i]=="WA")then
			t[p[i]]=1
		else
			t[p[i]]=0
		end
	end
end
ac,pn=0,0
for i=1,N do
	if(t[i])then
		if(t[i]<=0)then
			ac=ac+1
			pn=pn-t[i]
		end
	end
end
print(ac,pn)


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

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

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

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

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

----------

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

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

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

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

    return result
end

local result=0
for i=1,h do
    for j=1,w do
        if maze[i][j]~="#" then
            result=math.max(result,bfs(i,j))
        end
    end
end
print(result)

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


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

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

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

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

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

----------

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

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

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

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

    return result
end

local result=0
for i=1,h do
    for j=1,w do
        if maze[i][j]~="#" then
            result=math.max(result,bfs(i,j))
        end
    end
end
print(result)


local mma = math.max
local h, w = io.read("*n", "*n", "*l")
local map = {}
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    map[(i - 1) * w + j] = s:sub(j, j) == "."
  end
end

local function getlength(start_idx, dst_idx)
  local taskstate = {}
  for i = 1, h * w do taskstate[i] = false end
  local tasks = {}
  local tasknum = 0
  local done = 0

  local len = {}
  local inf = -1
  for i = 1, h * w do len[i] = inf end
  len[start_idx] = 0

  local function addtask(idx)
    if not taskstate[idx] then
      taskstate[idx] = true
      tasknum = tasknum + 1
      tasks[tasknum] = idx
    end
  end
  addtask(start_idx)

  local function walk(src, dst)
    if map[dst] then
      if len[dst] == inf or len[src] + 1 < len[dst] then
        len[dst] = len[src] + 1
        addtask(dst)
      end
    end
  end

  while done < tasknum do
    done = done + 1
    local idx = tasks[done]
    taskstate[idx] = false
    local tmp = {}

    if w < idx then walk(idx, idx - w) end
    if idx <= (h - 1) * w then walk(idx, idx + w) end
    if 1 < w then
      if idx % w ~= 0 then walk(idx, idx + 1) end
      if idx % w ~= 1 then walk(idx, idx - 1) end
    end
  end
  local ret = 0
  for i = 1, h * w do
    if 0 < len[i] then
      ret = mma(ret, len[i])
    end
  end
  return ret
end
local totret = 0
for i = 1, h * w do
  if map[i] then
    totret = mma(totret, getlength(i))
  end
end
print(totret)


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 n = io.read("*n")
local t = {io.read("*n")}
for i = 2, n do
  t[i] = io.read("*n")
end

local curlen = 0
local ret = 0
do
  curlen = t[n - 1] - t[1]
  ret = badd(ret, bmul(curlen, modinv(2)))
end
for i_range = 2, n - 1 do
  curlen = curlen + t[n] - t[i_range] - (t[n] - t[n - i_range])
  local a = bmul(i_range, i_range + 1) % mod
  ret = badd(ret, bmul(curlen, modinv(a)))
end
for i_range = 1, n - 1 do
  ret = badd(ret, bmul(modinv(i_range), t[n] - t[n - i_range]))
end
for i = 2, n - 1 do
  ret = bmul(ret, i)
end
print(ret)

Score : 600 points

Problem Statement

There are N slimes standing on a number line.
The i-th slime from the left is at position x_i.

It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}.

Niwango will perform N-1 operations. The i-th operation consists of the following procedures:

Choose an integer k between 1 and N-i (inclusive) with equal probability.

Move the k-th slime from the left, to the position of the neighboring slime to the right.

Fuse the two slimes at the same position into one slime.

Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime.

Constraints

2 \leq N \leq 10^{5}

1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}

x_i is an integer.

Subtasks

400 points will be awarded for passing the test cases satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N
x_1 x_2 \ldots x_N

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

5

With probability \frac{1}{2}, the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2.

With probability \frac{1}{2}, the middle slime is chosen in the first operation, in which case the total distance traveled is 3.

The answer is the expected total distance traveled, 2.5, multiplied by 2!, which is 5.

Sample Input 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

Sample Output 2

750927044

Find the expected value multiplied by (N-1)!, modulo (10^9+7).


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 n = io.read("*n")
local t = {io.read("*n")}
for i = 2, n do
  t[i] = io.read("*n")
end

local curlen = 0
local ret = 0
do
  curlen = t[n - 1] - t[1]
  ret = badd(ret, bmul(curlen, modinv(2)))
end
for i_range = 2, n - 1 do
  curlen = curlen + t[n] - t[i_range] - (t[n] - t[n - i_range])
  local a = (i_range * (i_range + 1)) % mod
  ret = badd(ret, bmul(curlen, modinv(a)))
end
for i_range = 1, n - 1 do
  ret = badd(ret, bmul(modinv(i_range), t[n] - t[n - i_range]))
end
for i = 2, n - 1 do
  ret = bmul(ret, i)
end
print(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 K, X = read.nn()
print(500 * K >= X and "Yes" or "No")

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


N=io.read"*n"
io.read()
S=io.read()
s=0
for _ in S:gmatch("(ABC)")do s=s+1 end
print(s)

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
-- https://www.youtube.com/watch?v=nYFd7VHKyWQ
-- https://github.com/mission-peace/interview/blob/master/python/recursion/stringpermutation.py

-- For simplicity, this algorithm does not allow duplicate elements.

local function permute(seq)
    local inuse = {}
    local result = {}
    local function rec(level)
        if level > #seq then
            --print(table.concat(result, ""))
            coroutine.yield(result)
            return
        end
        for i=1,#seq do
            if not inuse[i] then
                result[level] = seq[i]
                inuse[i] = true
                rec(level + 1)
                inuse[i] = false
            end
        end
    end
    local function start()
        rec(1)
    end
    return coroutine.wrap(start)
end
----
-----
local N = read.n()
local A = {}
for i=1,N do
    A[i] = i
end
local function test()
    for k in permute(A) do
        print(table.concat(k,""))
    end
end
--test()
local P, Q = {}, {}
for i=1,N do
    P[i] = read.n()
end
for i=1,N do
    Q[i] = read.n()
end

local function sameseq(a, b)
    return table.concat(a, " ") == table.concat(b, " ")
end

local a, b
local i = 1
for k in permute(A) do
    if sameseq(k, P) then
        a = i
    end
    if sameseq(k, Q) then
        b = i
    end
    i = i + 1
end
print(math.abs(a-b))

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


local mmi, mma = math.min, math.max
local mfl = math.floor
local xor = bit.bxor
-- local function xor(x, y) return x end

local n = io.read("*n")
local ar, br = {}, {}
for i = 1, n do
  ar[i] = io.read("*n")
end
for i = 1, n do
  br[i] = io.read("*n")
end
local as, bs = {}, {}
for i = 1, n - 1 do
  as[i], bs[i] = xor(ar[i], ar[i + 1]), xor(br[i], br[i + 1])
end
as[n], bs[n] = xor(ar[n], ar[1]), xor(br[n], br[1])
local idx = {}
local v = {}
for i = 1, n do
  idx[i] = i
  v[i] = as[i]
end
-- stage 1
table.sort(idx, function(a, b) return v[a] < v[b] end)
do
  local cur = 1
  local v_nxt = {}
  for i = 1, n do v_nxt[i] = 0 end
  v_nxt[idx[1]] = 1
  for i = 2, n do
    if v[idx[i]] ~= v[idx[i - 1]] then
      cur = cur + 1
    end
    v_nxt[idx[i]] = cur
  end
  for i = 1, n do v[i] = v_nxt[i] end
end
local step = 1
local vd = {}
while step < n do
  local stepdst = {}
  for i = 1, n do
    local dst = i + step
    if n < dst then dst = dst - n end
    stepdst[i] = dst
  end
  local left, major = 1, v[idx[1]]
  local minor_min = v[stepdst[idx[1]]]
  local minor_max = minor_min
  local cur = 0
  for i = 1, n do
    local minor = v[stepdst[idx[i]]]
    minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    local isend = i == n or major ~= v[idx[i + 1]]
    if isend then
      local right = i
      if minor_min ~= minor_max then
        local t = {}
        for j = left, right do
          t[j - left + 1] = idx[j]
        end
        -- R-L=A, Max-Min=B: O(A+B) or O(AlogA)
        if minor_max - minor_min < 100000 then
          local box = {}
          for b = 1, minor_max - minor_min + 1 do box[b] = {} end
          for ti = 1, right - left + 1 do
            local tminor = v[stepdst[t[ti]]]
            table.insert(box[tminor - minor_min + 1], t[ti])
          end
          local tcur = 1
          for b = 1, minor_max - minor_min + 1 do
            for bb = 1, #box[b] do
              t[tcur] = box[b][bb]
              tcur = tcur + 1
            end
          end
        else
          table.sort(t, function(x, y) return v[stepdst[x]] < v[stepdst[y]] end)
        end
        local v_prv = 0
        for j = left, right do
          idx[j] = t[j - left + 1]
          local v_nxt = v[stepdst[idx[j]]]
          if j == left or v_nxt ~= v_prv then
            cur = cur + 1
          end
          vd[idx[j]] = cur
          v_prv = v_nxt
        end
      else
        cur = cur + 1
        for j = left, right do
          vd[idx[j]] = cur
        end
      end
      if i < n then
        left, major = i + 1, v[idx[i + 1]]
        minor_min = v[stepdst[idx[i + 1]]]
        minor_max = minor_min
      end
    else
      -- local minor = v[stepdst[idx[i]]]
      -- minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    end
  end
  for i = 1, n do v[i] = vd[i] end
  step = step * 2
end

local function comp_l(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if as[ai] < bs[i] then return true
    elseif bs[i] < as[ai] then return false
    end
  end
  return false
end

local function comp_u(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if bs[i] < as[ai] then return true
    elseif as[ai] < bs[i] then return false
    end
  end
  return false
end

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

local function upper_bound(ary)
  local num = #ary
  if num == 0 then return 1 end
  if comp_u(ary[1]) then return 1 end
  if not comp_u(ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if not comp_u(ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local lb = lower_bound(idx)
local ub = upper_bound(idx)
local t = {}
for i = lb, ub - 1 do
  table.insert(t, idx[i])
end
table.sort(t)
for i = 1, #t do
  local k = t[i] - 1
  local v = xor(ar[t[i]], br[1])
  print(k .. " " .. v)
end
-- print(os.clock())

Score : 600 points

Problem Statement

Given are two sequences a=\{a_0,\ldots,a_{N-1}\} and b=\{b_0,\ldots,b_{N-1}\} of N non-negative integers each.

Snuke will choose an integer k such that 0 \leq k < N and an integer x not less than 0, to make a new sequence of length N, a'=\{a_0',\ldots,a_{N-1}'\}, as follows:

a_i'= a_{i+k \mod N}\ XOR \ x

Find all pairs (k,x) such that a' will be equal to b.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq a_i,b_i < 2^{30}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{N-1}

Output

Print all pairs (k, x) such that a' and b will be equal, using one line for each pair, in ascending order of k (ascending order of x for pairs with the same k).

If there are no such pairs, the output should be empty.

Sample Input 1

3
0 2 1
1 2 3

Sample Output 1

1 3

If (k,x)=(1,3),

a_0'=(a_1\ XOR \ 3)=1

a_1'=(a_2\ XOR \ 3)=2

a_2'=(a_0\ XOR \ 3)=3

and we have a' = b.

Sample Input 2

5
0 0 0 0 0
2 2 2 2 2

Sample Output 2

0 2
1 2
2 2
3 2
4 2

Sample Input 3

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

Sample Output 3

2 2
5 5

Sample Input 4

2
1 2
0 0

Sample Output 4

No pairs may satisfy the condition.


local mmi, mma = math.min, math.max
local mfl = math.floor
local xor = bit.bxor
-- local function xor(x, y) return x end

local n = io.read("*n")
local ar, br = {}, {}
for i = 1, n do
  ar[i] = io.read("*n")
end
for i = 1, n do
  br[i] = io.read("*n")
end
local as, bs = {}, {}
for i = 1, n - 1 do
  as[i], bs[i] = xor(ar[i], ar[i + 1]), xor(br[i], br[i + 1])
end
as[n], bs[n] = xor(ar[n], ar[1]), xor(br[n], br[1])
local idx = {}
local v = {}
for i = 1, n do
  idx[i] = i
  v[i] = as[i]
end
-- stage 1
table.sort(idx, function(a, b) return v[a] < v[b] end)
do
  local cur = 1
  local v_nxt = {}
  for i = 1, n do v_nxt[i] = 0 end
  v_nxt[idx[1]] = 1
  for i = 2, n do
    if v[idx[i]] ~= v[idx[i - 1]] then
      cur = cur + 1
    end
    v_nxt[idx[i]] = cur
  end
  for i = 1, n do v[i] = v_nxt[i] end
end
local step = 1
local vd = {}
while step < n do
  local stepdst = {}
  for i = 1, n do
    local dst = i + step
    if n < dst then dst = dst - n end
    stepdst[i] = dst
  end
  local left, major = 1, v[idx[1]]
  local minor_min = v[stepdst[idx[1]]]
  local minor_max = minor_min
  local cur = 0
  for i = 1, n do
    local isend = i == n or major ~= v[idx[i + 1]]
    if isend then
      local right = i
      if minor_min ~= minor_max then
        local t = {}
        for j = left, right do
          t[j - left + 1] = idx[j]
        end
        table.sort(t, function(x, y) return v[stepdst[x]] < v[stepdst[y]] end)
        local v_prv = 0
        for j = left, right do
          idx[j] = t[j - left + 1]
          local v_nxt = v[stepdst[idx[j]]]
          if j == left or v_nxt ~= v_prv then
            cur = cur + 1
          end
          vd[idx[j]] = cur
          v_prv = v_nxt
        end
      else
        cur = cur + 1
        for j = left, right do
          vd[idx[j]] = cur
        end
      end
      if i < n then
        left, major = i + 1, v[idx[i + 1]]
        minor_min = v[stepdst[idx[i + 1]]]
        minor_max = minor_min
      end
    else
      local minor = v[stepdst[idx[i]]]
      minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    end
  end
  for i = 1, n do v[i] = vd[i] end
  step = step * 2
end

local function comp_l(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if as[ai] < bs[i] then return true
    elseif bs[i] < as[ai] then return false
    end
  end
  return false
end

local function comp_u(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if bs[i] < as[ai] then return true
    elseif as[ai] < bs[i] then return false
    end
  end
  return false
end

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

local function upper_bound(ary)
  local num = #ary
  if num == 0 then return 1 end
  if comp_u(ary[1]) then return 1 end
  if not comp_u(ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if not comp_u(ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local lb = lower_bound(idx)
local ub = upper_bound(idx)
local t = {}
for i = lb, ub - 1 do
  table.insert(t, idx[i])
end
table.sort(t)
for i = 1, #t do
  local k = t[i] - 1
  local v = xor(ar[t[i]], br[1])
  print(k .. " " .. v)
end
-- print(os.clock())


local mmi, mma = math.min, math.max
local mfl = math.floor
local xor = bit.bxor
-- local function xor(x, y) return x end

local n = io.read("*n")
local ar, br = {}, {}
for i = 1, n do
  ar[i] = io.read("*n")
end
for i = 1, n do
  br[i] = io.read("*n")
end
local as, bs = {}, {}
for i = 1, n - 1 do
  as[i], bs[i] = xor(ar[i], ar[i + 1]), xor(br[i], br[i + 1])
end
as[n], bs[n] = xor(ar[n], ar[1]), xor(br[n], br[1])
local idx = {}
local v = {}
for i = 1, n do
  idx[i] = i
  v[i] = as[i]
end
-- stage 1
table.sort(idx, function(a, b) return v[a] < v[b] end)
do
  local cur = 1
  local v_nxt = {}
  for i = 1, n do v_nxt[i] = 0 end
  v_nxt[idx[1]] = 1
  for i = 2, n do
    if v[idx[i]] ~= v[idx[i - 1]] then
      cur = cur + 1
    end
    v_nxt[idx[i]] = cur
  end
  for i = 1, n do v[i] = v_nxt[i] end
end
local step = 1
while step < n do
  local vd = {}
  local left, major = 1, v[idx[1]]
  local minor_min = v[idx[1 + step]]
  local minor_max = minor_min
  for i = 1, n do
    local isend = i == n or major ~= v[idx[i + 1]]
    if isend then
      if minor_min ~= minor_max then
        local right = i
        local t = {}
        for j = left, right do
          t[j - left + 1] = idx[j]
        end
        table.sort(t, function(x, y)
          x, y = x + step, y + step
          if n < x then x = x - n end
          if n < y then y = y - n end
          return v[x] < v[y] end)
        for j = left, right do
          idx[j] = t[j - left + 1]
        end
      end
      if i < n then
        left, major = i + 1, v[idx[i + 1]]
        local tmp = idx[i + 1] + step
        if n < tmp then tmp = tmp - n end
        minor_min = v[tmp]
        minor_max = minor_min
      end
    else
      local j = idx[i] + step
      if n < j then j = j - n end
      local minor = v[j]
      minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
    end
  end
  vd[idx[1]] = 1
  local cur = 1
  for i = 2, n do
    local idx_prv, idx_nxt = idx[i - 1], idx[i]
    local idx_prv_i, idx_nxt_i = idx_prv + step, idx_nxt + step
    if n < idx_prv_i then idx_prv_i = idx_prv_i - n end
    if n < idx_nxt_i then idx_nxt_i = idx_nxt_i - n end
    if v[idx_prv] ~= v[idx_nxt] or v[idx_prv_i] ~= v[idx_nxt_i] then
      cur = cur + 1
    end
    vd[idx_nxt] = cur
  end
  for i = 1, n do v[i] = vd[i] end
  step = step * 2
end

local function comp_l(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if as[ai] < bs[i] then return true
    elseif bs[i] < as[ai] then return false
    end
  end
  return false
end

local function comp_u(offset)
  for i = 1, n do
    local ai = offset - 1 + i
    if n < ai then ai = ai - n end
    if bs[i] < as[ai] then return true
    elseif as[ai] < bs[i] then return false
    end
  end
  return false
end

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

local function upper_bound(ary)
  local num = #ary
  if num == 0 then return 1 end
  if comp_u(ary[1]) then return 1 end
  if not comp_u(ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if not comp_u(ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local lb = lower_bound(idx)
local ub = upper_bound(idx)
local t = {}
for i = lb, ub - 1 do
  table.insert(t, idx[i])
end
table.sort(t)
for i = 1, #t do
  local k = t[i] - 1
  local v = xor(ar[t[i]], br[1])
  print(k .. " " .. v)
end
-- print(os.clock())


Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


local a, b, k = io.read("*n", "*n", "*n")
if k <= a then
  print(a - k .. " " .. b)
else
  print(0 .. " " .. math.max(0, b - (k - a)))
end

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


x = io.read("*n")

not_prime = {true}

for i = 2, 10 ^ 6 do
  if not_prime[i] == nil then
    if i >= x then
      print(i)
      break
    end
    cur = i * 2
    while cur < 10 ^ 6 do
      not_prime[cur] = true
      cur = cur + i
    end
  end
end

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


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

for i = 2, math.ceil(math.sqrt(X)) do
    if X%i==0 then
        X = X+1
    elseif X%i~=0 and i==math.ceil(math.sqrt(X)) then
        print(X)
    end
end


local x = io.read("*n")
while true do
  local lim = math.ceil(math.sqrt(x))
  local f = true
  for i = 2, lim do
    if x ~= i and x % i == 0 then
      f = false
      break
    end
  end
  if f then print(x) break else x = x + 1 end
end


local x = io.read("*n")
while true do
  local lim = math.ceil(math.sqrt(x))
  local f = true
  for i = 2, lim do
    if x % i == 0 then
      f = false
      break
    end
  end
  if f then print(x) break else x = x + 1 end
end