local n,k=io.read("n","n","l")
local s=io.read()
local t={1}
for i=2,#s do
    if s:byte(i-1)~=s:byte(i) then
        table.insert(t,i)
    end
end

local max=0
for i=1,#t do
    local x
    if s:byte(t[i])==48 then
        x=(t[i+2*k] or #s+1)-t[i]
    else
        x=(t[i+2*k+1] or #s+1)-t[i]
    end
    max=math.max(x,max)
end
print(max)

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.


n, k = io.read("*n", "*n", "*l")
s = io.read()
pos = 1
poslist = {}
cnt = 0
p1, p2 = string.find(s, "1+", pos)
while(p1 ~= nil) do
  cnt = cnt + 1
  poslist[cnt] = {}
  poslist[cnt].fst, poslist[cnt].lst = p1, p2
  pos = p2 + 1
  p1, p2 = string.find(s, "1+", pos)
end

if(#poslist < k) then
  print(n)
else
  res = 0
  for i = 1, #poslist - k do
   res = math.max(res, poslist[i + k].lst - poslist[i].fst + 1)
  end
  res = math.max(res, poslist[k].lst)
  res = math.max(res, n - poslist[#poslist - k + 1].fst + 1)
  print(res)
end

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

Submitted source-code file size, in bytes.

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

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


a = io.read("*n")
b = 0
for i = 2, 5 do b = io.read("*n") end
c = io.read("*n")
print(c < b - a and ":(" or "Yay!")

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


a=io.read("*n")
b=io.read("*n")
c=io.read("*n")
d=io.read("*n")
e=io.read("*n")
k=io.read("*n")
if e-a<=k then
  print("Yay!")
else
  print(":(")
end


a={}
for i = 1,5 do
	a[i]=io.read()*1
end
print(a[5]-a[1]>io.read()*1 and ":("or"Yay!")


-- B
local a,b,c,d,e = io.read("n", "n", "n", "n", "n")
local tbl = {a,b,c,d,e}
table.sort(tbl, function(x,y)
    local rx = x % 10
    if rx == 0 then
        rx = 10
    end
    local ry = y % 10
    if ry == 0 then
        ry = 10
    end
    return rx > ry
end)

local tm = 0
for i=1, #tbl-1 do
    local r = 10 - tbl[i] % 10
    if r == 10 then
        r = 0
    end
    tm = tm + tbl[i] + r
end
tm = tm + tbl[#tbl]

print(tm)

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


local n=io.read("*n")
local min=10^15+1
for i=1,5 do
    min=math.min(min,io.read("*n"))
end
print(n//min+(n%min>0 and 1 or 0)+4)

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

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


local n=io.read("*n")
local min=10^15+1
for i=1,5 do
    min=math.min(min,io.read("*n"))
end
print(n//min+(n%min and 1 or 0)+4)


local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

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

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- D
local X, Y, Z, K = io.read("n", "n", "n", "n")
local A, B, C = {}, {}, {}

for i, tbl in ipairs {A, B, C} do
    local nums = {X, Y, Z}
    local count = nums[i]
    for j=1, count do
        tbl[j] = io.read("n")
    end
    table.sort(tbl, function (x,y)
        return x > y
    end)
end

local function goodness(a, b, c)
    if a > #A or b > #B or c > #C then
        return -10000000000000
    end
    return A[a] + B[b] + C[c]
end

local visited = {}
local function visit(a,b,c)
    visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)] = true
end
local function isvisited(a,b,c)
    return visited[tostring(a) .. "*" .. tostring(b) .. "*" .. tostring(c)]
end

-- most good comb right
local items = {}
local function item_sort(tbl)
    table.sort(items, function(x,y)
        return x[4] < y[4]
    end)
end
local function items_count()
    return #items
end
local function items_add(a, b, c)
    table.insert(items, {a,b,c,goodness(a,b,c)})
    return true
end
local function check_add(a,b,c)
    if a > #A or b > #B or c > #C then
        return -1
    end
    if isvisited(a, b, c) then
        return -2
    end
    visit(a,b,c)
    items_add(a, b, c)
end
local ans = {}
local a, b, c = 1, 1, 1
check_add(a, b, c)
while #ans < K do
    local e = table.remove(items)
    table.insert(ans, e[4])
    local a,b,c = e[1], e[2], e[3]
    check_add(a+1,b,c)
    check_add(a,b+1,c)
    check_add(a,b,c+1)
    item_sort(items)
end

--table.sort(ans, function(x,y) return x>y end)
for i=1,K do
    print(ans[i])
end

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

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


local n, q = io.read("*n", "*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do t[i] = s:sub(i, i):byte() end
local target, way = {}, {}
for i = 1, q do
  local strt, strway = io.read():match("(%w) (%w)")
  target[i] = strt:byte()
  way[i] = strway == "R"
end
local function solve(x)
  local result = 0
  for i = 1, q do
    if t[x] == target[i] then
      if way[i] then
        if x < n then x = x + 1
        else result = 1 break
        end
      else
        if 1 < x then x = x - 1
        else result = -1 break
        end
      end
    end
  end
  return result
end
local leftok = n + 1
if 0 <= solve(n) then
  leftok = n
  if 0 <= solve(1) then
    leftok = 1
  else
    local leftng = 1
    while 1 < leftok - leftng do
      local leftmid = (leftok + leftng) // 2
      if 0 <= solve(leftmid) then
        leftok = leftmid
      else
        leftng = leftmid
      end
    end
  end
end
local rightok = 0
if solve(1) <= 0 then
  rightok = 1
  if solve(n) <= 0 then
    rightok = n
  else
    local rightng = n
    while 1 < rightng - rightok do
      local rightmid = (rightng + rightok) // 2
      if solve(rightmid) <= 0 then
        rightok = rightmid
      else
        rightng = rightmid
      end
    end
  end
end
print(math.max(0, rightok - leftok + 1))

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


-- C
local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

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

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

-- https://golang.org/src/sort/search.go?s=2246:2286#L49
local function find_min_true(n, f)
    local function range_ok(x)
        return x >= 1 and x <= n
    end
    local function loop(i, j)
        -- Define f(0) == false, f(n+1) == true
        -- Invariant: f(i-1) == false and f(j) == true
        if i == j then
            assert(i >= 1 and i <= n + 1) -- i == n + 1 is valid
            -- i == j, f(i-1) == false, f(i) == true
            -- i is the minimum number that f(i) == true
            return i
        end
        local h = (i + j) // 2
        assert(h >= 1 and h <= n)
        if f(h) then
            -- f(h) == true
            -- j = h ==> f(j) == true
            return loop(i, h)
        else
            -- f(h) == false
            -- i = h + 1 ==> f(i-1) == false
            return loop(h + 1, j)
        end
    end
    -- f(i-1) == f(0) == false
    -- f(j) == f(n+1) == true
    return loop(1, n + 1)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end

local N, Q, _ = io.read("n", "n", "l")
local s = io.read("l")
assert(#s == N)
local spells = {}
for i=1,Q do
    local t, _, d, _ = io.read(1, 1, 1, "l")
    spells[i] = {t, d}
end

local place = str2tbl(s)

local function simulation_leftdeath(pos)
    for i=1,Q do
        local t, d = spells[i][1], spells[i][2]
        if place[pos] == t then
            if d == 'L' then
                pos = pos - 1
            else
                pos = pos + 1
            end
            if pos == 0 then
                return true
            end
            if pos == N + 1 then
                return false
            end
        end
    end
    return false
end
local function simulation_rightdeath(pos)
    for i=1,Q do
        local t, d = spells[i][1], spells[i][2]
        if place[pos] == t then
            if d == 'L' then
                pos = pos - 1
            else
                pos = pos + 1
            end
            if pos == 0 then
                return false
            end
            if pos == N + 1 then
                return true
            end
        end
    end
    return false
end

local rightmost_dead_of_left = -1 + find_min_true(N, function(x)
    return simulation_leftdeath(x) == false
end)

local leftmost_dead_of_right = find_min_true(N, function(x)
    return simulation_rightdeath(x) == true
end)

local dead = 0
for i=1,N do
    if i <= rightmost_dead_of_left or i >= leftmost_dead_of_right then
        dead = dead + 1
    end
end
print(N - dead)


-- C
local DBG = true
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

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

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end

local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

local function each_char(s)
    local f = function(_, i)
        i = i + 1
        if i <= #s then
            return i, string.sub(s, i, i)
        end
    end
    return f, nil, 0
end
local N, Q, _ = io.read("n", "n", "l")
local s = io.read("l")
assert(#s == N)
local spells = {}
for i=1,Q do
    local t, _, d, _ = io.read(1, 1, 1, "l")
    spells[i] = {t, d}
end

local place = str2tbl(s)

local paths = {}

local function new_path(pos)
    local p = {pos, 1}
    return p
end
local function push_path(p)
    table.insert(paths, p)
end

local death = {}
for i=1,N do
    death[i] = false
end

for i=Q,1,-1 do
    local t, d = spells[i][1], spells[i][2]
    local flag = true
    if d == 'L' then
        if t == place[1] and not left then
            local p = new_path(1)
            push_path(p)
            death[1] = true
            flag = false
            left = true
        end
    else
        if t == place[N] and not right then
            local p = new_path(N)
            push_path(p)
            death[N] = true
            flag = false
            right = true
        end
    end
    if flag then
        for _,p in ipairs(paths) do
            local pos, count = p[1], p[2]
            if d == 'L' then
                if t == place[pos+1] then
                    p[1], p[2] = pos+1, count+1
                    death[pos+1] = true
                end
            else
                if t == place[pos-1] then
                    p[1], p[2] = pos-1, count+1
                    death[pos-1] = true
                end
            end
        end
    end
end
local dead = 0
for i=1,N do
    if death[i] then
        dead = dead + 1
    end
end
print(N - dead)


local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

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

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

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

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

local fact = {1}
local invs = {1}
local invfact = {1}
for i = 2, 100010 do
  fact[i] = bmul(fact[i - 1], i)
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invfact[i] = bmul(invfact[i - 1], invs[i])
end

local function getPerm(n, k)
  if n == 0 then return 1 end
  if k == n then return fact[n] end
  return bmul(fact[n], invfact[n - k])
end

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

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

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

local n, x = io.read("*n", "*n")
local s = {}
local tbl = {}
for i = 1, n do
  s[i] = io.read("*n")
  tbl[i] = {}
end
table.sort(s)
for i = n, 1, -1 do
  local rem = x % s[i]
  if 0 < rem then
    tbl[i][rem] = getPerm(n - 1, n - i)
  end
end
local ret = 0
for src = n, 2, -1 do
  for val, cnt in pairs(tbl[src]) do
    local ub = upper_bound(s, val)
    if ub == 1 then
      ret = badd(ret, bmul(val, bmul(fact[src - 1], cnt)))
    else
      local skipfact = getPerm(src - 1, src - ub)
      cnt = bmul(cnt, skipfact)
      for dst = ub - 1, 1, -1 do
        local vc = bmul(cnt, getPerm(ub - 2, ub - 1 - dst))
        local rem = val % s[dst]
        if 0 < rem then
          if tbl[dst][rem] then
            tbl[dst][rem] = badd(tbl[dst][rem], vc)
          else
            tbl[dst][rem] = vc
          end
        end
      end
    end
  end
end
for val, cnt in pairs(tbl[1]) do
  ret = badd(ret, bmul(val, cnt))
end
print(ret)

Score : 600 points

Problem Statement

Snuke has a blackboard and a set S consisting of N integers.
The i-th element in S is S_i.

He wrote an integer X on the blackboard, then performed the following operation N times:

Choose one element from S and remove it.

Let x be the number written on the blackboard now, and y be the integer removed from S. Replace the number on the blackboard with x \bmod {y}.

There are N! possible orders in which the elements are removed from S.
For each of them, find the number that would be written on the blackboard after the N operations, and compute the sum of all those N! numbers modulo 10^{9}+7.

Constraints

All values in input are integers.

1 \leq N \leq 200

1 \leq S_i, X \leq 10^{5}

S_i are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N X
S_1 S_2 \ldots S_{N}

Output

Print the answer.

Sample Input 1

2 19
3 7

Sample Output 1

3

There are two possible orders in which we remove the numbers from S.

If we remove 3 and 7 in this order, the number on the blackboard changes as follows: 19 \rightarrow 1 \rightarrow 1.

If we remove 7 and 3 in this order, the number on the blackboard changes as follows: 19 \rightarrow 5 \rightarrow 2.

The output should be the sum of these: 3.

Sample Input 2

5 82
22 11 6 5 13

Sample Output 2

288

Sample Input 3

10 100000
50000 50001 50002 50003 50004 50005 50006 50007 50008 50009

Sample Output 3

279669259

Be sure to compute the sum modulo 10^{9}+7.


local mod = 1000000007
local mfl = math.floor
local mmi = math.min

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

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

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

local n, x = io.read("*n", "*n")
local fact = {1}
for i = 2, 200 do
  fact[i] = bmul(fact[i - 1], i)
end
local combfact = {}
for i = 1, 200 do
  combfact[i] = {}
  local c = 1
  for j = 1, i do
    c = bmul(c, i + 1 - j)
    c = bmul(c, modinv(j))
    combfact[i][j] = bmul(c, fact[j])
  end
end
local function getCombFact(n, k)
  if n == 0 then return 1 end
  if k == 0 then return 1 end
  return combfact[n][k]
end

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

for src = n, 1, -1 do
  for val, cnt in pairs(t[src]) do
    for dst = src - 1, 1, -1 do
      local k = cnt
      local nval = val % a[dst]
      local step = src - dst - 1
      k = bmul(k, getCombFact(src - 2, step))
      local precnt = t[dst][nval]
      if precnt then
        t[dst][nval] = badd(precnt, k)
      else
        t[dst][nval] = k
      end
    end
  end
end
local ret = 0
for k, v in pairs(t[1]) do
  ret = badd(ret, bmul(k, v))
end
print(ret)


s = io.read()
if(s == "A") then print("T") elseif(s == "T") then print ("A") elseif(s == "G") then print("C") else print("G") end

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


local S = io.read().."X"

local length = 0
local counter = 0

for i = 1, #S do
    local PS = string.sub(S,i,i)
    if PS == "A" or PS == "C" or PS == "G" or PS == "T" then
        counter = counter + 1
    else
        length = math.max(counter, length)
        counter = 0
    end
end

print(length)

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

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

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


local n,q=io.read("n","n","l")
local s=io.read()
local t={0}
for i=1,n-1 do
    t[i+1]=t[i]+(s:sub(i,i+1)=="AC" and 1 or 0)
end
for i=1,q do
    local l,r=io.read("n","n")
    print(t[r]-t[l])
end

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


n,q=io.read():match("(.+)%s(.+)")
s=io.read()
for i=1,q do
  l,r=io.read():match("(.+)%s(.+)")
  x,y=s:sub(l,r):gsub("AC","")
  print(y)
end


local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

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

local function dbgpr_tp(tbl)
    dbgpr_t(tbl, true)
end
-----------------------------------------
local MOD = 1000000007

local function pow4(n)
    local a = 1
    for i=1,n do
        a = a * 4 % MOD
    end
    return a
end

local ng_a, ng_c, ng_g, ng_t, ng

local ng_a_tbl = {0,0,2}
ng_a = function(k)
    dbgpr("ng_a", k)
    if ng_a_tbl[k] ~= nil then
        return ng_a_tbl[k]
    end
    local x = (ng_a(k-1) + pow4(k-3) + ng_t(k-1) + pow4(k-3)) % MOD
    ng_a_tbl[k] = x
    return x
end

local ng_c_tbl = {0,0,0}
ng_c = function(k)
    dbgpr("ng_c", k)
    if ng_c_tbl[k] ~= nil then
        return ng_c_tbl[k]
    end
    local x = ng(k-1)
    ng_c_tbl[k] = x
    return x
end

local ng_t_tbl = {0,0,0}
ng_t = function(k)
    dbgpr("ng_t", k)
    if ng_t_tbl[k] ~= nil then
        return ng_t_tbl[k]
    end
    local x = ng(k-1)
    ng_t_tbl[k] = x
    return x
end

local ng_g_tbl = {0,0,1}
ng_g = function(k)
    dbgpr("ng_g", k)
    if ng_g_tbl[k] ~= nil then
        return ng_g_tbl[k]
    end
    local x = (ng_c(k-1) + ng_g(k-1) + ng_t(k-1) + pow4(k-3)) % MOD
    ng_g_tbl[k] = x
    return x
end

local ng_tbl = {0,0,3}
ng = function(k)
    dbgpr("ng", k)
    if ng_tbl[k] ~= nil then
        return ng_tbl[k]
    end
    local x = (ng_a(k) + ng_c(k) + ng_g(k) + ng_t(k)) % MOD
    ng_tbl[k] = x
    return x
end

local function ok(k)
    return (pow4(k) - ng(k) + MOD) % MOD
end

--for i=3,6 do
--    print(i, ok(i))
--end

local N = io.read("n")
print(ok(N))

Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

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


local n = io.read("n")
local b = {}
for i = 1, n do
    b[i] = io.read("n")
end
local a = {}
for i = n,1,-1 do
    for j = i,1,-1 do
        if b[j] == j then
            a[i] = j
            table.remove(b, j)
            break
        end
    end
    if #b == i then
        print(-1)
        return 0
    end
end
for i = 1,n do
    print(a[i])
end

Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1


local n=io.read("n","l")
local t={}
for _,a in utf8.codes(io.read()) do
    if not t[a] then
        t[a]=1
    else
        t[a]=t[a]+1
    end
end

local combination=1
for _,frequency in pairs(t) do
    combination=combination*(frequency+1)%1000000007
end
print(combination-1)

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


local mod = 1000000007
local function badd(x, y)
  return (x + y) % mod
end
local n = io.read("*n")
local t = {}
local colors = {}
local pos = {}
for i = 1, n do
  local c = io.read("*n")
  t[i] = c
  if not colors[c] then colors[c] = {} end
  table.insert(colors[c], i)
  pos[i] = #colors[c]
end
local cnt = {}
for i = 1, n do
  cnt[i] = 1
end
for i = n - 1, 1, -1 do
  local c, p = t[i], pos[i]
  if p < #colors[c] and i + 1 < colors[c][p + 1] then
    cnt[i] = badd(cnt[i + 1], cnt[colors[c][p + 1]])
  else
    cnt[i] = cnt[i + 1]
  end
end
print(cnt[1])

Score : 700 points

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

Snuke will perform the following operation zero or more times:

Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

Constraints

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

1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1
:
C_N

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

Sample Input 1

5
1
2
1
2
2

Sample Output 1

3

We can make three sequences of colors of stones, as follows:

(1,2,1,2,2), by doing nothing.

(1,1,1,2,2), by choosing the first and third stones to perform the operation.

(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

Sample Input 2

6
4
2
5
4
2
4

Sample Output 2

5

Sample Input 3

7
1
3
1
2
3
3
2

Sample Output 3

5


Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


n,m,c=io.read("*n", "*n", "*n")
b={}
for i = 1, m do
    b[i] = io.read("*n")
end
cnt = 0
for j = 1, n do
    ans = c
    for i = 1, m do
        ans = ans + b[i] * io.read("*n")
    end
    if(0 < ans) then cnt = cnt + 1 end
end
print(cnt)

Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

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

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


n,m=io.read("*n","*n","*l")
a={}
for i=1,n do
    a[i]={}
    a[i][1],a[i][2]=io.read("*n","*n")
end
table.sort(a,function(x,y)
    if x[1]~=y[1] then
        return x[1]<y[1]
    else
        return x[2]>y[2]
    end
end)

drink=0
yen=0
for i=1,n do
    if m-drink<a[i][2] then
        yen=yen+a[i][1]*(m-drink)
        break
    else
        drink=drink+a[i][2]
        yen=yen+a[i][1]*a[i][2]
    end
end
print(yen)

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

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


A,B,K=io.read("n","n","n")
function d(a,b)
    if a%b==0 then
        return b
    end
    local c=a%b
    return d(b,c)
end
local mod=d(A,B)
local count=0
for i=mod,1,-1 do
    if A%i==0 and B%i==0 then
        count=count+1
    end
    if count==K then
        print(i)
        break
    end
end

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


s=io.read()
red=0
blue=0
for i=1,#s do
    x=tonumber(s:sub(i,i))
    if x==0 then
        red=red+1
    else
        blue=blue+1
    end
end
print(2*math.min(red,blue))

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


s=io.read()
red=0
blue=0
for i=1,#s do
    x=tonumber(s:sub(i,i))
    if x==0 then
        red=red+1
    else
        blue=blue+1
    end
end
print(math.min(red,blue))


local UnionFind={}

UnionFind.create=function(self,n)
    self.par={}
    self.rank={}
    self.size={}
    for i=1,n do
        self.par[i]=i
        self.rank[i]=1
        self.size[i]=1
    end
end

UnionFind.getroot=function(self,x)
    local nx=x
    while self.par[x]~=x do
        x=self.par[x]
    end
    while self.par[nx]~=x do
        self.par[nx],nx=x,self.par[nx]
    end
    return x
end

UnionFind.same=function(self,x,y)
    return self:getroot(x)==self:getroot(y)
end

UnionFind.unite=function(self,x,y)
    x=self:getroot(x)
    y=self:getroot(y)
    if x==y then
        return
    end
    if self.rank[x]>self.rank[y] then
        x,y=y,x
    elseif self.rank[x]==self.rank[y] then
        self.rank[x]=self.rank[x]+1
    end
    self.par[x]=y
    self.size[y]=self.size[y]+self.size[x]
end

UnionFind.getsize=function(self,x)
    return self.size[self:getroot(x)]
end

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

----------

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

local t={[m]=n*(n-1)//2}
local uf=UnionFind:new(n)
for i=m-1,1,-1 do
    if uf:same(a[i+1],b[i+1]) then
        t[i]=t[i+1]
    else
        local ua=uf:getsize(a[i+1])
        local ub=uf:getsize(b[i+1])
        uf:unite(a[i+1],b[i+1])
        t[i]=t[i+1]-ua*ub
    end
end

for i=1,m do
    print(t[i])
end

Score : 400 points

Problem Statement

There are N islands and M bridges.

The i-th bridge connects the A_i-th and B_i-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.

Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.

For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

All pairs (A_i, B_i) are distinct.

The inconvenience is initially 0.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 1

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

Sample Output 1

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4).

Sample Input 2

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

Sample Output 2

8
9
12
14
15

Sample Input 3

2 1
1 2

Sample Output 3

1


s=io.read()
b=tonumber(s:sub(6,7))
if b<5 then
	print("Heisei")
else
	print("TBD")
end

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


S=io.read()
b=tonumber(string.sub(S,6,7))
print(b)
if b<=4 then
  print("Heisei")
else
  print("TBD")
end


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

Score : 200 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

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

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

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

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

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

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

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

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

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

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


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


local n = io.read("n")
local c = io.read("n")
local b = io.read("n")
local a = io.read("n")
function pay(x,y,z)
    return math.abs(x-a)+math.abs(y-b)+math.abs(z-c)
end
local bamboos = {}
function dfs(cur,a,b,c)
    if cur == #bamboos+1 then
        return math.min(a,b,c) > 0 and pay(a,b,c) - 30 or math.huge
    end
    local vcur = bamboos[cur]
    local toa = 10 + dfs(cur+1, a+vcur, b, c)
    local tob = 10 + dfs(cur+1, a, b+vcur, c)
    local toc = 10 + dfs(cur+1, a, b, c+vcur)
    local skip = dfs(cur+1, a, b, c)
    return math.min(toa, tob, toc, skip)
end

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

Score : 300 points

Problem Statement

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

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

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

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

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

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

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

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

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

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

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

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

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

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

Sample Input 2

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

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

243


a, b = io.read("*n", "*n")
print(b % a == 0 and a + b or b - a)

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


a,b=io.read():match("(.+)%s(.+)")
print(string.format("%d",b%a==0 and b+a or b-a))


N=io.read("n")
M=io.read("n")
k={}
A={}
t={}
for i=1,N do
  k[i]=io.read("n")
  A[i]={}
  for j=1,k[i] do
    A[i][j]=io.read("n")
    t[A[i][j]]=(t[A[i][j]] or 0)+1
  end
end
local count=0
for i,v in pairs(t) do
  if v==M then
    count=count+1
  end
end
print(count)

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

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

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

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

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

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

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

Constraints

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

1

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

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


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

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

Score : 300 points

Problem Statement

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

Initially, the health of Monster i is A_i.

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

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

A random alive monster attacks another random alive monster.

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

4
2 10 8 40

Sample Output 1

2

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

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end
local n = io.read("*n")
local a = io.read("*n")
for i = 2, n do
  local b = io.read("*n")
  a = getgcd(a, b)
end
print(a)


function gcd(a,b)
  if a<b then
    return gcd(b,a)
  elseif a%b==0 then
    return b
  else
    return gcd(b,a%b)
  end
end
n=io.read():match("%d+")
for i in io.read():gmatch("%d+") do
  if ans==nil then
    ans=i*1
  end
  ans=gcd(ans,i*1)
end
print(string.format("%d",ans))


function parse_table(N)
    local tbl = {}
    for i=1,N do
        table.insert(tbl, io.read("n"))
    end
    return tbl
end
function gcd(a,b)
    if b == 0 then
        return a
    else
        return gcd(b, a % b)
    end
end
function gcd_tbl(tbl)
    local ans = tbl[1]
    for i=2,#tbl do
        ans = gcd(ans, tbl[i])
    end
    return ans
end
function main()
    local n = io.read("n")
    io.read("l")
    local hptbl = parse_table(n)
    local ans = gcd_tbl(hptbl)
    print(ans)
end
main()


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        for i,v in ipairs(tbl) do
            io.write("[dbg]")
            print(i,v)
        end
    end
end
function swap(tbl, i, j)
    local oldj = tbl[j]
    tbl[j] = tbl[i]
    tbl[i] = oldj
end
function parse_table(N)
    local tbl = {}
    for i=1,N do
        table.insert(tbl, io.read("n"))
    end
    return tbl
end
function getline()
    local k = io.read("n")
    local tbl = parse_table(k)
    io.read("l")
    return tbl
end
function attack(hptbl)
    hptbl[2] = hptbl[2] % hptbl[1]
    if hptbl[2] == 0 then
        table.remove(hptbl, 2)
    else
        swap(hptbl, 1, 2)
    end
end
function main()
    local n = io.read("n")
    io.read("l")
    local hptbl = parse_table(n)
    table.sort(hptbl)
    dbgpr_t(hptbl)
    while #hptbl > 1 do
        attack(hptbl)
        dbgpr_t(hptbl)
    end
    print(hptbl[1])
end
main()


local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end
function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        for i,v in ipairs(tbl) do
            io.write("[dbg]")
            print(i,v)
        end
    end
end
function parse_table(N)
    local tbl = {}
    for i=1,N do
        table.insert(tbl, io.read("n"))
    end
    return tbl
end
function getline()
    local k = io.read("n")
    local tbl = parse_table(k)
    io.read("l")
    return tbl
end
function attack(hptbl)
    hptbl[2] = hptbl[2] % hptbl[1]
    if hptbl[2] == 0 then
        table.remove(hptbl, 2)
    end
    table.sort(hptbl)
end
function main()
    local n = io.read("n")
    io.read("l")
    local hptbl = parse_table(n)
    table.sort(hptbl)
    dbgpr_t(hptbl)
    while #hptbl > 1 do
        attack(hptbl)
        dbgpr_t(hptbl)
    end
    print(hptbl[1])
end
main()


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

Score : 400 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

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

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

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

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

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

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

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

Sample Input 3

15 3
5 4 6

Sample Output 3

654


local k, a, b = io.read("*n", "*n", "*n")
if b - a <= 2 then
  print(k + 1)
elseif k < a + 1 then
  print(k + 1)
else
  k = k - (a - 1)
  local mul = k // 2
  print(a + mul * (b - a) + k % 2)
end

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

1 \leq K,A,B \leq 10^9

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099


Score : 100 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

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

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

Sample Input 1

8 3

Sample Output 1

2.6666666667

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

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

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


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

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

function main()
    local N, M, goals = parse_problem()
    table.sort(goals)
    local INF = 10000000
    local diffs = {}
    for i=1, #goals-1 do
        diffs[i] = goals[i + 1] - goals[i]
    end
    table.sort(diffs)
    dbgpr_t(goals)
    dbgpr_t(diffs)
    if N >= M then
        print(0)
    else
        local sum = 0
        for i=1, M-N do
            sum = sum + diffs[i]
        end
        print(sum)
    end
end
main()

Score : 300 points

Problem Statement

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

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

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

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

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

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

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

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

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

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

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

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

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


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

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

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

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


local n, m = io.read("*n", "*n")
local edge_c, edge_p_cnt = {}, {}
local p = {}
for i = 1, n do
  edge_c[i] = {}
  edge_p_cnt[i] = 0
  p[i] = 0
end
for i = 1, n + m - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge_c[a], b)
  edge_p_cnt[b] = edge_p_cnt[b] + 1
end

local tasks = {}
for i = 1, n do
  if edge_p_cnt[i] == 0 then
    p[i] = 0
    table.insert(tasks, i)
  end
end

while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  for i = 1, #edge_c[src] do
    local dst = edge_c[src][i]
    edge_p_cnt[dst] = edge_p_cnt[dst] - 1
    if edge_p_cnt[dst] == 0 then
      p[dst] = src
      table.insert(tasks, dst)
    end
  end
end
for i = 1, n do
  print(p[i])
end

Score : 500 points

Problem Statement

There is a rooted tree (see Notes) with N vertices numbered 1 to N.
Each of the vertices, except the root, has a directed edge coming from its parent.
Note that the root may not be Vertex 1.

Takahashi has added M new directed edges to this graph.
Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.

You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges.
More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.

Restore the original rooted tree.

Notes

For "tree" and other related terms in graph theory, see the article in Wikipedia, for example.

Constraints

3 \leq N

1 \leq M

N + M \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

If i \neq j, (A_i, B_i) \neq (A_j, B_j).

The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

Output

Print N lines.
In the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

Note that it can be shown that the original tree is uniquely determined.

Sample Input 1

3 1
1 2
1 3
2 3

Sample Output 1

0
1
2

The graph in this input is shown below:

It can be seen that this graph is obtained by adding the edge 1 \rightarrow 3 to the rooted tree 1 \rightarrow 2 \rightarrow 3.

Sample Input 2

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

Sample Output 2

6
4
2
0
6
2


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

Score : 100 points

Problem Statement

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

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

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

Constraints

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

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


local a = {}
a[1] = io.read("*n")

local function f(n)
    local x
    if n%2==0 then
        return n/2
    else
        return 3*n+1
    end
end

for i = 2, 1000000 do
   a[i] = f(a[i-1])
end

for i = 1, 1000000 do
    if a[i]==4 then
        print(i+3)
        break
    end
end

Score : 200 points

Problem Statement

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

The first term s is given as input.

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

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

Find the minimum integer m that satisfies the following condition:

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

Constraints

1 \leq s \leq 100

All values in input are integers.

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

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

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

Sample Input 2

7

Sample Output 2

18

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

Sample Input 3

54

Sample Output 3

114


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


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


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


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


local n = io.read("*n")
local h = {}
for i = 1, n do h[i] = io.read("*n") end
local tasks = {}
local tasknum, done = 0, 0
table.insert(tasks, 1 + n * 101)
tasknum = 1

local function getmin(l, r)
  local min, minpos = h[l], l
  for i = l + 1, r do
    if(h[i] < min) then
      min, minpos = h[i], i
    end
  end
  return min, minpos
end

local tot = 0
local curtask = 0
while(done < tasknum) do
  done = done + 1
  local task = tasks[done]
  local left, right = task % 101, task // 101
  local min, minpos = getmin(left, right)
  for i =left, right do
    h[i] = h[i] - min
  end
  if(left < minpos) then
    tasknum = tasknum + 1
    table.insert(tasks, left + (minpos - 1) * 101)
  end
  if(minpos < right) then
    tasknum = tasknum + 1
    table.insert(tasks, minpos + 1 + right * 101)
  end
  tot = tot + min
end
print(tot)

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

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

Sample Input 1

4
1 2 2 1

Sample Output 1

2

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

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

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

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


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


local n = io.read("n")
local k = io.read("n")
local list = {}
for _ = 1, n do
	local t = io.read("n")
	local d = io.read("n")
	table.insert(list, {t, d})
end
table.sort(list, function(a, b)
	return a[2] > b[2]
end)

local type2num = {}
local xcnt = 0
local total = 0
for i = 1, k do
	local node = list[i]
	local t = node[1]
	local d = node[2]
	local num = type2num[t]
	if not num then
		xcnt = xcnt + 1
		num = 0
	end
	num = num + 1
	type2num[t] = num
	total = total + d
end

local g = total + xcnt * xcnt
local maxv = g
local sp1 = k
local sp2 = k + 1
for i = xcnt + 1, n do
	local v1, v2
	while (sp1 > 0) do
		local node = list[sp1]
		sp1 = sp1 - 1
		local t = node[1]
		if type2num[t] > 1 then
			v1 = node[2]
			type2num[t] = type2num[t] - 1
			break
		end
	end
	while (sp2 <= n) do
		local node = list[sp2]
		sp2 = sp2 + 1
		local t = node[1]
		if not type2num[t] then
			v2 = node[2]
			type2num[t] = 1
			break
		end
	end
	if v1 and v2 then
		total = total - v1 + v2
		g = total + i * i
		if g > maxv then
			maxv = g
		end
	else
		break
	end
end

print(maxv)

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

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

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

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

Sample Output 1

26

If you eat Sushi 1,2 and 3:

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

The variety bonus is 2*2=4.

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

Sample Input 2

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

Sample Output 2

25

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

Sample Input 3

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

Sample Output 3

4900000016

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


s=io.read()
if s:match("%l*(keyence)") or s:match("(k)%l*(eyence)") or s:match("(ke)%l*(yence)") or s:match("(key)%l*(ence)") or s:match("(keye)%l*(nce)") or s:match("(keyen)%l*(ce)") or s:match("(keyenc)%l*(e)") or s:match("(keyence)%l*") then
    print("YES")
else
    print("NO")
end

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

Sample Input 1

keyofscience

Sample Output 1

YES

keyence is an abbreviation of key of science.

Sample Input 2

mpyszsbznf

Sample Output 2

NO

Sample Input 3

ashlfyha

Sample Output 3

NO

Sample Input 4

keyence

Sample Output 4

YES


N=io.read"*n"
A={}
for i=1,N do A[i]=io.read"*n"end
B={}
for i=1,N do B[i]=io.read"*n"end
as=0
bs=0
D={}
for i=1,N do
as=as+A[i]
bs=bs+B[i]
D[i]=A[i]-B[i]
end
if(as<bs)then print(-1)return end

table.sort(D)

dms=0
for i=1,N do if(D[i]>=0)then break end dms=dms+D[i] end

i=1
while(D[i]<0)do
	i=i+1
end

j=N
while(dms<0 and j>0)do
	dms=dms+D[j]
	j=j-1
end

print(i+N-j-1)

Score : 400 points

Problem Statement

A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i-th examination is A_{i}, and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i-th examination.

Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.

For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:

The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.

For every i, B_i \leq C_i holds.

If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

A_i and B_i are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N}
B_1 B_2 ... B_{N}

Output

Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Sample Input 1

3
2 3 5
3 4 1

Sample Output 1

3

(A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1). If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3, is achieved when:

(C_1, C_2, C_3) = (3, 5, 2)

Sample Input 2

3
2 3 3
2 2 1

Sample Output 2

0

In this case, he has to do nothing in order to pass all the exams.

Sample Input 3

3
17 7 1
25 6 14

Sample Output 3

-1

In this case, no matter what is done, he cannot pass all the exams.

Sample Input 4

12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212

Sample Output 4

5


Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

### ...
### ###
... ###

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


local ior = io.input()
local h, w = ior:read("*n", "*n", "*l")

local function getindex(i_h, i_w)
  return i_w + (i_h - 1) * w
end

local function createMap()
  local str = ""
  local map = {}
  for i_h = 1, h do
    str = ior:read()
    for i_w = 1, w do
      map[getindex(i_h, i_w)] = (str:sub(i_w, i_w) == "#")
    end
  end
  return map
end

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)
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  return idx
end

local map = createMap()
local parent = uf_initialize(h * w)

local function walk(src, dst)
  if map[src] ~= map[dst] then
    local sr, dr = uf_findroot(src, parent), uf_findroot(dst, parent)
    parent[src], parent[dst] = sr, dr
    if sr ~= dr then
      parent[dst], parent[dr] = sr, sr
    end
  end
end

for idx = 1, h * w do
  if w < idx then walk(idx, idx - w) end
  if idx <= (h - 1) * w then walk(idx, idx + w) end
  if 1 < w then
    if idx % w ~= 0 then walk(idx, idx + 1) end
    if idx % w ~= 1 then walk(idx, idx - 1) end
  end
end
local groups = {}
for idx = 1, h * w do
  local r = uf_findroot(idx, parent)
  if not groups[r] then
    local tmp = {0, 0}
    groups[r] = tmp
  end
  if map[idx] then
    groups[r][1] = groups[r][1] + 1
  else
    groups[r][2] = groups[r][2] + 1
  end
end
local ret = 0
for k, v in pairs(groups) do
  ret = ret + v[1] * v[2]
end
print(ret)

Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

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

Sample Output 3

6


local mfl = math.floor

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

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

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

local n, q = io.read("*n", "*n")
local a = {io.read("*n")}
local asum = {a[1]}
for i = 2, n do
  a[i] = io.read("*n")
  asum[i] = asum[i - 1] + a[i]
end
local oddsum = {a[1]}
for i = 2, n do
  if i % 2 == 0 then
    oddsum[i] = oddsum[i - 1]
  else
    oddsum[i] = oddsum[i - 1] + a[i]
  end
end

local function get_ao_state(x, taka_count)
  local p_right = n - taka_count
  local len = a[p_right] - x
  if len <= 0 then
    -- taka_count should be smaller
    return true, 0
  end
  local p_left = lower_bound(a, x - len)
  local need_ao_count = p_right - p_left + 1
  if taka_count < need_ao_count then
    -- taka_count should be larger
    return false
  else
    return true, need_ao_count
  end
end

for iq = 1, q do
  local x = io.read("*n")
  local x_lbpos = lower_bound(a, x)
  local taka_count_lim = n + 1 - x_lbpos
  local taka_count, ao_count = 0, 0
  if 0 < taka_count_lim then
    local f = get_ao_state(x, 0)
    if not f then
      local min, max = 0, taka_count_lim
      while 1 < max - min do
        local mid = mfl((max + min) / 2)
        if get_ao_state(x, mid) then
          max = mid
        else
          min = mid
        end
      end
      taka_count = max
    end
  end
  local _u, ao_count = get_ao_state(x, taka_count)
  if ao_count < taka_count - 1 then
    ao_count = taka_count - 1
  end
  local takasum = asum[n] - asum[n - taka_count]
  local rem = n - taka_count - ao_count
  if 0 < rem then
    if rem % 2 == 0 then
      if ao_count < taka_count then
        takasum = takasum + oddsum[n - taka_count - ao_count]
      else
        takasum = takasum + asum[n - taka_count - ao_count] - oddsum[n - taka_count - ao_count]
      end
    else
      if ao_count < taka_count then
        takasum = takasum + asum[n - taka_count - ao_count] - oddsum[n - taka_count - ao_count]
      else
        takasum = takasum + oddsum[n - taka_count - ao_count]
      end
    end
  end
  print(takasum)
end

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


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

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

for i=1,n do
    for j=1,3 do
        for k=1,3 do
            if j~=k then
                dp[i+1][k]=math.max(dp[i+1][k],dp[i][j]+t[i][k])
            end
        end
    end
end

print(math.max(table.unpack(dp[n+1])))

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.