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

local function uf_findroot(idx)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

for i = 1, m do
  local a, b = io.read("*n", "*n")
  local ra, rb = uf_findroot(a), uf_findroot(b)
  parent[rb], parent[b] = ra, ra
end
local groups = {}
local g1 = uf_findroot(1)
local g2 = uf_findroot(2)
groups[g1] = 1
groups[g2] = 1
for i = 3, n do
  local g = uf_findroot(i)
  if not groups[g] then groups[g] = 1
  else groups[g] = groups[g] + 1
  end
end
local c1, c2 = groups[g1], groups[g2]
local rem = 0
for k, v in pairs(groups) do
  if k ~= g1 and k ~= g2 then
    rem = rem + v
  end
end
if c1 < c2 then
  c2 = c2 + rem
else
  c1 = c1 + rem
end
print(math.floor(c1 * (c1 - 1) / 2) + math.floor(c2 * (c2 - 1) / 2) - m)

Score : 100 points

Problem Statement

You are given an undirected graph G. G has N vertices and M edges. The vertices are numbered from 1 through N, and the i-th edge (1 ≤ i ≤ M) connects Vertex a_i and b_i. G does not have self-loops and multiple edges.

You can repeatedly perform the operation of adding an edge between two vertices. However, G must not have self-loops or multiple edges as the result. Also, if Vertex 1 and 2 are connected directly or indirectly by edges, your body will be exposed to a voltage of 1000000007 volts. This must also be avoided.

Under these conditions, at most how many edges can you add? Note that Vertex 1 and 2 are never connected directly or indirectly in the beginning.

Constraints

2 ≤ N ≤ 10^5

0 ≤ M ≤ 10^5

1 ≤ a_i < b_i ≤ N

All pairs (a_i, b_i) are distinct.

Vertex 1 and 2 in G are not connected directly or indirectly by edges.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print the maximum number of edges that can be added.

Sample Input 1

4 1
1 3

Sample Output 1

2

As shown above, two edges can be added. It is not possible to add three or more edges.

Sample Input 2

2 0

Sample Output 2

0

No edge can be added.

Sample Input 3

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

Sample Output 3

12


function s1(c)
  if c == "A" then
    return s2
  elseif c == "K" then
    return s3
  else
    return nil
  end
end
function s2(c)
  if c == "K" then
    return s3
  else
    return nil
  end
end
function s3(c)
  if c == "I" then
    return s4
  else
    return nil
  end
end
function s4(c)
  if c == "H" then
    return s5
  else
    return nil
  end
end
function s5(c)
  if c == "A" then
    return s6
  elseif c == "B" then
    return s7
  else
    return nil
  end
end
function s6(c)
  if c == "B" then
    return s7
  else
    return nil
  end
end
function s7(c)
  if c == "A" then
    return s8
  elseif c == "R" then
    return s9
  else
    return nil
  end
end
function s8(c)
  if c == "R" then
    return s9
  else
    return nil
  end
end
function s9(c)
  if c == "A" then
    return s10
  elseif c == "\n" then
    print("YES")
    os.exit(0)
  else
    return nil
  end
end
function s10(c)
  if c == "\n" then
    print("YES")
    os.exit(0)
  else
    return nil
  end
end

local s = s1
while true do
  local c = io.read(1)
  s = s(c)
  if not s then
    print("NO")
    break
  end
end

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

Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

1 \leq |S| \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.

Sample Input 3

AAKIAHBAARA

Sample Output 3

NO

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 s = io.read()
local n = #s
local pos = 1
local f = false

pos = s:find("KIH", pos)
if pos == 1 or (pos == 2 and s:sub(1, 1) == "A") then
  pos = pos + 3
  local bpos = s:find("B", pos)
  if bpos and bpos < n then
    if bpos == pos or (bpos == pos + 1 and s:sub(pos, pos) == "A") then
      local rpos = s:find("R", bpos)
      if rpos then
        if rpos == bpos + 1 or (rpos == bpos + 2 and s:sub(bpos + 1, bpos + 1) == "A") then
          if rpos == n or (rpos == n - 1 and s:sub(n, n) == "A") then
            f = true
          end
        end
      end
    end
  end
end

print(f and "YES" or "NO")


local s = io.read()
local pos = 1
local f = false
while pos do
  pos = s:find("KIH", pos)
  if not pos then break end
  local bpos = s:find("B", pos)
  if bpos then
    if bpos == pos + 3 or (bpos == pos + 4 and s:sub(pos + 3) == "A") then
      local rpos = s:find("R", bpos)
      if rpos then
        if rpos == bpos + 1 or (rpos == bpos + 2 and s:sub(bpos + 1) == "A") then
          f = true
          break
        end
      end
    end
  end
  pos = pos + 1
end
print(f and "YES" or "NO")


local mmi, mma, mab, mfl = math.min, math.max, math.abs, math.floor
local function solve()
  local n = io.read("*n")
  local iszero = false
  local cnt = {}
  for i = 1, 12 do cnt[i] = 0 end
  for i = 1, n do
    local d = io.read("*n")
    if d == 0 then iszero = true
    else
      cnt[d] = cnt[d] + 1
      if 3 <= cnt[d] then iszero = true end
    end
  end
  if iszero then return 0 end
  local fix, float = {}, {}
  local minval = 12
  for i = 12, 1, -1 do
    if 0 < cnt[i] then
      minval = i
    end
    if cnt[i] == 1 then
      table.insert(float, i)
    elseif cnt[i] == 2 then
      table.insert(fix, i)
    end
  end
  --fix_fix
  for i = 1, #fix - 1 do
    for j = i + 1, #fix do
      minval = mmi(minval, fix[i] - fix[j])
    end
  end
  for i = 1, #fix do
    minval = mmi(minval, fix[i] * 2)
    minval = mmi(minval, (12 - fix[i]) * 2)
  end
  --fix_float
  for i = 1, #fix do
    for j = 1, #float do
      minval = mmi(minval, mab(fix[i] - float[j]))
    end
  end
  --float_float
  if 0 < #float then
    local mul = 1
    local z = {}
    for i = 1, #float do mul = mul * 2 end
    local partmax = 0
    for i = 0, mul - 1 do
      local partmin = 12
      for j = 1, #float do
        if i % 2 == 0 then
          z[j] = float[j]
        else
          z[j] = -float[j]
        end
        i = mfl(i / 2)
      end
      for j = 1, #float - 1 do
        for k = j + 1, #float do
          local v = mab(z[j] - z[k])
          v = mmi(v, 24 - v)
          partmin = mmi(partmin, v)
        end
      end
      partmax = mma(partmax, partmin)
    end
    minval = mmi(minval, partmax)
  end
  return minval
end
print(solve())

Score : 500 points

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.
Here, we are using 24-hour notation.
That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

Find the maximum possible value of s.

Constraints

1 \leq N \leq 50

0 \leq D_i \leq 12

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the maximum possible value of s.

Sample Input 1

3
7 12 8

Sample Output 1

4

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.

Sample Input 2

2
11 11

Sample Output 2

2

Sample Input 3

1
0

Sample Output 3

0

Note that Takahashi himself is also a participant.


local s = io.read()
if s:sub(1,1)==s:sub(2,2) and s:sub(1,1)==s:sub(3,3) then
  print("Yes")
elseif s:sub(4,4)==s:sub(2,2) and s:sub(4,4)==s:sub(3,3) then
  print("Yes")
else
  print("No")
end

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


N=io.read("n")
function lucas(n)
  local L={}
  L[0]=2
  L[1]=1
  if n>=2 then
    return lucas(n-1)+lucas(n-2)
  else
    return L[n]
  end
end
print(lucas(N))

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


x,_,y=io.read(1,1,1)
print(x>y and'>'or x<y and'<' or '=')

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


a,b=io.read():match("(.+)%s(.+)")
print(a==b and"="or a>b and">"or"<")


function main()
    local input_itr = io.read():gmatch("%d+")
    local m = tonumber(input_itr())
    local n = tonumber(input_itr())

    print((1900*n + 100*(m-n))*math.pow(2, n))
end

main()

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


local n, z, w = io.read("*n", "*n", "*n")
local a = {}
for i = 1, n do a[i] = io.read("*n") end
if n == 1 then
  print(math.abs(w - a[1]))
else
  print(math.max(math.abs(w - a[n]), math.abs(a[n - 1] - a[n])))
end

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


N=io.read("n")
Z=io.read("n")
W=io.read("n")
a={}
b={}
for i=1,N do
  a[i]=io.read("n")
  b[i]=a[i]
end
table.sort(b)
t={}
t[1]=math.abs(a[N]-W)
if b[1]==a[N] then
	t[2]=math.abs(b[2]-a[N])
  else
  t[2]=math.abs(b[1]-a[N])
end
if b[N]==a[N] then
  t[3]=math.abs(b[N-1]-a[N])
  else
  t[3]=math.abs(b[N]-a[N])
end
table.sort(t)
print(t[3])


N=io.read("n")
Z=io.read("n")
W=io.read("n")
a={}
b={}
for i=1,N do
  a[i]=io.read("n")
  b[i]=a[i]
