local mmi, mma = math.min, math.max
local r, c, k = io.read("*n", "*n", "*n")
local item_row = {}
for i = 1, r do
  item_row[i] = {}
end
for i = 1, k do
  local ra, ca, v = io.read("*n", "*n", "*n")
  table.insert(item_row[ra], {ca, v})
end
for i = 1, r do
  table.sort(item_row[i], function(a, b) return a[1] < b[1] end)
end

local v1, v2, v3 = 0, 0, 0
local function box_reset(val)
  v1, v2, v3 = val, 0, 0
end
local function box_update(val)
  if val <= v3 then return end
  v3 = val
  if val <= v2 then return end
  v2, v3 = val, v2
  if v1 < val then
    v1, v2 = val, v1
  end
end
local function getboxval()
  return v1 + v2 + v3
end

local tbl, tbl2 = {}, {}
for i = 1, r do
  if i == 1 then
    for j = 1, c do
      tbl[j] = 0
      tbl2[j] = 0
    end
  else
    for j = 1, c do
      tbl[j] = tbl2[j]
    end
  end
  local item = item_row[i]
  for j = 1, #item do
    local col, val = item[j][1], item[j][2]
    box_reset(val)
    local baseval = tbl[col]
    tbl2[col] = mma(tbl2[col], baseval + getboxval())
    for k = j + 1, #item do
      local c2, v2 = item[k][1], item[k][2]
      box_update(v2)
      tbl2[c2] = mma(tbl2[c2], baseval + getboxval())
    end
  end
  for j = 2, c do
    tbl2[j] = mma(tbl2[j], tbl2[j - 1])
  end
end
print(tbl2[c])


local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local mfl, mce = math.floor, math.ceil

local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end

local t2, t5 = {}, {}
local t2min, t2max, t5min, t5max = 0, 0, 0, 0

local n = io.read("*n", "*l")
for i = 1, n do
  local s = io.read()
  local c2, c5 = 0, 0
  if s:find("%.") then
    local x, y = s:match("(%d+)%.(%d+)")
    x = tonumber(x)
    local dig = #y
    local denom = 1
    for j = 1, #y do
      denom = denom * 10
    end
    y = tonumber(y)
    local gcd = getgcd(denom, y)
    denom = mfl(denom / gcd)
    y = mfl(y / gcd)
    local a = x * denom + y
    local b = denom

    while a % 2 == 0 do
      a = mfl(a / 2)
      c2 = c2 + 1
    end
    while a % 5 == 0 do
      a = mfl(a / 5)
      c5 = c5 + 1
    end
    while b % 2 == 0 do
      b = mfl(b / 2)
      c2 = c2 - 1
    end
    while b % 5 == 0 do
      b = mfl(b / 5)
      c5 = c5 - 1
    end
  else
    local x = tonumber(s)
    while x % 2 == 0 do
      x = mfl(x / 2)
      c2 = c2 + 1
    end
    while x % 5 == 0 do
      x = mfl(x / 5)
      c5 = c5 + 1
    end
  end
  t2[i] = c2
  t5[i] = c5
  t2min = mmi(t2min, c2)
  t2max = mma(t2max, c2)
  t5min = mmi(t5min, c5)
  t5max = mma(t5max, c5)
end
local t = {}
local wid2 = t2max - t2min + 1
local wid5 = t5max - t5min + 1
local sz = wid2 * wid5
for i = 1, sz do
  t[i] = 0
end
local ret = 0
for i = 1, n do
  local c2, c5 = t2[i], t5[i]
  local need2, need5 = mma(t2min, -c2), mma(t5min, -c5)
  for j = need2, t2max do
    for k = need5, t5max do
      local idx = (j - t2min) * wid5 + (k - t5min + 1)
      ret = ret + t[idx]
    end
  end
  do
    local idx = (c2 - t2min) * wid5 + (c5 - t5min + 1)
    t[idx] = t[idx] + 1
  end
end
print(ret)

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

Score : 300 points

Problem Statement

You are given N real values A_1, A_2, \ldots, A_N.
Compute the number of pairs of indices (i, j)
such that i < j and the product A_i \cdot A_j is integer.

Constraints

2 \leq N \leq 200\,000

0 < A_i < 10^4

A_i is given with at most 9 digits after the decimal.

Input

Input is given from Standard Input in the following format.

N
A_1
A_2
\vdots
A_N

Output

Print the number of pairs with integer product A_i \cdot A_j (and i < j).

Sample Input 1

5
7.5
2.4
17.000000001
17
16.000000000

Sample Output 1

3

There are 3 pairs with integer product:

7.5 \cdot 2.4 = 18

7.5 \cdot 16 = 120

17 \cdot 16 = 272

Sample Input 2

11
0.9
1
1
1.25
2.30000
5
70
0.000000001
9999.999999999
0.999999999
1.000000001

Sample Output 2

8

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 = math.floor
local TranFFT = {}

TranFFT.initialize = function(self)
  self.size = 19
  -- 2^19 = 524288
  self.n = 524288
  -- 40003698689: prime,
  -- 40003698689 % 524288 = 1
  self.mod = 40003698689
  -- (463881^524288) % mod = 1
  self.w = 463881
  -- (40003622388 * 524288) % mod = 1
  self.ninv = 40003622388
  -- (1547695164 * 463881) % mod = 1
  self.winv = 1547695164

  self.p2 = {1}
  for i = 2, self.size do
    self.p2[i] = self.p2[i - 1] * 2
  end
  self.binv = {}
  for i = 1, self.n do
    local y, z = 0, i - 1
    for j = 1, self.size do
      y = y + (z % 2) * self.p2[self.size + 1 - j]
      z = mfl(z / 2)
    end
    self.binv[i] = y + 1
  end
  self.wmul = {1}
  local w1 = self.w % 10000
  local w2 = mfl(self.w / 10000) % 10000
  local w3 = mfl(self.w / 100000000)
  for i = 2, self.n do
    self.wmul[i] = self:mul_predef(self.wmul[i - 1], w1, w2, w3)
  end
  self.winvmul = {1}
  w1 = self.winv % 10000
  w2 = mfl(self.winv / 10000) % 10000
  w3 = mfl(self.winv / 100000000)
  for i = 2, self.n do
    self.winvmul[i] = self:mul_predef(self.winvmul[i - 1], w1, w2, w3)
  end
end
TranFFT.add = function(self, x, y) return (x + y) % self.mod end

-- super slow multiply functions
TranFFT.mul = function(self, x, y)
  local mod = self.mod
  local x1, y1 = x % 10000, y % 10000
  local x2, y2 = mfl(x / 10000) % 10000, mfl(y / 10000) % 10000
  local x3, y3 = mfl(x / 100000000), mfl(y / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret +  x3 * y3 * 10000 % mod * 10000 % mod * 10000 % mod * 10000) % mod
  return ret
end

