local m = io.read("*n")
local digsum = 0
local quot = 0
local rem = 0
for i = 1, m do
  local d, c = io.read("*n", "*n")
  digsum = digsum + c
  rem = rem + d * c
  if 9 < rem then
    if rem % 9 == 0 then
      quot = rem // 9 + quot - 1
      rem = 9
    else
      quot = rem // 9 + quot
      rem = rem % 9
    end
  end
end
print(digsum - 1 + quot)

Score: 500 points

Problem Statement

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

The preliminary stage consists of several rounds, which will take place as follows:

All the N contestants will participate in the first round.

When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:

The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.

For example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).

When X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.

The preliminary stage ends when 9 or fewer contestants remain.

Ringo, the chief organizer, wants to hold as many rounds as possible.
Find the maximum possible number of rounds in the preliminary stage.

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \ldots, and the last c_M digits are all d_M.

Constraints

1 \leq M \leq 200000

0 \leq d_i \leq 9

d_1 \neq 0

d_i \neq d_{i+1}

c_i \geq 1

2 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

M
d_1 c_1
d_2 c_2
:
d_M c_M

Output

Print the maximum possible number of rounds in the preliminary stage.

Sample Input 1

2
2 2
9 1

Sample Output 1

3

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

Sample Input 2

3
1 1
0 8
7 1

Sample Output 2

9

In this case, 1000000007 will participate in the first round.


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

Score : 100 points

Problem Statement

Given is an integer r.

How many times is the area of a circle of radius r larger than the area of a circle of radius 1?

It can be proved that the answer is always an integer under the constraints given.

Constraints

1 \leq r \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r

Output

Print the area of a circle of radius r, divided by the area of a circle of radius 1, as an integer.

Sample Input 1

2

Sample Output 1

4

The area of a circle of radius 2 is 4 times larger than the area of a circle of radius 1.

Note that output must be an integer - for example, 4.0 will not be accepted.

Sample Input 2

100

Sample Output 2

10000

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"
io.read()
S=io.read()
print((
	S:sub(1,N/2)==S:sub(-N/2,-1)
)and"Yes"or"No")

Score : 200 points

Problem Statement

Given are a positive integer N and a string S of length N consisting of lowercase English letters.

Determine whether the string is a concatenation of two copies of some string.
That is, determine whether there is a string T such that S = T + T.

Constraints

1 \leq N \leq 100

S consists of lowercase English letters.

|S| = N

Input

Input is given from Standard Input in the following format:

N
S

Output

If S is a concatenation of two copies of some string, print Yes; otherwise, print No.

Sample Input 1

6
abcabc

Sample Output 1

Yes

Let T = abc, and S = T + T.

Sample Input 2

6
abcadc

Sample Output 2

No

Sample Input 3

1
z

Sample Output 3

No


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 factorial(n)
    local a = 1
    for i=1, n do
        a = a * i
    end
    return a
end

local function perm(seq)
    local up = table.unpack or unpack
    local t = {up(seq)}
    return coroutine.wrap(function()
        local function rec(i)
            if i > #t then
                --coroutine.yield({up(t)})
                coroutine.yield(t)
                return
            end
            rec(i+1)
            for j=i+1,#t do
                t[i], t[j] = t[j], t[i]
                rec(i+1)
                t[i], t[j] = t[j], t[i]
            end
        end
        rec(1)
    end)
end

-- C
local N = read.n()
local X, Y = {}, {}
for i=1,N do
    X[i], Y[i] = read.nn()
end

local route_num = factorial(N)

local sum = 0
local set = {}
for i=1,N do set[i] = i end
for route in perm(set) do
    for n=1,N-1 do
        local i, j = route[n], route[n+1]
        local d = math.sqrt((X[i]-X[j])^2 + (Y[i]-Y[j])^2)
        sum = sum + d
    end
end

print(sum / route_num)

Score : 300 points

Problem Statement

There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \sqrt{\left(x_i-x_j\right)^2+\left(y_i-y_j\right)^2}.

There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) (if i \neq j)

(Added 21:12 JST) All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the average length of the paths.
Your output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

There are six paths to visit the towns: 1 → 2 → 3, 1 → 3 → 2, 2 → 1 → 3, 2 → 3 → 1, 3 → 1 → 2, and 3 → 2 → 1.

The length of the path 1 → 2 → 3 is \sqrt{\left(0-1\right)^2+\left(0-0\right)^2} + \sqrt{\left(1-0\right)^2+\left(0-1\right)^2} = 1+\sqrt{2}.

By calculating the lengths of the other paths in this way, we see that the average length of all routes is:

\frac{\left(1+\sqrt{2}\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)}{6} = 2.276142...

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

There are two paths to visit the towns: 1 → 2 and 2 → 1. These paths have the same length.

Sample Input 3

8
-406 10
512 859
494 362
-955 -475
128 553
-986 -885
763 77
449 310

Sample Output 3

7641.9817824387


local mod = 1000000007

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

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

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

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

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

local function getComb(n, k)
  local ret = 1
  local inv = 1
  for i = 1, k do
    ret = bmul(ret, n + 1 - i)
    inv = bmul(inv, i)
  end
  return bmul(ret, modinv(inv))
end

local x, y = io.read("*n", "*n")
if (x + y) % 3 ~= 0 then
  print(0) os.exit()
end
local z = (x + y) // 3
x, y = x - z, y - z
if x < 0 or y < 0 then
  print(0) os.exit()
end
print(getComb(x + y, x))

Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.


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

