local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={default_val and {__index=function()return default_val 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, W = read.nn()
local w, v = {}, {}
for i=1,N do
    w[i], v[i] = read.nn()
end
local tbl = array(2, 10^10)
tbl[0][0] = 0
for i=0,N-1 do
    local nw, nv = w[i+1], v[i+1]
    for j=0,100000 do
        if tbl[i+1][j] > tbl[i][j] then
            tbl[i+1][j] = tbl[i][j]
        end
        if tbl[i+1][j+nv] > tbl[i][j] + nw then
            tbl[i+1][j+nv] = tbl[i][j] + nw
        end
    end
end
local ans = 0
for j=0,100000 do
    if tbl[N][j] <= W then
        ans = math.max(ans, j)
    end
end
print(ans)

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


local mma, mmi = math.max, math.min
local mfl, mce = math.floor, math.ceil
local function comp(a, b)
  return a < b
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, W = io.read("*n", "*n")
local v, w = {}, {}
local low_w = true
for i = 1, n do
  w[i], v[i] = io.read("*n", "*n")
end

local function solve_low_w()
  local t = {}
  for i = 1, W do t[i] = 0 end
  for i = 1, n do
    for j = W - w[i], 1, -1 do
      if 0 < t[j] then
        t[j + w[i]] = mma(t[j + w[i]], t[j] + v[i])
      end
    end
    t[w[i]] = mma(t[w[i]], v[i])
  end
  local ret = 0
  for i = 1, W do ret = mma(ret, t[i]) end
  return ret
end

local function solve_low_v()
  local t = {}
  for i = 1, 200000 do t[i] = 0 end
  local v_cursum = 0
  for i = 1, n do
    local vlim = mmi(v_cursum, 200000 - v[i])
    for j = vlim, 1, -1 do
      if 0 < t[j] then
        if t[j + v[i]] == 0 then
          t[j + v[i]] = t[j] + w[i]
        else
          t[j + v[i]] = mmi(t[j + v[i]], t[j] + w[i])
        end
      end
    end
    if t[v[i]] == 0 then
      t[v[i]] = w[i]
    else
      t[v[i]] = mmi(t[v[i]], w[i])
    end
    v_cursum = v_cursum + v[i]
  end
  local ret = 0
  for i = 200000, 1, -1 do
    if 0 < t[i] and t[i] <= W then ret = i break end
  end
  return ret
end
print(solve_low_v())

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 read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local unpack = table.unpack or unpack
local insert = table.insert or insert
local max = math.max
----
local function adj_list(num_nodes)
    local t = {}
    for n=1, num_nodes do
        t[n] = {}
    end
    return t
end
----
local N, M = read.nn()
local edges = adj_list(N)
local rev_adj_mat = array(2, nil)
for i=1,M do
    local x, y = read.nn()
    edges[x][#edges[x] + 1] = y
    rev_adj_mat[y][x] = true
end
local function indeg_zero(n)
    for k, v in pairs(rev_adj_mat[n]) do
        if v then
            return false
        end
    end
    return true
end

local toposorted = {}
local q = {}
for n=1,N do
    if indeg_zero(n) then
        insert(q, n)
    end
end
local front = 1
local back = #q + 1
local function push(v)
    q[back] = v
    back = back + 1
end
local function pop()
    local r = q[front]
    front = front + 1
    return r
end
local function empty()
    return back - front == 0
end

while not empty() do
    local n = pop()
    insert(toposorted, n)
    for k, m in pairs(edges[n]) do
        rev_adj_mat[m][n] = nil
        if indeg_zero(m) then
            push(m)
        end
    end
end
--[[
for k, v in pairs(toposorted) do
    print(k, v)
end
]]
local lp_from = array(1, 0)
for _, n in pairs(toposorted) do
    for _, m in pairs(edges[n]) do
        lp_from[m] = max(lp_from[m], lp_from[n] + 1)
    end
end
local ans = 0
for _, lp in pairs(lp_from) do
    ans = max(ans, lp)
end
print(ans)

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


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

local dp={}
for i=1,h do
    dp[i]={}
    for j=1,w do
        dp[i][j]=0
    end
end
dp[1][1]=1

local mod=1000000007
for i=1,h do
    for j=1,w do
        if i<h and a[i+1][j]==46 then
            dp[i+1][j]=(dp[i+1][j]+dp[i][j])%mod
        end
        if j<w and a[i][j+1]==46 then
            dp[i][j+1]=(dp[i][j+1]+dp[i][j])%mod
        end
    end
end
print(dp[h][w])

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns.
Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}.
If a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square.
It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W).
As the answer can be extremely large, find the count modulo 10^9 + 7.

Constraints

H and W are integers.

2 \leq H, W \leq 1000

a_{i, j} is . or #.

Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Sample Input 1

3 4
...#
.#..
....

Sample Output 1

3

There are three paths as follows:

Sample Input 2

5 2
..
#.
..
.#
..

Sample Output 2

0

There may be no paths.

Sample Input 3

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

Sample Output 3

24

Sample Input 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4

345263555

Be sure to print the count modulo 10^9 + 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) 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.n()
local ph = read.N(N)
local pt = {}
for i=1,N do pt[i] = 1 - ph[i] end

local tbl = array(2, 0)
tbl[0][0] = 1.

for i=1,N do
    tbl[i][0] = tbl[i-1][0] * pt[i]
    for j=1,i do
        tbl[i][j] = tbl[i-1][j] * pt[i] + tbl[i-1][j-1] * ph[i]
    end
end
local sum = 0
for c=math.ceil(N/2),N do
    sum = sum + tbl[N][c]
end

print(sum)

Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins.
Find the probability of having more heads than tails.

Constraints

N is an odd number.

1 \leq N \leq 2999

p_i is a real number and has two decimal places.

0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails.
The output is considered correct when the absolute error is not greater than 10^{-9}.

Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872


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

local dp={}
for i=1,n+1 do
    dp[i]={}
    for j=1,n+1 do
        dp[i][j]={}
        for k=1,n+1 do
            dp[i][j][k]=-1
        end
    end
end

local function solve(i,j,k)
    if dp[i][j][k]>=0 then
        return dp[i][j][k]
    end
    if i==1 and j==1 and k==1 then
        return 0
    end
    local total=0
    if i>1 then
        total=total+solve(i-1,j,k)*(i-1)
    end
    if n+1>i and j>1 then
        total=total+solve(i+1,j-1,k)*(j-1)
    end
    if n+1>j and k>1 then
        total=total+solve(i,j+1,k-1)*(k-1)
    end
    total=total+n
    total=total*(1/(i+j+k-3))

    dp[i][j][k]=total
    return dp[i][j][k]
end

print(solve(a[1],a[2],a[3]))

Score : 100 points

Problem Statement

There are N dishes, numbered 1, 2, \ldots, N.
Initially, for each i (1 \leq i \leq N), Dish i has a_i (1 \leq a_i \leq 3) pieces of sushi on it.

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

Constraints

All values in input are integers.

1 \leq N \leq 300

1 \leq a_i \leq 3

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten.
The output is considered correct when the relative difference is not greater than 10^{-9}.

