N=io.read("n")
x={}
u={}
local total=0
for i=1,N do
  x[i]=io.read("n")
  u[i]=io.read():match("%a+")
  if u[i]=="BTC" then
    x[i]=380000*x[i]
  end
  total=total+x[i]
end
print(total)


N=io.read("n")
x={}
u={}
local total=0
for i=1,N do
  x[i],u[i]=io.read():match("(%d+)%s(%w+)")
  if u[i]=="BTC" then
    x[i]=380000*x[i]
  end
  total=total+x[i]
end
print(total)

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

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.

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.


N=io.read("n")
x={}
u={}
local total=0
for i=1,N do
  x[i]=io.read("n")
  u[i]=io.read()
  if u[i]=="BTC" then
    x[i]=380000*x[i]
  end
  total=total+x[i]
end
print(total)


n=io.read()*1
lis={JPY=1,BTC=380000.0}
ans=0
for i=1,n do
  a,b=io.read():match("(.+)%s(.+)")
  ans=a*lis[b]+ans
end
print(ans)


n=io.read()*1
lis={JPY=1,BTC=38000.0}
ans=0
for i=1,n do
  a,b=io.read():match("(.+)%s(.+)")
  ans=a*lis[b]+ans
end
print(ans)


local n = io.read("n")
local c = io.read("n")
local b = io.read("n")
local a = io.read("n")
function pay(x,y,z)
    return math.abs(x-a)+math.abs(y-b)+math.abs(z-c)
