local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local bxor = bit.bxor

local function grayCode(x)
  return bxor(x, brs(x, 1))
end
local gw_way = {}
local gw_idx = {}
do
  local size = 11
  local prv = 0
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  local total = bls(1, size) - 1
  for i = 1, total do
    local v = grayCode(i)
    if prv < v then
      gw_way[i] = true
      gw_idx[i] = bpos[v - prv]
    else
      gw_way[i] = false
      gw_idx[i] = bpos[prv - v]
    end
    prv = v
  end
end

-- add_func(idx), rm_func(idx), work_func()
local function grayWalk(size, add_func, rm_func, work_func)
  local total = bls(1, size) - 1
  for i = 1, total do
    if gw_way[i] then
      add_func(gw_idx[i])
    else
      rm_func(gw_idx[i])
    end
    work_func()
  end
end

local n = io.read("*n")
local v, w = {}, {}
for i = 1, n do
  v[i], w[i] = io.read("*n", "*n")
end
local head_tbl = {}
local box = {}
local ten5 = 100000

local tgt = 0
local weight, val = 0, 0
local tbl = nil
local function add_func(idx)
  weight = weight + w[brs(tgt, idx - 1)]
  val = val + v[brs(tgt, idx - 1)]
end
local function rm_func(idx)
  weight = weight - w[brs(tgt, idx - 1)]
  val = val - v[brs(tgt, idx - 1)]
end
local function work_func()
  if weight <= ten5 then
    tbl[weight] = mma(tbl[weight], val)
  end
end

for i = 512, mmi(n, 1023) do
  tgt = i
  head_tbl[i - 511] = {}
  tbl = head_tbl[i - 511]
  for j = 1, ten5 do
    tbl[j] = 0
  end
  weight, val = 0, 0
  grayWalk(10, add_func, rm_func, work_func)
  for i = 2, ten5 do
    tbl[i] = mma(tbl[i], tbl[i - 1])
  end
end

local q = io.read("*n")
local ans, wlim = 0, 0
local function work_func2()
  if weight <= wlim then
    ans = mma(ans, val)
  end
end
local function work_func3()
  if weight <= wlim then
    ans = mma(ans, val)
  end
  if weight < wlim then
    ans = mma(ans, val + tbl[wlim - weight])
  end
end
for iq = 1, q do
  local pos, wlim_tmp = io.read("*n", "*n")
  wlim = wlim_tmp
  local deg = 0
  do
    local tmp = pos
    while 0 < tmp do
      deg, tmp = deg + 1, brs(tmp, 1)
    end
  end
  if pos < 512 then
    ans = 0
    weight, val = 0, 0
    tgt = pos
    grayWalk(deg, add_func, rm_func, work_func2)
    print(ans)
  elseif pos < 1024 then
    print(head_tbl[pos - 511][wlim])
  else
    deg = deg - 10
    tbl = head_tbl[brs(pos, deg) - 511]
    ans = tbl[wlim]
    weight, val = 0, 0
    tgt = pos
    grayWalk(deg, add_func, rm_func, work_func3)
    print(ans)
  end
end

Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

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

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253


local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local n = io.read("*n")
local v, w = {}, {}
for i = 1, n do
  v[i], w[i] = io.read("*n", "*n")
end
local head_tbl = {}
local box = {}
local ten5 = 100000
for i = 256, mmi(n, 1023) do
  head_tbl[i - 255] = {}
  local tbl = head_tbl[i - 255]
  for j = 1, ten5 do
    tbl[j] = 0
  end
  local jlim = i < 512 and 511 or 1023
  local klim, mul = 1, 2
  for j = 1, jlim do
    if j == mul then
      klim, mul = klim + 1, mul * 2
    end
    local tmpi, tmpj = i, j
    local weight, val = 0, 0
    for k = 1, klim do
      if tmpj % 2 == 1 then
        val = val + v[tmpi]
        weight = weight + w[tmpi]
      end
      tmpi = brs(tmpi, 1)
      tmpj = brs(tmpj, 1)
    end
    if weight <= ten5 then
      tbl[weight] = mma(tbl[weight], val)
    end
  end
  for i = 2, ten5 do
    tbl[i] = mma(tbl[i], tbl[i - 1])
  end
end
local function debug(...)
  -- print(...)
end
local function output(z)
  print(z)
end

debug(os.clock(), collectgarbage("count"))
local q = io.read("*n")
for iq = 1, q do
  if iq % 10000 == 0 then
    debug(iq, os.clock())
  end
  local pos, wlim = io.read("*n", "*n")
  local deg = 0
  do
    local tmp = pos
    while 0 < tmp do
      deg, tmp = deg + 1, brs(tmp, 1)
    end
  end
  if pos < 256 then
    local ans = 0
    local tot = bls(1, deg)
    local deglim, mul = 1, 2
    for j = 0, tot - 1 do
      if j == mul then
        deglim, mul = deglim + 1, mul * 2
      end
      local tmpi, tmpj = pos, j
      local val, weight = 0, 0
      for k = 1, deglim do
        if tmpj % 2 == 1 then
          val, weight = val + v[tmpi], weight + w[tmpi]
        end
        tmpi, tmpj = brs(tmpi, 1), brs(tmpj, 1)
      end
      if weight <= wlim then
        ans = mma(ans, val)
      end
    end
    output(ans)
  elseif pos < 1024 then
    output(head_tbl[pos - 255][wlim])
  else
    deg = deg - 10
    local basepos = pos
    for i = 1, deg do
      basepos = brs(basepos, 1)
    end
    local tbl = head_tbl[basepos - 255]
    local ans = tbl[wlim]
    local tot = bls(1, deg)
    local deglim, mul = 1, 2
    for j = 1, tot - 1 do
      local tmpi, tmpj = pos, j
      local val, weight = 0, 0
      if j == mul then
        deglim, mul = deglim + 1, mul * 2
      end
      for k = 1, deglim do
        if tmpj % 2 == 1 then
          val, weight = val + v[tmpi], weight + w[tmpi]
        end
        tmpi, tmpj = brs(tmpi, 1), brs(tmpj, 1)
      end
      if weight == wlim then
        ans = mma(ans, val)
      elseif weight < wlim then
        ans = mma(ans, val + tbl[wlim - weight])
      end
    end
    output(ans)
  end
end
debug(os.clock())

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

Submitted source-code file size, in bytes.

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

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


local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local n = io.read("*n")
local v, w = {}, {}
for i = 1, n do
  v[i], w[i] = io.read("*n", "*n")
end
local head_tbl = {}
local box = {}
local ten5 = 100000
for i = 256, mmi(n, 1023) do
  head_tbl[i - 255] = {}
  local tbl = head_tbl[i - 255]
  for j = 1, ten5 do
    tbl[j] = 0
  end
  for j = 1, 1023 do
    local tmpi, tmpj = i, j
    local weight, val = 0, 0
    for k = 1, 10 do
      if tmpj % 2 == 1 then
        val = val + v[tmpi]
        weight = weight + w[tmpi]
      end
      tmpi = brs(tmpi, 1)
      tmpj = brs(tmpj, 1)
      if tmpi == 0 then break end
    end
    if 0< weight and weight <= ten5 then
      tbl[weight] = mma(tbl[weight], val)
    end
  end
  for i = 2, ten5 do
    tbl[i] = mma(tbl[i], tbl[i - 1])
  end
