function split(str,sep)
	local ret = {}
	local pos = 0
	while true do
		local pp = string.find(str,sep,pos+1)
		if pp == nil then
			table.insert(ret,string.sub(str,pos+1))
			break
		else
			table.insert(ret,string.sub(str,pos+1,pp-1))
			pos = pp
		end
	end
	return ret
end
a = io.read()
t = split(a, " ")
n, x = tonumber(t[1]), tonumber(t[2])
s = {}
for i = 1, n do
	s[i] = io.read("*n")
end
table.sort(s)
ret = 0
for i = 1, n do
	if x >= s[i] then
		x = x - s[i]
		ret = ret + 1
	else
		break
	end
end
if x > 0 and ret > 0 then
	ret = ret - 1
end
print(ret)


function split(str,sep)
	local ret = {}
	local pos = 0
	while true do
		local pp = string.find(str,sep,pos+1)
		if pp == nil then
			table.insert(ret,string.sub(str,pos+1))
			break
		else
			table.insert(ret,string.sub(str,pos+1,pp-1))
			pos = pp
		end
	end
	return ret
end
a = io.read()
t = split(a, " ")
n, x = tonumber(t[1]), tonumber(t[2])
b = io.read()
t = split(b, " ")
s = {}
for i = 1, n do
	s[i] = tonumber(t[i])
end
table.sort(s)
ret = 0
for i = 1, n do
	if x >= s[i] then
		x = x - s[i]
		ret = ret + 1
	else
		break
	end
end
if x > 0 and ret > 0 then
	ret = ret - 1
end
print(ret)

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

Score : 200 points

Problem Statement

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

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.

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.


local n, m = io.read("*n", "*n", "*l")
local s = io.read()
local v = {}
local edge = {}
local acnt, bcnt = {}, {}
for i = 1, n do
  v[i] = s:byte(i) - 64
  edge[i] = {}
  acnt[i], bcnt[i] = 0, 0
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
  if v[a] == 1 then
    acnt[b] = acnt[b] + 1
  else
    bcnt[b] = bcnt[b] + 1
  end
  if v[b] == 1 then
    acnt[a] = acnt[a] + 1
  else
    bcnt[a] = bcnt[a] + 1
  end
end
local tasks = {}
for i = 1, n do
  if acnt[i] * bcnt[i] == 0 then
    table.insert(tasks, i)
  end
end
local done = 0
while done < #tasks do
  done = done + 1
  local src = tasks[done]
  local cnttbl = v[src] == 1 and acnt or bcnt
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if 0 < acnt[dst] and 0 < bcnt[dst] then
      cnttbl[dst] = cnttbl[dst] - 1
      if cnttbl[dst] == 0 then
        table.insert(tasks, dst)
      end
    end
  end