TranFFT.mul_predef = function(self, x, y1, y2, y3)
  local mod = self.mod
  local x1 = x % 10000
  local x2 = mfl(x / 10000) % 10000
  local x3 = mfl(x / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret +  x3 * y3 * 10000 % mod * 10000 % mod * 10000 % mod * 10000) % mod
  return ret
end

TranFFT.twice = function(self, x)
  local mod = self.mod
  local x1 = x % 10000
  local x2 = mfl(x / 10000) % 10000
  local x3 = mfl(x / 100000000)
  local ret = (x1 * x1 + (2 * x1 * x2) * 10000) % mod
  ret = (ret + (2 * x1 * x3 + x2 * x2) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (2 * x2 * x3) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret +  x3 * x3 * 10000 % mod * 10000 % mod * 10000 % mod * 10000) % mod
  return ret
end

TranFFT.fft_common = function(self, ary, wmul)
  local ret = {}
  for i = 1, self.n do
    ret[i] = ary[self.binv[i]]
  end
  for i = 1, self.size do
    local step_size = self.p2[i]
    local step_count = self.p2[self.size + 1 - i]
    for istep = 1, step_count do
      local ofst = (istep - 1) * step_size * 2
      for j = 1, step_size do
        local a1, a2 = ret[ofst + j], ret[ofst + step_size + j]
        ret[ofst + j] = self:add(a1, self:mul(a2, wmul[1 + (j - 1) * step_count]))
        ret[ofst + step_size + j] = self:add(a1, self:mul(a2, wmul[1 + (j + step_size - 1) * step_count]))
      end
    end
  end
  return ret
end

TranFFT.fft = function(self, ary)
  return self:fft_common(ary, self.wmul)
end
TranFFT.ifft = function(self, ary)
  local ret = self:fft_common(ary, self.winvmul)
  for i = 1, self.n do
    ret[i] = self:mul(ret[i], self.ninv)
  end
  return ret
end

TranFFT:initialize()
local a = {}
for i = 1, TranFFT.n do
  a[i] = 0
end

local prime = 200003
local function pmul(x, y) return (x * y) % prime end
local function padd(x, y) return (x + y) % prime end

local p2 = {1}
local p2inv = {0}
for i = 2, prime - 1 do
  p2[i] = (p2[i - 1] * 2) % prime
  p2inv[i] = 0
end
for i = 1, prime - 1 do
  p2inv[p2[i]] = i
end

local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
  if 0 < t[i] then
    local v = p2inv[t[i]]
    a[v] = a[v] + 1
  end
end
local at = TranFFT:fft(a)
for i = 1, TranFFT.n do
  -- at[i] = TranFFT:mul(at[i], at[i])
  at[i] = TranFFT:twice(at[i])
end
local c = TranFFT:ifft(at)

local ret = {}
for i = 1, prime - 1 do
  ret[i] = 0
end
for i = 1, prime * 2 - 2 do
  local ti = i
  if prime <= ti then
    ti = ti - (prime - 1)
  end
  local v = p2[ti]
  ret[v] = ret[v] + c[i]
end
for i = 1, n do
  if 0 < t[i] then
    local v = pmul(t[i], t[i])
    ret[v] = ret[v] - 1
  end
end
local ans = 0
for i = 1, prime - 1 do
  ans = ans + i * mfl(ret[i] / 2)
end
print(ans)
-- print(os.clock())

Score : 800 points

Problem Statement

Let’s take a prime P = 200\,003.
You are given N integers A_1, A_2, \ldots, A_N.
Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2 unordered pairs of elements (i < j).

Please note that the sum isn't computed modulo P.

Constraints

2 \leq N \leq 200\,000

0 \leq A_i < P = 200\,003

All values in input are integers.

Input

Input is given from Standard Input in the following format.

N
A_1 A_2 \cdots A_N

Output

Print one integer — the sum over ((A_i \cdot A_j) \bmod P).

Sample Input 1

4
2019 0 2020 200002

Sample Output 1

474287

The non-zero products are:

2019 \cdot 2020 \bmod P = 78320

2019 \cdot 200002 \bmod P = 197984

2020 \cdot 200002 \bmod P = 197983

So the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.

Sample Input 2

5
1 1 2 2 100000

Sample Output 2

600013


print(io.read"*n">=30 and "Yes" or "No")

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


local MOD
local function add(a,b)return (a+b)%MOD end
local function sub(a,b)return a>b and a-b or a-b+MOD end
local function mul(a,b)return a*b%MOD end
local function pow(a,b)local ret=1 while(b>0)do if(b%2==1)then ret=mul(ret,a)b=b-1 end a=mul(a,a)b=b*.5 end return ret end
local function inv(x)local a,b,u,v=x,MOD,1,0 repeat local t=math.floor(a/b)a,b=b,a-t*b u,v=v,u-t*v until(b==0)return u%MOD end
local function div(a,b)return mul(a,inv(b))end
K=io.read"*n"

if(K%2==0)then print(-1)return end
MOD=K
a=0
i=0
while(true)do
a=add(a,mul(7,pow(10,i)))
if(a==0)then break end
i=i+1
end
print(i+1)

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i) == "W"
end
local left, right = {}, {}
do
  local c = 0
  for i = 1, n do
    if t[i] then c = c + 1 end
    left[i] = c
    right[i] = 0
  end
  c = 0
  for i = n, 1, -1 do
    if t[i] then c = c + 1 end
    right[i] = c
  end
end
local ret = mmi(n - right[1], left[n])
for i = 1, n - 1 do
  local need_red = left[i]
  local need_white = n - i - right[i + 1]
  ret = mmi(ret, mma(need_red, need_white))
end
print(ret)

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i) == "W"
end
local left, right = {}, {}
do
  local c = 0
  for i = 1, n do
    if t[i] then c = c + 1 end
    left[i] = c
    right[i] = 0
  end
  c = 0
  for i = n, 1, -1 do
    if t[i] then c = c + 1 end
    right[i] = c
  end
end
local ret = mmi(n - right[1], left[n])
for i = 1, n - 1 do
  local need_red = left[i]
  local need_white = n - i - right[n - i]
  ret = mmi(ret, mma(need_red, need_white))
end
print(ret)


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.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.addValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + value
  if not silent then
    self:update(idx)
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, q = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local query = {}
for iq = 1, q do
  local a, b = io.read("*n", "*n")
  query[iq] = {a, b, iq}
end
table.sort(query, function(a, b) return a[2] < b[2] end)
local p = {}
for i = 1, n do
  p[i] = 0
end
local st = SegTree.new(n, function(a, b) return a + b end, 0)
local ti = 1
for iq = 1, q do
  local l, r = query[iq][1], query[iq][2]
  while ti <= r do
    local v = t[ti]
    local old = p[v]
    if 0 < old then
      st:addValue(old, -1)
    end
    p[v] = ti
    st:addValue(ti, 1)
    ti = ti + 1
  end
  query[iq][4] = st:getRange(l, r)