end
local function debug(...)
  -- print(...)
end
local function output(z)
  print(z)
end

debug(os.clock(), collectgarbage("count"))
local q = io.read("*n")
for iq = 1, q do
  if iq % 1000 == 0 then
    debug(iq, os.clock())
  end
  local pos, wlim = io.read("*n", "*n")
  local deg = 0
  do
    local tmp = pos
    while 0 < tmp do
      deg, tmp = deg + 1, brs(tmp, 1)
    end
  end
  if pos < 256 then
    local ans = 0
    local tot = bls(1, deg)
    for j = 0, tot - 1 do
      local tmpi, tmpj = pos, j
      local val, weight = 0, 0
      for k = 1, deg do
        if tmpj % 2 == 1 then
          val, weight = val + v[tmpi], weight + w[tmpi]
        end
        tmpi, tmpj = brs(tmpi, 1), brs(tmpj, 1)
      end
      if weight <= wlim then
        ans = mma(ans, val)
      end
    end
    output(ans)
  elseif pos < 1024 then
    output(head_tbl[pos - 255][wlim])
  else
    deg = deg - 10
    local basepos = pos
    for i = 1, deg do
      basepos = brs(basepos, 1)
    end
    local tbl = head_tbl[basepos - 255]
    local ans = tbl[wlim]
    local tot = bls(1, deg)
    for j = 1, tot - 1 do
      local tmpi, tmpj = pos, j
      local val, weight = 0, 0
      for k = 1, deg do
        if tmpj % 2 == 1 then
          val, weight = val + v[tmpi], weight + w[tmpi]
        end
        tmpi, tmpj = brs(tmpi, 1), brs(tmpj, 1)
      end
      if weight <= wlim then
        ans = mma(ans, val)
      end
      if weight < wlim then
        ans = mma(ans, val + tbl[wlim - weight])
      end
    end
    output(ans)
  end
end
debug(os.clock())


local A, B = io.read("*n", "*n")

print(A * B)

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


local n=io.read("n")

local p={}
for i=2,math.sqrt(n) do
    if n%i==0 then
        local counter=0
        while n%i==0 do
            n=n//i
            counter=counter+1
        end
        table.insert(p, counter)
    end
end
if n>1 then
    table.insert(p,1)
end

local counter=0
for i=1,#p do
    local j=1
    while j<=p[i] do
        p[i]=p[i]-j
        j=j+1
        counter=counter+1
    end
end
print(counter)

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


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 N = read.n()
local even = N % 2 == 0
local T = {}
for i=1,N do
    local a, b =read.nn()
    T[i] = {a, b}
end
local medmin = 10^10
local mins = {}
for i=1,N do
    local a, b = T[i][1], T[i][2]
    mins[i] = a
