local s = io.read("l")
local t = {string.byte(s,1,#s)}
if t[1] == t[2] or t[2] == t[3] or t[3] == t[4] then
    print("Bad")
else
    print("Good")
end

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


local N, L = io.read("n", "n")
local abs_min_i
local abs_min = 1000
local sum = 0
for i=1,N do
    local taste = L + i - 1
    local a = math.abs(taste)
    if a < abs_min then
        abs_min_i = i
        abs_min = a
    end
    sum = sum + taste
end
sum = sum - (L + abs_min_i - 1)
print(sum)

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

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044

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.


function gcd(a,b)
    if b~=0 then
        return gcd(b,a%b)
    else
        return a
    end
end
a,b,c,d=io.read("*n","*n","*n","*n")
x=(b//c+b//d-b//((c*d)//gcd(c,d)))-((a-1)//c+(a-1)//d-(a-1)//((c*d)//gcd(c,d)))
print(b-a+1-x)

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


local N = io.read("n")
local jobs = {}
for i=1, N do
    local a, b = io.read("n", "n")
    jobs[i] = {a, b}
end
table.sort(jobs, function(x1, x2)
    return x1[2] < x2[2]
end)
local cursor = 0
for i=1, N do
    local a, b = jobs[i][1], jobs[i][2]
    if cursor + a > b then
        print("No")
        return
    else
        cursor = cursor + a
    end
end
print("Yes")

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


local mfl = math.floor
local n, k = io.read("*n", "*n")
k = mfl((n - 1) * (n - 2) / 2) - k
if k < 0 then
  print(-1)
else
  local cur = 0
  print(n - 1 + k)
  for i = 2, n do
    print(1 .. " " .. i)
  end
  if k ~= 0 then
    for i = 2, n - 1 do
      for j = i + 1, n do
        print(i .." " .. j)
        cur = cur + 1
        if cur == k then break end
      end
      if cur == k then break end
    end
  end
end

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

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

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


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

if x < a then
  print(0)
else
  print(10)
end

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


local x, a = io.read("*n", "*n")
if x < a then print(0) else print(10) end


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

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

local cnt = 1
local bound = 0

for i = 1, n do
  bound = bound + L[i]
  if L[i] <= x then
    cnt = cnt + 1
  end
end

print(cnt)

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


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

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

local cnt = 0
local bound = 0

for i = 1, n do
  bound = bound + L[i]
  if L[i] <= x then
    cnt = cnt + 1
  end
end

print(cnt)


local w,h,x,y=io.read("*n","*n","*n","*n")
local tate=math.min(x*h,(w-x)*h)
local yoko=math.min(y*w,(h-y)*w)
local tatemax=(w//2)*h
local yokomax=(h//2)*w

tate=tate+(tatemax-tate)/2
yoko=yoko+(yokomax-yoko)/2
local max=string.format("%.10f",math.max(tate,yoko))
print(max,tate==yoko and 1 or 0)

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


local w, h = io.read("*n", "*n")
local x, y = io.read("*n", "*n")
local menseki = w * h / 2
local flag = 0
if x * 2 == w and y * 2 == h then
  flag = 1
end
io.write(string.format("%.10f", menseki))
io.write(" " .. flag .. "\n")


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n", "*l")
-- local left, right, up, down = {}, {}, {}, {}
local cxmin, cxmax, cymin, cymax = nil, nil, nil, nil
local xinc_max, xinc_min, xdec_max, xdec_min = nil, nil, nil, nil
local str = ""
for i = 1, n do
  str = io.read()
  local x, y, d = str:match("(-?%d+) (-?%d+) (%w)")
  x, y = tonumber(x), tonumber(y)
  -- print(x, y, d)
  if d == "R" then
    if not cymin then
      cymin, cymax = y, y
    else
      cymin, cymax = mmi(cymin, y), mma(cymax, y)
    end
    if not xinc_min then
      xinc_min, xinc_max = x, x
    else
      xinc_min, xinc_max =mmi(xinc_min, x), mma(xinc_max, x)
    end
  elseif d == "L" then
    if not cymin then
      cymin, cymax = y, y
    else
      cymin, cymax = mmi(cymin, y), mma(cymax, y)
    end
    if not xdec_min then
      xdec_min, xdec_max = x, x
    else
      xdec_min, xdec_max =mmi(xdec_min, x), mma(xdec_max, x)
    end
  elseif d == "U" then
    if not cxmin then
      cxmin, cxmax = x, x
    else
      cxmin, cxmax = mmi(cxmin, x), mma(cxmax, x)
    end
    if not yinc_min then
      yinc_min, yinc_max = y, y
    else
      yinc_min, yinc_max =mmi(yinc_min, y), mma(yinc_max, y)
    end
  else
    if not cxmin then
      cxmin, cxmax = x, x
    else
      cxmin, cxmax = mmi(cxmin, x), mma(cxmax, x)
    end
    if not ydec_min then
      ydec_min, ydec_max = y, y
    else
      ydec_min, ydec_max =mmi(ydec_min, y), mma(ydec_max, y)
    end
  end
end
-- print(xinc_min, xinc_max, xdec_min, xdec_max)
-- print(yinc_min, yinc_max, ydec_min, ydec_max)
-- print(cxmin, cxmax, cymin, cymax)

local candidate_t = {0}
if xinc_min then
  if xdec_min then
    if xinc_min < xdec_min then
      table.insert(candidate_t, (xdec_min - xinc_min) / 2)
    end
    if xinc_min < xdec_max then
      table.insert(candidate_t, (xdec_max - xinc_min) / 2)
    end
    if xinc_max < xdec_min then
      table.insert(candidate_t, (xdec_min - xinc_max) / 2)
    end
    if xinc_max < xdec_max then
      table.insert(candidate_t, (xdec_max - xinc_max) / 2)
    end
  end
  if cxmin then
    if xinc_min < cxmin then
      table.insert(candidate_t, cxmin - xinc_min)
    end
    if xinc_min < cxmax then
      table.insert(candidate_t, cxmax - xinc_min)
    end
    if xinc_max < cxmin then
      table.insert(candidate_t, cxmin - xinc_max)
    end
    if xinc_max < cxmax then
      table.insert(candidate_t, cxmax - xinc_max)
    end
  end
end
if xdec_min then
  if cxmin then
    if xdec_min > cxmin then
      table.insert(candidate_t, -cxmin + xdec_min)
    end
    if xdec_min > cxmax then
      table.insert(candidate_t, -cxmax + xdec_min)
    end
    if xdec_max > cxmin then
      table.insert(candidate_t, -cxmin + xdec_max)
    end
    if xdec_max > cxmax then
      table.insert(candidate_t, -cxmax + xdec_max)
    end
  end
end
-- y
if yinc_min then
  if ydec_min then
    if yinc_min < ydec_min then
      table.insert(candidate_t, (ydec_min - yinc_min) / 2)
    end
    if yinc_min < ydec_max then
      table.insert(candidate_t, (ydec_max - yinc_min) / 2)
    end
    if yinc_max < ydec_min then
      table.insert(candidate_t, (ydec_min - yinc_max) / 2)
    end
    if yinc_max < ydec_max then
      table.insert(candidate_t, (ydec_max - yinc_max) / 2)
    end
  end
  if cymin then
    if yinc_min < cymin then
      table.insert(candidate_t, cymin - yinc_min)
    end
    if yinc_min < cymax then
      table.insert(candidate_t, cymax - yinc_min)
    end
    if yinc_max < cymin then
      table.insert(candidate_t, cymin - yinc_max)
    end
    if yinc_max < cymax then
      table.insert(candidate_t, cymax - yinc_max)
    end
  end
end
if ydec_min then
  if cymin then
    if ydec_min > cymin then
      table.insert(candidate_t, -cymin + ydec_min)
    end
    if ydec_min > cymax then
      table.insert(candidate_t, -cymax + ydec_min)
    end
    if ydec_max > cymin then
      table.insert(candidate_t, -cymin + ydec_max)
    end
    if ydec_max > cymax then
      table.insert(candidate_t, -cymax + ydec_max)
    end
  end
end

local ret = nil
for i = 1, #candidate_t do
  local time = candidate_t[i]
  local qxmin, qxmax = nil, nil
  local qymin, qymax = nil, nil
  if xdec_min then
    if not qxmin then qxmin, qxmax = xdec_min - time, xdec_max - time
    else qxmin, qxmax = mmi(qxmin, xdec_min - time), mma(qxmax, xdec_max - time)
    end
  end
  if xinc_min then
    if not qxmin then qxmin, qxmax = xinc_min - time, xinc_max - time
    else qxmin, qxmax = mmi(qxmin, xinc_min + time), mma(qxmax, xinc_max + time)
    end
  end
  if cxmin then
    if not qxmin then qxmin, qxmax = cxmin, cxmax
    else qxmin, qxmax = mmi(qxmin, cxmin), mma(qxmax, cxmax)
    end
  end

  if ydec_min then
    if not qymin then qymin, qymax = ydec_min - time, ydec_max - time
    else qymin, qymax = mmi(qymin, ydec_min - time), mma(qymax, ydec_max - time)
    end
  end
  if yinc_min then
    if not qymin then qymin, qymax = yinc_min - time, yinc_max - time
    else qymin, qymax = mmi(qymin, yinc_min + time), mma(qymax, yinc_max + time)
    end
  end
  if cymin then
    if not qymin then qymin, qymax = cymin, cymax
    else qymin, qymax = mmi(qymin, cymin), mma(qymax, cymax)
    end
  end
  local tmp = 0
  if qxmin and qymin then
    tmp = (qxmax - qxmin) * (qymax - qymin)
  else
    tmp = 0
  end
  -- print(time, tmp)
  if not ret then ret = tmp
  else ret = mmi(ret, tmp)
  end
end
print(ret)

Score : 600 points

Problem Statement

There are N points in a two-dimensional plane. The initial coordinates of the i-th point are (x_i, y_i). Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x- or y- axis. You are given a character d_i that represents the specific direction in which the i-th point moves, as follows:

If d_i = R, the i-th point moves in the positive x direction;

If d_i = L, the i-th point moves in the negative x direction;

If d_i = U, the i-th point moves in the positive y direction;

If d_i = D, the i-th point moves in the negative y direction.

You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let x_{max} and x_{min} be the maximum and minimum among the x-coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y-coordinates of the N points, respectively.

Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it.

Constraints

1 \leq N \leq 10^5

-10^8 \leq x_i,\ y_i \leq 10^8

x_i and y_i are integers.

d_i is R, L, U, or D.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 d_1
x_2 y_2 d_2
.
.
.
x_N y_N d_N

Output

Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}).

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

Sample Input 1

2
0 3 D
3 0 L

Sample Output 1

0

After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment.

Sample Input 2

5
-7 -10 U
7 -6 U
-8 7 D
-3 3 D
0 -6 R

Sample Output 2

97.5

The answer may not be an integer.

Sample Input 3

20
6 -10 R
-4 -9 U
9 6 D
-3 -2 R
0 7 D
4 5 D
10 -10 U
-1 -8 U
10 -6 D
8 -5 U
6 4 D
0 3 D
7 9 R
9 -4 R
3 10 D
1 9 U
1 -6 U
9 -8 R
6 7 D
7 -3 D

Sample Output 3

273


local n, k = io.read("*n", "*n")
if k == 1 then print(0) else print(n - k) end

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {}
  t[i].x, t[i].y = io.read("*n", "*n")
end
table.sort(t, function(v, w)
  if v.x == w.x then
    return v.y < w.y
  else
    return v.x < w.x
  end
end
)

local function getcost(p, q)
  local cost = 0
  if p == 0 then
    local done = 0
    local donelist = {}
    for i = 1, n do donelist[i] = false end
    while done < n do
      cost = cost + 1
      local idx = 1
      for i = 1, n do
        if donelist[i] then idx = i break end
      end
      donelist[idx] = true
      done = done + 1
      local nx, ny = t[idx].x, t[idx].y + q
      local rep = true
      while rep do
        rep = false
        for i = 1, n do
          if nx == t[i].x and ny == t[i].y and not donelist[i] then
            ny = ny + q
            donelist[i] = true
            done = done + 1
            rep = true
          end
        end
      end
    end
  else
    local done = 0
    local donelist = {}
    for i = 1, n do donelist[i] = false end
    while done < n do
      cost = cost + 1
      local idx = 1
      for i = 1, n do
        if donelist[i] then idx = i break end
      end
      donelist[idx] = true
      done = done + 1
      local nx, ny = t[idx].x + p, t[idx].y + q
      for i = idx + 1, n do
        if nx == t[i].x and ny == t[i].y then
          nx, ny = nx + p, ny + q
          donelist[i] = true
          done = done + 1
        end
      end
    end
  end
  return cost
end

local ret = 50
for i = 1, n - 1 do
  for j = i + 1, n do
    local c = getcost(t[j].x - t[i].x, t[j].y - t[i].y)
    ret = mmi(ret, c)
  end
end
print(ret)

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


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

local n = io.read("*n")
local ga, sa, ba = io.read("*n", "*n", "*n")
local gb, sb, bb = io.read("*n", "*n", "*n")

local dp = {}
for i = 1, n do
  dp[i] = 0
end
if ga < gb then
  local max = mfl(n / ga)
  for i = 1, max do
    dp[i * ga] = i * gb
  end
end
if sa < sb then
  for i = n - 1, 1, -1 do
    if 0 < dp[i] then
      local addable = mfl((n - i) / sa)
      for j = 1, addable do
        dp[i + j * sa] = mma(dp[i + j * sa], dp[i] + j * sb)
      end
    end
  end
  local max = mfl(n / sa)
  for i = 1, max do
    dp[i * sa] = mma(dp[i * sa], i * sb)
  end
end
if ba < bb then
  for i = n - 1, 1, -1 do
    if 0 < dp[i] then
      local addable = mfl((n - i) / ba)
      for j = 1, addable do
        dp[i + j * ba] = mma(dp[i + j * ba], dp[i] + j * bb)
      end
    end
  end
  local max = mfl(n / ba)
  for i = 1, max do
    dp[i * ba] = mma(dp[i * ba], i * bb)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i])
end
n = newn

-- B to A
dp = {}
for i = 1, n do
  dp[i] = 0
end
if gb < ga then
  local max = mfl(n / gb)
  for i = 1, max do
    dp[i * gb] = i * ga
  end
end
if sb < sa then
  for i = n - 1, 1, -1 do
    if 0 < dp[i] then
      local addable = mfl((n - i) / sb)
      for j = 1, addable do
        dp[i + j * sb] = mma(dp[i + j * sb], dp[i] + j * sa)
      end
    end
  end
  local max = mfl(n / sb)
  for i = 1, max do
    dp[i * sb] = mma(dp[i * sb], i * sa)
  end
end
if bb < ba then
  for i = n - 1, 1, -1 do
    if 0 < dp[i] then
      local addable = mfl((n - i) / bb)
      for j = 1, addable do
        dp[i + j * bb] = mma(dp[i + j * bb], dp[i] + j * ba)
      end
    end
  end
  local max = mfl(n / bb)
  for i = 1, max do
    dp[i * bb] = mma(dp[i * bb], i * ba)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i])