local function solve()
  if t == 1 then
    local t1ret = b[1]
    for i = 2, n do
      t1ret = mma(t1ret, b[i])
    end
    return t1ret
  end
  local totret = 0
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    for j = 1, t - 1 do
      tail[i][j] = 0
    end
  end
  local curmax = 0
  for i = n, 1, -1 do
    for j = 1, curmax do
      tail[i][j] = mma(tail[i][j], tail[i + 1][j])
      if j + a[i] < t then
        tail[i][j + a[i]] = mma(tail[i][j + a[i]], tail[i + 1][j] + b[i])
        curmax = mma(curmax, j + a[i])
      end
    end
    if a[i] < t then
      tail[i][a[i]] = mma(tail[i][a[i]], b[i])
      curmax = mma(curmax, a[i])
    end
  end
  local head = {}
  for i = 1, t - 1 do
    head[i] = 0
  end
  curmax = 0
  for i = 1, n - 1 do
    local headmax = {}
    headmax[1] = head[1]
    for j = 2, t - 1 do
      headmax[j] = mma(head[j], headmax[j - 1])
    end
    local tailmax = {}
    tailmax[1] = tail[i + 1][1]
    for j = 2, t - 1 do
      tailmax[j] = mma(tail[i + 1][j], tailmax[j - 1])
    end
    local iret = 0
    if 1 < t then iret = mma(headmax[t - 1], tailmax[t - 1]) end
    for j = 1, t - 2 do
      iret = mma(iret, headmax[j] + tailmax[t - 1 - j])
    end
    iret = iret + b[i]
    totret = mma(totret, iret)
    for j = t - 1 - a[i], 1, -1 do
      head[j + a[i]] = mma(head[j + a[i]], head[j] + b[i])
    end
    if a[i] < t then
      head[a[i]] = mma(head[a[i]], b[i])
    end
  end
  do -- i == n
    local headmax = {}
    headmax[1] = head[1]
    for j = 2, t - 1 do
      headmax[j] = mma(head[j], headmax[j - 1])
    end
    local iret = 1 < t and headmax[t - 1] or 0
    iret = iret + b[n]
    totret = mma(totret, iret)
  end
  return totret
end
print(solve())

Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145


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

local function solve()
  if t == 1 then
    local t1ret = b[1]
    for i = 2, n do
      t1ret = mma(t1ret, b[i])
    end
    return t1ret
  end
  local totret = 0
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    for j = 1, t - 1 do
      tail[i][j] = 0
    end
  end
  local curmax = 0
  for i = n, 1, -1 do
    for j = 1, curmax do
      tail[i][j] = mma(tail[i][j], tail[i + 1][j])
      if j + a[i] < t and 0 < tail[i + 1][j] then
        tail[i][j + a[i]] = mma(tail[i][j + a[i]], tail[i + 1][j] + b[i])
        curmax = mma(curmax, j + a[i])
      end
    end
    if a[i] < t then
      tail[i][a[i]] = mma(tail[i][a[i]], b[i])
      curmax = mma(curmax, a[i])
    end
  end
  local head = {}
  for i = 1, t - 1 do
    head[i] = 0
  end
  curmax = 0
  for i = 1, n - 1 do
    local headmax = {}
    headmax[1] = head[1]
    for j = 2, t - 1 do
      headmax[j] = mma(head[j], headmax[j - 1])
    end
    local tailmax = {}
    tailmax[1] = tail[i + 1][1]
    for j = 2, t - 1 do
      tailmax[j] = mma(tail[i + 1][j], tailmax[j - 1])
    end
    local iret = mma(headmax[t - 1], tailmax[t - 1])
    for j = 1, t - 2 do
      iret = mma(iret, headmax[j] + tailmax[t - 1 - j])
    end
    iret = iret + b[i]
    totret = mma(totret, iret)
    for j = t - 1 - a[i], 1, -1 do
      if 0 < head[j] then
        head[j + a[i]] = mma(head[j + a[i]], head[j] + b[i])
      end
    end
  end
  do -- i == n
    local headmax = {}
    headmax[1] = head[1]
    for j = 2, t - 1 do
      headmax[j] = mma(head[j], headmax[j - 1])
    end
    local iret = headmax[t - 1]
    iret = iret + b[n]
    totret = mma(totret, iret)
  end
  return totret
end
print(solve())


local mmi, mma = math.min, math.max
local n, k = io.read("*n", "*n")
local h = {0}
for i = 1, n do
  h[i + 1] = io.read("*n")
end
h[n + 2] = 0
local hsum = {h[1]}
for i = 2, n + 2 do
  hsum[i] = hsum[i - 1] + h[i]
end
local t = {}
t[1] = {}
for i = 1, k + 1 do
  t[1][i] = 0
end
for i = 2, n + 1 do
  t[i] = {}
  for j = 1, k + 1 do
    t[i][j] = t[i - 1][j]
  end
  if h[i - 1] < h[i] and h[i + 1] < h[i] then
    local right = h[i + 1]
    local left = h[i]
    for j = i - 1, 1, -1 do
      if left <= h[j] then
        break
      end
      local len = i - j
      local score = hsum[i] - hsum[j]
      if h[j] <= right then
        score = score - len * right
        for z = len + 1, k + 1 do
          t[i][z] = mma(t[i][z], t[j][z - len] + score)
        end
        break
      else
        score = score - len * h[j]
        for z = len + 1, k + 1 do
          t[i][z] = mma(t[i][z], t[j][z - len] + score)
        end
      end
      left = h[j]
    end
  end
  for j = 2, k + 1 do
    t[i][j] = mma(t[i][j], t[i][j - 1])
  end
end
local ret = -t[n + 1][k + 1]
for i = 2, n + 1 do
  ret = ret + mma(0, h[i] - h[i - 1])
end
print(ret)

Score : 600 points

Problem Statement

We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.

The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.

Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).

Different values can be chosen for different columns.

Then, you will create the modified artwork by repeating the following operation:

Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)

Find the minimum number of times you need to perform this operation.

Constraints

1 \leq N \leq 300

0 \leq K \leq N

0 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 H_2 ... H_N

Output

Print the minimum number of operations required.

Sample Input 1

4 1
2 3 4 1

Sample Output 1

3

For example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:

Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.

Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.

Paint black the 2-nd square from the left in the 3-rd row from the bottom.

Sample Input 2

6 2
8 6 9 1 2 1

Sample Output 2

7

Sample Input 3

10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000

Sample Output 3

4999999996


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

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

table.sort(t, function(x, y) return x[1] < y[1] end)
local st = SegTree.new(n, mmi, 99999999999999)
st:setRange(1, 1, 0)
for i = 1, m do
  st:resolve(t[i][1])
  local lval = st:getValue(t[i][1])
  st:setRange(t[i][1] + 1, t[i][2], lval + t[i][3])
end
st:resolve(n)
print(99999999999999 <= st:getValue(n) and -1 or st:getValue(n))

Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

28


