local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
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.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.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end

SegTree.right_bound = function(self, left, right)
  local retpos = left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    if not self.stage[stage][mce(l / sz)] then
      retpos = l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end

SegTree.left_bound = function(self, left, right)
  local retpos = right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    if not self.stage[stage][mfl(r / sz)] then
      retpos = r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end

SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local b = {}
for i = 1, n do
  a[i] = io.read("*n")
  b[i] = i
end
table.sort(b, function(x, y) return a[x] < a[y] end)
local st = SegTree.new(n + 1, function(x, y) return x or y end, false)
st:setValue(n + 1, true)

local ret = a[b[q]] - a[b[1]]
local available_count = n - k + 1
for i = 1, n do
  local topidx = b[i]
  local rightlim = st:right_bound(topidx, n) - 1
  local leftlim = st:left_bound(1, topidx) + 1
  local cur_count = mma(0, (rightlim - leftlim + 1) - k + 1)
  local left_count = mma(0, (topidx - 1 - leftlim + 1) - k + 1)
  local right_count = mma(0, (rightlim - (topidx + 1) + 1) - k + 1)
  local reduce = left_count + right_count - cur_count
  available_count = available_count + reduce
  if q <= available_count then
    ret = mmi(ret, a[b[q + i]] - a[b[i + 1]])
  else
    break
  end
end
print(ret)


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

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local t = {}
local rank = {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = i
  rank[i] = 0
end
table.sort(t, function(x, y) return a[x] < a[y] end)
for i = 1, n do
  rank[t[i]] = i
end
-- availability (1 or 0) by array index
local stFlag = SegTree.new(n, function(x, y) return x * y end, 0)
for i = 1, n do
  stFlag:setValue(i, 1, true)
end
stFlag:updateAll()
-- available counts by sorted array index
local stCnt = SegTree.new(n, function(x, y) return x + y end, 0)
local omit = {}
for i = 1, n do
  stCnt:setValue(i, 1, true)
  omit[i] = false
end
stCnt:updateAll()
local ret = a[t[q]] - a[t[1]]

for i_sorted = 1, n do
  if stCnt:getRange(i_sorted, i_sorted) == 1 then
    stCnt:setValue(i_sorted, 0)
    local idx = t[i_sorted]
    stFlag:setValue(idx, 0)
    local rb = idx == n and n or stFlag:right_bound(0, idx + 1, n)
    if rb - (idx + 1) < k then
      if idx + 1 < rb then
        for j = idx + 1, rb - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    else
      local tmp = {}
      for j = idx + 1, rb - 1 do
        table.insert(tmp, rank[j])
      end
      table.sort(tmp)
      for j = 1, rb - idx - k do
        omit[tmp[j]] = false
      end
      for j = rb - idx - k + 1, #tmp do
        omit[tmp[j]] = true
      end
    end
    local lb = idx == 1 and 1 or stFlag:left_bound(0, 1, idx - 1)
    if idx - 1 - lb < k then
      if lb < idx - 1 then
        for j = lb + 1, idx - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    else
      local tmp = {}
      for j = lb + 1, idx - 1 do
        table.insert(tmp, rank[j])
      end
      table.sort(tmp)
      for j = 1, idx - lb - k do
        omit[tmp[j]] = false
      end
      for j = idx - lb - k + 1, #tmp do
        omit[tmp[j]] = true
      end
    end
    local p1, p2 = nil, nil
    local c = 0
    for j = 1, n do
      if stCnt:getRange(j, j) == 1 and not omit[j] then
        c = c + 1
        if c == 1 then p1 = j end
        if c == q then p2 = j break end
      end
    end
    if not p2 then
      break
    else
      ret = mmi(ret, a[t[p2]] - a[t[p1]])
    end
  end
end
print(ret)

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

Score : 600 points

Problem Statement

You are given an integer sequence A of length N and an integer K.
You will perform the following operation on this sequence Q times:

Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).

Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible.
Find the smallest possible value of X-Y when the Q operations are performed optimally.

Constraints

1 \leq N \leq 2000

1 \leq K \leq N

1 \leq Q \leq N-K+1

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1.
Thus, the first operation removes A_3=1 and now we have A=(4,3,5,2).
In the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2.
In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1.

Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184

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 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.setRange = function(self, left, right, value)
  for idx = left, right do
    self.stage[self.stagenum][idx] = value
  end
  for i = self.stagenum - 1, 1, -1 do
    left, right = mce(left / 2), mce(right / 2)
    for j = left, right do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local stage, l, r = 1, 1, self.size[1]
  while true do
    local sz = self.size[stage]
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if l + sz <= r then stage, l = stage + 1, l + sz
      else break
      end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break
      end
    end
  end
  return retpos + 1
end
SegTree.right_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, left - 1
  local t1, t2, t3 = {1}, {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]
    while (l - 1) % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp > val then
      ret, retpos = tmp, l + sz - 1
      if retpos == right then break end
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l + sz) table.insert(t3, r) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
    end
  end
  return retpos + 1