end
table.sort(b)
t={}
t[1]=math.abs(a[N]-W)
t[2]=math.abs(b[1]-a[N])
t[3]=math.abs(b[N]-a[N])
table.sort(t)
print(t[3])


action_x = nil
do
    local memo = {}
    action_x = function(tbl, left_i, w)
        if not memo[left_i] then
            memo[left_i] = {}
        elseif memo[left_i][w] then
            return memo[left_i][w]
        end

        local max_abs = math.abs(tbl[#tbl] - w)

        for i = left_i, #tbl - 1 do
            local diff = action_y(tbl, i + 1, tbl[i])
            if diff > max_abs then
                max_abs = diff
            end
        end
        memo[left_i][w] = max_abs
        return max_abs
    end
end

action_y = nil
do
    local memo = {}
    action_y = function(tbl, left_i, z)
        if not memo[left_i] then
            memo[left_i] = {}
        elseif memo[left_i][w] then
            return memo[left_i][z]
        end

        local min_abs = math.abs(tbl[#tbl] - z)

        for i = left_i, #tbl - 1 do
            local diff = action_x(tbl, i + 1, tbl[i])
            if diff < min_abs then
                min_abs = diff
            end
        end
        memo[left_i][z] = min_abs
        return min_abs
    end
end

function main()
    local nzw_input = string.gmatch(io.read(), "%d+")
    local n = tonumber(nzw_input())
    local z = tonumber(nzw_input())
    local w = tonumber(nzw_input())
    local a = {}
    for i in string.gmatch(io.read(), "%d+") do
        table.insert(a, tonumber(i))
    end

    if #a == 1 then
        return math.abs(a[1] - w)
    end

    return action_x(a, 1, w)
end

print(main())


local mfl = math.floor
local mmi, mma = math.min, math.max
local MPM = {}

MPM.initialize = function(self, n, spos, tpos)
  self.n = n
  self.spos, self.tpos = spos, tpos
  -- edge_dst[src][i] := dst
  self.edge_dst = {}
  -- edge_cap[src][i] := capacity from src to edge_dst[src][i]
  self.edge_cap = {}
  -- edge_dst_invedge_idx[src][i] := "j" where edge_dst[dst][j] == src
  -- in this case, edge_dst_invedge_idx[dst][j] should be "i".
  self.edge_dst_invedge_idx = {}
  -- level[v] := length from spos. level[spos] := 1
  self.level = {}
  -- level_vertex_count[i] := count of vertexes that levels are i
  self.level_vertex_count = {}
  -- sub_graph_v[i] := list of vertexes that are contained in the sub-graph.
  self.sub_graph_v = {}
  -- sub_graph_size := the size of sub_graph_v.
  -- may not equal to #sub_graph_v (because not cleared).
  self.sub_graph_size = 0
  -- sub_graph_flag[v] := whether to contains the vertex v in the sub-graph or not
  self.sub_graph_flag = {}
  -- send[v] := sum of sendable amount from v to other vertexes in the sub-graph
  self.send = {}
  -- receive[v] := sum of receivable amount toward v from other vertexes in the sub-graph
  self.receive = {}
  -- sub_edge_idxes[src][i] := edge (from v) using in the sub-graph.
  -- for example, if sub_edge_idxes[src][i] is j,
  -- the edge from src to dst (= edge_dst[src][j]) contains in the sub-graph.
  self.sub_edge_idxes = {}
  -- sub_edge_cnt[src] := the size of sub_edge_idxes[src].
  -- may not equal to #sub_edge_idxes[src] (because not cleared).
  self.sub_edge_cnt = {}
  -- sub_invedge_idxes[dst] := edge (to dst) using in the sub-graph.
  -- for example, if sub_invedge_idxes[dst][i] is j,
  -- the src is edge_dst[dst][j], and the edge index (from src to dst) is k := edge_dst_invedge_idx[dst][j].
  -- so edge_dst[src][k] is the edge from src to dst using in the sub-graph.
  self.sub_invedge_idxes = {}
  -- sub_invedge_cnt[dst] := the size of sub_invedge_idxes[dst].
  -- may not equal to #sub_invedge_idxes[dst] (because not cleared).
  self.sub_invedge_cnt = {}
  -- flow_route[v] := [for "flowToT"] whether to contain in the route from weak_vertex to tpos.
  self.flow_route = {}
  -- actual_flow_amount[v] := [for "flowToT"] send amount from v
  self.actual_flow_amount = {}
  for i = 1, n do
    self.edge_dst[i] = {}
    self.edge_cap[i] = {}
    self.edge_dst_invedge_idx[i] = {}
    self.level[i] = 0
    self.level_vertex_count[i] = 0
    self.sub_graph_flag[i] = false
    self.send[i] = 0
    self.receive[i] = 0
    self.sub_edge_idxes[i] = {}
    self.sub_edge_cnt[i] = 0
    self.sub_invedge_idxes[i] = {}
    self.sub_invedge_cnt[i] = 0
    self.flow_route[i] = false
    self.actual_flow_amount[i] = 0
  end
end

MPM.addEdge = function(self, src, dst, cap, invcap)
  if not invcap then invcap = 0 end
  table.insert(self.edge_dst[src], dst)
  table.insert(self.edge_cap[src], cap)
  table.insert(self.edge_dst_invedge_idx[src], 1 + #self.edge_dst[dst])
  table.insert(self.edge_dst[dst], src)
  table.insert(self.edge_cap[dst], invcap)
  table.insert(self.edge_dst_invedge_idx[dst], #self.edge_dst[src])
end
MPM.makeSubGraph = function(self)
  local inf = self.n + 2
  local level, sub_graph_flag = self.level, self.sub_graph_flag
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local send, receive = self.send, self.receive
  local sub_graph_v = self.sub_graph_v
  local sub_edge_idxes, sub_edge_cnt = self.sub_edge_idxes, self.sub_edge_cnt
  local sub_invedge_idxes, sub_invedge_cnt = self.sub_invedge_idxes, self.sub_invedge_cnt
  local level_vertex_count = self.level_vertex_count
  for i = 1, self.n do
    level[i] = inf
    sub_graph_flag[i] = false
    send[i], receive[i] = 0, 0
    sub_edge_cnt[i] = 0
    sub_invedge_cnt[i] = 0
    level_vertex_count[i] = 0
  end
  -- BFS
  level[self.spos] = 1
  local taskcnt, done = 1, 0
  sub_graph_v[1] = self.spos
  local reached = false
  while done < taskcnt do
    done = done + 1
    local src = sub_graph_v[done]
    if src == self.tpos then reached = true break end
    for i = 1, #edge_dst[src] do
      local cap = edge_cap[src][i]
      if 0 < cap then
        local dst = edge_dst[src][i]
        if level[dst] == inf then
          level[dst] = level[src] + 1
          taskcnt = taskcnt + 1
          sub_graph_v[taskcnt] = dst
        elseif level[dst] == level[src] + 1 then
        end
      end
    end
  end
  if not reached then
    self.sub_graph_size = 0
    return false
  end
  -- restore route
  sub_graph_flag[self.tpos] = true
  local curlevel = level[self.tpos]
  while curlevel == level[sub_graph_v[taskcnt]] do
    taskcnt = taskcnt - 1
  end
  for isrc = taskcnt, 1, -1 do
    local src = sub_graph_v[isrc]
    for i = 1, #edge_dst[src] do
      local dst, cap = edge_dst[src][i], edge_cap[src][i]
      if 0 < cap and sub_graph_flag[dst]
      and level[dst] == level[src] + 1 then
        sub_graph_flag[src] = true
        local edgecnt = sub_edge_cnt[src] + 1
        sub_edge_cnt[src] = edgecnt
        sub_edge_idxes[src][edgecnt] = i
        sub_invedge_cnt[dst] = sub_invedge_cnt[dst] + 1
        sub_invedge_idxes[dst][sub_invedge_cnt[dst]] = edge_dst_invedge_idx[src][i]
        send[src] = send[src] + cap
        receive[dst] = receive[dst] + cap
      end
    end
    if not sub_graph_flag[src] then
      for i = 1, #edge_dst[src] do
        local dst = edge_dst[src][i]
        local cap = edge_cap[src][i]
        if 0 < cap and level[dst] == level[src] + 1 then
          send[src] = send[src] - cap
          receive[dst] = receive[dst] - cap
        end
      end
    end
  end
  -- remove unused vertex from "taskcnt" and set as sub_graph_size
  local nodecnt = 1
  for i = 1, taskcnt do
    local v = sub_graph_v[i]
    if sub_graph_flag[v] then
      sub_graph_v[nodecnt] = v
      local lv = level[v]
      level_vertex_count[lv] = level_vertex_count[lv] + 1
      nodecnt = nodecnt + 1
    end
  end
  if sub_graph_v[nodecnt - 1] == self.tpos then
    nodecnt = nodecnt - 1
  else
    sub_graph_v[nodecnt] = self.tpos
    local lv = level[self.tpos]
    level_vertex_count[lv] = level_vertex_count[lv] + 1
  end
  self.sub_graph_size = nodecnt
  return true
end

MPM.subGraphConnected = function(self)
  local max_level = self.level[self.tpos]
  local level_vertex_count = self.level_vertex_count
  for i = 1, max_level do
    if level_vertex_count[i] <= 0 then return false end
  end
  return true
end

MPM.findWeakVertex = function(self)
  local sub_graph_v = self.sub_graph_v
  local sub_graph_size = self.sub_graph_size
  local send, receive = self.send, self.receive
  local min_vertex = self.spos
  local min_potential = send[min_vertex]
  if receive[self.tpos] < min_potential then
    min_vertex = self.tpos
    min_potential = receive[min_vertex]
  end
  for i = 2, sub_graph_size - 1 do
    local v = sub_graph_v[i]
    local min_v = mmi(send[v], receive[v])
    if min_v < min_potential then
      min_potential, min_vertex = min_v, v
    end
  end
  return min_vertex, min_potential
end

MPM.flowToT = function(self, weak_vertex, potential)
  if weak_vertex == self.tpos then return end
  local sub_graph_flag = self.sub_graph_flag
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local sub_graph_v = self.sub_graph_v
  local sub_graph_size = self.sub_graph_size
  local sub_edge_cnt = self.sub_edge_cnt
  local sub_edge_idxes = self.sub_edge_idxes
  local send, receive = self.send, self.receive
  local level = self.level
  local flow_route = self.flow_route
  local actual_flow_amount = self.actual_flow_amount
  local tpos = self.tpos
  for i = 1, sub_graph_size do
    local v = sub_graph_v[i]
    flow_route[v] = false
    actual_flow_amount[v] = 0
  end
  flow_route[weak_vertex] = true
  actual_flow_amount[weak_vertex] = potential
  local weak_vertex_level = level[weak_vertex]
  local max_level = level[tpos]
  for iv = 1, sub_graph_size do
    local src = sub_graph_v[iv]
    local lv = level[src]
    if lv == max_level then break end
    local need_to_send = actual_flow_amount[src]
    if flow_route[src] and 0 < need_to_send then
      send[src] = send[src] - need_to_send
      local sub_edge_idxes_src = sub_edge_idxes[src]
      local dsts, caps, invidxes = edge_dst[src], edge_cap[src], edge_dst_invedge_idx[src]
      local used = 0
      -- use edge in descending order, to remove used edges quickly
      for j = sub_edge_cnt[src], 1, -1 do
        local edgeidx = sub_edge_idxes_src[j]
        local dst, cap = dsts[edgeidx], caps[edgeidx]
        local actual_flow = mmi(cap, need_to_send)
        receive[dst] = receive[dst] - actual_flow
        caps[edgeidx] = caps[edgeidx] - actual_flow
        need_to_send = need_to_send - actual_flow
        flow_route[dst] = true
        actual_flow_amount[dst] = actual_flow_amount[dst] + actual_flow
        local inv_edge_idx = invidxes[edgeidx]
        edge_cap[dst][inv_edge_idx] = edge_cap[dst][inv_edge_idx] + actual_flow
        if caps[edgeidx] == 0 then
          used = used + 1
        end
        if need_to_send == 0 then
          break
        end
      end
      sub_edge_cnt[src] = sub_edge_cnt[src] - used
    end
  end
end

MPM.flowFromS = function(self, weak_vertex, potential)
  if weak_vertex == self.spos then return end
  local sub_graph_flag = self.sub_graph_flag
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local sub_graph_v = self.sub_graph_v
  local sub_graph_size = self.sub_graph_size
  local sub_edge_cnt = self.sub_edge_cnt
  local sub_edge_idxes = self.sub_edge_idxes
  local sub_invedge_idxes = self.sub_invedge_idxes
  local sub_invedge_cnt = self.sub_invedge_cnt
  local send, receive = self.send, self.receive
  local level = self.level
  local flow_route = self.flow_route
  local actual_flow_amount = self.actual_flow_amount
  local spos = self.spos
  for i = 1, sub_graph_size do
    local v = sub_graph_v[i]
    flow_route[v] = false
    actual_flow_amount[v] = 0
  end
  flow_route[weak_vertex] = true
  actual_flow_amount[weak_vertex] = potential
  local weak_vertex_level = level[weak_vertex]
  local max_level = level[tpos]
  for iv = sub_graph_size, 1, -1 do
    local dst = sub_graph_v[iv]
    local lv = level[dst]
    if lv == 1 then break end
    local need_to_receive = actual_flow_amount[dst]
    if flow_route[dst] and 0 < need_to_receive then
      receive[dst] = receive[dst] - need_to_receive
      local sub_invedge_idxes_dst = sub_invedge_idxes[dst]
      local srcs = edge_dst[dst]
      local inv_invidxes = edge_dst_invedge_idx[dst]
      -- local dsts, caps, invidxes = edge_dst[src], edge_cap[src], edge_dst_invedge_idx[src]
      local used = 0
      -- use edge in descending order, to remove used edges quickly
      for j = sub_invedge_cnt[dst], 1, -1 do
        local invedgeidx = sub_invedge_idxes_dst[j]
        local src = srcs[invedgeidx]
        local edgeidx = inv_invidxes[invedgeidx]
        assert(edge_dst[src][edgeidx] == dst)
        local cap = edge_cap[src][edgeidx]
        local actual_flow = mmi(cap, need_to_receive)

        send[src] = send[src] - actual_flow
        edge_cap[src][edgeidx] = edge_cap[src][edgeidx] - actual_flow
        need_to_receive = need_to_receive - actual_flow
        flow_route[src] = true
        actual_flow_amount[src] = actual_flow_amount[src] + actual_flow
        edge_cap[dst][invedgeidx] = edge_cap[dst][invedgeidx] + actual_flow
        if edge_cap[src][edgeidx] == 0 then
          used = used + 1
        end
        if need_to_receive == 0 then
          break
        end
      end
      sub_invedge_cnt[dst] = sub_invedge_cnt[dst] - used
    end
  end
end

MPM.updateSubGraph = function(self)
  local sub_graph_v = self.sub_graph_v
  local sub_graph_size = self.sub_graph_size
  local sub_graph_flag = self.sub_graph_flag
  local send, receive = self.send, self.receive
  local spos, tpos = self.spos, self.tpos
  local level = self.level
  local level_vertex_count = self.level_vertex_count
  local sub_edge_idxes = self.sub_edge_idxes
  local sub_edge_cnt = self.sub_edge_cnt
  local edge_dst = self.edge_dst
  local sub_invedge_idxes = self.sub_invedge_idxes
  local sub_invedge_cnt = self.sub_invedge_cnt
  local nodecnt = 0
  for i = 1, sub_graph_size do
    local v = sub_graph_v[i]
    local valid = true
    if v ~= spos and receive[v] <= 0 then valid = false end
    if v ~= tpos and send[v] <= 0 then valid = false end
    if valid then
      local sub_invedge_idxes_v = sub_invedge_idxes[v]
      valid = false
      for j = 1, sub_invedge_cnt[v] do
        local ei = sub_invedge_idxes_v[j]
        local src = edge_dst[v][ei]
        if sub_graph_flag[src] then valid = true break end
      end
    end
    if valid then
      nodecnt = nodecnt + 1
      sub_graph_v[nodecnt] = v
    else
      sub_graph_flag[v] = false
      local lv = level[v]
      level_vertex_count[lv] = level_vertex_count[lv] - 1
    end
  end
  sub_graph_size = nodecnt
  for i = sub_graph_size, 1, -1 do
    local v = sub_graph_v[i]
    local valid = false
    local sub_edge_idxes_v = sub_edge_idxes[v]
    for j = 1, sub_edge_cnt[v] do
      local ei = sub_edge_idxes_v[j]
      local dst = edge_dst[v][ei]
      if sub_graph_flag[dst] then valid = true break end
    end
    if not valid then
      sub_graph_flag[v] = false
      local lv = level[v]
      level_vertex_count[lv] = level_vertex_count[lv] - 1
    end
  end
  nodecnt = 0
  for i = 1, sub_graph_size do
    local v = sub_graph_v[i]
    if sub_graph_v[v] then
      nodecnt = nodecnt + 1
      sub_graph_v[nodecnt] = v
    end
  end
  self.sub_graph_size = nodecnt
end

MPM.partialwork = function(self)
  local sum = 0
  while(self:subGraphConnected()) do
    local weak_vertex, potential = self:findWeakVertex()
    self:flowToT(weak_vertex, potential)
    self:flowFromS(weak_vertex, potential)
    self:updateSubGraph()
    sum = sum + potential
  end
  return sum
end

MPM.getMaxFlow = function(self)
  local ret = 0
  while(self:makeSubGraph()) do
    ret = ret + self:partialwork()
  end
  return ret
end

local inf = 1000000007 * 1000
local n = io.read("*n")
MPM:initialize(n + 2, n + 1, n + 2)
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local score = 0
for i = 1, n do
  if 0 < a[i] then
    score = score + a[i]
    MPM:addEdge(i, n + 2, a[i])
  elseif a[i] < 0 then
    MPM:addEdge(n + 1, i, -a[i])
  end
end
for src = 1, n do
  local lim = mfl(n / src)
  for j = 2, lim do
    local dst = j * src
    MPM:addEdge(src, dst, inf)
  end
end
local flow = MPM:getMaxFlow()
print(score - flow)

Score : 700 points

Problem Statement

We have N gemstones labeled 1 through N.

You can perform the following operation any number of times (possibly zero).

Select a positive integer x, and smash all the gems labeled with multiples of x.

Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan).
However, a_i may be negative, in which case you will be charged money.

By optimally performing the operation, how much yen can you earn?

Constraints

All input values are integers.

1 \leq N \leq 100

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000


local mfl = math.floor
local n = io.read("*n")
local a = {}
local edge_to_child = {}
local edge_to_parent = {}
for i = 1, n do
  a[i] = io.read("*n")
  edge_to_child[i] = {}
  edge_to_parent[i] = {}
end
for i = 1, n - 1 do
  local lim = mfl(n / i)
  for j = 2, lim do
    edge_to_child[i][i * j] = true
    edge_to_parent[i * j][i] = true
  end
end

local cost = {}
for i = 1, n do cost[i] = a[i] end

local function recalcCost()
  for i = 1, n do cost[i] = a[i] end
  for child = n, 2, -1 do
    for parent, _u in pairs(edge_to_parent[child]) do
      cost[parent] = cost[parent] + a[child]
    end
  end
end

while true do
  recalcCost()
  local mincost, minpos = 0, 0
  for i = n, 1, -1 do
    if cost[i] < 0 then
      mincost = cost[i]
      minpos = i
      break
    end
  end
  if 0 <= mincost then
    break
  else
    local lim = mfl(n / minpos)
    for j = 1, lim do
      a[minpos * j] = 0
    end
  end
end
local ret = 0
for i = 1, n do
  ret = ret + a[i]
end
print(ret)


local mfl = math.floor
local n = io.read("*n")
local a = {}
local edge_to_child = {}
local edge_to_parent = {}
for i = 1, n do
  a[i] = io.read("*n")
  edge_to_child[i] = {}
  edge_to_parent[i] = {}
end
for i = 1, n - 1 do
  local lim = mfl(n / i)
  for j = 2, lim do
    edge_to_child[i][i * j] = true
    edge_to_parent[i * j][i] = true
  end
end

local cost = {}
for i = 1, n do cost[i] = a[i] end

local function recalcCost()
  for i = 1, n do cost[i] = a[i] end
  for child = n, 2, -1 do
    for parent, _u in pairs(edge_to_parent[child]) do
      cost[parent] = cost[parent] + a[child]
    end
  end
end

while true do
  recalcCost()
  local mincost, minpos = cost[1], 1
  for i = 2, n do
    if cost[i] < mincost then
      mincost = cost[i]
      minpos = i
    end
  end
  if 0 <= mincost then
    break
  else
    local lim = mfl(n / minpos)
    for j = 1, lim do
      a[minpos * j] = 0
    end
  end
end
local ret = 0
for i = 1, n do
  ret = ret + a[i]
end
print(ret)


Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


n=io.read()
m=io.read()
if string.reverse(m)==n then
  print("Yes")
  else
  print("No")
end


Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


local k, n = io.read("*n", "*n")
local t = {}

if k % 2 == 0 then
  table.insert(t, k / 2)
  for i = 1, n - 1 do table.insert(t, k) end
else
  local hk = (k + 1) / 2
  for i = 1, n do t[i] = hk end
  local decnum = math.ceil((n - 1) / 2)
  local curpos = n
  for i = 1, decnum do
    for j = curpos, 1, -1 do
      if 0 < t[j] then
        t[j] = t[j] - 1
        if t[j] ~= 0 then
          for s = j + 1, n do
            t[s] = k
          end
          curpos = n
        else
          curpos = curpos - 1
        end
        break
      end
    end
  end
  for i = 1, n do
    if t[i] == 0 then
      for j = i, n do
        table.remove(t)
      end
      break
    end
  end
end

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

Score : 800 points

Problem Statement

In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 and N (inclusive) consisting of integers between 1 and K (inclusive) are listed.

Let the total number of sequences listed in FEIS be X. Among those sequences, find the (X/2)-th (rounded up to the nearest integer) lexicographically smallest one.

Constraints

1 \leq N,K \leq 3 × 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

K N

Output

Print the (X/2)-th (rounded up to the nearest integer) lexicographically smallest sequence listed in FEIS, with spaces in between, where X is the total number of sequences listed in FEIS.

Sample Input 1

3 2

Sample Output 1

2 1

There are 12 sequences listed in FEIS: (1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3).
The (12/2 = 6)-th lexicographically smallest one among them is (2,1).

Sample Input 2

2 4

Sample Output 2

1 2 2 2

Sample Input 3

5 14

Sample Output 3

3 3 3 3 3 3 3 3 3 3 3 3 2 2


local k, n = io.read("*n", "*n")
local t = {}

if k % 2 == 0 then
  table.insert(t, k / 2)
  for i = 1, n - 1 do table.insert(t, k) end
else
  local hk = (k + 1) / 2
  for i = 1, n do t[i] = hk end
  local decnum = math.ceil((n - 1) / 2)
  for i = 1, decnum do
    for j = n, 1, -1 do
      if 0 < t[j] then
        t[j] = t[j] - 1
        if t[j] ~= 0 then
          for s = j + 1, n do
            t[s] = k
          end
        end
        break
      end
    end
  end
  for i = 1, n do
    if t[i] == 0 then
      for j = i, n do
        table.remove(t)
      end
      break
    end
  end
end

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


Score : 100 points

Problem Statement

Snuke built an online judge to hold a programming contest.

When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring.
(The judge can return any two-character substring of S.)

Determine whether the judge can return the string AC as the verdict to a program.

Constraints

2 \leq |S| \leq 5

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the judge can return the string AC as a verdict to a program, print Yes; if it cannot, print No.

Sample Input 1

BACD

Sample Output 1

Yes

The string AC appears in BACD as a contiguous substring (the second and third characters).

Sample Input 2

ABCD

Sample Output 2

No

Although the string ABCD contains both A and C (the first and third characters), the string AC does not appear in ABCD as a contiguous substring.

Sample Input 3

CABD

Sample Output 3

No

Sample Input 4

ACACA

Sample Output 4

Yes

Sample Input 5

XX

Sample Output 5

No


n=io.read("*n","*l")
m=0
for i=1,n do
    x=io.read("*n")
    if x%2==0 then
        m=m+1
    end
end
print(math.floor(3^n-2^m))

Score : 200 points

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).

In particular, any integer sequence is similar to itself.

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

Constraints

1 \leq N \leq 10

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.

Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

1, 2

1, 4

2, 2

2, 3

2, 4

3, 2

3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921


local n = io.read("*n")
local r = 1
for i = 1, n do
  r = r * 3
end
local odd = 1
for i = 1, n do
  local a = io.read("*n")
  if a % 2 == 0 then
    odd = odd * 2
  end
end
print(r - odd)


print(io.read('n') ~ io.read('n') ~ io.read('n'))

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


local n,m=io.read("n","n")
local a={}
local b={}
local visited={}
local graph={}
for i=1,n do
    graph[i]={}
    for j=1,n do
        graph[i][j]=false
    end
end

local function dfs(x)
    visited[x]=true
    for y=1,n do
        if graph[x][y]==false then
            goto continue
        end
        if visited[y]==true then
            goto continue
        end
        dfs(y)
        ::continue::
    end
end

for i=1,m do
    local x,y=io.read("n","n")
    a[i]=x
    b[i]=y
    graph[x][y]=true
    graph[y][x]=true
end

local counter=0
for i=1,m do
    graph[a[i]][b[i]]=false
    graph[b[i]][a[i]]=false
    for j=1,n do
        visited[j]=false
    end
    dfs(1)
    local bridge=false
    for j=1,n do
        if visited[j]==false then
            bridge=true
        end
    end
    if bridge==true then
        counter=counter+1
    end
    graph[a[i]][b[i]]=true
    graph[b[i]][a[i]]=true
end
print(counter)

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


n, m = io.read('n'), io.read('n')
e = {}
for i = 1, n do e[i] = {} end
for i = 1, m do
    local u, v = io.read('n'), io.read('n')
    e[u][#e[u] + 1] = v
    e[v][#e[v] + 1] = u
end

dfn, low = {}, {}
tot = 0
bridge_count = 0

dfs = function (u, p)
    tot = tot + 1
    dfn[u] = tot
    low[u] = tot
    for _, v in ipairs(e[u]) do if v ~= p then
        if dfn[v] == nil then
            dfs(v, u)
            low[u] = math.min(low[u], low[v])
        else
            low[u] = math.min(low[u], dfn[v])
        end
    end end
    if p ~= -1 and low[u] > dfn[p] then
        bridge_count = bridge_count + 1
    end
end

for i = 1, n do if dfn[i] == nil then dfs(i, -1) end end

print(bridge_count)


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

-- Discretize
rx, ry = {}, {}
for i = 1, n do rx[i], ry[i] = {x[i], i}, {y[i], i} end
pair_cmp = function (lhs, rhs) return lhs[1] < rhs[1] end
table.sort(rx, pair_cmp)
table.sort(ry, pair_cmp)
for i = 1, n do x[rx[i][2]], y[ry[i][2]] = i, i end
for i = 1, n do rx[i], ry[i] = rx[i][1], ry[i][1] end

-- O(n^4)
-- Prefix sum
p = {}
for i = 0, n do
    p[i] = {}
    for j = 0, n do p[i][j] = 0 end
end
for i = 1, n do p[x[i]][y[i]] = 1 end
for i = 1, n do
    for j = 1, n do
        p[i][j] = p[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1]
    end
end

-- Iteration
ans = 1 << 62
for l = 1, n do
    for r = l + 1, n do
        for d = 1, n do
            for u = d + 1, n do
                if p[r][u] - p[l - 1][u] - p[r][d - 1] + p[l - 1][d - 1] >= k then
                    ans = math.min(ans, (rx[r] - rx[l]) * (ry[u] - ry[d]))
                end
            end
        end
    end
end

print(ans)

Score : 400 points

Problem Statement

We have N points in a two-dimensional plane.

The coordinates of the i-th point (1 \leq i \leq N) are (x_i,y_i).

Let us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.

Here, points on the sides of the rectangle are considered to be in the interior.

Find the minimum possible area of such a rectangle.

Constraints

2 \leq K \leq N \leq 50

-10^9 \leq x_i,y_i \leq 10^9 (1 \leq i \leq N)

x_i≠x_j (1 \leq i<j \leq N)

y_i≠y_j (1 \leq i<j \leq N)

All input values are integers. (Added at 21:50 JST)

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1
:
x_{N} y_{N}

Output

Print the minimum possible area of a rectangle that satisfies the condition.

Sample Input 1

4 4
1 4
3 3
6 2
8 1

Sample Output 1

21

One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1), (8,1), (1,4) and (8,4).

Its area is (8-1) × (4-1) = 21.

Sample Input 2

4 2
0 0
1 1
2 2
3 3

Sample Output 2

1

Sample Input 3

4 3
-1000000000 -1000000000
1000000000 1000000000
-999999999 999999999
999999999 -999999999

Sample Output 3

3999999996000000001

Watch out for integer overflows.


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

ans = 1 << 62

for l = 1, n do
    for r = 1, n do if x[r] > x[l] then
        for u = 1, n do
            for d = 1, n do if y[u] > y[d] then
                local count = 0
                for i = 1, n do
                    if x[i] >= x[l] and x[i] <= x[r] and
                        y[i] >= y[d] and y[i] <= y[u]
                    then
                        count = count + 1
                    end
                end
                if count >= k then
                    ans = math.min(ans, (x[r] - x[l]) * (y[u] - y[d]))
                end
            end end
        end
    end end
end

print(ans)


local n, m = io.read("*n", "*n")
local color = {}
local edge = {}
for i = 1, n do
  color[i] = 0
  edge[i] = {}
end
local tasks = {}
do
  local a, b = io.read("*n", "*n")
  color[a] = 1
  color[b] = 2
  tasks[1] = a
  tasks[2] = b
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
for i = 2, m do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local allsame = false
for itask = 1, n do
  local src = tasks[itask]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if color[dst] == 0 then
      color[dst] = 3 - color[src]
      table.insert(tasks, dst)
    elseif color[dst] == color[src] then
      allsame = true
      break
    end
  end
  if allsame then break end
end
if allsame then
  print(n * (n - 1) // 2 - m)
else
  local cnt1, cnt2 = 0, 0
  for i = 1, n do
    if color[i] == 1 then
      cnt1 = cnt1 + 1
    else
      cnt2 = cnt2 + 1
    end
  end
  print(cnt1 * cnt2 - m)
end

Score : 500 points

Problem Statement

Rng has a connected undirected graph with N vertices.
Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.

Rng will add new edges to the graph by repeating the following operation:

Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

Find the maximum possible number of edges that can be added.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N

The graph has no self-loops or multiple edges.

The graph is connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Find the maximum possible number of edges that can be added.

Sample Input 1

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

Sample Output 1

4

If we add edges as shown below, four edges can be added, and no more.

Sample Input 2

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

Sample Output 2

5

Five edges can be added, for example, as follows:

Add an edge connecting Vertex 5 and Vertex 3.

Add an edge connecting Vertex 5 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 1.

Add an edge connecting Vertex 4 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 3.


local n=io.read("n")
for i=1,3500 do
    for j=1,3500 do
        local a=n*i*j
        local b=(4*i*j-n*j-n*i)
        if b>0 and a%b==0 then
            local k=math.floor(a/b)
            print(i.." "..j.." "..k)
            os.exit()
        end
    end
end

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


local n=io.read("n")
for i=1,3500 do
    for j=1,3500 do
        local k=n*i*j/(4*i*j-n*j-n*i)
        if math.fmod(k,1)==0 and k>=0 then
            print(i,j,string.format("%d",k))
            return
        end
    end
end


local mfl = math.floor
local n, k = io.read("*n", "*n")
local as, bs = {}, {}
local kdigs = {}
do
  local tk = k
  for i = 1, 32 do
    kdigs[i] = tk % 2
    tk = mfl(tk / 2)
  end
end
local klim = 1
for i = 32, 1, -1 do
  if kdigs[i] == 1 then
    klim = i
    break
  end
end

local digs = {}
local maxdig = {}
for i = 1, n do
  digs[i] = {}
  local a = io.read("*n")
  bs[i] = io.read("*n")
  for j = 1, 32 do
    digs[i][j] = a % 2
    a = mfl(a / 2)
  end
  maxdig[i] = 1
  for j = 32, 1, -1 do
    if digs[i][j] == 1 then
      maxdig[i] = j
      break
    end
  end
end

local ret = 0
local mma = math.max
for lim = 0, klim do
  if lim == 0 or kdigs[lim] == 1 then
    local score = 0
    for i = 1, n do
      local isok = maxdig[i] <= klim
      for dig = 1, klim do
        if dig < lim then
          -- nothing
        elseif dig == lim then
          if digs[i][dig] == 1 then isok = false break end
        else
          if kdigs[dig] < digs[i][dig] then isok = false break end
        end
      end
      if isok then score = score + bs[i] end
    end
    ret = mma(ret, score)
  end
end
print(ret)

Score : 500 points

Problem Statement

Seisu-ya, a store specializing in non-negative integers, sells N non-negative integers. The i-th integer is A_i and has a utility of B_i.
There may be multiple equal integers with different utilities.

Takahashi will buy some integers in this store. He can buy a combination of integers whose bitwise OR is less than or equal to K. He wants the sum of utilities of purchased integers to be as large as possible.

Find the maximum possible sum of utilities of purchased integers.

Constraints

1 \leq N \leq 10^5

0 \leq K < 2^{30}

0 \leq A_i < 2^{30}(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N K
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible sum of utilities of purchased integers.

Sample Input 1

3 5
3 3
4 4
2 5

Sample Output 1

8

Buy 2 and 3 to achieve the maximum possible total utility, 8.

Sample Input 2

3 6
3 3
4 4
2 5

Sample Output 2

9

Buy 2 and 4 to achieve the maximum possible total utility, 9.

Sample Input 3

7 14
10 5
7 4
11 4
9 8
3 6
6 2
8 9

Sample Output 3

32


local n = io.read("*n")
local amax, ret = 0, 0
for i = 1, n do
  local a, b = io.read("*n", "*n")
  if amax < a then
    ret = a + b
    amax = a
  end
end
print(ret)

Score : 200 points

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

0 \leq B_i \leq 10^9(1\leq i\leq N)

If i ≠ j, A_i ≠ A_j.

There exists a possible outcome of the game that are consistent with the facts.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001


local n, m, k = io.read("*n", "*n", "*n")
for i = 0, n do
  for j = 0, m do
    if k == i * (m - j) + (n - i) * j then
      print("Yes") os.exit()
    end
  end
end
print("No")

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No


local h,w=io.read("n","n","l")
local map={}
for i=1,h do
    local s=io.read()
    for j=1,w do
        local a=s:sub(j,j)
        map[a]=(map[a] or 0)+1
    end
end

local g1=(h%2)*(w%2)
local g2=(h%2)*(w//2)+(w%2)*(h//2)
local g4=(h*w-2*g2-g1)//4

while g1>0 do
    for k,v in pairs(map) do
        if g1==0 then
            break
        end
        if ( v%4==1 or v%4==3 ) and v>0 then
            map[k]=map[k]-1
            g1=g1-1
        end
    end
end

while g2>0 do
    for k,v in pairs(map) do
        if g2==0 then
            break
        end
        if v%4==2 and v>0  then
            map[k]=map[k]-2
            g2=g2-1
        end
    end
end

while g4>0 do
    for k,v in pairs(map) do
        if g4==0 then
            break
        end
        if v%4==0 and v>0 then
            map[k]=map[k]-4
            g4=g4-1
        end
    end
end

local checker=true
for k,v in pairs(map) do
    if v~=0 then
        checker=false
    end
end
print(checker and "Yes" or "No")

Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


local h,w=io.read("n","n","l")
local map={}
for i=1,h do
    local s=io.read()
    for j=1,w do
        local a=s:sub(j,j)
        map[a]=((map[a] or 0)+1)
        map[a]=map[a]%4
    end
end

local g1=(h%2)*(w%2)
local g2=(h%2)*(w//2)+(w%2)*(h//2)
local g4=(h*w-2*g2-g1)//4

for i=1,g1 do
    for k,v in pairs(map) do
        if v==1 or v==3 then
            map[k]="checked"
            g1=g1-1
            break
        end
    end
end

for i=1,g2 do
    for k,v in pairs(map) do
        if v==2 then
            map[k]="checked"
            g2=g2-1
            break
        end
    end
end

for i=1,g4 do
    for k,v in pairs(map) do
        if v==0 then
            map[k]="checked"
            g4=g4-1
            break
        end
    end
end

local checker=(g1==0 and g2==0 and g4==0)
print(checker and "Yes" or "No")


local h, w = io.read("*n", "*n", "*l")
local words = {}
for i = 1, h do
  local str = io.read()
  for j = 1, w do
    local sstr = str:sub(j, j)
    if not words[sstr] then words[sstr] = 1 else words[sstr] = words[sstr] + 1 end
  end
end
local a, b = 0, 0
for k, v in pairs(words) do
  if v % 2 == 1 then
    if b == 0 then
      b = v
    else
      b = -1
    end
  elseif v % 4 == 2 then
    a = a + 1
  end
end

if h % 2 == 0 and w % 2 == 0 then
  if a == 0 and b == 0 then
    print("Yes")
  else
    print("No")
  end
elseif h % 2 == 1 and w % 2 == 1 then
  if 0 < b then
    if (b - 1) % 4 == 2 then
      a = a + 1
    end
    local hh, hw = (h - 1) // 2, (w - 1) // 2
    if a <= hh + hw then
      print("Yes")
    else
      print("No")
    end
  else
    print("No")
  end
else
  if b ~= 0 then
    print("No")
  else
    if h % 2 == 1 then h, w = w, h end
    local hh = h // 2
    if a <= hh then
      print("Yes")
    else
      print("No")
    end
  end
end


Score : 100 points

Problem Statement

We have an N \times N square grid.

We will paint each square in the grid either black or white.

If we paint exactly A squares white, how many squares will be painted black?

Constraints

1 \leq N \leq 100

0 \leq A \leq N^2

Inputs

Input is given from Standard Input in the following format:

N
A

Outputs

Print the number of squares that will be painted black.

Sample Input 1

3
4

Sample Output 1

5

There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black.

Sample Input 2

19
100

Sample Output 2

261

Sample Input 3

10
0

Sample Output 3

100

As zero squares will be painted white, all the squares will be painted black.


local n, k = io.read("*n", "*n")
local sum = 0
for i = 1, n do
  local a = io.read("*n")
  sum = sum + 2 * math.min(a, math.abs(a - k))
end
print(sum)

Score : 200 points

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

There are just one ball, one type-A robot and one type-B robot.

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

Similarly, if the type-B robot is used, the total distance covered will be 16.

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Sample Input 2

2
9
3 6

Sample Output 2

12

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74


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

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

for k=1,n do
    for i=1,n do
        for j=1,n do
            if a[i][j]>a[i][k]+a[k][j] then
                print(-1)
                return
            elseif a[i][j]==a[i][k]+a[k][j] and a[i][k]>0 and a[k][j]>0 then
                d[i][j]=true
            end
        end
    end
end

local total=0
for i=1,n do
    for j=1,i do
        total=total+(not d[i][j] and a[i][j] or 0)
    end
end
print(total)

Score : 500 points

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.
The following are known about the road network:

People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.

Different roads may have had different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.
He thought that it represented the shortest distances between the cities along the roads in the kingdom.

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.
If such a network exist, find the shortest possible total length of the roads.

Constraints

1 \leq N \leq 300

If i ≠ j, 1 \leq A_{i, j} = A_{j, i} \leq 10^9.

A_{i, i} = 0

Inputs

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

Outputs

If there exists no network that satisfies the condition, print -1.
If it exists, print the shortest possible total length of the roads.

Sample Input 1

3
0 1 3
1 0 2
3 2 0

Sample Output 1

3

The network below satisfies the condition:

City 1 and City 2 is connected by a road of length 1.

City 2 and City 3 is connected by a road of length 2.

City 3 and City 1 is not connected by a road.

Sample Input 2

3
0 1 3
1 0 1
3 1 0

Sample Output 2

-1

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3.
However, according to the table, the shortest distance between City 1 and City 3 must be 3.

Thus, we conclude that there exists no network that satisfies the condition.

Sample Input 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

Sample Output 3

82

Sample Input 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

Sample Output 4

3000000000


local n = io.read("*n")
local p = {0}
for i = 2, n do
  p[i] = io.read("*n")
end
local base = {}
local t = {}
local x = {}
for i = 1, n do
  base[i] = 0
  t[i] = {}
  x[i] = io.read("*n")
end
local valid = true
for i = n, 1, -1 do
  local tgt = x[i] - base[i]
  if tgt < 0 then
    valid = false
    break
  end
  -- get highest value y where dp[y] == true
  local dp = {}
  for j = 1, tgt do
    dp[j] = false
  end
  local sum = base[i]
  for j = 1, #t[i] do
    local tv = t[i][j]
    sum = sum + tv
    for k = tgt - tv, 1, -1 do
      if dp[k] then dp[k + tv] = true end
    end
    if tv <= tgt then dp[tv] = true end
  end
  local r = 0
  for j = tgt, 1, -1 do
    if dp[j] then r = j break end
  end
  if 1 < i then
    local v1 = x[i] -- base[i] + r <= x[i]. "bese[i] + r" became x[i].
    local v2 = sum - base[i] - r
    local parent = p[i]
    if v2 < v1 then v1, v2 = v2, v1 end
    base[parent] = base[parent] + v1
    if v1 < v2 then table.insert(t[parent], v2 - v1) end
  end
end
print(valid and "POSSIBLE" or "IMPOSSIBLE")

Score : 700 points

Problem Statement

We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i.

To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.

Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v.

The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v.

Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.

Determine whether it is possible to allocate colors and weights in this way.

Constraints

1 \leq N \leq 1 000

1 \leq P_i \leq i - 1

0 \leq X_i \leq 5 000

Inputs

Input is given from Standard Input in the following format:

N
P_2 P_3 ... P_N
X_1 X_2 ... X_N

Outputs

If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3
1 1
4 3 2

Sample Output 1

POSSIBLE

For example, the following allocation satisfies the condition:

Set the color of Vertex 1 to white and its weight to 2.

Set the color of Vertex 2 to black and its weight to 3.

Set the color of Vertex 3 to white and its weight to 2.

There are also other possible allocations.

Sample Input 2

3
1 2
1 2 3

Sample Output 2

IMPOSSIBLE

If the same color is allocated to Vertex 2 and Vertex 3, Vertex 2 cannot be allocated a non-negative weight.

If different colors are allocated to Vertex 2 and 3, no matter which color is allocated to Vertex 1, it cannot be allocated a non-negative weight.

Thus, there exists no allocation of colors and weights that satisfies the condition.

Sample Input 3

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

Sample Output 3

POSSIBLE

Sample Input 4

1

0

Sample Output 4

POSSIBLE


local n=io.read("*n")
local t = n / 10
local o = n % 10

if t == 9 or o == 9 then
    print("Yes")
else
    print("No")
end

Score : 100 points

Problem Statement

It is September 9 in Japan now.

You are given a two-digit integer N. Answer the question: Is 9 contained in the decimal notation of N?

Constraints

10≤N≤99

Input

Input is given from Standard Input in the following format:

N

Output

If 9 is contained in the decimal notation of N, print Yes; if not, print No.

Sample Input 1

29

Sample Output 1

Yes

The one's digit of 29 is 9.

Sample Input 2

72

Sample Output 2

No

72 does not contain 9.

Sample Input 3

91

Sample Output 3

Yes


n=io.read("*n")
a={}
for i=1,n do
    number=io.read("*n")
    if not a[number]  then
        a[number]=1
    else
        a[number]=0
    end
end

counter=0
for _,v in pairs(a) do
    counter=counter+v
end
print(counter)

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


local mmi, mma = math.min, math.max
local mfl, mce = math.floor, math.ceil
local n, m, r = io.read("*n", "*n", "*n")
local t = {}
for i = 1, r do
  t[i] = io.read("*n")
end
local inf = 1000000007 * 1000
local len = {}
for i = 1, n do
  len[i] = {}
  for j = 1, n do
    len[i][j] = i == j and 0 or inf
  end
end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  len[a][b], len[b][a] = c, c
end
for k = 1, n do
  for i = 1, n do
    for j = 1, n do
      len[i][j] = mmi(len[i][j], len[i][k] + len[k][j])
    end
  end
end

local function getpattern(n, patall, idx)
  local used = {}
  local retary = {}
  local div = patall
  for i = 1, n do used[i] = false end
  for i = n, 1, -1 do
    div = mfl(div / i)
    local v_idx = mfl(idx / div)
    idx = idx % div
    local tmp_idx = 0
    for j = 1, n do
      if not used[j] then
        if tmp_idx == v_idx then
          table.insert(retary, j)
          used[j] = true
          break
        else
          tmp_idx = tmp_idx + 1
        end
      end
    end
  end
  return retary
end
local tot = 1
for i = 1, r do
  tot = tot * i
end
local ret = inf
for i = 0, tot - 1 do
  local pat = getpattern(r, tot, i)
  local z = 0
  for i = 1, r - 1 do
    z = z + len[t[pat[i]]][t[pat[i + 1]]]
  end
  if z < ret then ret = z end
end
print(ret)

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town A_i and B_i and has a length of C_i.

Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

2≤N≤200

1≤M≤N×(N-1)/2

2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)

r_i≠r_j (i≠j)

1≤A_i,B_i≤N, A_i≠B_i

(A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)

1≤C_i≤100000

Every town can be reached from every town by road.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

Sample Input 1

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

Sample Output 1

2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Sample Input 2

3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.

Sample Input 3

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

Sample Output 3

3


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

local disttable={}
local INF=10^14-1
for i=1,n do
    disttable[i]={}
    for j=1,n do
        disttable[i][j]=(i==j and 0 or INF)
    end
end

for i=1,m do
    local a,b,c=io.read("n","n","n")
    disttable[a][b]=c
    disttable[b][a]=c
end

for k=1,n do
    for i=1,n do
        for j=1,n do
            disttable[i][j]=math.min(disttable[i][j],disttable[i][k]+disttable[k][j])
        end
    end
end

local totaldist=0
for i=1,r-1 do
    local mindist=INF
    local r1=town[i]
    for j=i+1,r do
        local r2=town[j]
        mindist=math.min(mindist,disttable[r1][r2])
    end
    totaldist=totaldist+mindist
end
print(totaldist)


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

local n, m, rnum = io.read("*n", "*n", "*n")
local r = {}
for i = 1, rnum do r[i] = io.read("*n") end
local edge = {}
for i = 1, n do edge[i] = {} end
for i = 1, m do
  local u, v, l = io.read("*n", "*n", "*n")
  edge[u][v] = l
  edge[v][u] = l
end

local inf = 30000001

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

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

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

  while(done < tasknum) do
    done = done + 1
    local src = tasks[done]
    taskstate[src] = false
    for dst, cost in pairs(edge[src]) do
      if len[src] + cost < len[dst] then
        len[dst] = len[src] + cost
        addtask(dst)
      end
    end
  end
  return len[dst_idx]
end

local function heldkarp_prepare(n)
  local tot = 1
  local alltask = {}
  local stagetask = {}
  for i = 1, n do
    tot = tot * 2
    alltask[i] = {}
    stagetask[i] = {}
  end
  for i = 1, n do
    for j = 1, tot - 1 do
      alltask[i][j] = 160000001
    end
  end
  for i = 1, tot - 1 do
    local ti = i
    local cnt = 0
    for j = 1, n do
      if ti % 2 == 1 then
        cnt = cnt + 1
        ti = ti - 1
      end
      ti = ti / 2
    end
    table.insert(stagetask[cnt], i)
  end
  -- set first state
  local tmp = 1
  for i = 1, n do
    alltask[i][tmp] = 0
    tmp = tmp * 2
  end
  return tot, alltask, stagetask
end

local function heldkarp_doall(n, alltask, stagetask, len)
  for stage = 1, n - 1 do
    local stlen = #stagetask[stage]
    for i_stagetask = 1, stlen do
      local used = stagetask[stage][i_stagetask]
      local t_used = used
      for i = 1, n do
        if t_used % 2 == 1 then
          local val = alltask[i][used]
          local mul = 1
          local tmp = used
          for j = 1, n do
            if tmp % 2 == 0 then
              alltask[j][used + mul] = mmi(alltask[j][used + mul], val + len[(i - 1) * n + j])
            else
              tmp = tmp - 1
            end
            tmp = tmp / 2
            mul = mul * 2
          end
          t_used = t_used - 1
        end
        t_used = t_used / 2
      end
    end
  end
end

local function heldkarp_getresult(n, tot, alltask)
  local ret = alltask[1][tot - 1]
  for i = 2, n do
    ret = mmi(ret, alltask[i][tot - 1])
  end
  return ret
end

local len = {}
for i = 1, rnum * rnum do
  len[i] = 0
end
for i = 1, rnum - 1 do
  for j = i + 1, rnum do
    local l = getlength(r[i], r[j])
    len[(i - 1) * rnum + j] = l
    len[(j - 1) * rnum + i] = l
  end
end
local t1, t2, t3 = heldkarp_prepare(rnum)
heldkarp_doall(rnum, t2, t3, len)
print(heldkarp_getresult(rnum, t1, t2))


a, b = io.read("*n", "*n")
print(math.max(0, a - b))

Score : 100 points

Problem Statement

We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.

How many grams of sand will the upper bulb contains after t seconds?

Constraints

1≤X≤10^9

1≤t≤10^9

X and t are integers.

Input

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

X t

Output

Print the number of sand in the upper bulb after t second.

Sample Input 1

100 17

Sample Output 1

83

17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.

Sample Input 2

48 58

Sample Output 2

0

All 48 grams of sand will be gone, resulting in 0 grams.

Sample Input 3

1000000000 1000000000

Sample Output 3

0


local n = io.read("*n")
local t = {}
for i = 1, n do t[i] = io.read("*n") end
local cnt = 0
if(t[1] == 1) then t[1], t[2], cnt = t[2], t[1], 1 end
for i = 2, n do
  if(t[i] == i) then
    t[i], t[i - 1], cnt = t[i - 1], t[i], cnt + 1
  end
end
print(cnt)

Score : 400 points

Problem Statement

You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.
You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have p_i ≠ i for all 1≤i≤N.
Find the minimum required number of operations to achieve this.

Constraints

2≤N≤10^5

p_1,p_2,..,p_N is a permutation of 1,2,..,N.

Input

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

N
p_1 p_2 .. p_N

Output

Print the minimum required number of operations

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.
This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0

The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3


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

x=math.min(4*t[1],2*t[2],t[3])
if x<2*t[4] then
    print(n*x)
else
    print((n//2)*t[4]+(n%2)*x)
end

Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942


local s = io.read()
local n = #s
local t = {}
for i = 1, 26 do
  t[i] = 0
end
do
  local a = s:sub(1, 1):byte() - 96
  t[a] = t[a] + 1
end
local ret = 1
for i = 2, n do
  local a = s:sub(i, i):byte() - 96
  ret = ret + i - 1 - t[a]
  t[a] = t[a] + 1
end
print(ret)

Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

1 \leq |A| \leq 200,000

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.

Sample Input 3

abracadabra

Sample Output 3

44


local s = io.read()
local n = #s
local cycle_end_pos = {}
local box = {}
for i = 1, 26 do
  box[i] = false
end
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i):byte() - 96
end
local boxcnt = 0
for i = 1, n do
  local v = t[i]
  if not box[v] then
    box[v] = true
    boxcnt = boxcnt + 1
    if boxcnt == 26 then
      table.insert(cycle_end_pos, i)
      boxcnt = 0
      for j = 1, 26 do box[j] = false end
    end
  end
end
-- node = (#cycles + 1) * 26
local edge = {}
local invedge = {}
local canuse = {}

for i = 1, (#cycle_end_pos + 1) * 26 do
  edge[i] = {}
  invedge[i] = {}
  canuse[i] = false
end

local first_pos = {}
local last_pos = {}
for ic = 1, #cycle_end_pos do
  local spos = ic == 1 and 1 or cycle_end_pos[ic - 1] + 1
  local epos = cycle_end_pos[ic]
  for i = 1, 26 do
    box[i] = false
    first_pos[i] = 0
    last_pos[i] = 0
  end
  boxcnt = 0
  for i = spos, epos do
    local v = t[i]
    if first_pos[v] == 0 then
      first_pos[v] = i
    end
    last_pos[v] = i
  end
  for i = 1, 26 do
    local src = (ic - 1) * 26 + i
    for j = 1, 26 do
      if last_pos[j] <= first_pos[i] then
        local dst = ic * 26 + j
        table.insert(edge[src], dst)
        table.insert(invedge[dst], src)
      end
    end
  end
end
for i = 1, 26 do
  local idx = #cycle_end_pos * 26 + i
  canuse[idx] = true
end
if #cycle_end_pos == 0 then
  for i = 1, n do
    local v = t[i]
    local idx = #cycle_end_pos * 26 + v
    canuse[idx] = false
  end
else
  for i = cycle_end_pos[#cycle_end_pos] + 1, n do
    local v = t[i]
    local idx = #cycle_end_pos * 26 + v
    canuse[idx] = false
  end
end
local tasks = {}
for i = 1, 26 do
  local idx = #cycle_end_pos * 26 + i
  if canuse[idx] then
    table.insert(tasks, idx)
  end
end
local done = 0
while done < #tasks do
  done = done + 1
  local invsrc = tasks[done]
  for i = 1, #invedge[invsrc] do
    local invdst = invedge[invsrc][i]
    if not canuse[invdst] then
      canuse[invdst] = true
      table.insert(tasks, invdst)
    end
  end
end
local ret = {}
local curpos = 0
for i = 1, 26 do
  if canuse[i] then
    curpos = i
    table.insert(ret, string.char(96 + i))
    break
  end
end
for ic = 1, #cycle_end_pos do
  for i = 1, #edge[curpos] do
    local dst = edge[curpos][i]
    if canuse[dst] then
      curpos = dst
      table.insert(ret, string.char(96 + dst - 26 * ic))
      break
    end
  end
end
print(table.concat(ret))

Score : 600 points

Problem Statement

A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters.
For example, arc, artistic and (an empty string) are all subsequences of artistic; abc and ci are not.

You are given a string A consisting of lowercase English letters.
Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A.
If there are more than one such string, find the lexicographically smallest one among them.

Constraints

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

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a as a subsequence, but not b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

aa

Sample Input 3

frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn

Sample Output 3

aca


local s = io.read()
local n = #s
local t = {}
for i = 1, n do t[i] = s:sub(i, i):byte() - 96 end
local endpos = {}
local box = {}
local bc = 0
local function clearbox()
  for i = 1, 26 do
    box[i] = false
  end
  bc = 0
end
clearbox()
for i = 1, n do
  if not box[t[i]] then
    box[t[i]] = true
    bc = bc + 1
  end
  if bc == 26 then
    table.insert(endpos, i)
    clearbox()
  end
end
clearbox()
if #endpos == 0 then
  for i = 1, n do
    box[t[i]] = true
  end
  for i = 1, 26 do
    if not box[i] then
      print(string.char(96 + i))
      break
    end
  end
else
  local ret = {"a"}
  local prv = 0
  for i = 1, n do if t[i] == 1 then prv = i break end end
  for i = 1, #endpos do
    clearbox()
    for j = prv + 1, endpos[i] do
      box[t[j]] = true
    end
    for j = 1, 26 do
      if not box[j] then
        table.insert(ret, string.char(96 + j))
        for k = endpos[i] + 1, n do
          if t[k] == j then
            prv = k
            break
          end
        end
        break
      end
    end
  end
  print(table.concat(ret))
end


s = io.read('*l')
if s:reverse() == s and s % 10 ~= 0 then print('Yes') else print('No') end

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No


local ior = io.input()
local n = ior:read("*n")
local lines = {}
for i = 1, n do lines[i] = {} end
for i = 1, n - 1 do
  local a, b, c = ior:read("*n", "*n", "*n")
  lines[a][b] = c
  lines[b][a] = c
end
local q, k = ior:read("*n", "*n")
local tasks = {k}
local map = {}
for i = 1, n do map[i] = -1 end
map[k] = 0
local tasknum, done = 1, 0
while(done < tasknum) do
  done = done + 1
  local taskidx = tasks[done]
  for k, v in pairs(lines[taskidx]) do
    if(map[k] == -1) then
      map[k] = map[taskidx] + v
      table.insert(tasks, k)
      tasknum = tasknum + 1
    end
  end
end
for i = 1, q do
  local x, y = io.read("*n", "*n")
  print(map[x] + map[y])
end

Score : 400 points

Problem Statement

You are given a tree with N vertices.

Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.

The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i.

You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):

find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.

Constraints

3≤N≤10^5

1≤a_i,b_i≤N (1≤i≤N-1)

1≤c_i≤10^9 (1≤i≤N-1)

The given graph is a tree.

1≤Q≤10^5

1≤K≤N

1≤x_j,y_j≤N (1≤j≤Q)

x_j≠y_j (1≤j≤Q)

x_j≠K,y_j≠K (1≤j≤Q)

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q}

Output

Print the responses to the queries in Q lines.

In the j-th line j(1≤j≤Q), print the response to the j-th query.

Sample Input 1

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

Sample Output 1

3
2
4

The shortest paths for the three queries are as follows:

Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3

Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2

Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4

Sample Input 2

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

Sample Output 2

5
14
22

The path for each query must pass Vertex K=2.

Sample Input 3

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

Sample Output 3

17000000000


n = io.read('*n')

e = {}
for i = 1, n do e[i] = {} end

for i = 1, n - 1 do
    local a, b, c = io.read('*n'), io.read('*n'), io.read('*n')
    e[a][#e[a] + 1] = { b, c }
    e[b][#e[b] + 1] = { a, c }
end

far = {}

dfs = function (u, p, d)
    far[u] = d
    for i = 1, #e[u] do if e[u][i][1] ~= p then
        dfs(e[u][i][1], u, d + e[u][i][2])
    end end
end

q, k = io.read('*n'), io.read('*n')

dfs(k, -1, 0)

for i = 1, q do print(far[io.read('*n')] + far[io.read('*n')]) end


Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


s = io.read()
io.write(s:sub(1, 1))
io.write(#s - 2)
io.write(s:sub(#s, #s))
io.write("\n")

Score : 200 points

Problem Statement

The word internationalization is sometimes abbreviated to i18n.
This comes from the fact that there are 18 letters between the first i and the last n.

You are given a string s of length at least 3 consisting of lowercase English letters.
Abbreviate s in the same way.

Constraints

3 ≤ |s| ≤ 100 (|s| denotes the length of s.)

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the abbreviation of s.

Sample Input 1

internationalization

Sample Output 1

i18n

Sample Input 2

smiles

Sample Output 2

s4s

Sample Input 3

xyz

Sample Output 3

x1z


local n = io.read("*n", "*l")
local str = io.read()
local t = {0, 0, 0}
for v in str:gmatch("%d+") do
  local a = tonumber(v)
  if a % 4 == 0 then
    t[1] = t[1] + 1
  elseif a % 2 == 0 then
    t[2] = t[2] + 1
  else
    t[3] = t[3] + 1
  end
end
if n == 1 then
  if t[3] == 1 then
    print("Yes")
  else
    print("No")
  end
elseif t[1] + 1 < t[3] then
  print("No")
elseif t[1] + 1 == t[3] then
  if t[2] == 0 then
    print("Yes")
  else
    print("No")
  end
else
  print("Yes")
end

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


local k = io.read("*n")
local div = k // 50
local rem = k % 50
local ary = {}
ary[1] = 0
for i = 2, 50 do ary[i] = 49 end
for i = 1, rem do
  for j = 1, 50 do
    ary[j] = ary[j] + (j == i and 50 or -1)
  end
end
for i = 1, 50 do ary[i] = ary[i] + div end
print(50)
for i = 1, 50 do
  io.write(ary[i])
  io.write(i == 50 and "\n" or " ")
end

Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.

Constraints

0 ≤ K ≤ 50 \times 10^{16}

Input

Input is given from Standard Input in the following format:

K

Output

Print a solution in the following format:

N
a_1 a_2 ... a_N

Here, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.

Sample Input 1

0

Sample Output 1

4
3 3 3 3

Sample Input 2

1

Sample Output 2

3
1 0 3

Sample Input 3

2

Sample Output 3

2
2 2

The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].

Sample Input 4

3

Sample Output 4

7
27 0 0 0 0 0 0

Sample Input 5

1234567894848

Sample Output 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425


n,m=io.read("*n","*n")
c,d={},{}
for i=1,m do
	local a,b=io.read("*n","*n")
	if a==1 then c[#c+1]=b end
	if b==n then d[a]=true end
end
for _,i in ipairs(c)do
	if d[i] then
		print("POSSIBLE")
		os.exit()
	end
end
print("IMPOSSIBLE")

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

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

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.


local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end

local n, k = io.read("*n", "*n")
local gcd = io.read("*n")
local max = gcd
local mma = math.max
for i = 2, n do
  local t = io.read("*n")
  gcd = getgcd(gcd, t)
  max = mma(max, t)
end
local r = k % gcd == 0 and k <= max
print(r and "POSSIBLE" or "IMPOSSIBLE")

Score : 300 points

Problem Statement

There is a box containing N balls. The i-th ball has the integer A_i written on it.
Snuke can perform the following operation any number of times:

Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq K \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.

Sample Input 1

3 7
9 3 4

Sample Output 1

POSSIBLE

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9-4)=5.
Next, take out 3 and 5, and return them back along with abs(3-5)=2.
Finally, take out 9 and 2, and return them back along with abs(9-2)=7.
Now we have 7 in the box, and the answer is therefore POSSIBLE.

Sample Input 2

3 5
6 9 3

Sample Output 2

IMPOSSIBLE

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.

Sample Input 3

4 11
11 3 7 15

Sample Output 3

POSSIBLE

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.

Sample Input 4

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE