S=io.read()
T=io.read()
s={}
t={}
for i=1,#S do
  s[i]=string.sub(S,i,i)
  t[i]=string.sub(T,i,i)
end
out="Yes"
for i=1,#S-1 do
  for j=i+1,#S do
    if s[i]==s[j] and t[i]~=t[j] then
      out="No"
      break
    end
  end
end
print(out)

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 = {}
local t = {}
local check = {}

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

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

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

print(result)

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.


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

local counter = 0
for i = 1, N do
    if x - A[i] >= 0 then
        if i ~= N then
            x = x - A[i]
            counter = counter + 1
        elseif x - A[i] == 0 then
            counter = counter + 1
        end
    end
end
print(counter)

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.


local n, x = io.read("*n", "*n")
local a = {}
for i = 1, n do a[i] = io.read("*n") end
table.sort(a)
local cnt = 0
for i = 1, n do
  if i == n then
    if a[i] == x then
      cnt = cnt + 1
    end
  else
    if a[i] <= x then
      cnt = cnt + 1
      x = x - a[i]
    else
      break
    end
  end
end
print(cnt)


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 a[i][j]%2==1 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


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 operation={}
for i=1,h do
    for j=1,w do
        if 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


local n=io.read("n")
even=n//2
odd=n-even
print(odd*even)

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


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

Score : 300 points

Problem Statement

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

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

3 2

Sample Output 1

9

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

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


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


local n,k=io.read("n","n")
local t={}
for i=0,k-1 do
    t[i]=0
end
for i=1,n do
    t[i%k]=t[i%k]+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,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)


Score : 100 points

Problem Statement

There is an N-car train.

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

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

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

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


local function range(b, e)
  if not e then
    e = b
    b = 1
  end
  local t = {}
  for i=1,e-b+1 do
    t[i] = b+i-1
  end
  return t
end

local function all(t, f)
  for k,v in pairs(t) do
    if not f(v) then
      return false
    end
  end
  return true
end

local function map(t, f)
  local nt = {}
  for k,v in pairs(t) do
    nt[k] = f(v)
  end
  return nt
end

local N = {}
N[1], N[2] = io.read("n", "n", "l")
local A = {}
for h=1,N[1] do
  A[h] = {}
  for w=1,N[2] do
    local a = io.read(1)
    A[h][w] = a
  end
  io.read("l")
end
local function a(p, q, s)
  if s == 1 then
    return A[p][q]
  else
    return A[q][p]
  end
end

local range = {range(N[1]), range(N[2])}
local function create_R(x)
  local y = x % 2 + 1
  local function filter(p)
    local function check(q)
      return a(p,q,x) == '.'
    end
    return all(range[y], check)
  end
  return map(range[x], filter)
end
local R = {}

R[1] = create_R(1)
R[2] = create_R(2)

for h=1,N[1] do
  if not R[1][h] then
    for w=1,N[2] do
      if not R[2][w] then
        io.write(a(h,w,1))
      end
    end
    print()
  end
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 function range(b, e)
  local t = {}
  for i=1,e-b+1 do
    t[i] = b+i-1
  end
  return t
end
local function all(t, f)
  for k,v in pairs(t) do
    if not f(v) then
      return false
    end
  end
  return true
end

local function map(t, f)
  local nt = {}
  for k,v in pairs(t) do
    nt[k] = f(v)
  end
  return nt
end

local H, W = io.read("n", "n", "l")
local A = {}
for h=1,H do
  A[h] = {}
  for w=1,W do
    local a = io.read(1)
    A[h][w] = a
  end
  io.read("l")
end

local rangeh = range(1,H)
local rangew = range(1,W)

RH = map(rangeh, function(h) return all(rangew, function(w) return A[h][w]=='.' end) end)
RW = map(rangew, function(w) return all(rangeh, function(h) return A[h][w]=='.' end) end)

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


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


local mmi, mma = math.min, math.max
local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local ret = 1000000007
for left = 1, n - k + 1 do
  local right = left + k - 1
  if 0 <= t[left] then
    ret = mmi(ret, t[right])
  elseif t[right] <= 0 then
    ret = mmi(ret, -t[left])
  else
    if -t[left] < t[right] then
      ret = mmi(ret, t[right] - 2 * t[left])
    else
      ret = mmi(ret, 2 * t[right] - t[left])
    end
  end