end
table.sort(query, function(a, b) return a[3] < b[3] end)
for iq = 1, q do
  print(query[iq][4])
end

Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

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

Sample Input 1

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

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

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

Sample Output 2

1
2
2
1
2
2
6
3
3
3


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimensi0n, default_val) assert(type(default_val) ~= 'table') local n=dimensi0n local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
--
x = read.n()
if x < 400 then y = 0
elseif x < 600 then y = 8
elseif x < 800 then y = 7
elseif x < 1000 then y = 6
elseif x < 1200 then y = 5
elseif x < 1400 then y = 4
elseif x < 1600 then y = 3
elseif x < 1800 then y = 2
elseif x < 2000 then y = 1
end
print(y)

Score: 100 points

Problem Statement

M-kun is a competitor in AtCoder, whose highest rating is X.

In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

From 400 through 599: 8-kyu

From 600 through 799: 7-kyu

From 800 through 999: 6-kyu

From 1000 through 1199: 5-kyu

From 1200 through 1399: 4-kyu

From 1400 through 1599: 3-kyu

From 1600 through 1799: 2-kyu

From 1800 through 1999: 1-kyu

What kyu does M-kun have?

Constraints

400 \leq X \leq 1999

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the kyu M-kun has, as an integer.
For example, if he has 8-kyu, print 8.

Sample Input 1

725

Sample Output 1

7

M-kun's highest rating is 725, which corresponds to 7-kyu.

Thus, 7 is the correct output.

Sample Input 2

1600

Sample Output 2

2

M-kun's highest rating is 1600, which corresponds to 2-kyu.

Thus, 2 is the correct output.


a = io.read("*n")
b = 0
if a < 600 then
  b = 8
elseif a < 800 then
  b = 7
elseif a < 1000 then
  b = 6
elseif a < 1200 then
  b = 5
elseif a < 1400 then
  b = 4
elseif a < 1600 then
  b = 3
elseif a < 1800 then
  b = 2
else
  b = 1
end
print(b)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimensi0n, default_val) assert(type(default_val) ~= 'table') local n=dimensi0n local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
--
local R, G, B = read.nnn()
local K = read.n()
while K > 0 do
    if G <= R then
        G = G * 2
    elseif B <= G then
        B = B * 2
    end
    K = K - 1
end
if G > R and B > G then
    print("Yes")
else
    print("No")
end

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


a, b, c = io.read("*n", "*n", "*n")
k = io.read("*n")
d = 0
while b <= a do
  b = b * 2
  d = d + 1
end
while c <= b do
  c = c * 2
  d = d + 1
end
print(d <= k and "Yes" or "No")


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

local money = 1000
local kabu = 0

for i = 1, n do
  local needbuy = false
  local needsell = false
  for j = i + 1, n do
    if a[i] < a[j] then
      needbuy = true
    end
  end
  for j = i + 1, n do
    if a[i] > a[j] then
      needsell = true
      for k = i + 1, j - 1 do
        if a[i] < a[k] then
          needsell = false
          break
        end
      end
      break
    end
  end
  if i == n then needsell = true end
  if needsell then
    money = money + kabu * a[i]
    kabu = 0
  elseif needbuy then
    kabu = kabu + money // a[i]
    money = money % a[i]
  end
  -- print(i, money, kabu)
end
print(money)

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


local mmi, mma = math.min, math.max
local mab = math.abs
local mfl, mce = math.floor, math.ceil
local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor

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

local function grayCode(x)
  return bxor(x, brs(x, 1))
end

local function findright(idxes, ofst)
  for i = ofst, #idxes do
    if flag[idxes[i]] then
      return i
    end
  end
  return false
end

local function getmincosts(ik, pos, val, idx_pos, idx_val)
  local retary = {}
  for i = 1, ik + 1 do retary[i] = 0 end
  for i = 1, #pos do flag[i] = false end
  for linecnt = 0, ik do
    if #pos <= linecnt then
      break
    end
    if 0 < linecnt then
      flag[idx_val[linecnt]] = true
    end
    local retval = 0
    local leftpos = false
    local rightpos = findright(idx_pos, 1)
    for i = 1, #idx_pos do
      local idx_i = idx_pos[i]
      if flag[idx_i] then
        leftpos = rightpos
        if rightpos then
          rightpos = findright(idx_pos, rightpos + 1)
        end
      else
        local cand = mab(pos[idx_i])
        if leftpos then
          cand = mmi(cand, mab(pos[idx_i] - pos[idx_pos[leftpos]]))
        end
        if rightpos then
          cand = mmi(cand, mab(pos[idx_i] - pos[idx_pos[rightpos]]))
        end
        retval = retval + cand * val[idx_i]
      end
    end
    retary[linecnt + 1] = retval
  end
  return retary
end

local retik = {}

local g_ik = 0
local xs, ys = {}, {}
local function add_func(idx)
  local xv = x[idx]
  local yv = y[idx]
  xs[xv] = xs[xv] - p[idx]
  if xs[xv] == 0 then xs[xv] = nil end
  if not ys[yv] then
    ys[yv] = p[idx]
  else
    ys[yv] = ys[yv] + p[idx]
  end
end
local function rm_func(idx)
  local xv = x[idx]
  local yv = y[idx]
  ys[yv] = ys[yv] - p[idx]
  if ys[yv] == 0 then ys[yv] = nil end
  if not xs[xv] then
    xs[xv] = p[idx]
  else
    xs[xv] = xs[xv] + p[idx]
  end
end
local function work_func()
  local cand = 0
  local xpos, xval, xidx_pos, xidx_val = {}, {}, {}, {}
  for k, v in pairs(xs) do
    table.insert(xpos, k)
    table.insert(xval, v)
  end
  for i = 1, #xpos do xidx_pos[i], xidx_val[i] = i, i end
  table.sort(xidx_pos, function(a, b) return xpos[a] < xpos[b] end)
  table.sort(xidx_val, function(a, b) return xval[a] > xval[b] end)

  local ypos, yval, yidx_pos, yidx_val = {}, {}, {}, {}
  for k, v in pairs(ys) do
    table.insert(ypos, k)
    table.insert(yval, v)
  end
  for i = 1, #ypos do yidx_pos[i], yidx_val[i] = i, i end
  table.sort(yidx_pos, function(a, b) return ypos[a] < ypos[b] end)
  table.sort(yidx_val, function(a, b) return yval[a] > yval[b] end)

  local xcosts = getmincosts(n - 1, xpos, xval, xidx_pos, xidx_val)
  local ycosts = getmincosts(n - 1, ypos, yval, yidx_pos, yidx_val)
  for ik = 0, n - 1 do
    cand = xcosts[0 + 1] + ycosts[ik + 1]
    for i = 1, ik do
      cand = mmi(cand, xcosts[i + 1] + ycosts[ik - i + 1])
    end
    if not retik[ik + 1] then
      retik[ik + 1] = cand
    elseif cand < retik[ik + 1] then
      retik[ik + 1] = cand
    end
  end
end

local function grayWalk(size, add_func, rm_func, work_func)
  local prv = 0
  local total = bls(1, size) - 1
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  work_func()
  for i = 1, total do
    local v = grayCode(i)
    if prv < v then
      prv, v = v, v - prv
      add_func(bpos[v])
    else
      prv, v = v, prv - v
      rm_func(bpos[v])
    end
    work_func()
  end
end

for j = 1, n do
  local xj = x[j]
  if not xs[xj] then
    xs[xj] = p[j]
  else
    xs[xj] = xs[xj] + p[j]
  end
end
for ik = 0, n do
  retik[ik + 1] = false
end

grayWalk(n, add_func, rm_func, work_func)
retik[n + 1] = 0
print(table.concat(retik, "\n"))
-- print(os.clock())

Score: 500 points

Problem Statement

New AtCoder City has an infinite grid of streets, as follows:

At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point.

A straight street, which we will call East-West Main Street, runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane.

There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane.

A straight street, which we will call North-South Main Street, runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane.

There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane.

There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas.

The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.

M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets.

Let the walking distance of each citizen be the distance from his/her residential area to the nearest railroad.

M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized.

For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?

Constraints

1 \leq N \leq 15

-10 \ 000 \leq X_i \leq 10 \ 000

-10 \ 000 \leq Y_i \leq 10 \ 000

1 \leq P_i \leq 1 \ 000 \ 000

The locations of the N residential areas, (X_i, Y_i), are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 P_1
X_2 Y_2 P_2
: : :
X_N Y_N P_N

Output

Print the answer in N+1 lines.

The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1.

Sample Input 1

3
1 2 300
3 3 600
1 4 800

Sample Output 1

2900
900
0
0

When K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1, 3, and 1, respectively, to reach a railroad.

Thus, the sum of the walking distances of all citizens, S, is 1 \times 300 + 3 \times 600 + 1 \times 800 = 2900.

When K = 1, if we build a railroad along the street corresponding to the line y = 4 in the coordinate plane, the walking distances of the citizens of Area 1, 2, and 3 become 1, 1, and 0, respectively.

Then, S = 1 \times 300 + 1 \times 600 + 0 \times 800 = 900.

We have many other options for where we build the railroad, but none of them makes S less than 900.

When K = 2, if we build a railroad along the street corresponding to the lines x = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad with the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.

The figure below shows the optimal way to build railroads for the cases K = 0, 1, 2.

The street painted blue represents the roads along which we build railroads.

Sample Input 2

5
3 5 400
5 3 700
5 5 1000
5 7 700
7 5 400

Sample Output 2

13800
1600
0
0
0
0

The figure below shows the optimal way to build railroads for the cases K = 1, 2.

Sample Input 3

6
2 5 1000
5 2 1100
5 5 1700
-2 -5 900
-5 -2 600
-5 -5 2200

Sample Output 3

26700
13900
3200
1200
0
0
0

The figure below shows the optimal way to build railroads for the case K = 3.

Sample Input 4

8
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903

Sample Output 4

2576709
1569381
868031
605676
366338
141903
0
0
0

The figure below shows the optimal way to build railroads for the case K = 4.


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimensi0n, default_val) assert(type(default_val) ~= 'table') local n=dimensi0n local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
--
local DBG = false
local insert = table.insert
local function getkeys(t)
    local keys = {}
    for k,v in pairs(t) do
        insert(keys, k)
    end
    table.sort(keys)
    return keys
end

local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
--
local N = read.nl()
local X, Y, U = {},{},{}
local yy = {}
local xx = {}
local rr = {} -- right down
local ll = {} -- left down
local function push(tt, p, e)
    if not tt[p] then
        tt[p] = {}
    end
    insert(tt[p], e)
end
for i=1,N do
    X[i], Y[i], U[i] = read.nnl()
    U[i] = U[i]:sub(2,2)
    local e = i
    push(yy, Y[i], e)
    push(xx, X[i], e)
    local rrp = X[i] + Y[i]
    push(rr, rrp, e)
    local llp = X[i] - Y[i]
    push(ll, llp, e)
end
--richtraceback()
local INF = 10^18
local nearest_crash = INF
local function f1(name, tt, inv, fw, bk, sp)
    local ttk = getkeys(tt)
    for _,w in ipairs(ttk) do
        local t = tt[w]
        dbgpr("on same " .. name , w)
        local inttk = {}
        for _,i in ipairs(t) do
            if U[i] == fw or U[i] == bk then
                insert(inttk, {inv[i], i, U[i]})
            end
        end
        table.sort(inttk, function (a1, a2)
            return a1[1] < a2[1]
        end)
        local state = 0
        local recentfwinv = -200000
        for _,ee in ipairs(inttk) do
            local i = ee[2]
            if U[i] == fw or U[i] == bk then
                dbgpr("plane #" .. i, "inv="..inv[i], "X="..X[i], "Y="..Y[i], U[i])
                if state == 0 then
                    if U[i] == fw then
                        state = 1
                        recentfwinv = inv[i]
                    end
                elseif state == 1 then
                    if U[i] == fw then
                        recentfwinv = inv[i]
                    elseif U[i] == bk then
                        local d = inv[i] - recentfwinv
                        nearest_crash = math.min(nearest_crash, d * sp)
                        dbgpr("crash!", i, nearest_crash)
                    end
                end
            end
        end
    end

end
f1('Y', yy, X, 'R', 'L', 5)
f1('X', xx, Y, 'U', 'D', 5)
f1('rr', rr, X, 'R', 'U', 10)
f1('rr', rr, X, 'D', 'L', 10)
f1('ll', ll, X, 'U', 'L', 10)
f1('ll', ll, X, 'R', 'D', 10)

if nearest_crash == INF then
    print("SAFE")
else
    print(nearest_crash)
end

Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100


-- AIsing Programming Contest 2020
l, r, d = io.read("*n", "*n", "*n")
k = l / d
if (k * d) < l then k = k + 1 end
print(k)

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


n = io.read("*n")
cnt = 0
for i = 1, n do
  x = io.read("*n")
  if (i % 2 == 1) and (x % 2 == 1) then
    cnt = cnt + 1
  end
end
print(cnt)

Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

The assigned number, i, is odd.

The written integer is odd.

Constraints

All values in input are integers.

1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.

Sample Input 1

5
1 3 4 5 7

Sample Output 1

2

Two squares, Square 1 and 5, satisfy both of the conditions.

For Square 2 and 4, the assigned numbers are not odd.

For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3


n = io.read("*n")
for k = 1, n do
  cnt = 0
  for x = 1, math.floor(math.sqrt(k)) do
    for y = 1, x do
      for z = 1, y do
        if (x * x + y * y + z * z + x * y + x * z + y * z) == k then
          if x == y and y == z then
            cnt = cnt + 1
          elseif (x == y and y ~= z) or (x == z and x ~=y) or(y == z and y ~= x) then
            cnt = cnt + 3
          elseif (x ~= y and x ~= z and y ~= z) then
            cnt = cnt + 6
          end
        end
      end
    end
  end
  print(cnt)
