local mma = math.max
local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegForAvl = {}
SegForAvl.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.stage[i + 1][j * 2 - 1]
    end
  end
end
SegForAvl.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = i end
  self:updateAll()
end
SegForAvl.hold = function(self)
  local idx = self.stage[1][1]
  local ridx = idx
  self.stage[self.stagenum][idx] = false
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
  return ridx
end
SegForAvl.release = function(self, idx)
  self.stage[self.stagenum][idx] = idx
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
end
SegForAvl.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegForAvl})
  obj:create(n)
  return obj
end

local SegAvlTree = {}
SegAvlTree.makenode = function(self, val, parent)
  local i = self.seg:hold()
  self.v[i], self.u[i], self.p[i] = val, true, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  return i
end
SegAvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.seg = SegForAvl.new(n)
  -- value, leftCount, rightCount, left, right, parent, used
  self.v, self.lc, self.rc, self.l, self.r, self.p, self.u = {}, {}, {}, {}, {}, {}, {}
  for i = 1, n do
    self.v[i], self.u[i], self.p[i] = 0, false, 0
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  end
end

SegAvlTree.recalcCount = function(self, i)
  if self.u[i] then
    if self.u[self.l[i]] then self.lc[i] = 1 + mma(self.lc[self.l[i]], self.rc[self.l[i]])
    else self.lc[i] = 0
    end
    if self.u[self.r[i]] then self.rc[i] = 1 + mma(self.lc[self.r[i]], self.rc[self.r[i]])
    else self.rc[i] = 0
    end
  end
end
SegAvlTree.recalcCountAll = function(self, i)
  while self.u[i] do
    self:recalcCount(i)
    i = self.p[i]
  end
end

SegAvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.l[parent]] then self.p[self.l[parent]] = parent end
  if self.u[granp] then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.r[parent]] then self.p[self.r[parent]] = parent end
  if self.u[granp] then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val, 0) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, self.v[pos]) then
      if self.u[self.l[pos]] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        added = true
      end
    else
      if self.u[self.r[pos]] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        added = true
      end
    end
  end
  while self.u[pos] do
    local child, parent = pos, self.p[pos]
    if not self.u[parent] then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    end
  end
end

SegAvlTree.rmsub = function(self, node)
  while self.u[node] do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = nil
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        self:recalcCountAll(node)
        node = nil
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

SegAvlTree.pop = function(self)
  local node = self.root
  while self.u[self.l[node]] do
    node = self.l[node]
  end
  local v = self.v[node]
  if self.u[self.p[node]] then
    if self.u[self.r[node]] then self.p[self.r[node]] = self.p[node] end
    self.l[self.p[node]] = self.r[node]
    self:rmsub(self.p[node])
  else
    if self.r[node] then self.p[self.r[node]] = 0 end
    self.root = self.r[node]
  end
  self.seg:release(node)
  return v
end

SegAvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = SegAvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = SegAvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = SegAvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mma = math.max
local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegForAvl = {}
local segavl_mergefunc = function(x, y)
  if x and y then return mmi(x, y)
  elseif x then return x
  elseif y then return y
  else return false end
end
SegForAvl.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.stage[i + 1][j * 2 - 1]
    end
  end
end
SegForAvl.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = i end
  self:updateAll()
end
SegForAvl.hold = function(self)
  local idx = self.stage[1][1]
  local ridx = idx
  self.stage[self.stagenum][idx] = false
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = segavl_mergefunc(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
  return ridx
end
SegForAvl.release = function(self, idx)
  self.stage[self.stagenum][idx] = idx
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = segavl_mergefunc(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegForAvl.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegForAvl})
  obj:create(n)
  return obj
end

local SegAvlTree = {}
SegAvlTree.makenode = function(self, val, parent)
  local i = self.seg:hold()
  self.v[i], self.u[i], self.p[i] = val, true, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  return i
end
SegAvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.seg = SegForAvl.new(n)
  -- value, leftCount, rightCount, left, right, parent, used
  self.v, self.lc, self.rc, self.l, self.r, self.p, self.u = {}, {}, {}, {}, {}, {}, {}
  for i = 1, n do
    self.v[i], self.u[i], self.p[i] = 0, false, 0
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  end
end

SegAvlTree.recalcCount = function(self, i)
  if self.u[i] then
    if self.u[self.l[i]] then self.lc[i] = 1 + mma(self.lc[self.l[i]], self.rc[self.l[i]])
    else self.lc[i] = 0
    end
    if self.u[self.r[i]] then self.rc[i] = 1 + mma(self.lc[self.r[i]], self.rc[self.r[i]])
    else self.rc[i] = 0
    end
  end