local S = io.read()
local a = {}
a[#S+1] = 0
for i=#S,1,-1 do
  local c = S:sub(i,i)
  if c == '>' then
    a[i] = a[i+1] + 1
  else
    a[i] = 0
  end
end
for i=1,#S do
  local c = S:sub(i,i)
  if c == '<' and a[i+1] <= a[i] then
    a[i+1] = a[i] + 1
  end
end
local s = 0
for i=1,#a do
  s = s + a[i]
end
print(s)

Score : 300 points

Problem Statement

Given is a string S of length N-1.
Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

If S_i= <: a_i<a_{i+1}

If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

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

S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3.
There is no good sequence whose sum is less than 3.

Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28


local mma, mmi = math.max, math.min
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {io.read("*n", "*n")}
  t[i][3] = i
end
local lmaxp, rminp = 1, 1
for i = 2, n do
  if t[lmaxp][1] < t[i][1] then
    lmaxp = i
  end
  if t[i][2] < t[rminp][2] then
    rminp = i
  end
end
local cand = mma(0, t[rminp][2] - t[lmaxp][1] + 1)
do
  local maxlen = 0
  for i = 1, n do
    if i ~= lmaxp and i ~= rminp then
      maxlen = mma(maxlen, t[i][2] - t[i][1] + 1)
    end
  end
  cand = cand + maxlen
end
if lmaxp ~= rminp then
  local l_r, r_l = t[rminp][2], t[lmaxp][1]

  local l_l, r_r = false, t[lmaxp][2]
  table.sort(t, function(a, b) return a[1] > b[1] end)
  for i = 1, n do
    if t[i][3] ~= lmaxp then
      l_l = t[i][1]
      cand = mma(cand, mma(0, l_r - l_l + 1) + mma(0, r_r - r_l + 1))
    end
    if t[i][3] ~= rminp then
      r_r = mmi(r_r, t[i][2])
    end
  end
  cand = mma(cand, mma(0, l_r - l_l + 1) + mma(0, r_r - r_l + 1))
end
print(cand)

Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931


N=io.read"*n"
print((function()
	for i=1,9 do
		local a=N/i
		if(a==math.floor(a)and 1<=a and a<=9)then return true end
	end
return false end)()and"Yes"or"No")

Score : 200 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.

Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.

Sample Input 1

10

Sample Output 1

Yes

10 can be represented as, for example, 2 \times 5.

Sample Input 2

50

Sample Output 2

No

50 cannot be represented as the product of two integers between 1 and 9.

Sample Input 3

81

Sample Output 3

Yes


local n=io.read("*n")
for i=1,9 do
  for j=1,9 do
    if i*j==n then
      return print("Yes")
    end
  end
end
print("No")


        n=io.read("*n")
    	cnt=0
        for i=1, 9 do
          if n%i==0 and n/i<10 then
            print("Yes")
        	cnt=1
          end
        end
    if not(cnt==1) then
      print("No")
    end


local n=io.read("*n")
for i=math.ceil(math.sqrt(n)),1,-1 do
  if n%i==0 then
    return print(math.floor(i+n/i-2))
  end
end

Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.


local n = io.read("*n")
for i = math.ceil(math.sqrt(n)), 1, -1 do
  if n % i == 0 then
    print(i + (n // i) - 2)
    break
  end
end


local a,b,x=io.read("n","n","n")
print(x>a^2*b/2 and math.atan(2*(a^2*b-x)/a^3)*180/math.pi or math.atan((a*b^2)/(2*x))*180/math.pi)

Score : 400 points

Problem Statement

Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\mathrm{cm} and whose height is b~\mathrm{cm}. (The thickness of the bottle can be ignored.)

We will pour x~\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.

When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.

Constraints

All values in input are integers.

1 \leq a \leq 100

1 \leq b \leq 100

1 \leq x \leq a^2b

Input

Input is given from Standard Input in the following format:

a b x

Output

Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

2 2 4

Sample Output 1

45.0000000000

This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.

Sample Input 2

12 21 10

Sample Output 2

89.7834636934

This bottle is almost empty. When the water gets spilled, the bottle is nearly horizontal.

Sample Input 3

3 1 8

Sample Output 3

4.2363947991

This bottle is almost full. When the water gets spilled, the bottle is still nearly vertical.


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 find_min_true(n, f)
    -- Define f(-1) == false, f(n) == true
    -- Invariant: f(left) == false and f(right) == true
    local left = -1
    local right = n
    while right - left > 1 do
        local m = left + (right - left) // 2
        if f(m) then
            right = m -- keep f(right) == true
        else
            left = m -- keep f(left) == false
        end
    end
    return right
end
---
local N, K = read.nn()
local A = read.N(N)
local F = read.N(N)
table.sort(A)
table.sort(F, function(x,y)return x>y end)
local upper_lim = A[N] * F[1]

local function possible(max_time)
    local kk = K
    for i=1, N do
        local time = A[i] * F[i]
        if time > max_time then
            local train_cost = math.ceil((A[i] * F[i] - max_time) / F[i])
            kk = kk - train_cost
            if kk < 0 then
                return false
            end
        end
    end
    return true
end
print(find_min_true(upper_lim, possible))

Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:

A team should assign one member to each food, and should not assign the same member to multiple foods.

It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.

The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

All values in input are integers.

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

0 \leq K \leq 10^{18}

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

1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

Output

Print the minimum possible score of the team.

Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.

Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.

Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.

They cannot achieve a score of less than 2, so the answer is 2.

Sample Input 2

3 8
4 2 1
2 3 1

Sample Output 2

0

They can choose not to do exactly K sets of training.

Sample Input 3

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

Sample Output 3

12


        n=io.read("*n")
        k=io.read("*k")

        a={}
        f={}

        for i=1, n do
          table.insert(a, io.read("*n"))
        end
        for i=1, n do
          table.insert(f, io.read("*n"))
        end

            table.sort(a)
            for i=1, n do
              if a[i]<k then
                a[i]=0
                k=k-a[i]
              elseif (not(k==0)) and a[i]>=k then
                a[i]=a[i]-k
                k=0
                break
              end
            end

            function maxpos(t, len)
              local p=1
              for i=1,len do
                if t[i]>t[p] then
                  p=i
                end
              end

  			  return p
            end
            function minpos(t, len)
              local q=1
              for i=1,len do
                if t[i]<t[q] then
                  q=i
                end
              end
  			  return q
            end
            v={}
            for i=1,n do
              v[i]=math.min(unpack(a))*math.max(unpack(f))
              table.remove(a, minpos(a, n))
              n=n-1
            end

            print(math.max(unpack(v)))


local mma, mmi = math.max, math.min
local n, m = io.read("*n", "*n")
local edge = {}
local inf = n + 1
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
end

local len = {}
local function solve(z)
  for i = 1, n - 1 do
    len[i] = inf
  end
  len[n] = 0
  for idx = n - 1, 1, -1 do
    if #edge[idx] == 1 then
      len[idx] = 1 + len[edge[idx][1]]
    else
      local lensum, lenmax = 0, 0
      for i = 1, #edge[idx] do
        local v = len[edge[idx][i]]
        lensum = lensum + v
        lenmax = mma(lenmax, v)
      end
      if z == idx then
        len[idx] = 1 + (lensum - lenmax) / (#edge[idx] - 1)
      else
        len[idx] = 1 + lensum / #edge[idx]
      end
    end
  end
  return len[1]
end
local ret = solve(1)
for i = 2, n do
  ret = mmi(ret, solve(i))
end
print(string.format("%.9f", ret))

Score : 600 points

Problem Statement

There is a cave consisting of N rooms and M one-directional passages. The rooms are numbered 1 through N.

Takahashi is now in Room 1, and Room N has the exit. The i-th passage connects Room s_i and Room t_i (s_i < t_i) and can only be traversed in the direction from Room s_i to Room t_i. It is known that, for each room except Room N, there is at least one passage going from that room.

Takahashi will escape from the cave. Each time he reaches a room (assume that he has reached Room 1 at the beginning), he will choose a passage uniformly at random from the ones going from that room and take that passage.

Aoki, a friend of Takahashi's, can block one of the passages (or do nothing) before Takahashi leaves Room 1. However, it is not allowed to block a passage so that Takahashi is potentially unable to reach Room N.

Let E be the expected number of passages Takahashi takes before he reaches Room N. Find the value of E when Aoki makes a choice that minimizes E.

Constraints

2 \leq N \leq 600

N-1 \leq M \leq \frac{N(N-1)}{2}

s_i < t_i

If i != j, (s_i, t_i) \neq (s_j, t_j). (Added 21:23 JST)

For every v = 1, 2, ..., N-1, there exists i such that v = s_i.

Input

Input is given from Standard Input in the following format:

N M
s_1 t_1
:
s_M t_M

Output

Print the value of E when Aoki makes a choice that minimizes E.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

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

Sample Output 1

1.5000000000

If Aoki blocks the passage from Room 1 to Room 2, Takahashi will go along the path 1 → 3 → 4 with probability \frac{1}{2} and 1 → 4 with probability \frac{1}{2}. E = 1.5 here, and this is the minimum possible value of E.

Sample Input 2

3 2
1 2
2 3

Sample Output 2

2.0000000000

Blocking any one passage makes Takahashi unable to reach Room N, so Aoki cannot block a passage.

Sample Input 3

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

Sample Output 3

3.0133333333


Score : 100 points

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

We will close the window so as to minimize the total horizontal length of the uncovered part of the window.
Find the total horizontal length of the uncovered parts of the window then.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the total horizontal length of the uncovered parts of the window.

Sample Input 1

12 4

Sample Output 1

4

We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.

Sample Input 2

20 15

Sample Output 2

0

If the window is completely covered, print 0.

Sample Input 3

20 30

Sample Output 3

0

Each curtain may be longer than the window.


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 A, B = read.nn()
local ans = math.max(A-2*B,0)
print(ans)


local n = io.read("*n")
local t = {}
for i = 1, n do
  table.insert(t, io.read("*n"))
end
local ans = 0
for i = 1, #t - 1 do
  local sum = 0
  for j = i + 1, #t do
    sum = sum + t[j]
  end
  ans = ans + sum*t[i]
end
print(ans)

Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

All values in input are integers.

2 \leq N \leq 50

0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

Eat the first and second takoyaki. You will restore 3 health points.

Eat the second and third takoyaki. You will restore 2 health points.

Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.

Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312


local n = io.read("*n", "*l")
local s = io.read()
local c = 1
for i = 2, n do
  if s:sub(i, i) ~= s:sub(i - 1, i - 1) then c = c + 1 end
end
print(c)

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

1 \leq N \leq 10^5

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.

Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.

Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.

Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10


local n = io.read("*n")
io.read("*l")
local str = io.read("*l")
local ans = 1
for i = 1, #str - 1 do
  if string.sub(str, i, i) ~= string.sub(str, i+1, i+1) then
    ans = ans + 1
  end
end
print(ans)


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

n=io.read("*n","*l")
l={}
for i=1,n do
    l[i]=io.read("*n")
end
table.sort(l)
counter=0
for i=n,3,-1 do
    large=l[i]
    for j=i-1,2,-1 do
        middle=l[j]
        if middle*2<=large then
            break
        end
        local smallmin=upper_bound(large-middle,l)
        if smallmin<j then
            counter=counter+j-smallmin
        end
    end
end
print(counter)

Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23


local mfl = math.floor

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

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

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

local n = io.read("*n")
local l = {}
for i = 1, n do
  l[i] = io.read("*n")
end
table.sort(l)
local ret = 0
for i = n, 3, -1 do
  local large = l[i]
  for j = i - 1, 2, -1 do
    local middle = l[j]
    if middle * 2 <= large then break end
    local smallmin = upper_bound(l, large - middle)
    if smallmin < j then
      ret = ret + j - smallmin
    end
  end
end
print(ret)


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

local n, m, l = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  if c <= l then
    edge[a][b] = c
    edge[b][a] = c
  end
end
local tanklen = {}
local remlen = {}
local reallen = {}
local inf = 301 * 1000000000
for i = 1, n do
  tanklen[i] = 301
  remlen[i] = 0
  reallen[i] = inf
end
local function lessthan(x, y, cost)
  return reallen[x] + cost < reallen[y]
end
local function merge(x, y)
  if n < x then return y end
  if n < y then return x end
  return lessthan(x, y, 0) and x or y
end
local st = SegTree.new(n, merge, n + 1)
for i = 1, n do st:setValue(i, i) end
local alllen = {}
local function solve(start_pos)
  alllen[start_pos] = {}
  local asked = {}
  for i = 1, n do
    asked[i] = false
    tanklen[i] = 301
    remlen[i] = l
    reallen[i] = inf
    alllen[start_pos][i] = 301
  end
  tanklen[start_pos] = 0
  reallen[start_pos] = 0
  st:updateAll()
  for _u = 1, n do
    local src = st.stage[1][1]
    if src == n + 1 then break end
    if 301 <= tanklen[src] then break end
    if inf <= reallen[src] then break end
    asked[src] = true
    alllen[start_pos][src] = tanklen[src]
    for dst, cost in pairs(edge[src]) do
      if not asked[dst] and lessthan(src, dst, cost) then
        reallen[dst] = reallen[src] + cost
        tanklen[dst] = tanklen[src]
        remlen[dst] = remlen[src] - cost
        if remlen[dst] < 0 then
          tanklen[dst] = tanklen[dst] + 1
          remlen[dst] = l - cost
        end
        st:setValue(dst, dst)
      end
    end
    reallen[src] = inf
    st:setValue(src, src)
  end
end

for i = 1, n do
  solve(i)
end

local q = io.read("*n")
for i = 1, q do
  local s, t = io.read("*n", "*n")
  print(alllen[s][t] < 301 and alllen[s][t] or -1)
end

Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

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

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

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

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0


n=io.read("*n")

if n%2==0 then
  print((n/2)/n)
else
  print(((n+1)/2)/n)
end

Score : 100 points

Problem Statement

Given is an integer N.

Takahashi chooses an integer a from the positive integers not greater than N with equal probability.

Find the probability that a is odd.

Constraints

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the probability that a is odd.
Your output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

4

Sample Output 1

0.5000000000

There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \frac{2}{4} = 0.5.

Sample Input 2

5

Sample Output 2

0.6000000000

Sample Input 3

1

Sample Output 3

1.0000000000


local n,k=io.read("n","n")
local counter=0
for i=1,n do
    local h=io.read("n")
    if h>=k then
        counter=counter+1
    end
end
print(counter)

Score : 200 points

Problem Statement

N friends of Takahashi has come to a theme park.

To ride the most popular roller coaster in the park, you must be at least K centimeters tall.

The i-th friend is h_i centimeters tall.

How many of the Takahashi's friends can ride the roller coaster?

Constraints

1 \le N \le 10^5

1 \le K \le 500

1 \le h_i \le 500

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the number of people among the Takahashi's friends who can ride the roller coaster.

Sample Input 1

4 150
150 140 100 200

Sample Output 1

2

Two of them can ride the roller coaster: the first and fourth friends.

Sample Input 2

1 500
499

Sample Output 2

0

Sample Input 3

5 1
100 200 300 400 500

Sample Output 3

5


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 return function() return r(u(a)) end end})
local N = read.n()
local t = {}
for i=1,N do
    local a = io.read("n")
    t[i] = {i, a}
end
table.sort(t, function(x,y) return x[2] < y[2] end)
local ans = {}
for i=1,N do
    ans[i] = t[i][1]
end
print(table.concat(ans, " "))

Score : 300 points

Problem Statement

Takahashi is a teacher responsible for a class of N students.

The students are given distinct student numbers from 1 to N.

Today, all the students entered the classroom at different times.

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

From these records, reconstruct the order in which the students entered the classroom.

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the student numbers of the students in the order the students entered the classroom.

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1


local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {i, io.read("*n")}
end
table.sort(t, function(x, y) return x[2] < y[2] end)
local s = {}
for i = 1, n do s[i] = t[i][1] end
print(table.concat(s, " "))


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

local x=gcd(io.read("n","n"))

local counter=1
for i=2,math.sqrt(x) do
    if x%i==0 then
        while x%i==0 do
            x=x//i
        end
        counter=counter+1
    end
end
print(x~=1 and counter+1 or counter)

Score : 400 points

Problem Statement

Given are positive integers A and B.

Let us choose some number of positive common divisors of A and B.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisor

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

Definition of being coprime

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

Definition of dividing

An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x.

Constraints

All values in input are integers.

1 \leq A, B \leq 10^{12}

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

Sample Input 1

12 18

Sample Output 1

3

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

Sample Input 2

420 660

Sample Output 2

4

Sample Input 3

1 2019

Sample Output 3

1

1 and 2019 have no positive common divisors other than 1.


local x,y = io.read("*l"):match("(%d+).(%d+)")
local numX,numY = tonumber(x),tonumber(y)
local result = 1

local function FindFactors(numZ)
  local arr = {}
  for i = 2, numZ do
    local check = false
    while numZ % i == 0 do
      numZ = numZ // i
      check = true
    end
    if check then
      table.insert(arr,1,i)
    end
  end
  return arr
end

local arrX = FindFactors(numX)
local arrY = FindFactors(numY)

for i=1,#arrX do
  for j=1,#arrY do
    if arrX[i] == arrY[j] then
      result = result + 1
    end
  end
end
print(result)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, M = read.nn()
local KCost = {}
local KAbl = {}
for i=1,M do
    local a, b = read.nn()
    KCost[i] = a
    KAbl[i] = 0
    for j=1,b do
        local c = read.n()
        local bit = 1 << (c-1)
        KAbl[i] = KAbl[i] | bit
    end
end

local curset = 0
local STATMAX = math.floor(2^N) - 1

local INF = 10^9
local cost = array(1, INF)
cost[0] = 0
local min = math.min
local function chmin(s, val)
    cost[s] = min(cost[s], val)
end

for stat=0,STATMAX do
    if cost[stat] ~= INF then
        for curkey=1,M do
            local newstat = stat | KAbl[curkey]
            chmin(newstat, cost[stat] + KCost[curkey])
        end
    end
end

if cost[STATMAX] == INF then
    print("-1")
    return
end

print(cost[STATMAX])

Score : 500 points

Problem Statement

We have N locked treasure boxes, numbered 1 to N.

A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.

Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.

Constraints

All values in input are integers.

1 \leq N \leq 12

1 \leq M \leq 10^3

1 \leq a_i \leq 10^5

1 \leq b_i \leq N

1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}

Output

Print the minimum cost required to unlock all the treasure boxes.
If it is impossible to unlock all of them, print -1.

Sample Input 1

2 3
10 1
1
15 1
2
30 2
1 2

Sample Output 1

25

We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.

Sample Input 2

12 1
100000 1
2

Sample Output 2

-1

We cannot unlock all the boxes.

Sample Input 3

4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3

Sample Output 3

69942


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  for i = 1, n do self.stage[stagenum][i] = i end
  for i = n + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage, sz = 1, bls(1, stagenum - 1)
    local len = right - left + 1
    while (left - 1) % sz ~= 0 or len < sz do
      stage, sz = stage + 1, brs(sz, 1)
    end
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.top = function(self) return self.stage[1][1] end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local longest_len = {}
local edge = {}
local invedge = {}
local updated = {}
local startcand = {}
for i = 1, n + 1 do
  longest_len[i] = -1
  edge[i] = {}
  invedge[i] = {}
  updated[i] = false
  startcand[i] = true
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b] = true
  invedge[b][a] = true
  startcand[b] = false