end

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


-- 答え参照
local function xyz(x, y, z)
	return x*x + y*y + z*z + x*y + y*z + z*x
end

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

local loop_num = math.ceil(math.sqrt(N)^3)

local out_s = {}
for i = 1, N do
	out_s[i] = 0
end

for x = 1, loop_num do
	for y = 1, loop_num do
		for z = 1, loop_num do
			local result = xyz(x, y, z)
			if result <= N then
				out_s[result] = out_s[result] + 1
			end
		end
	end
end

print(table.concat(out_s, "\n"))


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
-----------------------
local N = read.n()
local L = 100
for i=1,N do
    local cnt = 0
    for x=1,L do
        local a = x*x
        if a > i then
            break
        end
        for y=1,L do
            local b = a + y*y+x*y
            if b > i then
                break
            end
            for z=1,L do
                local c = b + z*z + y*z + z*x
                if c > i then
                    break
                end
                if c == i then
                    cnt = cnt + 1
                end
            end
        end
    end
    print(cnt)
end


local mfl, mce = math.floor, math.ceil
local n = io.read("*n", "*l")
local t = {}
local s = io.read()
local cnt1 = 0
for i = 1, n do
  t[i] = s:byte(i) == 49
  if t[i] then cnt1 = cnt1 + 1 end
end

local function firstdevide(p, dv)
  local currem = 0
  for i = 1, n do
    currem = currem * 2
    if i == p then
      if not t[i] then
        currem = currem + 1
      end
    else
      if t[i] then
        currem = currem + 1
      end
    end
    currem = currem % dv
  end
  return currem
end

local function getpopcnt(x)
  local c = 0
  while 0 < x do
    if x % 2 == 1 then c = c + 1 end
    x = mfl(x / 2)
  end
  return c
end

for iq = 1, n do
  local dv = t[iq] and cnt1 - 1 or cnt1 + 1
  if dv == 0 then
    print(0)
  else
    local rem = firstdevide(iq, dv)
    local cnt = 1
    while 0 < rem do
      dv = getpopcnt(rem)
      rem = rem % dv
      cnt = cnt + 1
    end
    print(cnt)
  end
end

Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n.
For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

\mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.

\mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary.
For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted.
Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

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

X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).

Sample Input 1

3
011

Sample Output 1

2
1
1

X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.

X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.

X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

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


local length = io.read()
local data = {AC = 0, WA = 0, TLE =0, RE = 0}

for i = 1, length do
    local key = io.read()
    if data[key] then
        data[key] = data[key] + 1
    end
end

print("AC x".." "..data["AC"])
print("WA x".." "..data["WA"])
print("TLE x".." "..data["TLE"])
print("RE x".." "..data["RE"])

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


local n=io.read("n","l")
local s={}
for i=1,n do
    local input=io.read()
    s[input]=(s[input] or 0)+1
end

print("AC x "..(s["AC"] or 0))
print("WA x "..(s["WA"] or 0))
print("TLE x "..(s["TLE"] or 0))
print("RE x "..(s["RE"] or 0))


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

local num = 0
for i=1,tonumber(a)*tonumber(b) do
	s = io.read()
	if s == "#" then
		num = num + 1
	end
end
if num <= tonumber(c) then
	print(0)
end
local n = 0
for i= c,num-1 do
	n = n+ i
end
print(n)

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


function rl()
   local r = {}
   for word in io.read():gmatch("%S+") do
      table.insert(r, tonumber(word))
   end
   return r
end

function ru()
   return unpack(rl())
end

function pu(x, ...)
   if x then
      io.write(x, ' ')
      return pu(...)
   else
      io.write('\n')
   end
end

H, W, K = ru()
c = {}
for i = 1, H do
   c[i] = {}
   for j = 1, W do
      c[i][j] = io.read(1)
   end
   io.read()
end

function line(i)
   if i > H then
      return col(1)
   else
      local save = {}
      for j = 1, W do
	 save[j] = c[i][j]
	 c[i][j] = 'x'
      end
      local r = line(i + 1)
      for j = 1, W do
	 c[i][j] = save[j]
      end
      return r + line(i + 1)
   end
end

function col(j)
   if j > W then
      local count = 0
      for i = 1, H do
	 for j = 1, W do
	    if c[i][j] == '#' then
	       count = count + 1
	    end
	 end
      end
      return count == K and 1 or 0
   else
      local save = {}
      for i = 1, H do
	 save[i] = c[i][j]
	 c[i][j] = 'x'
      end
      local r = col(j + 1)
      for i = 1, H do
	 c[i][j] = save[i]
      end
      return r + col(j + 1)
   end
end

pu(line(1))


local n=io.read("n")
local a={}
for i=1,n do
    a[i]=io.read("n")