end
SegAvlTree.recalcCountAll = function(self, i)
  while self.u[i] do
    self:recalcCount(i)
    i = self.p[i]
  end
end

SegAvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.l[parent]] then self.p[self.l[parent]] = parent end
  if self.u[granp] then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.r[parent]] then self.p[self.r[parent]] = parent end
  if self.u[granp] then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val, 0) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, self.v[pos]) then
      if self.u[self.l[pos]] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        added = true
      end
    else
      if self.u[self.r[pos]] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        added = true
      end
    end
  end
  while self.u[pos] do
    local child, parent = pos, self.p[pos]
    if not self.u[parent] then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    end
  end
end

SegAvlTree.rmsub = function(self, node)
  while self.u[node] do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = nil
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        self:recalcCountAll(node)
        node = nil
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

SegAvlTree.pop = function(self)
  local node = self.root
  while self.u[self.l[node]] do
    node = self.l[node]
  end
  local v = self.v[node]
  if self.u[self.p[node]] then
    if self.u[self.r[node]] then self.p[self.r[node]] = self.p[node] end
    self.l[self.p[node]] = self.r[node]
    self:rmsub(self.p[node])
  else
    if self.r[node] then self.p[self.r[node]] = 0 end
    self.root = self.r[node]
  end
  self.seg:release(node)
  return v
end

SegAvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = SegAvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = SegAvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = SegAvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)

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

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.

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 mma = math.max
local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local node = {}
  node.v, node.lc, node.rc = val, 0, 0
  node.p = parent
  -- node.l, node.r = nil, nil
  return node
end
AvlTree.create = function(self, lessthan)
  self.lessthan = lessthan
end

AvlTree.recalcCount = function(self, node)
  if node then
    if node.l then node.lc = 1 + mma(node.l.lc, node.l.rc)
    else node.lc = 0
    end
    if node.r then node.rc = 1 + mma(node.r.lc, node.r.rc)
    else node.rc = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, node)
  while node do
    self:recalcCount(node)
    node = node.p
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = parent.p
  child.r, parent.l = parent, child.r
  child.p, parent.p = granp, child
  if parent.l then parent.l.p = parent end
  if granp then
    if granp.l == parent then
      granp.l = child
    else
      granp.r = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = parent.p
  child.l, parent.r = parent, child.l
  child.p, parent.p = granp, child
  if parent.r then parent.r.p = parent end
  if granp then
    if granp.r == parent then
      granp.r = child
    else
      granp.l = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, pos.v) then
      if pos.l then
        pos = pos.l
      else
        pos.l = self:makenode(val, pos)
        pos = pos.l
        added = true
      end
    else
      if pos.r then
        pos = pos.r
      else
        pos.r = self:makenode(val, pos)
        pos = pos.r
        added = true
      end
    end
  end
  while pos do
    local child, parent = pos, pos.p
    if not parent then
      break
    end
    self:recalcCount(parent)
    if parent.l == child then
      if parent.lc - 1 == parent.rc then
        pos = parent
      elseif parent.lc - 2 == parent.rc then
        self:recalcCount(child)
        if child.lc - 1 == child.rc then
          self:rotR(child, parent)
        else
          local cr = child.r
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    else -- parent.r == child
      if parent.rc - 1 == parent.lc then
        pos = parent
      elseif parent.rc - 2 == parent.lc then
        self:recalcCount(child)
        if child.rc - 1 == child.lc then
          self:rotL(child, parent)
        else
          local cl = child.l
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while node do
    self:recalcCount(node)
    if node.lc == node.rc then
      node = node.p
    elseif node.lc + 1 == node.rc then
      self:recalcCountAll(node.p)
      node = nil
    else
      if node.r.lc == node.r.rc then
        self:rotL(node.r, node)
        self:recalcCountAll(node)
        node = nil
      elseif node.r.lc + 1 == node.r.rc then
        local nr = node.r
        self:rotL(nr, node)
        node = nr
      else
        local nrl = node.r.l
        self:rotR(nrl, node.r)
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local edge = self.root
  while edge.l do
    edge = edge.l
  end
  local v = edge.v
  if edge.p then
    if edge.r then edge.r.p = edge.p end
    edge.p.l = edge.r
    self:rmsub(edge.p)
  else
    if edge.r then edge.r.p = nil end
    self.root = edge.r
  end
  return v