end
local function getmax(a, b)
  if updated[a] and updated[b] then
    return longest_len[a] < longest_len[b] and b or a
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end
-- find loop candidate
local st = SegTree.new(n, getmax, n + 1)
for i = 1, n do
  longest_len[i] = 0
  updated[i] = true
end
st:updateAll()
while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if longest_len[dst] < 0 or longest_len[dst] < longest_len[src] + 1 then
      if longest_len[dst] < n then
        longest_len[dst] = longest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  updated[src] = false
  st:update(src)
end

local loopCandidates = {}
for i = 1, n do
  if n <= longest_len[i] then
    loopCandidates[i] = true
  end
end
-- remove no-loop points from loop candidate points
while true do
  local removed = false
  for src, _u in pairs(loopCandidates) do
    local f = false
    for dst, _u2 in pairs(edge[src]) do
      if loopCandidates[dst] then
        f = true
        break
      end
    end
    if not f then loopCandidates[src], removed = nil, true break end
  end
  if not removed then break end
end

-- remove all no-loop edges
for src, _u in pairs(loopCandidates) do
  for dst, _u2 in pairs(edge[src]) do
    if not loopCandidates[dst] then edge[src][dst] = nil end
  end
  for dst, _u2 in pairs(invedge[src]) do
    if not loopCandidates[dst] then invedge[src][dst] = nil end
  end