Sample Input 1

3
1 1 1

Sample Output 1

5.5

The expected number of operations before the first piece of sushi is eaten, is 1.
After that, the expected number of operations before the second sushi is eaten, is 1.5.
After that, the expected number of operations before the third sushi is eaten, is 3.
Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.

Sample Input 2

1
3

Sample Output 2

3

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.

Sample Input 3

2
1 2

Sample Output 3

4.5

Sample Input 4

10
1 3 2 3 3 2 3 2 1 3

Sample Output 4

54.48064457488221


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

local dp={}
for i=0,k do
    if not dp[i] then
        for j=1,n do
            dp[i+a[j]]=true
        end
    end
end
print(dp[k] and "First" or "Second")

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


local n,k=io.read("n","n")
local a={}
local dp={}
for i=1,n do
    local input=io.read("n")
    a[i]=input
    dp[input]=true
end

for i=0,k do
    for j=1,n do
        if not dp[i] then
            dp[i+a[j]]=true
        end
    end
end
print(dp[k] and "First" or "Second")


local n,k=io.read("n","n")
local a={}
local m={}
for i=1,n do
    local input=io.read("n")
    a[i]=input
    m[input]=true
end

local dp={}
for i=0,k do
    if m[i] then
        dp[i]=true
    end
    for j=1,n do
        if not dp[i] then
            dp[i+a[j]]=true
        end
    end
end
print(dp[k] and "First" or "Second")


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringx(o)if type(o) == 'table' then local keys = {}for k in pairs(o) do table.insert(keys, k) end table.sort(keys)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, '['..k..'] = ' .. tostringx(v))end return '{' .. table.concat(inside, ', ') .. '}'else if type(o) == 'string' then o = string.format('%q', o)end return tostring(o)end end
----
local N, K = read.nn()
local A = read.N(N)
table.sort(A)
local amin = A[1]
local tbl = {}
local rev = {['😞']='🏆', ['🏆']='😞'}
for i=0,amin-1 do
    tbl[i] = '😞'
end
for i=0,K-1 do
    if tbl[i] == '😞' then
        for _,a in ipairs(A) do
            tbl[i+a] = '🏆'
        end
    else
        for _,a in ipairs(A) do
            if tbl[i+a] == nil then
                tbl[i+a] = '😞'
            end
        end
    end
end
--print(tostringx(tbl))
if tbl[K]=='🏆' then
    print('First')
elseif tbl[K]=='😞' then
    print('Second')
else
    error()
end


local mmi = math.min
local n = io.read("*n")
local sum = {io.read("*n")}
for i = 2, n do
  sum[i] = sum[i - 1] + io.read("*n")
end
local dp = {}
dp[1] = {}
for i = 1, n do
  dp[1][i] = 0
end
for i = 2, n do
  dp[i] = {}
  local leftposlim = n - i + 1
  for j = 1, leftposlim do
    local v = dp[1][j] + dp[i - 1][j + 1]
    for k = 2, i - 1 do
      v = mmi(v, dp[k][j] + dp[i - k][j + k])
    end
    v = v + sum[j + i - 1]
    if 1 < j then v = v - sum[j - 1] end
    dp[i][j] = v
  end
end
print(dp[n][1])

Score : 100 points

Problem Statement

There are N slimes lining up in a row.
Initially, the i-th slime from the left has a size of a_i.

Taro is trying to combine all the slimes into a larger slime.
He will perform the following operation repeatedly until there is only one slime:

Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints

All values in input are integers.

2 \leq N \leq 400

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 20 30 40

Sample Output 1

190

Taro should do as follows (slimes being combined are shown in bold):

(10, 20, 30, 40) → (30, 30, 40)

(30, 30, 40) → (60, 40)

(60, 40) → (100)

Sample Input 2

5
10 10 10 10 10

Sample Output 2

120

Taro should do, for example, as follows:

(10, 10, 10, 10, 10) → (20, 10, 10, 10)

(20, 10, 10, 10) → (20, 20, 10)

(20, 20, 10) → (20, 30)

(20, 30) → (50)

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

6
7 6 8 6 1 1

Sample Output 4

68

Taro should do, for example, as follows:

(7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)

(7, 6, 8, 6, 2) → (7, 6, 8, 8)

(7, 6, 8, 8) → (13, 8, 8)

(13, 8, 8) → (13, 16)

(13, 16) → (29)


local mod = 1000000007
local bls, brs = bit.lshift, bit.rshift
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {}
  for j = 1, n do
    t[i][j] = io.read("*n") == 1
  end
end

local tot = bls(1, n)
local alltask = {}
local stagetask = {}
do
  for i = 1, n do
    stagetask[i] = {}
  end
  for i = 1, tot - 1 do
    -- set empty state
    alltask[i] = 0
  end
  for i = 1, tot - 1 do
    local ti = i
    local cnt = 0
    for j = 1, n do
      if ti % 2 == 1 then
        cnt = cnt + 1
        ti = ti - 1
      end
      ti = ti / 2
    end
    table.insert(stagetask[cnt], i)
  end
  -- set first state
  local tmp = 1
  for i = 1, n do
    alltask[tmp] = t[1][i] and 1 or 0
    tmp = tmp * 2
  end
end

for stage = 1, n - 1 do
  local stlen = #stagetask[stage]
  for i_stagetask = 1, stlen do
    local used = stagetask[stage][i_stagetask]
    local mul = 1
    local tmp = used
    for j = 1, n do
      if tmp % 2 == 0 then
        if t[stage + 1][j] then
          alltask[used + mul] = (alltask[used + mul] + alltask[used]) % mod
        end
      else
        tmp = tmp - 1
      end
      tmp = tmp / 2
      mul = mul * 2
    end
  end
end
print(alltask[tot - 1])

Score : 100 points

Problem Statement

There are N men and N women, both numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}.
If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not.

Taro is trying to make N pairs, each consisting of a man and a woman who are compatible.
Here, each man and each woman must belong to exactly one pair.

Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 21