end

AvlTree.new = function(lessthan)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mma = math.max
local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local node = {}
  node.v, node.lc, node.rc = val, 0, 0
  node.p = parent
  -- node.l, node.r = nil, nil
  return node
end
AvlTree.create = function(self, lessthan)
  self.lessthan = lessthan
end

AvlTree.recalcCount = function(self, node)
  if node then
    if node.l then node.lc = 1 + mma(node.l.lc, node.l.rc)
    else node.lc = 0
    end
    if node.r then node.rc = 1 + mma(node.r.lc, node.r.rc)
    else node.rc = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, node)
  while node do
    self:recalcCount(node)
    node = node.p
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = parent.p
  child.r, parent.l = parent, child.r
  child.p, parent.p = granp, child
  if parent.l then parent.l.p = parent end
  if granp then
    if granp.l == parent then
      granp.l = child
    else
      granp.r = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = parent.p
  child.l, parent.r = parent, child.l
  child.p, parent.p = granp, child
  if parent.r then parent.r.p = parent end
  if granp then
    if granp.r == parent then
      granp.r = child
    else
      granp.l = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, pos.v) then
      if pos.l then
        pos = pos.l
      else
        pos.l = self:makenode(val, pos)
        pos = pos.l
        added = true
      end
    else
      if pos.r then
        pos = pos.r
      else
        pos.r = self:makenode(val, pos)
        pos = pos.r
        added = true
      end
    end
  end
  while pos do
    local child, parent = pos, pos.p
    if not parent then
      break
    end
    self:recalcCount(child)
    self:recalcCount(parent)
    if parent.l == child then
      if parent.lc - 1 == parent.rc then
        pos = parent
      elseif parent.lc - 2 == parent.rc then
        if child.lc - 1 == child.rc then
          self:rotR(child, parent)
        else
          local cr = child.r
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    else -- parent.r == child
      if parent.rc - 1 == parent.lc then
        pos = parent
      elseif parent.rc - 2 == parent.lc then
        if child.rc - 1 == child.lc then
          self:rotL(child, parent)
        else
          local cl = child.l
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        self:recalcCountAll(child)
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while node do
    self:recalcCount(node)
    if node.lc == node.rc then
      node = node.p
    elseif node.lc + 1 == node.rc then
      self:recalcCountAll(node.p)
      node = nil
    else
      if node.r.lc == node.r.rc then
        self:rotL(node.r, node)
        self:recalcCountAll(node)
        node = nil
      elseif node.r.lc + 1 == node.r.rc then
        local nr = node.r
        self:rotL(nr, node)
        node = nr
      else
        local nrl = node.r.l
        self:rotR(nrl, node.r)
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local edge = self.root
  while edge.l do
    edge = edge.l
  end
  local v = edge.v
  if edge.p then
    if edge.r then edge.r.p = edge.p end
    edge.p.l = edge.r
    self:rmsub(edge.p)
  else
    if edge.r then edge.r.p = nil end
    self.root = edge.r
  end
  return v
end

AvlTree.new = function(lessthan)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mma = math.max
local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local node = {}
  node.v, node.lc, node.rc = val, 0, 0
  node.p = parent
  -- node.l, node.r = nil, nil
  return node
end
AvlTree.create = function(self, lessthan)
  self.lessthan = lessthan
end

AvlTree.recalcCount = function(self, node)
  if node then
    if node.l then node.lc = 1 + mma(node.l.lc, node.l.rc)
    else node.lc = 0
    end
    if node.r then node.rc = 1 + mma(node.r.lc, node.r.rc)
    else node.rc = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, node)
  while node do
    self:recalcCount(node)
    node = node.p
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = parent.p
  child.r, parent.l = parent, child.r
  child.p, parent.p = granp, child
  if parent.l then parent.l.p = parent end
  if granp then
    if granp.l == parent then
      granp.l = child
    else
      granp.r = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = parent.p
  child.l, parent.r = parent, child.l
  child.p, parent.p = granp, child
  if parent.r then parent.r.p = parent end
  if granp then
    if granp.r == parent then
      granp.r = child
    else
      granp.l = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, pos.v) then
      if pos.l then
        pos = pos.l
      else
        pos.l = self:makenode(val, pos)
        pos = pos.l
        added = true
      end
    else
      if pos.r then
        pos = pos.r
      else
        pos.r = self:makenode(val, pos)
        pos = pos.r
        added = true
      end
    end
  end
  self:recalcCountAll(pos)
  local resolve_child = pos
  while resolve_child do
    local child, parent = resolve_child, resolve_child.p
    resolve_child = nil
    if not parent then
      break
    end
    if parent.l == child then
      if parent.lc - 1 == parent.rc then
        resolve_child = parent
      elseif parent.lc - 2 == parent.rc then
        if child.lc - 1 == child.rc then
          self:rotR(child, parent)
        else
          local cr = child.r
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
      end
    else -- parent.r == child
      if parent.rc - 1 == parent.lc then
        resolve_child = parent
      elseif parent.rc - 2 == parent.lc then
        if child.rc - 1 == child.lc then
          self:rotL(child, parent)
        else
          local cl = child.l
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while node do
    self:recalcCount(node)
    if node.lc == node.rc then
      node = node.p
    elseif node.lc + 1 == node.rc then
      self:recalcCountAll(node.p)
      node = nil
    else
      if node.r.lc == node.r.rc then
        self:rotL(node.r, node)
        self:recalcCountAll(node)
        node = nil
      elseif node.r.lc + 1 == node.r.rc then
        local nr = node.r
        self:rotL(nr, node)
        node = nr
      else
        local nrl = node.r.l
        self:rotR(nrl, node.r)
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local edge = self.root
  while edge.l do
    edge = edge.l
  end
  local v = edge.v
  if edge.p then
    if edge.r then edge.r.p = edge.p end
    edge.p.l = edge.r
    self:rmsub(edge.p)
  else
    if edge.r then edge.r.p = nil end
    self.root = edge.r
  end
  return v
end

AvlTree.new = function(lessthan)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mfl, mce, mmi, mma = math.floor, math.ceil, math.min, math.max
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.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.addValue = function(self, idx, val)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + val
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(ary, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(ary, func, emptyvalue)
  return obj
end

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

local n = io.read("*n", "*l")
local a = {}
local str = io.read()
for v in str:gmatch("%d+") do
  table.insert(a, tonumber(v))
end
local value_list, value_to_idx = {}, {}
for i = 1, n * 2 do
  if not value_to_idx[a[i]] then
    value_to_idx[a[i]] = true
    table.insert(value_list, a[i])
  end
end
table.sort(value_list)
value_to_idx = {}
for i = 1, #value_list do value_to_idx[value_list[i]] = i end
local st = nil
local leftsum = 0
do
  local stary = {}
  for i = 1, #value_list do stary[i] = 0 end
  for i = 1, n do
    stary[value_to_idx[a[i]]] = stary[value_to_idx[a[i]]] + 1
    leftsum = leftsum + a[i]
  end
  st = SegTree.new(stary, function(a,b)return a+b end, 0)
end
leftsum = {leftsum}
for i = 1, n do
  st:addValue(value_to_idx[a[n + i]], 1)
  local pos = st:takeLeft()
  leftsum[i + 1] = leftsum[i] + a[n + i] - value_list[pos]
end
-- r
value_list, value_to_idx = {}, {}
for i = n + 1, n * 3 do
  if not value_to_idx[-a[i]] then
    value_to_idx[-a[i]] = true
    table.insert(value_list, -a[i])
  end
end
table.sort(value_list)
value_to_idx = {}
for i = 1, #value_list do value_to_idx[value_list[i]] = i end
local st = nil
local rightsum = 0
do
  local stary = {}
  for i = 1, #value_list do stary[i] = 0 end
  for i = 2 * n + 1, n * 3 do
    stary[value_to_idx[-a[i]]] = stary[value_to_idx[-a[i]]] + 1
    rightsum = rightsum - a[i]
  end
  st = SegTree.new(stary, function(a,b)return a+b end, 0)
end
rightsum = {rightsum}
for i = n, 1, -1 do
  st:addValue(value_to_idx[-a[n + i]], 1)
  local pos = st:takeLeft()
  rightsum[n + 2 - i] = rightsum[n + 1 - i] - a[n + i] - value_list[pos]
end
local ret = leftsum[1] + rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] + rightsum[n + 2 - i])
end
print(ret)


s=io.read()
large=string.len(s)
numa = string.find(s, "A")
numz = large - string.find(string.reverse(s),"Z")
print(numz-numa+2)

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


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

local A_index = s:find("A")
local Z_index = s:len() - s:reverse():find("Z") + 1

print(Z_index - A_index + 1)


