a = io.read("*n")
print(math.ceil(a / 111) * 111)

Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


local N = io.read("n")
for i=1,9 do
    local x = i*100 + i*10 + i
    if x >= N then
        print(x)
        return
    end
end

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

Submitted source-code file size, in bytes.

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

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


N=io.read("n")
for i=1,9 do
  if 100*i+10*i+i>=N then
    print(100*i+10*i+i)
  end
end


n=io.read("*n","*l")
v={}
for i=1,n do
    x=io.read("*n")
    if i%2==1 then
        if not v[x] then
            v[x]={1,0}
        else
            v[x][1]=v[x][1]+1
        end
    else
        if not v[x] then
            v[x]={0,1}
        else
            v[x][2]=v[x][2]+1
        end
    end
end

omax={0,0}
emax={0,0}
omax2={0,0}
emax2={0,0}
for k,_ in pairs(v) do
    omax={math.max(omax[1],v[k][1]),k}
    emax={math.max(emax[1],v[k][2]),k}
end
for k,_ in pairs(v) do
    if k~=omax[2] then
        omax2={math.max(omax2[1],v[k][1]),k}
    elseif k~=emax[2] then
        emax2={math.max(emax2[1],v[k][2]),k}
    end
end

if omax[2]==emax[2] then
    print(n-math.max(omax2[1]+emax[1],omax[1]+emax2[1]))
else
    print(n-omax[1]-emax[1])
end

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