end
for i = 1, n do
  if not loopCandidates[i] then
    edge[i] = {}
    invedge[i] = {}
  end
end

-- find first length and second length from certain point
local loop_len = {}
local loop_started = {}
for src, _u in pairs(loopCandidates) do
  loop_len[src] = {-1, -1}
  loop_started[src] = false
end
if next(loopCandidates) then
  local src = next(loopCandidates)
  loop_len[src][1] = 0
  updated[src] = true
end

local function getlooptask(a, b)
  if updated[a] and updated[b] then
    if loop_len[a][2] < 0 and loop_len[b][2] < 0 then
      return loop_len[a][1] < loop_len[b][1] and a or b
    elseif loop_len[a][2] < 0 then return a
    elseif loop_len[b][2] < 0 then return b
    else
      return loop_len[a][2] < loop_len[b][2] and a or b
    end
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

st.func = getlooptask
st:updateAll()

while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if loop_started[dst] then
      if loop_started[src] then
        if loop_len[dst][2] < 0 or loop_len[src][2] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][2] + 1
          updated[dst] = true
          st:update(dst)
        end
      else
        if loop_len[dst][2] < 0 or loop_len[src][1] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][1] + 1
          updated[dst] = true
          st:update(dst)
        end
      end
    else
      if loop_len[dst][1] < 0 or loop_len[src][1] + 1 < loop_len[dst][1] then
        loop_len[dst][1] = loop_len[src][1] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  loop_started[src] = true
  updated[src] = false
  st:update(src)