local str = io.read("*l")
local a, z = 0, 0
local flag = false
for i = 1, #str do
  local char = string.sub(str, i, i)
  if flag == false and char == "A" then
    flag = true
    a = i
  elseif char == "Z" then
    z = i
  end
end
local ans = z - a + 1
print(ans)


local str = io.read("*l")
local a, z = 0, 0
local flag = false
for i = 1, #str do
  local char = string.sub(str, i, i)
  if flag == false and char == "a" then
    flag = true
    a = i
  elseif char == "z" then
    z = i
  end
end
local ans = z - a + 1
print(ans)


local s = io.read()
local a = s:find("A")
local _, zr = s:reverse():find("Z")
local z = #s-(zr-1)
print(z-a+1)


local s = io.read()
local left = s:find("A")
local right = s:find("Z")
local c = right + 1
while c do
  c = s:find("Z", c)
  if c then
    right = c
    c = c + 1
  end
end
print(right - left + 1)


s = io.read("*n")
count = 0
ret = 0
start = false
for i = 1, #s do
	c = string.sub(s, i, i)
	if start then
		count = count + 1
		if  c == "Z" then
			ret = count
		end
	elseif c == "A" then
		start = true
		count = 1
	end
end
print(ret)


s = io.read("*l")
count = 0
ret = 0
start = false
for i = 1, #s do
	c = string.sub(s, i, i)
	if start then
		count = count + 1
		if  c == "Z" then
			ret = count
		end
	elseif c == "A" then
		start = true
		count = 1
	end
end
print(ret)


s = io.read("*l")
count = 0
ret = 0
start = false
for i = 1, #s do
	c = string.sub(s, i, i)
	if start then
		count = count + 1
		if  c == "Z" then
			ret = count
		end
	elseif c == "A" then
		start = true
		count = 1
	end
end


local str = io.read()
local spos = string.find(str, "A")
local epos

if not spos then
	print(0)
end

for i=string.len(str), spos + 1, -1 do
	local v = string.sub(str, i, i)
	if v == "Z" then
		epos = i
		break
	end
end

if not epos or (spos > epos) then
	print(0)
end

print(epos - spos + 1)


local str = io.read()
local spos = string.find(str, "A")
local epos

if not spos then
	return 0
end

for i=string.len(str), spos + 1, -1 do
	local v = string.sub(str, i, i)
	if v == "Z" then
		epos = i
		break
	end
end

if not epos or (spos > epos) then
	return 0
end

return epos - spos + 1


local str = io.read()
local spos = string.find(str, "A")
local epos

for i=string.len(str), spos + 1, -1 do
	local v = string.sub(str, i, i)
	if v == "Z" then
		epos = i
		break
	end
end

if not epos then
	return 0
end

return epos - spos + 1


local str = io.read("*a")
local spos = string.find(str, "A")
local epos

for i=string.len(str),spos + 1, -1 do
	local v = string.sub(str, i, i)
	if v == "Z" then
		epos = i
		break
	end
end

if not epos then
	return 0
end

return epos - spos + 1


s = io.read()
count = 0
ret = 0
start = false
for i = 1, #s do
	c = string.sub(s, i, i)
	if start then
		count = count + 1
		if  c == "Z" then
			ret = count
		end
	elseif c == "A" then
		start = true
		count = 1
	end
end

print(ret)


local s = io.read()

local function getAAA(str)
	local len = string.len(str)
	local p = string.find(str, "A")
	if not p then
		return 0
	end
	local p2 = string.find(str, "Z[[a-y]*[A-Y]*$")
	if not p2 then
		return 0
	end
	local n = len - p - (len - p2) + 1
	return n
end

local n = getAAA(s)
print(n)


local str = io.read("*l")
local reverse = string.reverse(str)
local len = string.len(str)
local apos = string.find(str,"A")
local zpos = len - string.find(reverse,"Z") + 1
print(zpos - apos +1)


local k,s=io.read("*n","*n")
local counter=0
for x=0,k do
    for y=0,k do
        local z=s-x-y
        counter=counter+(k>=z and z>=0 and 1 or 0)
    end
end
print(counter)

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


local k,s=io.read("n","n")
local counter=0
for x=0,k do
    for y=0,k do
        local z=s-x-y
        counter=counter+(k>=z and z>=0 and 1 or 0)
    end
end
print(counter)


local K, S = io.read("*n", "*n")

local ans = 0