end
if even then
    medmin = mins[N//2] + mins[1+N//2]
else
    medmin = mins[(N+1)//2]
end
local medmax = -1
local maxs = {}
for i=1,N do
    local a, b = T[i][1], T[i][2]
    maxs[i] = b
end
if even then
    medmax = maxs[N//2] + maxs[1+N//2]
else
    medmax = maxs[(N+1)//2]
end

print(medmax-medmin+1)

Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

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

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991


local mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local pow2 = {1}
for i = 2, 100000 do
  pow2[i] = (2 * pow2[i - 1]) % mod
end

local n, s = io.read("*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local dp = {}
for i = 1, s + 1 do
  dp[i] = 0
end
dp[1] = 1
for i = 1, n do
  local v = a[i]
  for j = s + 1, s + 2 - v, -1 do
    dp[j] = bmul(dp[j], 2)
  end
  for j = s + 1 - v, 1, -1 do
    dp[j + v] = badd(dp[j + v], dp[j])
    dp[j] = bmul(dp[j], 2)
  end
end
print(dp[s + 1])

Score : 600 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N and another positive integer S.

For a non-empty subset T of the set \{1, 2, \ldots , N \}, let us define f(T) as follows:

f(T) is the number of different non-empty subsets \{x_1, x_2, \ldots , x_k \} of T such that A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(T) over all 2^N-1 subsets T of \{1, 2, \ldots , N \}. Since the sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(T) modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

6

For each T, the value of f(T) is shown below. The sum of these values is 6.

f(\{1\}) = 0

f(\{2\}) = 0

f(\{3\}) = 1 (One subset \{3\} satisfies the condition.)

f(\{1, 2\}) = 1 (\{1, 2\})

f(\{2, 3\}) = 1 (\{3\})

f(\{1, 3\}) = 1 (\{3\})

f(\{1, 2, 3\}) = 2 (\{1, 2\}, \{3\})

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

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

Sample Output 3

3296


local n=io.read("n")
if n%10==3 then
    print("bon")
elseif n%10==0 or n%10==1 or n%10==6 or n%10==8 then
    print("pon")
else
    print("hon")
end

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


local k=io.read("n","l")
local s=io.read()

if k>=#s then
    print(s)
else
    print(s:sub(1,k).."...")
end

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

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

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


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

u={[1]=0}
local function f(x)
	for i,v in ipairs(t[x])do
		if(u[v]==nil)then
			u[v]=i
			f(v)
		end
	end
end
f(1)
for i=2,N do
	if(u[i]==nil)then
		print"No"
		return
	end
end
print"Yes"
for i=2,N do
	print(u[i])
end

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

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

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

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

Sample Output 2

Yes
6
5
5
1
1

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


local function gcd(a,b)
    if b>0 then
        return gcd(b,a%b)
    else
        return a
    end
end

local mod=1000000007
local p={1}
for i=1,200010 do
    p[i+1]=p[i]*2%mod
end

local n=io.read("n")
local iwashi={}
local zero=0
for i=1,n do
    local a,b=io.read("n","n")
    if a==0 and b==0 then
        zero=zero+1
    elseif a==0 then
        iwashi[0]=(iwashi[0] or {})
        iwashi[0][0]=(iwashi[0][0] or {})
        iwashi[0][0][1]=(iwashi[0][0][1] or 0)+1
    elseif b==0 then
        iwashi[0]=(iwashi[0] or {})
        iwashi[0][0]=(iwashi[0][0] or {})
        iwashi[0][0][2]=(iwashi[0][0][2] or 0)+1
    else
        if b<0 then
            a,b=-a,-b
        end
        local gcdab=gcd(math.abs(a),math.abs(b))
        a=a//gcdab
        b=b//gcdab
        if a<0 then
            iwashi[b]=(iwashi[b] or {})
            iwashi[b][-a]=(iwashi[b][-a] or {})
            iwashi[b][-a][1]=(iwashi[b][-a][1] or 0)+1
        else
            iwashi[a]=(iwashi[a] or {})
            iwashi[a][b]=(iwashi[a][b] or {})
            iwashi[a][b][2]=(iwashi[a][b][2] or 0)+1
        end
    end
end

local combination=1
for a,_ in pairs(iwashi) do
    for b,_ in pairs(iwashi[a]) do
        local ba=iwashi[a][b][1] or 0
        local ab=iwashi[a][b][2] or 0
        local select=p[ba+1]+p[ab+1]-1
        combination=(combination*select)%mod
    end
end
combination=(combination+zero-1)%mod
print(combination)

Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


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 function bsub(x, y)
  return x < y and x - y + mod or x - y
end

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

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

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

local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local x1, y1 = {}, {}
local x2, y2 = {}, {}
local xzero = 0
local yzero = 0

local n = io.read("*n", "*l")
local a, b = {}, {}
for i = 1, n do
  local s = io.read()
  local ai, bi = s:match("(%-?%d+) (%-?%d+)")
  a[i] = lltonumber(ai)
  b[i] = lltonumber(bi)
  if a[i] == 0LL and b[i] == 0LL then
    -- no use
  elseif a[i] == 0LL then
    xzero = xzero + 1
  elseif b[i] == 0LL then
    yzero = yzero + 1
  else
    if b[i] < 0LL then
      a[i], b[i] = -a[i], -b[i]
    end
    if 0LL < a[i] then
      local xi = a[i]
      local yi = b[i]
      local gcd = getgcd(xi, yi)
      table.insert(x1, xi / gcd)
      table.insert(y1, yi / gcd)
    else
      local xi = -a[i]
      local yi = b[i]
      local gcd = getgcd(xi, yi)
      table.insert(x2, xi / gcd)
      table.insert(y2, yi / gcd)
    end
  end
end
-- print(os.clock())
-- local idx1 = {}
-- for i = 1, #x1 do
--   idx1[i] = i
-- end
-- local idx2 = {}
-- for i = 1, #x2 do
--   idx2[i] = i
-- end
-- table.sort(idx1, function(aa, bb)
--   if x1[aa] ~= x1[bb] then
--     return x1[aa] < x1[bb]
--   else
--     return y1[aa] < y1[bb]
--   end
-- end)
--
-- table.sort(idx2, function(aa, bb)
--   if y2[aa] ~= y2[bb] then
--     return y2[aa] < y2[bb]
--   else
--     return x2[aa] < x2[bb]
--   end
-- end)

local pow2 = {1}
for i = 1, 200010 do
  pow2[i + 1] = (pow2[i] * 2) % mod
end

local ret = badd(pow2[xzero + 1], pow2[yzero + 1])
ret = bsub(ret, 1)

local m1 = {}
for i1 = 1, #x1 do
  local xs, ys = tostring(x1[i1]), tostring(y1[i1])
  if not m1[xs] then m1[xs] = {} end
  if not m1[xs][ys] then
    m1[xs][ys] = 1
  else
    m1[xs][ys] = m1[xs][ys] + 1
  end
end

local m2 = {}
for i2 = 1, #x2 do
  local xs, ys = tostring(x2[i2]), tostring(y2[i2])
  if not m2[ys] then m2[ys] = {} end
  if not m2[ys][xs] then
    m2[ys][xs] = 1
  else
    m2[ys][xs] = m2[ys][xs] + 1
  end
end
for k1, v in pairs(m1) do
  if m2[k1] then
    local m2k1 = m2[k1]
    for k2, c1 in pairs(v) do
      local c2 = m2k1[k2]
      if c2 then
        local val = pow2[c1 + c2 + 1]
        ret = bmul(ret, bsub(badd(pow2[c1 + 1], pow2[c2 + 1]), 1))
        -- local rm = bmul(pow2[c1 + 1] - 1, pow2[c2 + 1] - 1)
        -- ret = bmul(ret, bsub(val, rm))
        m2k1[k2] = nil
      else
        ret = bmul(ret, pow2[c1 + 1])
      end
    end
  else
    for k2, c1 in pairs(v) do
      ret = bmul(ret, pow2[c1 + 1])
    end
  end
end
for k1, v in pairs(m2) do
  for k2, c in pairs(v) do
    ret = bmul(ret, pow2[c + 1])
  end
end

print(bsub(ret, 1))


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 function bsub(x, y)
  return x < y and x - y + mod or x - y
end

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

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

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

local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]

local function lltonumber(str)
  return C.atoll(str)
end

local n = io.read("*n", "*l")
local a, b = {}, {}
for i = 1, n do
  local s = io.read()
  local ai, bi = s:match("(%-?%d+) (%-?%d+)")
  a[i] = lltonumber(ai)
  b[i] = lltonumber(bi)
end

local x1, y1 = {}, {}
local x2, y2 = {}, {}
local xzero = 0
local yzero = 0

for i = 1, n do
  if b[i] < 0LL then
    a[i], b[i] = -a[i], -b[i]
  end
  if a[i] == 0LL and b[i] == 0LL then
  elseif a[i] == 0LL then
    xzero = xzero + 1
  elseif b[i] == 0LL then
    yzero = yzero + 1
  else
    if 0LL < a[i] then
      local xi = a[i]
      local yi = b[i]
      local gcd = getgcd(xi, yi)
      table.insert(x1, xi / gcd)
      table.insert(y1, yi / gcd)
    else
      local xi = -a[i]
      local yi = b[i]
      local gcd = getgcd(xi, yi)
      table.insert(x2, xi / gcd)
      table.insert(y2, yi / gcd)
    end
  end
end
-- local idx1 = {}
-- for i = 1, #x1 do
--   idx1[i] = i
-- end
-- local idx2 = {}
-- for i = 1, #x2 do
--   idx2[i] = i
-- end
-- table.sort(idx1, function(aa, bb)
--   if x1[aa] ~= x1[bb] then
--     return x1[aa] < x1[bb]
--   else
--     return y1[aa] < y1[bb]
--   end
-- end)
--
-- table.sort(idx2, function(aa, bb)
--   if y2[aa] ~= y2[bb] then
--     return y2[aa] < y2[bb]
--   else
--     return x2[aa] < x2[bb]
--   end
-- end)

local pow2 = {1}
for i = 1, 200010 do
  pow2[i + 1] = (pow2[i] * 2) % mod
end

local ret = badd(pow2[xzero + 1] - 1, pow2[yzero + 1] - 1)
ret = (ret + 1) % mod

local m1 = {}
for i1 = 1, #x1 do
  local xs, ys = tostring(x1[i1]), tostring(y1[i1])
  if not m1[xs] then m1[xs] = {} end
  if not m1[xs][ys] then
    m1[xs][ys] = 1
  else
    m1[xs][ys] = m1[xs][ys] + 1
  end
end

local m2 = {}
for i2 = 1, #x2 do
  local xs, ys = tostring(x2[i2]), tostring(y2[i2])
  if not m2[xs] then m2[xs] = {} end
  if not m2[xs][ys] then
    m2[xs][ys] = 1
  else
    m2[xs][ys] = m2[xs][ys] + 1
  end
end
for k1, v in pairs(m1) do
  for k2, c1 in pairs(v) do
    local c2 = nil
    if m2[k2] then c2 = m2[k2][k1] end
    if c2 then
      local val = pow2[c1 + c2 + 1]
      local rm = bmul(pow2[c1 + 1] - 1, pow2[c2 + 1] - 1)
      ret = bmul(ret, bsub(val, rm))
      m2[k2][k1] = nil
    else
      ret = bmul(ret, pow2[c1 + 1])
    end
  end
end
for k1, v in pairs(m2) do
  for k2, c in pairs(v) do
    ret = bmul(ret, pow2[c + 1])
  end
end

print(bsub(ret, 1))


local mmi, mma = math.min, math.max
local mfl, mce = math.floor, math.ceil
local TranSufA = {}
TranSufA.makeSufA = function(self)
  self.sufa = {}
  local n = #self.str
  local idx, tbl1, tbl2 = self.sufa, {}, {}
  local tmp_tbl = {}
  for i = 1, n do
    idx[i] = i
    tbl1[i] = self.str:sub(i, i):byte() - 95 -- "a" = 97
    tbl2[i] = 0
    tmp_tbl[i] = 0
  end
  idx[n + 1], tbl1[n + 1], tbl2[n + 1] = n + 1, 1, 0
  n = n + 1 -- add empty to last
  table.sort(idx, function(a, b) return tbl1[a] < tbl1[b] end)
  local step, stepflag = 1, true
  while step < n do
    local v = stepflag and tbl1 or tbl2
    local vd = stepflag and tbl2 or tbl1
    stepflag = not stepflag
    local stepdst = {}
    for i = 1, n do
      local dst = i + step
      if n < dst then dst = dst - n end
      stepdst[i] = dst
    end
    local left, major = 1, v[idx[1]]
    local minor_min = v[stepdst[idx[1]]]
    local minor_max = minor_min
    local minormap = {}
    local cur = 0
    for i = 1, n do
      local minor = v[stepdst[idx[i]]]
      if minormap[minor] then minormap[minor] = minormap[minor] + 1
      else minormap[minor] = 1
      end
      minor_min, minor_max = mmi(minor_min, minor), mma(minor_max, minor)
      local isend = i == n or major ~= v[idx[i + 1]]
      if isend then
        local right = i
        if left == right then
          cur = cur + 1
          vd[idx[left]] = cur
        elseif minor_min ~= minor_max then
          local minortbl = {}
          for k, _u in pairs(minormap) do table.insert(minortbl, k) end
          table.sort(minortbl)
          local offset = 0
          for i = 1, #minortbl do
            local cnt = minormap[minortbl[i]]
            minormap[minortbl[i]] = i
            minortbl[i] = offset -- reuse
            offset = offset + cnt
          end
          for j = left, right do
            local idxj = idx[j]
            local minor_idx = minormap[v[stepdst[idxj]]]
            local ofst = minortbl[minor_idx]
            ofst = ofst + 1
            minortbl[minor_idx] = ofst
            tmp_tbl[ofst] = idxj
            vd[idxj] = cur + minor_idx
          end
          cur = cur + #minortbl
          for j = left, right do
            idx[j] = tmp_tbl[j - left + 1]
          end
        else
          cur = cur + 1
          for j = left, right do
            vd[idx[j]] = cur
          end
        end
        if i < n then
          left, major = i + 1, v[idx[i + 1]]
          minor_min = v[stepdst[idx[i + 1]]]
          minor_max = minor_min
          minormap = {}
        end
      end
    end
    step = step * 2
  end
  -- remove empty from first (O(N))
  table.remove(self.sufa, 1)
  n = n - 1
  self.sufa_inv = {}
  for i = 1, n do self.sufa_inv[i] = 0 end
  for i = 1, n do
    self.sufa_inv[self.sufa[i]] = i
  end
end
TranSufA.makeLCPA = function(self)
  assert(self.sufa)
  local n = #self.sufa
  self.lcpa = {}
  local str, sufa, lcpa = self.str, self.sufa, self.lcpa
  for i = 1, n - 1 do lcpa[i] = 0 end
  local spos = 0
  for i = 1, n do
    local lcppos = self.sufa_inv[i]
    if lcppos < n then
      local len = spos
      local p1, p2 = sufa[lcppos], sufa[lcppos + 1]
      p1, p2 = p1 + spos, p2 + spos
      while p1 <= n and p2 <= n do
        if str:sub(p1, p1) == str:sub(p2, p2) then
          len = len + 1
          p1, p2 = p1 + 1, p2 + 1
        else break
        end
      end
      lcpa[lcppos] = len
      spos = mma(0, len - 1)
    end
  end
end

TranSufA.lowerBound = function(self, s)
  if s <= self.str:sub(self.sufa[1], #self.str) then
    return 1
  end
  if self.str:sub(self.sufa[#self.str], #self.str) < s then
    return #self.str + 1
  end
  local min, max = 1, #self.str
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if s <= self.str:sub(self.sufa[mid], #self.str) then
      max = mid
    else
      min = mid
    end
  end
  return max
end

TranSufA.create = function(self, str)
  self.str = str
  self:makeSufA()
  self:makeLCPA()
end

TranSufA.new = function(str)
  local obj = {}
  setmetatable(obj, {__index = TranSufA})
  obj:create(str)
  return obj
end

-- sample
local s = io.read()
local sa = TranSufA.new(s)
local t = io.read()
local sa_lbpos = sa:lowerBound(t)
local f = 1 < sa_lbpos and sa.sufa[sa_lbpos - 1] == 1
if f then
  for i = 1, #s do
    if s:sub(i, i) ~= t:sub(i, i) then
      f = false break
    end
  end
end
print(f and "Yes" or "No")

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


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

if s==t:sub(1,#s) then
    print("Yes")
else
    print("No")
end


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

if s~=t then
    print("Yes")
else
    print("No")
end


A=io.read"*n"
B=io.read"*n"
C=io.read"*n"
K=io.read"*n"
if(A>=K)then print(K)return end
if(A+B>=K)then print(A)return end
print(A-(K-A-B))

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


local read = io.read
local min = math.min

local n, m, x = read("n", "n", "n")
local c_t, a_t = {}, {}
for i = 1, n do
	c_t[i] = read("n")
	a_t[i] = {}
	for j = 1, m do
		a_t[i][j] = read("n")
	end
end

local money_min = math.maxinteger
for i = 0, 2^n - 1 do
	local money = 0
	local score_t = {}
	for j = 1, m do
		score_t[j] = 0
	end

	for j = 0, n - 1 do
		local bit = i >> j
		if bit == 0 then
			break
		end
		if bit & 1 == 1 then
			money = money + c_t[j + 1]
			for k = 1, m do
				score_t[k] = score_t[k] + a_t[j + 1][k]
			end
		end
	end

	local score_min = math.maxinteger
	for j = 1, m do
		score_min = min(score_min, score_t[j])
	end
	if score_min >= x then
		money_min = min(money_min, money)
	end
end

print((money_min == math.maxinteger) and -1 or money_min)

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


N=io.read"*n"
io.read()
S={}
for i=1,N do table.insert(S,io.read())end

local lr={}

local ls=string.byte"("
local rs=string.byte")"

for i,s in ipairs(S)do
	local b={s:byte(1,#s)}
	local l,r=0,0
	for j,v in ipairs(b) do
		if(v==ls)then
			r=r+1
		else
			if(r>0)then r=r-1
			else l=l+1 end
		end
	end
	if(l~=0 or r~=0)then table.insert(lr,{l,r})end
end

if(#lr==0)then print"Yes"return end

do
local s=0
for _,v in ipairs(lr)do
	s=s+v[1]-v[2]
end
if(s~=0)then print"No"return end
end

local bl,br=-1,-1
for i,v in ipairs(lr)do
	if(v[1]==0)then bl=i end
	if(v[2]==0)then br=i end
end
if(bl==-1 or br==-1)then print"No"return end

local s=lr[bl][2]
table.remove(lr,math.max(bl,br))
table.remove(lr,math.min(bl,br))
table.sort(lr,function(a,b)return a[1]-a[2]<b[1]-b[2]end)

for i,v in ipairs(lr)do
	s=s-v[1]
	if(s<0)then print"No"return end
	s=s+v[2]
end

print"Yes"

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local up, down = {}, {}
for i = 1, n do
  local s = io.read()
  local cur = 0
  local min = 0
  for j = 1, #s do
    if s:sub(j, j) == "(" then
      cur = cur + 1
    else
      cur = cur - 1
    end
    min = mmi(min, cur)
  end
  local dec = -min
  if 0 <= cur then
    table.insert(up, {dec, cur})
  else
    local flag = s:sub(#s, #s) == "("
    table.insert(down, {dec, cur, flag})
  end
end

local function sort1(a, b)
  return a[1] < b[1]
end
local function sort2(a, b)
  if a[3] and not b[3] then
    return true
  elseif b[3] and not a[3] then
    return flase
  else
    return a[1] > b[1]
  end
end
table.sort(up, sort1)
table.sort(down, sort2)
local cur = 0
local valid = true
for i = 1, n do
  local dec, tot = 0, 0
  if i <= #up then
    dec, tot = up[i][1], up[i][2]
  else
    local p = i - #up
    dec, tot = down[p][1], down[p][2]
  end
  -- print(i, dec, tot)
  if cur - dec < 0 then
    -- print(cur, dec)
    valid = false
    break
  end
  cur = cur + tot
  if cur < 0 then valid = false break end
  -- print(i)
end
if valid then
  valid = cur == 0
end
-- valid = valid and cur == 0
print(valid and "Yes" or "No")


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local s = {}
local t = {}
-- local up, down = {}, {}
for i = 1, n do
  s[i] = io.read()
  local cur = 0
  local min = 0
  for j = 1, #s[i] do
    if s[i]:sub(j, j) == "(" then
      cur = cur + 1
    else
      cur = cur - 1
    end
    min = mmi(min, cur)
  end
  t[i] = {min, cur}
  -- if 0 <= cur then
  --   table.insert(up, {min, cur})
  -- else
  --   table.insert(down, {min, cur})
  -- end
end
local function sortfunc(a, b)
  -- return a[1] > b[1]
  if 0 <= a[2] * b[2] then
    return a[1] > b[1]
  else
    return a[2] > b[2]
  end

  -- if 0 <= a[2] and 0 <= b[2] then
  --   return a[1] >= b[1]
  -- elseif a[2] < 0 and b[2] < 0 then
  --   return a[1] >= b[1]
  -- else
  --   return a[2] > b[2]
  -- end
end
table.sort(t, sortfunc)
local cur = 0
local valid = true
for i = 1, n do
  local min, last = t[i][1], t[i][2]
  if cur + min < 0 then
    valid = false
    break
  end
  cur = cur + last
end
valid = valid and cur == 0
print(valid and "Yes" or "No")


local str1 = io.read()
if str1 == "ABC" then
	print('ARC')
else
	print('ABC')
end

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


local s = io.read()
if s == "ABC" then
	print("ARC")
else
	print("ABC")
end


local s = io.read()

local function func(s)
	if s ~= "ARC" and s ~= "ABC" then
		return s
	end
	return ({ARC = "ABC",ABC = "ARC"})[s]
end

print(func(s))


local s = io.read()

local function func(s)
	if s ~= "ARC" and s ~= "ABC" then
		return s
	end
	return ({ARC = "ABC",ABC = "ARC"})[s]
end

print(func("ARC"))


n,k=io.read("*n","*n","*l")
snuke={}
for i=1,n do
    snuke[i]=1
end
a={}
for i=1,k do
    a[i]={}
    d=io.read("*n","*l")
    for j=1,d do
        a[i][j]=io.read("*n")
        if snuke[a[i][j]]==1 then
            snuke[a[i][j]]=0
        end
    end
end

counter=0
for i=1,n do
    counter=conter+snuke[i]
end
print(counter)

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


n,m=io.read("*n","*n","*l")
h={}
t={}
for i=1,n do
    h[i]=io.read("*n")
    t[i]=0
end
for i=1,m do
    a,b=io.read("*n","*n")
    if h[a]>h[b] then
        t[a]=1
        t[b]=0
    elseif h[a]<h[b] then
        t[a]=0
        t[b]=1
    end
end

counter=0
for i=1,n do
    counter=counter+t[i]
end
print(counter)

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


local x = io.read("*n")
for a = 0, 1000 do
  for b = 0, a - 1 do
    if a * a * a * a * a - b * b * b * b * b == x then
      print(a .." " .. b)
      os.exit()
    end
  end
end
for a = 0, 100 do
  for b = -100, 100 do
    if a * a * a * a * a - b * b * b * b * b == x then
      print(a .." " .. b)
      os.exit()
    end
  end
end

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


n=io.read("*n","*l")
m={}
combination=0
for i=1,n do
    a=io.read("*n")
    if not m[i+a] then
        m[i+a]=0
    end
    if m[i-a] then
        combination=combination+m[i-a]
    end
    m[i+a]=m[i+a]+1
end
print(combination)

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

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

Sample Output 3

22


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

combination=0
for i,_ in pairs(l) do
    if r[i] then
        combination=combination+l[i]*r[i]
    end
end
print(combination)


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

local count = 0
for i = 1, N - 1 do
	for j = i + 1, N do
		if j - i == A[i] + A[j] then
			count = count + 1
		end
	end
end

print(count)


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
for j=i+1,N do
	if(A[i]+A[j]==j-i)then s=s+1 end
end
end
print(s)


local q, a, b, c = io.read("*n", "*n", "*n", "*n", "*l")
local op = {}
for i = 1, q do
  op[i] = io.read()
end
if a + b + c == 0 then
  print("No")
  os.exit()
elseif a + b + c == 1 then
  local ret = {}
  local valid = true
  for iq = 1, q do
    if op[iq] == "AB" then
      if a == 1 then
        ret[iq] = "B"
        a, b = 0, 1
      elseif b == 1 then
        ret[iq] = "A"
        a, b = 1, 0
      else
        valid = false break
      end
    elseif op[iq] == "BC" then
      if b == 1 then
        ret[iq] = "C"
        b, c = 0, 1
      elseif c == 1 then
        ret[iq] = "B"
        b, c = 1, 0
      else
        valid = false break
      end
    else
      if a == 1 then
        ret[iq] = "C"
        a, c = 0, 1
      elseif c == 1 then
        ret[iq] = "A"
        a, c = 1, 0
      else
        valid = false break
      end
    end
  end
  if valid then
    print("Yes")
    print(table.concat(ret, "\n"))
  else
    print("No")
  end
  os.exit()
elseif 3 <= a + b + c then
  if a + b == 0 and op[1] == "AB" then
    print("No") os.exit()
  elseif b + c == 0 and op[1] == "BC" then
    print("No") os.exit()
  elseif a + c == 0 and op[1] == "AC" then
    print("No") os.exit()
  end
  local ret = {}
  for iq = 1, q do
    if op[iq] == "AB" then
      if a < b then
        ret[iq] = "A"
        a, b = a + 1, b - 1
      else
        ret[iq] = "B"
        a, b = a - 1, b + 1
      end
    elseif op[iq] == "BC" then
      if b < c then
        ret[iq] = "B"
        b, c = b + 1, c - 1
      else
        ret[iq] = "C"
        b, c = b - 1, c + 1
      end
    else
      if a < c then
        ret[iq] = "A"
        a, c = a + 1, c - 1
      else
        ret[iq] = "C"
        a, c = a - 1, c + 1
      end
    end
  end
  print("Yes")
  print(table.concat(ret, "\n"))
  os.exit()
end
-- a + b + c == 2
local dp = {}
for j = 1, q + 1 do
  dp[j] = {}
  -- 110, 101, 011, 200, 020, 002
  for i = 1, 6 do
    dp[j][i] = false
  end
end
if a == 1 and b == 1 then
  dp[1][1] = true
elseif a == 1 and c == 1 then
  dp[1][2] = true
elseif b == 1 and c == 1 then
  dp[1][3] = true
elseif a == 2 then
  dp[1][4] = true
elseif b == 2 then
  dp[1][5] = true
else
  dp[1][6] = true
end
for i = 1, q do
  if op[i] == "AB" then
    if dp[i][1] then
      dp[i + 1][4] = true
      dp[i + 1][5] = true
    end
    if dp[i][2] then
      dp[i + 1][3] = true
    end
    if dp[i][3] then
      dp[i + 1][2] = true
    end
    if dp[i][4] or dp[i][5] then
      dp[i + 1][1] = true
    end
  elseif op[i] == "BC" then
    if dp[i][3] then
      dp[i + 1][5] = true
      dp[i + 1][6] = true
    end
    if dp[i][2] then
      dp[i + 1][1] = true
    end
    if dp[i][1] then
      dp[i + 1][2] = true
    end
    if dp[i][5] or dp[i][6] then
      dp[i + 1][3] = true
    end
  else
    if dp[i][2] then
      dp[i + 1][4] = true
      dp[i + 1][6] = true
    end
    if dp[i][1] then
      dp[i + 1][3] = true
    end
    if dp[i][3] then
      dp[i + 1][1] = true
    end
    if dp[i][4] or dp[i][6] then
      dp[i + 1][2] = true
    end
  end
end
local cur = false
for i = 1, 6 do
  if dp[q + 1][i] then
    cur = i
    break
  end
end
if not cur then
  print("No")
  os.exit()
end
local ret = {}
for i = 1, q do ret[i] = 0 end
for i = q, 1, -1 do
  if op[iq] == "AB" then
    if cur == 1 then
      if dp[i][4] then
        ret[i] = "B"
        cur = 4
      else--5
        ret[i] = "A"
        cur = 5
      end
    elseif cur == 2 then
      ret[i] = "A"
      cur = 3
    elseif cur == 3 then
      ret[i] = "B"
      cur = 2
    elseif cur == 4 then
      ret[i] = "A"
      cur = 1
    else-- 5
      ret[i] = "B"
      cur = 1
    end
  elseif op[iq] == "BC" then
    if cur == 3 then
      if dp[i][5] then
        ret[i] = "C"
        cur = 5
      else--6
        ret[i] = "B"
        cur = 6
      end
    elseif cur == 1 then
      ret[i] = "B"
      cur = 2
    elseif cur == 2 then
      ret[i] = "C"
      cur = 1
    elseif cur == 5 then
      ret[i] = "B"
      cur = 3
    else-- 6
      ret[i] = "C"
      cur = 3
    end
  else
    if cur == 2 then
      if dp[i][4] then
        ret[i] = "C"
        cur = 4
      else--6
        ret[i] = "A"
        cur = 6
      end
    elseif cur == 1 then
      ret[i] = "A"
      cur = 3
    elseif cur == 3 then
      ret[i] = "C"
      cur = 1
    elseif cur == 4 then
      ret[i] = "A"
      cur = 2
    else-- 6
      ret[i] = "C"
      cur = 2
    end
  end
end
print("Yes")
print(table.concat(ret, "\n"))

Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


N=io.read"*n"
A=io.read"*n"
B=io.read"*n"
C=io.read"*n"
io.read()
s={}
for i=1,N do
	table.insert(s,io.read())
end

r={}
for i=1,N do
	if(s[i]=="AB")then
		if(A==0 and B==0)then
			print("No") return
		end
		if(A<B)then A=A+1 B=B-1 table.insert(r,"A")
		else A=A-1 B=B+1 table.insert(r,"B")end
	elseif(s[i]=="AC")then
		if(A==0 and C==0)then
			print("No") return
		end
		if(A<C)then A=A+1 C=C-1 table.insert(r,"A")
		else A=A-1 C=C+1 table.insert(r,"C")end
	else
		if(B==0 and C==0)then
			print("No") return
		end
		if(B<C)then B=B+1 C=C-1 table.insert(r,"B")
		else B=B-1 C=C+1 table.insert(r,"C")end
	end
end
print("Yes")
for _,v in ipairs(r)do
print(v)
end


local K, A, B = io.read("*n", "*n", "*n")

local div_a = math.ceil(A / K)
local near_b = div_a * K

print((near_b <= B) and "OK" or "NG")

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


money = 100
X = tonumber(io.read())

year = 0
while money <= X do
	money = money + math.floor(money/100)
	year = year + 1
end

print(year)

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


money = 100
X = tonumber(io.read())

year = 0
while money <= X do
	money = math.floor((money * 101)/100)
	year = year + 1
end

print(year)


money = 100
X = tonumber(io.read())

year = 0
while money <= X do
	money = math.floor(money * 1.01)
	year = year + 1
end

print(year)


X=io.read"*n"

a=100
i=0
while(a<X)do
	a=math.floor(a*1.01)
	i=i+1
end
print(i)


local n,m,q=io.read("n","n","n")
local a,b,c,d={},{},{},{}
for i=1,q do
    a[i],b[i],c[i],d[i]=io.read("n","n","n","n")
end

local t={}
for i=1,10 do
    t[i]={}
end
for i=1,m do
    table.insert(t[1],{i})
end

for i=1,n-1 do
    for j,_ in pairs(t[i]) do
        local x=t[i][j]
        local p=x[#x]
        for k=p,m do
            local U={}
            for l=1,#x do
                U[l]=x[l]
            end
            table.insert(U,k)
            table.insert(t[i+1],U)
        end
    end
end

local answer=0
for _,T in pairs(t[n]) do
    local total=0
    for i=1,q do
        if T[b[i]]-T[a[i]]==c[i] then
            total=total+d[i]
        end
    end
    answer=math.max(answer,total)
end
print(answer)

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


A,B,N = string.match(io.read(), "(%d+) (%d+) (%d+)")
A,B,N = tonumber(A), tonumber(B), tonumber(N)

max = 0
for x = 0, N, 1 do
	tmp = math.floor((A*x)/B) - A * math.floor(x/B)
	if tmp > max then
		max = tmp
	end
end

print(max)

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


local mmi, mma = math.min, math.max
local a, b, n = io.read("*n", "*n", "*n")
if b == 1 then
  print(0)
  os.exit()
end
local l = mmi(n, b - 1)
print(l * a // b)


local mfl = math.floor

local function comp(a, b)
  return a < b
end

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

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

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

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

local lis = {}
for i = 1, n + 1 do
  lis[i] = 1000000007
end
local ret = {}
for i = 1, n do ret[i] = 1 end
local tasks = {1}
local undotask = {}
local sti = 1
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  -- print(src)
  if src == -1 then
    local ubpos, v, z = undotask[#undotask][1], undotask[#undotask][2], undotask[#undotask][3]
    table.remove(undotask)
    lis[ubpos] = v
    -- print("UNDO", z, ubpos, v)
  else
    if not asked[src] then
      asked[src] = true
      local ubpos = lower_bound(lis, t[src])
      table.insert(undotask, {ubpos, lis[ubpos], src})
      -- print("ADD", src, ubpos, lis[ubpos])
      lis[ubpos] = t[src]
      local r = lower_bound(lis, 1000000007) - 1
      ret[src] = r
    end
    -- print(src, len[src])
    stpos[src] = sti
    sti = sti + 1
    while cpos[src] < #edge[src] do
      cpos[src] = cpos[src] + 1
      local dst = edge[src][cpos[src]]
      if not asked[dst] then
        len[dst] = len[src] + 1
        table.insert(tasks, src)
        table.insert(tasks, -1)
        table.insert(tasks, dst)
        break
      end
    end
  end
end
print(table.concat(ret, "\n"))

Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

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

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

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

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.


local mfl = math.floor

local function comp(a, b)
  return a < b
end

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

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

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

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

local lis = {}
for i = 1, n + 1 do
  lis[i] = 1000000007
end
local ret = {}
for i = 1, n do ret[i] = 1 end
local tasks = {1}
local undotask = {}
local sti = 1
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  if src == -1 then
    ubpos, v = undotask[#undotask][1], undotask[#undotask][2]
    table.remove(undotask)
    lis[ubpos] = v
  else
    if not asked[src] then
      asked[src] = true
      local ubpos = upper_bound(lis, t[src])
      table.insert(undotask, {ubpos, lis[ubpos]})
      lis[ubpos] = t[src]
      local r = lower_bound(lis, 1000000007) - 1
      ret[src] = r
    end
    -- print(src, len[src])
    stpos[src] = sti
    sti = sti + 1
    while cpos[src] < #edge[src] do
      cpos[src] = cpos[src] + 1
      local dst = edge[src][cpos[src]]
      if not asked[dst] then
        len[dst] = len[src] + 1
        table.insert(tasks, src)
        table.insert(tasks, -1)
        table.insert(tasks, dst)
        break
      end
    end
  end
end
print(table.concat(ret, "\n"))


local s,w=io.read("n","n")
if s<=w then
    print("unsafe")
else
    print("safe")
end

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


S=io.read"*n"
W=io.read"*n"
print((
	S<=W
)and"unsafe"or"safe")


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

local counter=0
for k,v in pairs(smap) do
    counter=counter+1
end
print(counter)

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


local N = io.read("*n")
io.read("*l")
local S = {}
local count = 0
for i = 1, N do
	local str = io.read("*l")
	if not S[str] then
		S[str] = true
	else
		count = count + 1
	end
end

print(N - count)


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 N = read.nl()
local set = {}
for i=1,N do
    local s = read.l()
    set[s] = true
end
local c = 0
for k,v in pairs(set) do
    c = c + 1
end
print(c)


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


N=io.read"*n"
M=io.read"*n"
s=0
for i=1,M do s=s+io.read"*n"end
print(math.max(-1,N-s))

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


n,k=io.read("*n","*n")
combination=0
for i=k,n+1 do
    min=i*(i-1)//2
    max=(2*n-i-1)*i//2
    combination=(combination+(max-min+1))%1000000007
end
print(combination)


local n, k = io.read('*n', '*n')
local mfl =  math.floor
local mod = 1000000007
local ans = 0
for i = k, n + 1 do
    local pre = mfl(i * (i - 1) / 2)
    local suf = mfl((n + n - i + 1) * i / 2)
    ans = (ans + suf - pre + 1) % mod
end
print(ans)


local n, k = io.read('*n', '*n')
local mod = 1000000007
local ans = 0
for i = k, n + 1 do
    local pre = i * (i - 1) / 2
    local suf = (n + n + i - 1) * i / 2
    ans = (ans + suf - pre) % mod
end
print(ans)


local MOD=1000000007
local N=io.read"*n"
local K=io.read"*n"

local r=0
for i=K,N+1 do
local min=i*(i-1)/2
local max=(N-i+1+N)*i/2
r=(r+max-min+1)%MOD
end
print(r)


local mfl, mce = math.floor, math.ceil
local mod = 1000000007
local n, k = io.read("*n", "*n")
local ret = 0
for i = k, n + 1 do
  local smallest = mfl(i * (i - 1) / 2)
  local largest = mfl((n + n - i + 1 ) * i / 2)
  -- print(smallest, largest)
  ret = (ret + largest - smallest + 1) % mod
end
print(ret)


local N,K=io.read("*n","*n")
N=N%K
if(N>K-N)then print(K-N)
else print(N)
end

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

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


local k=io.read("n")

local que={1,2,3,4,5,6,7,8,9}

for i=1,k-1 do
    local pop=que[i]
    for j=-1,1 do
        local add=(pop%10)+j
        if 0<=add and add<=9 then
            table.insert(que,pop*10+add)
        end
    end
end
print(que[k])


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

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

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

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

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

----------

local k=io.read("n")

local que=List.new()
for i=1,9 do
    List.pushright(que,i)
end

for i=1,k-1 do
    local pop=List.popleft(que)
    for j=-1,1 do
        local add=(pop%10)+j
        if 0<=add and add<=9 then
            List.pushright(que,pop*10+add)
        end
    end
end
print(List.popleft(que))


K=io.read"*n"

a={{}}
for i=1,9 do table.insert(a[1],{i}) end
function lunlun()
	local d=1
	while true do
		a[d+1]={}
		for i=1,#a[d] do
			local l=a[d][i][#a[d][i]]
			for k=math.max(0,l-1),math.min(9,l+1)do
				local b={}
				for j=1,d do
					table.insert(b,a[d][i][j])
				end
				table.insert(b,k)
				table.insert(a[d+1],b)
				coroutine.yield(b)
			end
		end
		d=d+1
	end
end

if(K<10)then print(K) return end

co=coroutine.create(lunlun)
for i=10,K do
	_,b=coroutine.resume(co)
end
print(table.concat(b))


local msq = math.sqrt
local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local n = io.read("*n")
-- n = k^a(1-kb)
local ret = 0
local lim = mmi(n - 1, mce(msq(n)))
-- a = 0
for i = 1, lim do
  if n - 1 < i * i then break
  elseif n - 1 == i * i then
    ret = ret + 1
  elseif (n - 1) % i == 0 then
    ret = ret + 2
  end
end
-- a = 1, ..., 40
for a = 1, 40 do
  for k = 2, lim do
    local kpow = 1
    for i = 1, a do
      kpow = kpow * k
    end
    if n < kpow then break end
    if n % kpow == 0 then
      local v = n // kpow
      if v % k == 1 then
        ret = ret + 1
      end
    end
  end
end
print(ret)

Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9


local k_enshu, n_ken = io.read("*n", "*n")

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

local max_aida = 0
for i = 1, n_ken - 1 do
    local ie_aida = a_ie[i + 1] - a_ie[i] --math.maxを使った方が良かったかも。
    if ie_aida > max_aida then
        max_aida = ie_aida
    end
end
local ie_aida = k_enshu + a_ie[1] - a_ie[n_ken] --math.maxを使った方が良かったかも。
if ie_aida > max_aida then
    max_aida = ie_aida
end

print(k_enshu - max_aida)

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


local k, n = string.match(io.read(), "(%d+) (%d+)")
k = tonumber(k)
n = tonumber(n)

local a = {}

for x in string.gmatch(io.read(), "(%d+)") do
    table.insert(a, tonumber(x))
end

table.sort(a, function(a, b) return a < b end)
table.insert(a, 1, a[n])
table.insert(a, k + a[2])

local ans = 9999999999

for i = 2, n+1 do
    local t = a[i - 1] - a[i]

    if t < 0 then
        t = t + k
    end

    ans = math.min(ans, t)
end

print(ans)


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 K, N = read.nn()
local A = {}
for i=0,N-1 do
    A[i] = read.n()
end
local ans = 10^7
for i=0,N-1 do
    local ed = i
    local st = (i+1)%N
    local d
    if st == 0 then
        d = K - ((A[st]) + (K - A[ed]))
    else
        d = K - (A[st] - A[ed])
    end
    ans = math.min(ans, d)
end
print(ans)


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


-- mC2 + nCc
local n, m = io.read("*n", "*n")
local nC2, mC2 = 0, 0
if n >= 2 then
    nC2 = 1
    nC2 = n * (n - 1) / 2
end

if m >= 2 then
    mC2 = m * (m - 1) / 2
end

print(nC2 + mC2)

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


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

local taskstate = {}
for i = 1, h * w do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = h * w

local function addtask(idx)
  if not taskstate[idx] then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
  end
end

local inf = 1000000007
local len = {}
for i = 1, h * w do
  len[i] = inf
end
if not map[1] then
  len[1] = 1
else
  len[1] = 0
end

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

addtask(1)

while done < tasknum do
  done = done + 1
  local taskidx = done % tasklim
  if taskidx == 0 then taskidx = tasklim end
  local idx = tasks[taskidx]
  taskstate[idx] = false

  if idx <= (h - 1) * w then walk(idx, idx + w) end
  if 1 < w then
    if idx % w ~= 0 then walk(idx, idx + 1) end
  end
end
local ret = len[h * w]
print(math.ceil(ret / 2))

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

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

Sample Output 4

4


Score : 100 points

Problem Statement

Print the K-th element of the following sequence of length 32:

1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51

Constraints

1 \leq K \leq 32

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the K-th element.

Sample Input 1

6

Sample Output 1

2

The 6-th element is 2.

Sample Input 2

27

Sample Output 2

5

The 27-th element is 5.


local h, w = io.read("*n", "*n")
if h == 1 then
  print(1)
elseif w == 1 then
  print(1)
elseif h % 2 == 0 or w % 2 == 0 then
  print((h * w) // 2)
else
  local a = w // 2
  local b = h // 2
  print(a * b + (a + 1) * (b + 1))
end

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


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 H, W = read.nn()
print(H*W//2 + H*W%2)


local h, w = io.read("*n", "*n")
if h % 2 == 0 or w % 2 == 0 then
  print(h * w // 2)
else
  local a = h * w // 2
  print(a + 1)
end


local n=io.read("n")
local s=""
local function dfs(s,max)
    if #s==n then
        print(s)
    else
        for c=97,max do
            dfs(s..string.char(c),(c==max and max+1 or max))
        end
    end
end
dfs(s,97)

Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab


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 A = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
local N = read.n()

local ans = {}
local build = {}
local function dfs_cps(i, mx, cont)
    if i == N then
        table.insert(ans, table.concat(build))
        return cont()
    else
        local function make_cont(j, lim)
            return function()
                if j <= lim then
                    build[i+1] = A[j]
                    return dfs_cps(i+1, math.max(j,mx), make_cont(j+1,lim))
                else
                    return cont()
                end
            end
        end
        make_cont(1, mx+1)()
    end
end
dfs_cps(0, 0, function()
    for i=1,#ans do
        print(ans[i])
    end
end)


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 open(...) for _,t in pairs{...} do for k,v in pairs(t) do _G[k]=v end end end
----
open(table, math)
local A = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
local N = read.n()

local ans = {}
local q = {["b"]=1, ["f"]=1}
local function count()
    return q.f - q.b
end
local function pop()
    local a = q[q.b]
    q[q.b] = nil
    q.b = q.b + 1
    return unpack(a)
end
local function push(a)
    q[q.f] = a
    q.f = q.f + 1
end

push({0,0,{}})
while count() > 0 do
    local i,mx,t = pop()
    if i == N then
        insert(ans, concat(t, ""))
    else
        for j=1,mx+1 do
            local n = {unpack(t)}
            insert(n, A[j])
            push {i+1, max(j,mx), n}
        end
    end
end
for i=1,#ans do
    print(ans[i])
end