end
print(ret)

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.


local a, b = string.match(io.read(), "(%d+) (%d+)");
a, b = tonumber(n), tonumber(m);

print((a - 1) * (b - 1))


local n = tonumber(io.read());

if n < 105 then
    print(0);
elseif n < 135 then
    print(1);
elseif n < 165 then
    print(2);
elseif n < 189 then
    print(3);
elseif n < 195 then
    print(4);
else
    print(5);
end

Score: 200 points

Problem Statement

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

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

Sample Input 2

7

Sample Output 2

0

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


local n = tonumber(io.read());

if n < 105 then
    print(0);
elseif n < 145 then
    print(1);
elseif n < 165 then
    print(2);
elseif n < 175 then
    print(3);
elseif n < 189 then
    print(4);
elseif n < 195 then
    print(5);
else
    print(6);
end


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
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 = tostring(io.read("*n"))
N = io.read("*n")

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


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] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.inc = function(self, idx)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m, q = io.read("*n", "*n", "*n")
local t = {}
for i = 1, m do
  local l, r = io.read("*n", "*n")
  t[i] = {l, r}
end
table.sort(t, function(x, y) return x[2] < y[2] end)
local st = SegTree.new(t[m][2], function(x, y) return x + y end, 0)
t[m + 1] = {1000000007, 1000000007}
local tq = {}
for i = 1, q do
  local ql, qr = io.read("*n", "*n")
  tq[i] = {i, ql, qr}
end
table.sort(tq, function(x, y) return x[3] < y[3] end)

local cpos = 1
for i = 1, q do
  local l, r = tq[i][2], tq[i][3]
  while t[cpos][2] <= r do
    st:inc(t[cpos][1])
    cpos = cpos + 1
  end
  tq[i][4] = st:getRange(l, r)
end

table.sort(tq, function(x, y) return x[1] < y[1] end)
for i = 1, q do
  print(tq[i][4])
end

Score: 400 points

Problem Statement

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

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

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

Constraints

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

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

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

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

Sample Input 2

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

Sample Output 2

1
1

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

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


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

sum={}
for i=0,n do
	sum[i]={}
	for j=0,n do
		sum[i][j]=0
	end
end

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

for i=0,n do
	for j=1,n do
		sum[i][j]=sum[i][j-1]+sum[i][j]
	end
end

for i=0,n do
	for j=1,n do
		sum[j][i]=sum[j-1][i]+sum[j][i]
	end
end

for i=1,q do
	l,r=io.read("*n","*n")
	print(sum[r][r]-sum[l-1][r]-sum[r][l-1]+sum[l-1][l-1])
end


N, M, Q = io.read("*n","*n","*n")

L = {}
R = {}
for v = 1, M do
  table.insert(L, io.read("*n"))
  table.insert(R, io.read("*n"))
end

for v = 1, Q do
  p, q = io.read("*n","*n")
  ans = 0

  for w = 1, M do
    if p <= L[w] and q >= R[w] then ans = ans + 1 end
  end

  print(ans)
end


Score : 100 points

Problem Statement

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

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

7 3

Sample Output 1

1

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

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


local N = io.read("n")
for i=0,100/7 do
  local r = N - i * 7
  if r < 0 then
      break
  end
  if r % 4 == 0 then
      print("Yes")
      return
  end
end
print("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")
for i=1,100/7 do
  local r = N - i * 7
  if r % 4 == 0 then
      print("Yes")
      return
  end
end
print("No")


N=io.read("n")
res="No"
for i=0,N//4 do
  for j=0,N//7 do
    if N==i*4+j*7 then
      res="Yes"
      break
    end
  end
  if res=="Yes" then
    break
  end
end
print(res)


N = io.read("*n")

N = -N
nb = {}
i = 1
while N ~= 0 do
  nb[i] = N % 2
  N = N // -2
  i = i + 1
end

for v = #nb, 1, -1 do
  io.write(nb[v])