for x = 0, K do
	for y = 0, K do
		local z = S - x -y
		if (0 <= z and z <= K) then
			ans = ans + 1
		end
	end
end

print(ans)


local K, S = io.read("*n", "*n")

---x >= y >= z
local x, y, z = 0, 0, 0
if K >= S then
	x = S
elseif 2*K >= S then
	x, y = K, S - K
else
	x, y, z = K, K, S - 2*K
end

local count = 0
local sub_x, sub_y, sub_z = x, y, z
while sub_y >= sub_z do
	while y >= z do
		if x == y and y == z then
			count = count + 1
		elseif x == y or y == z then
			count = count + 3
		else
			count = count + 6
		end
		y, z = y - 1, z + 1
	end

	if sub_x == sub_y + 1 then
		sub_x, sub_z = sub_x - 1, sub_z + 1
	elseif x == sub_y then
		sub_x, sub_y, sub_z = sub_x - 1, sub_y - 1, sub_z + 2
	else
		sub_x, sub_y = sub_x - 1, sub_y + 1
	end
	x, y, z = sub_x, sub_y, sub_z
end

print(count)


local K, S = io.read("*n", "*n")

---x >= y >= z
local x, y, z = 0, 0, 0
if K >= S then
	x = S
elseif 2*K >= S then
	x, y = K, S - K
else
	x, y, z = K, K, S - 2*K
end

local count = 0
local sub_y, sub_z = y, z
while x >= y and y >= z do
	while y >= z do
		if x == y and y == z then
			count = count + 1
		elseif x == y or y == z then
			count = count + 3
		else
			count = count + 6
		end
		y, z = y - 1, z + 1
	end
	y, z = sub_y, sub_z
	if x == y + 1 then
		x, z = x - 1, z + 1
	elseif x == y then
		x, y, z = x - 1, y - 1, z + 2
	else
		x, y = x - 1, y + 1
	end
	sub_y, sub_z = y, z
end

print(count)


local mfl = math.floor
local TranFFT = {}

TranFFT.initialize = function(self)
  self.size = 18
  -- 2^18
  self.n = 262144
  -- 1007681537: prime,
  -- 1007681537 % 262144 = 1
  self.mod = 1007681537
  -- (6161^262144) % mod = 1
  self.w = 6161
  -- (1007677693 * 262144) % mod = 1
  self.ninv = 1007677693
  -- (534835031 * 6161) % mod = 1
  self.winv = 534835031
  self.p2 = {1}
  for i = 2, self.size do
    self.p2[i] = self.p2[i - 1] * 2
  end
  self.binv = {}
  for i = 1, self.n do
    local y, z = 0, i - 1
    for j = 1, self.size do
      y = y + (z % 2) * self.p2[self.size + 1 - j]
      z = mfl(z / 2)
    end
    self.binv[i] = y + 1
  end
  self.wmul = {1}
  for i = 2, self.n do
    self.wmul[i] = self:mul(self.wmul[i - 1], self.w)
  end
  self.winvmul = {1}
  for i = 2, self.n do
    self.winvmul[i] = self:mul(self.winvmul[i - 1], self.winv)
  end
end

-- (44893^2) % 1007681537 = 18375
-- 1007681537 = 22446 * 44893 + rem(13259)
-- x0, y0 <= 44892
-- x1, y1 <= 22446
-- max(x1y1*18375+(x1y0+x0y1)*44893+x0y0) < 10^14
TranFFT.mul = function(self, x, y)
  local x0, y0 = x % 44893, y % 44893
  local x1, y1 = mfl(x / 44893), mfl(y / 44893)
  return (x1 * y1 * 18375 + (x1 * y0 + x0 * y1) * 44893 + x0 * y0) % self.mod
end

TranFFT.add = function(self, x, y) return (x + y) % self.mod end

TranFFT.fft_common = function(self, ary, wmul)
  local ret = {}
  for i = 1, self.n do
    ret[i] = ary[self.binv[i]]
  end
  for i = 1, self.size do
    local step_size = self.p2[i]
    local step_count = self.p2[self.size + 1 - i]
    for istep = 1, step_count do
      local ofst = (istep - 1) * step_size * 2
      for j = 1, step_size do
        local a1, a2 = ret[ofst + j], ret[ofst + step_size + j]
        ret[ofst + j] = self:add(a1, self:mul(a2, wmul[1 + (j - 1) * step_count]))
        ret[ofst + step_size + j] = self:add(a1, self:mul(a2, wmul[1 + (j + step_size - 1) * step_count]))
      end
    end
  end
  return ret