end
print(#tasks == n and "No" or "Yes")

Score : 900 points

Problem Statement

You are given an undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.
In addition, each vertex has a label, A or B. The label of Vertex i is s_i.
Edge i bidirectionally connects vertex a_i and b_i.

The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times.
Today, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.

For example, in a graph where Vertex 1 has the label A and Vertex 2 has the label B, if Nusook travels along the path 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2, the resulting string is ABABB.

Determine if Nusook can make all strings consisting of A and B.

Constraints

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

1 \leq M \leq 2 \times 10^{5}

|s| = N

s_i is A or B.

1 \leq a_i, b_i \leq N

The given graph may NOT be simple or connected.

Input

Input is given from Standard Input in the following format:

N M
s
a_1 b_1
:
a_{M} b_{M}

Output

If Nusook can make all strings consisting of A and B, print Yes; otherwise, print No.

Sample Input 1

2 3
AB
1 1
1 2
2 2

Sample Output 1

Yes

Since Nusook can visit Vertex 1 and Vertex 2 in any way he likes, he can make all strings consisting of A and B.

Sample Input 2

4 3
ABAB
1 2
2 3
3 1

Sample Output 2

No

For example, Nusook cannot make BB.

The given graph may not be connected.

Sample Input 3

13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9

Sample Output 3

Yes

Sample Input 4

13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11

Sample Output 4

No


local h, w = io.read("*n", "*n")

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

local map = {}
for i = 1, h * w do map[i] = 0 end
for i_h = 1, h do
  for i_w = 1, w do
    local idx = getindex(i_h, i_w)
    map[idx] = io.read("*n")
  end
end

local command = {}

for i_h = 1, h do
  local inc = i_h % 2 == 1 and 1 or -1
  local bg = i_h % 2 == 1 and 1 or w
  local ed = i_h % 2 == 1 and w or 1
  for i_w = bg, ed, inc do
    local src = getindex(i_h, i_w)
    if i_w == ed then
      if i_h ~= h then
        if map[src] % 2 == 1 then
          local dst = getindex(i_h + 1, i_w)
          local tmp = {i_h, i_w, i_h + 1, i_w}
          table.insert(command, tmp)
          map[src], map[dst] = map[src] - 1, map[dst] + 1
        end
      end
    else
      local dst = getindex(i_h, i_w + inc)
      if map[src] % 2 == 1 then
        local tmp = {i_h, i_w, i_h, i_w + inc}
        table.insert(command, tmp)
        map[src], map[dst] = map[src] - 1, map[dst] + 1
      end
    end
  end
end
print(#command)
for i = 1, #command do
  print(table.concat(command[i], " "))
end

Score : 400 points

Problem Statement

There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).

In Cell (i, j), a_{ij} coins are placed.

You can perform the following operation any number of times:

Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.

Maximize the number of cells containing an even number of coins.

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

H W
a_{11} a_{12} ... a_{1W}
a_{21} a_{22} ... a_{2W}
:
a_{H1} a_{H2} ... a_{HW}

Output

Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:

N
y_1 x_1 y_1' x_1'
y_2 x_2 y_2' x_2'
:
y_N x_N y_N' x_N'

That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.

In the (i+1)-th line (1 \leq i \leq N), print four integers y_i, x_i, y_i' and x_i' (1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').

Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

3
2 2 2 3
1 1 1 2
1 3 1 2

Every cell contains an even number of coins after the following sequence of operations:

Move the coin in Cell (2, 2) to Cell (2, 3).

Move the coin in Cell (1, 1) to Cell (1, 2).

Move one of the coins in Cell (1, 3) to Cell (1, 2).

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

3
1 1 1 2
1 2 2 2
3 1 3 2

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


K=io.read("n")
if K%2==0 then
  print(K*K/4)
  else
  print((K*K-1)/4)
end

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


K=io.read("n")
if K%2==0 then
  print(K*K/4)
  else
  print((K//2)*((K//2)+1))
end


local x1,y1,x2,y2=io.read("n","n","n","n")
local dx,dy=x2-x1,y2-y1
local adx,ady=math.abs(dx),math.abs(dy)
local sdx,sdy=1,1
if dx<0 then
  sdx=-1
end
if dy<0 then
  sdy=-1
end
local x3=x2 - sdy*ady
local y3=y2 + sdx*adx
local x4=x3 - sdx*adx
local y4=y3 - sdy*ady
print(x3,y3,x4,y4)

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


N=io.read("n")
K=io.read("n")
if K%2==1 then
  s=0
  for i=K,N,K do
    s=s+1
  end
  s=s*s*s
end
if K%2==0 then
  s=0
  for i=K//2,N,K//2 do
    s=s+1
  end
  if s%2==0 then
    s=s*s*s//4
  else
    s=math.ceil((s//2)^3+(s//2+1)^3)
  end
end
print(s)

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


local n,a=io.read("*n","*n")
if a%2==1 then
  x=math.floor(n/a)
  print(x*x*x)
else
  x=math.floor((n+math.floor(a/2))/a)
  y=math.floor(n/a)
  print(x*x*x + y*y*y)
end


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

Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


local read = io.read
local concat = table.concat
local char = string.char

local h, w = read("n", "n", "l")
local black = string.byte("#")

local a_t = {}
for i = 1, h do
	a_t[i] = read()
end

local del_i_t = {}
for i = 1, h do
	local is_breaked = false
	for j = 1, w do
		if a_t[i]:byte(j) == black then
			is_breaked = true
			break
		end
	end
	if not is_breaked then
		del_i_t[i] = true
	end
end

local del_j_t = {}
for j = 1, w do
	local is_breaked = false
	for i = 1, h do
		if a_t[i]:byte(j) == black then
			is_breaked = true
			break
		end
	end
	if not is_breaked then
		del_j_t[j] = true
	end
end

local copy_a_t = {}
local last_i = 0
for i = 1, h do
	if not del_i_t[i] then
		last_i = last_i + 1
		local last_j = 0
		copy_a_t[last_i] = {}
		for j = 1, w do
			if not del_j_t[j] then
				last_j = last_j + 1
				copy_a_t[last_i][last_j] = a_t[i]:sub(j, j) --char(a_t[i]:byte(j))
			end
		end
	end
end

local out_t = {}
for i = 1, #copy_a_t do
	out_t[i] = concat(copy_a_t[i], "")
end

print(concat(out_t, "\n"))

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

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


local read = io.read
local concat = table.concat
local char = string.char

local h, w = read("n", "n", "l")
local black = string.byte("#")

local a_t = {}
for i = 1, h do
	a_t[i] = read()
end

local del_i_t = {}
for i = 1, h do
	local is_breaked = false
	for j = 1, w do
		if a_t[i]:byte(j) == black then
			is_breaked = true
			break
		end
	end
	if not is_breaked then
		del_i_t[i] = true
	end
end

local del_j_t = {}
for j = 1, w do
	local is_breaked = false
	for i = 1, h do
		if a_t[i]:byte(j) == black then
			is_breaked = true
			break
		end
	end
	if not is_breaked then
		del_j_t[j] = true
	end
end

local copy_a_t = {}
local last_i = 0
for i = 1, h do
	if not del_i_t[i] then
		last_i = last_i + 1
		local last_j = 0
		copy_a_t[last_i] = {}
		for j = 1, w do
			if not del_j_t[j] then
				last_j = last_j + 1
				copy_a_t[last_i][last_j] = char(a_t[i]:byte(j))
			end
		end
	end
end

local out_t = {}
for i = 1, #copy_a_t do
	out_t[i] = concat(copy_a_t[i], "")
end

print(concat(out_t, "\n"))


local H, W = io.read("n", "n", "l")
local A = {}
for h=1,H do
  A[h] = {}
  for w=1,W do
    local a = io.read(1)
    A[h][w] = a
  end
  io.read("l")
end
local RH ={}
for h=1,H do
  local remove = true
  for w=1,W do
    if A[h][w] == '#' then
      remove = false
      break
    end
  end
  if remove then
    RH[h] = 1
  end
end
local RW = {}
for w=1,W do
  local remove = true
  for h=1,H do
    if A[h][w] == '#' then
      remove = false
      break
    end
  end
  if remove then
    RW[w] = 1
  end
end

for h=1,H do
  if not RH[h] then
    for w=1,W do
      if not RW[w] then
        io.write(A[h][w])
      end
    end
    print()
  end
end


H=io.read("n")
W=io.read("n")
t={}
for i=1,H do
  t[i]={}
  for j=1,W do
    t[i][j]=io.read()
  end
end
for i=1,H do
  res=true
  for j=1,W do
    if t[i][j]=="#" then
      res=false
      break
    end
  end
  if res==true then
    table.remove(t,i)
  end
end
for i=1,W do
  res=true
  for j=1,#t do
    if t[j][i]=="#" then
      res=false
      break
    end
  end
  if res==true then
    for k=1,#t do
      table.remove(t[k],i)
    end
  end
end
for i,v in ipairs(t) do
  for j,p in ipairs(v) do
    io.write(p)
  end
  print()
end


H=io.read("n")
W=io.read("n")
t={}
for i=1,H do
  t[i]={}
  for j=1,W do
    t[i][j]=io.read()
  end
end
for i=1,H do
  res=true
  for j=1,W do
    if t[i][j]=="#" then
      res=false
      break
    end
  end
  if res==true then
    table.remove(t,i)
  end
end
for i=1,W do
  res=true
  for j=1,#t do
    if t[j][i]=="#" then
      res=false
      break
    end
  end
  if res==true then
    for k=1,#t do
      table.remove(t[k],i)
    end
  end
end
for i,v in ipairs(t) do
  for j,p in ipairs(v) do
  io.write(p)
end
print()
end


H=io.read("n")
W=io.read("n")
t={}
for i=1,H do
  t[i]={}
  for j=1,W do
    t[i][j]=io.read()
  end
end
for i=1,H do
  res=true
  for j=1,W do
    if t[i][j]=="#" then
      res=false
      break
    end
  end
  if res==true then
    table.remove(t,i)
  end
end
for i=1,W do
  res=true
  for j=1,#t do
    if t[j][i]=="#" then
      res=false
      break
    end
  end
  if res==true then
    for k=1,#t do
      table.remove(t[k],i)
    end
  end
end
for i,v in pairs(t) do
  for j,k in pairs(v) do
  io.write(k)
end
print()
end


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

local min=math.maxinteger
for i=1,n-k+1 do
    local l=i
    local r=k+i-1
    local ltor=math.abs(x[l])+math.abs(x[r]-x[l])
    local rtol=math.abs(x[r])+math.abs(x[r]-x[l])
    min=math.min(min,ltor,rtol)
end
print(min)

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

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

Sample Output 4

10


local mfl, mce, mmi = math.floor, math.ceil, math.min
local mma = math.max
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.stage[i + 1][j * 2 - 1] + self.stage[i + 1][j * 2]
    end
  end
end
SegTree.clear = function(self)
  for i = self.stagenum, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = 0
    end
  end
end
SegTree.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  self:clear()
end
SegTree.getRange = function(self, right)
  local stage = 1
  while right < self.size[stage] do
    stage = stage + 1
  end
  local ret = 0
  local l, r = 1, right
  while true do
    local sz = self.size[stage]
    ret = ret + self.stage[stage][mce(l / sz)]
    l = l + sz
    if l <= r then
      local nsize = r + 1 - l
      while nsize < self.size[stage] do
        stage = stage + 1
      end
    else
      break
    end
  end
  return ret
end
SegTree.inc = function(self, idx)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + 1
end
SegTree.dec = function(self, idx)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] + self.stage[i + 1][rem]
    idx = dst
  end
end
SegTree.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n)
  return obj
end

local n = io.read("*n")
local a = {}
local t = {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = a[i]
end
table.sort(t)
local totcnt = mfl(n * (n + 1) / 2)
local st = SegTree.new(n * 2 + 1)
local z = {}
local function solve(v)
  st:clear()
  local ofst = n + 1
  z[1] = v <= a[1] and 1 or -1
  for i = 2, n do
    z[i] = v <= a[i] and z[i - 1] + 1 or z[i - 1] - 1
  end
  for i = 1, n do
    st:inc(z[i] + ofst)
  end
  st:updateAll()
  local cnt = 0
  local n2_1 = 2 * n + 1
  for i = 1, n do
    local left = i == 1 and ofst or z[i - 1] + ofst
    if left <= 1 then
      cnt = cnt + n + 1 - i
    else
      cnt = cnt + n + 1 - i - st:getRange(left - 1)
    end
    st:dec(z[i] + ofst)
  end
  return totcnt <= cnt * 2
end
local function solveAll()
  if solve(t[n]) then
    return t[n]
  end
  local min, max = 1, n
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if solve(t[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return t[min]
end
print(solveAll())

Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8


Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

N is an integer between 1 and 200 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


local n = io.read("*n")
if 195 <= n then print(5)
elseif 189 <= n then print(4)
elseif 165 <= n then print(3)
elseif 135 <= n then print(2)
elseif 105 <= n then print(1)
else print(0)
end


N=io.read("n")
t=0
for i=1,N,2 do
  s=0
  for j=3,i//3,2 do
    if i%j==0 then
      s=s+1
    end
  end
  if s==6 then
    t=t+1
  end
end
print(t)


local s = io.read()
local k = io.read("*n")
ret = "1"
ilast = 101
for i = 1, #s do
  local a = string.sub(s, i, i)
  if(a ~= "1") then
    ilast = i - 1
    ret = a
    break
  end
end
if(k <= ilast) then print("1") else print(ret) end

Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

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

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2


local n, m, q = io.read("*n", "*n", "*n")

local t = {}
for i = 1, n * n do t[i] = 0 end

for i = 1, m do
  local l, r = io.read("*n", "*n")
  local idx = (r - 1) * n + l
  t[idx] = t[idx] + 1
end

for l = 1, n do
  for r = 2, n do
    local src = (r - 2) * n + l
    local dst = (r - 1) * n + l
    t[dst] = t[dst] + t[src]
  end
end
for r = 1, n do
  for l = n - 1, 1, -1 do
    local src = (r - 1) * n + l + 1
    local dst = (r - 1) * n + l
    t[dst] = t[dst] + t[src]
  end
end

for i = 1, q do
  local a, b = io.read("*n", "*n")
  local idx = (b - 1) * n + a
  print(t[idx])
end

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

N is an integer between 1 and 500 (inclusive).

M is an integer between 1 and 200 \ 000 (inclusive).

Q is an integer between 1 and 100 \ 000 (inclusive).

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


N, K = io.read("*n", "*n")

if N % K > 0 then
  print(1)
else
  print(0)
end

Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


local n = tonumber(io.read());
local count4, count7 = n // 4, n // 7;
local result = "No";

for i = 0, count4 do
    for j = 0 , count7 do
        if i * 4 + j * 7 == n then
            result = "Yes";
            goto exit
        end
    end
end

::exit::
print(result);

Score : 200 points

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.
Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

Sample Input 1

11

Sample Output 1

Yes

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

Sample Input 2

40

Sample Output 2

Yes

If you buy ten cakes, the total will be 4 \times 10 = 40 dollars.

Sample Input 3

3

Sample Output 3

No

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.


local n=io.read("n")
local output=""
while n~=0 do
    local r=(n>0 and n%2 or -n%2)
    n=(n-r)//-2
    output=r..output
end
print(#output>0 and output or 0)

Score : 300 points

Problem Statement

Given an integer N, find the base -2 representation of N.

Here, S is the base -2 representation of N when the following are all satisfied:

S is a string consisting of 0 and 1.

Unless S = 0, the initial character of S is 1.

Let S = S_k S_{k-1} ... S_0, then S_0 \times (-2)^0 + S_1 \times (-2)^1 + ... + S_k \times (-2)^k = N.

It can be proved that, for any integer M, the base -2 representation of M is uniquely determined.

Constraints

Every value in input is integer.

-10^9 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

Print the base -2 representation of N.

Sample Input 1

-9

Sample Output 1

1011

As (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9.

Sample Input 2

123456789

Sample Output 2

11000101011001101110100010101

Sample Input 3

0

Sample Output 3

0


n = io.read("*n")
dig = 1
flg = 1
if(0 < n) then flg = 0 else n = -n end
t = {}
while(0 < n) do
  rem = n % 2
  if(dig % 2 == flg and rem == 1) then
    n = n + 1
  end
  table.insert(t, 1, rem)
  n = math.floor(n / 2)
  flg = (flg + 1) % 2
end
len = #t
for i = 1, len do
  io.write(string.format("%d", t[i]))
end
if(len == 0) then io.write(0) end
io.write("\n")


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

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


R = io.read("*n")
if R < 1200 then
  print("ABC")
elseif R < 2800 then
  print("ARC")
else
  print("AGC")
end


S=io.read()
res="AC"
if string.sub(S,1,1)~="A" then
  res="WA"
end
if string.sub(S,3,3)~="C" then
  res="WA"
end
if string.sub(S,2,2)<"a" or string.sub(S,2,2)>"z" then
  res="WA"
end
for i=4,#S do
  if string.sub(S,i,i)<"a" or string.sub(S,i,i)>"z" then
    res="WA"
    break
  end
end
print(res)

Score : 200 points

Problem Statement

You are given a string S. Each character of S is uppercase or lowercase English letter.
Determine if S satisfies all of the following conditions:

The initial character of S is an uppercase A.

There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).

All letters except the A and C mentioned above are lowercase.

Constraints

4 ≤ |S| ≤ 10 (|S| is the length of the string S.)

Each character of S is uppercase or lowercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

If S satisfies all of the conditions in the problem statement, print AC; otherwise, print WA.

Sample Input 1

AtCoder

Sample Output 1

AC

The first letter is A, the third letter is C and the remaining letters are all lowercase, so all the conditions are satisfied.

Sample Input 2

ACoder

Sample Output 2

WA

The second letter should not be C.

Sample Input 3

AcycliC

Sample Output 3

WA

The last letter should not be C, either.

Sample Input 4

AtCoCo

Sample Output 4

WA

There should not be two or more occurrences of C.

Sample Input 5

Atcoder

Sample Output 5

WA

The number of C should not be zero, either.


S=io.read()
res="AC"
if string.sub(S,1,1)~="A" then
  res="WA"
end
if string.sub(S,3,3)~="C" then
  res="WA"
end
if string.sub(S,2,2)<"a" or string.sub(S,2,2)>"z" then
  res="WA"
end
for i=4,#S do
  if string.sub(S,i,i)<"a" or string.sub(S,i,i)>"z" then
    res="WA"
  end
end
print(res)


local mod = 1000000007
local mfl = math.floor

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

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

local s = io.read()
local n = #s
local ret = 0
local acnt = 0
local abcnt = 0
local mul = 1
for i = 1, n do
  local ss = s:sub(i, i)
  if ss == "A" then
    acnt = badd(acnt, mul)
  elseif ss == "B" then
    abcnt = badd(abcnt, acnt)
  elseif ss == "C" then
    ret = badd(ret, abcnt)
  else
    ret = badd(bmul(ret, 3), abcnt)
    abcnt = badd(bmul(abcnt, 3), acnt)
    acnt = badd(bmul(acnt, 3), mul)
    mul = bmul(mul, 3)
  end
end
print(ret)

Score : 400 points

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

1 ≤ i < j < k ≤ |T| (|T| is the length of T.)

T_i = A (T_i is the i-th character of T from the beginning.)

T_j = B

T_k = C

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

You are given a string S. Each character of S is A, B, C or ?.

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

This sum can be extremely large, so print the sum modulo 10^9 + 7.

Constraints

3 ≤ |S| ≤ 10^5

Each character of S is A, B, C or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

Sample Input 1

A??C

Sample Output 1

8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

AAAC: 0

AABC: 2

AACC: 0

ABAC: 1

ABBC: 2

ABCC: 2

ACAC: 0

ACBC: 1

ACCC: 0

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Sample Input 2

ABCBC

Sample Output 2

3

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

Sample Input 3

????C?????B??????A???????

Sample Output 3

979596887

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.


Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


local a, b, c = string.match(io.read(), "(%d+) (%d+) (%d+)");
local x = {
  tonumber(a),
  tonumber(b),
  tonumber(c),
};

table.sort(x);

print(math.abs(x[2] - x[1]) + math.abs(x[3] - x[2]));


local s = {};
local t = {};

for v in string.gmatch(io.read(),"%w") do
  table.insert(s, v);
end

for v in string.gmatch(io.read(),"%w") do
  table.insert(t, v);
end

table.sort(s);
table.sort(t);

local result = "Yes";
for i = 1, #s do
  if s[i] ~= t[i] then
    result = "No";
    break;
  end
end

print(result);

Score : 200 points

Problem Statement

You are given string S and T consisting of lowercase English letters.

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}.

Here, |X| denotes the length of the string X.

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


io.read()

fm = 0

for v in string.gmatch(io.read(), "(%d+)") do
  fm = fm + (tonumber(v) - 1)
end

print(fm)

Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


local n, m = io.read("*n", "*n")
local left, right = {}, {}
local idx = {}
for i = 1, m do
  left[i], right[i] = io.read("*n", "*n")
  idx[i] = i
end
local function sortfunc(a, b)
  if right[a] == right[b] then
    return left[a] < left[b]
  else
    return right[a] < right[b]
  end
end
table.sort(idx, sortfunc)

local cnt = 0
local curleft = 1
for i = 1, m do
  local l, r = left[idx[i]], right[idx[i]]
  if curleft <= l then
    curleft = r
    cnt = cnt + 1
  end
end
print(cnt)

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


local n, m = io.read("*n", "*n")
local t = {}
for i = 1, m do
  local a, b = io.read("*n", "*n")
  local tmp = {a, b, false}
  table.insert(t, tmp)
end
table.sort(t, function(x, y)
  if x[2] == y[2] then return x[1] < y[1]
  else return x[2] < y[2] end
end
)
local curidx = 1
local ret = 0
while curidx <= m do
  local cutpos = t[curidx][2] - 1
  ret = ret + 1
  curidx = curidx + 1
  while curidx <= m and t[curidx][1] <= cutpos do
    curidx = curidx + 1
  end
end
print(ret)


io.read()

p = 0
ans = 0

for v in string.gmatch(io.read(), "(%d+)") do
  if v == p then
    ans = ans + 1
    p = 0
  else
    p = v
  end
end

print(ans)

Score : 200 points

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i.
If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

Takahashi can change the color of one slime to any of the 10000 colors by one spell.
How many spells are required so that no slimes will start to combine themselves?

Constraints

2 \leq N \leq 100

1 \leq a_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of spells required.

Sample Input 1

5
1 1 2 2 2

Sample Output 1

2

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.

Sample Input 2

3
1 2 1

Sample Output 2

0

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.

Sample Input 3

5
1 1 1 1 1

Sample Output 3

2

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.

Sample Input 4

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

Sample Output 4

4


local function getgcd(x, y)
  while(0 < x and 0 < y) do
    if(x < y) then y = y % x else x = x % y end
  end
  return math.max(x, y)
end

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

for i = 1, t do
  local a, b, c, d = io.read("*n", "*n", "*n", "*n")
  if(a < b) then print("No")
  elseif(d < b) then print("No")
  elseif(b <= c) then print("Yes")
  elseif(d % b == 0) then
    if(a % b <= c) then print("Yes") else print("No") end
  else
    local gcd = getgcd(a, getgcd(b, d))
    if(c // gcd + 1 == b // gcd) then print("Yes") else print("No") end
  end
end

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning.
Snuke buys B cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

1 \leq T \leq 300

1 \leq A, B, C, D \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.

Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 6
→D x

In the second query, the number of cans of juice in stock changes as follows:

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 15
→D 8
→N 8
→D 1
→N 10
→D 3
→N 12
→D 5
→N 14
→D 7
→N 7
→D 0
→N 9
→D 2
→N 11
→D …

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

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

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No


function M(n,t)
  for i,v in pairs(t) do
    if n == v then
      return true
    end
  end
  return false
end

T = io.read("*n")

A = {}
B = {}
C = {}
D = {}

for v = 1,T do
  table.insert(A, io.read("*n"))
  table.insert(B, io.read("*n"))
  table.insert(C, io.read("*n"))
  table.insert(D, io.read("*n"))
end

for v = 1,T do
  local j = A[v]
  local W = {}
  local ret = "No"

  while j >= 0 do
    if M(j, W) then
      ret = "Yes"
      break
    else
      table.insert(W, j)
    end

    if j >= B[v] then
      j = j - B[v]
    else
      break
    end

    if j <= C[v] then
      j = j + D[v]
    else
      break
    end
  end

  print(ret)
end


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

local precheck=true
local t={s:byte(1,2*n)}
table.sort(t)
for i=1,2*n-1,2 do
    if t[i]~=t[i+1] then
        precheck=false
    end
end
if not precheck then
    print(0)
    return
end

local rmap={}
local bmap={}
local bit=require("bit")
local lshift=bit.lshift
local band=bit.band
for bit=0,lshift(1,n)-1 do
    local r1=""
    local r2=""
    local b1=""
    local b2=""
    for i=0,n-1 do
        if band(bit,lshift(1,i))>0 then
            local r=i+1
            local b=i+1+n
            r1=r1..s:sub(r,r)
            b2=s:sub(b,b)..b2
        else
            local r=i+1
            local b=i+1+n
            b1=s:sub(b,b)..b1
            r2=r2..s:sub(r,r)
        end
    end
    local rkey=r1.." "..r2
    local bkey=b1.." "..b2
    rmap[rkey]=(rmap[rkey] or 0)+1
    bmap[bkey]=(bmap[bkey] or 0)+1
end

local combination=0
for k,_ in pairs(rmap) do
    combination=combination+rmap[k]*(bmap[k] or 0)
end
print(combination)

Score : 600 points

Problem Statement

You are given a string S of length 2N consisting of lowercase English letters.

There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition?

The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.

Constraints

1 \leq N \leq 18

The length of S is 2N.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to paint the string that satisfy the condition.

Sample Input 1

4
cabaacba

Sample Output 1

4

There are four ways to paint the string, as follows:

cabaacba

cabaacba

cabaacba

cabaacba

Sample Input 2

11
mippiisssisssiipsspiim

Sample Output 2

504

Sample Input 3

4
abcdefgh

Sample Output 3

0

Sample Input 4

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 4

9075135300

The answer may not be representable as a 32-bit integer.


s,w=io.read(),io.read("*n")
for i=1,#s,w do
	io.write(s:sub(i,i))
end

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
We will write down this string, starting a new line after every w letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

Constraints

1 \leq w \leq |S| \leq 1000

S consists of lowercase English letters.

w is an integer.

Input

Input is given from Standard Input in the following format:

S
w

Output

Print the desired string in one line.

Sample Input 1

abcdefgh
3

Sample Output 1

adg

When we write down abcdefgh, starting a new line after every three letters, we get the following:

abc

def

gh

Concatenating the letters at the beginnings of these lines, we obtain adg.

Sample Input 2

lllll
1

Sample Output 2

lllll

Sample Input 3

souuundhound
2

Sample Output 3

suudon


local mmi, mma = math.min, math.max
local n, m = io.read("*n", "*n")
local edge = {}
local v = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  v[i] = false
  len[i] = false
end
v[1] = 0
len[1] = 0
local evmin = 0
local oddmin = false
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c
  edge[b][a] = c
  if a == 1 or b == 1 then
    oddmin = c
  end
end
local rem_edge = {}
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for dst, sum in pairs(edge[src]) do
    if not v[dst] then
      edge[dst][src] = nil
      v[dst] = sum - v[src]
      len[dst] = 1 - len[src]
      if len[dst] == 1 then
        oddmin = mmi(oddmin, v[dst])
      else
        evmin = mmi(evmin, v[dst])
      end
      table.insert(tasks, dst)
    else
      table.insert(rem_edge, {src, dst, sum})
      edge[dst][src] = nil
    end
  end
end
if #rem_edge == 0 then
  local diff = evmin - 1
  oddmin = oddmin + diff
  print(mma(0, oddmin))
else
  local valid = true
  local offset = false
  for i = 1, #rem_edge do
    local p1, p2, sum = rem_edge[i][1], rem_edge[i][2], rem_edge[i][3]
    if len[p1] + len[p2] == 1 then
      if v[p1] + v[p2] ~= sum then
        valid = false break
      end
    else
      if len[p1] + len[p2] == 0 then
        if not offset then
          offset = sum - v[p1] - v[p2]
          if offset % 2 == 1 then valid = false break end
          offset = math.floor(offset / 2)
        else
          if v[p1] + v[p2] + offset * 2 ~= sum then
            valid = false break
          end
        end
      else
        if not offset then
          offset = v[p1] + v[p2] - sum
          if offset % 2 == 1 then valid = false break end
          offset = math.floor(offset / 2)
        else
          if v[p1] + v[p2] - offset * 2 ~= sum then
            valid = false break
          end
        end
      end
    end
    if offset then
      if evmin + offset < 1 then valid = false end
      if oddmin - offset < 1 then valid = false end
    end
  end
  print(valid and 1 or 0)
end

Score : 600 points

Problem Statement

Kenkoooo found a simple connected graph.
The vertices are numbered 1 through n.
The i-th edge connects Vertex u_i and v_i, and has a fixed integer s_i.

Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied:

For every edge i, the sum of the positive integers written in Vertex u_i and v_i is equal to s_i.

Find the number of such ways to write positive integers in the vertices.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq u_i < v_i \leq n

2 \leq s_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

The graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m
u_1 v_1 s_1
:
u_m v_m s_m

Output

Print the number of ways to write positive integers in the vertices so that the condition is satisfied.

Sample Input 1

3 3
1 2 3
2 3 5
1 3 4

Sample Output 1

1

The condition will be satisfied if we write 1,2 and 3 in vertices 1,2 and 3, respectively.
There is no other way to satisfy the condition, so the answer is 1.

Sample Input 2

4 3
1 2 6
2 3 7
3 4 5

Sample Output 2

3

Let a,b,c and d be the numbers to write in vertices 1,2,3 and 4, respectively.
There are three quadruples (a,b,c,d) that satisfy the condition:

(a,b,c,d)=(1,5,2,3)

(a,b,c,d)=(2,4,3,2)

(a,b,c,d)=(3,3,4,1)

Sample Input 3

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

Sample Output 3

0


local mmi, mma = math.min, math.max
local n, m = io.read("*n", "*n")
local edge = {}
local v = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  v[i] = false
  len[i] = false
end
v[1] = 0
len[1] = 0
local evmin = 0
local oddmin = false
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c
  edge[b][a] = c
  if a == 1 or b == 1 then
    oddmin = c
  end
end
local rem_edge = {}
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for dst, sum in pairs(edge[src]) do
    if not v[dst] then
      edge[dst][src] = nil
      v[dst] = sum - v[src]
      len[dst] = 1 - len[src]
      if len[dst] == 1 then
        oddmin = mmi(oddmin, v[dst])
      else
        evmin = mmi(evmin, v[dst])
      end
      table.insert(tasks, dst)
    else
      table.insert(rem_edge, {src, dst, sum})
    end
  end
end
if #rem_edge == 0 then
  local diff = evmin - 1
  oddmin = oddmin + diff
  print(math.max(0, oddmin))
else
  local valid = true
  local offset = false
  for i = 1, #rem_edge do
    local p1, p2, sum = rem_edge[i][1], rem_edge[i][2], rem_edge[i][3]
    if len[p1] + len[p2] == 1 then
      if v[p1] + v[p2] ~= sum then
        valid = false break
      end
    else
      if len[p1] + len[p2] == 0 then
        if not offset then
          offset = sum - v[p1] - v[p2]
          if offset % 2 == 1 then valid = false break end
        else
          if v[p1] + v[p2] + offset * 2 ~= sum then
            valid = false break
          end
        end
      else
        if not offset then
          offset = v[p1] + v[p2] - sum
          if offset % 2 == 1 then valid = false break end
        else
          if v[p1] + v[p2] - offset * 2 ~= sum then
            valid = false break
          end
        end
      end
    end
    if offset then
      if evmin + offset < 1 then valid = false end
      if oddmin - offset < 1 then valid = false end
    end
    print(valid and 1 or 0)
  end
end


local N = tonumber(io.read())
local result = N % 2 == 0 and N or N * 2
io.write(result .. '\n')

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


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

local biggest, smallest = 0, 1000000000
for i = 1, N do
    if biggest < A[i] then
        biggest = A[i]
    end
    if smallest > A[i] then
        smallest = A[i]
    end
end
print(biggest - smallest)

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


local N = tonumber(io.read())
local A = {}
for n in io.read():gmatch('%d+') do
  table.insert(A, tonumber(n))
end
local result = math.max(unpack(A)) - math.min(unpack(A))
io.write(result .. '\n')


local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n") - i
end
if n == 1 then
  print(0)
else
  table.sort(a)
  local hn = n // 2
  local pos = a[hn]
  local ret = 0
  for i = 1, hn do
    ret = ret + pos - a[i]
  end
  for i = hn + 1, n do
    ret = ret + a[i] - pos
  end
  print(ret)
end

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


N=io.read("n")
t={}
for i=1,N do
  t[i]=io.read("n")-i
end
table.sort(t)
s=(t[#t]-t[1])//2
res=0
for i=1,N do
  res=res+math.abs(t[i]-s)
end
ser=0
k=(t[#t]-t[1])//2+1
for i=1,N do
  ser=ser+math.abs(t[i]-k)
end
print(math.min(res,ser))


local mfl, mma, mmi = math.floor, math.max, math.min
local function lower_bound(ary, x, min, max)
  if(x <= ary[min]) then return min end
  if(ary[max] < x) then return max + 1 end
  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 ret = nil
local n = io.read("*n")
local tot = {}
local cur = 0
for i = 1, n do
  cur = cur + io.read("*n")
  tot[i] = cur
end
for i = 3, n - 1 do
  local leftsum = tot[i - 1]
  local rightsum = tot[n] - leftsum
  local leftlb = lower_bound(tot, leftsum / 2, 1, i - 1)
  leftlb = mmi(leftlb, i - 2)
  local rightlb = lower_bound(tot, leftsum + rightsum / 2, i, n)
  rightlb = mmi(rightlb, n - 1)
  for k = 1, 4 do
    local v1 = tot[leftlb]
    local v3 = tot[rightlb] - leftsum
    if k <= 2 then
      if(1 < leftlb) then
        v1 = tot[leftlb - 1]
      end
    end
    if k % 2 == 0 then
      if(i < rightlb) then
        v3 = tot[rightlb - 1] - leftsum
      end
    end
    local v2 = leftsum - v1
    local v4 = rightsum - v3
    local mi = mmi(v1, mmi(v2, mmi(v3, v4)))
    local ma = mma(v1, mma(v2, mma(v3, v4)))
    if(ret == nil) then
      ret = ma - mi
    else
      ret = mmi(ret, ma - mi)
    end
  end
end
print(ret)

Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994


local mfl = math.floor
local alltask = {}
local stagetask = {}
local a = {}
local tot = 1
local function bdp_prepare(n)
  for i = 1, n do
    tot = tot * 2
    stagetask[i] = {}
  end
  local a0 = io.read("*n")
  for i = 1, tot - 1 do
    a[i] = io.read("*n")
    if a[i] < a0 then
      alltask[i] = {tot, i}
    else
      alltask[i] = {i, tot}
    end
  end
  a[tot] = a0
  a[tot + 1] = 0
  for i = 1, tot - 1 do
    local ti = i
    local cnt = 0
    for j = 1, n do
      if ti % 2 == 1 then
        cnt = cnt + 1
        ti = ti - 1
      end
      ti = mfl(ti / 2)
    end
    table.insert(stagetask[cnt], i)
  end
end

local function bdp_walk(src, dst)
  local l1 = alltask[dst][1]
  local l2 = alltask[dst][2]
  local r1 = alltask[src][1]
  local r2 = alltask[src][2]
  if l1 == r1 or l2 == r1 then r1 = tot + 1 end
  if l1 == r2 or l2 == r2 then r2 = tot + 1 end
  if a[l1] < a[r1] then
    alltask[dst][1] = r1
    if a[l1] < a[r2] then
      alltask[dst][2] = r2
    else
      alltask[dst][2] = l1
    end
  else
    alltask[dst][1] = l1
    if a[l2] < a[r1] then
      alltask[dst][2] = r1
    else
      alltask[dst][2] = l2
    end
  end
end
local function bdp_doall(n)
  for stage = 1, n - 1 do
    local stlen = #stagetask[stage]
    for i_stagetask = 1, stlen do
      local used = stagetask[stage][i_stagetask]
      local mul = 1
      local tmp = used
      for j = 1, n do
        if tmp % 2 == 0 then
          bdp_walk(used, used + mul)
        else
          tmp = tmp - 1
        end
        tmp = mfl(tmp / 2)
        mul = mul * 2
      end
    end
  end
end

local n = io.read("*n")
bdp_prepare(n)
bdp_doall(n)
local prv = 0
for i = 1, tot - 1 do
  local cur = a[alltask[i][1]] + a[alltask[i][2]]
  if cur < prv then cur = prv end
  print(cur)
  prv = cur
end

Score : 700 points

Problem Statement

There is an integer sequence of length 2^N: A_0, A_1, ..., A_{2^N-1}. (Note that the sequence is 0-indexed.)

For every integer K satisfying 1 \leq K \leq 2^N-1, solve the following problem:

Let i and j be integers. Find the maximum value of A_i + A_j where 0 \leq i < j \leq 2^N-1 and (i or j) \leq K.
Here, or denotes the bitwise OR.

Constraints

1 \leq N \leq 18

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 ... A_{2^N-1}

Output

Print 2^N-1 lines.
In the i-th line, print the answer of the problem above for K=i.

Sample Input 1

2
1 2 3 1

Sample Output 1

3
4
5

For K=1, the only possible pair of i and j is (i,j)=(0,1), so the answer is A_0+A_1=1+2=3.

For K=2, the possible pairs of i and j are (i,j)=(0,1),(0,2).
When (i,j)=(0,2), A_i+A_j=1+3=4. This is the maximum value, so the answer is 4.

For K=3, the possible pairs of i and j are (i,j)=(0,1),(0,2),(0,3),(1,2),(1,3),(2,3) .
When (i,j)=(1,2), A_i+A_j=2+3=5. This is the maximum value, so the answer is 5.

Sample Input 2

3
10 71 84 33 6 47 23 25

Sample Output 2

81
94
155
155
155
155
155

Sample Input 3

4
75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32

Sample Output 3

101
120
147
156
156
178
194
194
194
194
194
194
194
194
194


v = 0

for o in string.gmatch(io.read(), "(%p)") do
  if(o == "+") then
    v = v + 1
  elseif(o == "-") then
    v = v - 1
  end
end

print(v)

Score : 100 points

Problem Statement

There is always an integer in Takahashi's mind.

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

Find the integer in Takahashi's mind after he eats all the symbols.

Constraints

The length of S is 4.

Each character in S is + or -.

Input

Input is given from Standard Input in the following format:

S

Output

Print the integer in Takahashi's mind after he eats all the symbols.

Sample Input 1

+-++

Sample Output 1

2

Initially, the integer in Takahashi's mind is 0.

The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.

The second integer to eat is -. After eating it, the integer in his mind becomes 0.

The third integer to eat is +. After eating it, the integer in his mind becomes 1.

The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

Sample Input 2

-+--

Sample Output 2

-2

Sample Input 3

----

Sample Output 3

-4


n = io.read("*n")
a, s = n, 0
while(0 < a) do
    s = s + a % 10
    a = math.floor(a / 10)
end
print(n % s == 0 and "Yes" or "No")

Score : 200 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(101) = 1 + 0 + 1 = 2.

Given an integer N, determine if S(N) divides N.

Constraints

1 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

If S(N) divides N, print Yes; if it does not, print No.

Sample Input 1

12

Sample Output 1

Yes

In this input, N=12.
As S(12) = 1 + 2 = 3, S(N) divides N.

Sample Input 2

101

Sample Output 2

No

As S(101) = 1 + 0 + 1 = 2, S(N) does not divide N.

Sample Input 3

999999999

Sample Output 3

Yes


N=io.read("n")
S=0
n=N
while(N>0) do
  S=S+N%10
  N=N//10
end
if n%S==0 then
  print("Yes")
  else
  print("No")
end


N=io.read("n")
K=io.read("n")
A={}
for i=1,N do
  A[i]=io.read("n")
end
table.sort(A)
minnub=1
for i=2,N do
  if A[i]==A[i-1] then
    minnub=minnub+1
    else
    break
  end
end
res=0
if (N-minnub)%(K-1)==0 then
  res=(N-minnub)//(K-1)
  else
  res=(N-minnub)//(K-1)+1
end
print(res)

Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


local function llprint(llnumber)
  local str = tostring(llnumber):gsub("LL", "")
  print(str)
end

local function snuke(k)
  local snuke = 0
  local tmp = k
  while 0 < tmp do
    local a = tmp % 10
    snuke = snuke + a
    tmp = (tmp - a) / 10
  end
  return k / snuke
end

local k = io.read("*n")
local dig = 1
local cnt = 0
local min = 1
while cnt < k do
  if dig == 1 then
    for i = 1, 9 do
      cnt = cnt + 1
      print(i)
      if cnt == k then break end
    end
  elseif dig <= 3 then
    -- candidate: 19,29,...,99, 199,299, ..., 999
    base = 0
    mul = 1
    for i = 2, dig do
      base = base * 10 + 9
      mul = mul * 10
    end
    for i = 1, 9 do
      v = base + mul * i
      snk = snuke(v)
      if min <= snk then
        cnt = cnt + 1
        min = snk
        print(v)
        if cnt == k then break end
      end
    end
  elseif dig <= 14 then
    -- candidate: 1z9..9, 2z9..9, ..., 9z9..9 z=[0-9]
    base = 0LL
    mul = 1LL
    for i = 3, dig do
      base = base * 10 + 9
      mul = mul * 10
    end
    for i = 10, 99 do
      v = base + mul * i
      snk = snuke(v)
      if min <= snk then
        cnt = cnt + 1
        min = snk
        llprint(v)
        if cnt == k then break end
      end
    end
  else
    -- candidate: 1xy9..9, 2xy9..9, ..., 9xy9..9 x,y=[0-9]
    base = 0LL
    mul = 1LL
    for i = 4, dig do
      base = base * 10 + 9
      mul = mul * 10
    end
    for i = 100, 999 do
      v = base + mul * i
      snk = snuke(v)
      if min <= snk then
        cnt = cnt + 1
        min = snk
        llprint(v)
        if cnt == k then break end
      end
    end
  end
  dig = dig + 1
  if dig == 16 then break end
end

Score : 500 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.

We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.

Given an integer K, list the K smallest Snuke numbers.

Constraints

1 \leq K

The K-th smallest Snuke number is not greater than 10^{15}.

Input

Input is given from Standard Input in the following format:

K

Output

Print K lines. The i-th line should contain the i-th smallest Snuke number.

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19


function snuke(k)
  local snuke = 0
  local tmp = k
  while(0 < tmp) do
    local a = tmp % 10
    snuke = snuke + a
    tmp = (tmp - a) / 10
  end
  return k / snuke
end

k = io.read("*n")
dig = 0
cnt = 0
min = 1
while(cnt <= k) do
  for i = 1, 9 do
    v = i
    for j = 1, dig do
      v = v * 10 + 9
    end
    snk = snuke(v)
    if(min <= snk) then
      cnt = cnt + 1
      min = snk
      print(v)
      if(cnt == k) then break end
    end
  end
  dig = dig + 1
end


local read = io.read
local min = math.min

local n, k = read("n", "n")
local a_t = {}
local min_num = math.maxinteger
for i = 1, n do
	local a_i = read("n")
	a_t[i] = a_i
	min_num = min(min_num, a_i)
end

local not_min_amount = 0
for i = 1, n do
	if a_t[i] ~= min_num then
		not_min_amount = not_min_amount + 1
	end
end

print(math.ceil(not_min_amount / (k - 1)))


local A, B = io.read("n","n")
print( (A <= 8 and B <= 8) and 'Yay!' or ':(' )

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


n=io.read("*n","*l")
counter=0
for i=1,n do
    x=io.read("*n")
    while x%2==0 do
        x=x//2
        counter=counter+1
    end
end
print(counter)

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.

Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.

At most how many operations can be performed?

Constraints

N is an integer between 1 and 10 \ 000 (inclusive).

a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.

Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.

Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.

Sample Input 2

4
631 577 243 199

Sample Output 2

0

No operation can be performed since all the elements are odd. Thus, the answer is 0.

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


a = io.read("*n")
print(a <= 999 and "ABC" or "ABD")

Score : 100 points

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?

In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.

You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Constraints

1 \leq N \leq 1998

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Sample Input 1

999

Sample Output 1

ABC

The 999-th round of AtCoder Beginner Contest is labeled as ABC999.

Sample Input 2

1000

Sample Output 2

ABD

The 1000-th round of AtCoder Beginner Contest is labeled as ABD001.

Sample Input 3

1481

Sample Output 3

ABD

The 1481-th round of AtCoder Beginner Contest is labeled as ABD482.


local a=io.read("n")
local b=io.read("n")
local s=0
for i=1,b-a do
  s=s+i
end
print(s-b)

Score : 200 points

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

Assume also that the depth of the snow cover is always at least 1 meter.

Constraints

1 \leq a < b < 499500(=1+2+3+...+999)

All values in input are integers.

There is no input that contradicts the assumption.

Input

Input is given from Standard Input in the following format:

a b

Output

If the depth of the snow cover is x meters, print x as an integer.

Sample Input 1

8 13

Sample Output 1

2

The heights of the two towers are 10 meters and 15 meters, respectively.
Thus, we can see that the depth of the snow cover is 2 meters.

Sample Input 2

54 65

Sample Output 2

1


local memo={}
local function rec(x)
    if x==0 then
        return 0
    end
    if memo[x]~=-1 then
        return memo[x]
    end
    local result=x
    local six=1
    while six<=x do
        result=math.min(result,rec(x-six)+1)
        six=six*6
    end
    local nine=1
    while nine<=x do
        result=math.min(result,rec(x-nine)+1)
        nine=nine*9
    end
    memo[x]=result
    return memo[x]
end

local n=io.read("n")
for i=1,n do
    memo[i]=-1
end
print(rec(n))

Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16


local tgt = io.read("*n")
tgt9 = math.floor(tgt / 9)
local first = true
local cnttot = 0
for i = 0, tgt9 do
    local tmp = i
    local cnt9, val9 = 0, 0
    local mul = 9
    while(0 < tmp) do
        local a = tmp % 9
        cnt9, val9 = cnt9 + a, val9 + mul * a
        mul, tmp = mul * 9, math.floor(tmp / 9)
    end
    local rem = tgt - val9
    local cnt6, rem6 = 0, rem
    while(0 < rem6)do
        local a = rem6 % 6
        cnt6, rem6 = cnt6 + a, math.floor(rem6 / 6)
    end
    if(first) then first, cnttot = false, cnt6 + cnt9 else cnttot = math.min(cnttot, cnt6 + cnt9) end
end
print(cnttot)


N=io.read("n")
t={}
for i=1,N do
  count=0
  k=i
  j=N-i
  while (k>0) do
    count=count+k%6
    k=k//6
  end
  while (j>0) do
    count=count+j%9
    j=j//9
  end
  t[i]=count
end
table.sort(t)
print(t[1])


local mmin, mfl, ior = math.min, math.floor, io.read
local n, c = ior("*n", "*n")
local d = {}
for i = 1, c * c do
  d[i] = ior("*n")
end
local diffsum = {}
for i = 1, 3 * c do
  diffsum[i] = 0
end
for pos = 1, n * n do
  local row, col = 1 + mfl((pos - 1) / n), pos % n
  if(col == 0) then col = n end
  local md = (row + col) % 3
  local a = ior("*n")
  for i_c = 1, c do
    local idx = md * c + i_c
    diffsum[idx] = diffsum[idx] + d[(a - 1) * c + i_c]
  end
end
local dfmin = diffsum[1] + diffsum[c + 2] + diffsum[2 * c + 3]
for i_1 = 1, c do
  for i_2 = 1, c do
    if(i_1 ~= i_2) then
      for i_3 = 1, c do
        if(i_1 ~= i_3 and i_2 ~= i_3) then
          dfmin = mmin(dfmin, diffsum[i_1] + diffsum[c + i_2] + diffsum[2 * c + i_3])
        end
      end
    end
  end
end
print(dfmin)

Score : 400 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:

If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.

If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.

Here, X \% Y represents X modulo Y.

We will repaint zero or more squares so that the grid will be a good grid.

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.

Find the minimum possible sum of the wrongness of all the squares.

Constraints

1 \leq N \leq 500

3 \leq C \leq 30

1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)

1 \leq c_{i,j} \leq C

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
D_{1,1} ... D_{1,C}
:
D_{C,1} ... D_{C,C}
c_{1,1} ... c_{1,N}
:
c_{N,1} ... c_{N,N}

Output

If the minimum possible sum of the wrongness of all the squares is x, print x.

Sample Input 1

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

Sample Output 1

3

Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1.

Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1.

Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1.

In this case, the sum of the wrongness of all the squares is 3.

Note that D_{i,j} \neq D_{j,i} is possible.

Sample Input 2

4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2

Sample Output 2

428


local n, c = io.read("*n", "*n")
local d = {}
for i = 1, c * c do
  d[i] = io.read("*n")
end
local diffsum = {}
for i = 1, 3 * c do
  diffsum[i] = 0
end
for pos = 1, n * n do
  local row, col = 1 + math.floor((pos - 1) / n), pos % n
  if(col == 0) then col = n end
  local md = (row + col) % 3
  local a = io.read("*n")
  for i_c = 1, c do
    local idx = md * c + i_c
    diffsum[idx] = diffsum[idx] + d[(a - 1) * c + i_c]
  end
end
local dfmin = diffsum[1] + diffsum[c + 2] + diffsum[2 * c + 3]
local mmin = math.min
for i_1 = 1, c do
  for i_2 = 1, c do
    if(i_1 ~= i_2) then
      for i_3 = 1, c do
        if(i_1 ~= i_3 and i_2 ~= i_3) then
          dfmin = mmin(dfmin, diffsum[i_1] + diffsum[c + i_2] + diffsum[2 * c + i_3])
        end
      end
    end
  end
end
print(dfmin)


local floor = math.floor
local min = math.min
local N = io.read("*n")

local function idiv(operand1, operand2)
	return floor(operand1 / operand2)
end

local function sumDigits(num)
	local sum = 0
	while num > 0 do
		sum = sum + num % 10
		num = idiv(num, 10)
	end
	return sum
end

local out = 100001
for A = 1, idiv(N, 2) do
	local B = N - A
	local sum = sumDigits(A) + sumDigits(B)
	out = min(out, sum)
end

print(out)

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N.
Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".

Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

Sample Input 2

100000

Sample Output 2

10


local mod = 998244353
local mfl = math.floor

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 = mfl(pow / 2)
  end
  return res
end

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

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

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

local ret = 0
local n, a, b, k = io.read("*n", "*n", "*n", "*n")
for ai = 0, n do
  local acomb = getComb(n, ai)
  local rem = k - ai * a
  if rem % b == 0 and rem // b <= n then
    local bi = rem // b
    local bcomb = getComb(n, bi)
    ret = badd(ret, bmul(acomb, bcomb))
  end
end
print(ret)

Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the beauty of the tower as follows:

The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.
How many such ways are there to paint the tower? Find the count modulo 998244353.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.

Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

The total number of the ways to produce them is 40.

Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


Score : 100 points

Problem Statement

You are given two integers A and B.
Find the largest value among A+B, A-B and A \times B.

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest value among A+B, A-B and A \times B.

Sample Input 1

3 1

Sample Output 1

4

3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.

Sample Input 2

4 -2

Sample Output 2

6

The largest is 4 - (-2) = 6.

Sample Input 3

0 0

Sample Output 3

0


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

t={}
for i=1,n do
    alphabet=s:sub(i,i)
    if not t[alphabet] then
        t[alphabet]={}
    end
    table.insert(t[alphabet],i)
end

max=0
for i=1,n do
    counter=0
    for alphabet,_ in pairs(t) do
        if #t[alphabet]>1 then
            if t[alphabet][1]<=i and i<=t[alphabet][#t[alphabet]] then
                counter=counter+1
            end
        end
    end
    max=math.max(counter,max)
end
print(max)

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


function qchong(s)
  local t={}
  for i=1,#s do
    t[i]=string.sub(s,i,i)
  end
  table.sort(t)
  local i=1
  while t[i] do
    if t[i]==t[i+1] then
      table.remove(t,i)
      i=i-1
    end
    i=i+1
  end
  return t
end

S=io.read()
res={}
for i=1,#S-1 do
  a=string.sub(S,1,i)
  b=string.sub(S,i+1,#S)
  a=qchong(a)
  b=qchong(b)
  local count=0
  for i=1,#a do
    for j=1,#b do
      if a[i]==b[j] then
        count=count+1
      end
    end
  end
  res[i]=count
end
table.sort(res,function (a,b) return a>b end)
print(res[1])


N=io.read()
S=io.read()
count=0
for i=2,#S do
  if string.sub(S,i,i)=="E" then
    count=count+1
  end
end
k=count
for i=2,#S do
  if string.sub(S,i-1,i-1)=="W" then
    k=k+1
  end
  if string.sub(S,i,i)=="E" then
    k=k-1
  end
  if k<count then
    count=k
  end
end
print(count)

Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


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

local w={}
local e={}
local west=0
local east=0

for i=1,n do
    if s:sub(n-i+1,n-i+1)=="E" then
        west=west+1
    end
    if s:sub(i,i)=="W" then
        east=east+1
    end
    w[n-i+1]=west
    e[i]=east
end

local counter=10^9
for i=1,n do
    counter=math.min(counter,w[i]+e[i]-1)
end
print(counter)


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

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local b = {}
local used = {}
for i = 1, n do
  a[i] = io.read("*n")
  b[i] = i
  used[i] = false
end
table.sort(b, function(x, y) return a[x] < a[y] end)

local ret = a[b[q]] - a[b[1]]
for i = 1, n do
  local topidx = b[i]
  used[topidx] = true
  local tmp = {}
  local cand = {}
  for j = 1, n do
    if used[j] then
      table.sort(tmp)
      for z = 1, #tmp - k + 1 do
        table.insert(cand, tmp[z])
      end
      tmp = {}
    else
      table.insert(tmp, a[j])
    end
  end
  table.sort(tmp)
  for z = 1, #tmp - k + 1 do
    table.insert(cand, tmp[z])
  end
  if #cand < q then break end
  table.sort(cand)
  ret = mmi(ret, cand[q] - cand[1])
end
print(ret)

Score : 600 points

Problem Statement

You are given an integer sequence A of length N and an integer K.
You will perform the following operation on this sequence Q times:

Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).

Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible.
Find the smallest possible value of X-Y when the Q operations are performed optimally.

Constraints

1 \leq N \leq 2000

1 \leq K \leq N

1 \leq Q \leq N-K+1

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1.
Thus, the first operation removes A_3=1 and now we have A=(4,3,5,2).
In the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2.
In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1.

Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184