local S, T = io.read("l","l")
S={string.byte(S,1,#S)}
T={string.byte(T,1,#T)}
local origin = {}
local result = {}
for i=1,#S do
    if origin[T[i]] == nil then
        origin[T[i]] = S[i];
    end
    if result[S[i]] == nil then
        result[S[i]] = T[i];
    end
    if origin[T[i]] ~= S[i] or result[S[i]] ~= T[i] then
        print("No")
        return
    end
end
print("Yes")

Score : 300 points

Problem Statement

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

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


local S, T = io.read("l","l")
S={string.byte(S,1,#S)}
T={string.byte(T,1,#T)}
local source = {}
for i=1,#S do
    if source[T[i]] == nil then
        source[T[i]] = S[i];
    elseif source[T[i]] ~= S[i] then
        print("No")
        return
    end
end
print("Yes")


local a = io.read("*l")
local b = io.read("*l")
function test(a,b)
    local test = string.format("[%s]",a)
    local test2 = string.format("[%s]",b)
    local x = string.gsub(a,test2,"")
    local y = string.gsub(b,test,"")
    local newTest1 = {}
    local newTest2 = {}
    local num1 = 0
    local num2 = 0
    for i=1,string.len(x) do
        if not newTest1[string.sub(x,i,i)] then
            num1 = num1 + 1
            newTest1[string.sub(x,i,i)] = true
        end
    end

    for i=1,string.len(y) do
        if newTest2[string.sub(y,i,i)] then

        else
            num2 = num2 + 1
            newTest2[string.sub(y,i,i)] = true
        end

    end
    if math.abs(num1-num2)%2 == 0 then
        print("Yes")
    else
        print("No")
    end

end
test(a,b)


local mfl, mce = math.floor, math.ceil
local mod = 1000000007
local function badd(x, y) return (x + y) % mod end
local function bmul(x, y) return (x * y) % mod end

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end
local primes = getprimes(31623)

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}
for i = 2, 100100 do
  fact[i] = bmul(fact[i - 1], i)
end

local function getComb(n, k)
  return bmul(fact[n], modinv(bmul(fact[k], fact[n - k])))
end

local n, m = io.read("*n", "*n")
local tm = m
local ret = 1
for i = 1, #primes do
  local p = primes[i]
  if tm < p * p then break end
  local cnt = 0
  while tm % p == 0 do
    tm = tm // p
    cnt = cnt + 1
  end
  if 0 < cnt then
    ret = bmul(ret, getComb(n + cnt - 1, n - 1))
  end
end
if tm ~= 1 then
  ret = bmul(ret, n)
end
print(ret)

Score : 400 points

Problem Statement

You are given positive integers N and M.

How many sequences a of length N consisting of positive integers satisfy a_1 \times a_2 \times ... \times a_N = M? Find the count modulo 10^9+7.

Here, two sequences a' and a'' are considered different when there exists some i such that a_i' \neq a_i''.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of the sequences consisting of positive integers that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

2 6

Sample Output 1

4

Four sequences satisfy the condition: \{a_1, a_2\} = \{1, 6\}, \{2, 3\}, \{3, 2\} and \{6, 1\}.

Sample Input 2

3 12

Sample Output 2

18

Sample Input 3

100000 1000000000

Sample Output 3

957870001


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local mod = 1000000007
local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if(allnums[i]) then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end

local function getdivisorparts(x, primes)
  local prime_num = #primes
  local tmp = {}
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  while(primepos <= prime_num and dv <= lim) do
    if(x % dv == 0) then
      local asdf = {}
      asdf.p = dv
      asdf.cnt = 1
      x = x / dv
      while(x % dv == 0) do
        x = x / dv
        asdf.cnt = asdf.cnt + 1
      end
      table.insert(tmp, asdf)
      lim = mce(msq(x))
    end
    if(primepos == prime_num) then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if(x ~= 1) then
    local asdf = {}
    asdf.p, asdf.cnt = x, 1
    table.insert(tmp, asdf)
  end
  return tmp
end
local function bmul(x, y)
  local x1, y1 = x % 10000, y % 10000
  local x2, y2 = mfl(x / 10000) % 10000, mfl(y / 10000) % 10000
  local x3, y3 = mfl(x / 100000000), mfl(y / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret + x3 * y3 * 930000007) % mod
  return ret
end
local function bmul(x, y)
  local x1, y1 = x % 10000, y % 10000
  local x2, y2 = mfl(x / 10000) % 10000, mfl(y / 10000) % 10000
  local x3, y3 = mfl(x / 100000000), mfl(y / 100000000)
  local ret = (x1 * y1 + (x1 * y2 + x2 * y1) * 10000) % mod
  ret = (ret + (x1 * y3 + x2 * y2 + x3 * y1) * 10000 % mod * 10000 % mod) % mod
  ret = (ret + (x2 * y3 + x3 * y2) * 10000 % mod * 10000 % mod * 10000) % mod
  ret = (ret + x3 * y3 * 930000007) % mod
  return ret
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 function getConv(n, k)
  local ret = 1
  for i = 1, k do
    ret = bmul(ret, n + 1 - i)
    ret = bmul(ret, modinv(i))
  end
  return ret
end

local n, m = io.read("*n", "*n")
local primes = getprimes(mce(msq(m)))
local divisorparts = getdivisorparts(m, primes)
local pat = 1
for i = 1, #divisorparts do
  local c = divisorparts[i].cnt
  pat = bmul(pat, getConv(n + c - 1, c))
end
print(pat)


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

local sum = 0
for i = 1, N do
	a[i] = read("*n")
	sum = sum + a[i]
end
if sum == x then
	print(N)
	os.exit()
end
sum = nil

local out = 0
table.sort(a)
for i = 1, N do
	local pre_x = x - a[i]
	if pre_x < 0 then
		out = i - 1
		break
	elseif i == N then
		out = N - 1
		break
	end
	x = pre_x
end

print(out)

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.


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

for i=1,n-1 do
	for j=i+1 ,n do
		if a[i] > a[j] then
			a[i],a[j] = a[j],a[i]
		end
	end
end

for i=1,n do
	if sum + a[i] <= x then
		sum = sum + a[i]
		result = result + 1
	end
end

print(result)


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

local bit=require("bit")
local operation={}
for i=1,h do
    for j=1,w do
        if bit.band(a[i][j],1)>0 then
            if j<w then
                a[i][j+1]=a[i][j+1]+1
                a[i][j]=a[i][j]-1
                table.insert(operation,{i,j,i,j+1})
            elseif i<h then
                a[i+1][j]=a[i+1][j]+1
                a[i][j]=a[i][j]-1
                table.insert(operation,{i,j,i+1,j})
            end
        end
    end
end

local n=#operation
print(n)
for i=1,n do
    print(table.concat(operation[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


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


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")
s=0
for i=1,N do
  for j=1,N do
    for k=1,N do
      if (i+j)%K==0 and (i+k)%K==0 and (k+j)%K==0 then
        s=s+1
      end
    end
  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 l = io.read("*n")
local fact = {}
local c = 1
while c * 2 <= l do
  table.insert(fact, c)
  c = c * 2
end
local ret = {}
local n = #fact + 1
for i = 1, n - 1 do
  table.insert(ret, {i, i + 1, 0})
  table.insert(ret, {i, i + 1, fact[i]})
end
local rem = l - 1 - (2 * fact[#fact] - 1)
local offset = 2 * fact[#fact]
for i = #fact, 1, -1 do
  if fact[i] <= rem then
    table.insert(ret, {i, n, offset})
    rem = rem - fact[i]
    offset = offset + fact[i]
  end
end
print(n .. " " .. #ret)
for i = 1, #ret do
  print(ret[i][1] .. " " .. ret[i][2] .. " " .. ret[i][3])
end

Score : 700 points

Problem Statement

You are given an integer L. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.

The number of vertices, N, is at most 20. The vertices are given ID numbers from 1 to N.

The number of edges, M, is at most 60. Each edge has an integer length between 0 and 10^6 (inclusive).

Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.

There are exactly L different paths from Vertex 1 to Vertex N. The lengths of these paths are all different, and they are integers between 0 and L-1.

Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.

Constraints

2 \leq L \leq 10^6

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

In the first line, print N and M, the number of the vertices and edges in your graph.
In the i-th of the following M lines, print three integers u_i,v_i and w_i, representing the starting vertex, the ending vertex and the length of the i-th edge.
If there are multiple solutions, any of them will be accepted.

Sample Input 1

4

Sample Output 1

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

In the graph represented by the sample output, there are four paths from Vertex 1 to N=8:

1 → 2 → 3 → 4 → 8 with length 0

1 → 2 → 3 → 7 → 8 with length 1

1 → 2 → 6 → 7 → 8 with length 2

1 → 5 → 6 → 7 → 8 with length 3

There are other possible solutions.

Sample Input 2

5

Sample Output 2

5 7
1 2 0
2 3 1
3 4 0
4 5 0
2 4 0
1 3 3
3 5 1


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

local counter=0
for a=0,k-1 do
    local b=(k-a)%k
    local c=(k-a)%k
    if (b+c)%k==0 then
        counter=counter+t[a]*t[b]*t[c]
    end
end
print(counter)


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


n, k = io.read("*n", "*n")
local a = n // k
local sum = a * a * a
if(k % 2 == 0) then
  if(k * a + k // 2 <= n) then
    a = a + 1
  end
  sum = sum + a * a * a
end
print(sum)


local N, i = io.read("n", "n")
print(N-i+1)

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


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,k in ipairs(v) do
  io.write(k.." ")
end
print()
end

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 n, k = io.read("*n", "*n")
local left, right = {}, {}
for i = 1, n do
  local d = io.read("*n")
  if(d == 0) then k = k - 1
  elseif(0 < d) then table.insert(right, d)
  else table.insert(left, -d) end
end
table.sort(left)
table.sort(right)
local leftusemin = math.max(0, k - #right)
local leftusemax = math.min(k, #left)

local minlen = 0
for leftuse = leftusemin, leftusemax do
  local rightuse = k - leftuse
  local leftlen, rightlen = 0, 0
  if(0 < leftuse) then leftlen = left[leftuse] end
  if(0 < rightuse) then rightlen = right[rightuse] end
  local curlen = leftlen + rightlen + math.min(leftlen, rightlen)
  if(minlen == 0) then minlen = curlen else minlen = math.min(minlen, curlen) end
end
print(minlen)

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


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.


N = io.read("*n")

S = {105, 135, 165, 189, 195}

ans = 0

for v = 1, 5 do
  if N < S[v] then
    break
  else
    ans = ans + 1
  end
end

print(ans)

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 S = io.read()
local K = io.read("n")
local function a()
  local c
  for i=1,K do
    c = S:sub(i,i)
    if c ~= "1" then
      return c
    end
  end
  return c
end
print(a())

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


S=io.read()
K=io.read("n")
a=string.sub(S,1,1)
t=2
while(a=="1") do
  a=string.sub(S,t,t)
  t=t+1
end
b=tonumber(a)
if K<t-1 then
  print(1)
  else
  print(b)
end


s,k=io.read("*l","*n")
i=1
while s:sub(i,i)=="1" do
	i=i+1
end
if k<i then
	print(1)
	os.exit(0);
end
print(s:sub(i,i))


local s = io.read();
local k = tonumber(io.read());
local first = string.sub(s,1,1);

if k == 1 or string.len(s) == 1 then
    print(first);
elseif tonumber(first) == 1 then
    print(string.sub(s,2,2));
else
    print(string.sub(s,1,1));
end


S = tostring(io.read("*n"))
N = io.read("*n")

for v in string.gmatch(S, "%d") do
  if tonumber(v) > 1 or N <= 0 then
    print(v)
    break
  end
  N = N - 1
end


local n, k = io.read("*n", "*n")
print(n % k == 0 and 0 or 1)

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


N=io.read("n")
K=io.read("n")
if N%K~=0 then
  print(1)
  else
  print(0)
end


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

local max_7 = math.modf(N / 7)

local isYes = false
for i = 0, max_7 do
	local without_7 = N - 7 * i
	if without_7 % 4 == 0 then
		isYes = true
		break
	end
end

print(isYes and "Yes" or "No")

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")
if n % 4 == 0 then
  print("Yes")
elseif n % 4 == 1 then
  print(21 <= n and "Yes" or "No")
elseif n % 4 == 2 then
  print(14 <= n and "Yes" or "No")
else
  print(7 <= n and "Yes" or "No")
end


N = io.read("*n")

ans = "No"

for v = 0, 6 do
  t = N - 4*v
  if t >= 0 and t % 7 == 0 then
    ans = "Yes"
    break
  end
end

print(ans)


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

local function baseneg2_tostr(t)
    if #t == 0 then
        return "0"
    else
        local r = {}
        for i=#t,1,-1 do
            table.insert(r, t[i])
        end
        return table.concat(r, "")
    end
end

local function baseneg2_to_dec(t)
    if #t == 0 then
        return 0
    else
        local n = 0
        for i=0,#t-1 do
            n = n + t[i+1]*(-2)^i
        end
        return math.floor(n)
    end
end

local function dec_to_baseneg2(n)
    dbgpr("dec_to_baseneg2", n)
    local t = {}
    local i = 1
    while n ~= 0 do
        local b = math.floor((-2)^i)
        local bb = math.floor((-2)^(i-1))
        dbgpr("ibnbb", i, b, n, bb)
        local q, r = math.abs(n // b) - 1, nil
        while true do
            dbgpr("rem", n, q, b*q, b*-q, bb)
            if n == b * q then
                q = q
                r = 0
                break
            elseif n == b * q + bb then
                q = q
                r = 1
                break
            elseif n == b * -q then
                q = -q
                r = 0
                break
            elseif n == b * -q + bb then
                q = -q
                r = 1
                break
            end
            q = q + 1
        end
        local rr = r * bb
        dbgpr("r rr", r, rr)
        n = n - rr
        dbgpr("newn", n)
        table.insert(t, r)
        i = i + 1
    end
    return t
end

local function test(n)
    local t = dec_to_baseneg2(n)
    --print("res", n, baseneg2_tostr(t))
    assert(n == baseneg2_to_dec(t))
end

test(0)
test(1)
test(-2)
test(-1)
test(-9)
test(123456789)

print(baseneg2_tostr(dec_to_baseneg2(io.read("n"))))

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")
M=io.read("n")
c={}
for i=1,N do
  c[i]=io.read("n")
end
res=0
for i=1,N do
  k=0
  for j=i,N do
    k=k+c[j]
    if k%M==0 then
      res=res+1
    end
  end
end
print(res)

Score : 400 points

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.

You will take out the candies from some consecutive boxes and distribute them evenly to M children.

Such being the case, find the number of the pairs (l, r) that satisfy the following:

l and r are both integers and satisfy 1 \leq l \leq r \leq N.

A_l + A_{l+1} + ... + A_r is a multiple of M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the number of the pairs (l, r) that satisfy the conditions.

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

Sample Input 1

3 2
4 1 5

Sample Output 1

3

The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

13 17
29 7 5 7 9 51 7 13 8 55 42 9 81

Sample Output 2

6

Sample Input 3

10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

25


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

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


s = io.read()
n = #s
c = 0
r = s:sub(1, 1) == "A"
for i = 3, n - 1 do
  if s:sub(i, i) == "C" then
    c = c + 1
  end
end
if c ~= 1 then r = false end
for i = 2, n do
  ss = s:sub(i, i)
  if ss:byte() < 96 and ss ~= "C" then
    r = false
  end
end
print(r and "AC" or "WA")

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.


d, g = io.read("*n", "*n")
g = g / 100
psum = 0
p, c = {}, {}
for i = 1, d do
  p[i], c[i] = io.read("*n", "*n")
  c[i], psum = c[i] / 100, psum + p[i]
end
t = {}
currentmax = 0
ignoremax = psum + 1
for i = 0, psum do t[i] = 0 end

for i_d = 1, d do
  searchmax = currentmax + p[i_d]
  for summed_count = searchmax, 0, -1 do
    i_count_min = math.max(0, summed_count - currentmax)
    i_count_max = math.min(summed_count, p[i_d])
    for i_count = i_count_min, i_count_max do
      i_score = i_count * i_d
      if(i_count == p[i_d]) then i_score = i_score + c[i_d] end
      t[summed_count] = math.max(t[summed_count], t[summed_count - i_count] + i_score)
    end
  end
  currentmax = searchmax
end
for i = 1, psum do
  if(g <= t[i]) then print(i) break end
end

Score : 300 points

Problem Statement

A programming competition site AtCode provides algorithmic problems.
Each problem is allocated a score based on its difficulty.
Currently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.
These p_1 + … + p_D problems are all of the problems available on AtCode.

A user of AtCode has a value called total score.
The total score of a user is the sum of the following two elements:

Base score: the sum of the scores of all problems solved by the user.

Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D).

Takahashi, who is the new user of AtCode, has not solved any problem.
His objective is to have a total score of G or more points.
At least how many problems does he need to solve for this objective?

Constraints

1 ≤ D ≤ 10

1 ≤ p_i ≤ 100

100 ≤ c_i ≤ 10^6

100 ≤ G

All values in input are integers.

c_i and G are all multiples of 100.

It is possible to have a total score of G or more points.

Input

Input is given from Standard Input in the following format:

D G
p_1 c_1
:
p_D c_D

Output

Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).

Sample Input 1

2 700
3 500
5 800

Sample Output 1

3

In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.

One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.

Sample Input 2

2 2000
3 500
5 800

Sample Output 2

7

This case is similar to Sample Input 1, but the Takahashi's objective this time is 2000 points or more. In this case, we inevitably need to solve all five 200-point problems, and by solving two 100-point problems additionally we have the total score of 2000 points.

Sample Input 3

2 400
3 500
5 800

Sample Output 3

2

This case is again similar to Sample Input 1, but the Takahashi's objective this time is 400 points or more. In this case, we only need to solve two 200-point problems to achieve the objective.

Sample Input 4

5 25000
20 1000
40 1000
50 1000
30 1000
1 1000

Sample Output 4

66

There is only one 500-point problem, but the perfect bonus can be earned even in such a case.


t={}
for i=1,3 do
  t[i]=io.read("n")
end
table.sort(t)
print(t[3]-t[1])

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+)")
a, b, c = tonumber(a), tonumber(b), tonumber(c)

local arr = {
    a, b, c
}

local max = 0
local min = 0

for i = 1, 3 do
    if max == 0 then
        max = arr[i]
    elseif arr[i] > max then
        max = arr[i]
    end

    if min == 0 then
        min = arr[i]
    elseif arr[i] < min then
        min = arr[i]
    end
end

local output = max - min

print(tostring(output))


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

local x = string.len(a)
local sum = 0

for i = 1, math.huge do

    if a == b then
        print("Yes")
        break
    else
        a = string.sub(a, -1) .. string.sub(a, 1, -2)
    end
    sum = sum + 1
    if sum == x then
        print("No")
        break
    end

end

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


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

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


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={}
for bit=0,(1<<n)-1 do
    local r1=""
    local r2=""
    local b1=""
    local b2=""
    for i=0,n-1 do
        if bit&(1<<i)>0 then
            local r=i+1
            local b=i+1+n
            r1=r1.." "..s:byte(r)
            b2=s:byte(b).." "..b2
        else
            local r=i+1
            local b=i+1+n
            b1=s:byte(b).." "..b1
            r2=r2.." "..s:byte(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.


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={}
for bit=0,(1<<n)-1 do
    local r1=""
    local r2=""
    local b1=""
    local b2=""
    for i=0,n-1 do
        if bit&(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)


local mfl = math.floor
local n = io.read("*n", "*l")
local s = io.read()
local tot = 1
for i = 1, n do
  tot = tot * 2
end
local first_str_by_ruse = {}
for i = 0, n do
  first_str_by_ruse[i] = {}
end
for i = 0, tot - 1 do
  local left = 1
  local right = n
  local t = {}
  for j = 1, n do
    t[j] = 0
  end
  local ruse = 0
  for j = 1, n do
    if i % 2 == 0 then
      t[left] = s:sub(j, j)
      ruse = ruse + 1
      left = left + 1
    else
      t[right] = s:sub(j, j)
      right = right - 1
    end
    i = mfl(i / 2)
  end
  local str = table.concat(t)
  if first_str_by_ruse[ruse][str] then
    first_str_by_ruse[ruse][str] = first_str_by_ruse[ruse][str] + 1
  else
    first_str_by_ruse[ruse][str] = 1
  end
end
local ret = 0
for i = 0, tot - 1 do
  local left = 1
  local right = n
  local t = {}
  for j = 1, n do
    t[j] = 0
  end
  local ruse = 0
  for j = n, 1, -1 do
    if i % 2 == 0 then
      t[left] = s:sub(j + n, j + n)
      ruse = ruse + 1
      left = left + 1
    else
      t[right] = s:sub(j + n, j + n)
      right = right - 1
    end
    i = mfl(i / 2)
  end
  local str = table.concat(t)
  if first_str_by_ruse[ruse][str] then
    ret = ret + first_str_by_ruse[ruse][str]
  end
end
print(ret)


a, b = io.read("*n", "*n")
if a + b == 15 then print("+")
elseif a * b == 15 then print("*")
else print("x")
end

Score : 100 points

Problem Statement

You are given two integers a and b.
Determine if a+b=15 or a\times b=15 or neither holds.

Note that a+b=15 and a\times b=15 do not hold at the same time.

Constraints

1 \leq a,b \leq 15

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If a+b=15, print +;
if a\times b=15, print *;
if neither holds, print x.

Sample Input 1

4 11

Sample Output 1

+

4+11=15.

Sample Input 2

3 5

Sample Output 2

*

3\times 5=15.

Sample Input 3

1 1

Sample Output 3

x

1+1=2 and 1\times 1=1, neither of which is 15.


local n, m, d = io.read("*n", "*n", "*n")
local mul = 1 / n
if d ~= 0 then
  mul = 2 * (n - d) / n / n
end
print(string.format("%.10f", mul * (m - 1)))

Score : 300 points

Problem Statement

Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d.
For example, when d=1, the beauty of the sequence (3, 2, 3, 10, 9) is 3.

There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusive).
Find the beauty of each of these n^m sequences, and print the average of those values.

Constraints

0 \leq d < n \leq 10^9

2 \leq m \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m d

Output

Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n.
The output will be judged correct if the absolute or relative error is at most 10^{-6}.

Sample Input 1

2 3 1

Sample Output 1

1.0000000000

The beauty of (1,1,1) is 0.

The beauty of (1,1,2) is 1.

The beauty of (1,2,1) is 2.

The beauty of (1,2,2) is 1.

The beauty of (2,1,1) is 1.

The beauty of (2,1,2) is 2.

The beauty of (2,2,1) is 1.

The beauty of (2,2,2) is 0.

The answer is the average of these values: (0+1+2+1+1+2+1+0)/8=1.

Sample Input 2

1000000000 180707 0

Sample Output 2

0.0001807060


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

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

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

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

  while done < tasknum do
    local tmp = {}
    done = done + 1
    local src = tasks[done]
    taskstate[src] = false
    for dst, cost in pairs(edge[src]) do
      if len[dst] < 0 or len[src] + cost[ci] < len[dst] then
        len[dst] = len[src] + cost[ci]
        table.insert(tmp, dst)
      end
    end
    table.sort(tmp, function(a, b) return len[a] < len[b] end)
    for i = 1, #tmp do addtask(tmp[i]) end
  end
  return len
end

local yen = getlength(s, 1)
local snk = getlength(t, 2)
local ret = {}
for i = 1, n - 1 do ret[i] = 0 end
ret[n] = yen[n] + snk[n]
local mmi = math.min
for i = n - 1, 1, -1 do
  ret[i] = mmi(ret[i + 1], yen[i] + snk[i])
end
for i = 1, n do
  local a = 1000000000000000LL
  a = a - ret[i]
  local str = tostring(a):gsub("LL", "")
  print(str)
end

Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

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

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994


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

local n, m = io.read("*n", "*n")
local edge = {}
local updated = {}
local vmin, vmax = {}, {}
for i = 1, n do
  edge[i] = {}
  updated[i] = true
  vmin[i] = 1
  vmax[i] = 1000000000 - 1
end
updated[n + 1] = false
vmin[n + 1], vmax[n + 1] = 1, 1000000000 - 1
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c
  edge[b][a] = c
  vmax[a] = c - 1
  vmax[b] = c - 1
end
local function merge(a, b)
  if not updated[a] and not updated[b] then return n + 1
  elseif not updated[a] then return b
  elseif not updated[b] then return a end
  return vmax[a] - vmin[a] < vmax[b] - vmin[b] and a or b
end
local st = SegTree.new(n, merge, n + 1)
while true do
  local src = st:top()
  if n < src then break end
  updated[src] = false
  st:update(src)
  local srcmin, srcmax = vmin[src], vmax[src]
  for dst, sum in pairs(edge[src]) do
    local f = false
    if vmin[dst] < sum - srcmax then
      vmin[dst] = sum - srcmax
      f = true
    end
    if sum - srcmin < vmax[dst] then
      vmax[dst] = sum - srcmin
      f = true
    end
    if f then
      updated[dst] = true
      st:update(dst)
    end
  end
end
print(math.max(0, vmax[1] - vmin[1] + 1))

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


N=io.read("n")
if N%2==0 then
  print(N)
  else
  print(2*N)
end

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


n=io.read("*n")
if n%2==0 then print(n)
else print(n*2) end


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

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


N=io.read("n")
t={}
for i=1,N do
  t[i]=io.read("n")
end
table.sort(t)
print(t[#t]-t[1])


local n = tonumber(io.read());
local input = {}

for v in string.gmatch(io.read(), "(%d+)") do
    table.insert(input, tonumber(v));
end

table.sort(input, function(a, b)
    return a < b;
end)

local result = input[#input] - input[1];
print(result);


local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n") - i
end
table.sort(a)
local hn = (n + 1) // 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)

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


function median(list)
  table.sort(list)
  local center = math.floor(#list / 2)
  if #list % 2 == 0 then
    return (list[center] + list[center + 1]) / 2
  else
    return list[center + 1]
  end
end

function sad(A, b)
  local acc = 0
  for i, v in ipairs(A) do
    acc = acc + math.abs(v - (b + i))
  end
  return acc
end

local N = tonumber(io.read())
local A = {}
for n in io.read():gmatch('%d+') do
  table.insert(A, tonumber(n))
end

local B = {}
for i, v in ipairs(A) do
  table.insert(B, (v - i))
end
local result = sad(A, median(B))
io.write(result)


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

if n%2==1 then
    local median=a[n//2+1]
    local sadness=0
    for i=1,n do
        sadness=sadness+math.abs(a[i]-median)
    end
    print(sadness)
else
    local median1=a[n//2]
    local sadness1=0
    local median2=a[n//2+1]
    local sadness2=0
    for i=1,n do
        sadness1=sadness1+math.abs(a[i]-median1)
        sadness2=sadness2+math.abs(a[i]-median2)
    end
    print(math.min(sadness1,sadness2))
end


local function bdp_prepare(n)
  local tot = 1
  local alltask = {}
  local stagetask = {}
  for i = 1, n do
    tot = tot * 2
    stagetask[i] = {}
  end
  local a = {}
  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
  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 = ti / 2
    end
    table.insert(stagetask[cnt], i)
  end
  return tot, a, alltask, stagetask
end

local function bdp_walk(a, alltask, src, dst)
  local dst1 = alltask[dst][1]
  local dst2 = alltask[dst][2]
  local src1 = alltask[src][1]
  local src2 = alltask[src][2]
  local tmp = {dst1, dst2, src1, src2}
  table.sort(tmp, function(x, y) return a[x] > a[y] end)
  for i = 4, 2, -1 do
    if tmp[i] == tmp[i - 1] then
      table.remove(tmp, i)
    end
  end
  alltask[dst][1] = tmp[1]
  alltask[dst][2] = tmp[2]
end

local function bdp_doall(n, a, alltask, stagetask)
  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(a, alltask, used, used + mul)
        else
          tmp = tmp - 1
        end
        tmp = tmp / 2
        mul = mul * 2
      end
    end
  end
end

local n = io.read("*n")
local tot, a, alltask, stagetask = bdp_prepare(n)
bdp_doall(n, a, alltask, stagetask)
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


s = io.read()
r = -4
for i = 1, 4 do
    a = string.sub(s, i, i)
    if(a == "+") then
        r = r + 2
    end
end
print(r)

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


function S(n)
  local ret = 0
  while n ~= 0 do
    ret = ret + n % 10
    n = n // 10
  end
  return ret
end

N = io.read("*n")

if N % S(N) == 0 then
  print("Yes")
else
  print("No")
end

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


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


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 = 1
cnt = 0
min = 1
res = {}
while(cnt < k) do
  if(dig == 1) then
    for i = 1, 9 do
      cnt = cnt + 1
      print(i)
      table.insert(res, {i, 1})
      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)
        table.insert(res, {v, snk})
        if(cnt == k) then break end
      end
    end
  elseif(dig <= 14) then
    -- candidate: 1z9..9, 2z9..9, ..., 9z9..9 z=[0-9]
    base = 0
    mul = 1
    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
        print(v)
        table.insert(res, {v, snk})
        if(cnt == k) then break end
      end
    end
  else
    -- candidate: 1xy9..9, 2xy9..9, ..., 9xy9..9 x,y=[0-9]
    base = 0
    mul = 1
    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
        print(v)
        table.insert(res, {v, snk})
        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


v = io.read("*n")

n = 0
a = 1
for i=1, v do
  if i >= a*10 then
    a = a*10
  end
  n = n+a
  print(n)
end


v = io.read("*n")

for i=1, v do
  if i%9 ~= 0 then
    io.write(i%9)
  end
  for j=1, i//9 do
    io.write(9)
  end
  io.write("\n")
end


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.


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

if a <= 8 and b <= 8 then
  print("Yay!")
else
  print(":(")
end


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

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


N=io.read("n")
n={}
s=0
for i=1,N do
  n[i]=io.read("n")
  while n[i]%2==0 do
    n[i]=n[i]//2
    s=s+1
  end
end
print(s)


io.read();
local count = 0;

for v in string.gmatch(io.read(), "(%d+)") do
  local l = tonumber(v);

  while l & 1 == 0 do
    count = count + 1;
    l = l >> 1;
  end
end

print(count);


local n, m = io.read("*n", "*n")
local t = {}
for i = 1, 8 do
  t[i] = {}
end
for i = 1, n do
  local a, b, c = io.read("*n", "*n", "*n")
  t[1][i] = a + b + c
  t[2][i] = a + b - c
  t[3][i] = a - b + c
  t[4][i] = a - b - c
  t[5][i] = -a + b + c
  t[6][i] = -a + b - c
  t[7][i] = -a - b + c
  t[8][i] = -a - b - c
end
local ret = 0
for i = 1, 8 do
  local tbl = t[i]
  table.sort(tbl, function(x, y) return x > y end)
  local z = 0
  for j = 1, m do
    z = z + tbl[j]
  end
  if i == 1 or ret < z then
    ret = z
  end
end
print(ret)

Score: 400 points

Problem Statement

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

The shop sells N kinds of cakes.

Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.

These values may be zero or negative.

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

Do not have two or more pieces of the same kind of cake.

Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Constraints

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

M is an integer between 0 and N (inclusive).

x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 z_1
x_2 y_2 z_2
: :
x_N y_N z_N

Output

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Sample Input 1

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

Sample Output 1

56

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 3 + 9 = 13

Tastiness: 5 + 5 + 7 = 17

Popularity: 9 + 8 + 9 = 26

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

Sample Input 2

5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15

Sample Output 2

54

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 7 + 13 = 21

Tastiness: (-2) + (-8) + (-14) = -24

Popularity: 3 + (-9) + 15 = 9

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

Sample Input 3

10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82

Sample Output 3

638

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

Sample Input 4

3 2
2000000000 -9000000000 4000000000
7000000000 -5000000000 3000000000
6000000000 -1000000000 8000000000

Sample Output 4

30000000000

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.


N=io.read("n")
M=io.read("n")
x,y,z,t={},{},{},{}
for i=1,N do
  x[i]=io.read("n")
  y[i]=io.read("n")
  z[i]=io.read("n")
  t[i]=math.abs(x[i])+math.abs(y[i])+math.abs(z[i])
end
table.sort(t,function (a,b) return a>b end)
max=0
for i=1,M do
  max=max+t[i]
end
print(max)


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.


N=io.read("n")
if N>999 then
  print("ABD")
  else
  print("ABC")
end


n = tonumber(io.read())
if n < 1000 then
  print("ABC")
else
  print("ABD")
end


N=io.read("n")
t={}
for i=0,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+1]=count
end
table.sort(t)
print(t[1])

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


N=io.read("n")
t={}
for i=0,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])


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


N=io.read("n")
for i=1,N do
  local count=0
  local j=N-i
  while i>0 do
    count=count+i%6
    i=i//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 n = io.read("*n")
local hn = n // 2

local function get(x)
  local r = 0
  while 0 < x do
    r = r + x % 10
    x = x // 10
  end
  return r
end
local mmi = math.min
local ret = get(1) + get(n - 1)
for i = 2, hn do
  ret = mmi(ret, get(i) + get(n - i))
end
print(ret)

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 someTable = {}
for i=1,10 do
    someTable[10^i] = true
end

function suibian(a)
    local total = 0
    if someTable[a] then
        print(10)
        return
    end
    a = tostring(a)
    for i=1,string.len(a) do
        total = tonumber(string.sub(a,i,i))+total
    end
    print(total)
end
local n = io.read("*n")
suibian(n)


n = io.read("*n")
if n % 10 == 0 then
	print(10)
else
	sum = 0
	while n > 0 do
		sum = sum + n % 10
		n = math.floor(n / 10)
	end
	print(sum)
end


a, b = io.read(): match("(.+)%s(.+)")
print(string.format("%d", math.max(math.max(a + b, a - b), a * b)))

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


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


local n = io.read("*n")
local t = {}
for i = 1, n do t[i] = i end
for i = 1, n do
  local a = io.read("*n")
  t[a] = i
end
local f = {}
for i = 1, n - 1 do
  f[i] = t[i] < t[i + 1]
end
f[n] = false
local len = 0
local mma = math.max
local mlen = 0
for i = 1, n do
  if f[i] then
    len = len + 1
  else
    mlen = mma(mlen, len)
    len = 0
  end
end
print(n - 1 - mlen)

Score : 500 points

Problem Statement

You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N.
You would like to sort this sequence in ascending order by repeating the following operation:

Choose an element in the sequence and move it to the beginning or the end of the sequence.

Find the minimum number of operations required.
It can be proved that it is actually possible to sort the sequence using this operation.

Constraints

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

(P_1,P_2,...,P_N) is a permutation of (1,2,...,N).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1
:
P_N

Output

Print the minimum number of operations required.

Sample Input 1

4
1
3
2
4

Sample Output 1

2

For example, the sequence can be sorted in ascending order as follows:

Move 2 to the beginning. The sequence is now (2,1,3,4).

Move 1 to the beginning. The sequence is now (1,2,3,4).

Sample Input 2

6
3
2
5
1
4
6

Sample Output 2

4

Sample Input 3

8
6
3
1
2
7
4
8
5

Sample Output 3

5


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

for i=1,N do
    local n = P[i]
    sort_count[n] = sort_count[n-1] + 1
end

local max = 0
for i=1,N do
    if sort_count[i] > max then
        max = sort_count[i]
    end
end

print(N-max)


a,b,c,d=io.read():match("(.+)%s(.+)%s(.+)%s(.+)")
print(math.abs(a-c)>d*1 and (math.abs(a-b)>d*1 or math.abs(b-c)>d*1)and"No"or"Yes")

Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


a,b,c,d=io.read("n","n","n","n")
if math.abs(c-a)<=d or math.abs(b-a)<=d and math.abs(c-b)<=d then
  print("Yes")
  else
  print("No")
end


local par={}
local size={}

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

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

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

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

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

----------

local n,m=io.read("n","n")
init(n)

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

for i=1,m do
    local x,y=io.read("n","n")
    unite(x,y)
end

local counter=0
for i=1,n do
    if same(p[i],i) then
        counter=counter+1
    end
end
print(counter)

Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


local par={}
local rank={}
local size={}

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

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

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

local function unite(x,y)
    x=root(x)
    y=root(y)
    if x==y then
        return
    end
    if rank[x]>rank[y] then
        par[x]=y
        size[y]=size[y]+size[x]
    else
        par[x]=y
        size[y]=size[y]+size[x]
        if rank[x]==rank[y] then
            rank[y]=rank[y]+1
        end
    end
end

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

----------

local n,m=io.read("n","n")
init(n)

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

for i=1,m do
    local x,y=io.read("n","n")
    unite(x,y)
end

local counter=0
for i=1,n do
    if same(p[i],i) then
        counter=counter+1
    end
end
print(counter)


local s=io.read()
local n=#s
local k=io.read("n")
local t1={}
for i=1,k do
    for j=1,n-i+1 do
        local sub=s:sub(j,j+i-1)
        table.insert(t1,sub)
    end
end
table.sort(t1)

local t2={t1[1]}
for i=1,#t1-1 do
    if t1[i]~=t1[i+1] then
        table.insert(t2,t1[i+1])
    end
end
print(t2[k])

Score : 300 points

Problem Statement

You are given a string s.
Among the different substrings of s, print the K-th lexicographically smallest one.

A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.
Also, we say that substrings are different when they are different as strings.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


local n, m = io.read("*n", "*n","*l")
local str = io.read()
local posval = {}
for strnum in string.gmatch(str, "%d+") do
  table.insert(posval, tonumber(strnum))
end

local parent = {}
for i = 1, n do parent[i] = 0 end
function findroot(x)
  local cand = x
  while(parent[cand] ~= 0) do
    cand = parent[cand]
  end
  return cand
end

for i = 1, m do
  local a, b = io.read("*n", "*n")
  local aroot, broot = findroot(a), findroot(b)
  if(aroot ~= a) then parent[a] = aroot end
  if(broot ~= b) then parent[b] = broot end
  if(aroot ~= broot) then
    parent[broot] = aroot
  end
end
local ret = 0
for i = 1, n do
  local gsrc, gdst = findroot(i), findroot(posval[i])
  if(gsrc ~= 0 and gsrc == gdst) then ret = ret + 1 end
end
print(ret)