end

TranFFT.fft = function(self, ary)
  return self:fft_common(ary, self.wmul)
end
TranFFT.ifft = function(self, ary)
  local ret = self:fft_common(ary, self.winvmul)
  for i = 1, self.n do
    ret[i] = self:mul(ret[i], self.ninv)
  end
  return ret
end

TranFFT:initialize()
local a = {}
for i = 1, TranFFT.n do
  a[i] = 0
end
local k, s = io.read("*n", "*n")
for i = 1, k + 1 do
  a[i] = 1
end

local at = TranFFT:fft(a)
for i = 1, TranFFT.n do
  local z = at[i]
  at[i] = TranFFT:mul(z, TranFFT:mul(z, z))
end
local b = TranFFT:ifft(at)
print(b[s + 1])


print(io.read():gsub("erase(r?)", ""):gsub("dream[er]?","")=="" and "YES" or "NO")

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


print(io.read():gsub("erase(r-)",""):gsub("dream(e-)(r-)","")=="" and "YES" or "NO")


print(io.read():gsub("erase(r-)",""):gsub("dream[er]-","")=="" and "YES" or "NO")


print(io.read():gsub("erase(r*)",""):gsub("dream[er]*","")=="" and "YES" or "NO")


print(io.read():gsub("[ed]r[ae]a*[sm]e*r*", "")=="" and "YES" or "NO")


print(io.read():gsub("eraser",""):gsub("erase",""):gsub("dreamer",""):gsub("dream","")=="" and "YES" or "NO")


local s=io.read()
local daydream={"dream","dreamer","erase","eraser"}

while true do
    local flag=false
    for j=1,4 do
        local l=#s-#daydream[j]
        if s:sub(l+1)==daydream[j] then
            s=s:sub(1,l)
            flag=true
        end
    end
    if not flag then
        print("NO")
        break
    elseif s=="" then
        print("YES")
        break
    end
end


local long_str = io.read("*l")
local tmps = {"dream", "dreamer", "erase", "eraser"}

long_str = long_str:reverse()
for i = 1, #tmps do
    tmps[i] = string.reverse(tmps[i])
end

local index = 1
local is_none = false
while index < long_str:len() do
    local old_index = index
    for i = 1, #tmps do
        local tmp = tmps[i]
        if long_str:sub(index, index + string.len(tmp) - 1) == tmp then
            index = index + string.len(tmp)
            break
        end
    end
    if old_index == index then
        is_none = true
        break
    end
end
print(is_none and "NO" or "YES")


local long_str = io.read("*l")
local tmps = {"dream", "dreamer", "erase", "eraser"}

long_str = long_str:reverse()
for i = 1, #tmps do
    tmps[i] = string.reverse(tmps[i])
end

local index = 1
local is_none = false
while index < long_str:len() do
    local old_index = index
    for i = 1, #tmps do
        local tmp = tmps[i]
        if long_str:sub(index, index + string.len(tmp) - 1) == tmp then
            index = index + string.len(tmp)
            break
        end
    end
    if old_index == index then
        is_none = true
        break
    end
end
print(is_none and "No" or "Yes")


s = io.read()
s = s:gsub("eraser", "")
s = s:gsub("erase", "")
s = s:gsub("dreamer", "")
s = s:gsub("dream", "")
print(s == "" and "YES" or "NO")


s = io.read()
s = s:gsub("eraser", "")
s = s:gsub("dreamer", "")
s = s:gsub("dream", "")
s = s:gsub("erase", "")
print(s == "" and "YES" or "NO")


s = io.read()
s = string.gsub(s, "eraser", "")
s = string.gsub(s, "erase", "")
s = string.gsub(s, "dreamer", "")
s = string.gsub(s, "dream", "")
print(string.len(s) == 0 and "YES" or "NO")


local t
do
  local _accum_0 = { }
  local _len_0 = 1
  local _list_0 = {
    "dream",
    "dreamer",
    "erase",
    "eraser"
  }
  for _index_0 = 1, #_list_0 do
    local e = _list_0[_index_0]
    _accum_0[_len_0] = e:reverse()
    _len_0 = _len_0 + 1
  end
  t = _accum_0
end
local s = io.read("*l"):reverse()
local c = 1
local l = s:len()
while c <= l do
  local k = nil
  for _index_0 = 1, #t do
    local e = t[_index_0]
    if s:sub(c, c + e:len() - 1) == e then
      k = e
      break
    end
  end
  if k == nil then
    print("NO")
    return
  end
  c = c + k:len()