end

-- get start of shortest loop
local short_loop_start = false
local short_loop_len = false
local short_loop_v1 = false
local short_loop_v2 = false
for src, v in pairs(loop_len) do
  local len = v[2] - v[1]
  if 0 < len then
    local f = false
    if not short_loop_len or len < short_loop_len then
      f = true
    elseif short_loop_len == len then
      if v[1] < short_loop_v1 then
        f = true
      end
    end
    if f then
      short_loop_start, short_loop_len = src, len
      short_loop_v1, short_loop_v2 = v[1], v[2]
    end
  end
end

-- recalculate length
local shortest_len = {}
for i = 1, n do
  shortest_len[i] = -1
  updated[i] = false
end
local function getmin(a, b)
  if updated[a] and updated[b] then
    return shortest_len[a] < shortest_len[b] and a or b
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

if short_loop_start then
  shortest_len[short_loop_start] = 0
  updated[short_loop_start] = true
  st.func = getmin
  st:updateAll()
  while true do
    local src = st:top()
    if src == n + 1 then break end
    for dst, _u in pairs(edge[src]) do
      if shortest_len[dst] < 0 or shortest_len[src] + 1 < shortest_len[dst] then
        shortest_len[dst] = shortest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
    updated[src] = false
    st:update(src)
  end
  -- restore path
  local ans = {short_loop_start}
  for i = short_loop_len - 1, 1, -1 do
    local dst = ans[#ans]
    for src, _u in pairs(invedge[dst]) do
      if shortest_len[src] == i then
        table.insert(ans, src)
        break
      end
    end
  end
  print(short_loop_len)
  print(table.concat(ans, "\n"))
else
  print(-1)
end

Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

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

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

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

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

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

Sample Output 3

4
2
3
4
5


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  for i = 1, n do self.stage[stagenum][i] = i end
  for i = n + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage, sz = 1, bls(1, stagenum - 1)
    local len = right - left + 1
    while (left - 1) % sz ~= 0 or len < sz do
      stage, sz = stage + 1, brs(sz, 1)
    end
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.top = function(self) return self.stage[1][1] end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local longest_len = {}
local edge = {}
local invedge = {}
local updated = {}
local startcand = {}
for i = 1, n + 1 do
  longest_len[i] = -1
  edge[i] = {}
  invedge[i] = {}
  updated[i] = false
  startcand[i] = true
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b] = true
  invedge[b][a] = true
  startcand[b] = false
end
local function getmax(a, b)
  if updated[a] and updated[b] then
    return longest_len[a] < longest_len[b] and b or a
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end
-- find loop candidate
local st = SegTree.new(n, getmax, n + 1)
for i = 1, n do
  longest_len[i] = 0
  updated[i] = true
end
st:updateAll()
while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if longest_len[dst] < 0 or longest_len[dst] < longest_len[src] + 1 then
      if longest_len[dst] < n then
        longest_len[dst] = longest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  updated[src] = false
  st:update(src)
end

local loopCandidates = {}
for i = 1, n do
  if n <= longest_len[i] then
    loopCandidates[i] = true
  end
end
-- remove no-loop points from loop candidate points
while true do
  local removed = false
  for src, _u in pairs(loopCandidates) do
    local f = false
    for dst, _u2 in pairs(edge[src]) do
      if loopCandidates[dst] then
        f = true
        break
      end
    end
    if not f then loopCandidates[src], removed = nil, true break end
  end
  if not removed then break end
end

