local function lower_bound(ary, x)
  if(x <= ary[1]) then return 1 end
  local num = #ary
  if(ary[num] < x) then return num + 1 end
  local min, max = 1, num
  while(1 < max - min) do
    local mid = (min + max) // 2
    if(ary[mid] < x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end
local function getComb(n, k)
  local ret = 1
  for i = 1, k do
    ret = ret * (n + 1 - i)
    ret = ret // i
  end
  return ret
end
local n, a, b = io.read("*n", "*n", "*n")
local t = {}
for i = 1, n do t[i] = io.read("*n") end
table.sort(t)
local sum = 0
for i = 1, a do
  sum = sum + t[n + 1 - i]
end
print(string.format("%.8f", sum / a))
local minval = t[n + 1 - a]
if minval ~= t[n] then
  local minvalcount = 0
  local usecount = 0
  for i = 1, n do
    if(t[i] == minval) then
      minvalcount = minvalcount + 1
      if(n + 1 - a <= i) then
        usecount = usecount + 1
      end
    end
  end
  print(getComb(minvalcount, usecount))
else
  for i = n - a + 1, n - b + 1, -1 do
    if t[i] == minval then
      b = n + 1 - i
    end
  end
  print(string.format("%d", 2^b - 2^(a - 1)))
end

Score : 400 points

Problem Statement

You are given N items.

The value of the i-th item (1 \leq i \leq N) is v_i.

Your have to select at least A and at most B of these items.

Under this condition, find the maximum possible arithmetic mean of the values of selected items.

Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized.

Constraints

1 \leq N \leq 50

1 \leq A,B \leq N

1 \leq v_i \leq 10^{15}

Each v_i is an integer.

Input

The input is given from Standard Input in the following format:

N A B
v_1
v_2
...
v_N

Output

Print two lines.

The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.

The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

Sample Input 1

5 2 2
1 2 3 4 5

Sample Output 1

4.500000
1

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.

There is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

Sample Input 2

4 2 3
10 20 10 10

Sample Output 2

15.000000
3

There can be multiple ways to select items so that the mean of the values will be maximized.

Sample Input 3

5 1 5
1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996

Sample Output 3

1000000000000000.000000
1


s = io.read()
f = s:sub(1, 1) == s:sub(3, 3)
print(f and "H" or "D")

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

Score : 100 points

Problem Statement

Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.
In this game, an honest player always tells the truth, and an dishonest player always tell lies.
You are given two characters a and b as the input. Each of them is either H or D, and carries the following information:

If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.
If b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.

Given this information, determine whether TopCoDeer is honest.

Constraints

a=H or a=D.

b=H or b=D.

Input

The input is given from Standard Input in the following format:

a b

Output

If TopCoDeer is honest, print H. If he is dishonest, print D.

Sample Input 1

H H

Sample Output 1

H

In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.

Sample Input 2

D H

Sample Output 2

D

In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.

Sample Input 3

D D

Sample Output 3

H

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.


a=io.read()
print((a=="H H"or a =="D D")and"H"or"D")


local x = io.read("n")

local rem_len = x
for i = 1, x do
	if rem_len >= 2 * i + 1 then
		rem_len = rem_len - i
	else
		break
	end
end

print(rem_len)

Score : 200 points

Problem Statement

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.
During the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.
That is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.
The kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.
Find the earliest possible time to reach coordinate X.

Constraints

X is an integer.

1≤X≤10^9

Input

The input is given from Standard Input in the following format:

X

Output

Print the earliest possible time for the kangaroo to reach coordinate X.

Sample Input 1

6

Sample Output 1

3

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

Sample Input 2

2

Sample Output 2

2

He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second.

Sample Input 3

11

Sample Output 3

5


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

local function lastjump(x)
    local n=0
    local jump=1
    local last=0
    while n<x do
        n=n+jump
        jump=jump+1
        last=last+1
    end
    return last
end

print(lastjump(x))


x = io.read("*n")
ncand = (math.sqrt(1 + 4 * x) - 1) / 2
ncand = math.floor(ncand) - 1
while(ncand * (ncand + 1) < 2 * x) do
  ncand = ncand + 1
end
print(ncand)


local mmi = math.min
local mma = math.max
local mce, mfl = math.ceil, math.floor
local n, k = io.read("*n", "*n")
local a = {}
for i = 1, n do
  local at = io.read("*n")
  if at < k then
    table.insert(a, at)
  end
end
n = #a
local function solve()
  if n == 0 then return 0 end
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    for j = 1, mce((k - 1) / 2) do
      tail[i][j] = 0
    end
  end
  local function gettail(i, j)
    if j % 2 == 1 then
      return tail[i][mce(j / 2)] % 2 == 1
    else
      return mfl(tail[i][mce(j / 2)] / 2) == 1
    end
  end
  local function settail(i, j)
    local jidx = mce(j / 2)
    tail[i][jidx] = tail[i][jidx] + 2 - (j % 2)
  end
  local curmax = 0
  for i = n, 1, -1 do
    for j = 1, curmax do
      if gettail(i + 1, j) then
        settail(i, j)
        if j + a[i] < k then
          settail(i, j + a[i])
        end
      end
    end
    curmax = mmi(curmax + a[i], k - 1)
    settail(i, a[i])
  end
  local head = {}
  for i = 1, k - 1 do
    head[i] = false
  end
  local cnt = 0
  curmax = 0
  for i = 1, n - 1 do
    local ai = a[i]
    local need = false
    for j = k - ai, k - 1 do
      for p = 1, j - 1 do
        local q = j - p
        if head[p] and gettail(i + 1, q) then
          need = true
          break
        end
      end
      if head[j] or gettail(i + 1, j) then
        need = true
      end
      if need then break end
    end
    if not need then cnt = cnt + 1 end
    for j = mmi(curmax, k - 1 - ai), 1, -1 do
      if head[j] then head[j + ai] = true end
    end
    head[ai] = true
    curmax = mmi(curmax + ai, k - 1)
  end
  do -- i == n
    local ai = a[n]
    local need = false
    for j = k - ai, k - 1 do
      if head[j] then
        need = true
        break
      end
    end
    if not need then cnt = cnt + 1 end
  end
  return cnt
end
print(solve())

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


local mmi = math.min
local mma = math.max
local n, k = io.read("*n", "*n")
local a = {}
for i = 1, n do
  local at = io.read("*n")
  if at < k then
    table.insert(a, at)
  end
end
n = #a
local function solve()
  if n == 0 then return 0 end
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    -- for j = 1, k - 1 do
    --   tail[i][j] = false
    -- end
  end
  tail[n][a[n]] = true
  local curmax = a[n]
  for i = n - 1, 1, -1 do
    for j = 1, curmax do
      if tail[i + 1][j] then
        tail[i][j] = true
        if j + a[i] < k then
          tail[i][j + a[i]] = true
        end
      end
    end
    curmax = mmi(curmax + a[i], k - 1)
    tail[i][a[i]] = true
  end
  local head = {}
  for i = 1, k - 1 do
    head[i] = false
  end
  local cnt = 0
  curmax = 0
  for i = 1, n - 1 do
    local ai = a[i]
    local need = false
    for j = k - ai, k - 1 do
      for p = 1, j - 1 do
        local q = j - p
        if head[p] and tail[i + 1][q] then
          need = true
          break
        end
      end
      if head[j] or tail[i + 1][j] then
        need = true
      end
      if need then break end
    end
    if not need then cnt = cnt + 1 end
    for j = mmi(curmax, k - 1 - ai), 1, -1 do
      if head[j] then head[j + ai] = true end
    end
    head[ai] = true
    curmax = mmi(curmax + ai, k - 1)
  end
  do -- i == n
    local ai = a[n]
    local need = false
    for j = k - ai, k - 1 do
      if head[j] then
        need = true
        break
      end
    end
    if not need then cnt = cnt + 1 end
  end
  return cnt
end
print(solve())


local mmi = math.min
local mma = math.max
local n, k = io.read("*n", "*n")
local a = {}
for i = 1, n do
  local at = io.read("*n")
  if at < k then
    table.insert(a, at)
  end
end
n = #a
local function solve()
  if n == 0 then return 0 end
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    for j = 1, k - 1 do
      tail[i][j] = false
    end
  end
  tail[n][a[n]] = true
  local curmax = a[n]
  for i = n - 1, 1, -1 do
    for j = 1, curmax do
      if tail[i + 1][j] then
        tail[i][j] = true
        if j + a[i] < k then
          tail[i][j + a[i]] = true
        end
      end
    end
    curmax = mmi(curmax + a[i], k - 1)
    tail[i][a[i]] = true
  end
  local head = {}
  for i = 1, k - 1 do
    head[i] = false
  end
  local cnt = 0
  curmax = 0
  for i = 1, n - 1 do
    local ai = a[i]
    local need = false
    for j = k - ai, k - 1 do
      for p = 1, j - 1 do
        local q = j - p
        if head[p] and tail[i + 1][q] then
          need = true
          break
        end
      end
      if head[j] or tail[i + 1][j] then
        need = true
      end
      if need then break end
    end
    if not need then cnt = cnt + 1 end
    for j = mmi(curmax, k - 1 - ai), 1, -1 do
      if head[j] then head[j + ai] = true end
    end
    head[ai] = true
    curmax = mmi(curmax + ai, k - 1)
  end
  do -- i == n
    local ai = a[n]
    local need = false
    for j = k - ai, k - 1 do
      if head[j] then
        need = true
        break
      end
    end
    if not need then cnt = cnt + 1 end
  end
  return cnt
end
print(solve())


local mmi = math.min
local mma = math.max
local n, k = io.read("*n", "*n")
local a = {}
for i = 1, n do
  local at = io.read("*n")
  if at < k then
    table.insert(a, at)
  end
end
n = #a
local function solve()
  if n == 0 then return 0 end
  local tail = {}
  for i = 1, n do
    tail[i] = {}
    for j = 1, k - 1 do
      tail[i][j] = false
    end
  end
  tail[n][a[n]] = true
  local curmax = a[n]
  for i = n - 1, 1, -1 do
    for j = 1, curmax do
      if tail[i + 1][j] and j + a[i] < k then
        tail[i][j + a[i]] = true
      end
    end
    curmax = mma(curmax + a[i], k - 1)
    tail[i][a[i]] = true
  end
  local head = {}
  for i = 1, k - 1 do
    head[i] = false
  end
  local cnt = 0
  local curmax = 0
  for i = 1, n - 1 do
    local ai = a[i]
    local need = false
    for j = k - ai, k - 1 do
      for p = 1, j - 1 do
        local q = j - p
        if head[p] and tail[i + 1][q] then
          need = true
          break
        end
      end
      if head[j] or tail[i + 1][j] then
        need = true
      end
      if need then break end
    end
    if not need then cnt = cnt + 1 end
    for j = mmi(curmax, k - 1 - ai), 1, -1 do
      if head[j] then head[j + ai] = true end
    end
    head[ai] = true
    curmax = mmi(curmax + ai, k - 1)
  end
  do -- i == n
    local ai = a[n]
    local need = false
    for j = k - ai, k - 1 do
      if head[j] then
        need = true
        break
      end
    end
    if not need then cnt = cnt + 1 end
  end
  return cnt
end
print(solve())


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

local bus=0
local capacity=0
local tadius=t[1]+k
for i=1,n do
    if t[i]<=tadius then
        capacity=capacity+1
        if capacity==c then
            bus=bus+1
            tadius=t[i]+k
            capacity=0
        end
    else
        bus=bus+1
        tadius=t[i]+k
        capacity=0
    end
end
print(bus+1)

Score : 300 points

Problem Statement

Every day, N passengers arrive at Takahashi Airport.
The i-th passenger arrives at time T_i.

Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers.
Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport.
Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane.
For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between T_i and T_i + K (inclusive).

When setting the departure times for buses under this condition, find the minimum required number of buses.
Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.

Constraints

2 \leq N \leq 100000

1 \leq C \leq 10^9

1 \leq K \leq 10^9

1 \leq T_i \leq 10^9

C, K and T_i are integers.

Input

The input is given from Standard Input in the following format:

N C K
T_1
T_2
:
T_N

Output

Print the minimum required number of buses.

Sample Input 1

5 3 5
1
2
3
6
12

Sample Output 1

3

For example, the following three buses are enough:

A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.

A bus departing at time 6, that carries the passengers arriving at time 1 and 6.

A bus departing at time 12, that carries the passenger arriving at time 12.

Sample Input 2

6 3 3
7
6
2
8
10
6

Sample Output 2

3


local n, c, k = io.read("*n", "*n", "*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
table.sort(t)
local pos = 1
local busnum = 0
while(pos <= n) do
  busnum = busnum + 1
  local klim = t[pos] + k
  local clim = pos + c - 1
  local i = pos + 1
  while(i <= n) do
    if(clim < i) then break end
    if(klim < t[i]) then break end
    i = i + 1
  end
  pos = i
end
print(busnum)


Score : 100 points

Problem Statement

Snuke has a favorite restaurant.

The price of any meal served at the restaurant is 800 yen (the currency of Japan), and each time a customer orders 15 meals, the restaurant pays 200 yen back to the customer.

So far, Snuke has ordered N meals at the restaurant.
Let the amount of money Snuke has paid to the restaurant be x yen, and let the amount of money the restaurant has paid back to Snuke be y yen.
Find x-y.

Constraints

1 ≤ N ≤ 100

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

20

Sample Output 1

15800

So far, Snuke has paid 16000 yen, and the restaurant has paid back 200 yen. Thus, the answer is 15800.

Sample Input 2

60

Sample Output 2

47200

Snuke has paid 48000 yen for 60 meals, and the restaurant has paid back 800 yen.


local n = io.read("*n", "*l")
local str = io.read()
local t = {}
for i = 1, n do
  t[i] = str:sub(i, i) == "o"
end
local found = false
for c = 1, 4 do
  local res = {}
  res[1] = c <= 2
  res[2] = c % 2 == 1
  for i = 3, n do
    if res[i - 1] then
      if t[i - 1] then
        res[i] = res[i - 2]
      else
        res[i] = not res[i - 2]
      end
    else
      if t[i - 1] then
        res[i] = not res[i - 2]
      else
        res[i] = res[i - 2]
      end
    end
  end
  local cand = false
  if res[n] then
    if t[n] then
      cand = res[n - 1]
    else
      cand = not res[n - 1]
    end
  else
    if t[n] then
      cand = not res[n - 1]
    else
      cand = res[n - 1]
    end
  end
  if res[1] == cand then
    for k = 1, n do
      io.write(res[k] and "S" or "W")
    end
    io.write("\n")
    found = true
    break
  end
end
if not found then print(-1) end

Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897


local sub = string.sub
local n = io.read('*n', '*l')
local s = io.read('*l')
ss = {}
for i = 1, n do
	ss[i] = sub(s, i, i)
end
local ret = {
	{[1] = 1, [2] = 1},
	{[1] = 1, [2] = 0},
	{[1] = 0, [2] = 0},
	{[1] = 0, [2] = 1},
}
for i = 2, n-1 do
	for j = 1, 4 do
		local ans = ret[j]
		ans[i + 1] = ((ss[i] == 'o') == (ans[i] == 1)) and ans[i - 1] or 1 - ans[i - 1]
	end
end

local function isOK(a, b, c, ch)
	return ((ch == 'o') == (b == 1)) and (a == c) or (a ~= c)
end

local sum = 0
local result = nil
for j = 1, 4 do
	local ans = ret[j]
	if isOK(ans[n], ans[1], ans[2], ss[1]) and isOK(ans[n-1], ans[n], ans[1], ss[n]) then
		sum = sum  + 1
		result = ans
		break
	end
end

if sum >= 1 then
	for i = 1, n do
		result[i] = result[i] == 1 and "S" or "W"
	end
	print(table.concat(result))
else
	print(-1)
end


n = io.read('*n')
a = {}
for i = 1, n do a[i] = io.read('*n') end

max = -1
max_idx = {}
cnt = {}
for i = 1, n do
    if a[i] > max then
        max = a[i]
        max_idx[a[i]] = i
    end
    cnt[a[i]] = (cnt[a[i]] or 0) + 1
end

cnt_sorted = {}
for val, count in pairs(cnt) do
    cnt_sorted[#cnt_sorted + 1] = { val, count }
end
table.sort(cnt_sorted, function (a, b) return a[1] > b[1] end)

ans = {}
for i = 1, n do ans[i] = 0 end
accum, total = 0, 0
next_idx, next_ans = -1, 0
for i = 1, #cnt_sorted do
    local val, count = cnt_sorted[i][1], cnt_sorted[i][2]
    if max_idx[val] ~= nil then
        if next_idx ~= -1 then
            ans[next_idx] = accum - val * total - next_ans
            next_ans = accum - val * total
        end
        next_idx = max_idx[val]
    end
    accum = accum + val * count
    total = total + count
end
ans[next_idx] = accum - next_ans

for i = 1, n do print(ans[i]) end

Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0


local n,ma,mb=io.read("n","n","n")
local n1=math.floor(n/2)
local n2=math.floor(n/2)+n%2
local a1,b1,c1={},{},{}
local a2,b2,c2={},{},{}
for i=1,n1 do
    a1[i],b1[i],c1[i]=io.read("n","n","n")
end
for i=1,n2 do
    a2[i],b2[i],c2[i]=io.read("n","n","n")
end

local bit=require("bit")
local lshift=bit.lshift
local band=bit.band

local INF=10^13
local mincost=INF

local map1={}
for bit=0,lshift(1,n1) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n1 do
        if band(bit,lshift(1,i-1))>0 then
            asum=asum+a1[i]
            bsum=bsum+b1[i]
            csum=csum+c1[i]
        end
    end
    if csum>0 then
        map1[asum]=map1[asum] or {}
        if not map1[asum][bsum] then
            map1[asum][bsum]=csum
        else
            map1[asum][bsum]=math.min(map1[asum][bsum],csum)
        end
        if asum*mb==bsum*ma then
            mincost=math.min(mincost,csum)
        end
    end
end
local map2={}
for bit=0,lshift(1,n2) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n2 do
        if band(bit,lshift(1,i-1))>0 then
            asum=asum+a2[i]
            bsum=bsum+b2[i]
            csum=csum+c2[i]
        end
    end
    if csum>0 then
        map2[asum]=map2[asum] or {}
        if not map2[asum][bsum] then
            map2[asum][bsum]=csum
        else
            map2[asum][bsum]=math.min(map2[asum][bsum],csum)
        end
        if asum*mb==bsum*ma then
            mincost=math.min(mincost,csum)
        end
    end
end

local m=math.max(ma,mb)
for a1,_ in pairs(map1) do
    for b1,c1 in pairs(map1[a1]) do
        for i=0,400,m do
            i=math.floor(i/m)
            local c2=(map2[i*ma-a1] and map2[i*ma-a1][i*mb-b1])
            if c2 then
                mincost=math.min(mincost,c1+c2)
            end
        end
    end
end
print(INF>mincost and mincost or -1)

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


local n,ma,mb=io.read("n","n","n")
local n1=math.floor(n/2)
local n2=math.floor(n/2)+n%2
local a1,b1,c1={},{},{}
local a2,b2,c2={},{},{}
for i=1,n1 do
    a1[i],b1[i],c1[i]=io.read("n","n","n")
end
for i=1,n2 do
    a2[i],b2[i],c2[i]=io.read("n","n","n")
end

local bit=require("bit")
local lshift=bit.lshift
local band=bit.band

local map1={}
for bit=0,lshift(1,n1) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n1 do
        if band(bit,lshift(1,i-1))>0 then
            asum=asum+a1[i]
            bsum=bsum+b1[i]
            csum=csum+c1[i]
        end
    end
    if csum>0 then
        map1[asum]=map1[asum] or {}
        if not map1[asum][bsum] then
            map1[asum][bsum]=csum
        else
            map1[asum][bsum]=math.min(map1[asum][bsum],csum)
        end
    end
end
local map2={}
for bit=0,lshift(1,n2) do
    local asum=0
    local bsum=0
    local csum=0
    for i=1,n2 do
        if band(bit,lshift(1,i-1))>0 then
            asum=asum+a2[i]
            bsum=bsum+b2[i]
            csum=csum+c2[i]
        end
    end
    if csum>0 then
        map2[asum]=map2[asum] or {}
        if not map2[asum][bsum] then
            map2[asum][bsum]=csum
        else
            map2[asum][bsum]=math.min(map2[asum][bsum],csum)
        end
    end
end

local m=math.max(ma,mb)
local INF=10^13
local mincost=INF
for a1,_ in pairs(map1) do
    for b1,c1 in pairs(map1[a1]) do
        if a1*mb==b1*ma then
            mincost=math.min(mincost,c1)
        else
            for i=0,400,m do
                i=math.floor(i/m)
                local c2=(map2[i*ma-a1] and map2[i*ma-a1][i*mb-b1])
                if c2 then
                    mincost=math.min(mincost,c1+c2)
                end
            end
        end
    end
end
print(INF>mincost and mincost or -1)


local mmi = math.min
local inf = 0
local t = {}
for i = 1, 400 * 400 do t[i] = 0 end

local n, ma, mb = io.read("*n", "*n", "*n")
for i = 1, n do
  local a, b, c = io.read("*n", "*n", "*n")
  for ia = 400, 1, -1 do
    for ib = 400, 1, -1 do
      local src = (ia - 1) * 400 + ib
      if 0 < t[src] then
        local dst = (ia + a - 1) * 400 + ib + b
        if t[dst] == 0 then
          t[dst] = t[src] + c
        else
          t[dst] = mmi(t[dst], t[src] + c)
        end
      end
    end
  end
  local first_dst = (a - 1) * 400 + b
  if t[first_dst] == 0 then
    t[first_dst] = c
  else
    t[first_dst] = mmi(t[first_dst], c)
  end
end

local ret = -1
for ia = 1, 400 do
  if ia % ma == 0 then
    ib = math.floor(ia * mb / ma)
    if ib <= 400 then
      local c = t[(ia - 1) * 400 + ib]
      if 0 < c then
        if ret < 0 then
          ret = c
        else
          ret = mmi(ret, c)
        end
      end
    end
  end
end
print(ret)


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.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.top = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  return self.v[node]
end

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

local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
if n == 1 then print("YES") os.exit() end
do
  local sum = 0
  local mod = mfl(n * (n + 1) / 2)
  for i = 1, n do
    sum = (sum + a[i]) % mod
  end
  if sum ~= 0 then print("NO") os.exit() end
end
local b = {}
for i = 1, n - 1 do
  b[i] = a[i + 1] - a[i]
end
b[n] = a[1] - a[n]

local avl = AvlTree.new(function(x, y) return b[x] < b[y] end, n)
local cnt = 0
local asked = {}
for i = 1, n do
  asked[i] = false
  avl:push(i)
end
while true do
  local p = avl:pop()
  if asked[p] then break end
  asked[p] = true
  local next_top = avl:top()
  if b[p] < cnt then
    local addcnt = mce((b[next_top] - b[p]) / n)
    addcnt = mma(addcnt, 1)
    b[p] = b[p] + addcnt * n
    cnt = cnt + addcnt
    avl:push(p)
  else
    break
  end
end
local valid = true
for i = 1, n do
  if cnt ~= b[i] then
    valid = false
    break
  end
end
print(valid and "YES" or "NO")

Score : 500 points

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

All the stones can be removed in one operation by selecting the second box.

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO


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.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.top = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  return self.v[node]
end

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

local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
if n == 1 then print("YES") os.exit() end
do
  local sum = 0
  local mod = mfl(n * (n + 1) / 2)
  for i = 1, n do
    sum = (sum + a[i]) % mod
  end
  if mod ~= 0 then print("NO") os.exit() end
end
local b = {}
for i = 1, n - 1 do
  b[i] = a[i + 1] - a[i]
end
b[n] = a[1] - a[n]

local avl = AvlTree.new(function(x, y) return b[x] < b[y] end, n)
local cnt = 0
local asked = {}
for i = 1, n do
  asked[i] = false
  avl:push(i)
end
while true do
  local p = avl:pop()
  if asked[p] then break end
  asked[p] = true
  local next_top = avl:top()
  if b[p] < cnt then
    local addcnt = mce((b[next_top] - b[p]) / n)
    addcnt = mma(addcnt, 1)
    b[p] = b[p] + addcnt * n
    cnt = cnt + addcnt
    avl:push(p)
  else
    break
  end
end
local valid = true
for i = 1, n do
  if cnt ~= b[i] then
    valid = false
    break
  end
end
print(valid and "YES" or "NO")


local mmi = math.min
local n = io.read("*n")
local c_term, c_thru = {}, {}
local edge = {}
local parent = {}
local childcnt = {}
for i = 1, n do
  c_term[i] = 0
  c_thru[i] = io.read("*n")
  edge[i] = {}
  parent[i] = -1
  childcnt[i] = 0
end
parent[1] = 0
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if parent[dst] == -1 then
      parent[dst] = src
      childcnt[src] = childcnt[src] + 1
      table.insert(tasks, dst)
    end
  end
end
tasks = {}
for i = 1, n do
  if childcnt[i] == 0 then
    c_term[i], c_thru[i] = c_thru[i], 0
    table.insert(tasks, i)
  end
end
local valid = true
for i = 1, n - 1 do
  local c = tasks[i]
  local p = parent[c]
  if c_term[p] + c_thru[p] < c_term[c] then
    valid = false
    break
  end
  local sub = mmi(c_term[c], c_thru[p])
  c_term[c] = c_term[c] - sub
  c_thru[p] = c_thru[p] - sub
  c_term[p] = c_term[p] + sub - c_term[c]
  childcnt[p] = childcnt[p] - 1
  if 0 == childcnt[p] then
    if 0 < c_thru[p] then
      valid = false
      break
    end
    table.insert(tasks, p)
  end
end
if c_term[1] + c_thru[1] ~= 0 then valid = false end
print(valid and "YES" or "NO")

Score : 700 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Currently, there are A_i stones placed on vertex i.
Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:

Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices.
Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.

Note that the operation cannot be performed if there is a vertex with no stone on the path.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i,b_i ≦ N

0 ≦ A_i ≦ 10^9

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.

Sample Input 1

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

Sample Output 1

YES

All the stones can be removed, as follows:

Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.

Select vertices 1 and 5. Then, there is no stone on any vertex.

Sample Input 2

3
1 2 1
1 2
2 3

Sample Output 2

NO

Sample Input 3

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

Sample Output 3

YES


print(io.read("n") < 1200 and "ABC" or "ARC")

Score : 100 points

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

Constraints

1 ≦ x ≦ 3{,}000

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

1000

Sample Output 1

ABC

Smeke's current rating is less than 1200, thus the output should be ABC.

Sample Input 2

2000

Sample Output 2

ARC

Smeke's current rating is not less than 1200, thus the output should be ARC.


print(io.read("*n") < 1200 and "ABC" or "ARC")


a=io.read()*1
print(a<1200 and "ABC" or "ARC")


x=io.read("*n")
point = 0
time = 0
repeat
  point = point + 6
  time = time + 1
  	if (x <= point) then
    	break
  	end
  point = point + 5
  time = time + 1
until point >= x
print(time)

Score : 300 points

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.
If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.
Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


n = io.read("*n")
r = 2 * (n // 11)
if 0 < n % 11 then
  if n % 11 <= 6 then
    r = r + 1
  else
    r = r + 2
  end
end
print(r)


local n = io.read("*n")
local t = {}
for i = 1, n do
  local a = io.read("*n")
  if not t[a] then t[a] = 0 else t[a] = t[a] + 1 end
end
local rm = 0
for k, v in pairs(t) do
  rm = rm + v
end
if rm == 1 then rm = 2 end
print(n - rm)

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


x=io.read("*n")
c=x%11
d=x*2//11
if 1<=c-d then
    d=d+(c-d<=6 and 1 or 2)
end
print(d)


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

local k=0
for _ in pairs(a) do
    k=k+1
end
print(k%2>0 and k or k-1)


local n = io.read("*n")
local t = {}
for i = 1, n do
  local a = io.read("*n")
  if t[a] then
    t[a] = t[a] + 1
    if t[a] == 3 then t[a] = 1 end
  else
    t[a] = 1
  end
end
local num2 = 0
local rem = 0
for k, v in pairs(t) do
  if v == 2 then num2 = num2 + 1 end
  rem = rem + 1
end
if num2 % 2 == 0 then
  print(rem)
else
  print(rem - 1)
end


local mfl = math.floor
local n, m = io.read("*n", "*n")
local imos = {}
for i = 1, m + 1 do
  imos[i] = 0
end
for i_n = 1, n do
  local l, r = io.read("*n", "*n")
  if l == r then
    local lim = math.ceil(math.sqrt(r))
    for i = 1, lim do
      if r % i == 0 then
        local z = mfl(r / i)
        if i <= z then
          imos[i] = imos[i] + 1
          imos[i + 1] = imos[i + 1] - 1
          if i ~= z then
            imos[z] = imos[z] + 1
            imos[z + 1] = imos[z + 1] - 1
          end
        end
      end
    end
  else
    l = l - 1
    imos[1] = imos[1] + 1
    imos[r + 1] = imos[r + 1] - 1
    for i = 1, r do
      local rlim2 = mfl(r / (i + 1))
      local llim = mfl(l / i)
      if rlim2 < llim then
        imos[rlim2 + 1] = imos[rlim2 + 1] - 1
        imos[llim + 1] = imos[llim + 1] + 1
      else
        if llim <=  r - l then
          break
        else
          for j = r - l + 1, llim do
            local ld, rd = mfl(l / j), mfl(r / j)
            if ld == rd then
              imos[j] = imos[j] - 1
              imos[j + 1] = imos[j + 1] + 1
            end
          end
          break
        end
      end
    end
  end
end
for i = 2, m do
  imos[i] = imos[i] + imos[i - 1]
end
imos[m + 1] = nil
print(table.concat(imos, "\n"))

Score : 700 points

Problem Statement

Snuke has decided to play a game, where the player runs a railway company.
There are M+1 stations on Snuke Line, numbered 0 through M.
A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train.
For example, if d = 3, the train stops at station 0, 3, 6, 9, and so forth.

There are N kinds of souvenirs sold in areas around Snuke Line. The i-th kind of souvenirs can be purchased when the train stops at one of the following stations: stations l_i, l_i+1, l_i+2, ..., r_i.

There are M values of d, the interval between two stops, for trains on Snuke Line: 1, 2, 3, ..., M.
For each of these M values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of d at station 0.
Here, assume that it is not allowed to change trains.

Constraints

1 ≦ N ≦ 3 × 10^{5}

1 ≦ M ≦ 10^{5}

1 ≦ l_i ≦ r_i ≦ M

Input

The input is given from Standard Input in the following format:

N M
l_1 r_1
:
l_{N} r_{N}

Output

Print the answer in M lines. The i-th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every i-th station.

Sample Input 1

3 3
1 2
2 3
3 3

Sample Output 1

3
2
2

If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind 1, 2 and 3.

If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind 1 and 2.

If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind 2 and 3.

Sample Input 2

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

Sample Output 2

7
6
6
5
4
5
5
3
2


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

local push=0
for i=n,1,-1 do
    a[i]=a[i]+push
    if a[i]%b[i]~=0 then
        push=push+b[i]-a[i]%b[i]
    end
end
print(push)

Score : 300 points

Problem Statement

There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons.
When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.

There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.

Find the minimum number of times Takahashi will press the buttons.

Constraints

All input values are integers.

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)

1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print an integer representing the minimum number of times Takahashi will press the buttons.

Sample Input 1

3
3 5
2 7
9 4

Sample Output 1

7

Press the first button twice, the second button twice and the third button three times.

Sample Input 2

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

Sample Output 2

22


local mmi, mma = math.min, math.max
local n = io.read("*n")
local edge = {}
local ccnt = {}
local nodetask = {}
local len = {}
for i = 1, n do
  ccnt[i] = 0
  edge[i] = {}
  nodetask[i] = {}
  len[i] = 0
end
for i = 2, n do
  local a = io.read("*n")
  table.insert(edge[i], a)
  ccnt[a] = ccnt[a] + 1
end
local tasks = {}
for i = 2, n do
  if ccnt[i] == 0 then
    table.insert(tasks, i)
  end
end
for i = 1, n do
  local src = tasks[i]
  if 0 < #nodetask[src] then
    table.sort(nodetask[src])
    local nts = nodetask[src]
    local v = nts[1] + 1
    for j = 2, #nts do
      v = 1 + mma(v, nts[j])
    end
    len[src] = v
  end
  local lensrc = len[src]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    local ccntdst= ccnt[dst]
    if 1 <= ccntdst then
      table.insert(nodetask[dst], lensrc)
      ccnt[dst] = ccntdst - 1
      if ccntdst == 1 then
        table.insert(tasks, dst)
      end
    end
  end
end
print(len[1])

Score : 800 points

Problem Statement

N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament.
For some reasons, the tournament may not be "fair" for all the contestants.
That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.

After each match, there were always one winner and one loser. The last contestant standing was declared the champion.

Figure: an example of a tournament

For convenience, the contestants were numbered 1 through N. The contestant numbered 1 was the champion, and the contestant numbered i(2 ≦ i ≦ N) was defeated in a match against the contestant numbered a_i.

We will define the depth of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.

Find the minimum possible depth of the tournament.

The formal description of the structure of the tournament is as follows. In the i-th match, one of the following played against each other:

Two predetermined contestants

One predetermined contestant and the winner of the j-th match, where j(j<i) was predetermined

The winner of the j-th match and the winner of the k-th match, where j and k (j,k<i, j ≠ k) were predetermined

Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i ≦ N(2 ≦ i ≦ N)

It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).

Input

The input is given from Standard Input in the following format:

N
a_2
:
a_N

Output

Print the minimum possible depth of the tournament.

Sample Input 1

5
1
1
2
4

Sample Output 1

3

The following tournament and the result of the matches are consistent with the given information:

In the first match, contestants 4 and 5 played against each other, and contestant 4 won.

In the second match, contestants 2 and 4 played against each other, and contestant 2 won.

In the third match, contestants 1 and 3 played against each other, and contestant 1 won.

In the fourth match, contestants 1 and 2 played against each other, and contestant 1 won.

The depth of this tournament is 3, since contestant 5 must play three matches in order to win the championship.
There is no tournament with depth 2 or less that is consistent with the given information, thus the output should be 3.

Sample Input 2

7
1
2
1
3
1
4

Sample Output 2

3

Sample Input 3

4
4
4
1

Sample Output 3

3


Score : 100 points

Problem Statement

There are two rectangles.
The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.
The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

The input is given from Standard Input in the following format:

A B C D

Output

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7

Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.
Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300

Sample Output 2

60000


local mmi = math.min
local n, a, b = io.read("*n", "*n", "*n", "*l")
local str = io.read()
local prv = nil
local sum = 0
for v in str:gmatch("%d+") do
  local nxt = tonumber(v)
  if prv ~= nil then
    sum = sum + mmi((nxt - prv) * a, b)
  end
  prv = nxt
end
print(sum)

Score : 500 points

Problem Statement

There are N towns on a line running east-west.
The towns are numbered 1 through N, in order from west to east.
Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value.
The coordinate of town i is X_i.

You are now at town 1, and you want to visit all the other towns.
You have two ways to travel:

Walk on the line.
Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.

Teleport to any location of your choice.
Your fatigue level increases by B, regardless of the distance covered.

Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.

Constraints

All input values are integers.

2≤N≤10^5

1≤X_i≤10^9

For all i(1≤i≤N-1), X_i<X_{i+1}.

1≤A≤10^9

1≤B≤10^9

Input

The input is given from Standard Input in the following format:

N A B
X_1 X_2 ... X_N

Output

Print the minimum possible total increase of your fatigue level when you visit all the towns.

Sample Input 1

4 2 5
1 2 5 7

Sample Output 1

11

From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4.
The total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.

Sample Input 2

7 1 100
40 43 45 105 108 115 124

Sample Output 2

84

From town 1, walk all the way to town 7.
The total increase of your fatigue level in this case is 84, which is the minimum possible value.

Sample Input 3

7 1 2
24 35 40 68 72 99 103

Sample Output 3

12

Visit all the towns in any order by teleporting six times.
The total increase of your fatigue level in this case is 12, which is the minimum possible value.


a=string.gsub(io.read(),",", " ")
print(a)

Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.

The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].

Dolphin wants to convert the comma-separated string s into a space-separated string.

Write a program to perform the conversion for him.

Constraints

The length of s is 19.

The sixth and fourteenth characters in s are ,.

The other characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

s

Output

Print the string after the conversion.

Sample Input 1

happy,newyear,enjoy

Sample Output 1

happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

Sample Input 2

haiku,atcoder,tasks

Sample Output 2

haiku atcoder tasks

Sample Input 3

abcde,fghihgf,edcba

Sample Output 3

abcde fghihgf edcba


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

local function getlength(start_idx)
  local taskstate = {}
  for i = 1, n do taskstate[i] = false end
  local tasks = {}
  local tasknum = 0
  local done = 0

  local len = {}
  for i = 1, n do len[i] = 100000 end
  len[start_idx] = 0

  local function addtask(idx)
    if not taskstate[idx] then
      taskstate[idx] = true
      tasknum = tasknum + 1
      tasks[tasknum] = idx
    end
  end
  addtask(start_idx)

  while done < tasknum do
    done = done + 1
    local src = tasks[done]
    taskstate[src] = false
    local tmp = {}
    for dst, cost in pairs(edge[src]) do
      if len[src] + cost < len[dst] then
        len[dst] = len[src] + cost
        table.insert(tmp, dst)
      end
    end
    table.sort(tmp, function(a, b) return len[a] < len[b] end)
    for i = 1, #tmp do addtask(tmp[i]) end
  end
  return len
end
local lenall = {}
for i = 1, n do
  lenall[i] = getlength(i)
end
local ret = 0
for i = 1, m do
  local contains = false
  local edge_l, edge_r, c = edgeinfo[i][1], edgeinfo[i][2], edgeinfo[i][3]
  for src = 1, n do
    for dst = 1, n do
      if lenall[src][dst] == lenall[src][edge_l] + c + lenall[edge_r][dst] then
        contains = true
        break
      end
    end
    if contains then break end
  end
  if not contains then ret = ret + 1 end
end
print(ret)

Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

The given graph contains neither self-loops nor double edges.

The given graph is connected.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.


Score : 600 points

Problem Statement

A tetromino is a figure formed by joining four squares edge to edge.
We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively:

Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession are a_I, a_O, a_T, a_J, a_L, a_S and a_Z, respectively.
Snuke will join K of his tetrominos to form a rectangle that is two squares tall and 2K squares wide.
Here, the following rules must be followed:

When placing each tetromino, rotation is allowed, but reflection is not.

Each square in the rectangle must be covered by exactly one tetromino.

No part of each tetromino may be outside the rectangle.

Snuke wants to form as large a rectangle as possible.
Find the maximum possible value of K.

Constraints

0≤a_I,a_O,a_T,a_J,a_L,a_S,a_Z≤10^9

a_I+a_O+a_T+a_J+a_L+a_S+a_Z≥1

Input

The input is given from Standard Input in the following format:

a_I a_O a_T a_J a_L a_S a_Z

Output

Print the maximum possible value of K. If no rectangle can be formed, print 0.

Sample Input 1

2 1 1 0 0 0 0

Sample Output 1

3

One possible way to form the largest rectangle is shown in the following figure:

Sample Input 2

0 0 10 0 0 0 0

Sample Output 2

0

No rectangle can be formed.


c = io.read()
if string.find(c , "a") == nil and string.find(c , "i") == nil
and string.find(c , "u") == nil and string.find(c , "e") == nil
and string.find(c , "o") == nil then
	print("consonant")
else
	print("vowel")
end

Score : 100 points

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

Constraints

c is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

c

Output

If c is a vowel, print vowel. Otherwise, print consonant.

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


a, b = io.read("*n", "*n", "*l")
for i = 1, a do
  s = io.read()
  print(s) print(s)
end


local road={}
local rail={}
local size={}

local function init(n)
    for i=1,n do
        road[i]=i
        rail[i]=i
        size[i]=1
    end
end

local function root(x,list)
    return list[x]==x and x or root(list[x],list)
end

local function same(x,y,list)
    return root(x,list)==root(y,list)
end

local function unite(x,y,list)
    x=root(x,list)
    y=root(y,list)
    if x==y then
        return
    end
    if size[x]>size[y] then
        x,y=y,x
    end
    list[x]=y
    size[y]=size[y]+size[x]
end

local function getsize(x)
    return size[root(x,list)]
end

----------

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

for i=1,k do
    local p,q=io.read("n","n")
    unite(p,q,road)
end

for i=1,l do
    local r,s=io.read("n","n")
    unite(r,s,rail)
end

local counter={}
for i=1,n do
    local key=root(i,road).." "..root(i,rail)
    counter[key]=(counter[key] or 0)+1
end

for i=1,n do
    local key=root(i,road).." "..root(i,rail)
    io.write(counter[key].." ")
end
print()

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

The input is given from Standard Input in the following format:

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


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

local function uf_initialize(n)
  local parent = {}
  for i = 1, n do parent[i] = i end
  return parent
end

local function uf_findroot(idx, parent)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

local p1 = uf_initialize(n)
for i = 1, k do
  local p, q = io.read("*n", "*n")
  local pr, qr = uf_findroot(p, p1), uf_findroot(q, p1)
  p1[qr], p1[q] = pr, pr
end

local p2 = uf_initialize(n)
for i = 1, l do
  local r, s = io.read("*n", "*n")
  local rr, sr = uf_findroot(r, p2), uf_findroot(s, p2)
  p2[sr], p2[s] = rr, rr
end

local group = {}
for i = 1, n do
  local r1, r2 = uf_findroot(i, p1), uf_findroot(i, p2)
  if not group[r1] then group[r1] = {} end
  if not group[r1][r2] then
    group[r1][r2] = 1
  else
    group[r1][r2] = group[r1][r2] + 1
  end
end
for i = 1, n do
  local r1, r2 = uf_findroot(i, p1), uf_findroot(i, p2)
  io.write(group[r1][r2])
  io.write(i == n and "\n" or " ")
end


s = io.read()
a = s:match("AtCoder (%w+) Contest")
print("A" .. a:sub(1, 1) .. "C")

Score : 100 points

Problem Statement

Snuke is going to open a contest named "AtCoder s Contest".
Here, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters.

Snuke has decided to abbreviate the name of the contest as "AxC".
Here, x is the uppercase English letter at the beginning of s.

Given the name of the contest, print the abbreviation of the name.

Constraints

The length of s is between 1 and 100, inclusive.

The first character in s is an uppercase English letter.

The second and subsequent characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

AtCoder s Contest

Output

Print the abbreviation of the name of the contest.

Sample Input 1

AtCoder Beginner Contest

Sample Output 1

ABC

The contest in which you are participating now.

Sample Input 2

AtCoder Snuke Contest

Sample Output 2

ASC

This contest does not actually exist.

Sample Input 3

AtCoder X Contest

Sample Output 3

AXC


s = io.read()
a = s:match("AtCoder (%w+) Contest")
print("A" .. a .. "C")


local a,b,x=io.read("n","n","n")
if a==0 then
    print(b//x+1)
elseif a==b then
    print(0)
else
    print((b//x+1)-((a-1)//x+1))
end

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

The input is given from Standard Input in the following format:

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


local a, b, x = io.read("*n", "*n", "*n")
if a == 0 then
  print(math.floor(b / x) + 1)
else
  print(math.floor(b / x) - math.floor((a - 1) / x))
end


local a, b, x = io.read("*n", "*n", "*n")
local ret = b // x
if(0 < a) then
  ret = ret - (a - 1) // x
else
  ret = ret + 1
end
print(ret)


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

local counter=0
for i=1,n-1 do
    local sum=a[i]+a[i+1]
    if sum>x then
        local diff=sum-x
        counter=counter+diff
        if diff<a[i+1] then
            a[i+1]=a[i+1]-diff
        else
            diff=diff-a[i+1]
            a[i+1]=0
            a[i]=a[i]-diff
        end
    end
end
print(string.format("%d",counter))

Score : 300 points

Problem Statement

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.

Snuke can perform the following operation any number of times:

Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

Any two neighboring boxes contain at most x candies in total.

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

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

The input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3 3
2 2 2

Sample Output 1

1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.


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

local counter=0
for i=1,n-1 do
    local sum=a[i]+a[i+1]
    if sum>x then
        local diff=sum-x
        counter=counter+diff
        if diff<a[i+1] then
            a[i+1]=a[i+1]-diff
        else
            diff=diff-a[i+1]
            a[i+1]=0
            a[i]=a[i]-diff
        end
    end
end
print(counter)


s = io.read()
p = s:sub(1, 1) == s:sub(#s, #s)
q = #s % 2 == 1
print(p == q and "Second" or "First")

Score : 500 points

Problem Statement

There is a string s of length 3 or greater.
No two neighboring characters in s are equal.

Takahashi and Aoki will play a game against each other.
The two players alternately performs the following operation, Takahashi going first:

Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.

The player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.

Constraints

3 ≤ |s| ≤ 10^5

s consists of lowercase English letters.

No two neighboring characters in s are equal.

Input

The input is given from Standard Input in the following format:

s

Output

If Takahashi will win, print First. If Aoki will win, print Second.

Sample Input 1

aba

Sample Output 1

Second

Takahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring.

Sample Input 2

abc

Sample Output 2

First

When Takahashi removes b from s, it becomes ac.
Then, Aoki cannot perform the operation, since there is no character in s, excluding both ends.

Sample Input 3

abcab

Sample Output 3

First


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

local lastval = 10000
while 0 < n do
  local min, max = 0, lastval
  while 1 < max - min do
    local mid = (min + max) // 2
    if n <= (mid * (mid + 1)) // 2 then
      max = mid
    else
      min = mid
    end
  end
  lastval = max
  table.insert(t, max)
  n = n - max
end
print(table.concat(t, "\n"))

Score : 300 points

Problem Statement

The problem set at CODE FESTIVAL 20XX Finals consists of N problems.

The score allocated to the i-th (1≦i≦N) problem is i points.

Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.

As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.

Determine the set of problems that should be solved.

Constraints

1≦N≦10^7

Partial Score

200 points will be awarded for passing the test set satisfying 1≦N≦1000.

Additional 100 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N

Output

Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.

If there exists more than one such set, any of them will be accepted.

Sample Input 1

4

Sample Output 1

1
3

Solving only the 4-th problem will also result in the total score of 4 points, but solving the 1-st and 3-rd problems will lower the highest score of a solved problem.

Sample Input 2

7

Sample Output 2

1
2
4

The set \{3,4\} will also be accepted.

Sample Input 3

1

Sample Output 3

1


local function uf_initialize(n)
  local parent = {}
  for i = 1, n do parent[i] = i end
  return parent
end

local function uf_findroot(idx, parent)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

local n, m = io.read("*n", "*n")
local lang = {}
for i = 1, m do lang[i] = {} end
for i = 1, n do
  local k = io.read("*n")
  for j = 1, k do
    local l = io.read("*n")
    table.insert(lang[l], i)
  end
end

local parent = uf_initialize(n)
for i = 1, m do
  if 1 < #lang[i] then
    local a = lang[i][1]
    local ra = uf_findroot(a, parent)
    for j = 2, #lang[i] do
      local b = lang[i][j]
      local rb = uf_findroot(b, parent)
      parent[b], parent[rb] = ra, ra
    end
  end
end
local g = uf_findroot(1, parent)
local ret = true
for i = 2, n do
  if uf_findroot(i, parent) ~= g then
    ret = false
    break
  end
end
print(ret and "YES" or "NO")

Score : 400 points

Problem Statement

On a planet far, far away, M languages are spoken. They are conveniently numbered 1 through M.

For CODE FESTIVAL 20XX held on this planet, N participants gathered from all over the planet.

The i-th (1≦i≦N) participant can speak K_i languages numbered L_{i,1}, L_{i,2}, ..., L_{i,{}K_i}.

Two participants A and B can communicate with each other if and only if one of the following conditions is satisfied:

There exists a language that both A and B can speak.

There exists a participant X that both A and B can communicate with.

Determine whether all N participants can communicate with all other participants.

Constraints

2≦N≦10^5

1≦M≦10^5

1≦K_i≦M

(The sum of all K_i)≦10^5

1≦L_{i,j}≦M

L_{i,1}, L_{i,2}, ..., L_{i,{}K_i} are pairwise distinct.

Partial Score

200 points will be awarded for passing the test set satisfying the following: N≦1000, M≦1000 and (The sum of all K_i)≦1000.

Additional 200 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N M
K_1 L_{1,1} L_{1,2} ... L_{1,{}K_1}
K_2 L_{2,1} L_{2,2} ... L_{2,{}K_2}
:
K_N L_{N,1} L_{N,2} ... L_{N,{}K_N}

Output

If all N participants can communicate with all other participants, print YES. Otherwise, print NO.

Sample Input 1

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

Sample Output 1

YES

Any two participants can communicate with each other, as follows:

Participants 1 and 2: both can speak language 2.

Participants 2 and 3: both can speak language 4.

Participants 1 and 3: both can communicate with participant 2.

Participants 3 and 4: both can speak language 6.

Participants 2 and 4: both can communicate with participant 3.

Participants 1 and 4: both can communicate with participant 2.

Note that there can be languages spoken by no participant.

Sample Input 2

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

Sample Output 2

NO

For example, participants 1 and 3 cannot communicate with each other.


local mmi, mma = math.min, math.max
local MinCostFlow = {}

MinCostFlow.initialize = function(self, n, spos, tpos, inf)
  self.n = n
  self.spos, self.tpos = spos, tpos
  self.inf = inf
  -- edge_dst[src][i] := dst
  self.edge_dst = {}
  -- edge_cost[src][i] := cost from src to edge_dst[src][i]
  self.edge_cost = {}
  -- edge_cap[src][i] := capacity from src to edge_dst[src][i]
  self.edge_cap = {}
  -- initial capacity. corresponding to edge_cap
  self.edge_initialcap = {}
  -- edge_dst_invedge_idx[src][i] := "j" where edge_dst[dst][j] == src
  -- in this case, edge_dst_invedge_idx[dst][j] should be "i".
  self.edge_dst_invedge_idx = {}
  -- len[v] := length from spos. len[spos] := 0
  self.len = {}
  -- sub_graph_flag[v] := whether to contains the vertex v in the sub-graph or not
  self.sub_graph_flag = {}
  -- sub_graph_v[i] := list of vertexes that are contained in the sub-graph. from tpos to spos.
  self.sub_graph_v = {}
  -- sub_graph_edgeidx[i] := edge index from sub_graph_v[i + 1] to sub_graph_v[i]
  self.sub_graph_edgeidx = {}
  -- sub_graph_size := the size of sub_graph_v.
  -- may not equal to #sub_graph_v (because not cleared).
  self.sub_graph_size = 0
  for i = 1, n do
    self.edge_dst[i] = {}
    self.edge_cost[i] = {}
    self.edge_cap[i] = {}
    self.edge_initialcap[i] = {}
    self.edge_dst_invedge_idx[i] = {}
    self.len[i] = 0
    self.sub_graph_flag[i] = false
  end
end

MinCostFlow.addEdge = function(self, src, dst, cost, cap)
  table.insert(self.edge_dst[src], dst)
  table.insert(self.edge_cost[src], cost)
  table.insert(self.edge_cap[src], cap)
  table.insert(self.edge_initialcap[src], cap)
  table.insert(self.edge_dst_invedge_idx[src], 1 + #self.edge_dst[dst])
  table.insert(self.edge_dst[dst], src)
  table.insert(self.edge_cost[dst], -cost)
  table.insert(self.edge_cap[dst], 0)--invcap
  table.insert(self.edge_initialcap[dst], 0)--invcap
  table.insert(self.edge_dst_invedge_idx[dst], #self.edge_dst[src])
end
MinCostFlow.invwalk_recursive = function(self, invsrc)
  if invsrc == self.spos then return true end
  local edge_dst, edge_cap, edge_cost = self.edge_dst, self.edge_cap, self.edge_cost
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local len = self.len
  local sub_graph_flag = self.sub_graph_flag
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  for i = 1, #edge_dst[invsrc] do
    local invdst = edge_dst[invsrc][i]
    local j = edge_dst_invedge_idx[invsrc][i]
    if 0 < edge_cap[invdst][j]
    and len[invdst] + edge_cost[invdst][j] == len[invsrc]
    and not sub_graph_flag[invdst] then
      self.sub_graph_size = self.sub_graph_size + 1
      sub_graph_v[self.sub_graph_size] = invdst
      sub_graph_edgeidx[self.sub_graph_size - 1] = j
      sub_graph_flag[invdst] = true
      if self:invwalk_recursive(invdst) then
        return true
      else
        self.sub_graph_flag[invdst] = false
        self.sub_graph_size = self.sub_graph_size - 1
      end
    end
  end
  return false
end

MinCostFlow.makeSubGraph = function(self)
  local inf = self.inf
  local len = self.len
  local n = self.n
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local edge_cost = self.edge_cost
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  local sub_graph_flag = self.sub_graph_flag
  for i = 1, n do
    len[i] = inf
    sub_graph_flag[i] = false
  end
  len[self.spos] = 0
  -- Dijkstra-like
  do
    local taskstate = {}
    for i = 1, n do taskstate[i] = false end
    local tasks = {}
    local tasknum = 0
    local done = 0
    local tasklim = n

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

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

    addtask(self.spos)

    while done < tasknum do
      done = done + 1
      local taskidx = done % tasklim
      if taskidx == 0 then taskidx = tasklim end
      local idx = tasks[taskidx]
      taskstate[idx] = false
      local eddst, edcap, edcost = edge_dst[idx], edge_cap[idx], edge_cost[idx]
      for i = 1, #eddst do
        if 0 < edcap[i] then
          walk(idx, eddst[i], edcost[i])
        end
      end
    end
  end
  self.sub_graph_size = 0
  if inf <= len[self.tpos] then
    return 0
  end
  -- restore route (from tpos to spos)
  self.sub_graph_size = 1
  sub_graph_v[1] = self.tpos
  self:invwalk_recursive(self.tpos)
  return 1
end

MinCostFlow.flow = function(self, capacity)
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  for i = self.sub_graph_size, 2, -1 do
    local src = sub_graph_v[i]
    local dst = sub_graph_v[i - 1]
    local j = sub_graph_edgeidx[i - 1]
    edge_cap[src][j] = edge_cap[src][j] - capacity
    local k = edge_dst_invedge_idx[src][j]
    edge_cap[dst][k] = edge_cap[dst][k] + capacity
  end
end

MinCostFlow.getMinCostFlow = function(self, amount, invalid)
  local ret = 0
  local cap = self:makeSubGraph()
  while 0 < cap do
    cap = mmi(amount, cap)
    ret = ret + self.len[self.tpos] * cap
    self:flow(cap)
    amount = amount - cap
    if 0 < amount then
      cap = self:makeSubGraph()
    else
      break
    end
  end
  if 0 < amount then return invalid end
  return ret
end

local h, w = io.read("*n", "*n")
local s = 2 * h * w + 1
local t = 2 * h * w + 2
local inf = 100000 * 200 * 2 + 100
MinCostFlow:initialize(t, s, t, 1000000007 * 100)
MinCostFlow:addEdge(s, 1, 0, 2)
MinCostFlow:addEdge(2 * h * w, t, 0, 2)
for i = 1, h do
  for j = 1, w - 1 do
    local idx = (i - 1) * w + j
    MinCostFlow:addEdge(idx + h * w, idx + 1, 0, 2)
  end
end
for i = 1, h - 1 do
  for j = 1, w do
    local idx = (i - 1) * w + j
    MinCostFlow:addEdge(idx + h * w, idx + w, 0, 2)
  end
end
for i = 1, h do
  for j = 1, w do
    local idx = (i - 1) * w + j
    local a = io.read("*n")
    MinCostFlow:addEdge(idx, idx + h * w, inf, 1)
    MinCostFlow:addEdge(idx, idx + h * w, inf - a, 1)
  end
end
local ret = MinCostFlow:getMinCostFlow(2)
if h == 1 or w == 1 then
  ret = ret - inf * math.max(h, w)
else
  ret = ret - inf * 2 * (h + w - 1)
end
print(math.abs(ret))

Max Score: $600$ Points

Problem Statement

Sigma and his brother Sugim are in the $H \times W$ grid. They wants to buy some souvenirs.

Their start position is upper-left cell, and the goal position is lower-right cell.

Some cells has a souvenir shop. At $i$-th row and $j$-th column, there is $a_{i, j}$ souvenirs.

In one move, they can go left, right, down, and up cell.

But they have little time, so they can move only $H+W-2$ times.

They wanted to buy souvenirs as many as possible, but they had no computer, so they couldn't get the maximal numbers of souvenirs.

Write a program and calculate the maximum souvenirs they can get, and help them.

Input

The input is given from standard input in the following format.

$H \ W$
$a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}$
$a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}$
$\vdots \ \ \ \ \ \ \ \ \ \ \vdots \ \ \ \ \ \ \ \ \ \ \vdots$
$a_{H, 1} \ a_{H, 2} \ \cdots \ a_{H, W}$

Output

Print the maximum number of souvenirs they can get.

Constraints

$1 \le H, W \le 200$

$0 \le a_{i, j} \le 10^5$

Subtasks

Subtask 1 [ 50 points ]

The testcase in the subtask satisfies $1 \le H \le 2$.

Subtask 2 [ 80 points ]

The testcase in the subtask satisfies $1 \le H \le 3$.

Subtask 3 [ 120 points ]

The testcase in the subtask satisfies $1 \le H, W \le 7$.

Subtask 4 [ 150 points ]

The testcase in the subtask satisfies $1 \le H, W \le 30$.

Subtask 5 [ 200 points ]

There are no additional constraints.

Sample Input 1

3 3
1 0 5
2 2 3
4 2 4

Sample Output 1

21

The cell at $i$-th row and $j$-th column is denoted $(i, j)$.

In this case, one of the optimal solution is this:

Sigma moves $(1, 1) -> (1, 2) -> (1, 3) -> (2, 3) -> (3, 3)$.

Sugim moves $(1, 1) -> (2, 1) -> (3, 1) -> (3, 2) -> (3, 3)$.

Then, they can get $21$ souvernirs.

Sample Input 2

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

Sample Output 2

97

Writer : square1001


a, b, c = io.read("*n", "*n", "*n")
m = a + b + c
if a + a == m or b + b == m or c + c == m then
  print("Yes")
else
  print("No")
end

Score : 100 points

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

There are three candy packs, each of which contains a, b, and c candies, respectively.

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

Constraints

1 ≦ a, b, c ≦ 100

Input

The input is given from Standard Input in the following format:

a b c

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

Sample Input 1

10 30 20

Sample Output 1

Yes

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

Sample Input 2

30 30 100

Sample Output 2

No

In this case, the student who gets the pack with 100 candies always has more candies than the other.

Note that every pack must be given to one of them.

Sample Input 3

56 25 31

Sample Output 3

Yes


s = io.read("*l")
l = string.len(s)
t = {string.byte(s, 1, -1)}
ans = 0
for i = 1, l-1 do
	if t[i] ~= t[i+1] then
		ans = ans + 1
	end
end
print(ans)


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

sum = a + b + c
max = math.max(a, b, c)
if sum - max == max then
	print("Yes")
else
	print("No")
end


local w, h, n = io.read("*n", "*n", "*n")
local l, r, t, b = 0, w, 0, h
for i = 1, n do
  local x, y, a = io.read("*n", "*n", "*n")
  if a == 1 then
    l = math.max(l, x)
  elseif a == 2 then
    r = math.min(r, x)
  elseif a == 3 then
    t = math.max(t, y)
  else
    b = math.min(b, y)
  end
end
r = math.max(l, r)
b = math.max(t, b)
print((r - l) * (b - t))

Score : 200 points

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

Then, he created an integer sequence a of length N, and for each 1 ≦ i ≦ N, he painted some region within the rectangle black, as follows:

If a_i = 1, he painted the region satisfying x < x_i within the rectangle.

If a_i = 2, he painted the region satisfying x > x_i within the rectangle.

If a_i = 3, he painted the region satisfying y < y_i within the rectangle.

If a_i = 4, he painted the region satisfying y > y_i within the rectangle.

Find the area of the white region within the rectangle after he finished painting.

Constraints

1 ≦ W, H ≦ 100

1 ≦ N ≦ 100

0 ≦ x_i ≦ W (1 ≦ i ≦ N)

0 ≦ y_i ≦ H (1 ≦ i ≦ N)

W, H (21:32, added), x_i and y_i are integers.

a_i (1 ≦ i ≦ N) is 1, 2, 3 or 4.

Input

The input is given from Standard Input in the following format:

W H N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_N y_N a_N

Output

Print the area of the white region within the rectangle after Snuke finished painting.

Sample Input 1

5 4 2
2 1 1
3 3 4

Sample Output 1

9

The figure below shows the rectangle before Snuke starts painting.

First, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:

Then, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:

Now, the area of the white region within the rectangle is 9.

Sample Input 2

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

Sample Output 2

0

It is possible that the whole region within the rectangle is painted black.

Sample Input 3

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

Sample Output 3

64