end
return print("YES")


local a, b = {}, {}
for i = 1, 6 do a[i] = io.read("*n") / 100 end
for i = 1, 6 do b[i] = io.read("*n") / 100 end
local red, blue = 0, 0
for i = 1, 6 do
  local r = a[i] / (a[i] + b[i])
  r = math.max(r, 1 - r)
  red = red + a[i] * r
  blue = blue + b[i] * r
end
print(string.format("%.10f", math.min(red, blue)))

Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are two (6-sided) dice: a red die and a blue die.
When a red die is rolled, it shows i with probability p_i percents, and when a blue die is rolled, it shows j with probability q_j percents.

Petr and tourist are playing the following game.
Both players know the probabilistic distributions of the two dice.
First, Petr chooses a die in his will (without showing it to tourist), rolls it, and tells tourist the number it shows.
Then, tourist guesses the color of the chosen die.
If he guesses the color correctly, tourist wins. Otherwise Petr wins.

If both players play optimally, what is the probability that tourist wins the game?

Constraints

0 ≤ p_i, q_i ≤ 100

p_1 + ... + p_6 = q_1 + ... + q_6 = 100

All values in the input are integers.

Input

The input is given from Standard Input in the following format:

p_1 p_2 p_3 p_4 p_5 p_6
q_1 q_2 q_3 q_4 q_5 q_6

Output

Print the probability that tourist wins.
The absolute error or the relative error must be at most 10^{-9}.

Sample Input 1

25 25 25 25 0 0
0 0 0 0 50 50

Sample Output 1

1.000000000000

tourist can always win the game: If the number is at most 4, the color is definitely red. Otherwise the color is definitely blue.

Sample Input 2

10 20 20 10 20 20
20 20 20 10 10 20

Sample Output 2

0.550000000000


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

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


local s = io.read()
local n = #s
local cur = s:sub(1, 1)
local cnt = 0
for i = 2, n do
  if cur ~= s:sub(i, i) then
    cnt = cnt + 1
    cur = s:sub(i, i)
  end
end
print(cnt)


s = io.read("*l")
l = string.len(s)
t = {string.byte(s, 1, -1)}
ans = 0
for i = 1, l-1 do
	if t[i] ~= t[i+1] then
		ans = ans + 1
	end
end
print(ans)


s = io.read("*l")
l = string.len(s)
t = {string.byte(s, 1, -1)}
ans = 0
for i = 1, l-1 do
	print(i, " ", t[i])
	if t[i] ~= t[i+1] then
		ans = ans + 1
	end
end
print(ans)


l = io.read("*l")
t = {string.byte(l, 1, -1)}
i, ans = 1, 0

for j = 2, string.len(l) do
	if t[i] ~= t[j] then
		ans = ans + 1
	end
	i = i + 1
end
print(ans)


t = {string.byte(io.read("*l"), 1, -1)}
i, ans = 1, 0
for j = 2, table.maxn(t) do
	if t[i] ~= t[j] then
		ans = ans + 1
	end
	i = i + 1
end
print(ans)


local n, l = io.read("*n", "*n", "*l")
local t = {}
for i = 1, n do
  t[i] = io.read()
end
table.sort(t)
print(table.concat(t))

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


local read = io.read
local byte = string.byte
local N, L = read("*n", "*n")
read("*l")
local S = {}
for i = 1, N do
	local str = read("*l")
	S[i] = {byte(str, 1, L)}
	setmetatable(S[i], {__call = function() return str end})
end

local function isDict(s1, s2)
	for i = 1, L do
		if s1[i] < s2[i] then
			return true
		elseif s1[i] > s2[i] then
			return false
		end
	end
	return false
end

table.sort(S, isDict)
local out = ""
for i = 1, N do
	out = out..S[i]()
end

print(out)


n,l=io.read("*n","*n","*l")
s={}
for i=1,n do
    table.insert(s,io.read())
end
table.sort(s)
answer=table.concat(s,"")
print(answer)


N,L=io.read("n","n","l")
S={}
for i=1,N do S[i]=io.read() end
table.sort(S)
print(table.concat(S,""))


a,b= io.read("*n", "*n", "*l")
s ={}
for i = 1, a do
  s[i] = io.read()
end
table.sort(s)
for i = 1, a do
  io.write(s[i])
end
io.write("\n")