-- remove all no-loop edges
for src, _u in pairs(loopCandidates) do
  for dst, _u2 in pairs(edge[src]) do
    if not loopCandidates[dst] then edge[src][dst] = nil end
  end
  for dst, _u2 in pairs(invedge[src]) do
    if not loopCandidates[dst] then invedge[src][dst] = nil end
  end
end
for i = 1, n do
  if not loopCandidates[i] then
    edge[i] = {}
    invedge[i] = {}
  end
end

-- find first length and second length from certain point
local loop_len = {}
local loop_started = {}
for src, _u in pairs(loopCandidates) do
  loop_len[src] = {-1, -1}
  loop_started[src] = false
end
if next(loopCandidates) then
  local src = next(loopCandidates)
  loop_len[src][1] = 0
  updated[src] = true
end

local function getlooptask(a, b)
  if updated[a] and updated[b] then
    if loop_len[a][2] < 0 and loop_len[b][2] < 0 then
      return loop_len[a][1] < loop_len[b][1] and a or b
    elseif loop_len[a][2] < 0 then return a
    elseif loop_len[b][2] < 0 then return b
    else
      return loop_len[a][2] < loop_len[b][2] and a or b
    end
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

st.func = getlooptask
st:updateAll()

while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if loop_started[dst] then
      if loop_started[src] then
        if loop_len[dst][2] < 0 or loop_len[src][2] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][2] + 1
          updated[dst] = true
          st:update(dst)
        end
      else
        if loop_len[dst][2] < 0 or loop_len[src][1] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][1] + 1
          updated[dst] = true
          st:update(dst)
        end
      end
    else
      if loop_len[dst][1] < 0 or loop_len[src][1] + 1 < loop_len[dst][1] then
        loop_len[dst][1] = loop_len[src][1] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  loop_started[src] = true
  updated[src] = false
  st:update(src)
end

-- get start of shortest loop
local short_loop_start = false
local short_loop_len = false
local short_loop_v1 = false
local short_loop_v2 = false
for src, v in pairs(loop_len) do
  local len = v[2] - v[1]
  if 0 < len then
    local f = false
    if not short_loop_len or len < short_loop_len then
      f = true
    elseif short_loop_len == len then
      if v[1] < short_loop_v1 then
        f = true
      end
    end
    if f then
      short_loop_start, short_loop_len = src, len
      short_loop_v1, short_loop_v2 = v[1], v[2]
    end
  end
end

-- recalculate length
local shortest_len = {}
for i = 1, n do
  shortest_len[i] = -1
  updated[i] = false
end
local function getmin(a, b)
  if updated[a] and updated[b] then
    return shortest_len[a] < shortest_len[b] and a or b
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

if short_loop_start then
  shortest_len[short_loop_start] = 0
  updated[short_loop_start] = true
  st.func = getmin
  st:updateAll()
  while true do
    local src = st:top()
    if src == n + 1 then break end
    for dst, _u in pairs(edge[src]) do
      if shortest_len[dst] < 0 or shortest_len[src] + 1 < shortest_len[dst] then
        shortest_len[dst] = shortest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
    updated[src] = false
    st:update(src)
  end
  -- restore path
  local ans = {short_loop_start}
  for i = short_loop_len - 1, 1, -1 do
    local dst = ans[#ans]
    for src, _u in pairs(invedge[dst]) do
      if (shortest_len[src] + 1) % short_loop_len == shortest_len[dst] then
        table.insert(ans, src)
        break
      end
    end
  end
  print(short_loop_len)
  print(table.concat(ans, "\n"))
else
  print(-1)
end


local s=io.read()
if s=="Sunny"then
  print("Cloudy")
elseif s=="Cloudy"then
  print("Rainy")
else
  print("Sunny")
end

Score: 100 points

Problem Statement

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

Constraints

S is Sunny, Cloudy, or Rainy.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string representing the expected weather tomorrow, in the same format in which input is given.

Sample Input 1

Sunny

Sample Output 1

Cloudy

In Takahashi's town, a sunny day is followed by a cloudy day.

Sample Input 2

Rainy

Sample Output 2

Sunny


s=io.read()
len=s:len()

for i=1,len do
  if i%2==0 then
    l=s:sub(i, i)
    if not (l=="L" or l=="U" or l=="D") then
      print("No")
      os.exit()
    end
  else
    r=s:sub(i, i)
    if not (r=="R" or r=="U" or r=="D") then
      print("No")
      os.exit()
    end
  end
end

print("Yes")

Score: 200 points

Problem Statement

Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

S is said to be easily playable if and only if it satisfies both of the following conditions:

Every character in an odd position (1-st, 3-rd, 5-th, \ldots) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th, \ldots) is L, U, or D.

Your task is to print Yes if S is easily playable, and No otherwise.

Constraints

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

Each character of S is L, R, U, or D.

Input

Input is given from Standard Input in the following format:

S

Output

Print Yes if S is easily playable, and No otherwise.

Sample Input 1

RUDLUDR

Sample Output 1

Yes

Every character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th) is L, U, or D.

Thus, S is easily playable.

Sample Input 2

DULL

Sample Output 2

No

The 3-rd character is not R, U, nor D, so S is not easily playable.

Sample Input 3

UUUUUUUUUUUUUUU

Sample Output 3

Yes

Sample Input 4

ULURU

Sample Output 4

No

Sample Input 5

RDULULDURURLRDULRLR

Sample Output 5

Yes


n=io.read("*n")
k=io.read("*n")
q=io.read("*n")

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

ans={}
for i=1,n do
  table.insert(ans,k)
end

for i=1,q do
  ans[a[i]]=ans[a[i]]+1
end

sum=0
for i=1,n do
  p=ans[i]-k
  if p>0 then
    sum=sum+p
  end
end

for i=1,n do
  ans[i]=ans[i]-sum
  if ans[i]<=0 then
    print("No")
  else
    print("Yes")
  end
end

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

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

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

1 \leq N \leq 9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.

Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.


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

local total=0
for i=1,n do
    if i>=2 then
        total=total+b[a[i]]+(a[i-1]+1==a[i] and c[a[i-1]] or 0)
    else
        total=total+b[a[i]]
    end
end
print(total)

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

All values in input are integers.

2 \leq N \leq 20

1 \leq A_i \leq N

A_1, A_2, ..., A_N are all different.

1 \leq B_i \leq 50

1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150