end
print()

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


local n = tonumber(io.read());
local result = "";
n = -n;

repeat
    if n & 1 == 0 then
        result = "0" .. result;
    else
        result = "1" .. result;
    end
    n = n // -2;
until n == 0

print(result)


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

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

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

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

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

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

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

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

Sample Input 1

1199

Sample Output 1

ABC

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

Sample Input 2

1200

Sample Output 2

ARC

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

Sample Input 3

4208

Sample Output 3

AGC


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


S=io.read()
s=string.lower(S)

if S:sub(1,1)=="A" then
    Counter=0
    for i=2, #S do
        if S:sub(i,i)=="C" and 3<=i and i<=#S-1 then
            Counter=Counter+1
        end
    end
    if Counter==1 then
        checker=0
        for i=1, #S do
            if S:sub(i,i)~=s:sub(i,i) then
                checker=checker+1
            end
        end
        if checker==2 then
            print("AC")
            return
        end
    end
end
print("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.


function isLower(c)
	return string.lower(c) == c
end
function solve(s)
	if string.sub(s, 1, 1) ~= "A" then
		return false
	end
	if not isLower(string.sub(s, 2, 2)) or not isLower(string.sub(s, #s, #s)) then
		return false
	end
	local num = 0
	for i = 3, #s - 1 do
		local c = string.sub(s, i, i)
		if not isLower(c) then
			if num == 0 and c == "C" then
				num = 1
			else
				return false
			end
		end
	end
	if num == 0 then
		return false
	end
	return true
end
local s = io.read()
if solve(s) then
	print("AC")
else
	print("WA")
end


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


S = io.read()
m = string.match(S, "A%l%l*C%l*%l")
if m and string.len(m) == string.len(S) then
  print("AC")
else
  print("WA")
end


t={}
for i in io.read():gmatch("%d+")do
  table.insert(t,math.floor(i*1))
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


a = {}

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

table.sort(a)

print(a[#a] - a[1])


local a, b = io.read("*l", "*l")
local ok = true
for j = 0, #a - 1 do
  ok = true
  for i = 1, #a do
    local bi = i + j
    if #a < bi then bi = bi - #a end
    if a:sub(i, i) ~= b:sub(bi, bi) then ok = false break end
  end
  if ok then break end
end
print(ok and "Yes" or "No")

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


S=io.read()
T=io.read()
res="No"
for i=1,#S do
  S=string.sub(S,#s,#s)..string.sub(S,1,#s-1)
  if S==T then
    res="Yes"
    break
  end
end
print(res)


local s = io.read();
local t = io.read();

local result = "Yes";
for k in string.gmatch(t,"%w") do
  if string.find(s, k) == nil then
    result = "No";
    break;
  end
end

print(result);


local n = io.read("*n", "*l")
local str = io.read()
local sum = 0
for v in str:gmatch("%d+") do
  sum = sum + tonumber(v)
end
print(sum - n)

Score : 300 points

Problem Statement

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

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

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

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

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

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


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


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

Score : 400 points

Problem Statement

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

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

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

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

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

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

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

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


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

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

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

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

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

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

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

Constraints

1 \leq T \leq 300

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

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

Output

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

Sample Input 1

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

Sample Output 1

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

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

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

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

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

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

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

Sample Output 2

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


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

Score : 200 points

Problem Statement

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

Constraints

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

S consists of lowercase English letters.

w is an integer.

Input

Input is given from Standard Input in the following format:

S
w

Output

Print the desired string in one line.

Sample Input 1

abcdefgh
3

Sample Output 1

adg

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

abc

def

gh

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

Sample Input 2

lllll
1

Sample Output 2

lllll

Sample Input 3

souuundhound
2

Sample Output 3

suudon


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

Score : 600 points

Problem Statement

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

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

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

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

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq u_i < v_i \leq n

2 \leq s_i \leq 10^9

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

The graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m
u_1 v_1 s_1
:
u_m v_m s_m

Output

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

Sample Input 1

3 3
1 2 3
2 3 5
1 3 4

Sample Output 1

1

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

Sample Input 2

4 3
1 2 6
2 3 7
3 4 5

Sample Output 2

3

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

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

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

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

Sample Input 3

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

Sample Output 3

0


n = io.read("*n")
if n % 2 == 0 then
  print(n)
else
  print(n * 2)
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


a=math.floor(io.read()*1)
print(a%2==0 and a or 2*a)


local n = tonumber(io.read());

if (n & 0x1) == 1 then
  print(n * 2);
else
  print(n);
end


n = io.read("*n")
t = {}
for i = 1, n do t[i] = io.read("*n") end
table.sort(t)
print(t[n] - t[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")-i
end
table.sort(t)
s=0
if N%2==0 then
  for i=1,N do
    s=s+math.abs(t[i]-t[N//2])
  end
  else
  for i=1,N do
    s=s+math.abs(t[i]-t[N//2+1])
  end
end
print(s)

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

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

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

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

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

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

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


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


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

io.write(sad(A, median(B)))


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


S=io.read()
res=0
for i=1,#S do
  if string.sub(S,i,i)=="+" then
    res=res+1
    elseif string.sub(S,i,i)=="-" then
    res=res-1
  end
end
print(res)


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

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

12

Sample Output 1

Yes

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

Sample Input 2

101

Sample Output 2

No

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

Sample Input 3

999999999

Sample Output 3

Yes


n, k = io.read("*n", "*n")
minpos = 1
min = io.read("*n")
for i = 2, n do
  local a = io.read("*n")
  if(a < min) then min, minpos = a, i end
end
-- find min(s) where minpos - (k-1) * s <= 1
s = math.ceil((minpos - 1) / (k - 1))
minpos = 1 + s * (k - 1)
-- find min(t) where n <= minpos + (k-1) * t
t = math.ceil((n - minpos) / (k - 1))
print(s + t)

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


n, k = io.read("*n", "*n")
minpos = 1
min = io.read("*n")
for i = 2, n do
  local a = io.read("*n")
  if(a < min) then min, minpos = a, i end
end
-- find min(s) where minpos - (k-1) * s <= 1
s = math.ceil((minpos - 1) / (k - 1))
-- find min(t) where n <= minpos + (k-1) * t
t = math.ceil((n - minpos) / (k - 1))
print(s + t)


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

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

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


-- この問題ってaに1からnまで絶対に存在するじゃん
local n, k = io.read("n", "n", "l", "l")
print(math.ceil((n - 1) / (k - 1)))


a,b=io.read("*n","*n")
print(a <= 8 and b <= 8 and "Yay!" or ":(")

Score: 100 points

Problem Statement

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

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

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

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

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

Constraints

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

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

5 4

Sample Output 1

Yay!

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

Sample Input 2

8 8

Sample Output 2

Yay!

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

Sample Input 3

11 4

Sample Output 3

:(

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


n = io.read("*n")
sum = 0
for i = 1, n do
    a = io.read("*n")
    while(0 < a) do
        if(a % 2 == 0) then sum = sum + 1 else break end
        a = math.floor(a / 2)
    end
end
print(sum)

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()
a = {}
for i=1,N do
  table.insert(a, tonumber(io.read("*n")))
end

result = 0

for i=1,N do
  while a[i]&0x01 == 0 do
    a[i] = a[i] / 2
    result = result + 1
  end
end

print(string.format("%d",result))


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

Score : 100 points

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

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

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

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

Constraints

1 \leq N \leq 1998

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

999

Sample Output 1

ABC

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

Sample Input 2

1000

Sample Output 2

ABD

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

Sample Input 3

1481

Sample Output 3

ABD

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


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


local L = {}
for i=1, 999 do
    L[i] = i * (i + 1) // 2
end
local a, b = io.read("n", "n")
for i=1, 998 do
    if b - a == L[i+1] - L[i] then
        print(L[i] - a)
        return
    end
end

Score : 200 points

Problem Statement

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

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

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

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

Constraints

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

All values in input are integers.

There is no input that contradicts the assumption.

Input

Input is given from Standard Input in the following format:

a b

Output

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

Sample Input 1

8 13

Sample Output 1

2

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

Sample Input 2

54 65

Sample Output 2

1


local n=io.read("n")
local result=n
for i=0,n do
    local counter=0
    local draw=i
    while draw>0 do
        counter=counter+draw%6
        draw=math.floor(draw/6)
    end
    local draw=n-i
    while draw>0 do
        counter=counter+draw%9
        draw=math.floor(draw/9)
    end
    if result>counter then
        result=counter
    end
end
print(result)

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=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])


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

Score : 400 points

Problem Statement

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

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

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

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

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

Here, X \% Y represents X modulo Y.

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

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

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

Constraints

1 \leq N \leq 500

3 \leq C \leq 30

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

3

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

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

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

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

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

Sample Input 2

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

Sample Output 2

428


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


local N = io.read("*n")
local count = 0
while(N > 0) do
local tmp = N % 10
N = math.floor(N / 10)
count = count + tmp
end
print(count)

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

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

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

15

Sample Output 1

6

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

Sample Input 2

100000

Sample Output 2

10


local mod = 998244353
local mfl = math.floor

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

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

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

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

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

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

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

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

Score : 700 points

Problem Statement

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

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

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

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

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

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

Sample Input 1

4 1 2 5

Sample Output 1

40

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

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

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

Sample Input 2

2 5 6 0

Sample Output 2

1

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

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


Score : 100 points

Problem Statement

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

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

3 1

Sample Output 1

4

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

Sample Input 2

4 -2

Sample Output 2

6

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

Sample Input 3

0 0

Sample Output 3

0


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

local function check(i, str)
  local left = str:sub(1, i)
  local right = str:sub(i + 1, #str)
  local t = {}
  for i = 1, i do
    t[left:sub(i, i)] = true
  end
  local c = 0
  for k, v in pairs(t) do
    if right:find(k) then
      c = c + 1
    end
  end
  return c
end

local cnt = 0
for i = 1, n - 1 do
  cnt = math.max(cnt, check(i, s))
end
print(cnt)

Score : 200 points

Problem Statement

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

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

6
aabbca

Sample Output 1

2

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

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

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

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


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


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

local counter=10^9
for i=1,n do
    local left=#s:sub(1,i-1):gsub("E","")
    local right=#s:sub(i+1):gsub("W","")
    counter=math.min(counter,left+right)
end
print(counter)

Score : 300 points

Problem Statement

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

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

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

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

5
WEEWW

Sample Output 1

1

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

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt 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.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    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.getValue = function(self, idx)
  return self.stage[self.stagenum][idx]
end

SegTree.right_bound = function(self, left, right)
  local retpos = left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    if not self.stage[stage][mce(l / sz)] then
      retpos = l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end

SegTree.left_bound = function(self, left, right)
  local retpos = right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    if not self.stage[stage][mfl(r / sz)] then
      retpos = r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end

SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local b = {}
for i = 1, n do
  a[i] = io.read("*n")
  b[i] = i
end
table.sort(b, function(x, y) return a[x] < a[y] end)
local st = SegTree.new(n + 1, function(x, y) return x or y end, false)
st:setValue(n + 1, true)

local cost = {}
local function updateCost()
  for i = 1, n do
    local rightlim = st:right_bound(i, n) - 1
    local leftlim = st:left_bound(1, i) + 1
    local cur_count = mma(0, (rightlim - leftlim + 1) - k + 1)
    local left_count = mma(0, (i - 1 - leftlim + 1) - k + 1)
    local right_count = mma(0, (rightlim - (i + 1) + 1) - k + 1)
    cost[i] = cur_count - left_count - right_count
  end
end

local ret = a[b[q]] - a[b[1]]
local available_count = n - k + 1
for i = 1, n do
  updateCost()
  local topidx = 0
  for j = 1, n do
    if st:getValue(j) == false then
      if topidx == 0 then
        topidx = j
      else
        if a[j] < a[topidx] then
          topidx = j
        elseif a[j] == a[topidx] and cost[j] < cost[topidx] then
          topidx = j
        end
      end
    end
  end
  local rightlim = st:right_bound(topidx, n) - 1
  local leftlim = st:left_bound(1, topidx) + 1
  local cur_count = mma(0, (rightlim - leftlim + 1) - k + 1)
  local left_count = mma(0, (topidx - 1 - leftlim + 1) - k + 1)
  local right_count = mma(0, (rightlim - (topidx + 1) + 1) - k + 1)
  available_count = available_count - cur_count + left_count + right_count
  if q <= available_count then
    ret = mmi(ret, a[b[q + i]] - a[b[i + 1]])
  else
    break
  end
end
print(ret)

Score : 600 points

Problem Statement

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

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

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

Constraints

1 \leq N \leq 2000

1 \leq K \leq N

1 \leq Q \leq N-K+1

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

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

Sample Input 2

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

Sample Output 2

7

Sample Input 3

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

Sample Output 3

451211184


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] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.setRange = function(self, left, right, value)
  for idx = left, right do
    self.stage[self.stagenum][idx] = value
  end
  for i = self.stagenum - 1, 1, -1 do
    left, right = mce(left / 2), mce(right / 2)
    for j = left, right do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local stage, l, r = 1, 1, self.size[1]
  while true do
    local sz = self.size[stage]
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if l + sz <= r then stage, l = stage + 1, l + sz
      else break
      end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break
      end
    end
  end
  return retpos + 1
end
SegTree.right_bound = function(self, val, left, right)
  local ret, retpos = 1, left - 1
  local t1, t2, t3 = {1}, {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]
    while (l - 1) % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp > val then
      ret, retpos = tmp, l + sz - 1
      if retpos == right then break end
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l + sz) table.insert(t3, r) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
    end
  end
  return retpos + 1
end
SegTree.left_bound = function(self, val, left, right)
  local ret, retpos = 1, right + 1
  local t1, t2, t3 = {1}, {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]
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mfl(r / sz)])
    if tmp > val then
      ret, retpos = tmp, r - sz + 1
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l) table.insert(t3, r - sz) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, r - sz + 2) table.insert(t3, r) end
    end
  end
  return retpos - 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local t = {}
local rank = {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = i
  rank[i] = 0
end
table.sort(t, function(x, y) return a[x] < a[y] end)
for i = 1, n do
  rank[t[i]] = i
end
-- availability (1 or 0) by array index
local stFlag = SegTree.new(n, function(x, y) return x * y end, 0)
for i = 1, n do
  stFlag:setValue(i, 1, true)
end
stFlag:updateAll()
-- available counts by sorted array index
local stCnt = SegTree.new(n, function(x, y) return x + y end, 0)
local omit = {}
for i = 1, n do
  stCnt:setValue(i, 1, true)
  omit[i] = false
end
stCnt:updateAll()
local ret = a[t[q]] - a[t[1]]

for i_sorted = 1, n do
  if stCnt:getRange(i_sorted, i_sorted) == 1 then
    stCnt:setValue(i_sorted, 0)
    local idx = t[i_sorted]
    stFlag:setValue(idx, 0)
    local rb = idx == n and n or stFlag:right_bound(0, idx + 1, n)
    if rb - (idx + 1) < k then
      if idx + 1 < rb then
        for j = idx + 1, rb - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    else
      local tmp = {}
      for j = idx + 1, rb - 1 do
        table.insert(tmp, rank[j])
      end
      table.sort(tmp)
      for j = 1, rb - idx - k do
        omit[tmp[j]] = false
      end
      for j = rb - idx - k + 1, #tmp do
        omit[tmp[j]] = true
      end
    end
    local lb = idx == 1 and 1 or stFlag:left_bound(0, 1, idx - 1)
    if idx - 1 - lb < k then
      if lb < idx - 1 then
        for j = lb + 1, idx - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    else
      local tmp = {}
      for j = lb + 1, idx - 1 do
        table.insert(tmp, rank[j])
      end
      table.sort(tmp)
      for j = 1, idx - lb - k do
        omit[tmp[j]] = false
      end
      for j = idx - lb - k + 1, #tmp do
        omit[tmp[j]] = true
      end
    end
    local p1, p2 = nil, nil
    local c = 0
    for j = 1, n do
      if stCnt:getRange(j, j) == 1 and not omit[j] then
        c = c + 1
        if c == 1 then p1 = j end
        if c == q then p2 = j break end
      end
    end
    if not p2 then
      break
    else
      ret = mmi(ret, a[t[p2]] - a[t[p1]])
    end
  end
end
print(ret)


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] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.setRange = function(self, left, right, value)
  for idx = left, right do
    self.stage[self.stagenum][idx] = value
  end
  for i = self.stagenum - 1, 1, -1 do
    left, right = mce(left / 2), mce(right / 2)
    for j = left, right do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local stage, l, r = 1, 1, self.size[1]
  while true do
    local sz = self.size[stage]
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if l + sz <= r then stage, l = stage + 1, l + sz
      else break
      end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break
      end
    end
  end
  return retpos + 1
end
SegTree.right_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, left - 1
  local t1, t2, t3 = {1}, {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]
    while (l - 1) % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp > val then
      ret, retpos = tmp, l + sz - 1
      if retpos == right then break end
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l + sz) table.insert(t3, r) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
    end
  end
  return retpos + 1
end
SegTree.left_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, right + 1
  local t1, t2, t3 = {1}, {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]
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = self.size[stage]
    end
    local tmp = self.func(ret, self.stage[stage][mfl(r / sz)])
    if tmp > val then
      ret, retpos = tmp, r - sz + 1
      if l + sz <= r then table.insert(t1, 1) table.insert(t2, l) table.insert(t3, r - sz) end
    else
      if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, r - sz + 2) table.insert(t3, r) end
    end
  end
  return retpos - 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local t = {}
local rank = {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = i
  rank[i] = 0
end
table.sort(t, function(x, y) return a[x] < a[y] end)
for i = 1, n do
  rank[t[i]] = i
end
-- availability (1 or 0) by array index
local stFlag = SegTree.new(n, function(x, y) return x * y end, 1)
for i = 1, n do
  stFlag:setValue(i, 1, true)
end
stFlag:updateAll()
-- available counts by sorted array index
local stCnt = SegTree.new(n, function(x, y) return x + y end, 0)
for i = 1, n do
  stCnt:setValue(i, 1, true)
end
stCnt:updateAll()
local ret = a[t[q]] - a[t[1]]

for i_sorted = 1, n do
  if stCnt:getRange(i_sorted, i_sorted) == 1 then
    stCnt:setValue(i_sorted, 0)
    local idx = t[i_sorted]
    stFlag:setValue(idx, 0)
    local rb = idx == n and n or stFlag:right_bound(0, idx + 1, n)
    if rb - (idx + 1) < k then
      if idx + 1 < rb then
        stFlag:setRange(idx + 1, rb - 1, 0)
        for j = idx + 1, rb - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    end
    local lb = idx == 1 and 1 or stFlag:left_bound(0, 1, idx - 1)
    if idx - 1 - lb < k then
      if lb < idx - 1 then
        stFlag:setRange(lb + 1, idx - 1, 0)
        for j = lb + 1, idx - 1 do
          stCnt:setValue(rank[j], 0)
        end
      end
    end
    if not removed then break end
    local p1, p2 = stCnt:lower_bound(1), stCnt:lower_bound(q)
    if n < p2 then
      break
    else
      ret = mmi(ret, a[t[p2]] - a[t[p1]])
    end
  end
end
print(ret)


a,b,c,k=io.read("*n","*n","*n","*n")
if k%2==0 then
    if a-b>=10^18 then
        print("Unfair")
    else
        print(a-b)
    end
else
    if b-a>=10^18 then
        print("Unfair")
    else
        print(b-a)
    end
end

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

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

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


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

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


x=io.read("*n")
maxex=0
for i=1,31 do
    for j=2,9 do
        if i^j<=x then
            maxex=i^j
        end
    end
end
print(maxex)

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


local x = io.read("*n")
local ret = 1
local mma = math.max
for a = 2, 32 do
  local t = a * a
  while(t <= x) do
    ret = mma(ret, t)
    t = t * a
  end
end
print(ret)


a,b=io.read():match("(.+)%s(.+)")
print(a+1>b*1 and math.floor(a-1)or a)

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.