local h,w,a,b=io.read("n","n","n","n")
for i=1,b do
    print(string.rep("0",a)..string.rep("1",w-a))
end
for i=1,h-b do
    print(string.rep("1",a)..string.rep("0",w-a))
end

Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s_{11}s_{12}\cdots s_{1W}
s_{21}s_{22}\cdots s_{2W}
\vdots
s_{H1}s_{H2}\cdots s_{HW}

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

If multiple solutions exist, printing any of them will be accepted.

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010


local n, k = io.read("*n","*n")
local t = {}
for i = 1, n do
  t[i] = 1 + io.read("*n")
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.setValue = function(self, idx, set, silent)
  if set then
    self.stage[self.stagenum][idx] = idx
  else
    self.stage[self.stagenum][idx] = self.emptyvalue
  end
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local mma, mmi = math.max, math.min
local stmin = SegTree.new(n, mmi, n + 1)
local stmax = SegTree.new(n, mma, 0)
for i = 1, k do
  stmin:setValue(t[i], true, true)
  stmax:setValue(t[i], true, true)
end
stmin:updateAll()
stmax:updateAll()

local sortedCnt = 0
local cnt = 1
local curlen = 1
for i = 1, k - 1 do
  if t[i] < t[i + 1] then
    curlen = curlen + 1
  else
    curlen = 1
  end
end
if k <= curlen then
  sortedCnt = sortedCnt + 1
end

for i = k + 1, n do
  local sorted = false
  if t[i - 1] < t[i] then
    curlen = curlen + 1
  else
    curlen = 1
  end
  if k <= curlen then
    sorted = true
  end

  local prv, nxt = t[i - k], t[i]
  local curmin, curmax = stmin.stage[1][1], stmax.stage[1][1]
  if curmin == prv and curmax < nxt then
  else
    if sorted then
      sortedCnt = sortedCnt + 1
    end
    cnt = cnt + 1
  end
  stmin:setValue(t[i - k], false)
  stmin:setValue(t[i], true)
  stmax:setValue(t[i - k], false)
  stmax:setValue(t[i], true)
end
if 1 < sortedCnt then
  cnt = cnt - sortedCnt + 1
end
print(cnt)

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

Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

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

Sample Output 3

6

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, k = io.read("*n","*n")
local t = {}
for i = 1, n do
  t[i] = 1 + io.read("*n")
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.setValue = function(self, idx, set, silent)
  if set then
    self.stage[self.stagenum][idx] = idx
  else
    self.stage[self.stagenum][idx] = self.emptyvalue
  end
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local mma, mmi = math.max, math.min
local stmin = SegTree.new(n, mmi, n + 1)
local stmax = SegTree.new(n, mma, 0)
for i = 1, k do
  stmin:setValue(t[i], true, true)
  stmax:setValue(t[i], true, true)
end
stmin:updateAll()
stmax:updateAll()
local cnt = 1
for i = k + 1, n do
  local prv, nxt = t[i - k], t[i]
  local curmin, curmax = stmin.stage[1][1], stmax.stage[1][1]
  if curmin == prv and curmax < nxt then
  else
    cnt = cnt + 1
  end
  stmin:setValue(t[i - k], false)
  stmin:setValue(t[i], true)
  stmax:setValue(t[i - k], false)
  stmax:setValue(t[i], true)
end
local sortedCnt = 0
local curlen = 1
for i = 1, n - 1 do
  if t[i] < t[i + 1] then
    curlen = curlen + 1
    if k <= curlen then
      sortedCnt = sortedCnt + 1
    end
  else
    curlen = 1
  end
end
if 1 < sortedCnt then
  cnt = cnt - (sortedCnt - 1)
end
print(cnt)


local s = io.read()
if s == "Sunny" then
  print("Cloudy")
elseif s == "Cloudy" then
  print("Rainy")
else
  print("Sunny")
end

Score: 100 points

Problem Statement

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

Constraints

S is Sunny, Cloudy, or Rainy.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string representing the expected weather tomorrow, in the same format in which input is given.

Sample Input 1

Sunny

Sample Output 1

Cloudy

In Takahashi's town, a sunny day is followed by a cloudy day.

Sample Input 2

Rainy

Sample Output 2

Sunny


local s = io.read()
local n = #s
local ret = true
for i = 1, n do
  if i % 2 == 0 then
    if s:sub(i, i) == "R" then
      ret = false
      break
    end
  else
    if s:sub(i, i) == "L" then
      ret = false
      break
    end
  end
end
print(ret and "Yes" or "No")

Score: 200 points

Problem Statement

Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

S is said to be easily playable if and only if it satisfies both of the following conditions:

Every character in an odd position (1-st, 3-rd, 5-th, \ldots) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th, \ldots) is L, U, or D.

Your task is to print Yes if S is easily playable, and No otherwise.

Constraints

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

Each character of S is L, R, U, or D.

Input

Input is given from Standard Input in the following format:

S

Output

Print Yes if S is easily playable, and No otherwise.

Sample Input 1

RUDLUDR

Sample Output 1

Yes

Every character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th) is L, U, or D.

Thus, S is easily playable.

Sample Input 2

DULL

Sample Output 2

No

The 3-rd character is not R, U, nor D, so S is not easily playable.

Sample Input 3

UUUUUUUUUUUUUUU

Sample Output 3

Yes

Sample Input 4

ULURU

Sample Output 4

No

Sample Input 5

RDULULDURURLRDULRLR

Sample Output 5

Yes


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
K=io.read"*n"
Q=io.read"*n"
c={}
for i=1,Q do
	local a=io.read"*n"
	if(c[a])then c[a]=c[a]+1
	else c[a]=1 end
end
for i=1,N do
	print(((c[i]or 0)>Q-K) and"Yes"or"No")
end

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