end
print(newn)

Score : 600 points

Problem Statement

The squirrel Chokudai has N acorns.
One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.

His plan is as follows:

Get out of the nest with N acorns in his hands.

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order:

Lose g_{X} acorns and gain 1 gram of gold.

Gain g_{X} acorns and lose 1 gram of gold.

Lose s_{X} acorns and gain 1 gram of silver.

Gain s_{X} acorns and lose 1 gram of silver.

Lose b_{X} acorns and gain 1 gram of bronze.

Gain b_{X} acorns and lose 1 gram of bronze.

Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.

What is the maximum number of acorns that he can bring to the nest?
Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

Print the maximum number of acorns that Chokudai can bring to the nest.

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

He can bring 46 acorns to the nest, as follows:

In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }

In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }

In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }

He cannot have 47 or more acorns, so the answer is 46.


a, b, c = io.read("*n", "*n", "*n")
print(a + b + c - math.max(a, b, c))

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


local MOD = 1000000007
local N, M = io.read("n", "n")
local broken = {}
for i=1,N do
    broken[i] = false
end
for i=1,M do
    local a = io.read("n")
    broken[a] = true
end

local dp = {}
dp[-1] = 0
dp[0] = 1
for i=1,N do
    if broken[i] then
        dp[i] = 0
    else
        dp[i] = (dp[i-1] + dp[i-2]) % MOD
    end