a_{i, j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Sample Input 1

3
0 1 1
1 0 1
1 1 1

Sample Output 1

3

There are three ways to make pairs, as follows ((i, j) denotes a pair of Man i and Woman j):

(1, 2), (2, 1), (3, 3)

(1, 2), (2, 3), (3, 1)

(1, 3), (2, 1), (3, 2)

Sample Input 2

4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

Sample Output 2

1

There is one way to make pairs, as follows:

(1, 2), (2, 4), (3, 1), (4, 3)

Sample Input 3

1
0

Sample Output 3

0

Sample Input 4

21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

Sample Output 4

102515160

Be sure to print the number modulo 10^9 + 7.


local n=io.read("n")
local g={}
for i=1,n do
    g[i]={}
end
for i=1,n-1 do
    local x,y=io.read("n","n")
    g[x][y]=true
    g[y][x]=true
end

local bp,wp={},{}
local mod=1000000007
local function solve(cur,frm)
    bp[cur]=1
    wp[cur]=1
    for nxt in pairs(g[cur]) do
        if nxt~=frm then
            solve(nxt,cur)
            wp[cur]=wp[cur]*(wp[nxt]+bp[nxt])%mod
            bp[cur]=bp[cur]*wp[nxt]%mod
        end
    end
    return (bp[cur]+wp[cur])%mod
end
print(solve(1,0))

Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black.
Here, it is not allowed to paint two adjacent vertices both in black.

Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Sample Input 1

3
1 2
2 3

Sample Output 1

5

There are five ways to paint the vertices, as follows:

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

9

There are nine ways to paint the vertices, as follows:

Sample Input 3

1

Sample Output 3

2

Sample Input 4

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

Sample Output 4

157


local n=io.read("n")
local g={}
for i=1,n do
    g[i]={}
end
for i=1,n-1 do
    local x,y=io.read("n","n")
    g[x][y]=true
    g[y][x]=true
end

local bp,wp={},{}
local mod=1000000007
local function solve(cur,frm)
    bp[cur]=1
    wp[cur]=1
    for nxt in pairs(g[cur]) do
        if nxt~=frm then
            solve(nxt,cur)
            wp[cur]=wp[cur]*(wp[nxt]+bp[nxt])%mod
            bp[cur]=bp[cur]*wp[nxt]%mod
        end
    end
end
solve(1,0)
print(bp[1]+wp[1])


local n=io.read("n")
local g={}
for i=1,n do
    g[i]={}
end
for i=1,n-1 do
    local x,y=io.read("n","n")
    g[x][y]=true
    g[y][x]=true
end

local bp,wp={},{}
local mod=1000000007
local function solve(cur,frm)
    bp[cur]=1
    wp[cur]=1
    for nxt in pairs(g[cur]) do
        if nxt~=frm then
            solve(nxt,cur)
            bp[cur]=bp[cur]*wp[nxt]%mod
            wp[cur]=wp[cur]*(wp[nxt]+bp[nxt])%mod
        end
    end
end
solve(1,0)
print(bp[1]+wp[1])


local n=io.read("n")
local g={}
for i=1,n do
    g[i]={}
end
for i=1,n-1 do
    local x,y=io.read("n","n")
    g[x][y]=true
    g[y][x]=true
end

local bp,wp={},{}
local mod=1000000007
local function solve(cur,frm)
    bp[cur]=1
    wp[cur]=1
    for nxt in pairs(g[cur]) do
        if nxt~=frm then
            solve(nxt,cur)
            bp[cur]=bp[cur]*wp[nxt]%mod
            wp[cur]=wp[cur]*(wp[nxt]+bp[nxt])%mod
        end
    end
    if cur==1 then
        return bp[cur]+wp[cur]
    end
end

print(solve(1,0))


local function lower_bound(ary,key)
    local n=#ary
    if n==0 then
        return 1
    end
    if key<=ary[1] then
        return 1
    end
    if key>ary[n] then
        return n+1
    end
    local min,max=1,n
    while 1<max-min do
        local mid=math.floor((min+max)/2)
        if key>ary[mid] then
            min=mid
        else
            max=mid
        end
    end
    return max
end

----------

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

local INF=10^20
local hp={}
local ap={}
for i=1,n do
    hp[i]=INF
    ap[i]=0
end

for i=1,n do
    local H,A=h[i],a[i]
    local k=lower_bound(hp,H)
    hp[k]=H
    ap[k]=math.max(ap[k],A)
end

local total=0
for i=1,n do
    total=total+ap[i]
end
print(total)

Score : 100 points

Problem Statement

There are N flowers arranged in a row.
For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively.
Here, h_1, h_2, \ldots, h_N are all distinct.

Taro is pulling out some flowers so that the following condition is met:

The heights of the remaining flowers are monotonically increasing from left to right.

Find the maximum possible sum of the beauties of the remaining flowers.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq h_i \leq N

h_1, h_2, \ldots, h_N are all distinct.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

Output

Print the maximum possible sum of the beauties of the remaining flowers.

Sample Input 1

4
3 1 4 2
10 20 30 40

Sample Output 1

60

We should keep the second and fourth flowers from the left.
Then, the heights would be 1, 2 from left to right, which is monotonically increasing, and the sum of the beauties would be 20 + 40 = 60.

Sample Input 2

1
1
10

Sample Output 2

10

The condition is met already at the beginning.

Sample Input 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

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

Sample Output 4

31

We should keep the second, third, sixth, eighth and ninth flowers from the left.


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

local n = io.read("*n")
local h = {}
local a = {}
for i = 1, n do h[i] = io.read("*n") end
for i = 1, n do a[i] = 0LL + io.read("*n") end
local st = SegTree.new(n, function(x, y) return x < y and y or x end, 0)
for i = 1, n do
  if h[i] == 1 then
    st:update(h[i], a[i])
  else
    local cur = st:getRange(1, h[i] - 1)
    st:update(h[i], cur + a[i])
  end
end
local str = tostring(st.stage[1][1]):gsub("LL", "")
print(str)


local mfl = math.floor
local mod = 1000000007
local function badd(x, y) return (x + y) % mod end
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 lltonumber(str)
  local ret = 0LL
  for i = 1, #str do
    ret = ret * 10LL + str:sub(i, i):byte() - 48
  end
  return ret
end

local s = io.read()
local n, k = s:match("(%d+) (%d+)")
n = tonumber(n)
k = lltonumber(k)
local t = {}
for i = 1, n do
  t[i] = {}
  for j = 1, n do
    t[i][j] = io.read("*n")
  end
end
local cpy = {}
for i = 1, n do
  cpy[i] = {}
  for j = 1, n do
    cpy[i][j] = 0
  end
end
local function update()
  for i = 1, n do
    for j = 1, n do
      cpy[i][j] = 0
    end
  end
  for i = 1, n do
    for j = 1, n do
      for k = 1, n do
        cpy[i][k] = badd(cpy[i][k], bmul(t[i][j], t[j][k]))
      end
    end
  end
  for i = 1, n do
    for j = 1, n do
      t[i][j] = cpy[i][j]
    end
  end
end
local src, dst = {}, {}
for i = 1, n do
  src[i] = 1
  dst[i] = 0
end
while 0LL < k do
  if k % 2LL == 1LL then
    for i = 1, n do
      dst[i] = 0
    end
    for i = 1, n do
      for j = 1, n do
        dst[j] = badd(dst[j], bmul(t[i][j], src[i]))
      end
    end
    for i = 1, n do
      src[i] = dst[i]
    end
  end
  update()
  k = k / 2LL
end
local ret = 0
for i = 1, n do
  ret = badd(ret, src[i])
end
print(ret)

Score : 100 points

Problem Statement

There is a simple directed graph G with N vertices, numbered 1, 2, \ldots, N.

For each i and j (1 \leq i, j \leq N), you are given an integer a_{i, j} that represents whether there is a directed edge from Vertex i to j.
If a_{i, j} = 1, there is a directed edge from Vertex i to j; if a_{i, j} = 0, there is not.

Find the number of different directed paths of length K in G, modulo 10^9 + 7.
We will also count a path that traverses the same edge multiple times.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 10^{18}

a_{i, j} is 0 or 1.

a_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of different directed paths of length K in G, modulo 10^9 + 7.

Sample Input 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

Sample Output 1

6

G is drawn in the figure below:

There are six directed paths of length 2:

1 → 2 → 3

1 → 2 → 4

2 → 3 → 4

2 → 4 → 1

3 → 4 → 1

4 → 1 → 2

Sample Input 2

3 3
0 1 0
1 0 1
0 0 0

Sample Output 2

3

G is drawn in the figure below:

There are three directed paths of length 3:

1 → 2 → 1 → 2

2 → 1 → 2 → 1

2 → 1 → 2 → 3

Sample Input 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3

1

G is drawn in the figure below:

There is one directed path of length 2:

4 → 5 → 6

Sample Input 4

1 1
0

Sample Output 4

0

Sample Input 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5

957538352

Be sure to print the count modulo 10^9 + 7.


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

local s = io.read()
local d = io.read("*n")
local dpmax, dpnormal = {}, {}
for i = 1, d do
  dpmax[i] = 0
  dpnormal[i] = 0
end
dpmax[d] = 1
local nxt_max, nxt_normal = {}, {}
for i = 1, #s do
  for j = 1, d do
    nxt_max[j] = 0
    nxt_normal[j] = 0
  end
  local v = s:sub(i, i):byte() - 48
  for src = 1, d do
    for add = 0, v do
      local dst = (add + src) % d
      if dst == 0 then dst = d end
      if add == v then
        nxt_max[dst] = badd(nxt_max[dst], dpmax[src])
      else
        nxt_normal[dst] = badd(nxt_normal[dst], dpmax[src])
      end
    end
    for add = 0, 9 do
      local dst = (add + src) % d
      if dst == 0 then dst = d end
      nxt_normal[dst] = badd(nxt_normal[dst], dpnormal[src])
    end
  end
  for j = 1, d do
    dpmax[j] = nxt_max[j]
    dpnormal[j] = nxt_normal[j]
  end
end
local ret = badd(dpmax[d], dpnormal[d])
ret = badd(ret, mod - 1)
print(ret)

Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

The sum of the digits in base ten is a multiple of D.

Constraints

All values in input are integers.

1 \leq K < 10^{10000}

1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.

Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.

Sample Input 2

1000000009
1

Sample Output 2

2

Be sure to print the number modulo 10^9 + 7.

Sample Input 3

98765432109876543210
58

Sample Output 3

635270834


local a, b, c = io.read("*n", "*n", "*n")
local ret = 0
c = math.min(c, a + b + 1)
print(b + c)

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

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

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


local ior = io.read
local l, n = ior("*n", "*n")
local x = {}
for i = 1, n do
  x[i] = ior("*n")
end
local dpl, dpr = {}, {}
dpl[1 + 0] = 0
dpr[1 + 0] = l - x[n]
dpl[1 + 1] = x[1]
dpr[1 + 1] = 0

local altl, altr = {}, {}
local srcl, srcr, dstl, dstr = dpl, dpr, altl, altr
local mma = math.max

local function getlen(idx_from, idx_to)
  local len = x[idx_to] - x[idx_from]
  if(len < 0) then len = len + l end
  return len
end

for i = 2, n do
  dstl[1 + 0] = 0
  dstl[1 + 1] = srcr[1 + 0] + getlen(n + 1 - (i - 1), 1)
  for j = 2, i - 1 do
    dstl[1 + j] = mma(srcl[1 + j - 1] + getlen(j - 1, j), srcr[1 + j - 1] + getlen(n + 1 - (i - j), j))
  end
  dstl[1 + i] = srcl[1 + i - 1] + getlen(i - 1, i)

  dstr[1 + 0] = srcr[1 + 0] + getlen(n + 1 - i, n + 1 - (i - 1))
  for j = 1, i - 2 do
    dstr[1 + j] = mma(srcr[1 + j] + getlen(n + 1 - (i - j), n + 1 - (i - 1 - j)), srcl[1 + j] + getlen(n + 1 - (i - j), j))
  end
  dstr[1 + i - 1] = srcl[1 + i - 1] + getlen(n, i - 1)
  dstr[1 + i] = 0

  srcl, srcr, dstl, dstr = dstl, dstr, srcl, srcr

end

local max = 0
for k, v in pairs(srcl) do
  max = mma(max, v)
end
for k, v in pairs(srcr) do
  max = mma(max, v)
end
print(max)

Score : 800 points

Problem Statement

Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi.
Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.

Starting at his residence, Takahashi will repeat the following action:

If all trees are burnt, terminate the process.

Specify a direction: clockwise or counter-clockwise.

Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.

When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.

Find the longest possible total distance Takahashi walks during the process.

Partial Score

A partial score can be obtained in this problem:

300 points will be awarded for passing the input satisfying N \leq 2000.

Constraints

2 \leq L \leq 10^9

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

1 \leq X_1 < ... < X_N \leq L-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L N
X_1
:
X_N

Output

Print the longest possible total distance Takahashi walks during the process.

Sample Input 1

10 3
2
7
9

Sample Output 1

15

Takahashi walks the distance of 15 if the process goes as follows:

Walk a distance of 2 counter-clockwise, burn the tree at the coordinate 2 and stay there.

Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.

Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

Sample Input 2

10 6
1
2
3
6
7
9

Sample Output 2

27

Sample Input 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

Sample Output 3

1204124749


local n = io.read("*n")
local cnt = 0
while 0 < n do
  if n % 10 == 2 then cnt = cnt + 1 end
  n = n // 10
end
print(cnt)

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


local n = io.read("*n")
local a, b = io.read("*n", "*n")
local cnt = 0
for i = 1, n do
  local x, y = io.read("*n", "*n")
  if a <= x and b <= y then cnt = cnt + 1 end
end
print(cnt)

Score : 200 points

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.
The dimensions of the i-th material are A_i \times B_i (A_i vertically and B_i horizontally).

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \times W.
He is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.
When cutting a material, the cuts must be parallel to one of the sides of the material.
Also, the materials have fixed directions and cannot be rotated.
For example, a 5 \times 3 material cannot be used as a 3 \times 5 plate.

Out of the N materials, how many can produce an H \times W plate if properly cut?

Constraints

1 \leq N \leq 1000

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N H W
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

3 5 2
10 3
5 2
2 5

Sample Output 1

2

Takahashi wants a 5 \times 2 plate.

The dimensions of the first material are 10 \times 3. We can obtain a 5 \times 2 plate by properly cutting it.

The dimensions of the second material are 5 \times 2. We can obtain a 5 \times 2 plate without cutting it.

The dimensions of the third material are 2 \times 5. We cannot obtain a 5 \times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \times 2 plate.

Sample Input 2

10 587586158 185430194
894597290 708587790
680395892 306946994
590262034 785368612
922328576 106880540
847058850 326169610
936315062 193149191
702035777 223363392
11672949 146832978
779291680 334178158
615808191 701464268

Sample Output 2

8


N, P = io.read("*n","*n")
io.read()

i = 2
a = 1

while i^N <= P do
  v = i^N
  while P % v ==0 do
    P = P // v
    a = a * i
  end

  i = i + 1
end

print(a)

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end

local function getdivisorparts(x, primes)
  local prime_num = #primes
  local tmp = {}
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  while primepos <= prime_num and dv <= lim do
    if x % dv == 0 then
      local t = {}
      t.p = dv
      t.cnt = 1
      x = mfl(x / dv)
      while x % dv == 0 do
        x = mfl(x / dv)
        t.cnt = t.cnt + 1
      end
      table.insert(tmp, t)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    local t = {}
    t.p, t.cnt = x, 1
    table.insert(tmp, t)
  end
  return tmp
end

local n, p = io.read("*n", "*n")
local primes = getprimes(1000000)
local dvp = getdivisorparts(p, primes)
local ret = 1
for id = 1, #dvp do
  local v, cnt = dvp[id].p, dvp[id].cnt
  local z = cnt // n
  for i = 1, z do
    ret = ret * v
  end
end
print(ret)


local white=0
local counter=0
for i,_ in io.read():gmatch("B*W") do
    white=white+#i-1
    counter=counter+white
end
print(counter)

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


local n=io.read("n")
local a,a_map={},{}
for i=1,n do
    local x=io.read("n")
    a[i]=x
    a_map[x]=(a_map[x] or 0)+1
end
table.sort(a)

local counter=0
for i=n,2,-1 do
    local p=1
    while a[i]>=p do
        p=p*2
    end
    if a_map[a[i]] and a_map[a[i]]>0 then
        a_map[a[i]]=a_map[a[i]]-1
        if a_map[p-a[i]] and a_map[p-a[i]]>0 then
            a_map[p-a[i]]=a_map[p-a[i]]-1
            counter=counter+1
        end
    end
end
print(counter)

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


local N = io.read("n")
local A = {}
local S = {}
for i=1,N do
    local n = io.read("n")
    A[i] = n
    if not S[n] then
        S[n] = 0
    end
    S[n] = S[n] + 1
end
table.sort(A)

local function target(r)
    local r2 = r + 1
    local t = 2
    while t < r2 do
        t = t * 2
    end
    return t, t - r
end

local ans = 0
for i=N,1,-1 do
    local r = A[i]
    local t, c = target(r)
    local hit = false
    if S[c] then
        if r ~= c and S[r] > 0 and S[c] > 0 then
            ans = ans + 1
            S[r] = S[r] - 1
            S[c] = S[c] - 1
            hit = true
        elseif S[c] > 1 then
            ans = ans + 1
            S[c] = S[c] - 2
            hit = true
        end
    end
    --print(r, t, c, hit)
end
print(ans)


print((function (fh)
	local r,m = 0,0
	for _=1,fh:read("*n") do
		local t=fh:read("*n")
		r,m = r+t,m<t and t or m
	end
	return r-m/2
end)(io.stdin))

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


local n = tonumber(io.read())
local p = {}
local maxp = 0
local sum = 0
for i=1,n,1 do
  p[i] = tonumber(io.read())
  sum = sum + p[i]
  if p[i]>maxp then
    maxp = p[i]
  end
end
print(sum-maxp//2)


n,k=io.read("*n","*n","*l")
h={}
for i=1,n do
    h[i]=io.read("*n")
end
table.sort(h)

minth=10^9+1
for i=1,n-k+1 do
    minth=math.min(minth,h[i+k-1]-h[i])
end
print(minth)

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


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


local inp = io.read()
local n,k
for i=1,string.len(inp),1 do
  if inp:sub(i,i)==' ' then
    n = tonumber(inp:sub(1,i-1))
    k = tonumber(inp:sub(i+1,string.len(inp)))
  end
end
local h = {}
for i=1,n,1 do
  h[i] = tonumber(io.read())
end
table.sort(h)
local ans=1000000000
for i=1,n-k+1,1 do
  if ans>h[i+k-1]-h[i] then
    ans = h[i+k-1]-h[i]
  end
end
print(ans)


n=io.read("n")
x=io.read("n")
f={0}
for i=1,n do
	w={}
	table.insert(w,1)
	for i=1,#f do
		table.insert(w,f[i])
	end
	table.insert(w,0)
	for i=1,#f do
		table.insert(w,f[i])
	end
	table.insert(w,1)
	f=w
end
s=0
for i=1,x do
	if w[i]==0 then
		s=s+1
	end
end
print(s)

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.


local n = io.read("n")
-- local n = 999999999
local total = 0
local value_stack = {0}
local mark_stack = {0}
while true do
	local len = #value_stack
	local value = value_stack[len]
	local mark = mark_stack[len]
	if not value then
		break
	end
	table.remove(value_stack, len)
	table.remove(mark_stack, len)
	if value <= n then
		if mark == 7 then
			total = total + 1
		end
		table.insert(value_stack, value * 10 + 3)
		table.insert(mark_stack, mark | 1)
		table.insert(value_stack, value * 10 + 5)
		table.insert(mark_stack, mark | 2)
		table.insert(value_stack, value * 10 + 7)
		table.insert(mark_stack, mark | 4)
	end
end
print(total)

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


a=io.read()*1
if(a==5 or a==7 or a==3)then
	print("YES")
else
	print("NO")
end


local S = io.read("n")
local function take3(n)
    return ((S // math.floor(10^n)) % 1000)
end
local ans = 1000
for i=0,7 do
    ans = math.min(ans, math.abs(753 - take3(i)))
end
print(ans)

Score : 200 points

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?

Constraints

S is a string of length between 4 and 10 (inclusive).

Each character in S is 1, 2, ..., or 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

Sample Input 2

35753

Sample Output 2

0

If 753 itself can be taken out, the answer is 0.

Sample Input 3

1111111111

Sample Output 3

642

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.


local str = io.read()
local n = tonumber(str)
local min = 753
for i=1,#str-2 do
	local newmin = math.abs(753 - tonumber(string.sub(str,i,i)..string.sub(str,i+1,i+1)..string.sub(str,i+2,i+2)))
	if min > newmin then min = newmin end
end
print(min)


local n = io.read("*n")
local dignum = 0
local digits = {}
while 0 < n do
  table.insert(digits, n % 10)
  n = n // 10
  dignum = dignum + 1
end
local norm = {}
local maymax = {}

-- 0, 03, 05, 07, 035, 037, 057, 0357
for i = 1, 8 do
  norm[i] = {}
  maymax[i] = {}
  for j = 1, dignum do
    norm[i][j] = 0
    maymax[i][j] = 0
  end
end
local digval = digits[dignum]
norm[1][dignum] = 1
if 8 <= digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  norm[4][dignum] = 1
elseif 7 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  maymax[4][dignum] = 1
elseif 6 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
elseif 5 == digval then
  norm[2][dignum] = 1
  maymax[3][dignum] = 1
elseif 4 == digval then
  norm[2][dignum] = 1
elseif 3 == digval then
  maymax[2][dignum] = 1
end

for i = dignum - 1, 1, -1 do
  local digval = digits[i]
  norm[1][i] = 1
  -- 03, 05, 07
  for j = 2, 4 do
    local tgt = j * 2 - 1
    if tgt < digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif tgt == digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = maymax[j][i + 1]
    else
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = 0
    end
  end
  -- 035, 037, 057
  for j = 5, 7 do
    local rightval = j == 5 and 5 or 7
    local rightidx = j == 5 and 3 or 4
    local leftval = j == 7 and 5 or 3
    local leftidx = j == 7 and 3 or 2
    if rightval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[leftidx][i + 1] + maymax[rightidx][i + 1] + maymax[j][i + 1] * 2
      maymax[j][i] = 0
    elseif rightval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = maymax[leftidx][i + 1] + maymax[j][i + 1]
    elseif leftval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif leftval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = maymax[rightidx][i + 1] + maymax[j][i + 1]
    else
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = 0
    end
  end
  if 7 < digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[5][i + 1] + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 3
    maymax[8][i] = 0
  elseif 7 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = maymax[5][i + 1] + maymax[8][i + 1]
  elseif 6 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = 0
  elseif 5 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = maymax[6][i + 1] + maymax[8][i + 1]
  elseif 4 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = 0
  elseif 3 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = maymax[7][i + 1] + maymax[8][i + 1]
  else
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = 0
  end
end
print(norm[8][1] + maymax[8][1])

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


local mfl = math.floor

local n = io.read("*n", "*l")
local s = io.read()
local m_cnt = {}
local d_pos = {}
local c_pos = {}
local ss = ""
local mc = 0LL
for i = 1, n do
  ss = s:sub(i, i)
  if ss == "D" then
    table.insert(d_pos, i)
  elseif ss == "C" then
    table.insert(c_pos, i)
  elseif ss == "M" then
    mc = mc + 1LL
  end
  m_cnt[i] = mc
end
local m_sum_per_c = {}
for i = 1, #c_pos do
  local ic = c_pos[i]
  if i == 1 then
    m_sum_per_c[i] = m_cnt[ic]
  else
    m_sum_per_c[i] = m_sum_per_c[i - 1] + m_cnt[ic]
  end
end

local function llprint(llnumber)
  local str = tostring(llnumber):gsub("LL", "")
  print(str)
end

local cposall = {}
local tmp = 1
for i = 1, n + 1 do
  while c_pos[tmp] and c_pos[tmp] < i do
    tmp = tmp + 1
  end
  cposall[i] = tmp
end
local mmi = math.min
local q = io.read("*n")
for i = 1, q do
  local k = io.read("*n")
  local ret = 0LL
  for id = 1, #d_pos do
    local dp = d_pos[id]
    local c_from = cposall[dp]
    local c_to = cposall[mmi(n + 1, dp + k)] - 1
    if 1 <= c_to then
      ret = ret + m_sum_per_c[c_to] - (c_to - c_from + 1) * m_cnt[dp]
      if 1 < c_from then
        ret = ret - m_sum_per_c[c_from - 1]
      end
    end
  end
  llprint(ret)
end

Score : 600 points

Problem Statement

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.

The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

Given a string S of length N and an integer k (k \geq 3),
he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions:

0 \leq a < b < c \leq N - 1

S[a] = D

S[b] = M

S[c] = C

c-a < k

Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds.

For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1).

Constraints

3 \leq N \leq 10^6

S consists of uppercase English letters

1 \leq Q \leq 75

3 \leq k_i \leq N

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
S
Q
k_{0} k_{1} ... k_{Q-1}

Output

Print Q lines.
The i-th line should contain the k_i-DMC number of the string S.

Sample Input 1

18
DWANGOMEDIACLUSTER
1
18

Sample Output 1

1

(a,b,c) = (0, 6, 11) satisfies the conditions.

Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition.

Sample Input 2

18
DDDDDDMMMMMCCCCCCC
1
18

Sample Output 2

210

The number of triples can be calculated as 6\times 5\times 7.

Sample Input 3

54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40

Sample Output 3

0
1
2

(a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i.

By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".

Sample Output 4

30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20

Sample Output 4

10
52
110
140


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 n = io.read("*n", "*l")
local s = io.read()
local m_cnt = {}
local d_pos = {}
local c_pos = {}
local ss = ""
local mc = 0
for i = 1, n do
  ss = s:sub(i, i)
  if ss == "D" then
    table.insert(d_pos, i)
  elseif ss == "C" then
    table.insert(c_pos, i)
  elseif ss == "M" then
    mc = mc + 1
  end
  m_cnt[i] = mc
end
local c_froms = {}
for id = 1, #d_pos do
  c_froms[id] = lower_bound(c_pos, d_pos[id])
end
local q = io.read("*n")
for i = 1, q do
  local k = io.read("*n")
  local ret = 0
  for id = 1, #d_pos do
    local dp = d_pos[id]
    local c_from = c_froms[id]
    local c_to = lower_bound(c_pos, dp + k) - 1
    for ic = c_from, c_to do
      local cp = c_pos[ic]
      ret = ret + m_cnt[cp] - m_cnt[dp]
    end
  end
  print(ret)
end


x, y = io.read("*n", "*n")
print(x + math.floor(y / 2))

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


local inp = io.read()
t={}
string.gsub(inp,"(%S+)", function (v) table.insert(t,tonumber(v)) end)
print(t[1]+t[2]//2)


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

local near_index = 0
H[near_index] = 100000
for i = 1, N do
    if math.abs(A - (T - H[i] * 0.006))
    <  math.abs(A - (T - H[near_index] * 0.006)) then
        near_index = i
    end
end

print(near_index)

Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


local str = io.read("*l")
local tb = {}
for i = 1, string.len(str) do
    table.insert(tb,string.sub(str,i,i))
end

local finalA = io.read("*n")
local finalB = io.read("*n")
io.read()

local dirTb = {
    {0,1},
    {0,-1},
    {-1,0},
    {1,0}
}

local turnTb = {
    {3,4},
    {3,4},
    {1,2},
    {1,2}
}
function digui( a,b,idx,curDir )
    print(a.." "..b)
    if a == finalA and b == finalB then
        return true
    end

    if idx > #tb then
        return false
    end

    if a < 0 or a > 8000 or b < 0 or b > 8000 then
        return false
    end

    if tb[idx] == 'F' then
        return digui(a+dirTb[curDir][1],b+dirTb[curDir][2],idx+1,curDir)
    else
        return digui(a+dirTb[turnTb[curDir][1]][1],b+dirTb[turnTb[curDir][1]][2],idx+1,turnTb[curDir][1]) or digui(a+dirTb[turnTb[curDir][2]][1],b+dirTb[turnTb[curDir][2]][2],idx+1,turnTb[curDir][2])
    end

end

local f = digui(1,1,1,1) or digui(1,1,1,2) or digui(1,1,1,3) digui(1,1,1,4)
print(f and "yes" or "no")

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


function read()
    return io.read("*num")
end
n = read()
m = read()
a = {}
for i = 1, m do
    p = read()
    y = read()
    a[i] = {p = p, y = y, id = i}
end
table.sort(a, function(A, B)
                return A.y < B.y
            end)

a[1].c = 1
b = {}
for i = 2, m do
    if a[i].y > a[i - 1].y then
        a[i].c = a[i - 1].c + 1
    else
        a[i].c = a[i - 1].c
    end
end

for i = 1, m do
    b[a[i].id] = a[i]
end

for i = 1, m do
    str = string.format("%06d%06d", b[i].p, b[i].c)
    print(str)
end


local zones = io.read("*n")
local num = io.read("*n")
local data = {}
for j = 1,num do
	local idx = io.read("*n")
	local y = io.read("*n")
	local d = {n = 0, year = y,i=j,z = idx}
	table.insert(data,d)
end
local tmp = {}
table.sort(data,function(a,b)
 	return a.year < b.year
end)

-- for k,v in ipairs(data) do
-- 	v.k = k
-- end
for k,v in ipairs(data) do
	if not tmp[v.z] then
		tmp[v.z] = 1
	else
		tmp[v.z] = tmp[v.z] + 1
	end
	v.n = tmp[v.z]
end

local x = {}
for k,v in pairs(data) do
	x[v.i]={v.z,v.n}
end

for i = 1,#x do
	local digits = 0
	if x[i][1] < 100000 then
		local num = x[i][1]

		for i= 1,5 do
			num = num/10
			if num >= 10 then
				digits = digits + 1
			end
		end
	end
	x[i][1] = string.rep("0",5-digits)..x[i][1]
	if x[i][2] < 100000 then
		local num = x[i][2]
		for i= 1,5 do
			num = num/10
			if num >= 10 then
				digits = digits + 1
			end
		end
	end
	x[i][2] = string.rep("0",5-digits)..x[i][2]
	print(x[i][1]..x[i][2])
end


local sheng = io.read("*n")
local shi = io.read("*n")

local tab = {}
local tab1 = {}
local t = {}

for i=1,shi do
	local s1 = io.read("*n")
	local s2 = io.read("*n")
	if not tab[s1] then
		tab[s1] = {}
	end
	table.insert(tab[s1], {s1, s2, i})
	tab1[s1] = true
end

for v,_ in pairs(tab1) do
	table.sort(tab[v], function(a,b)
		return a[2] < b[2]
	end)
	for i,v in ipairs(tab[v]) do
		t[v[3]] = string.format("%06d%06d", v[1], i)
	end
end

for i,v in ipairs(t) do
	print(v)
end


S = io.read()

if #S == 2 then
  print(S)
elseif #S == 3 then
  print(string.reverse(S))
end

Score : 100 points

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

Constraints

The length of S is 2 or 3.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

Sample Input 1

abc

Sample Output 1

cba

As the length of S is 3, we print it after reversing it.

Sample Input 2

ac

Sample Output 2

ac

As the length of S is 2, we print it as is.


a, b, k = io.read("*n", "*n", "*n")
for i = 1, k do
  if i % 2 == 1 then
    if a % 2 == 1 then a = a - 1 end
    a = a // 2
    b = b + a
  else
    if b % 2 == 1 then b = b - 1 end
    b = b // 2
    a = a + b
  end
end
print(a .. " " .. b)

Score : 200 points

Problem Statement

In the beginning, Takahashi has A cookies, and Aoki has B cookies.
They will perform the following operation alternately, starting from Takahashi:

If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person.

Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total.

Constraints

1 \leq A,B \leq 10^9

1 \leq K \leq 100

A,B and K are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total.

Sample Input 1

5 4 2

Sample Output 1

5 3

The process will go as follows:

In the beginning, Takahashi and Aoki have 5 and 4 cookies, respectively.

Takahashi eats one cookie and gives two cookies to Aoki. They now have 2 and 6 cookies, respectively.

Aoki gives three cookies to Takahashi. They now have 5 and 3 cookies, respectively.

Sample Input 2

3 3 3

Sample Output 2

1 3

Sample Input 3

314159265 358979323 84

Sample Output 3

448759046 224379523


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

if n % 2 == 0 then
  local sum = 0
  local half = n / 2
  for i = 1, half do
    sum = sum - a[i] + a[half + i]
  end
  sum = sum * 2 - a[half + 1] + a[half]
  print(sum)
else
  local sum = 0
  local half = (n - 1) / 2
  for i = 1, half do
    sum = sum - a[i]
  end
  for i = half + 1, n do
    sum = sum + a[i]
  end
  sum = sum * 2 - a[half + 1] - a[half + 2]
  local cand = sum
  sum = 0
  half = (n + 1) / 2
  for i = 1, half do
    sum = sum - a[i]
  end
  for i = half + 1, n do
    sum = sum + a[i]
  end
  sum = sum * 2 + a[half - 1] + a[half]
  print(math.max(cand, sum))
end

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


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

local n,m=io.read("*n","*n","*l")
local s=io.read()
local t=io.read()

local l=n*m//gcd(n,m)
local ln=m//gcd(n,m)
local lm=n//gcd(n,m)

local x={}
for i=1,n do
    local p=(i-1)*ln+1
    x[p]=s:sub(i,i)
end

local bool=true
for i=1,m do
    local p=(i-1)*lm+1
    if x[p] and x[p]~=t:sub(i,i) then
        bool=false
        break
    end
end
print(bool and l or -1)

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


local mod = 1000000007
local mfl, mce = math.floor, math.ceil
local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y) return mod <= x + y and x + y - mod or x + y end
local n = io.read("*n")
local invs = {1}
local invsum = {1}
for i = 2, n do
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invsum[i] = badd(invsum[i - 1], invs[i])
end
local fact = 1
for i = 2, n do
  fact = bmul(fact, i)
end
local ret = 0
for i = 1, n do
  local a = io.read("*n")
  local mul = 1
  if 1 < i then
    mul = badd(mul, invsum[i])
    mul = badd(mul, mod - 1)
  end
  if i < n then
    mul = badd(mul, invsum[n + 1 - i])
    mul = badd(mul, mod - 1)
  end
  mul = bmul(mul, fact)
  ret = badd(ret, bmul(a, mul))
end
print(ret)

Score : 600 points

Problem Statement

There are N blocks arranged in a row, numbered 1 to N from left to right.
Each block has a weight, and the weight of Block i is A_i.
Snuke will perform the following operation on these blocks N times:

Choose one block that is still not removed, and remove it.
The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself).
Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed.

There are N! possible orders in which Snuke removes the blocks.
For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs.
As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7.

Sample Input 1

2
1 2

Sample Output 1

9

First, we will consider the order "Block 1 -> Block 2".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 2, as only Block 2 remains.
Thus, the total cost of the two operations for this order is 3+2=5.

Then, we will consider the order "Block 2 -> Block 1".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 1, as only Block 1 remains.
Thus, the total cost of the two operations for this order is 3+1=4.

Therefore, the answer is 5+4=9.

Sample Input 2

4
1 1 1 1

Sample Output 2

212

Sample Input 3

10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

880971923


a = io.read("*n")
if a == 1 then print("Hello World")
else b, c = io.read("*n", "*n") print(b + c)
end

Score: 100 points

Problem Statement

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.

One day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.

Takahashi, who is taking this exam, suddenly forgets his age.

He decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.

Write this program for him.

Constraints

N is 1 or 2.

A is an integer between 1 and 9 (inclusive).

B is an integer between 1 and 9 (inclusive).

Input

Input is given from Standard Input in one of the following formats:

1

2
A
B

Output

If N=1, print Hello World; if N=2, print A+B.

Sample Input 1

1

Sample Output 1

Hello World

As N=1, Takahashi is one year old. Thus, we should print Hello World.

Sample Input 2

2
3
5

Sample Output 2

8

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.


local n = tonumber(io.read())
if n==1 then
  print("Hello World")
else
  local a = tonumber(io.read())
  local b = tonumber(io.read())
  print(a+b)
end


local n = io.read()

if n == "1" then
  print("Hello World")
else
  local a = tonumber(io.read())
  local b = tonumber(io.read())
  print(a+b)
end


N = tonumber(io.read())
T = tonumber(io.read())

max = 2000
cnt = 0
ans = max
while cnt < N do
      c = tonumber(io.read())
      t = tonumber(io.read())
      if t < N and c < ans then
         ans = c
      end
end

if ans >= max then
  e1 = "TLE"
  print(e1)
else
  print(ans)
end

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


local n,m=io.read("n","n")
if m%n==0 then
    print(m//n)
else
    local d={}
    for i=1,math.sqrt(m) do
        if m%i==0 then
            table.insert(d,i)
            table.insert(d,m//i)
        end
    end
    table.sort(d)

    local max=0
    for _,v in pairs(d) do
        if v*n<=m then
            max=math.max(max,v)
        end
    end
    print(max)
end

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


local n,m=io.read("n","n")
if m%n==0 then
    print(m//n)
else
    local d=0
    for i=2,math.sqrt(m) do
        if m%i==0 and i<=m/n then
            d=i
        end
    end
    print(d)
end


s=io.read()
for i = 1, 3 do
  if s:sub(i,i) == "1" then io.write(9) else io.write(1) end
end
io.write("\n")

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


n=io.read("*n")
while 1 do
	s={}
	a=n
	cnt=0
	while a~=0 do
		if s[a%10]==nil then
			cnt=cnt+1
		end
		s[a%10]=1
		a=math.floor(a/10)
	end
	if cnt==1 then
		print(n)
		os.exit()
	end
	n=n+1
end


n=io.read("*n","*l")
v={}
for i=1,n do
    x=io.read("*n")
    if i%2==1 then
        if not v[x] then
            v[x]={1,0}
        else
            v[x][1]=v[x][1]+1
        end
    else
        if not v[x] then
            v[x]={0,1}
        else
            v[x][2]=v[x][2]+1
        end
    end
end

table.sort(v, function(a,b)
    return a[1]<b[1]
end)
max=0
omax2=0
for k,_ in pairs(v) do
    max=math.max(max,v[k][1])
    if v[k][1]~=v[k][2] then
        omax2=math.max(omax2,v[k][1])
    end
end

table.sort(v, function(a,b)
    return a[2]<b[2]
end)
emax2=0
for k,_ in pairs(v) do
    if v[k][1]~=v[k][2] then
        emax2=math.max(emax2,v[k][2])
    end
end

print(n-max-math.max(omax2,emax2))

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


-- for 300 points
local N = io.read("n")
local points = {}
for i=1,N do
    local t = {}
    t[1] = io.read("n")
    t[2] = io.read("n")
    table.insert(points, t)
    assert(math.abs(t[1]) <= 10)
    assert(math.abs(t[2]) <= 10)
end

local even = false
local odd = false
for i=1, N do
    local t = points[i]
    if (t[1] + t[2]) % 2 == 0 then
        even = true
    else
        odd = true
    end
end
if even and odd then
    print("-1")
    return
end

local m = 20
if odd then
    m = m + 1
end
local lengths = {}
for i=1,m do
    lengths[i] = 1
end

local modes = {}
for i=1, N do
    local move = {}
    local x, y = 0, 0
    local t = points[i]
    while x ~= t[1] do
        if t[1] < 0 then
            table.insert(move, "L")
            x = x - 1
        else
            table.insert(move, "R")
            x = x + 1
        end
        assert(#move <= m)
    end
    while y ~= t[2] do
        if t[2] < 0 then
            table.insert(move, "D")
            y = y - 1
        else
            table.insert(move, "U")
            y = y + 1
        end
        assert(#move <= m)
    end
    while #move < m do
        table.insert(move, "L")
        table.insert(move, "R")
        assert(#move <= m)
    end
    assert(#move == m)
    modes[i] = table.concat(move, "")
end

print(m)
print(table.concat(lengths, " "))
for i=1, N do
    print(modes[i])
end

Score : 600 points

Problem Statement

Snuke is introducing a robot arm with the following properties to his factory:

The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.

For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):

(x_0, y_0) = (0, 0).

If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).

If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).

If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).

If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).

Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.
Is this possible?
If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).

Constraints

All values in input are integers.

1 \leq N \leq 1000

-10^9 \leq X_i \leq 10^9

-10^9 \leq Y_i \leq 10^9

Partial Score

In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.

m
d_1 d_2 ... d_m
w_1
w_2
:
w_N

m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.
Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.

w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.

Sample Input 1

3
-1 0
0 3
2 -1

Sample Output 1

2
1 2
RL
UU
DR

In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:

To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).

To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).

To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).

Sample Input 2

5
0 0
1 0
2 0
3 0
4 0

Sample Output 2

-1

Sample Input 3

2
1 1
1 1

Sample Output 3

2
1 1
RU
UR

There may be duplicated points among (X_j, Y_j).

Sample Input 4

3
-7 -3
7 3
-3 -7

Sample Output 4

5
3 1 4 1 5
LRDUL
RDULR
DULRD


a = io.read()
b = io.read()
-- a = "abcdefghijklmnopqrstuvwxyz"
-- b = "ibyhqfrekavclxjstdwgpzmonu"
t1 = {}
t2 = {}
local function f(t, a, i)
	c = a:sub(i,i)
	t[c] = t[c] or 0
	t[c] = t[c] + 1
end
for i = 1, #a do
	f(t1, a, i)
	f(t2, b, i)
end

local function solve()
	for i = 1, #a do
		c1 = a:sub(i,i)
		c2 = b:sub(i,i)
		if t1[c1] ~= t2[c2] then
			return "No"
		end
	end
	return "Yes"
end
print(solve())

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


local s = {}
local t = {}
local check = {}

for v in string.gmatch(io.read(), "%w") do
  table.insert(s, v)
end

for v in string.gmatch(io.read(), "%w") do
  table.insert(t, v)
end

local result = "Yes"
for i = 1, #s do
  local c = check[t[i]]
  if c ~= nil and c ~= s[i] then
    result = "No"
    break
  else
    check[t[i]] = s[i]
  end
end

print(result)


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

local counter = 0
for i = 1, N do
    if x - A[i] >= 0 then
        x = x - A[i]
        counter = counter + 1
    end
end
print(counter)

Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.