1 \leq A_i \leq N\ (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

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

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end
N=io.read"*n"
B=reada(N-1)
s=0
b=B[N-1]
for i=N-1,1,-1 do
	b=math.min(b,B[i])
	s=s+b
end
print(s+B[1])

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


n=io.read("*n")
b={}

for i=1, n-1 do
  table.insert(b, i, io.read("*n"))
end

ans=0

a1=0
a2=0

for i=1, n-1 do
  a1=a2
  if i == 1 then
    a1=b[i]
    a2=b[i]
  end
  a2=b[i]
  if b[i+1] == nil then
    a2=b[i]
  elseif b[i] > b[i+1] then
    a2=b[i+1]
  end

  ans = ans + a1
end

ans = ans + a2

print(ans)


local n = io.read("*n")
local sum = io.read("*n")
local prv = sum
local mmi = math.min
for i = 2, n - 1 do
  local nxt = io.read("*n")
  sum = sum + mmi(prv, nxt)
  prv = nxt
end
sum = sum + prv
print(sum)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, string.sub(k, i, i)) end local r = io.read local u = table.unpack return function() return r(u(a)) end end})
local N = read.n()
local B = {}
for i=1,N-1 do B[i] = read.n() end

local A = {}
A[N] = B[N-1]
local ans = A[N]
for i=N-1,2,-1 do
    A[i] = math.min(B[i-1], B[i])
    ans = ans + A[i]
end
A[1] = B[1]
ans = ans + A[1]
--print(table.concat(A, "\t"))
--print(table.concat(B, "\t"))
print(ans)


local n,k=io.read("n","n","l")
local s=io.read()
local counter=0
for i=1,n-1 do
    if s:byte(i)~=s:byte(i+1) then
        counter=counter+1
    end
end
print(n-1-(counter-2*k>0 and counter-2*k or 0))

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

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

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

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


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

local N, K = io.read("n", "n", "l")
local S = str2tbl(io.read("l"))
local h = 0
local sw = 0
for i=1,N-1 do
    if S[i] == 'L' and S[i+1] == 'L' then
        h = h + 1
    elseif S[i] == 'R' and S[i+1] == 'R' then
        h = h + 1
    else
        sw = sw + 1
    end
end
for k=1,K do
    if sw == 0 then
        break
    elseif sw == 1 then
        h = h + 1
        break
    else
        h = h + 2
        sw = sw - 2
    end