end
SegTree.left_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, right + 1
  local t1, t2, t3 = {1}, {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]
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mfl(r / sz)])
    if tmp > val then
      ret, retpos = tmp, r - sz + 1
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l) table.insert(t3, r - sz) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, r - sz + 2) table.insert(t3, r) end
    end
  end
  return retpos - 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local t = {}
local rank = {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = i
  rank[i] = 0
end
table.sort(t, function(x, y) return a[x] < a[y] end)
for i = 1, n do
  rank[t[i]] = i
end
-- availability (1 or 0) by array index
local stFlag = SegTree.new(n, function(x, y) return x * y end, 1)
for i = 1, n do
  stFlag:setValue(i, 1, true)
end
stFlag:updateAll()
-- available counts by sorted array index
local stCnt = SegTree.new(n, function(x, y) return x + y end, 0)
for i = 1, n do
  stCnt:setValue(i, 1, true)
end
stCnt:updateAll()
local ret = a[t[q]] - a[t[1]]

for i_sorted = 1, n do
  if stCnt:getRange(i_sorted, i_sorted) == 1 then
    stCnt:setValue(i_sorted, 0)
    local idx = t[i_sorted]
    stFlag:setValue(idx, 0)
    local rb = idx == n and n or stFlag:right_bound(0, idx + 1, n)
    if rb - (idx + 1) < k then
      if idx + 1 < rb then
        stFlag:setRange(idx + 1, rb - 1, 0)
        for j = idx + 1, rb - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    end
    local lb = idx == 1 and 1 or stFlag:left_bound(0, 1, idx - 1)
    if idx - 1 - lb < k then
      if lb < idx - 1 then
        stFlag:setRange(lb + 1, idx - 1, 0)
        for j = lb + 1, idx - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    end
    local p1, p2 = stCnt:lower_bound(1), stCnt:lower_bound(q)
    if n < p2 then
      break
    else
      ret = mmi(ret, a[t[p2]] - a[t[p1]])
    end
  end
end
print(ret)


local a, b, c, k = io.read("*n", "*n", "*n", "*n")
ret = a - b
if k % 2 == 1 then ret = -ret end
print(ret)

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

1 \leq A,B,C \leq 10^9

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


a, b, c, d = io.read("*n", "*n", "*n", "*n")
if math.abs(a - c) <= d or (math.abs(a - b) <= d and math.abs(c - b) <= d) then
  print("Yes")
else
  print("No")
end

Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


a,b,c,d=io.read():match("(.+)%s(.+)%s(.+)%s(.+)")
print(math.abs(a-c)>d*1 and (math.abs(a-b)>d*1 or math.abs(b-c)>d*1)and"No"or"Yes")


X=io.read("n")
res=0
if X<4 then
	res=1
else
	k=math.floor(math.sqrt(X))
	for i=2,k do
		local j=2
		while math.pow(i,j)<=X do
			j=j+1
		end
		j=j-1
		res=math.floor(math.max(res,math.pow(i,j)))
	end
end
print(res)

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


x=io.read("*n")
Max=1
for i=2,math.sqrt(x) do
	a=i*i
	while a*i<=x do a=a*i end
	Max=math.max(Max,a)
end
print(Max)


local par={}
local rank={}
--local size={}

local function init(n)
    for i=1,n do
        par[i]=i
        rank[i]=1
        --size[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
        par[x]=y
        --size[y]=size[y]+size[x]
    else
        par[x]=y
        --size[y]=size[y]+size[x]
        if rank[x]==rank[y] then
            rank[y]=rank[y]+1
        end
    end
end

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

----------

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

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

for i=1,m do
    local x,y=io.read("n","n")
    unite(x,y)
end

local counter=0
for i=1,n do
    if same(p[i],i) then
        counter=counter+1
    end
end
print(counter)

Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


local s=io.read()
local n=#s
local k=io.read("n")
local t1={}
for i=1,k do
    for j=1,n-i+1 do
        local sub=s:sub(j,j+i-1)
        table.insert(t1,sub)
    end
end
table.sort(t1)

local t2={t1[1]}
for i=1,#t1-1 do
    if t1[i]~=t1[i+1] then
        table.insert(t2,t1[i+1])
    end
end
print(t2[k])

Score : 300 points

Problem Statement

You are given a string s.
Among the different substrings of s, print the K-th lexicographically smallest one.

A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.
Also, we say that substrings are different when they are different as strings.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


local n, m = io.read("*n", "*n","*l")
local str = io.read()
local posval = {}
for strnum in string.gmatch(str, "%d+") do
  table.insert(posval, tonumber(strnum))
end

local parent = {}
for i = 1, n do parent[i] = 0 end
function findroot(x)
  local cand = x
  while(parent[cand] ~= 0) do
    cand = parent[cand]
  end
  return cand
end

for i = 1, m do
  local a, b = io.read("*n", "*n")
  local aroot, broot = findroot(a), findroot(b)
  if(aroot ~= broot) then
    parent[broot] = aroot
  end
end
local ret = 0
for i = 1, n do
  local gsrc, gdst = findroot(i), findroot(posval[i])
  if(gsrc ~= 0 and gsrc == gdst) then ret = ret + 1 end
end
print(ret)


local a, b = io.read("*n", "*n")
local ret = a - 1
if a <= b then ret = ret + 1 end
print(ret)

Score: 100 points

Problem Statement

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

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

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

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

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

a b

Output

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

Sample Input 1

5 5

Sample Output 1

5

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

Sample Input 2

2 1

Sample Output 2

1

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

Sample Input 3

11 30

Sample Output 3

11

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


a,b=io.read():match("(.+)%s(.+)")
print(a*1>b*1 and math.floor(a-1)or a)


local t = {io.read("*n", "*n", "*n")}
local k = io.read("*n")
table.sort(t)
for i = 1, k do
  t[3] = t[3] * 2
end
print(t[1] + t[2] + t[3])

Score: 200 points

Problem Statement

There are three positive integers A, B and C written on a blackboard. E869120 performs the following operation K times:

Choose one integer written on the blackboard and let the chosen integer be n. Replace the chosen integer with 2n.

What is the largest possible sum of the integers written on the blackboard after K operations?

Constraints

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

K is an integer between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C
K

Output

Print the largest possible sum of the integers written on the blackboard after K operations by E869220.

Sample Input 1

5 3 11
1

Sample Output 1

30

In this sample, 5, 3, 11 are initially written on the blackboard, and E869120 can perform the operation once.

There are three choices:

Double 5: The integers written on the board after the operation are 10, 3, 11.

Double 3: The integers written on the board after the operation are 5, 6, 11.

Double 11: The integers written on the board after the operation are 5, 3, 22.

If he chooses 3., the sum of the integers written on the board afterwards is 5 + 3 + 22 = 30, which is the largest among 1. through 3.

Sample Input 2

3 3 4
2

Sample Output 2

22

E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows:

First, double 4. The integers written on the board are now 3, 3, 8.

Next, double 8. The integers written on the board are now 3, 3, 16.

Then, the sum of the integers eventually written on the blackboard is 3 + 3 + 16 = 22.


a={io.read("*n","*n","*n")}
k=bit.lshift(1,io.read("*n"))
table.sort(a)
print(a[3]*k+a[1]+a[2])


h, w = io.read("*n", "*n", "*l")
map = {}
for i = 1, h do
  map[i] = {}
  s = io.read()
  for j = 1, w do
    map[i][j] = s:sub(j, j) == "#"
  end
end
valid = true
for i = 1, h do
  for j = 1, w do
    if map[i][j] then
      f = false
      if 1 < i and map[i - 1][j] then
        f = true
      elseif i < h and map[i + 1][j] then
        f = true
      elseif 1 < j and map[i][j - 1] then
        f = true
      elseif j < w and map[i][j + 1] then
        f = true
      end
      if not f then valid = false end
    end
  end
end
print(valid and "Yes" or "No")

Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


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

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


local n = io.read("*n")
io.write("11")
local cnt = 1
local ofst = 21

local function isprime(x)
  local a = math.ceil(math.sqrt(x))
  for i = 2, a do
    if(x % i == 0) then return false end
  end
  return true
end

local t = {}
while(cnt < n) do
  if(isprime(ofst)) then
    cnt = cnt + 1
    io.write(" " .. ofst)
  end
  ofst = ofst + 10
end
io.write("\n")

Score: 400 points

Problem Statement

Print a sequence a_1, a_2, ..., a_N whose length is N that satisfies the following conditions:

a_i (1 \leq i \leq N) is a prime number at most 55 555.

The values of a_1, a_2, ..., a_N are all different.

In every choice of five different integers from a_1, a_2, ..., a_N, the sum of those integers is a composite number.

If there are multiple such sequences, printing any of them is accepted.

Notes

An integer N not less than 2 is called a prime number if it cannot be divided evenly by any integers except 1 and N, and called a composite number otherwise.

Constraints

N is an integer between 5 and 55 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print N numbers a_1, a_2, a_3, ..., a_N in a line, with spaces in between.

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

First, 3, 5, 7, 11 and 31 are all different, and all of them are prime numbers.

The only way to choose five among them is to choose all of them, whose sum is a_1+a_2+a_3+a_4+a_5=57, which is a composite number.

There are also other possible outputs, such as 2 3 5 7 13, 11 13 17 19 31 and 7 11 5 31 3.

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

2, 3, 5, 7, 11, 13 are all different prime numbers.

2+3+5+7+11=28 is a composite number.

2+3+5+7+13=30 is a composite number.

2+3+5+11+13=34 is a composite number.

2+3+7+11+13=36 is a composite number.

2+5+7+11+13=38 is a composite number.

3+5+7+11+13=39 is a composite number.

Thus, the sequence 2 3 5 7 11 13 satisfies the conditions.

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


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

Score : 200 points

Problem Statement

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

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

Constraints

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

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

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

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

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.


local n = io.read("*n", "*l")
local rowmap = {}
for i = 1, n do
  local tmps = io.read()
  rowmap[i] = {}
  tmps = tmps .. tmps
  for j = 1, n do
    rowmap[i][j] = tmps:sub(n + 2 - j, n + 2 - j + n - 1)
  end
end

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

local function check(r, c)
  for k = 1, n do
    local row_tgt = k + n - r
    if n < row_tgt then row_tgt = row_tgt - n end
    local row_ofst = 1 + c
    local col_tgt = k + n - c
    if n < col_tgt then col_tgt = col_tgt - n end
    local col_ofst = 1 + r
    if rowmap[row_tgt][row_ofst] ~= colmap[col_tgt][col_ofst] then return 0 end
  end
  return 1
end
local ret = 0
for i = 0, n - 1 do
  for j = 0, n - 1 do
    ret = ret + check(i, j)
  end
end
print(ret)

Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.
For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

First, choose two integers A and B ( 0 \leq A, B < N ).

Write one letter in each square on the second board.
Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.
Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

1 \leq N \leq 300

S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.

Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.


local n = io.read("*n", "*l")
local rowmap = {}
local coltmp = ""
for i = 1, n do
  local tmps = io.read()
  coltmp = coltmp .. tmps:sub(1, 1)
  rowmap[i] = {tmps}
  for j = 2, n do
    tmps = tmps:sub(n, n) .. tmps:sub(1, n - 1)
    rowmap[i][j] = tmps
  end
end

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

local function check(r, c)
  for k = 1, n do
    local row_tgt = k + n - r
    if n < row_tgt then row_tgt = row_tgt - n end
    local row_ofst = 1 + c
    local col_tgt = k + n - c
    if n < col_tgt then col_tgt = col_tgt - n end
    local col_ofst = 1 + r
    if rowmap[row_tgt][row_ofst] ~= colmap[col_tgt][col_ofst] then return 0 end
  end
  return 1
end
local ret = 0
for i = 0, n - 1 do
  for j = 0, n - 1 do
    ret = ret + check(i, j)
  end
end
print(ret)


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

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

local function check(r, c)
  for k = 1, n do
    local row_tgt = k + n - r
    if n < row_tgt then row_tgt = row_tgt - n end
    local row_ofst = 1 + c
    local col_tgt = k + n - c
    if n < col_tgt then col_tgt = col_tgt - n end
    local col_ofst = 1 + r
    if rowmap[row_tgt][row_ofst] ~= colmap[col_tgt][col_ofst] then return 0 end
  end
  return 1
end

local ret = 0
for i = 0, n - 1 do
  for j = 0, n - 1 do
    ret = ret + check(i, j)
  end
end
print(ret)


s = io.read()
r = 700
for i = 1, 3 do if s:sub(i, i) == "o" then r = r + 100 end end
print(r)

Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


S=io.read()
cost=700
for i=1,#S do
  if string.sub(S,i,i)=="o" then
    cost=cost+100
  end
end
print(cost)


s = io.read()
if s == "ooo" then
  print(1000)
elseif s == "xoo" then
  print(900)
elseif s == "oxo" then
  print(900)
elseif s == "oox" then
  print(800)
elseif s == "xxo" then
  print(800)
elseif s == "oxx" then
  print(800)
elseif s == "xox" then
  print(800)
else
  print(700)
end


n,x=io.read("*n","*n")
Min=200000
for i=1,n do
	m=io.read("*n")
	if Min>m then Min=m end
	x=x-m
end
print(n+math.floor(x/Min))

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


local a, b, x = io.read("*n", "*n", "*n")
if a <= x and x <= a + b then
  print("YES")
else
  print("NO")
end

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


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

local left = 0
local right = 0
for i = 1, N+1 do
    for j = 1, M do
        if i == A[j] and X < i then
            right = right + 1
        elseif i == A[j] and X > i then
            left = left + 1
        end
    end
end

print(math.min(left, right))

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


local N=io.read("n")
local M=io.read("n")
local X=io.read("n")
local A={}
local left=0
for i=1,M do
  A[i]=io.read("n")
  if A[i]<X then
    left=left+1
  end
end
print(math.min(left,M-left))


N=io.read("n")
X={}
B={}
for i=1,N do
  X[i]=io.read("n")
  B[i]=X[i]
end
table.sort(B)
left=B[N//2]
right=B[N//2+1]
for i=1,N do
  if X[i]<=left then
    print(right)
  	elseif X[i]>=right then
    print(left)
  end
end

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


N=io.read("n")
local a={}
for i=1,N do
	a[i]=io.read("n")
end
function c(n,m)
	local res=0
	if m<=n//2 then
		local s=1
		local k=1
		for i=n,n-m+1,-1 do
			s=s*i
		end
		for i=1,m do
			k=k*i
		end
		res=s//k
		else res=c(n,n-m)
	end
	return res
end
table.sort(a)
local a1=a[1]
local b1=a[2]
local total=c(a1,b1)
for i=1,N-1 do
	for j=i+1,N do
		if c(a[i],a[j])>total then
			total=c(a[i],a[j])
			a1=a[i]
			b1=a[j]
		end
	end
end
print(a1,b1)

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


N=io.read("n")
local a={}
for i=1,N do
	a[i]=io.read("n")
end
function c(n,m)
	local res=0
	if m<=n//2 then
		local s=1
		local k=1
		for i=n,n-m+1,-1 do
			s=s*i
		end
		for i=1,m do
			k=k*i
		end
		res=s//k
		else res=c(n,n-m)
	end
	return res
end
table.sort(a)
local a1=a[1]
local b1=a[2]
local total=c(a1,b1)
for i=1,N-1 do
	for j=i,N do
		if c(a[i],a[j])>total then
			total=c(a[i],a[j])
			a1=a[i]
			b1=a[j]
		end
	end
end
print(a1,b1)


local h, w = io.read("*n", "*n", "*l")
local map = {}
for i = 1, h do
  local str = io.read()
  for j = 1, w do
    map[(i - 1) * w + j] = str:sub(j, j)
  end
end
local vertical = {}
local vmap = {}
for i = 1, w do
  vertical[i] = {}
  for j = 1, h do
    vertical[i][j] = map[(j - 1) * w + i]
  end
  table.sort(vertical[i])
  vertical[i] = table.concat(vertical[i], "")
  local v = vertical[i]
  if vmap[v] then
    vmap[v] = nil
  else
    vmap[v] = true
  end
end
local vrem = 0
for k, v in pairs(vmap) do
  vrem = vrem + 1
end

local horizontal = {}
local hmap = {}
for i = 1, h do
  horizontal[i] = {}
  for j = 1, w do
    horizontal[i][j] = map[(i - 1) * w + j]
  end
  table.sort(horizontal[i])
  horizontal[i] = table.concat(horizontal[i], "")
  local v = vertical[i]
  if hmap[v] then
    hmap[v] = nil
  else
    hmap[v] = true
  end
end
local hrem = 0
for k, v in pairs(vmap) do
  hrem = hrem + 1
end
local f = vrem <= 1 and hrem <= 1
print(f and "YES" or "NO")

Score : 700 points

Problem Statement

There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter.
Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i.

Snuke can apply the following operation to this grid any number of times:

Choose two different rows and swap them. Or, choose two different columns and swap them.

Snuke wants this grid to be symmetric.
That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal.

Determine if Snuke can achieve this objective.

Constraints

1 \leq H \leq 12

1 \leq W \leq 12

|S_i| = W

S_i consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

If Snuke can make the grid symmetric, print YES; if he cannot, print NO.

Sample Input 1

2 3
arc
rac

Sample Output 1

YES

If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:

Sample Input 2

3 7
atcoder
regular
contest

Sample Output 2

NO

Sample Input 3

12 12
bimonigaloaf
faurwlkbleht
dexwimqxzxbb
lxdgyoifcxid
ydxiliocfdgx
nfoabgilamoi
ibxbdqmzxxwe
pqirylfrcrnf
wtehfkllbura
yfrnpflcrirq
wvcclwgiubrk
lkbrwgwuiccv

Sample Output 3

YES


a=io.read()
b,x=a:gsub("a","")
b,y=a:gsub("b","")
b,z=a:gsub("c","")
print(x==1 and y==1 and z==1 and "Yes"or "No")

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


local a, b, k = io.read("*n", "*n", "*n")
for i = a, b do
  if i < a + k or b < i + k then
    print(i)
  end
end

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1 \leq A \leq B \leq 10^9

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


Q=io.read("n")
for i=1,Q do
  A=io.read("n")
  B=io.read("n")
  if A>b then
    A,B=B,A
  end
  local count=0
  local k=1
  for i=B-1,A,-1 do
    if i*(A+k)<A*B then
      count=count+1
      k=k+1
    end
  end
  print(A*2+count-2)
end

Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.


--[[

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

4 9 20
3 9 7

*14

]]
local n = io.read("*n")
local wmax = 0
local score = 0
local ws, vs = {}, {}
local samesum = 0
for i = 1, n do
  local a, b = io.read("*n", "*n")
  if a < b then
    score = score + b
    wmax = wmax + b - a
  elseif a == b then
    samesum = samesum + b
  else
    table.insert(ws, a - b + 1)
    table.insert(vs, b)
  end
end
if 0 < wmax then
  score = score + samesum
end

local dp = {}
for i = 1, wmax - 1 do dp[i] = 0 end
local mma = math.max
local dpret = 0
local tn = #ws
for i = 1, tn do
  local w, v = ws[i], vs[i]
  for j = wmax - w, 1, -1 do
    dp[j + w - 1] = mma(dp[j + w - 1], dp[j] + v)
  end
  if w <= wmax then
    dp[w - 1] = mma(dp[w - 1], v)
  end
end
for i = 1, wmax - 1 do
  dpret = mma(dpret, dp[i])
end
print(score + dpret)

Score : 700 points

Problem Statement

You are given sequences A and B consisting of non-negative integers.
The lengths of both A and B are N, and the sums of the elements in A and B are equal.
The i-th element in A is A_i, and the i-th element in B is B_i.

Tozan and Gezan repeats the following sequence of operations:

If A and B are equal sequences, terminate the process.

Otherwise, first Tozan chooses a positive element in A and decrease it by 1.

Then, Gezan chooses a positive element in B and decrease it by 1.

Then, give one candy to Takahashi, their pet.

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible.
Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

1 \leq N \leq 2 × 10^5

0 \leq A_i,B_i \leq 10^9(1\leq i\leq N)

The sums of the elements in A and B are equal.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

Sample Input 1

2
1 2
3 2

Sample Output 1

2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

Tozan decreases A_1 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

Tozan decreases A_2 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

As A and B are equal, the process is terminated.

Sample Input 2

3
8 3
0 1
4 8

Sample Output 2

9

Sample Input 3

1
1 1

Sample Output 3

0


local s=io.read()

if #s==26 then
    for i=#s,2,-1 do
        if s:sub(i,i)>s:sub(i-1,i-1) then
            s=s:sub(1,i-1)
            break
        end
    end
    if #s==26 then
        print(-1)
        return
    else
        for i=97,122 do
            local c=string.char(i)
            if not s:find(c) and s:sub(#s,#s)<c then
                print(s:sub(1,#s-1)..c)
                break
            end
        end
    end
elseif #s==1 then
    for i=97,122 do
        local c=string.char(i)
        if s<c then
            print(c)
            break
        end
    end
else
    for i=97,122 do
        local c=string.char(i)
        if not s:find(c) then
            print(s..c)
            break
        end
    end
end

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


Score : 600 points

Problem Statement

Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.

She thinks that a set S = \{a_{1}, a_{2}, ..., a_{N}\} of distinct positive integers is called special if for all 1 \leq i \leq N, the gcd (greatest common divisor) of a_{i} and the sum of the remaining elements of S is not 1.

Nagase wants to find a special set of size N. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000.

Constraints

3 \leq N \leq 20000

Input

Input is given from Standard Input in the following format:

N

Output

Output N space-separated integers, denoting the elements of the set S. S must satisfy the following conditions :

The elements must be distinct positive integers not exceeding 30000.

The gcd of all elements of S is 1, i.e. there does not exist an integer d > 1 that divides all elements of S.

S is a special set.

If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.

Sample Input 1

3

Sample Output 1

2 5 63

\{2, 5, 63\} is special because gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7. Also, gcd(2, 5, 63) = 1. Thus, this set satisfies all the criteria.

Note that \{2, 4, 6\} is not a valid solution because gcd(2, 4, 6) = 2 > 1.

Sample Input 2

4

Sample Output 2

2 5 20 63

\{2, 5, 20, 63\} is special because gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9. Also, gcd(2, 5, 20, 63) = 1. Thus, this set satisfies all the criteria.


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

local aa = 0
local bb = 0

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

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

print(aa+bb)

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


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

local aa = 0
local bb = 0

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

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

print(aa+bb)


local N, D, X = io.read("*n", "*n", "*n")
local A = {}
local counter = N
for i = 1, N do
    A[i] = io.read("*n")
    counter = counter + (D-1)//A[i]
end
print(counter+X)

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp.
The camp had N participants and lasted for D days.
The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.
As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

1 \leq N \leq 100

1 \leq D \leq 100

1 \leq X \leq 100

1 \leq A_i \leq 100 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days.
Each participant eats chocolate pieces as follows:

The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.

The second participant eats one chocolate piece on Day 1 and 6, for a total of two.

The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56


local N, D, X = io.read("*n", "*n", "*n")
local A = {}
for i = 1, N do
    A[i] = io.read("*n")
end
table.sort(A)

local counter = N
for i = 1, N do
    counter = counter + (D-1)//A[i]
end
print(counter+X)


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

local total=0
for i=1,n+1 do
    total=total+math.abs(a[i]-a[i+1])
end

for i=2,n+1 do
    local x=math.abs(a[i-1]-a[i+1])
    local y=math.abs(a[i-1]-a[i])
    local z=math.abs(a[i]-a[i+1])
    print(total+x-y-z)
end

Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^5

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

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

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

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

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

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

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

Sample Output 3

21630
21630
19932
8924
21630
19288


Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


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

yen=0
for k,v in pairs(s) do
    if t[k] then
        x=t[k]
    else
        x=0
    end
    yen=math.max(yen,s[k]-x)
end
print(yen)

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


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


A=io.read()
B=io.read()
C=io.read()
print(string.sub(A,1,1)..string.sub(B,2,2)..string.sub(C,3,3))

Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


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

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

Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


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

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


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

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


n,m=io.read("*n","*n")
if n>4 and m>4 then
    print(n*m-(2*n+2*m-4))
    return
else
    grid={}
    for i=1,n+2 do
        grid[i]={}
        for j=1,m+2 do
            grid[i][j]="x"
        end
    end
    for i=2,n+1 do
        for j=2,m+1 do
            for k=-1,1 do
                for l=-1,1 do
                    if grid[i-k][j-l]=="x" then
                        grid[i-k][j-l]="o"
                    else
                        grid[i-k][j-l]="x"
                    end
                end
            end
        end
    end

    counter=0
    for i=2,n+1 do
        for j=2,m+1 do
            if grid[i][j]=="o" then
                counter=counter+1
            end
        end
    end
    print(counter)
end


local n,k=io.read("n","n")
if k==0 then
    print(n*n)
else
    local sum=0
    for i=1,n do
        sum=sum+math.max(0,i-k)*(n//i)+math.max(0,n%i-k+1)
    end
    print(sum)
end


local n, a, b = io.read("*n", "*n", "*n")
local bmax = n + 1 - a
local bmin = math.ceil(n / a)
if n < a or b < bmin or bmax < b then
  print(-1)
else
  local t = {}
  for i = 1, a do
    t[i] = n - a + i
  end
  local curmax = n - a
  local rem = n + 1 - a - b
  while 0 < rem do
    if a - 1 <= rem then
      for i = 1, a do
        table.insert(t, curmax - a + i)
      end
      curmax = curmax - a
      rem = rem - a + 1
    else
      for i = 1, rem + 1 do
        table.insert(t, curmax - rem - 1 + i)
      end
      curmax = curmax - rem - 1
      rem = 0
    end
  end
  for i = curmax, 1, -1 do
    table.insert(t, i)
  end
  print(table.concat(t, " "))
end

Score : 700 points

Problem Statement

Determine if there exists a sequence obtained by permuting 1,2,...,N that satisfies the following conditions:

The length of its longest increasing subsequence is A.

The length of its longest decreasing subsequence is B.

If it exists, construct one such sequence.

Notes

A subsequence of a sequence P is a sequence that can be obtained by extracting some of the elements in P without changing the order.

A longest increasing subsequence of a sequence P is a sequence with the maximum length among the subsequences of P that are monotonically increasing.

Similarly, a longest decreasing subsequence of a sequence P is a sequence with the maximum length among the subsequences of P that are monotonically decreasing.

Constraints

1 \leq N,A,B \leq 3\times 10^5

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

If there are no sequences that satisfy the conditions, print -1.

Otherwise, print N integers. The i-th integer should be the i-th element of the sequence that you constructed.

Sample Input 1

5 3 2

Sample Output 1

2 4 1 5 3

One longest increasing subsequence of this sequence is {2,4,5}, and one longest decreasing subsequence of it is {4,3}.

Sample Input 2

7 7 1

Sample Output 2

1 2 3 4 5 6 7

Sample Input 3

300000 300000 300000

Sample Output 3

-1


Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


local mfl, mab = math.floor, math.abs
local h, w, d = io.read("*n", "*n", "*n")
local n = h * w
local map = {}
local cost = {}
for i = 1, n do
  map[i] = io.read("*n")
  cost[i] = 0
end
for t_i_d = 1, d do
  local src_x, src_y = 0, 0
  local costsum = 0
  local i_d = t_i_d
  while i_d <= n do
    for i = 1, n do
      if map[i] == i_d then
        local dst_x, dst_y = 1 + (i - 1) % w, 1 + mfl((i - 1) / w)
        costsum = costsum + mab(dst_x - src_x) + mab(dst_y - src_y)
        cost[i_d] = costsum
        src_x, src_y = dst_x, dst_y
        break
      end
    end
    i_d = i_d + d
  end
end
local q = io.read("*n")
for i = 1, q do
  local l, r = io.read("*n", "*n")
  print(cost[r] - cost[l])
end

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0


local s = io.read()
local all9 = true
for i = 2, #s do
  if s:sub(i, i) ~= "9" then
    all9 = false
  end
end
local n = tonumber(s:sub(1, 1))
n = n + 9 * (#s - 1)
if not all9 then n = n - 1 end
print(n)

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


Score: 100 points

Problem Statement

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
A

Output

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

Sample Input 1

2018
218

Sample Output 1

Yes

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

Sample Input 2

2763
0

Sample Output 2

No

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

Sample Input 3

37
514

Sample Output 3

Yes


Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


-- capture
local h, w = io.read("*n", "*n", "*l")
local s = h * w
local str = ""
local t = {}
local white = 0
for i = 1, h do
  str = io.read()
  for j = 1, w do
    if(string.sub(str, j, j) == ".") then
      t[(i - 1) * w + j] = 0
      white = white + 1
    else
      t[(i - 1) * w + j] = -1
    end
  end
end
t[1] = 1
local check_pos = {1}
local rem_count, task_idx, step = 1, 1, 1
while(0 < rem_count) do
  local idx = check_pos[task_idx]
  rem_count, task_idx = rem_count - 1, task_idx + 1
  -- left
  if(idx % w ~= 1) then
    if(t[idx - 1] == 0) then t[idx - 1] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx - 1) end
  end
  -- right
  if(idx % w ~= 0) then
    if(t[idx + 1] == 0) then t[idx + 1] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx + 1) end
  end
  -- up
  if(w < idx) then
    if(t[idx - w] == 0) then t[idx - w] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx - w) end
  end
  -- down
  if(idx <= s - w) then
    if(t[idx + w] == 0) then t[idx + w] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx + w) end
  end
end
if(0 < t[s]) then print(white - t[s]) else print(-1) end

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


local 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 asum, bsum = 0, 0
local c = {}
for i = 1, n do
  c[i] = b[i] - a[i]
  asum, bsum = asum + a[i], bsum + b[i]
end
table.sort(c)
local cnt = bsum - asum
for i = 1, n do
  if c[i] < 0 then
    cnt = cnt + c[i]
  elseif 0 < c[i] then
    if c[i] % 2 == 1 then cnt = cnt - 1 end
  end
end
print(0 <= cnt and "Yes" or "No")

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


local n = io.read("*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  a = a + 1
  b = b + 1
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local parent = {}
local asked = {}
local asked_child_cnt = {}
local root = nil
local tasks = {}
local required = {}
for i = 1, n do
  required[i] = false
  parent[i] = 0
  asked[i] = false
  asked_child_cnt[i] = 0
  if #edge[i] == 1 then
    if root then
      table.insert(tasks, i)
    else
      root = i
    end
  end
end
local done = 0
while done < #tasks do
  done = done + 1
  local child = tasks[done]
  asked[child] = true
  if child == root then break end
  if 3 <= #edge[child] then
    required[child] = true
  end
  for i = 1, #edge[child] do
    local pcand = edge[child][i]
    if not asked[pcand] then
      parent[child] = pcand
      asked_child_cnt[pcand] = asked_child_cnt[pcand] + 1
      if asked_child_cnt[pcand] == #edge[pcand] - 1 then
        table.insert(tasks, pcand)
      end
    end
  end
end
-- for i = 1, n do
--   asked[i] = false
-- end
-- asked[root] = true
tasks[1] = edge[root][1]
tasknum = 1
done = 0
local ret = 1
while done < tasknum do
  done = done + 1
  local p = tasks[done]
  local required_cnt = #edge[p] - 2
  if 0 < required_cnt then
    for i = 1, #edge[p] do
      local c = edge[p][i]
      if c ~= parent[p] then
        if required[c] then
          tasknum = tasknum + 1
          tasks[tasknum] = c
          required_cnt = required_cnt - 1
        end
      end
    end
    if 0 < required_cnt then
      ret = ret + required_cnt
    end
  end
end
print(ret)

Score : 900 points

Problem Statement

We have a tree with N vertices.
The vertices are numbered 0 through N - 1, and the i-th edge (0 ≤ i < N - 1) comnnects Vertex a_i and b_i.
For each pair of vertices u and v (0 ≤ u, v < N), we define the distance d(u, v) as the number of edges in the path u-v.

It is expected that one of the vertices will be invaded by aliens from outer space.
Snuke wants to immediately identify that vertex when the invasion happens.
To do so, he has decided to install an antenna on some vertices.

First, he decides the number of antennas, K (1 ≤ K ≤ N).
Then, he chooses K different vertices, x_0, x_1, ..., x_{K - 1}, on which he installs Antenna 0, 1, ..., K - 1, respectively.
If Vertex v is invaded by aliens, Antenna k (0 ≤ k < K) will output the distance d(x_k, v).
Based on these K outputs, Snuke will identify the vertex that is invaded.
Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:

For each vertex u (0 ≤ u < N), consider the vector (d(x_0, u), ..., d(x_{K - 1}, u)). These N vectors are distinct.

Find the minumum value of K, the number of antennas, when the condition is satisfied.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i, b_i < N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_0 b_0
a_1 b_1
:
a_{N - 2} b_{N - 2}

Output

Print the minumum value of K, the number of antennas, when the condition is satisfied.

Sample Input 1

5
0 1
0 2
0 3
3 4

Sample Output 1

2

For example, install an antenna on Vertex 1 and 3.
Then, the following five vectors are distinct:

(d(1, 0), d(3, 0)) = (1, 1)

(d(1, 1), d(3, 1)) = (0, 2)

(d(1, 2), d(3, 2)) = (2, 2)

(d(1, 3), d(3, 3)) = (2, 0)

(d(1, 4), d(3, 4)) = (3, 1)

Sample Input 2

2
0 1

Sample Output 2

1

For example, install an antenna on Vertex 0.

Sample Input 3

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

Sample Output 3

3

For example, install an antenna on Vertex 0, 4, 9.


m = io.read("*number")-io.read("*number")
y = io.read("*number")
print(m-math.floor(m/y)*y)

Score : 100 points

Problem Statement

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

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

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

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

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


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

total=0
for i=1,n do
    count=0
    for j=1,i do
        count=count+a[1][j]
    end
    for k=i,n do
        count=count+a[2][k]
    end
    total=math.max(count,total)
end
print(total)

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

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

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


local N = io.read("n")
local A1, A2 = {}, {}
for i=1,N do A1[i]=io.read("n") end
for i=1,N do A2[i]=io.read("n") end
local ans = 0
for sw=1,N do
  local c=0
  for i=1,sw do
    c=c+A1[i]
  end
  for i=sw,N do
    c=c+A2[i]
  end
  ans=math.max(ans,c)
end
print(ans)


local n = io.read("*n")
local a, b = {}, {}
a[1], b[1] = 0, 0
for i = 2, n+1 do a[i] = a[i-1] + io.read("*n") end
for i = 2, n+1 do b[i] = b[i-1] + io.read("*n") end
local max = 0
local bm = b[n+1]
for i = 2, n+1 do
  max = math.max(max, a[i]+bm-b[i-1])
end
print(max)