end
local mem = {}
function dfs(arr)
    local arrstr = table.concat(arr,":")
    if mem[arrstr] then
        return mem[arrstr]
    end
    mem[arrstr] = math.huge
    for i=1,#arr do
        for j=i+1, #arr do
            for k=j+1, #arr do
                cnt = pay(arr[i],arr[j],arr[k])
                if cnt < mem[arrstr] then
                    mem[arrstr] = cnt
                end
            end
        end
    end

    if #arr > 3 then
        for i=1,#arr do
            for j=i+1,#arr do
                local newarr = {}
                for k=1,#arr do
                    if k ~= i and k ~= j then
                        newarr[#newarr+1] = arr[k]
                    end
                end
                newarr[#newarr+1] = arr[i]+arr[j]
                table.sort(newarr)
                local newv = 10 + dfs(newarr)
                if newv < mem[arrstr] then
                    mem[arrstr] = newv
                end
            end
        end
    end

    return mem[arrstr]
end

local bamboos = {}

for i=1,n do
    bamboos[i] = io.read("n")
end
table.sort(bamboos)
print(dfs(bamboos))

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function tbl_clone(tbl)
    return {table.unpack(tbl)}
end

function parse_problem()
    local N = io.read("n")
    local goals = {}
    table.insert(goals, io.read("n"))
    table.insert(goals, io.read("n"))
    table.insert(goals, io.read("n"))
    local ingredients = {}
    for i=1,N do
        local l, _ = io.read("n", "l")
        table.insert(ingredients, l)
    end
    return goals, ingredients
end

function main()
    local goals, ingredients = parse_problem()
    local INF = 999999

    local function find_min()
        local function update(cur_lengths, k, x)
            -- Update cur_lengths.
            -- Ingredients k is used for x.
            local t = tbl_clone(cur_lengths)
            t[x] = t[x] + ingredients[k]
            return t
        end
        local function synthesis_cost(cur_lengths, x)
            -- Ingredients is used for x.
            if cur_lengths[x] == 0 then
                return 0
            else
                return 10
            end
        end
        local function find_min_rec(i, cur_lengths, cur_cost)
            -- i (0 <= i <= #ingredients): Number for decided ingredients.
            -- cur_lengths[k] (1 <= k <= 3): Current length for goal k.
            dbgpr(i, cur_cost)
            if i == #ingredients then
                for k=1,#goals do
                    if cur_lengths[k] == 0 then
                        return INF
                    end
                    cur_cost = cur_cost + math.abs(cur_lengths[k] - goals[k])
                end
                return cur_cost
            end
            local next_i = i + 1
            local visit_costs = {
                find_min_rec(next_i, update(cur_lengths, next_i, 1), cur_cost + synthesis_cost(cur_lengths, 1)),
                find_min_rec(next_i, update(cur_lengths, next_i, 2), cur_cost + synthesis_cost(cur_lengths, 2)),
                find_min_rec(next_i, update(cur_lengths, next_i, 3), cur_cost + synthesis_cost(cur_lengths, 3)),
                find_min_rec(next_i, tbl_clone(cur_lengths), cur_cost)
            }
            table.sort(visit_costs)
            return visit_costs[1]
        end
        return find_min_rec(0, {0, 0, 0}, 0)
    end
    local min_used_mp = find_min()
    print(min_used_mp)
end
main()


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 = (min + max) // 2
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local a, b, q = io.read("*n", "*n", "*n")
local s, t = {}, {}
for i = 1, a do
  s[i] = io.read("*n")
end
for i = 1, b do
  t[i] = io.read("*n")
end
local mab, mmi = math.abs, math.min
for iq = 1, q do
  local x = io.read("*n")
  ap = lower_bound(s, x)
  bp = lower_bound(t, x)
  local al = 1 < ap and s[ap - 1] or false
  local bl = 1 < bp and t[bp - 1] or false
  local ar = ap <= a and s[ap] or false
  local br = bp <= b and t[bp] or false
  local ret = 1000000007 * 100
  if al and bl then
    ret = mmi(ret, mab(x - al) + mab(al - bl), mab(x - bl) + mab(bl - al))
  end
  if al and br then
    ret = mmi(ret, mab(x - al) + mab(al - br), mab(x - br) + mab(br - al))
  end
  if ar and bl then
    ret = mmi(ret, mab(x - ar) + mab(ar - bl), mab(x - bl) + mab(bl - ar))
  end
  if ar and br then
    ret = mmi(ret, mab(x - ar) + mab(ar - br), mab(x - br) + mab(br - ar))
  end
  print(ret)
end

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

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

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


local ior = io.input()
local n, m = ior:read("*n", "*n")
local t = {}
for i = 1, m do t[i] = 0 end
for i = 1, n do
  local k = ior:read("*n")
  for j = 1, k do
    local a = ior:read("*n")
    t[a] = t[a] + 1
  end
end
local cnt = 0
for i = 1, m do
  if(t[i] == n) then cnt = cnt + 1 end
end
print(cnt)

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


N=io.read("n")
A={}
for i=1,N do
  A[i]=io.read("n")
end
function mod()
    while A[#A-1]~=0 do
        table.sort(A)
        local index=0
        for i=1,#A do
            if A[i]~=0 then
                if A[i]==1 then
                    return 1
                end
                index=i
                break
            end
        end
        for i=index,#A-1 do
            A[i+1]=A[i+1]%A[i]
        end
    end
    return A[#A]
end
print(mod())

Score : 300 points

Problem Statement

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

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


local n, m = io.read("*n", "*n")
local a = {}
for i = 1, 9 do a[i] = false end
local num_to_line = {2, 5, 5, 4, 5, 6, 3, 7, 6}
local topval = {0, 0, 0, 0, 0, 0, 0}
for i = 1, m do
  local tmp = io.read("*n")
  a[tmp] = true
  local line = num_to_line[tmp]
  topval[line] = math.max(topval[line], tmp)
end
local r = {}
local ins = table.insert
if a[1] then
  if n % 2 == 1 then
    local tmp = {7, 5, 3, 2, 8}
    for i = 1, 5 do
      if a[tmp[i]] then
        ins(r, tmp[i])
        n = n - num_to_line[tmp[i]]
        break
      end
    end
  end
  for i = 1, n / 2 do ins(r, 1) end
elseif a[7] then
  if n % 3 == 1 then
    if a[4] then ins(r, 4) n = n - 4
    elseif a[8] then ins(r, 8) n = n - 7
    else ins(r, topval[5]) ins(r, topval[5]) n = n - 10
    end
  elseif n % 3 == 2 then
    if 0 < topval[5] then ins(r, topval[5]) n = n - 5
    elseif a[4] then ins(r, 4) ins(r, 4) n = n - 8
    else ins(r, 8) ins(r, 8) n = n - 14
    end
  end
  for i = 1, n / 3 do ins(r, 7) end
elseif a[4] then
  if n % 4 == 1 then
    if 0 < topval[5] then ins(r, topval[5]) n = n - 5
    elseif 0 < topval[6] then ins(r, topval[6]) ins(r, 8) n = n - 13
    else ins(r, 8) ins(r, 8) ins(r, 8) n = n - 21
    end
  elseif n % 4 == 2 then
    if 0 < topval[6] then ins(r, topval[6]) n = n - 6
    elseif 0 < topval[5] then ins(r, topval[5]) ins(r, topval[5]) n = n - 10
    else ins(r, 8) ins(r, 8) n = n - 14
    end
  elseif n % 4 == 3 then
    if a[9] and 0 < topval[5] then ins(r, 9) ins(r, topval[5]) n = n - 11
    elseif a[8] then ins(r, 8) n = n - 7
    elseif a[6] then ins(r, 6) ins(r, topval[5]) n = n - 11
    else ins(r, topval[5]) ins(r, topval[5]) ins(r, topval[5]) n = n - 15
    end
  end
  for i = 1, n / 4 do ins(r, 4) end
elseif 0 < topval[5] then
  if n % 5 == 1 then
    if 0 < topval[6] then ins(r, topval[6]) n = n - 6
    else ins(r, 8) ins(r, 8) ins(r, 8) n = n - 21
    end
  elseif n % 5 == 2 then
    if a[9] then ins(r, 9) ins(r, 9) n = n - 12
    elseif a[8] then ins(r, 8) n = n - 7
    else ins(r, 6) ins(r, 6) n = n - 12
    end
  elseif n % 5 == 3 then
    if 0 < topval[6] and a[8] then ins(r, topval[6]) ins(r, 8) n = n - 13
    elseif 0 < topval[6] then ins(r, topval[6]) ins(r, topval[6]) ins(r, topval[6]) n = n - 18
    else ins(r, 8) ins(r, 8) ins(r, 8) ins(r, 8) n = n - 28
    end
  elseif n % 5 == 4 then
    if a[8] then ins(r, 8) ins(r, 8) n = n - 14
    else
      for i = 1, 4 do ins(r, topval[6]) end
    end
    n = n - 24
  end
  for i = 1, n / 5 do ins(r, topval[5]) end
elseif 0 < topval[6] then
  for i = 1, n % 6 do ins(r, 8) end
  n = n - 7 * (n % 6)
  for i = 1, n / 6 do ins(r, topval[6]) end
else
  for i = 1, n / 7 do ins(r, 8) end
end
table.sort(r, function(a, b) return a > b end)
print(table.concat(r, ""))

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654


Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


local N = io.read("n")
local L = {}
local max_len = 0
local sum = 0
for i=1,N do
    L[i] = io.read("n")
    max_len = math.max(L[i], max_len)
    sum = sum + L[i]
end
sum = sum - max_len
print(max_len < sum and "Yes" or "No")

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


local n = io.read("*n")
local sum = 0
local max = 0
for i = 1, n do
  local k = io.read("*n")
  sum, max = sum + k, math.max(max, k)
end
print(max * 2 < sum and "Yes" or "No")


N=io.read("n")
t={}
m=0
s=0
for i=1,N do
  t[i]=io.read("n")
  s=s+t[i]
  if m<t[i] then
    m=t[i]
  end
end
if s-m>m then
  print("Yes")
  else
  print("No")
end


n,m=io.read("*n","*n","*l")
if n>=m then
    print(0)
    return
end

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

l={}
for i=1,m-1 do
    table.insert(l,x[i+1]-x[i])
end
table.sort(l)

distance=0
for i=1,m-n do
    distance=distance+l[i]
end
print(distance)

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


local n, m = io.read("*n", "*n")
local a = {}
for i = 1, m do
  table.insert(a, io.read("*n"))
end
if(m <= n) then print(0) else
  table.sort(a)
  local ret = a[#a] - a[1]
  local diff = {}
  for i = 2, m do
    diff[i - 1] = a[i] - a[i - 1]
  end
  table.sort(diff, function(x, y) return x > y end)
  for i = 1, n - 1 do
    ret = ret - diff[i]
  end
  print(ret)
end


local a=io.read("n")
local b=io.read("n")
print(a*b//2)

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


local s = io.read("*n")
local t = {}
t[s] = 1
local p = 1
local cur = s
while true do
  p = p + 1
  local nxt = cur % 2 == 0 and cur // 2 or 3 * cur + 1
  if t[nxt] then
    print(p)
    break
  else
    t[nxt] = p
  end
  cur = nxt
end

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


local s=io.read("n")
t={}
t[1]=s
for i=2,1000000 do
  if t[i-1]%2==0 then
    t[i]=t[i-1]/2
    else
    t[i]=t[i-1]*3+1
    end
  if t[i]==1 then
    print(i+1)
    break
    end
  end


local s = io.read("n")
local i = 1
while true do
  if (s == 4 or s == 1 or s == 2) then
    print(i + 3)
    break
  end
  i = i + 1
  if (s % 2 == 0) then
    s = s // 2
  else
    s = s * 3 + 1
  end
end


local _i={}
for i=1,1 do
    local a=io.read()
    _i[i]={}
    for s in string.gmatch(a,"%S+")do
        _i[i][#_i[i]+1]=s
    end
end

local s=tonumber(_i[1][1])
if(s==1 or s==2)then
  print(4)
  return
end
local i=1
while true do
  if(s==4)then
  	print(i+3)
  	return
  elseif(s%2==0)then
  	s=s*.5
  else
  	s=3*s+1
  end
  i=i+1
end


local N=io.read("n")
h={}
for i=1,N do
  h[i]=io.read("n")
  end
a=1
s=0
while a<=N-s do
  if h[a]==h[a+1] then
    for i=a,N-1-s do
      h[i]=h[i+1]
      end
    s=s+1
    else
    a=a+1
    end
  end
if N-s==1 then
  print(h[1])
elseif N-s==2 then
  if h[1]>h[2] then
    print(h[1])
    else print(h[2])
    end
  else

D=0
d=0
if h[1]>h[2] then
  D=D+h[1]
  end
if h[N-s]>h[N-s-1] then
  D=D+h[N-s]
  end
for i=2,N-s-1 do
  if h[i]>h[i-1] and h[i]>h[i+1] then
  D=D+h[i]
  elseif h[i]<h[i-1] and h[i]<h[i+1] then
  d=d+h[i]
  end
end
print(D-d)
  end

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


local N=io.read("n")
h={}
for i=1,N do
  h[i]=io.read("n")
  end
a=1
s=0
while a<=N-s do
  if h[a]==h[a+1] then
    for i=a,N-1-s do
      h[i]=h[i+1]
      end
    s=s+1
    else
    a=a+1
    end
  end
D=0
d=0
if h[1]>h[2] then
  D=D+h[1]
  end
if h[N-s]>h[N-s-1] then
  D=D+h[N-s]
  end
for i=2,N-s-1 do
  if h[i]>h[i-1] and h[i]>h[i+1] then
  D=D+h[i]
  elseif h[i]<h[i-1] and h[i]<h[i+1] then
  d=d+h[i]
  end
end
print(D-d)


local _i={}
for i=1,2 do
    local a=io.read()
    _i[i]={}
    for s in string.gmatch(a,"%S+")do
        _i[i][#_i[i]+1]=tonumber(s)
    end
end

local n=_i[1][1]
local h=_i[2]
local rs=h[1]

for i=2,n do
  if(h[i-1]<h[i])then
    rs=rs+(h[i]-h[i-1])
  end
end
print(rs)


local N, K = io.read("n", "n")
local sushi_list = {}
--local sushi_types = {}
for i=1,N do
    local t, d = io.read("n", "n")
    local tuple = {t, d, false} -- 3rd element: used flag
    --local tuple = {t, d}
    sushi_list[i] = tuple
    --if not sushi_types[t] then
    --    sushi_types[t] = {}
    --end
    --table.insert(sushi_types[t], tuple)
end

-- sort order by deliciousness (most tasty sushi first)
table.sort(sushi_list, function (x, y) return x[2] > y[2] end)
--for _,sushi_t in pairs(sushi_types) do
--    table.sort(sushi_t, function (x, y) return x[2] > y[2] end)
--end

-- maximaize base score
local base_score = 0
local used = {}
for i=1,K do
    local tuple = sushi_list[i]
    base_score = base_score + tuple[2]
    if used[tuple[1]] == nil then
        used[tuple[1]] = 0
    end
    used[tuple[1]] = used[tuple[1]] + 1
    tuple[3] = true
end
local not_used_sushis = {}
local not_used_sushis_appeared = {}
for i=K+1,N do
    local tuple = sushi_list[i]
    if used[tuple[1]] == nil then
        if not_used_sushis_appeared[tuple[1]] then
            -- never use this one
        else
            not_used_sushis_appeared[tuple[1]] = true
            table.insert(not_used_sushis, tuple)
        end
    end
end
local ntypes = 0
for k,_ in pairs(used) do
    ntypes = ntypes + 1
end
local bonus_score = ntypes * ntypes
local score = base_score + bonus_score

if false then
    print("maximized base")
    for i,v in ipairs(sushi_list) do
        print(v[1],v[2],v[3])
    end
    print("not used sushis")
    for i,v in ipairs(not_used_sushis) do
        print(v[1],v[2],v[3])
    end
    print("--")
end

-- try increase bonus
local discard_i = K
local take_i = 1
local max_score = score
while true do
    -- discard least tasty one (constraint: keep ntypes)
    local discard_tuple = sushi_list[discard_i]
    if used[discard_tuple[1]] >= 2 then
        used[discard_tuple[1]] = used[discard_tuple[1]] - 1
        local take_tuple = not_used_sushis[take_i]
        take_i = take_i + 1
        local score_temp = (score - discard_tuple[2] + take_tuple[2]) - ntypes * ntypes + (ntypes + 1) * (ntypes + 1)
        if score_temp > max_score then
            max_score = score_temp
        end
        discard_tuple[3] = false
        take_tuple[3] = true
        score = score_temp
        ntypes = ntypes + 1
        if take_i > #not_used_sushis then
            break
        end
    else
        -- keep ntypes
    end
    discard_i = discard_i - 1
    if discard_i == 0 then
        break
    end
end

print(max_score)

Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

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

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

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


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 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 N, M = read.nn()
local adj_mat = array(2, nil)
local rev_adj_mat = array(2, nil)
for i=1,M do
    local x, y = read.nn()
    adj_mat[x][y] = true
    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 function make_queue()
    local q = {}
    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
    return push, pop, empty
end
local push, pop, empty = make_queue()
for n=1,N do
    if indeg_zero(n) then
        push(n)
    end
end

local lp_from = array(1, 0)
local ans = 0
while not empty() do
    local n = pop()
    insert(toposorted, n)
    for m in pairs(adj_mat[n]) do
        rev_adj_mat[m][n] = nil
        if indeg_zero(m) then
            push(m)
            --lp_from[m] = max(lp_from[m], lp_from[n] + 1)
            lp_from[m] = lp_from[n] + 1
            ans = max(ans, lp_from[m])
        end
    end
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:


r=io.read n,k=r("n","n")r()A=r()d={}for i=0,k do for a in A:gmatch"%d+"do d[i+a]=not d[i]and 1 end end print(d[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={}
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")


local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local dp = {}
local box = {}
local bzero = false
for i = 1, k do
  bzero = false
  for j = 1, n do
    box[j] = false
  end
  for j = 1, n do
    if t[j] < i then
      local v = dp[i - t[j]]
      if v == 0 then bzero = true
      else box[v] = true
      end
    elseif t[j] == i then
      bzero = true
    else
      break
    end
  end
  if not bzero then dp[i] = 0
  else
    for j = 1, n do
      if not box[j] then dp[i] = j break end
    end
  end
end
print(dp[k] == 0 and "Second" or "First")


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 = read.n()
local A = read.N(N)
local t = array(2)
for i=1,N do
    t[i][i] = A[i]
end
local function rec(i, j)
    if t[i][j] then
        return t[i][j]
    end
    local ri = A[j] - rec(i, j-1)
    local le = A[i] - rec(i+1, j)
    local ret = math.max(ri, le)
    t[i][j] = ret
    return ret
end
print(rec(1, N))

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

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 resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

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

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.


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

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

local mod=1000000007
for i=1,n do
    for j=2,k+1 do
        if j-1-a[i]>0 then
            dp[i+1][j]=(dp[i+1][j-1]+dp[i][j]-dp[i][j-1-a[i]])%mod
        else
            dp[i+1][j]=(dp[i+1][j-1]+dp[i][j])%mod
        end
    end
end
print(dp[n+1][k+1])

Score : 100 points

Problem Statement

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

They have decided to share K candies among themselves.
Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7.
Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq K \leq 10^5

0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.

Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.

Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

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


local mod = 1000000007
local function badd(x, y) return (x + y) % mod end
local function bsub(x, y) return y <= x and x - y or x + mod - y end
local n, k = io.read("*n", "*n")
local dp1, dp2 = {1}, {0}
for i = 2, k + 1 do
  dp1[i] = 0
  dp2[i] = 0
end
for i = 1, n do
  local src = i % 2 == 1 and dp1 or dp2
  local dst = i % 2 == 1 and dp2 or dp1
  for j = 1, k + 1 do
    dst[j] = 0
  end
  local a = io.read("*n") + 1
  local sum = 0
  for j = 1, k + 1 do
    sum = badd(sum, src[j])
    if a < j then
      sum = bsub(sum, src[j - a])
    end
    dst[j] = badd(dst[j], sum)
  end
end
local ret = n % 2 == 1 and dp2 or dp1
print(ret[k + 1])


local BIT={}

local update=function(a,b)
    if a==0 then
        return
    end
    while a<=#BIT do
        BIT[a]=math.max(BIT[a],b)
        a=a+(a&-a)
    end
end

local getmax=function(a)
    local sum=0
    while a>0 do
        sum=math.max(BIT[a],sum)
        a=a-(a&-a)
    end
    return sum
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

for i=1,n do
    BIT[i]=0
end
for i=1,n do
    update(h[i],getmax(h[i])+a[i])
end

print(getmax(n))

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 mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local bor = bit.bor
local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
  for j = 1, n do
    edge[i][j] = io.read("*n")
  end
end
if n == 1 then
  print(0)
  os.exit()
elseif n == 2 then
  print(mma(0, edge[1][2]))
  os.exit()
end

local all = bls(1, n)
local bdp = {}
local inf = 1000000007 * 1000
for i = 1, all - 1 do
  bdp[i] = -inf
end

local edgesum = {}
for it = 1, all - 1 do
  local tmp = it
  local box = {}
  for j = n, 1, -1 do
    if tmp % 2 == 1 then
      table.insert(box, j)
    end
    tmp = brs(tmp, 1)
  end
  local c = 0
  for i = 1, #box - 1 do
    for j = i + 1 , #box do
      c = c + edge[box[i]][box[j]]
    end
  end
  edgesum[it] = c
end

local base = 0
do
  -- use 1 and others
  local tot1 = bls(1, n - 1)
  base = base + tot1
  for it = 0, tot1 - 1 do
    local dst = base + it
    bdp[dst] = edgesum[dst]
  end
end
local map_devide_or1, map_devide_or2 = {}, {}
for it = 0, 3^(n - 3) - 1 do
  local tmpit = it
  local a, b = 0, 0
  local mul = 1
  for j = 4, n do
    if tmpit % 3 == 2 then
      a = a + mul
    elseif tmpit % 3 == 1 then
      b = b + mul
    end
    mul = mul * 2
    tmpit = mfl(tmpit / 3)
  end
  map_devide_or1[it + 1], map_devide_or2[it + 1] = a, b
end
--use i
for i = 2, n do
  local totrem = 3^(n - i)
  local basei = 2^(n - i)
  for it = 0, totrem - 1 do
    local add_from_i = 0
    local marge_src = 0
    if i == 2 then
      if 2 * (3^(n - 3)) <= it then
        marge_src = marge_src + 2^(n - 3)
        it = it - 2 * (3^(n - 3))
      elseif 3^(n - 3) <= it then
        add_from_i = add_from_i + 2^(n - 3)
        it = it - (3^(n - 3))
      end
    end
    add_from_i = add_from_i + basei + map_devide_or1[it + 1]
    marge_src = marge_src + base + map_devide_or2[it + 1]
    local dst = bor(add_from_i, marge_src)
    bdp[dst] = mma(bdp[dst], bdp[marge_src] + edgesum[add_from_i])
  end
  base = base + basei
end
print(bdp[all - 1])

Score : 100 points

Problem Statement

There are N rabbits, numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}.
Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).

Taro is dividing the N rabbits into some number of groups.
Here, each rabbit must belong to exactly one group.
After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.

Find Taro's maximum possible total score.

Constraints

All values in input are integers.

1 \leq N \leq 16

|a_{i, j}| \leq 10^9

a_{i, i} = 0

a_{i, j} = a_{j, i}

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 Taro's maximum possible total score.

Sample Input 1

3
0 10 20
10 0 -100
20 -100 0

Sample Output 1

20

The rabbits should be divided as \{1, 3\}, \{2\}.

Sample Input 2

2
0 -10
-10 0

Sample Output 2

0

The rabbits should be divided as \{1\}, \{2\}.

Sample Input 3

4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0

Sample Output 3

4999999999

The rabbits should be divided as \{1, 2, 3, 4\}.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 4

16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0

Sample Output 4

132


N, H, W = io.read("*n","*n","*n")
io.read()

c = 0

for v = 1, N do
  A, B = io.read("*n","*n")
  io.read()

  if A >= H and B >= W then
    c = c + 1
  end
end

print(c)

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 = 3
a = 1

while P % (2^N) == 0 and P ~= 0 do
  P = P // (2^N)
  a = a * 2
end

while i^N <= P do
  while P % (i^N) == 0 and P ~= 0 do
    P = P // (i^N)
    a = a * i
  end

  i = i + 2
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 n,p=io.read("n","n")
local x=math.floor(p^(1/n))
for i=x+1,1,-1 do
    if p%(i^n)==0 then
        print(i)
        return
    end
end


local n,p=io.read("n","n")
local x=0
for i=1,p^(1/5) do
    if p%(i^n)==0 then
        x=i
    end
end
print(x)


-- 左黒右白
local b = string.byte("B")

local s = io.read()
local s_len = s:len()
local s_bool_t = {}
for i = 1, s_len do
	if s:byte(i) == b then
		s_bool_t[i] = true
	else
		s_bool_t[i] = false
	end
end

local count_sum = 0
local count = 1
while count > 0 do
	count = 0
	for i = 1, s_len - 1 do
		if s_bool_t[i] and not s_bool_t[i + 1] then
			s_bool_t[i] = false
			s_bool_t[i + 1] = true
			count = count + 1
		end
	end
	count_sum = count_sum + count
end

print(count_sum)

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 s = io.read()
local t = {}
local n = #s
for i = 1, n do
  table.insert(t, s:sub(i, i) == "B")
end
local cnt = 0
local curcnt = 0
for i = n, 1, -1 do
  if t[i] then
    cnt = cnt + curcnt
    t[i] = false
  else
    curcnt = curcnt + 1
  end
end
print(cnt)


S = {}

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

C = 0
mc = 0
bf = 0

for i=2, #S do
  p = table.concat(S,"",i-1,i);
  if p == "BW" then
    mc = mc + 1
    C = C + bf
    bf = 0
    C = C + mc
  else
    if p ~= "WB" then
      mc = 0

      if p == "WW" then
        C = C + 1
      end
    end

    if p == "BB" then
      bf = 1
    else
      bf = 0
    end
  end
end

print(C)


S = {}

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

C = 0
mc = 0
bf = 0

for i=2, #S do
  p = table.concat(S,"",i-1,i);
  if p == "BW" then
    mc = mc + 1
    C = C + bf
    bf = 0
  else
    if p ~= "WB" then
      mc = 0

      if p == "WW" then
        C = C + 1
      end
    end

    if p == "BB" then
      bf = 1
    else
      bf = 0
    end
  end

  C = C + mc
end

print(C)


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local function lower_bound(ary, x, num)
  if num == 0 then return 1 end
  if(x <= ary[1].val) then return 1 end
  if(ary[num].val < x) then return num + 1 end
  local min, max = 1, num
  while(1 < max - min) do
    local mid = mfl((min + max) / 2)
    if(ary[mid].val < x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local function upper_bound(ary, x, num)
  if num == 0 then return 1 end
  if(x < ary[1].val) then return 1 end
  if(ary[num].val <= x) then return num + 1 end
  local min, max = 1, num
  while(1 < max - min) do
    local mid = mfl((min + max) / 2)
    if(ary[mid].val <= x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local n = io.read("*n", "*l")
local t = {}
local str = io.read()
for v in str:gmatch("%d+") do
  local tmp = {}
  tmp.val = tonumber(v)
  tmp.used = false
  table.insert(t, tmp)
end
table.sort(t, function(x, y) return x.val < y.val end)
local cnt = 0
for i = n, 2, -1 do
  if not t[i].used then
    t[i].used = true
    local v = t[i].val
    local mul = 2
    while mul <= v do
      mul = mul * 2
    end
    v = mul - v
    local lb, ub = lower_bound(t, v, i - 1), upper_bound(t, v, i - 1)
    for j = ub - 1, lb, -1 do
      if not t[j].used then
        cnt = cnt + 1
        t[j].used = true
        break
      end
    end
  end
end
print(cnt)

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 mfl = math.floor
local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end

local function solve(x)
  if x == 1 then
    for i = 1, n - 1 do
      if a[i + 1] <= a[i] then return false end
    end
    return true
  end
  local tasks = {}
  local ok = true
  for i = 1, n do
    if 0 == #tasks or tasks[#tasks][1] < a[i] then
      table.insert(tasks, {a[i], 1})
    else
      while 0 < #tasks and a[i] < tasks[#tasks][1] do
        table.remove(tasks)
      end
      if 0 == #tasks then
        table.insert(tasks, {a[i], 2})
      elseif tasks[#tasks][1] == a[i] then
        tasks[#tasks][2] = tasks[#tasks][2] + 1
      else
        table.insert(tasks, {a[i], 2})
      end
      while 0 < #tasks do
        if x < tasks[#tasks][2] then
          local pos = tasks[#tasks][1]
          if pos == 1 then
            ok = false
            break
          else
            table.remove(tasks)
            if 0 < #tasks and tasks[#tasks][1] == pos - 1 then
              tasks[#tasks][2] = tasks[#tasks][2] + 1
            else
              table.insert(tasks, {pos - 1, 2})
              break
            end
          end
        else
          break
        end
      end
      if 0 == #tasks or tasks[#tasks][1] < a[i] then
        table.insert(tasks, {a[i], 1})
      end
    end
    if not ok then break end
  end
  return ok
end

local min, max = 0, n
while 1 < max - min do
  local mid = mfl((max + min) / 2)
  if solve(mid) then
    max = mid
  else
    min = mid
  end
end
print(max)

Score : 700 points

Problem Statement

There are N strings arranged in a row.
It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right.
That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of different characters contained in the strings.

Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.

Sample Input 2

5
2 3 2 1 2

Sample Output 2

2


local n = io.read("n")
local max = 0
local total = 0
for i = 1, n do
  local p = io.read("n")
  total = total + p
  if p > max then
    max = p
  end
end
print(total - max//2)

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.


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]=t[k]-t[i]
  k=k+1
end
talbe.sort(t)
print(t[1])

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).


local n = io.read("n")
local k = io.read("n")
local list = {}
for i = 1, n do
  list[i] = io.read("n")
end
table.sort(list)
local min = nil
for i = 1, n - k + 1 do
  local d = list[i + k - 1] - list[i]
  if not min or d < min then
    min = d
  end
end
print(min)


local function memoize(f)
    local cache = {}
    local function key(...)
        local args = {...}
        local k = tostring(args[1])
        for i=2,#args do
            k = k .. "*" .. tostring(args[i])
        end
        return k
    end
    return function(...)
        local k = key(...)
        if cache[k] then
            return cache[k]
        end
        local r = f(...)
        cache[k] = r
        return r
    end
end

local thickness
thickness = memoize(function(n)
    if n == 0 then
        return 1
    else
        return 2 * thickness(n-1) + 3
    end
end)

local patties
patties = memoize(function (n, x)
    if n == 0 then
        return 1
    end
    local th_upper = thickness(n-1)
    if x == 1 then
        return 0
    elseif x <= th_upper + 1 then
        return patties(n-1, x-1)
    elseif x == th_upper + 2 then
        return 1 + patties(n-1, th_upper)
    else
        return 1 + patties(n-1, th_upper) + patties(n-1, x-2-th_upper)
    end
end)

local N, X = io.read("n", "n")
print(patties(N,X))

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.


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)


-- local n = io.read("n")
-- local k = io.read("n")
local n, k = 50, 4321098765432109
local g = {[0] = {1, 1}}
for i = 1, n do
	local c0 = g[i - 1][1]
	local p0 = g[i - 1][2]
	local c = c0 * 2 + 3
	local p = p0 * 2 + 1
	g[i] = {c, p}
end
local function f(_n, _k)
	if _k <= 0 then
		return 0
	end
	if _n <= 0 then
		return 1
	end
	local c = g[_n][1]
	local hc = (g[_n][1] + 1) // 2
	if _k >= c then
		return g[_n][2]
	elseif _k == hc then
		return f(_n - 1, _k - 2) + 1
	elseif _k < hc then
		return f(_n - 1, _k - 1)
	else
		return g[_n - 1][2] + 1 + f(_n - 1, _k - hc)
	end
end
print(f(n, k))


local n = io.read("n")
local k = io.read("n")
--local n, k = 50, 4321098765432109
local g = {[0] = {1, 1}}
for i = 1, n do
	local c0 = g[i - 1][1]
	local p0 = g[i - 1][2]
	local c = c0 * 2 + 3
	local p = p0 * 2 + 1
	g[i] = {c, p}
end
local function f(_n, _k)
	if _k <= 0 then
		return 0
	end
	if _n <= 0 then
		return 1
	end
	local c = g[_n][1]
	local hc = (g[_n][1] + 1) // 2
	if _k >= c then
		return g[_n][2]
	elseif _k == hc then
		return f(_n - 1, _k - 2) + 1
	elseif _k < hc then
		return f(_n - 1, _k - 1)
	else
		return f(_n - 1, _k - 1) + 1 + f(_n - 1, _k - hc)
	end
end
print(f(n, k))


X=io.read("n")
if X==3 or X==5 or X==7 then
  print("YES")
  else
  print("NO")
end

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.


local n=io.read("n")
local t={3,5,7}
local i=1
while t[#t]<=n do
    for j=3,7,2 do
        t[#t+1]=t[i]*10+j
    end
    i=i+1
end
local counter=0
for i=1,#t do
    local three=false
    local five=false
    local seven=false
    local copy=t[i]
    while copy>0 do
        if copy%10==3 then three=true
        elseif copy%10==5 then five=true
        elseif copy%10==7 then seven=true
        end
        copy=math.floor(copy/10)
    end
    if three and five and seven and t[i]<=n then
        counter=counter+1
    end
end
print(counter)

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 n = io.read("n")
-- local n = 3600
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)


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


local mfl, mce, msq = math.floor, math.ceil, math.sqrt
local n = io.read("*n")

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 primes = getprimes(47)
local t = {}
for i = 1, 47 do
  t[i] = 0
end
for i = 1, #primes do
  local p = primes[i]
  local ctot = 0
  local c = mfl(n / p)
  while 0 < c do
    ctot = ctot + c
    c = mfl(c / p)
  end
  t[p] = ctot
end

local ov74, ov24, ov14, ov4, ov2 = 0, 0, 0, 0, 0
for i = 1, 47 do
  if 74 <= t[i] then ov74 = ov74 + 1 end
  if 24 <= t[i] then ov24 = ov24 + 1 end
  if 14 <= t[i] then ov14 = ov14 + 1 end
  if 4 <= t[i] then ov4 = ov4 + 1 end
  if 2 <= t[i] then ov2 = ov2 + 1 end
end
local ret = ov74 + ov24 * (ov2 - 1) + ov14 * (ov4 - 1) + mfl(ov4 * (ov4 - 1) / 2) * (ov2 - 2)
print(ret)

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


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 = 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 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 = 0LL
  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
    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


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 n = tonumber(io.read())
local inp = io.read()
local T,a
for i=1,string.len(inp),1 do
  if inp:sub(i,i)==' ' then
    T = tonumber(inp:sub(1,i-1))
    a = tonumber(inp:sub(i+1,string.len(inp)))
  end
end
local hline = io.read()
h={}
string.gsub(hline,"(%S+)", function (v) table.insert(h,tonumber(v)) end)
t={}
for i=1,n,1 do
  t[i]=T-0.006*h[i]
end
res = 1
ti = t[1]
for i=1,n,1 do
  if math.abs(a-ti)>math.abs(a-t[i]) then
    res = i
    ti = t[i]
  end
end
print(res)

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 mod = 1000000007

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

local h, w, k = io.read("*n", "*n", "*n")
if w == 1 then
  print(1)
else
  local t = {1}
  for i = 2, w do t[i] = 0 end
  local tot = 2^(w - 1)
  local map = {}
  for i = 1, w do
    map[i] = {}
    for j = 1, w do
      map[i][j] = 0
    end
  end
  for it = 0, tot - 1 do
    local valid = true
    local flag = {}
    for i = 1, w - 1 do
      flag[i] = it % 2 == 1
      it = it // 2
    end
    for i = 1, w - 2 do
      if flag[i] and flag[i + 1] then
        valid = false
      end
    end
    if valid then
      for i = 1, w do
        if i == 1 then
          if flag[1] then
            map[1][2] = map[1][2] + 1
          else
            map[1][1] = map[1][1] + 1
          end
        elseif i == w then
          if flag[w - 1] then
            map[w][w - 1] = map[w][w - 1] + 1
          else
            map[w][w] = map[w][w] + 1
          end
        else
          if flag[i] then
            map[i][i + 1] = map[i][i + 1] + 1
          elseif flag[i - 1] then
            map[i][i - 1] = map[i][i - 1] + 1
          else
            map[i][i] = map[i][i] + 1
          end
        end
      end
    end
  end
  for ih = 1, h do
    local nt = {}
    for i = 1, w do nt[i] = 0 end
    for src = 1, w do
      for dst = 1, w do
        nt[dst] = badd(nt[dst], bmul(t[src], map[src][dst]))
      end
    end
    for i = 1, w do t[i] = nt[i] end
  end
  print(t[k])
end

Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

A valid amidakuji is an amidakuji that satisfies the following conditions:

No two horizontal lines share an endpoint.

The two endpoints of each horizontal lines must be at the same height.

A horizontal line must connect adjacent vertical lines.

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

Sample Input 6

15 8 5

Sample Output 6

437760187

Be sure to print the answer modulo 1\ 000\ 000\ 007.


local mod = 1000000007
local mfl = math.floor
local h, w, K = io.read("*n", "*n", "*n")

local linepat = 1
for i = 1, w - 1 do
  linepat = linepat * 2
end
local linetbl = {}
for i = 0, linepat - 1 do
  local lineidx, mul = 0, 1
  local box = {}
  for j = 1, w - 1 do box[j] = false end
  for j = 1, w - 1 do
    if i % 2 == 1 then
      box[j] = true
      lineidx = lineidx + mul
      i = i - 1
    end
    i = i / 2
    mul = mul * 2
  end
  local isok = true
  for j = 1, w - 2 do
    if box[j] and box[j + 1] then isok = false break end
  end
  if isok then table.insert(linetbl, lineidx) end
end
-- for i = 1, #linetbl do print(linetbl[i]) end

local t, u = {}, {}
for i = 1, w do t[i], u[i] = 0, 0 end
t[1] = 1
for i = 1, h do
  for j = 1, w do u[j] = t[j] end
  for j = 2, #linetbl do
    local line_i = linetbl[j]
    local k = 1
    while k <= w - 1 do
      if line_i % 2 == 1 then
        u[k] = (u[k] + t[k + 1]) % mod
        u[k + 1] = (u[k + 1] + t[k]) % mod
        line_i = mfl((line_i - 1) / 4)
        k = k + 2
        if k == w then u[w] = (u[w] + t[w]) % mod end
      else
        u[k] = (u[k] + t[k]) % mod
        if k == w - 1 then u[k + 1] = (u[k + 1] + t[k + 1]) % mod end
        k = k + 1
        line_i = line_i / 2
      end
    end
  end
  for j = 1, w do t[j] = u[j] end
end
print(t[K])


a, b, k = io.read("*n", "*n", "*n")
for i = 1, k do
  if a % 2 == 1 then a = a - 1 end
  a = a // 2
  b = b + a
  if b % 2 == 1 then b = b - 1 end
  b = b // 2
  a = a + b
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 = 0
for i = 1, 1000 do
  if i * (i + 1) == n * 2 then
    a = i
    break
  end
end
if a == 0 then
  print("No")
else
  print("Yes")
  print(a + 1)
  local t = {}
  for i = 1, a + 1 do
    t[i] = {}
    for j = 1, a do t[i][j] = 0 end
  end
  local cur = 1
  for i = 1, a do
    for j = i, a do
      t[i][j] = cur + j - i
      t[j + 1][i] = cur + j - i
    end
    cur = cur + a + 1 - i
  end
  for i = 1, a + 1 do
    print(a .. " " .. table.concat(t[i], " "))
  end
end

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

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

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


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
    x[(i-1)*ln+1]=s:sub(i,i)
end

for i=1,m do
    if not x[(i-1)*lm+1] then
        x[(i-1)*lm+1]=t:sub(i,i)
    elseif x[(i-1)*lm+1]~=t:sub(i,i) then
        print(-1)
        return
    end
end
print(l)

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


function gcd(u, v)
  while v ~= 0 do
    u, v = v, u%v
  end
  return u
end

function lcm(u,v)
  return u * v / gcd(u, v)
end

N, M = string.match(io.read(), "(%d) (%d)")

L = lcm(N,M)
ans = L

oS = L/N
oT = L/M

X = {}
p = 1
for v = 1, N do
  X[p] = io.read(1)
  p = p + oS
end
io.read()

p = 1
for v = 1, M do
  c = io.read(1)
  if not (X[p] == nil or X[p] == c) then
    ans = -1
    break
  end
  p = p + oT
end
io.read()

print(string.format("%d",ans))


local n = io.read("n")
if n==1 then
    print('Hello World')
else
    local A, B = io.read("n", "n")
    print(A+B)
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.


N=io.read("n")
if N==1 then
  print("Hello world")
  elseif N==2 then
  	A=io.read("n")
  	B=io.read("n")
  	print(A+B)
end


str = io.read()
if str == "1" then
  e1 = "Hello World"
  print(e1)
else
  a = io.read()
  b = io.read()
  ans = a + b
  print(ans)
end


local n, t = io.read("*n", "*n")
local min = nil
for i = 1, n do
  local a, b = io.read("*n", "*n")
  if b <= t then
    if not min then min = a
    else min = math.min(min, a)
    end
  end
end
if min then print(min) else print("TLE") 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


N=io.read("n")
T=io.read("n")
c={}
t={}
for i=1,N do
  c[i]=io.read("n")
  t[i]=io.read("n")
end
s="TLE"
for i=1,N do
  if t[i]<T then
    s=math.max(table.unpack(c))
    break
  end
end
for i=1,N do
  if t[i]<T and s>c[i] then
    s=c[i]
  end
end
print(s)


local n = io.read("n")
local m = io.read("n")
local mm = math.ceil(m ^ 0.5)
local max = 0
for i = 1, mm do
	if m % i == 0 then
		local a = i
		local b = m // a
		if b >= n then
			max = math.max(a, max)
		end
		if a >= n then
			max = math.max(b, max)
		end
	end
end
print(max)

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 = io.read("n")
local m = io.read("n")
local mm = math.ceil(m ^ 0.5)
local max = 0
for i = 1, mm do
	if m % i == 0 then
		local a = i
		local b = m // a
		if b >= n then
			max = math.max(a, max)
		end
	end
end
print(max)


function input()
  local inp = io.read()
  local r={}
  string.gsub(inp,"(%S+)", function (v) table.insert(r,tonumber(v)) end)
  return r
end

local n,m
local inp = input()
n = inp[1]
m = inp[2]
local b={}
local cnt = 1
local i=1
while i*i<=m do
  if m%i==0 then
    b[cnt]=i
    cnt = cnt+1
    b[cnt]=m//i
    cnt = cnt+1
  end
  i=i+1
end
local res = 1
for i=1,cnt-1,1 do
  if b[i]*n<=m then
    res = math.max(res,b[i])
  end
end
print(res)


s=io.read()
for i=1,#s do
	if s:sub(i,i)=='1' then io.write(9)
	else io.write(1) end
end

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