end
print(h)


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.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local stagenum = self.stagenum
  local stage, l, r = 1, 1, bls(1, stagenum - 1)
  while true do
    local sz = bls(1, stagenum - 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.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.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end
local inf = 1000000007
local n = io.read("*n")
local tot = bls(1, n)
local t = {}
local idx = {}
local childcnt = {}
local capacity = {}
for i = 1, tot do
  t[i] = io.read("*n")
  idx[i] = i
  childcnt[i] = 0
  capacity[i] = -1
end
table.sort(t)
local parent = {0}
local len = {0}
for i = 1, n do
  local sn = bls(1, i - 1)
  for j = 1, sn do
    childcnt[j] = childcnt[j] + 1
    parent[j + sn] = j
    len[j + sn] = len[j] + 1
  end
end
table.sort(idx, function(a, b) return len[a] > len[b] end)
local invidx = {}
for i = 1, tot do
  invidx[i] = i
end
for i = 1, tot do
  invidx[idx[i]] = i
end
-- print(table.concat(parent, " "))
-- print(table.concat(len, " "))
-- print(table.concat(idx, " "))

local st = SegTree.new(tot, mma, -inf)
for i = 1, tot do
  if childcnt[i] == 0 then
    st:setValue(invidx[i], -1)
  end
end
local valid = true
for i = 1, tot do
  local stpos = st:lower_bound(-t[i])
  if tot < stpos then valid = false break end
  st:setValue(stpos, -inf)
  local src = idx[stpos]
  if i ~= tot then
    local p = parent[src]
    -- print(stpos, src, p)
    local stp = invidx[p]
    capacity[p] = mmi(capacity[p], -t[i] - 1)
    childcnt[p] = childcnt[p] - 1
    if childcnt[p] == 0 then
      st:setValue(stp, capacity[p])
    end
  end
end
print(valid and "Yes" or "No")

Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

All values in input are integers.

1 \leq N \leq 18

1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.

Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.

Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.

Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

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

Sample Output 4

No


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.lower_bound = function(self, val)
  local ret, retpos = self.emptyvalue, 0
  local stagenum = self.stagenum
  local stage, l, r = 1, 1, bls(1, stagenum - 1)
  while true do
    local sz = bls(1, stagenum - 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.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.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end
local inf = 1000000007
local n = io.read("*n")
local tot = bls(1, n)
local t = {}
local idx = {}
local childcnt = {}
local capacity = {}
for i = 1, tot do
  t[i] = io.read("*n")
  idx[i] = i
  childcnt[i] = 0
  capacity[i] = -1
end
table.sort(t)
local parent = {0}
local len = {0}
for i = 1, n do
  local sn = bls(1, i - 1)
  for j = 1, sn do
    childcnt[j] = childcnt[j] + 1
    parent[j + sn] = j
    len[j + sn] = len[j] + 1
  end
end
table.sort(idx, function(a, b) return len[a] > len[b] end)
-- print(table.concat(parent, " "))
-- print(table.concat(len, " "))
-- print(table.concat(idx, " "))

local st = SegTree.new(tot, mma, -inf)
for i = 1, tot do
  if childcnt[i] == 0 then
    st:setValue(i, capacity[i])
  end
end
local valid = true
for i = 1, tot do
  local pos = st:lower_bound(-t[i])
  if tot < pos then valid = false break end
  st:setValue(pos, -inf)
  if i ~= tot then
    local p = parent[pos]
    capacity[p] = mmi(capacity[p], -t[i] - 1)
    childcnt[p] = childcnt[p] - 1
    if childcnt[p] == 0 then
      st:setValue(p, capacity[p])
    end
  end
end
print(valid and "Yes" or "No")


local s = io.read()
local t = io.read()
local c = 0
for i = 1, 3 do
  if s:sub(i, i) == t:sub(i, i) then c = c + 1 end
end
print(c)

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


local a,b = io.read("a"):match("(%d+).(%d+)")
a,b = math.tointeger(tonumber(a)) - 1,math.tointeger(tonumber(b))
local count = 0
local loop = true
while loop do
  count = count + 1
  if b <= a * count + 1 then
    loop = false
  end
end
print(count)

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


local a,b = io.read("a"):match("(%d+).(%d+)")
a,b = tonumber(a),tonumber(b)
local count = 0
local loop = true
while loop do
  count = count + 1
  if b <= (a - 1) * count + 1 then
    loop = false
  end
end
print(count)


n=io.read("*n")
h={}
for i=1, n do
  table.insert(h, io.read("*n"))
end

ans=0
s=0
for i=1, n-1 do
  if h[i] < h[i+1] then
    s=0
  else
    s=s+1
  end

  if ans < s then
    ans=s
  end
end

print(ans)

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


a={}
n=io.read()
temp=0
now_max= 0  --最高ジャンプ回数
count=0 --カウント
for i=1, n,1 do
  temp=io.read("*n")
  a[i]=temp
end

now=a[1] --現在の高さ

for k=2,#a,1 do
  if now >= a[k] then
    now=a[k]
    count=count + 1
  else
    now=a[k]
    count=0
  end

  if now_max < count then
    now_max=count
  end
end

if n== 1 then
print(0)
else
print(now_max)
end


local l = io.read("*l")
local beforeNum = nil
local count,result = 0,0
for _ in io.read("*l"):gmatch("%d+") do
  if beforeNum == nil then
    beforeNum = tonumber(_)
  elseif beforeNum >= tonumber(_) then
    count = count + 1
  elseif count > result then
    result = count
    count = 0
  else
    count = 0
  end
  beforeNum = tonumber(_)
end
if count > result then
  result = count
end
print(result)


Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0


local l = io.read("*l")
l = math.tointeger(tonumber(l))
local result = math.tointeger(0)
if l == 1 then
  result = 0
elseif l % 2 == 0 then
  result = (l - 1) * (l // 2)
else
  result = (l - 2) * (l // 2) + l - 1
end
print(result)


local n = io.read("*n")
local edge = {}
local srccnt = {}
for i = 1, n * n do
  edge[i] = {}
  srccnt[i] = 0
end
local function getindex(a, b)
  if a < b then
    return (a - 1) * n + b
  else
    return (b - 1) * n + a
  end
end
for i = 1, n do
  local prvidx = getindex(i, io.read("*n"))
  for j = 2, n - 1 do
    local nxtidx = getindex(i, io.read("*n"))
    edge[prvidx][nxtidx] = true
    srccnt[nxtidx] = srccnt[nxtidx] + 1
    prvidx = nxtidx
  end
end
local tasks = {}
for i = 1, n * n do
  if next(edge[i]) and srccnt[i] == 0 then
    table.insert(tasks, i)
  end
end
local day = 0
local matched = 0
while 0 < #tasks do
  day = day + 1
  local ng = false
  local newtasks = {}
  for i = 1, #tasks do
    local src = tasks[i]
    matched = matched + 1
    for dst, _unused in pairs(edge[src]) do
      srccnt[dst] = srccnt[dst] - 1
      if srccnt[dst] == 0 then
        table.insert(newtasks, dst)
      end
    end
    if ng then break end
  end
  if ng then break end
  tasks = newtasks
end
if matched * 2 ~= n * (n - 1) then
  day = -1
end
print(day)

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

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

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


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

for i = 1, n do
  if not waitlist[t[i][1]] then
    waitlist[t[i][1]] = {}
  end
  waitlist[t[i][1]][i] = true
end

local ret = 0
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  local done = {}
  for i = 1, n do done[i] = false end
  for i, dstlist in pairs(waitlist) do
    if pos[i] < n and not done[i] then
      for nxt, _unused in pairs(dstlist) do
        remain = true
        if waitlist[nxt] and waitlist[nxt][i] and not done[nxt] then
          proceed = true
          waitlist[nxt][i] = nil
          waitlist[i][nxt] = nil
          pos[i] = pos[i] + 1
          pos[nxt] = pos[nxt] + 1
          done[i] = true
          done[nxt] = true
          if pos[i] < n then
            if not waitlist[t[i][pos[i]]] then waitlist[t[i][pos[i]]] = {} end
            waitlist[t[i][pos[i]]][i] = true
          end
          if pos[nxt] < n then
            if not waitlist[t[nxt][pos[nxt]]] then waitlist[t[nxt][pos[nxt]]] = {} end
            waitlist[t[nxt][pos[nxt]]][nxt] = true
          end
          break
        end
      end
    end
  end
  if not remain then
    if not proceed then
      ret = ret - 1
    end
    break
  end
  if not proceed then
    ret = -1
    break
  end
end
print(ret)


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

for i = 1, n do
  if not waitlist[t[i][1]] then
    waitlist[t[i][1]] = {}
  end
  waitlist[t[i][1]][i] = true
end

local ret = 0
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  local done = {}
  for i = 1, n do done[i] = false end
  for i, dstlist in pairs(waitlist) do
    if pos[i] < n and not done[i] then
      for nxt, _unused in pairs(dstlist) do
        remain = true
        local nxt = t[i][pos[i]]
        if waitlist[nxt] and waitlist[nxt][i] and waitlist[i][nxt] and not done[nxt] then
          proceed = true
          waitlist[nxt][i] = nil
          waitlist[i][nxt] = nil
          pos[i] = pos[i] + 1
          pos[nxt] = pos[nxt] + 1
          done[i] = true
          done[nxt] = true
          if pos[i] < n then
            if not waitlist[t[i][pos[i]]] then waitlist[t[i][pos[i]]] = {} end
            waitlist[t[i][pos[i]]][i] = true
          end
          if pos[nxt] < n then
            if not waitlist[t[nxt][pos[nxt]]] then waitlist[t[nxt][pos[nxt]]] = {} end
            waitlist[t[nxt][pos[nxt]]][nxt] = true
          end
          break
        end
      end
    end
  end
  if not remain then
    if not proceed then
      ret = ret - 1
    end
    break
  end
  if not proceed then
    ret = -1
    break
  end
end
print(ret)


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

local ret = 0
local done = {}
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  for i = 1, n do done[i] = false end
  for i = 1, n do
    if pos[i] < n and not done[i] then
      remain = true
      local nxt = t[i][pos[i]]
      if t[nxt][pos[nxt]] == i and not done[nxt] then
        proceed = true
        pos[i] = pos[i] + 1
        pos[nxt] = pos[nxt] + 1
        done[i] = true
        done[nxt] = true
      end
    end
  end
  if not remain then
    if not proceed then
      ret = ret - 1
    end
    break
  end
  if not proceed then
    ret = -1
    break
  end
end
print(ret)


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

for i = 1, n do
  waitlist[t[i][1]][i] = true
end

local ret = 0
while true do
  ret = ret + 1
  local proceed = false
  local remain = false
  local done = {}
  for i = 1, n do done[i] = false end
  for i = 1, n do
    -- print(ret, i, pos[i])
    if pos[i] < n and not done[i] then
      remain = true
      local nxt = t[i][pos[i]]
      if waitlist[i][nxt] and waitlist[nxt][i] and not done[nxt] then
        -- print("MATCH", ret, i, nxt)
        proceed = true
        waitlist[nxt][i] = nil
        waitlist[i][nxt] = nil
        pos[i] = pos[i] + 1
        pos[nxt] = pos[nxt] + 1
        done[i] = true
        done[nxt] = true
        if pos[i] < n then
          waitlist[t[i][pos[i]]][i] = true
        end
        if pos[nxt] < n then
          waitlist[t[nxt][pos[nxt]]][nxt] = true
        end
      end
    end
  end
  if not remain then
    if not proceed then
      ret = ret - 1
    end
    break
  end
  if not proceed then
    ret = -1
    break
  end
end
print(ret)


local n = io.read("*n")
local t = {}
for i = 1, n do
  local x, y = io.read("*n", "*n")
  if x ~= 0 or y ~= 0 then
    table.insert(t, {x, y})
  end
end
n = #t
local at2 = math.atan2
table.sort(t, function(a, b)
  return at2(a[2], a[1]) < at2(b[2], b[1])
end
)
local mma, msq = math.max, math.sqrt
local maxlen = 0
local halfpi = math.pi / 2
local pi2 = math.pi * 2
for i_start = 1, n do
  local cur_x, cur_y = t[i_start][1], t[i_start][2]
  local cur_rad = at2(cur_y, cur_x)
  local cur_len = msq(cur_x * cur_x + cur_y * cur_y)
  local used = {}
  for j = 1, n - 1 do
    used[j] = false
  end
  local needreset = true
  while needreset do
    needreset = false
    for j = 1, n - 1 do
      if not used[j] then
        local pos = i_start + j
        local up = false
        if n < pos then
          pos = pos - n
          up = true
        end
        local nxt_x, nxt_y = t[pos][1], t[pos][2]
        local nxt_rad = at2(nxt_y, nxt_x)
        if up then
          nxt_rad = nxt_rad + pi2
        end
        local isok = false
        if nxt_rad - cur_rad <= halfpi then
          cur_x = cur_x + nxt_x
          cur_y = cur_y + nxt_y
          cur_rad = at2(cur_y, cur_x)
          cur_len = msq(cur_x * cur_x + cur_y * cur_y)
          used[j] = true
        else
          local candx = cur_x + nxt_x
          local candy = cur_y + nxt_y
          if cur_len < msq(candx * candx + candy * candy) then
            used[j] = true
            needreset = true
            cur_x = candx
            cur_y = candy
            cur_rad = at2(cur_y, cur_x)
            cur_len = msq(cur_x * cur_x + cur_y * cur_y)
          end
        end
      end
    end
  end
  maxlen = mma(maxlen, cur_len)
end
print(string.format("%.13f", maxlen))

Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

Sample Output 7

148.660687473185055226120082139313966514489855137208


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

local mod=1000000007
local A=0
local B=0
local counter=0
for i=1,n do
    for j=1,n do
        if a[i]>a[j] and i<j then
            A=A+1
        end
        if a[i]>a[j] then
            B=B+1
        end
    end
end
local counterA=A*k%mod
local counterB=(k*(k-1)//2%mod)*B%mod
print((counterA+counterB)%mod)

Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

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

Sample Output 3

185297239

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


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

local sum = 0
local mul = 2
for i = 1, n do
  sum = sum + v[i] / mul
  if i ~= n - 1 then mul = mul * 2 end
end
print(string.format("%.10f",sum))

Score : 300 points

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i.

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

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

Sample Input 1

2
3 4

Sample Output 1

3.5

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

Printing 3.50001, 3.49999, and so on will also be accepted.

Sample Input 2

3
500 300 200

Sample Output 2

375

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.

Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.

Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.

Thus, the maximum possible value of the last ingredient remaining is 375.

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


local mfl = math.floor

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

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

local s = io.read()
local t = io.read()
local n = #s
local m = #t
local s_tbl = {}
local sstr = ""
for i = 1, n do
  sstr = s:sub(i, i)
  if not s_tbl[sstr] then s_tbl[sstr] = {} end
  table.insert(s_tbl[sstr], i)
end

local curpos = 0
local loop = 0
local isok = true
for i = 1, m do
  sstr = t:sub(i, i)
  if not s_tbl[sstr] then
    isok = false
    break
  else
    local bnd = upper_bound(s_tbl[sstr], curpos)
    if #s_tbl[sstr] < bnd then
      loop = loop + 1
      curpos= s_tbl[sstr][1]
    else
      curpos = s_tbl[sstr][bnd]
    end
  end
end

if isok then
print(loop * n + curpos)
else
  print(-1)
end

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


s=io.read()
counter=0
previous=""
current=""
for i=1,#s do
    current=current..s:sub(i,i)
    if current~=previous then
        previous=current
        current=""
        counter=counter+1
    end
end
print(counter)

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.

Sample Input 1

aabbaa

Sample Output 1

4

We can, for example, divide S into four strings aa, b, ba, and a.

Sample Input 2

aaaccacabaababc

Sample Output 2

12


s=io.read()
counter=0
previous=""
current=""
for i=1,#s do
    current=current+s:sub(i,i)
    if current~=previous then
        previous=current
        current=""
        counter=counter+1
    end
end
print(counter)


s=io.read()
counter=1
for i=2,#s do
    checker=true
    if s:sub(i-1,i-1)~=s:sub(i,i) then
        counter=counter+1
    elseif s:sub(i-1,i-1)==s:sub(i,i) then
        if i>=3 then
            counter=counter+0.5
        end
    end
end
print(string.format("%d",counter))


local function str2tbl(s)
    -- limit of #s is approximately 999959
    local t = {string.byte(s, 1, #s)}
    local char = string.char
    for i=1, #t do
        t[i] = char(t[i])
    end
    return t
end
local S = str2tbl(io.read("l"))
local function solve()
    if #S == 1 then
        return 1
    elseif #S == 2 then
        return S[1] == S[2] and 1 or 2
    end
    local function strategy1()
        local p = S[1]
        local count = 1
        local i = 2
        while i <= #S do
            if S[i] == p then
                if i + 1 <= #S then
                    count = count + 1
                    p = p .. S[i]
                    i = i + 2
                else
                    return count
                end
            else
                count = count + 1
                p = S[i]
                i = i + 1
            end
        end
        return count
    end
    return strategy1()
end
print(solve())


Score : 100 points

Problem Statement

We have two integers: A and B.

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

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.

Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.


local K, X = io.read("n", "n")
local left = math.max(-1000000, X-K+1)
local right = math.min(1000000, X+K-1)
local ans = {}
for i=left,right do
    table.insert(ans, i)
end
print(table.concat(ans, ' '))

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


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

local function factorial(n)
    local a = 1
    for i=1, n do
        a = a * i
    end
    return a
end

local function nCr(n, r)
    if r > n then
        return 0
    end
    return factorial(n) // factorial(r) // factorial(n - r)
end
---
local N, _ = io.read("n", "l")
local sorted_dic = {}
for i=1,N do
    local s = io.read("l")
    local s_tbl = str2tbl(s)
    table.sort(s_tbl)
    local s_sorted = table.concat(s_tbl, "")
    --print(s_sorted)
    sorted_dic[s_sorted] = (sorted_dic[s_sorted] or 0) + 1
end
local ans = 0
for k,v in pairs(sorted_dic) do
    --print(k,v,factorial(v),factorial(2), factorial(v - 2), nCr(v, 2))
    ans = ans + nCr(v, 2)
end
print(ans)

Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


local num, day = string.match(io.read(), "(%d+) (%d+)")
num, day = tonumber(num), tonumber(day)
local input = {}
for i = 1, num do
  local a, b = string.match(io.read(), "(%d+) (%d+)")
  table.insert(input, {
	day = tonumber(a),
    result = tonumber(b)
  })
end

table.sort(input, function (a, b)
    if a.result == b.result then
      return a.day >= b.day
    else
      return a.result >= b.result
    end
  end)

local sum = 0
for i = 1, #input do
  if day >= input[i].day then
    sum = sum + input[i].result
    day = day - 1
  end
end
print(sum)

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


-- heap
local Heap = {}
function Heap.new(upper_func)
    local self = setmetatable({}, {__index = Heap})
    self.a = {}
    self.upper_func = upper_func
    self.upper = function(k1, k2)
        assert(k1 < k2)
        if k1 == 0 then
            return false
        else
            return upper_func(self.a[k1], self.a[k2])
        end
    end
    self.lower = function(k1, k2)
        return not self.upper(k1, k2)
    end
    return self
end

function Heap:count()
    return #self.a
end

function Heap:_swap(k1, k2)
    local old_k1 = self.a[k1]
    self.a[k1] = self.a[k2]
    self.a[k2] = old_k1
end

function Heap:upheap(newk)
    local ku = newk // 2
    local k = newk
    while ku > 0 and self.lower(ku, k) do
        self:_swap(ku, k)
        k = k//2
        ku = ku//2
    end
end

function Heap:insertq(e)
    table.insert(self.a, e)
    self:upheap(#self.a)
end

function Heap:downheap(newk)
    local k = newk
    while k <= #self.a//2 do
        local kdl = k*2
        local kdr = kdl + 1
        local kd
        if kdr <= #self.a and self.lower(kdl, kdr) then
            kd = kdr
        else
            kd = kdl
        end
        if self.upper(k, kd) then
            break
        end
        self:_swap(k, kd)
        k = kd
    end
end

function Heap:top()
    assert(#self.a >= 1)
    return self.a[1]
end

function Heap:remove_top()
    assert(#self.a >= 1)
    local v = self.a[1]
    self:_swap(1, #self.a)
    table.remove(self.a)
    self:downheap(1)
    return v
end

----
local N, M = io.read("n", "n")
local jobs_of_a = {}
for i=1, 100000 do
    jobs_of_a[i] = {}
end
for i=1, N do
    local a, b = io.read("n", "n")
    table.insert(jobs_of_a[a], b)
end
local q = Heap.new(function (v1, v2)
    return v1 > v2
end)
local ans = 0
for a=1,M do
    local day = M + 1 - a
    assert(day >= 1)
    for k,v in ipairs(jobs_of_a[a]) do
        q:insertq(v)
    end
    if q:count() > 0 then
        local job = q:remove_top()
        ans = ans + job
    end
end
print(ans)


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

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

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

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

local n, m = io.read("*n", "*n")
local t = {}
for i = 1, n do
  local a, b = io.read("*n", "*n")
  t[i] = {a, b}
end
table.sort(t, function(x, y) return x[1] < y[1] end)
local alist = {}
for i = 1, n do
  alist[i] = t[i][1]
end
local treeary = {}
for i = 1, n do
  treeary[i] = {i, t[i][2]}
end

local tree = SegTree.new(treeary, function(x, y)
  if x[2] == y[2] then
    if x[1] < y[1] then
      return y
    else
      return x
    end
  elseif x[2] < y[2] then
    return y
  else
    return x
  end
end, {0, 0})

local ret = 0
for i = m, 1, -1 do
  local ub = upper_bound(alist, i)
  if 1 < ub then
    local r = tree:getRange(1, ub - 1)
    local idx, val = r[1], r[2]
    tree:setValue(idx, {idx, 0})
    ret = ret + val
  end
end
print(ret)


local mfl, mce = math.floor, math.ceil
local p = io.read("*n")
local function badd(x, y) return (x + y) % p end
local function bmul(x, y) return (x * y) % p end

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

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

for i = 0, p - 1 do
  local v = a[i + 1]
  if 0 < i then
    v = bmul(v, invfact[i])
  end
  if i < p - 1 then
    v = bmul(v, invfact[p - 1 - i])
    if (p - 1 - i) % 2 == 1 then
      v = (p - v) % p
    end
  end
  local pol = {}
  pol[1] = 1
  for j = 2, p do pol[j] = 0 end
  for j = 0, p - 1 do
    if i ~= j then
      for k = p, 1, -1 do
        if 1 < k then
          pol[k] = badd(pol[k - 1], bmul(pol[k], p - j))
        else
          pol[1] = bmul(pol[1], p - j)
        end
      end
    end
  end
  for j = 1, p do
    poldst[j] = badd(poldst[j], bmul(v, pol[j]))
  end
end
print(table.concat(poldst, " "))

Score : 600 points

Problem Statement

Given are a prime number p and a sequence of p integers a_0, \ldots, a_{p-1} consisting of zeros and ones.

Find a polynomial of degree at most p-1, f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \ldots + b_0, satisfying the following conditions:

For each i (0 \leq i \leq p-1), b_i is an integer such that 0 \leq b_i \leq p-1.

For each i (0 \leq i \leq p-1), f(i) \equiv a_i \pmod p.

Constraints

2 \leq p \leq 2999

p is a prime number.

0 \leq a_i \leq 1

Input

Input is given from Standard Input in the following format:

p
a_0 a_1 \ldots a_{p-1}

Output

Print b_0, b_1, \ldots, b_{p-1} of a polynomial f(x) satisfying the conditions, in this order, with spaces in between.

It can be proved that a solution always exists. If multiple solutions exist, any of them will be accepted.

Sample Input 1

2
1 0

Sample Output 1

1 1

f(x) = x + 1 satisfies the conditions, as follows:

f(0) = 0 + 1 = 1 \equiv 1 \pmod 2

f(1) = 1 + 1 = 2 \equiv 0 \pmod 2

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

f(x) = 0 is also valid.

Sample Input 3

5
0 1 0 1 0

Sample Output 3

0 2 0 1 3


n=io.read("*n")
if c-(a-b)>=0 then
    print(c-(a-b))
else
    print(0)
end

Score : 100 points

Problem Statement

We have two bottles for holding water.

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

Bottle 2 contains C milliliters of water.

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

Sample Input 1

6 4 3

Sample Output 1

1

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


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

ans=c-(a-b)
if ans < 0 then
  print("0")
else
  print(ans)
end


local insert = table.insert

local s = io.read()
local n = s:len()

local s_byte_t = {}
for i = 1, n do
	s_byte_t[i] = s:byte(i);
end

local L, R = string.byte("LR", 1, 2)
-- 例の場所に入ったとき偶数回目の移動だったならその場

local next_different_t = {}
local current = R
for i = 1, n do
	if s_byte_t[i] ~= current then
		insert(next_different_t, i - 1)
		if current == R then
			current = L
		else
			current = R
		end
	end
end

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

local next_different_t_len = #next_different_t
current = R
for i = 1, next_different_t_len + 1 do
	local front = (i ~= next_different_t_len + 1) and next_different_t[i] or n
	local front_before = (i - 1 ~= 0) and next_different_t[i - 1] or 0

	if current == R then
		if current == R and (front - front_before) % 2 == 0 then
			ans_t[front]     = ans_t[front]
				+ (front - front_before) // 2
			ans_t[front + 1] = ans_t[front + 1]
				+ (front - front_before) // 2
		else
			ans_t[front]     = ans_t[front]
				+ (front - front_before) // 2 + 1
			ans_t[front + 1] = ans_t[front + 1]
				+ (front - front_before) // 2
		end
		current = L
	elseif current == L then
		if (front - front_before) % 2 == 0 then
			ans_t[front_before]     = ans_t[front_before]
				+ (front - front_before) // 2
			ans_t[front_before + 1] = ans_t[front_before + 1]
				+ (front - front_before) // 2
		else
			ans_t[front_before]     = ans_t[front_before]
				+ (front - front_before) // 2
			ans_t[front_before + 1] = ans_t[front_before + 1]
				+ (front - front_before) // 2 + 1
		end
		current = R
	end
end

print(table.concat(ans_t, " "))

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

Initially, one child is standing on each square.

Each child will perform the move below 10^{100} times:

Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.

Find the number of children standing on each square after the children performed the moves.

Constraints

S is a string of length between 2 and 10^5 (inclusive).

Each character of S is L or R.

The first and last characters of S are R and L, respectively.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.

After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

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

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


S=io.read()

for s,l,m,r in S:gmatch"(()R+()L+())"do
for i=l,m-2 do
	print(0)
end
ln=m-l
rn=r-m
print(math.ceil(ln*.5)+math.floor(rn*.5))
print(math.floor(ln*.5)+math.ceil(rn*.5))
for i=m+1,r-1 do
	print(0)
end
end


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

--
local S = str2tbl(io.read("l"))
local N = #S
assert(S[1] == 'R')
assert(S[N] == 'L')

local anstbl = {}
for i=1,N do
    anstbl[i] = 0
end

local function inc(i)
    anstbl[i] = anstbl[i] + 1
end

local dst = {}
local nearest_left_R = 1
for i=2, N do
    if S[i] == 'R' then
        nearest_left_R = i
    else
        dst[i] = {'L', nearest_left_R}
    end
end

local nearest_right_L = N
for i=N-1, 1, -1 do
    if S[i] == 'L' then
        nearest_right_L = i
    else
        dst[i] = {'R', nearest_right_L}
    end
end

for i=1, N do
    local lr, nearest_x = dst[i][1], dst[i][2]
    if lr == 'R' then
        local nearest_right_L = nearest_x
        if (nearest_right_L - i) % 2 == 0 then
            inc(nearest_right_L)
        else
            inc(nearest_right_L-1)
        end
    else
        -- lr == 'L'
        local nearest_left_R = nearest_x
        if (i - nearest_left_R ) % 2 == 0 then
            inc(nearest_left_R)
        else
            inc(nearest_left_R+1)
        end

    end
end

print(table.concat(anstbl, ' '))


a,b=io.read("*n","*n")
if (a+b)%2==0 then
  print(math.floor((a+b)/2))
else
  print("IMPOSSIBLE")
end

Score: 100 points

Problem Statement

We have two distinct integers A and B.

Print the integer K such that |A - K| = |B - K|.

If such an integer does not exist, print IMPOSSIBLE instead.

Constraints

All values in input are integers.

0 \leq A,\ B \leq 10^9

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

If such an integer does not exist, print IMPOSSIBLE instead.

Sample Input 1

2 16

Sample Output 1

9

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


local A,B=io.read("n","n")
if B<A then
  A,B=B,A
end
local d=A-B
if d%2==0 then
  print(A-d//2)
else
  print("IMPOSSIBLE")
end


n=io.read("*n")

for i=1,n do
    x=io.read("*n")
    if i~=x then
        counter=counter+1
    end
end

if counter==2 or counter==0 then
    print("YES")
else
    print("NO")
end

Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.


local N=io.read("n")
local prev=0
local r=0
for i=1,N do
  local cur = io.read("n")
  if prev>cur then
    r = r + 1
  end
  prev = cur
end
if r<=2 then
  print("Yes")
else
  print("No")
end


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

counter=0
for i=1,n do

    if a[i]<b[i] then
        counter=counter+a[i]
        b[i]=b[i]-a[i]
    else
        counter=counter+b[i]
        b[i]=0
    end

    if a[i+1]<b[i] then
        counter=counter+a[i+1]
        a[i+1]=0
    else
        counter=counter+b[i]
        a[i+1]=a[i+1]-b[i]
    end

end

print(counter)

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local MOD = math.floor(10^9)+7

local tmp = read.l():totable()
local S = {}
for i=1,#tmp do
    if tmp[i] == '?' then
        S[i] = '?'
    else
        S[i] = tonumber(tmp[i])
    end
end

local N = #S
local ti = array(1, 0)
local tio = array(1, 0)
tio[0] = 1
for i=1,N do
    if S[i] == '?' then
        local oldsum = tio[0] + tio[1] + tio[2] + tio[3] + tio[4] + tio[5] + tio[6] + tio[7] + tio[8] + tio[9] + tio[10] + tio[11] + tio[12]
        ti[0] = (oldsum - tio[4] - tio[8] - tio[12]) % MOD
        ti[1] = (oldsum - tio[3] - tio[8] - tio[12]) % MOD
        ti[2] = (oldsum - tio[3] - tio[7] - tio[12]) % MOD
        ti[3] = (oldsum - tio[3] - tio[7] - tio[11]) % MOD
        ti[4] = (oldsum - tio[2] - tio[7] - tio[11]) % MOD
        ti[5] = (oldsum - tio[2] - tio[6] - tio[11]) % MOD
        ti[6] = (oldsum - tio[2] - tio[6] - tio[10]) % MOD
        ti[7] = (oldsum - tio[1] - tio[6] - tio[10]) % MOD
        ti[8] = (oldsum - tio[1] - tio[5] - tio[10]) % MOD
        ti[9] = (oldsum - tio[1] - tio[5] - tio[9]) % MOD
        ti[10] = (oldsum - tio[0] - tio[5] - tio[9]) % MOD
        ti[11] = (oldsum - tio[0] - tio[4] - tio[9]) % MOD
        ti[12] = (oldsum - tio[0] - tio[4] - tio[8]) % MOD
    else
        local c = S[i]
        ti[(0 + c) % 13] = tio[0]
        ti[(10 + c) % 13] = tio[1]
        ti[(20 + c) % 13] = tio[2]
        ti[(30 + c) % 13] = tio[3]
        ti[(40 + c) % 13] = tio[4]
        ti[(50 + c) % 13] = tio[5]
        ti[(60 + c) % 13] = tio[6]
        ti[(70 + c) % 13] = tio[7]
        ti[(80 + c) % 13] = tio[8]
        ti[(90 + c) % 13] = tio[9]
        ti[(100 + c) % 13] = tio[10]
        ti[(110 + c) % 13] = tio[11]
        ti[(120 + c) % 13] = tio[12]
    end
    tio, ti = ti, tio
end
print(tio[5])

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local MOD = math.floor(10^9)+7

local tmp = read.l():totable()
local S = {}
for i=1,#tmp do
    if tmp[i] == '?' then
        S[i] = '?'
    else
        S[i] = tonumber(tmp[i])
    end
end
local N = #S
local tbl = array(2, 0)
tbl[0][0] = 1
for i=1,N do
    if S[i] == '?' then
        for nd=0,9 do
            for oldm=0,12 do
                local newm = (oldm * 10 + nd) % 13
                tbl[i][newm] = (tbl[i][newm] + tbl[i-1][oldm]) % MOD
            end
        end
    else
        for oldm=0,12 do
            local newm = (oldm * 10 + S[i]) % 13
            tbl[i][newm] = tbl[i-1][oldm]
        end
    end
end
print(tbl[N][5])


local mod = 1000000007
local mfl = math.floor

local s = io.read()
local n = #s
local t = {}
for i = 1, n do
  t[i] = {}
  for j = 0, 12 do
    t[i][j] = 0
  end
end
local sstr = s:sub(1, 1)
if sstr == "?" then
  for i = 0, 9 do
    t[1][i] = 1
  end
else
  t[1][tonumber(sstr)] = 1
end

local jumptbl = {0, 10, 7, 4, 1, 11, 8, 5, 2, 12, 9, 6, 3}

for i = 2, n do
  -- times 10
  local tmp = {}
  for j = 0, 12 do
    tmp[jumptbl[j + 1]] = t[i - 1][j]
  end
  for j = 0, 12 do
    t[i - 1][j] = tmp[j]
  end
  -- add
  sstr = s:sub(i, i)
  if sstr == "?" then
    for num = 0, 9 do
      for j = 0, 12 do
        t[i][(j + num) % 13] = (t[i][(j + num) % 13] + t[i - 1][j]) % 1000000007
      end
    end
  else
    local num = tonumber(sstr)
    for j = 0, 12 do
      t[i][(j + num) % 13] = t[i - 1][j]
    end
  end
end
print(t[n][5])


local s = io.read()
local t = io.read()
local swapped = false
if #s < #t then
  swapped = true
  s, t = t, s
end
local slen = #s
local tlen = #t
s = s .. s

local mma = math.max
local findpos = s:find(t, 1, true)
local maxlength = 0
local mfl = math.floor
while findpos do
  if slen < findpos then
    break
  end
  local curlength = 1
  while true do
    local p = findpos + tlen * curlength
    if p < findpos + slen and t == s:sub(p, p + tlen - 1) then
      curlength = curlength + 1
    else
      break
    end
  end
  maxlength = mma(maxlength, curlength)
  if slen % tlen == 0 and curlength == mfl(slen / tlen) then
    maxlength = -1
    break
  end
  findpos = s:find(t, findpos + tlen * curlength, true)
end

local same_s, same_t = true, true
for i = 2, slen do if s:sub(1, 1) ~= s:sub(i, i) then same_s = false break end end
for i = 2, tlen do if t:sub(1, 1) ~= t:sub(i, i) then same_t = false break end end
if same_s and same_t and s:sub(1, 1) == t:sub(1, 1) then
  maxlength = -1
end
if swapped then
  if 0 < maxlength then
    maxlength = 0
  end
end
print(maxlength)

Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.


Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


n, d = io.read("*n", "*n")
print(math.ceil(n / (2 * d + 1)))

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


local N, D = io.read("n", "n")
print(math.ceil(N / (2 * D + 1)))


local N = io.read("n")
local A = {}
for i=1,N do
    A[i] = io.read("n")
end
local max_ltoi = {}
local max_itor = {}
max_ltoi[0] = -1
max_itor[N+1] = -1
for i=1,N do
    max_ltoi[i] = math.max(max_ltoi[i-1], A[i])
end
for i=N,1,-1 do
    max_itor[i] = math.max(max_itor[i+1], A[i])
end
for i=1,N do
    local m = math.max(max_ltoi[i-1], max_itor[i+1])
    print(m)
end

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


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

local box={}
for i=n,1,-1 do
    local ball=0
    for j=2*i,n,i do
        ball=ball+(box[j] or 0)
    end
    if ball%2~=a[i] then
        box[i]=1
    end
end

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

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


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

local box={}
for i=n,1,-1 do
    local ball=0
    for j=2*i,n,i do
        ball=ball+(box[j] or 0)
    end
    if ball%2~=a[i] then
        box[i]=1
    end
end

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


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

local box = {}
for i=N, 1, -1 do
    local k = i
    local bi = A[i]
    for n=2,1000000 do
        k = k + i
        if k > N then
            break
        end
        bi = bi ~ box[k]
    end
    box[i] = bi
end
local M = 0
for i=1,N do
    if box[i] == 1 then
        M = M + 1
    end
end
print(M)
if M == 0 then return end
local sb = {}
for i=1,N do
    if box[i] == 1 then
        table.insert(sb, tostring(i))
    end
end
print(table.concat(sb, " "))


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n")
local a, t = {}, {}
for i = 1, n do
  a[i] = io.read("*n")
  t[i] = 0
end
for i = n, 1, -1 do
  local dst = a[i]
  local j = 2
  local cur = 0
  while j * i <= n do
    cur = cur + t[j * i]
    j = j + 1
  end
  cur = cur % 2
  t[i] = (a[i] + cur) % 2
end
local m = 0
for i = 1, n do
  if t[i] == 1 then m = m + 1 end
end
print(m)
for i = 1, n do
  if t[i] == 1 then print(i) end
end


local function getxor(x, y)
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    if (x % 2) + (y % 2) == 1 then
      ret = ret + mul
    end
    x, y, mul = x // 2, y // 2, mul * 2
  end
  return ret
end

local n = io.read("*n")
local a = {}
local allzero = true
local t = {}
local tcnt = 0
for i = 1, n do
  a[i] = io.read("*n")
  if a[i] ~= 0 then allzero = false end
  if t[a[i]] then
    t[a[i]] = t[a[i]] + 1
  else
    if tcnt < 3 then
      tcnt = tcnt + 1
      t[a[i]] = 1
    else
      tcnt = 4
    end
  end
end
if allzero then
  print("Yes")
elseif n % 3 ~= 0 then
  print("No")
elseif tcnt == 3 then
  local f = true
  local g = {}
  for k, v in pairs(t) do
    if v ~= n // 3 then f = false end
    table.insert(g, k)
  end
  if f then
    f = getxor(g[1], g[2]) == g[3]
  end
  print(f and "Yes" or "No")
elseif tcnt == 2 then
  local f = true
  for k, v in pairs(t) do
    if v == n // 3 and k == 0 then
    elseif v == n * 2 // 3 then
    else f = false
    end
  end
  print(f and "Yes" or "No")
else
  print("No")
end

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


local mfl, mce = math.floor, math.ceil
local n, m = io.read("*n", "*n")
local edge = {}
local edgenum = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
end
if m % 2 == 1 then
  print(-1)
  os.exit()
end
for i = 1, n do
  local dsts = {}
  for dst, _u in pairs(edge[i]) do
    table.insert(dsts, dst)
  end
  local lim = mfl(#dsts / 2)
  for j = 1, lim, 2 do
    print(i .. " " .. dsts[j * 2 - 1])
    print(i .. " " .. dsts[j * 2])
  end
  for j = 1, lim * 2 do
    edge[dsts[j]][i] = nil
  end
end

Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i.
Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph.
Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.

Notes

An undirected graph is said to be simple when it contains no self-loops or multiple edges.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N (1\leq i\leq M)

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If it is impossible to assign directions to satisfy the requirement, print -1.
Otherwise, print an assignment of directions that satisfies the requirement, in the following format:

C_1 D_1
:
C_M D_M

Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges.

Sample Input 2

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

Sample Output 2

-1