end
print(dp[N])

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

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


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

local u,d,l,r={},{},{},{}
for j=1,w do
    for i=1,h do
        u[i]=u[i] or {}
        if s[i][j]==35 then
            u[i][j]=0
        else
            if i==1 then
                u[i][j]=1
            else
                u[i][j]=u[i-1][j]+1
            end
        end
    end
end
for j=1,w do
    for i=h,1,-1 do
        d[i]=d[i] or {}
        if s[i][j]==35 then
            d[i][j]=0
        else
            if i==h then
                d[i][j]=1
            else
                d[i][j]=d[i+1][j]+1
            end
        end
    end
end
for i=1,h do
    l[i]={}
    for j=1,w do
        if s[i][j]==35 then
            l[i][j]=0
        else
            if j==1 then
                l[i][j]=1
            else
                l[i][j]=l[i][j-1]+1
            end
        end
    end
end
for i=1,h do
    r[i]={}
    for j=w,1,-1 do
        if s[i][j]==35 then
            r[i][j]=0
        else
            if j==w then
                r[i][j]=1
            else
                r[i][j]=r[i][j+1]+1
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
    end
end
print(max)

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


local n, a, b, mod = io.read("*n", "*n", "*n", "*n")

local function badd(x, y) return (x + y) % mod end
local function bsub(x, y) return (mod + x - y) % mod end
local function bmul(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, pow = bmul(res, src), pow - 1
    end
    src, pow = bmul(src, src), pow // 2
  end
  return res
end

local function func(i)
  local c, z = 0, a + b * (i - 1)
  while 0 < z do
    c, z = c + 1, z // 10
  end
  return c
end

local function getright(d, left)
  local min = left
  local max = n
  if d < func(min) then return left - 1 end
  if func(max) == d then return max end
  while 1 < max - min do
    local mid = (min + max) // 2
    if d < func(mid) then max = mid
    else min = mid
    end
  end
  return min
end

local function solve1(d, n)
  if n == 1 then return 1 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = modpow(tend, n - 1)
    n = n - 1
  end
  n = n // 2
  local v = solve1(d, n)
  v = bmul(v, badd(1, modpow(tend, n)))
  return badd(add, v)
end

local function solve2(d, n)
  if n == 1 then return 0 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = bmul((n - 1) % mod, modpow(tend, n - 1))
    n = n - 1
  end
  n = n // 2
  local v1 = solve2(d, n)
  v1 = bmul(v1, badd(1, modpow(tend, n)))
  local v2 = solve1(d, n)
  v2 = bmul(v2, bmul(n % mod, modpow(tend, n)))
  return badd(add, badd(v1, v2))
end
local cur_digsum = 0
local left = 1
local ret = 0
for d = 1, 18 do
  local right = getright(d, left)
  if left <= right then
    local s1 = solve1(d, right - left + 1)
    local v1 = bmul(a % mod, s1)
    local v2 = bmul(b % mod, (right - 1) % mod)
    v2 = bmul(v2, s1)
    local v3 = bmul(b % mod, solve2(d, right - left + 1))
    local tend = modpow(10, d)
    ret = bmul(ret, modpow(tend, right - left + 1))
    ret = badd(ret, badd(v1, bsub(v2, v3)))
    cur_digsum = cur_digsum + (right - left + 1) * d
    left = right + 1
  end
  if n < left then break end
end
print(ret)

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908


local n, a, b, mod = io.read("*n", "*n", "*n", "*n")

local function badd(x, y) return (x + y) % mod end
local function bsub(x, y) return (mod + x - y) % mod end
local function bmul(x, y) return (x * y) % mod end
local function bmul_m(x, y) return (x * y) % (mod - 1) end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res, pow = bmul(res, src), pow - 1
    end
    src, pow = bmul(src, src), pow // 2
  end
  return res
end

local function func(i)
  local c, z = 0, a + b * (i - 1)
  while 0 < z do
    c, z = c + 1, z // 10
  end
  return c
end

local function getright(d, left)
  local min = left
  local max = n
  if d < func(min) then return left - 1 end
  if func(max) == d then return max end
  while 1 < max - min do
    local mid = (min + max) // 2
    if d < func(mid) then max = mid
    else min = mid
    end
  end
  return min
end

local function solve1(d, n)
  if n == 1 then return 1 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + modpow(tend, n - 1)
    n = n - 1
  end
  n = n // 2
  local v = solve1(d, n)
  v = bmul(v, badd(1, modpow(tend, n)))
  return badd(add, v)
end

local function solve2(d, n)
  if n == 1 then return 0 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + bmul(n - 1, modpow(tend, n - 1))
    n = n - 1
  end
  n = n // 2
  local v1 = solve2(d, n)
  v1 = bmul(v1, badd(1, modpow(tend, n)))
  local v2 = solve1(d, n)
  v2 = bmul(v2, bmul(n % mod, modpow(tend, n)))
  return badd(add, badd(v1, v2))
end

local cur_digsum = 0
local left = 1
local ret = 0
for d = 1, 18 do
  local right = getright(d, left)
  if left <= right then
    local s1 = solve1(d, right - left + 1)
    local v1 = bmul(a % mod, s1)
    local v2 = bmul(b % mod, (right - 1) % mod)
    v2 = bmul(v2, s1)
    local v3 = bmul(b % mod, solve2(d, right - left + 1))
    ret = bmul(ret, modpow(10, bmul_m((right - left + 1), d)))
    ret = badd(ret, badd(v1, bsub(v2, v3)))
    cur_digsum = cur_digsum + (right - left + 1) * d
    left = right + 1
  end
  if n < left then break end
end
print(ret)


local n,a,b,c,d=io.read("*n","*n","*n","*n","*n","*l")
local s=io.read()

if c<d then
    local flag=true
    for i=a,c-1 do
        if s:sub(i,i+1)=="##" then
            flag=false
        end
    end
    for i=b,d-1 do
        if s:sub(i,i+1)=="##" then
            flag=false
        end
    end
    print(flag and "Yes" or "No")
elseif c>d then
    local flag=true
    for i=a,c-1 do
        if s:sub(i,i+1)=="##" then
            flag=false
        end
    end
    for i=b,d-1 do
        if s:sub(i,i+1)=="##" then
            flag=false
        end
    end
    if flag then
        flag=false
        for i=b-1,d-1 do
            if s:sub(i,i+2)=="..." then
                flag=true
            end
        end
    end
    print(flag and "Yes" or "No")
end

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


local n,a,b,c,d=io.read("*n","*n","*n","*n","*n","*l")
local s=io.read()
if #s:sub(a,c):match("#+")>1 or #s:sub(b,d):match("#+")>1 then
    print("No")
else
    print("Yes")
end


local n, a, b, c, d = io.read("*n", "*n", "*n", "*n", "*n", "*l")
local s = io.read()

local function check1(l, r)
  for i = l, r - 1 do
    if s:sub(i, i) == "#" and s:sub(i + 1, i + 1) == "#" then
      return false
    end
  end
  return true
end

local function check2(l, r)
  for i = l, r - 2 do
    if s:sub(i, i) == "." and s:sub(i + 1, i + 1) == "." and s:sub(i + 2, i + 2) == "." then
      return true
    end
  end
  return false
end

if c < d then
  local r = check1(a, c) and check1(b, d)
  print(r and "Yes" or "No")
else
  local r = check1(a, c)
  if not r then print("No")
  else
    r = check2(b - 1, d - 1)
    print(r and "Yes" or "No")
  end
end


local n = io.read("*n")
print(180 * (n - 2))

Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


local bls, brs = bit.lshift, bit.rshift
local mod = 1000003
local function badd(x, y) return (x + y) % mod end
local function bmul(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 = brs(pow, 1)
  end
  return res
end

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

local fact = {1}
for i = 2, mod - 1 do
  fact[i] = bmul(fact[i - 1], i)
end

local q = io.read("*n")
for iq = 1, q do
  local x, d, n = io.read("*n", "*n", "*n")
  if d == 0 then
    print(modpow(x, n))
  else
    local a = bmul(x, modinv(d))
    local lim = a + n - 1
    if mod <= lim then
      print(0)
    else
      local numer = fact[lim]
      numer = bmul(numer, modpow(d, n))
      local denom = 1
      if 1 < a then
        denom = fact[a - 1]
      end
      local ret = bmul(numer, modinv(denom))
      print(ret)
    end
  end
end

Score : 600 points

Problem Statement

Consider the following arithmetic progression with n terms:

x, x + d, x + 2d, \ldots, x + (n-1)d

What is the product of all terms in this sequence?
Compute the answer modulo 1\ 000\ 003.

You are given Q queries of this form.
In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i.

Constraints

1 \leq Q \leq 10^5

0 \leq x_i, d_i \leq 1\ 000\ 002

1 \leq n_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
x_1 d_1 n_1
:
x_Q d_Q n_Q

Output

Print Q lines.

In the i-th line, print the answer for the i-th query.

Sample Input 1

2
7 2 4
12345 67890 2019

Sample Output 1

9009
916936

For the first query, the answer is 7 \times 9 \times 11 \times 13 = 9009.
Don't forget to compute the answer modulo 1\ 000\ 003.


a, p = io.read("*n", "*n")
print(math.floor((a * 3 + p)) / 2)

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


n = io.read("*n", "*l")
t = {}
for i = 1, n do
  s = io.read()
  name, p = s:match("(%w+) (%d+)")
  t[i] = {name, tonumber(p), i}
end
table.sort(t, function(a, b)
  if a[1] ~= b[1] then
    return a[1] < b[1]
  else
    return a[2] > b[2]
  end
end)
for i = 1, n do
  print(t[i][3])
end

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


n=io.read("*n","*l")
t={}
for i=1,n do
    x=io.read()
    s,p=x:match("(%w+) (%d+)")
    table.insert(t, {s,tonumber(p),i})
end
table.sort(t,function(x,y)
    if x[1]~=y[1] then
        return x[1]<y[1]
    else
        return x[2]>y[2]
    end
end)
for i=1,n do
    print(t[i][3])
end


local function str2tbl(s)
    -- limit of #s is approximately 999959
    local t = {string.byte(s, 1, #s)}
    local char = string.char
    for i=1, #t do
        t[i] = char(t[i])
    end
    return t
end
--
local N = io.read("n", "l")
local restaurants = {}
for i=1,N do
    local tmp = io.read("l")
    local tbl = str2tbl(tmp)
    local s, p
    for j=1, #tbl do
        if tbl[j] == ' ' then
            s = string.sub(tmp, 1, j-1)
            p = tonumber(string.sub(tmp, j+1))
            break
        end
    end
    --print(s, p)
    table.insert(restaurants, {i, s, p})
end

table.sort(restaurants, function(x, y)
    if x[2] ~= y[2] then
        return x[2] < y[2]
    else
        return x[3] > y[3]
    end
end)
for i=1, N do
    print(restaurants[i][1])
end


local n, m = io.read("*n", "*n")
local t = {}
for i = 1, m do
  z = io.read("*n")
  t[i] = {}
  for j = 1, z do
    table.insert(t[i], io.read("*n"))
  end
end
local p = {}
for i = 1, m do
  p[i] = io.read("*n")
end
local ret = 0
local tot = 1
for i = 1, n do tot = tot * 2 end
local use = {}
for i = 0, tot - 1 do
  local ti = i
  for j = 1, n do
    use[j] = ti % 2
    ti = ti // 2
  end
  local v = true
  for j = 1, m do
    local q = 0
    for k = 1, #t[j] do
      q = q + use[t[j][k]]
    end
    if q % 2 ~= p[j] then v = false break end
  end
  if v then ret = ret + 1 end
end
print(ret)

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


local Nsw, Nli = io.read("n", "n")
local switches = {}
local onconds = {}
for i=1,Nli do
    local k = io.read("n")
    local swi = {}
    switches[i] = swi
    for j=1,k do
        swi[j] = io.read("n")
    end
end
for i=1,Nli do
    onconds[i] = io.read("n")
end
-- Cartesian product
local function carprod(seqs)
    return coroutine.wrap(function()
        if #seqs == 0 then
            coroutine.yield({})
            return
        end
        local s1 = seqs[1]
        for _,v in ipairs(s1) do
            for t in carprod({select(2, table.unpack(seqs))}) do
                coroutine.yield({v, table.unpack(t)})
            end
        end
    end)
end

local zo = {0,1}
local set = {}
for i=1,Nsw do
    set[i] = zo
end
local ans = 0
for ss in carprod(set) do -- 1024 times
    --print(table.concat(ss, ""))
    local all_on = true
    for i=1,Nli do -- 10 times
        local swi = switches[i]
        local sum = 0
        for k,v in ipairs(swi) do -- 10 times
            if ss[v] == 1 then
                sum = sum + 1
            end
        end
        if sum % 2 == onconds[i] then
        else
            all_on = false
            break
        end
    end
    if all_on then
        ans = ans + 1
    end
end
print(ans)


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local ior = io.input()
local n, q = ior:read("*n", "*n")
local s, t, x, d = {}, {}, {}, {}
for i = 1, n do
  s[i], t[i], x[i] = ior:read("*n", "*n", "*n")
end

local timelist = {}
local omitinfo = {}
for i = 1, q do
  local tmp = ior:read("*n")
  d[i] = tmp
  table.insert(timelist, tmp)
  local tmptbl = {}
  table.insert(omitinfo, tmptbl)
end
-- table.sort(timelist)

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

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

for i = 1, n do
  -- [ngfrom, ngto]
  local ngfrom, ngto = s[i] - x[i], t[i] - x[i] - 1
  local real_ngfrom_pos_idx = lower_bound(timelist, ngfrom)
  local real_ngto_pos_idx = upper_bound(timelist, ngto) - 1
  -- print(i, ngfrom, ngto, real_ngfrom_pos_idx, real_ngto_pos_idx)
  if(real_ngfrom_pos_idx <= q and 1 <= real_ngto_pos_idx and real_ngfrom_pos_idx <= real_ngto_pos_idx) then
    table.insert(omitinfo[real_ngfrom_pos_idx], x[i])
    table.insert(omitinfo[real_ngto_pos_idx], -x[i])
  end
end
-- for i = 1, q do
--   print(i, "-- ", unpack(omitinfo[i]))
-- end
local cur_omit_list = {}
local cur_min_omit_x = -1
for i = 1, q do
  for j = 1, #omitinfo[i] do
    local val = omitinfo[i][j]
    if(0 < val) then
      cur_omit_list[val] = true
      if(cur_min_omit_x < 0 or val < cur_min_omit_x) then
        cur_min_omit_x = val
      end
    end
  end
  print(cur_min_omit_x)

  for j = 1, #omitinfo[i] do
    local val = omitinfo[i][j]
    if(val < 0) then
      cur_omit_list[-val] = nil
      if(cur_min_omit_x == -val) then
        cur_min_omit_x = -1
        for key, value in pairs(cur_omit_list) do
          if(cur_min_omit_x < 0 or key < cur_min_omit_x) then
            cur_min_omit_x = key
          end
        end
      end
    end
  end
end

Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

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

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.


local mfl, mce = math.floor, math.ceil
local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local n = io.read("*n")
local t = {}
_u = io.read("*n")
for i = 1, n - 1 do
  t[i] = io.read("*n")
end
n = n - 1
local ret = 0
local lim = brs(n - 1, 1)
for i = 1, lim do
  local lim2 = mfl(lim / i)
  local c = 0
  for j = 1, n do
    if n % i == 0 then
      if n - i * j <= i * j then break end
    end
    if n - i * j < j then break end
    c = c + t[i * j] + t[n - i * j]
    ret = mma(ret, c)
  end
end
print(ret)

Score : 600 points

Problem Statement

There is an infinitely large pond, which we consider as a number line.
In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1.
On the lotus at coordinate i, an integer s_i is written.

You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:

1. Choose positive integers A and B. Your score is initially 0.

2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

4. Go back to step 2.

You want to end the game with as high a score as possible.
What is the score obtained by the optimal choice of A and B?

Constraints

3 \leq N \leq 10^5

-10^9 \leq s_i \leq 10^9

s_0=s_{N-1}=0

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_0 s_1 ...... s_{N-1}

Output

Print the score obtained by the optimal choice of A and B.

Sample Input 1

5
0 2 5 1 0

Sample Output 1

3

If you choose A = 3 and B = 2, the game proceeds as follows:

Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.

Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.

Move to coordinate 1 + 3 = 4. The game ends with a score of 3.

There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.

Sample Input 2

6
0 10 -7 -4 -13 0

Sample Output 2

0

The optimal strategy here is to land the final lotus immediately by choosing A = 5 (the value of B does not matter).

Sample Input 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

Sample Output 3

59


local mfl, mce = math.floor, math.ceil
local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local n = io.read("*n")
local t = {}
_u = io.read("*n")
for i = 1, n - 1 do
  t[i] = io.read("*n")
end
n = n - 1
local ret = 0
local lim = brs(n - 2, 1)
for i = 1, lim do
  local lim2 = mfl(lim / i)
  local c = 0
  for j = 1, lim2 do
    c = c + t[i * j] + t[n - i * j]
    ret = mma(ret, c)
  end
end
print(ret)


a, b = io.read("*n", "*n")
if a <= 5 then
    print(0)
elseif a <= 12 then
    print(b // 2)
else
    print(b)
end

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


local ior = io.input()
local a, b = ior:read("*n", "*n")
if a <= 5 then
  print(0)
elseif a <= 12 then
  print(b // 2)
else
  print(b)
end


local ior = io.input()
local r, d, x = ior:read("*n", "*n", "*n")
for i = 1, 10 do
  x = r * x - d
  print(string.format("%d", x))
end

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


N, M = io.read("*n", "*n", "*l")
L_max = 0
R_min = 10^5
for i = 1, M do
    L, R = io.read("*n", "*n", "*l")
    L_max = math.max(L, L_max)
    R_min = math.min(R, R_min)
end
print(math.max(0, R_min-L_max+1))

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


N, M = io.read("*n", "*n", "*l")
L_max = 0
R_min = 10^5
for i = 1, M do
    L, R = io.read("*n", "*n", "*l")
    if L <= R_min then
        L_max = math.max(L, L_max)
    end
    if R >= L_max then
        R_min = math.min(R, R_min)
    end
end
if R_min > L_max then
    print(string.format("%d", R_min-L_max+1))
else
    print(0)
end


N, M = io.read("*n", "*n", "*l")
L_max = 0
R_min = 10^5
for i = 1, M do
    L, R = io.read("*n", "*n", "*l")
    if L < R_min then
        L_max = math.max(L, L_max)
    elseif R > L_max then
        R_min = math.min(R, R_min)
    end
end
print(R_min-L_max+1)


local mab, mce, mfl, msq, mmi, mma = math.abs, math.ceil, math.floor, math.sqrt, math.min, math.max
local ior = io.input()
local n, m = ior:read("*n", "*n")
local left, right = ior:read("*n", "*n")
for i = 2, m do
  local x, y = ior:read("*n", "*n")
  left, right = mma(left, x), mmi(right, y)
end
if left <= right then
  print(right - left + 1)
else
  print(0)
end


local mod = 1000000007

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

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

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

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

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

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

local n, m, k = io.read("*n", "*n", "*n")
local a = bmul(n, n)
a = bmul(a, bmul(modinv(6), bmul(bmul(m + 1, m), m - 1)))
local b = bmul(m, m)
b = bmul(b, bmul(modinv(6), bmul(bmul(n + 1, n), n - 1)))
local r = badd(a, b)
r = bmul(r, getComb(n * m - 2, k - 2))
print(r)

Score : 500 points

Problem Statement

We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.

If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:

\sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)

Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.

We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Constraints

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

2 \leq K \leq N \times M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.

Sample Input 1

2 2 2

Sample Output 1

8

There are six possible arrangements of the pieces, as follows:

((1,1),(1,2)), with the cost |1-1|+|1-2| = 1

((1,1),(2,1)), with the cost |1-2|+|1-1| = 1

((1,1),(2,2)), with the cost |1-2|+|1-2| = 2

((1,2),(2,1)), with the cost |1-2|+|2-1| = 2

((1,2),(2,2)), with the cost |1-2|+|2-2| = 1

((2,1),(2,2)), with the cost |2-2|+|1-2| = 1

The sum of these costs is 8.

Sample Input 2

4 5 4

Sample Output 2

87210

Sample Input 3

100 100 5000

Sample Output 3

817260251

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


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

SegTree.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local t1, t2, t3 = {1}, {1}, {self.size[1]}
  local retval = 0
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if sz <= r + 1 - l then
      local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
      if tmp[1] < val then
        ret, retpos = tmp, l + sz - 1
        retval = tmp[2]
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l + sz) table.insert(t3, r) end
      else
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
      end
    else
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return retpos + 1, retval
end

local q = io.read("*n")
local qlist = {}
local alist = {}
for i = 1, q do
  local tp = io.read("*n")
  if tp == 1 then
    local a, b = io.read("*n", "*n")
    qlist[i] = {a, b}
    table.insert(alist, a)
  else
    qlist[i] = false
  end
end
table.sort(alist)
local amap = {}
local asum = {}
for i = 1, #alist do
  if not amap[alist[i]] then
    amap[alist[i]] = i
  end
  if i == 1 then asum[i] = alist[i]
  else asum[i] = asum[i - 1] + alist[i]
  end
end
local stary = {}
for i = 1, #alist do stary[i] = {0, 0LL} end
local tree = SegTree.new(stary, function(x, y) return {x[1] + y[1], x[2] + y[2]} end, {0, 0LL})
local addcnt = {}
local acnt = 0
local bsum = 0
for i = 1, q do
  local query = qlist[i]
  if query then
    local a = query[1]
    local addpos = amap[query[1]]
    if addcnt[a] then
      addpos = addpos + addcnt[a]
      addcnt[a] = addcnt[a] + 1
    else
      addcnt[a] = 1
    end
    tree:setValue(addpos, {1, 0LL + a})
    bsum = bsum + query[2]
    acnt = acnt + 1
  else
    local half = mfl((acnt + 1) / 2)
    local calcpos, preval = tree:lower_bound(half)
    local src = alist[calcpos]
    preval = preval + src
    local curtot = tree.stage[1][1][2]
    local result = bsum + half * src - preval
      + (curtot - preval) - (acnt - half) * src
    local resultstr = tostring(result):gsub("LL", "")
    print(src .. " " .. resultstr)
  end
end

Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

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

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000


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

SegTree.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local t1, t2, t3 = {1}, {1}, {self.size[1]}
  local retval = 0
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if sz <= r + 1 - l then
      local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
      if tmp[1] < val then
        ret, retpos = tmp, l + sz - 1
        retval = tmp[2]
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l + sz) table.insert(t3, r) end
      else
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
      end
    else
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return retpos + 1, retval
end

local q = io.read("*n")
local qlist = {}
local alist = {}
for i = 1, q do
  local tp = io.read("*n")
  if tp == 1 then
    local a, b = io.read("*n", "*n")
    qlist[i] = {a, b}
    table.insert(alist, a)
  else
    qlist[i] = false
  end
end
table.sort(alist)
local amap = {}
local asum = {}
for i = 1, #alist do
  if not amap[alist[i]] then
    amap[alist[i]] = i
  end
  if i == 1 then asum[i] = alist[i]
  else asum[i] = asum[i - 1] + alist[i]
  end
end
local stary = {}
for i = 1, #alist do stary[i] = {0, 0} end
local tree = SegTree.new(stary, function(x, y) return {x[1] + y[1], x[2] + y[2]} end, {0, 0})
local addcnt = {}
local acnt = 0
local bsum = 0
for i = 1, q do
  local query = qlist[i]
  if query then
    local a = query[1]
    local addpos = amap[query[1]]
    if addcnt[a] then
      addpos = addpos + addcnt[a]
      addcnt[a] = addcnt[a] + 1
    else
      addcnt[a] = 1
    end
    tree:setValue(addpos, {1, a})
    bsum = bsum + query[2]
    acnt = acnt + 1
    -- for i = 1, #alist do
    --   print("A", i)
    --   print(unpack(tree.stage[tree.stagenum][i]))
    -- end
  else
    local half = mfl((acnt + 1) / 2)
    local calcpos, preval = tree:lower_bound(half)
    local src = alist[calcpos]
    preval = preval + src
    local curtot = tree.stage[1][1][2]
    -- print("B", half, calcpos, src, bsum, curtot, asum[calcpos])
    local result = bsum + half * src - preval
      + (curtot - preval) - (acnt - half) * src
    print(src .. " " .. result)
  end
end


n, k = io.read("*n", "*n", "*l")
s = io.read()
for i = 1, n do
  if i == k then
    io.write(string.char(s:byte(k) + 32))
  else
    io.write(s:sub(i, i))
  end
end
io.write("\n")

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


local fst, snd = io.read(2, 2)
fst = tonumber(fst)
snd = tonumber(snd)
local yymm, mmyy = true, true
if fst >= 13 or fst <= 0 then
    mmyy = false
end
if snd >= 13 or snd <= 0 then
    yymm = false
end
if yymm and mmyy then
    print("AMBIGUOUS")
elseif yymm then
    print("YYMM")
elseif mmyy then
    print("MMYY")
else
    print("NA")
end

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


local List = {}
function List.new ()
    return {first = 0, last = -1}
end

function List.pushleft (list, value)
    local first = list.first - 1
    list.first = first
    list[first] = value
end

function List.pushright (list, value)
    local last = list.last + 1
    list.last = last
    list[last] = value
end

function List.popleft (list)
    local first = list.first
    local value = list[first]
    list[first] = nil
    list.first = first + 1
    return value
end

function List.popright (list)
    local last = list.last
    local value = list[last]
    list[last] = nil
    list.last = last - 1
    return value
end

function List.empty (list)
    return list.first > list.last
end

----------

local n=io.read("n")
local graph={}
for i=1,n do
    graph[i]={}
end
for i=1,n-1 do
    local u,v,w=io.read("n","n","n")
    w=w%2
    graph[u][v]=w
    graph[v][u]=w
end

local stack=List.new()
List.pushright(stack,{1,0})
local checked={true}
local color={1}

while not List.empty(stack) do
    local pop=List.popright(stack)
    local u=pop[1]
    local w=pop[2]
    for v,_ in pairs(graph[u]) do
        local nw=w+graph[u][v]
        if not checked[v] then
            if nw%2==0 then
                color[v]=1
            else
                color[v]=0
            end
            checked[v]=true
            List.pushright(stack,{v,nw})
        end
    end
end

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

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1


local ior = io.input()
local n, m = ior:read("*n", "*n")

local parent = {}
for i = 1, n do parent[i] = i end
-- for i = 1, n do groupsize[i] = 1 end

local function getroot(idx)
  while(parent[idx] ~= idx) do
    idx = parent[idx]
  end
  return idx
end

for i = 1, m do
  local x, y, z = ior:read("*n", "*n", "*n")
  local xroot, yroot = getroot(x), getroot(y)
  parent[x] = xroot
  parent[yroot] = xroot
  parent[y] = xroot
end
local grouplist = {}
local cnt = 0
for i = 1, n do
  local r = getroot(i)
  if(grouplist[r] == nil) then
    grouplist[r] = true
    cnt = cnt + 1
  end
end
print(cnt)

Score : 500 points

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

Let A_i be the integer written on the i-th card.

Your objective is to guess A_1, A_2, ..., A_N correctly.

You know the following facts:

For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.

You are a magician and can use the following magic any number of times:

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

It is guaranteed that there is no contradiction in given input.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq X_i < Y_i \leq N

1 \leq Z_i \leq 100

The pairs (X_i, Y_i) are distinct.

There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

Sample Input 1

3 1
1 2 1

Sample Output 1

2

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

100000 1
1 100000 100

Sample Output 3

99999


r,g,b,n=io.read("*n","*n","*n","*n")

counter=0
for i=0,n do
    for j=0,n-i do
        rg=r*i+g*j
        if n>=rg and (n-rg)%b==0 then
            counter=counter+1
        end
    end
end
print(counter)

Score : 100 points

Problem Statement

Snuke has N integers: 1,2,\ldots,N.
He will choose K of them and give those to Takahashi.

How many ways are there to choose K consecutive integers?

Constraints

All values in input are integers.

1 \leq K \leq N \leq 50

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: (1,2) and (2,3).

Sample Input 2

13 3

Sample Output 2

11


local ior = io.input()
local r, g, b, n = ior:read("*n", "*n", "*n", "*n")
local cnt = 0
for i = 0, n do
  for j = 0, n - i do
    local k = n - i - j
    if i % r == 0 and j % g == 0 and k % b == 0 then cnt = cnt + 1 end
  end
end
print(cnt)

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


local ior = io.input()
local n = ior:read("*n", "*l")
local num_b_st_a_ed = 0
local num_b_st = 0
local num_a_ed = 0

local tot = 0

for i = 1, n do
  local str = ior:read()
  local len = #str
  local isfirst = true
  local bcand = false
  local adet = false
  if(str:sub(1, 1) == "B") then bcand = true end
  if(str:sub(len, len) == "A") then adet = true end
  for j = 1, len - 1 do
    if(str:sub(j, j) == "A" and str:sub(j + 1, j + 1) == "B") then
      tot = tot + 1
      -- print("FOUND")
    end
  end
  if(adet and bcand) then
    num_b_st_a_ed = num_b_st_a_ed + 1
    -- print(i, "AB")
  elseif(adet) then
    num_a_ed = num_a_ed + 1
    -- print(i, "A")
  elseif(bcand) then
    num_b_st = num_b_st + 1
    -- print(i, "B")
  end
end

local min = math.min(num_a_ed, math.min(num_b_st, num_b_st_a_ed))
tot = tot + min * 2
num_a_ed, num_b_st, num_b_st_a_ed = num_a_ed - min, num_b_st - min, num_b_st_a_ed - min

if(num_b_st_a_ed == 0) then
  tot = tot + math.min(num_a_ed, num_b_st)
elseif(num_a_ed == 0) then
  if(0 < num_b_st_a_ed) then
    tot = tot + (num_b_st_a_ed - 1)
    if(0 < num_b_st) then tot = tot + 1 end
  end
else
  if(0 < num_b_st_a_ed) then
    tot = tot + (num_b_st_a_ed - 1)
    if(0 < num_a_ed) then tot = tot + 1 end
  end
end
-- print(num_a_ed, num_b_st, num_b_st_a_ed)
print(tot)

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


local n=io.read("n")

local t={}
for i=1,math.sqrt(n) do
    if n%i==0 then
        if i>1 then
            table.insert(t,i)
        end
        if i~=n//i then
            table.insert(t,n//i)
        end
    end
end
table.sort(t)

local sum=0
for i=1,#t do
    local m=t[i]-1
    if (m+1)*(n//m)==n then
        sum=sum+m
    end
end
print(sum)

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


-- Vicfred
-- https://atcoder.jp/contests/abc125/tasks/abc125_b
-- greedy
n = io.read("*n")
v, c = {}, {}
for i = 1, n do v[i] = io.read("*n") end
for i = 1, n do c[i] = io.read("*n") end
ans = 0
for i = 1, n do
    if(c[i] < v[i]) then
        ans = ans + v[i] - c[i]
    end
end
print(ans)

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


local n = io.read("*n")
local v, c = {}, {}
for i = 1, n do v[i] = io.read("*n") end
for i = 1, n do c[i] = io.read("*n") end
local sum = 0
for i = 1, n do
  if(c[i] < v[i]) then sum = sum + v[i] - c[i] end
end
print(sum)


Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \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 maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

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


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

local minus = false
local abs_min = 1000000001
local abs_sum = 0
for i=1,N do
    if A[i] < 0 then
        minus = not minus
    end
    abs_min = math.min(abs_min, math.abs(A[i]))
    abs_sum = abs_sum + math.abs(A[i])
end
local ans = abs_sum
if minus then
    ans = ans - 2 * abs_min
end
--print(minus, abs_min, abs_sum)
print(ans)


Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


n = io.read("*n", "*l")
s = io.read()
k = io.read("*n")
z = s:sub(k, k)
for i = 1, n do
  if z == s:sub(i, i) then
    io.write(z)
  else
    io.write("*")
  end
end
io.write("\n")

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


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

local _,white=s:gsub("%.","")
local black=0
local result=black+white
for i=1,n do
    if s:sub(i,i)=="#" then
        black=black+1
    else
        white=white-1
    end
    result=math.min(result,black+white)
end
print(result)

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


a, b = io.read("*n", "*n")
if (a == b) then
    print(a + b)
elseif (a > b) then
    print(2 * a - 1)
else
    print(2 * b - 1)
end

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


a, b = io.read("*n", "*n")
print(math.max(a + a - 1, b + b - 1, a + b))


local N=tonumber(io.read())
local H={}
for s in io.read():gmatch("(%S+)%s*")do
  H[#H+1]=tonumber(s)
end
local max=0
local cnt=0

for i=1,N do
  if(max<=H[i])then
    cnt=cnt+1
    max=H[i]
  end
end
print(cnt)

Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


local S=io.read()

local cnt=0
for i=1,#S do
  if(tonumber(S:sub(i,i))==i%2)then
    cnt=cnt+1
  end
end
print(math.min(cnt,#S-cnt))

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0