function split(s, c)
  if string.find(s, c) == nil then
    return { s }
  end

  local cs = {}

  local lastPos = 0
  for part, p in string.gmatch(s, "(.-)" .. c .. "()") do
    table.insert(cs, part)
    lastPos = p
  end

  table.insert(cs, string.sub(s, lastPos))

  return cs
end

n=io.read("*n")
ans=0
a={}

print("")

for i=0, n-1 do
  table.insert(a, i, io.read("*n")-1)
end

for i=0, n-1 do
  ans = ans + io.read("*n")
end

c={}
for i=0, n-2 do
  table.insert(c, i, io.read("*n"))
end

for i=0, n-2 do
  if a[i]+1 == a[i+1] then
    ans = ans + c[a[i]]
  end
end

print(ans)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, string.sub(k, i, i)) end local r = io.read local u = table.unpack return function() return r(u(a)) end end})
local N = read.n()
local B = {}
for i=1,N-1 do B[i] = read.n() end

local AN = B[N-1]
local ans = AN
local next = AN
for i=N-1,2,-1 do
    local Ai = math.min(B[i-1], next)
    ans = ans + Ai
    next = Ai
end
local A1 = B[1]
ans = ans + A1
print(ans)

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


local n, k = io.read("*n", "*n", "*l")
local s = io.read()
local prv = s:sub(1, 1)
local nxt = ""
local changed = 0
local score = 0
for i = 2, n do
  nxt = s:sub(i, i)
  if prv == nxt then
    score = score + 1
  else
    changed = changed + 1
  end
  prv = nxt
end
while 0 < changed and 0 < k do
  if 2 <= changed then
    changed = changed - 2
    k = k - 1
    score = score + 2
  else
    changed = changed - 1
    k = k - 1
    score = score + 1
  end
end
print(score)

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


function comp_rev(a, b)
  if a > b then
    return true
  end

  return false
end

n=io.read("*n")
p={}

for i=1, n do
  table.insert(p, i, io.read("*n"))
end

ans=0

for i=1, n-1 do
  for j=i+1, n do
    m={}
    for l=i, j do
      table.insert(m, p[l])
    end
    table.sort(m, comp_rev)
    ans = ans + m[2]
  end
end

print(ans)

Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  if not i then i = #self.v + 1
  else table.remove(self.box)
  end
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalc = function(self, i)
  local kl, kr = self.l[i], self.r[i]
  if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
  else self.lc[i] = 0
  end
  if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
  else self.rc[i] = 0
  end
end
AvlTree.recalcAll = function(self, i)
  while 1 < i do
    self:recalc(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, parent)
  local granp, child = self.p[parent], self.l[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
end

AvlTree.rotL = function(self, parent)
  local granp, child = self.p[parent], self.r[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
end

AvlTree.rotLR = function(self, lparent, rparent)
  local sp, sl, sr = self.p, self.l, self.r
  local granp, d = sp[rparent], sr[lparent]
  sp[lparent], sr[lparent] = d, sl[d]
  sp[rparent], sl[rparent] = d, sr[d]
  sp[sl[d]], sp[sr[d]] = lparent, rparent
  sp[d], sl[d], sr[d] = granp, lparent, rparent
  if 1 < granp then
    if sr[granp] == rparent then sr[granp] = d
    else sl[granp] = d
    end
  else self.root = d
  end
end

AvlTree.rotRL = function(self, rparent, lparent)
  local sp, sl, sr = self.p, self.l, self.r
  local granp, d = sp[lparent], sl[rparent]
  sp[rparent], sl[rparent] = d, sr[d]
  sp[lparent], sr[lparent] = d, sl[d]
  sp[sr[d]], sp[sl[d]] = rparent, lparent
  sp[d], sr[d], sl[d] = granp, rparent, lparent
  if 1 < granp then
    if sl[granp] == lparent then sl[granp] = d
    else sr[granp] = d
    end
  else self.root = d
  end
end
AvlTree.empty = function(self) return self.root <= 1 end
AvlTree.push = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalc(parent)
    local lcp_m_rcp = self.lc[parent] - self.rc[parent]
    if lcp_m_rcp % 2 ~= 0 then -- 1 or -1
      pos = parent
    elseif lcp_m_rcp == 2 then
      if self.lc[child] - 1 == self.rc[child] then
        self:rotR(parent)
        self:recalcAll(parent)
      else
        self:rotLR(child, parent)
        self:recalc(child)
        self:recalcAll(parent)
      end
      break
    elseif lcp_m_rcp == -2 then
      if self.rc[child] - 1 == self.lc[child] then
        self:rotL(parent)
        self:recalcAll(parent)
      else
        self:rotRL(child, parent)
        self:recalc(child)
        self:recalcAll(parent)
      end
      break
    else
      break
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalc(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcAll(self.p[node])
      break
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(node)
        self:recalcAll(node)
        break
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(node)
        self:recalc(node)
        node = nr
      else
        local nr = self.r[node]
        local nrl = self.l[nr]
        self:rotRL(nr, node)
        self:recalc(nr)
        self:recalc(node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
  local obj = {}
  setmetatable(obj, {__index = AvlTree})
  obj:create(lessthan, n)
  return obj
end

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

local lv0 = 0
local avl = AvlTree.new(function(a, b) return a > b end, 1)
avl:push(nd)
local rem = 1

local left = 1
local z = {}
while true do
  local right = left
  while t[left] == t[right + 1] do
    right = right + 1
  end
  local taskcnt = right - left + 1
  for i = 1, taskcnt do
    if avl:empty() then
      local rem = taskcnt + 1 - i
      if rem <= lv0 then
        lv0 = lv0 - rem
        taskcnt = i - 1
        break
      else
        print("No")
        os.exit()
      end
    end
    z[i] = avl:pop()
  end
  lv0 = lv0 + taskcnt
  for i = 1, taskcnt do
    local v = z[i]
    for j = 1, v - 1 do
      avl:push(j)
    end
  end
  left = right + 1
  if n < left then break end
end
print("Yes")

Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

All values in input are integers.

1 \leq N \leq 18

1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.

Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.

Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.

Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

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

Sample Output 4

No


s=io.read():match("%w+")
t=io.read():match("%w+")
cnt=0
for i=0,2,1 do
  if s:sub(i,i)==t:sub(i,i)then
    cnt=cnt+1
  end
end
print(cnt)

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


a=io.read("*n")
b=io.read("*n")

s=1
ans=0
while s < b do
  ans=ans+1
  s=s+a-1
end

print(ans)

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1