end
table.sort(a)
local total=0
for i=1,n-1 do
    total=total+a[n-i//2]
end
print(total)

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6


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

local total=a[1]
for i=3,n do
    total=total+math.max(a[i],a[i-1])
end
print(total)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
-----------------------
local D = read.n()
local C = read.N(26)
local S = {}
for i=1,D do
    S[i] = read.N(26)
end
-----
-----
local last = array(1, 0)
for d=1,D do
    local amax = 0
    local maxi = -1
    for i=1,26 do
        local ans = S[d][i]
        for j=1,26 do
            if j ~= i then
                ans = ans - C[j] * (d - last[j])
            end
        end
        if ans > amax then
            amax = ans
            maxi = i
        end
    end
    last[maxi] = d
    print(maxi)
end

(Please read problem A first. The maximum score you can get by solving this problem B is 1, which will have almost no effect on your ranking.)

Beginner's Guide

Let's first write a program to calculate the score from a pair of input and output. You can know the total score by submitting your solution, or an official program to calculate a score is often provided for local evaluation as in this contest. Nevertheless, writing a score calculator by yourself is still useful to check your understanding of the problem specification. Moreover, the source code of the score calculator can often be reused for solving the problem or debugging your solution. So it is worthwhile to write a score calculator unless it is very complicated.

Problem Statement

You will be given a contest schedule for D days.
For each d=1,2,\ldots,D, calculate the satisfaction at the end of day d.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D

The constraints and generation methods for the input part are the same as those for Problem A.

For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints.

Output

Let v_d be the satisfaction at the end of day d.
Print D integers v_d to Standard Output in the following format:

v_1
v_2
\vdots
v_D

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13

Sample Output 1

18398
35037
51140
65837
79325

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

We can build a solution (schedule) for this problem in the order of day 1, day 2, and so on. And for every partial solution we have built, we can calculate the goodness (satisfaction) by using the above score calculator. So we can construct the following algorithm: for each d=1,2,\ldots,D, we select the contest type that maximizes the satisfaction at the end of day d. You may have already encountered this kind of "greedy algorithms" in algorithm contests such as ABC. Greedy algorithms can guarantee the optimality for several problems, but unfortunately, it doesn't ensure optimality for this problem. However, even if it does not ensure optimality, we can still obtain a reasonable solution in many cases. Let's go back to Problem A and implement the greedy algorithm by utilizing the score calculator you just implemented!

Greedy methods can be applied to a variety of problems, are easy to implement, and often run relatively fast compared to other methods. Greedy is often the most powerful method when we need to process huge inputs.
We can further improve the score by changing the greedy selection criteria (evaluation function), keeping multiple candidates instead of focusing on one best partial solution (beam search), or using the output of greedy algorithms as an initial solution of other methods.
For more information, please refer to the editorial that will be published after the contest.


local day = io.read("*n")
local c = {}
for i = 1, 26 do
  c[i] = io.read("*n")
end
local s = {}
for id = 1, day do
  s[id] = {}
  for i = 1, 26 do
    s[id][i] = io.read("*n")
  end
end

local scores = {}
local penalties = {}
for i = 1, 26 do
  scores[i] = 0
  penalties[i] = 0
end
local function solveAll(t)
  local lastday = {}
  for i = 1, 26 do lastday[i] = 0 end
  for id = 1, day do
    local pid = t[id]
    scores[pid] = scores[pid] + s[id][pid]
    lastday[pid] = id
    for i = 1, 26 do
      penalties[i] = penalties[i] + c[i] * (id - lastday[i])
    end
  end
end
local function solveDiff(t, iday, pto)
  local pfrom = t[iday]
  scores[pfrom] = scores[pfrom] - s[iday][pfrom]
  scores[pto] = scores[pto] + s[iday][pto]
  t[iday] = pto
  penalties[pfrom] = 0
  penalties[pto] = 0
  local ld = {}
  for i = 1, 26 do ld[i] = 0 end
  for id = 1, day do
    local p = t[id]
    ld[p] = id
    for ip = pfrom, pto, (pto - pfrom) do
      penalties[ip] = penalties[ip] + c[ip] * (id - ld[ip])
    end
  end
end

local function solveSum()
  local z = 0
  for i = 1, 26 do z = z + scores[i] - penalties[i] end
  return z
end

local t = {}
for id = 1, day do
  t[id] = io.read("*n")
  -- t[id] = 1 + (id % 26)
end
solveAll(t)
local score = solveSum()
local m = io.read("*n")
for i = 1, m do
  local d, q = io.read("*n", "*n")
  solveDiff(t, d, q)
  print(solveSum())
end
os.exit()

math.randomseed(1)
local iter = 1
local random = math.random
while os.clock() < 1.85 do
  iter = iter + 1
  local changeday = random(1, 365)
  local changeto = random(1, 25)
  local changefrom = t[changeday]
  if changefrom <= changeto then
    changeto = changeto + 1
  end
  solveDiff(t, changeday, changeto)
  local tmp = solveSum()
  if score < tmp then
    score = tmp
  else
    solveDiff(t, changeday, changefrom)
  end
end
-- print(iter)
print(table.concat(t, "\n"))

(Please read problem A first. The maximum score you can get by solving this problem C is 1, which will have almost no effect on your ranking.)

Beginner's Guide

"Local search" is a powerful method for finding a high-quality solution.
In this method, instead of constructing a solution from scratch, we try to find a better solution by slightly modifying the already found solution.
If the solution gets better, update it, and if it gets worse, restore it.
By repeating this process, the quality of the solution is gradually improved over time.
The pseudo-code is as follows.

solution = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
slightly modify the solution (randomly)
if the solution gets worse:
restore the solution

For example, in this problem, we can use the following modification: pick the date d and contest type q at random and change the type of contest to be held on day d to q.
The pseudo-code is as follows.

t[1..D] = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
pick d and q at random
old = t[d] # Remember the original value so that we can restore it later
t[d] = q
if the solution gets worse:
t[d] = old

The most important thing when using the local search method is the design of how to modify solutions.

If the amount of modification is too small, we will soon fall into a dead-end (local optimum) and, conversely, if the amount of modification is too large, the probability of finding an improving move becomes extremely small.

In order to increase the number of iterations, it is desirable to be able to quickly calculate the score after applying a modification.

In this problem C, we focus on the second point.
The score after the modification can, of course, be obtained by calculating the score from scratch.
However, by focusing on only the parts that have been modified, it may be possible to quickly compute the difference between the scores before and after the modification.
From another viewpoint, the impossibility of such a fast incremental calculation implies that a small modification to the solution affects a majority of the score calculation.
In such a case, we may need to redesign how to modify solutions, or there is a high possibility that the problem is not suitable for local search.
Let's implement fast incremental score computation.
It's time to demonstrate the skills of algorithms and data structures you have developed in ABC and ARC!

In this kind of contest, where the objective is to find a better solution instead of the optimal one, a bug in a program does not result in a wrong answer, which may delay the discovery of the bug.
For early detection of bugs, it is a good idea to unit test functions you implemented complicated routines.
For example, if you implement fast incremental score calculation, it is a good idea to test that the scores computed by the fast implementation match the scores computed from scratch, as we will do in this problem C.

Problem Statement

You will be given a contest schedule for D days and M queries of schedule modification.
In the i-th query, given integers d_i and q_i, change the type of contest to be held on day d_i to q_i, and then output the final satisfaction at the end of day D on the updated schedule.
Note that we do not revert each query. That is, the i-th query is applied to the new schedule obtained by the (i-1)-th query.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A and the queries.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
M
d_1 q_1
d_2 q_2
\vdots
d_M q_M

The constraints and generation methods for the input part are the same as those for Problem A.

For each d=1,\ldots,D, t_d is an integer generated independently and uniformly at random from {1,2,\ldots,26}.

The number of queries M is an integer satisfying 1\leq M\leq 10^5.

For each i=1,\ldots,M, d_i is an integer generated independently and uniformly at random from {1,2,\ldots,D}.

For each i=1,\ldots,26, q_i is an integer satisfying 1\leq q_i\leq 26 generated uniformly at random from the 25 values that differ from the type of contest on day d_i.

Output

Let v_i be the final satisfaction at the end of day D on the schedule after applying the i-th query.
Print M integers v_i to Standard Output in the following format:

v_1
v_2
\vdots
v_M

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19

Sample Output 1

72882
56634
38425
27930
42884

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

Let's go back to Problem A and implement the local search algorithm by utilizing the incremental score calculator you just implemented!
For this problem, the current modification "pick the date d and contest type q at random and change the type of contest to be held on day d to q" is actually not so good. By considering why it is not good, let's improve the modification operation.
One of the most powerful and widely used variant of the local search method is "Simulated Annealing (SA)", which makes it easier to reach a better solution by stochastically accepting worsening moves.
For more information about SA and other local search techniques, please refer to the editorial that will be published after the contest.


local day = io.read("*n")
local c = {}
for i = 1, 26 do
  c[i] = io.read("*n")
end
local s = {}
for id = 1, day do
  s[id] = {}
  for i = 1, 26 do
    s[id][i] = io.read("*n")
  end
end

local scores = {}
local penalties = {}
for i = 1, 26 do
  scores[i] = 0
  penalties[i] = 0
end
local function solveAll(t)
  local lastday = {}
  for i = 1, 26 do lastday[i] = 0 end
  for id = 1, day do
    local pid = t[id]
    scores[pid] = scores[pid] + s[id][pid]
    lastday[pid] = id
    for i = 1, 26 do
      penalties[i] = penalties[i] + c[i] * (id - lastday[i])
    end
  end
end
local function solveDiff(t, iday, pto)
  local pfrom = t[iday]
  scores[pfrom] = scores[pfrom] - s[iday][pfrom]
  scores[pto] = scores[pto] + s[iday][pto]
  t[iday] = pto
  penalties[pfrom] = 0
  penalties[pto] = 0
  local ld = {}
  for i = 1, 26 do ld[i] = 0 end
  for id = 1, day do
    local p = t[id]
    ld[p] = id
    for ip = pfrom, pto, (pto - pfrom) do
      penalties[ip] = penalties[ip] + c[ip] * (id - ld[ip])
    end
  end
end

local function solveSum()
  local z = 0
  for i = 1, 26 do z = z + scores[i] - penalties[i] end
  return z
end

local t = {}
for id = 1, day do
  t[id] = 1 + (id % 26)
end
solveAll(t)
local score = solveSum()
math.randomseed(1)
local iter = 1
local random = math.random
while os.clock() < 1.85 do
  iter = iter + 1
  local changeday = random(1, 365)
  local changeto = random(1, 25)
  local changefrom = t[changeday]
  if changefrom <= changeto then
    changeto = changeto + 1
  end
  solveDiff(t, changeday, changeto)
  local tmp = solveSum()
  if score < tmp then
    score = tmp
  else
    solveDiff(t, changeday, changefrom)
  end
end
-- print(iter)
print(table.concat(t, "\n"))


local s = io.read()
s = tonumber(s)

local function func(s)
	return s + s * s + s * s * s
end

print(func(s))

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


local s = io.read()
local t = io.read()

local ans = 0

for i = 1, string.len(s) do
    if string.sub(s, i, i) ~= string.sub(t, i, i) then
        ans = ans + 1
    end
end

print(ans)

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


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


s = io.read()
t = io.read()
n = #s
c = 0
for i = 1, n do
  if s:sub(i, i) ~= t:sub(i, i) then
    c = c + 1
  end
end
print(c)


local mod = 1000000007
local mfl = math.floor

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

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

local fact = {1}
local invs = {1}
local invfact = {1}

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

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

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

local n, m = io.read("*n", "*n")
if m < n then
  print(0) os.exit()
end
local p_mn = getPerm(m, n)
local matched = 0
for i = 1, n do
  local comb = getComb(n, i)
  local remPerm = getPerm(m - i, n - i)
  local z = bmul(comb, remPerm)
  if i % 2 == 1 then
    matched = badd(matched, z)
  else
    matched = badd(matched, mod - z)
  end
end
local not_matched = badd(p_mn, mod - matched)
print(bmul(p_mn, not_matched))

Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

A_i \neq B_i, for every i such that 1\leq i\leq N.

A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

1\leq N \leq M \leq 5\times10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.

Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484


local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local xor = 0
for i = 3, n do
  xor = xor ~ a[i]
end
local sum = a[1] + a[2]
if sum < xor then
  print(-1)
  os.exit()
end
local b_xor, b_sum = {}, {}
while 0 < sum do
  table.insert(b_sum, sum % 2)
  table.insert(b_xor, xor % 2)
  sum = mfl(sum / 2)
  xor = mfl(xor / 2)
end
local dig = #b_sum
local moveup = false
local state = {}
for i = 1, dig do state[i] = 0 end
-- state of bit of a[1] and a[2]
-- 1: [0, 0], 2: [1, 1], 3: [0, 1] or [1, 0]
for idig = dig, 1, -1 do
  if b_sum[idig] == 1 then
    if b_xor[idig] == 1 then
      if moveup then
        print(-1)
        os.exit()
      else
        state[idig] = 3
      end
    else
      if moveup then
        state[idig] = 2
      else
        state[idig] = 1
      end
      moveup = true
    end
  else-- sum = 0
    if b_xor[idig] == 1 then
      if moveup then
        state[idig] = 3
      else
        print(-1)
        os.exit()
      end
    else
      if moveup then
        state[idig] = 2
      else
        state[idig] = 1
      end
      moveup = false
    end
  end
end
if moveup then print(-1) os.exit() end
local alim = {}
do
  local tmp = a[1]
  for i = 1, dig do
    alim[i] = tmp % 2
    tmp = mfl(tmp / 2)
  end
end
local tbl = {}
for i = 1, dig do tbl[i] = 0 end

local function dfs(idig, limit)
  if idig == 0 then return true end
  if limit then
    if alim[idig] == 0 then
      if state[idig] == 2 then
        return false
      else
        tbl[idig] = 0
        return dfs(idig - 1, true)
      end
    else
      if state[idig] == 2 then
        tbl[idig] = 1
        return dfs(idig - 1, true)
      elseif state[idig] == 1 then
        tbl[idig] = 0
        return dfs(idig - 1, false)
      else
        tbl[idig] = 1
        local ret = dfs(idig - 1, true)
        if ret then
          return true
        else
          tbl[idig] = 0
          return dfs(idig - 1, false)
        end
      end
    end
  else
    if state[idig] == 1 then
      tbl[idig] = 0
      return dfs(idig - 1, false)
    else
      tbl[idig] = 1
      return dfs(idig - 1, false)
    end
  end
end

local ret = dfs(dig, true)
if not ret then
  print(-1)
  os.exit()
end
local ans = 0
local mul = 1
for i = 1, dig do
  ans = ans + mul * tbl[i]
  mul = mul * 2
end
if ans == 0 then
  print(-1)
else
  print(a[1] - ans)
end

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


local s=io.read():byte(1)
if 97<=s then
    print("a")
else
    print("A")
end

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


n = io.read("*n")
local rep = 1
while true do
  local mul = 1
  for i = 1, rep do
    mul = mul * 26
  end
  if n <= mul then
    local z = n - 1
    local s = {}
    for i = 1, rep do
      s[i] = string.char(z % 26 + 97)
      z = z // 26
    end
    print(table.concat(s):reverse())
    break
  else
    n = n - mul
    rep = rep + 1
  end
end

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


local N = io.read("*n")
local abc = "abcdefghijklmnopqrstuvwxyz"

local N_to_0_num = N
local str = ""
while N_to_0_num > 0 do
	local index = N_to_0_num%26
	str = str..abc:sub(index, index)
	N_to_0_num = N_to_0_num // 26
end

print(str:reverse())


N=io.read"*n"
t={}
for i=1,N do
	n=io.read"*n"
	t[n]=(t[n]or 0)+1
end
Q=io.read"*n"

for i=1,Q do
	B=io.read"*n"
	C=io.read"*n"

	t[C]=(t[C]or 0)+(t[B]or 0)
	t[B]=nil

	s=0
	for k,v in pairs(t)do
		s=s+k*v
	end
	print(s)

end

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

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

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


N=io.read"*n"
t={}
for i=1,N do
	n=io.read"*n"
	t[n]=(t[n]or 0)+1
end
Q=io.read"*n"

for i=1,Q do
	B=io.read"*n"
	C=io.read"*n"

	t[C]=(t[C]or 0)+(t[B]or 0)

	t[B]=0

	s=0
	for k,v in pairs(t)do
		s=s+k*v
	end
	print(s)

end


local n = io.read("*n")
local t = {}
local z = 0
for i = 1, n do
  t[i] = io.read("*n")
  z = z ~ t[i]
end
local r = {}
for i = 1, n do
  r[i] = z ~ t[i]
end
print(table.concat(r, " "))

Score : 500 points

Problem Statement

There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even.

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

Recently, they learned the operation called xor (exclusive OR).

What is xor?

For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is defined as follows:

When x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3~\textrm{xor}~5 = 6.

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i.
Using this information, restore the integer written on the scarf of each Snuke Cat.

Constraints

All values in input are integers.

2 \leq N \leq 200000

N is even.

0 \leq a_i \leq 10^9

There exists a combination of integers on the scarfs that is consistent with the given information.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print a line containing N integers separated with space.

The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.

If there are multiple possible solutions, you may print any of them.

Sample Input 1

4
20 11 9 24

Sample Output 1

26 5 7 22

5~\textrm{xor}~7~\textrm{xor}~22 = 20

26~\textrm{xor}~7~\textrm{xor}~22 = 11

26~\textrm{xor}~5~\textrm{xor}~22 = 9

26~\textrm{xor}~5~\textrm{xor}~7 = 24

Thus, this output is consistent with the given information.


local xor = bit.bxor
local n = io.read("*n")
local t = {}
local z = 0
for i = 1, n do
  t[i] = io.read("*n")
  z = xor(z, t[i])
end
local r = {}
for i = 1, n do
  r[i] = xor(z, t[i])
end
print(table.concat(r, " "))


local bit=require"bit"
local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
a=reada(N)
s=0
for i=1,N do
	s=bit.bxor(a[i],s)
end
for i=1,N do
	print(bit.bxor(a[i],s))
end


local mod = 1000000007
local mfl = math.floor

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

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

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

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

local pow25 = {1}
local pow26 = {1}
for i = 2, 1000010 do
  pow25[i] = bmul(pow25[i - 1], 25)
  pow26[i] = bmul(pow26[i - 1], 26)
end

local k = io.read("*n", "*l")
local s = io.read()
local n = #s
local ret = 0
for j = 0, k do
  local z = getCombQ(n + j - 1, n - 1)
  z = bmul(z, pow25[j + 1])
  local rem = k - j
  z = bmul(z, pow26[rem + 1])
  ret = badd(ret, z)
end
print(ret)

Score: 600 points

Problem Statement

How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"?

The answer can be enormous, so print it modulo (10^9+7).

Constraints

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

S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

K
S

Output

Print the number of strings satisfying the condition, modulo (10^9+7).

Sample Input 1

5
oof

Sample Output 1

575111451

For example, we can obtain proofend, moonwolf, and onionpuf, while we cannot obtain oofsix, oofelevennn, voxafolt, or fooooooo.

Sample Input 2

37564
whydidyoudesertme

Sample Output 2

318008117


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

for i = 1, 5 do
	if x[i] == 0 then
		print(i)
	end
end

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


X=io.read"*n"
Y=io.read"*n"
if(Y%2==1)then print"No"return end
print(X*2<=Y and Y<=X*4 and "Yes" or "No")

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


local read = io.read
local insert = table.insert
local abs = math.abs

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

local not_p = {}
for i = 0, 101 do
	local can_ins = true
	for j = 1, N do
		if p[j] == i then
			can_ins = false
			break
		end
	end
	if can_ins then
		insert(not_p, i)
	end
end

local out = math.huge
for i = 1, #not_p do
	if abs(not_p[i] - X) < abs(out - X) then
		out = not_p[i]
	end
end

print(out)

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


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

local h, w, k = io.read("*n", "*n", "*n")
local inf = h * w * k + 1
local x1, y1 = io.read("*n", "*n")
local sidx = (x1 - 1) * w + y1
local x2, y2 = io.read("*n", "*n", "*l")
local tidx = (x2 - 1) * w + y2
local len = {}
local asked = {}
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    local idx = (i - 1) * w + j
    local f = s:sub(j, j) ~= "."
    for k = -1, 0 do
      len[idx * 2 + k] = inf
      asked[idx * 2 + k] = f
    end
  end
end
len[sidx * 2 - 1] = 0
len[sidx * 2] = 0

local n = h * w * 2
asked[n + 1] = true

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

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

local function walk(src, dst, cost)
  if not asked[dst] and len[src] + cost < len[dst] then
    len[dst] = len[src] + cost
    st:update(dst)
  end
end

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  asked[src] = true
  st:update(src)
  local idx = mce(src / 2)

  local l = len[src]
  local z = mce(l / k) * k - l
  if src % 2 == 1 then
    -- to left-right
    if 1 < w then
      if idx % w ~= 1 then walk(src, src - 2, 1) end
      if idx % w ~= 0 then walk(src, src + 2, 1) end
    end
    walk(src, src + 1, z)
  else
    -- to up-down
    if w < idx then
      walk(src, src - 2 * w, 1)
    end
    if idx <= (h - 1) * w then
      walk(src, src + 2 * w, 1)
    end
    walk(src, src - 1, z)
  end
end

local ret = mmi(len[tidx * 2 - 1], len[tidx * 2])
if inf <= ret then
  print(-1)
else
  local l = mce(ret / k)
  print(l)
end
-- print(os.clock())

Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1


Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


A=io.read"*n"
V=io.read"*n"
B=io.read"*n"
W=io.read"*n"
T=io.read"*n"
t=math.abs(A-B)/(V-W)
print((V~=W and 0<=t and t<=T)and"YES"or"NO")

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


A=io.read"*n"
V=io.read"*n"
B=io.read"*n"
W=io.read"*n"
T=io.read"*n"
t=math.abs(A-B)/(V-W)
print((V~=W and 0<=t and t<=T)and"Yes"or"No")


local a,v=io.read("n","n")
local b,w=io.read("n","n")
local t=io.read("n")

if a<b then
    if a+v*t>=b+w*t then
        print("YES")
    else
        print("NO")
    end
else
    if a+v*t<=b+w*t then
        print("YES")
    else
        print("NO")
    end
end