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

local max=-math.maxinteger
for bit=1,(1<<10)-1 do
    local gain=0
    for i=1,n do
        local open=0
        for j=0,9 do
            if (bit>>j&1)>0 and f[i][j]~=0 then
                open=open+1
            end
        end
        gain=gain+p[i][open]
    end
    if max<gain then
        max=gain
    end
end
print(max)

Score : 300 points

Problem Statement

Joisino is planning to open a shop in a shopping street.

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

There are already N stores in the street, numbered 1 through N.

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

Constraints

1≤N≤100

0≤F_{i,j,k}≤1

For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.

-10^7≤P_{i,j}≤10^7

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}
:
F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}
P_{1,0} ... P_{1,10}
:
P_{N,0} ... P_{N,10}

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

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

Sample Output 1

8

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

Sample Input 2

2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1

Sample Output 2

-2

Note that a shop must be open during at least one period, and the profit may be negative.

Sample Input 3

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

Sample Output 3

23


local n, c = io.read("*n", "*n")
local tbl = {}
for i = 1, n do
  local tmp = {}
  tmp.s, tmp.t, tmp.c = io.read("*n", "*n", "*n")
  tbl[i] = tmp
end
table.sort(tbl, function(a, b) return a.s < b.s end)
local rec_end = {tbl[1].t}
local rec_ch = {tbl[1].c}

for i = 2, n do
  local fpos = nil
  for ir = 1, #rec_end do
    if rec_end[ir] == tbl[i].s and rec_ch[ir] == tbl[i].c then
      rec_end[ir] = tbl[i].t
      fpos = ir
      break
    elseif rec_end[ir] < tbl[i].s then
      if not fpos then
        fpos = ir
      else
        if rec_end[ir] < rec_end[fpos] then
          fpos = ir
        end
      end
    end
  end
  if fpos then
    rec_end[fpos] = tbl[i].t
  else
    table.insert(rec_end, tbl[i].t)
    table.insert(rec_ch, tbl[i].c)
  end
end
print(#rec_end)

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

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2

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 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
end
print(f and "YES" or "NO")

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


s = io.read()
t = {0, 0, 0}
for i = 1, #s do
  v = s:byte(i) - 96
  t[v] = t[v] + 1
end
table.sort(t)
print(t[3] - t[1] <= 1 and "YES" or "NO")

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


local s=io.read()
local sable={a=0,b=0,c=0}
for i=1,#s do
    s_i=s:sub(i,i)
    sable[s_i]=sable[s_i]+1
end

if math.max(sable.a,sable.b,sable.c)-math.min(sable.a,sable.b,sable.c)<=1 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")
a=N%10
b=N//10%10
c=N//100%10
d=N//1000
if a==b and b==c or b==c and c==d then
  print("Yes")
  else
  print("No")
end


N=io.read("n")
function lucas(n)
  local L={}
  L[0]=2
  L[1]=1
  L[n]=lucas(n-1)+lucas(n-2)
  return L[n]
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


A={io.read(1),io.read(1),io.read(1),io.read(1)}

for bit=0,8,1 do
  a=A[1]
  for i=0,2,1 do
    if (bit>>i)&1==1 then a=a-A[i+2] else a=a+A[i+2] end
  end
  if math.floor(a)==7 then
    ans=A[1]
    for i=0,2,1 do
      if (bit>>i)&1==1 then ans=ans.."-"..A[i+2] else ans=ans.."+"..A[i+2] end
    end
    print(ans.."=7")
    break
  end
end

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


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

--Warshall-Floyd法
for i=0,9 do
    for j=0,9 do
        for k=0,9 do
            c[i][j]=math.min(c[i][j],c[i][k]+c[k][j])
        end
    end
end

local counter=0
for i=1,h do
    for j=1,w do
        local input=io.read("n")
        if input>=0 then
            counter=counter+c[input][1]
        end
    end
end
print(counter)

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


local mmi = math.min
local h, w = io.read("*n", "*n")
local map = {}
for i = 1, 100 do map[i] = io.read("*n") end

local tasks = {2}
local tasknum, done = 1, 0
local cost = {}
for i = 1, 10 do cost[i] = 10000 end
cost[2] = 0
while(done < tasknum) do
  done = done + 1
  local dst = tasks[done]
  for i = 1, 10 do
    if(i ~= 2 and i ~= dst) then
      local v = cost[dst] + map[dst + (i - 1) * 10]
      if v < cost[i] then
        cost[i] = v
        tasknum = tasknum + 1
        table.insert(tasks, i)
      end
    end
  end
end

local tot = 0
for i = 1, h * w do
  local a = io.read("*n")
  if 0 <= a then
    tot = tot + cost[a + 1]
  end
end
print(tot)


X=io.read(1)
io.read(1)
Y=io.read(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"<")


Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


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


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 N>=2 then
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
end
table.sort(t)
print(t[#t])

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


function binary_search(tbl, key, imin, imax)
    if imax < imin then
        return imin
    end

    local imid = imin + math.floor((imax - imin)/2)
    if tbl[imid] > key then
        return binary_search(tbl, key, imin, imid - 1)
    elseif tbl[imid] < key then
        return binary_search(tbl, key, imid + 1, imax)
    else
        return imid
    end
end

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

    if #a == 0 then
        return math.abs(z - w)
    end

    local sorted_nums = {a[#a]}
    local max_diff = math.abs(a[#a] - w)
    for i = #a - 1, 1, -1 do
        local idx = binary_search(sorted_nums, a[i], 1, #sorted_nums)
        local min_diff = 1000000000
        if sorted_nums[idx] then
            min_diff = math.abs(sorted_nums[idx] - a[i])
        end
        if sorted_nums[idx-1] then
            local diff = math.abs(sorted_nums[idx-1] - a[i])
            if diff < min_diff then
                min_diff = diff
            end
        end
        if min_diff > max_diff then
            max_diff = min_diff
        end
        table.insert(sorted_nums, a[i], idx)
    end
    return max_diff
end

print(main())


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


local function upper_bound(key,ary)
    local n=#ary
    if n==0 then
        return 1
    end
    if key<ary[1] then
        return 1
    end
    if key>=ary[n] then
        return n+1
    end
    local min,max=1,n
    while 1<max-min do
        local mid=(min+max)//2
        if key>=ary[mid] then
            min=mid
        else
            max=mid
        end
    end
    return max
end

local function lower_bound(key,ary)
    local n=#ary
    if n==0 then
        return 1
    end
    if key<=ary[1] then
        return 1
    end
    if key>ary[n] then
        return n+1
    end
    local min,max=1,n
    while 1<max-min do
        local mid=(min+max)//2
        if key>ary[mid] then
            min=mid
        else
            max=mid
        end
    end
    return max
end

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

local combination=0
for i=1,n do
    ab=lower_bound(b[i],a)-1
    bc=n-upper_bound(b[i],c)+1
    combination=combination+ab*bc
end
print(combination)

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


function calculate_pattern(parts, i)
    local under_pointer = 0
    for p = 1, #parts[i] do
        while under_pointer + 1 <= #parts[i+1] and parts[i+1][under_pointer+1].size > parts[i][p].size do
            under_pointer = under_pointer + 1
        end
        if under_pointer > 0 then
            parts[i][p].pattern = pattern_sum(parts, i+1, under_pointer)
        else
            parts[i][p].pattern = 0
        end
    end
end

pattern_sum = nil
do
    local memo = {}
    for i = 1, 3 do
        table.insert(memo, {})
    end
    function pattern_sum(parts, i, p_max)
        if memo[i][p_max] then
            return memo[i][p_max]
        end

        local sum = 0
        for p = 1, p_max do
            sum = sum + parts[i][p].pattern
        end
        memo[i][p_max] = sum
        return sum
    end
end

function main()
    local inputs = {}
    for i = 1, 4 do
        table.insert(inputs, io.read())
    end

    local n = tonumber(inputs[1])
    local parts = {}
    for i = 1, 3 do
        table.insert(parts, {})
        for size in inputs[1+i]:gmatch("%d+") do
            table.insert(parts[i], {size = tonumber(size), pattern = 1})
        end
    end

    for i = 1, #parts do
        table.sort(parts[i], function(a, b) return b.size < a.size end)
    end

    for i = #parts - 1, 1, -1 do
        calculate_pattern(parts, i)
    end

    print(pattern_sum(parts, 1, #parts[1]))
end

main()


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
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


r=io.read("*n")
g=io.read("*n")
print(math.ceil(2 * g - r))

Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


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


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

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


local s=io.read()
local l,r=1,#s
local counter=0
while l<r do
    sl=s:sub(l,l)
    sr=s:sub(r,r)
    if sl==sr then
        l=l+1
        r=r-1
    elseif sl=="x" then
        l=l+1
        counter=counter+1
    elseif sr=="x" then
        r=r-1
        counter=counter+1
    else
        print(-1)
        return
    end
end
print(counter)

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

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

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


local s = io.read()
local n = #s
local cnt = 0
local t = {}
for i = 1, n do
  local sstr = s:sub(i, i)
  if sstr == "x" then
    cnt = cnt + 1
  else
    local tmp = {}
    tmp.c = cnt
    tmp.s = sstr
    table.insert(t, tmp)
    cnt = 0
  end
end
local ret = 0
local mma = math.max
local nt = #t
local isok = true

local half = nt // 2
for i = 1, half do
  if t[i].s ~= t[nt + 1 - i].s then
    isok = false
    break
  else
    if i == 1 then
      ret = ret + mma(t[i].c, cnt)
    else
      ret = ret + mma(t[i].c, t[nt + 2 - i].c)
    end
  end
end
if nt % 2 == 1 then
  if nt == 1 then
    ret = ret + mma(t[1].c, cnt)
  else
    ret = ret + mma(t[half + 1].c, t[half + 2].c)
  end
end
print(isok and ret or -1)


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

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


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


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

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

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

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

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

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

----------

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

local counter=0
for i=1,m do
    init(n)
    for j=1,m do
        if i~=j then
            unite(a[j],b[j])
        end
    end
    if not same(a[i],b[i]) then
        counter=counter+1
    end
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.


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")
local f = false
for i = 1, 3500 do
  for j = 1, 3500 do
    local numer = n * i * j
    local denom = 4 * i * j - n * (i + j)
    if 0 < denom and numer % denom == 0 then
      print(i .. " " .. j .. " " .. math.floor(numer / denom))
      f = true
      break
    end
  end
  if f then break end
end


s = io.read()
print(s:find("YAKI") == 1 and "Yes" or "No")

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

1 \leq |S| \leq 10

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.

Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.

Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

Sample Input 3

YAK

Sample Output 3

No


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

for i=0,n do
    for j=0,m do
        if (n-i)*j+(m-j)*i==k then
            print("Yes")
            return
        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 count={}
for k,v in pairs(map) do
    count[v]=(count[v] or 0)+1
end

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

while g1>0 do
    local checker=false
    for i=1,10000 do
        if i%4==1 or i%4==3 then
            if count[i] and count[i]>0 then
                count[i]=count[i]-1
                count[i-1]=(count[i-1] or 0)+1
                checker=true
                g1=g1-1
            end
        end
    end
    if not checker then
        print("No")
        return
    end
end

while g4>0 do
    local checker=false
    for i=1,10000 do
        if i%2==0 then
            if count[i] and count[i]>0 then
                count[i]=count[i]-1
                count[i-4]=(count[i-4] or 0)+1
                checker=true
                g4=g4-1
            end
        end
    end
    if not checker then
        print("No")
        return
    end
end

while g2>0 do
    local checker=false
    for i=1,10000 do
        if i%2==0 then
            if count[i] and count[i]>0 then
                count[i]=count[i]-1
                count[i-2]=(count[i-2] or 0)+1
                checker=true
                g2=g2-1
            end
        end
    end
    if not checker then
        print("No")
        return
    end
end

print("Yes")

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
    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 k,v in pairs(map) do
    if g1==0 then
        break
    end
    if map[k]%4==1 or map[k]%4==3 then
        while map[k]>0 do
            map[k]=map[k]-1
            g1=g1-1
        end
    end
end

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

for k,v in pairs(map) do
    if g1==0 then
        break
    end
    if map[k]%4==0 then
        while map[k]>0 do
            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")


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

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

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

for i=1,g4 do
    for k,v in pairs(map) do
        if v%4==0 then
            map[k]=map[k]-4
            break
        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")


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
            end
            if 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


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


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

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


n=io.read("*n")
a={}
for i=1,n do
    number=io.read("*n")
    if not a[number] or a[number]==0 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 n = io.read("*n")
local t = {}
for i = 1, n do
  local a = io.read("*n")
  if(t[a] ~= nil) then t[a] = nil else t[a] = true end
end
local cnt = 0
for k, v in pairs(t) do cnt = cnt + 1 end
print(cnt)


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 edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b], edge[b][a] = c, c
end
local inf = 1000000007

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 ret = 1000000007 * 1000

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
for i = 0, tot - 1 do
  local pat = getpattern(r, tot, i)
  local z = 0
  for i = 1, r - 1 do
    z = z + getlength(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 do
    local mindist=INF
    local r1=town[i]
    for j=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] = 20000001
    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))


local X, t = io.read("n","n")
print(math.max(0, X - t))

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


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

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

Each character in s is a lowercase English letter.

1≤|s|≤10^5

Input

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

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.

Sample Input 1

atcoder

Sample Output 1

acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.

Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh


n = io.read("*n")
t = {}
for i = 1, n do
  a = io.read("*n")
  for j = -1, 1 do
    if not t[a + j] then t[a + j] = 1
    else t[a + j] = t[a + j] + 1
    end
  end
end
c = 0
for k, v in pairs(t) do
  if c < v then c = v end
end
print(c)

Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

a_i is an integer.

Input

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

N
a_1 a_2 .. a_N

Output

Print the maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1


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

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

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


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

counter=0
::loop::
for i=1,n-1 do
    if p[i]==i or p[i+1]==i+1 then
        p[i],p[i+1]=p[i+1],p[i]
        counter=counter+1
    end
end
for i=1,n do
    if p[i]==i then
        goto loop
    end
end
print(counter)


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


local q, h, s, d = io.read("*n", "*n", "*n", "*n")
local n = io.read("*n")
s = math.min(s, q * 4)
s = math.min(s, h * 2)
if s * 2 < d then
  print(s * n)
else
  print((n // 2) * d + n % 2 * s)
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


x,a,b=io.read("*n","*n","*n")
print(math.abs(x-a)<math.abs(x-b) and "A" or "B")

Score : 100 points

Problem Statement

Snuke lives at position x on a number line.
On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery.

Snuke decided to get food delivery from the closer of stores A and B.
Find out which store is closer to Snuke's residence.

Here, the distance between two points s and t on a number line is represented by |s-t|.

Constraints

1 \leq x \leq 1000

1 \leq a \leq 1000

1 \leq b \leq 1000

x, a and b are pairwise distinct.

The distances between Snuke's residence and stores A and B are different.

Input

Input is given from Standard Input in the following format:

x a b

Output

If store A is closer, print A; if store B is closer, print B.

Sample Input 1

5 2 7

Sample Output 1

B

The distances between Snuke's residence and stores A and B are 3 and 2, respectively.
Since store B is closer, print B.

Sample Input 2

1 999 1000

Sample Output 2

A


x = io.read "n"
a = io.read "n"
b = io.read "n"

if math.abs(x-a) < math.abs(x-b) then
    io.write("A\n")
else
    io.write("B\n")
end


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

local barmax=0
for length,number in pairs(a) do
    if number>1 then
        barmax=math.max(length,barmax)
    end
end
if barmax==0 then
    print(0)
    return
end
if a[barmax]>=4 then
    print(barmax*barmax)
else
    local barmax2=0
    for length,number in pairs(a) do
        if length<barmax and number>1 then
            barmax2=math.max(length,barmax2)
        end
    end
    print(barmax*barmax2)
end

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


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

local barmax=0
for length,number in pairs(a) do
    if number>1 then
        barmax=math.max(length,barmax)
    end
end
if barmax==0 then
    print(0)
end
if a[barmax]>=4 then
    print(barmax*barmax)
else
    local barmax2=0
    for length,number in pairs(a) do
        if length<barmax and number>1 then
            barmax2=math.max(length,barmax2)
        end
    end
    print(barmax*barmax2)
end


local n = io.read("*n", "*l")
local str = io.read()
local mod = 1000000007
local _unused = io.read()
local t = {str:byte(1, n)}
local ret = 1
local prevstate = 0
local i = 1
while(i <= n) do
  if(i ~= n and t[i] == t[i + 1]) then
    if(prevstate == 0) then
      ret = (ret * 6) % mod
    elseif(prevstate == 1) then
      ret = (ret * 3) % mod
    else
      ret = (ret * 2) % mod
    end
    i = i + 2
    prevstate = 1
  else
    if(prevstate == 0) then
      ret = (ret * 3) % mod
    elseif(prevstate == 1) then
      --  * 1
    else
      ret = (ret * 2) % mod
    end
    i = i + 1
    prevstate = 2
  end
end
print(ret)

Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902


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 function gcd(a,b)
    if b>0 then
        return gcd(b,a%b)
    else
        return a
    end
end

local function lcm(a,b)
    return (a//gcd(a,b))*b
end

local time=1
local n=io.read("*n")
for i=1,n do
    time=lcm(time,io.read("*n"))
end
print(time)

Score : 300 points

Problem Statement

We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly T_i seconds.

Initially, the hand of every clock stands still, pointing directly upward.

Now, Dolphin starts all the clocks simultaneously.

In how many seconds will the hand of every clock point directly upward again?

Constraints

1≤N≤100

1≤T_i≤10^{18}

All input values are integers.

The correct answer is at most 10^{18} seconds.

Input

Input is given from Standard Input in the following format:

N
T_1
:
T_N

Output

Print the number of seconds after which the hand of every clock point directly upward again.

Sample Input 1

2
2
3

Sample Output 1

6

We have two clocks. The time when the hand of each clock points upward is as follows:

Clock 1: 2, 4, 6, ... seconds after the beginning

Clock 2: 3, 6, 9, ... seconds after the beginning

Therefore, it takes 6 seconds until the hands of both clocks point directly upward.

Sample Input 2

5
2
5
10
1000000000000000000
1000000000000000000

Sample Output 2

1000000000000000000


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:


local h,w=io.read("n","n")
local n=io.read("n")
local grid={}
for i=1,n do
    local a=io.read("n")
    while a>0 do
        table.insert(grid,i)
        a=a-1
    end
end

for i=1,h do
    local g={}
    if i%2==1 then
        for j=1,w do
            g[j]=grid[j+w*(i-1)]
        end
    else
        for j=1,w do
            g[w-j+1]=grid[j+w*(i-1)]
        end
    end
    print(table.concat(g," "))
end

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

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

Sample Input 3

1 1
1
1

Sample Output 3

1


Score : 100 points

Problem Statement

This contest, AtCoder Beginner Contest, is abbreviated as ABC.

When we refer to a specific round of ABC, a three-digit number is appended after ABC. For example, ABC680 is the 680th round of ABC.

What is the abbreviation for the N-th round of ABC? Write a program to output the answer.

Constraints

100 ≤ N ≤ 999

Input

Input is given from Standard Input in the following format:

N

Output

Print the abbreviation for the N-th round of ABC.

Sample Input 1

100

Sample Output 1

ABC100

The 100th round of ABC is ABC100.

Sample Input 2

425

Sample Output 2

ABC425

Sample Input 3

999

Sample Output 3

ABC999


a,b=io.read():match("(.+)%s(.+)")
print((a%3==0 or b%3==0 or (a+b)%3==0)and "Possible"or"Impossible")

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

Sample Input 1

4 5

Sample Output 1

Possible

If Snuke gives nine cookies, each of the three goats can have three cookies.

Sample Input 2

1 1

Sample Output 2

Impossible

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.


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

local total_i=0
local diff=10^19
for i=1,n-1 do
    total_i=total_i+a[i]
    diff=math.min(diff,math.abs(total-2*total_i))
end
print(diff)

Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

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

-10^{9} \leq a_i \leq 10^{9}

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.


local function combination(x,y)
    if y==0 then
        return 1
    else
        return x*combination(x-1,y-1)//y
    end
end

n,p=io.read("*n","*n")
local co=0
local ce=0
for i=1,n do
    if io.read("*n")%2==1 then
        co=co+1
    end
end
ce=n-co

local cootal=0
local ceotal=0
for i=p,co,2 do
    cootal=cootal+combination(co,i)
end
for i=0,ce do
    ceotal=ceotal+combination(ce,i)
end
print(cootal*ceotal)

Score : 200 points

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

Takaki will select some of these bags and eat all of the biscuits inside.
Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2.
How many such ways to select bags there are?

Constraints

1 \leq N \leq 50

P = 0 or 1

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 ... A_N

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.

Sample Input 1

2 0
1 3

Sample Output 1

2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

Select neither bag. The total number of biscuits is 0.

Select both bags. The total number of biscuits is 4.

Sample Input 2

1 1
50

Sample Output 2

0

Sample Input 3

3 0
1 1 1

Sample Output 3

4

Two bags are distinguished even if they contain the same number of biscuits.

Sample Input 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

17592186044416


local n, p = io.read("*n", "*n")
local ev, od = {}, {}
local a = io.read("*n")
if(a % 2 == 0) then ev[1], od[1] = 2, 0
else ev[1], od[1] = 1, 1 end
for i = 2, n do
  a = io.read("*n")
  if(a % 2 == 0) then
    ev[i] = ev[i - 1] * 2
    od[i] = od[i - 1] * 2
  else
    ev[i] = ev[i - 1] + od[i - 1]
    od[i] = ev[i - 1] + od[i - 1]
  end
end
if(p == 0) then print(ev[n]) else print(od[n]) end


x={}
for i in io.read():gmatch("%d+")do
  table.insert(i)
end
table.sort(x)
print(math.floor(x[1]+x[2]))

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


n = io.read("*n")
a, b = {}, {}
for i = 1, n do
  z = io.read("*n")
  if i % 2 == 1 then
    table.insert(a, z)
  else
    table.insert(b, z)
  end
end
if n % 2 == 0 then
  c = {}
  for i = #b, 1, -1 do
    table.insert(c, b[i])
  end
  for i = 1, #a do
    table.insert(c, a[i])
  end
  print(table.concat(c, " "))
else
  c = {}
  for i = #a, 1, -1 do
    table.insert(c, a[i])
  end
  for i = 1, #b do
    table.insert(c, b[i])
  end
  print(table.concat(c, " "))
end

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


local n=io.read("*n")
local b={}
for i=1,n do
    local a=io.read("*n")
    if i%2==1 then
        table.insert(b,a)
    else
        table.insert(b,1,a)
    end
end
if n%2==0 then
    print(table.concat(b," "))
else
    local c={}
    for i=n,1,-1 do
        table.insert(c,b[i])
    end
    print(table.concat(c," "))
end


local n=io.read("*n")
local b={}
for i=1,n do
    local a=io.read("*n")
    if i%2==1 then
        table.insert(b,a)
    else
        table.insert(b,1,a)
    end
end
print(table.concat(b," "))


local n = io.read("*n")
local t = {}
for i = 1, n do
  if(i % 2 == 1) then
    table.insert(t, io.read("*n"))
  else
    table.insert(t, 1, io.read("*n"))
  end
end
if(n % 2 == 0) then
  io.write(t[1])
  for i = 2, n do
    io.write(" "..t[i])
  end
  io.write("\n")
else
  io.write(t[n])
  for i = n - 1, 1, -1 do
    io.write(" "..t[i])
  end
  io.write("\n")
end


x,a,b=io.read():match("(.+)%s(.+)%s(.+)")
if b-a<=0 then
  print("delicious")
elseif b-a <=x*1 then
  print("safe")
else
  print("dangerous")
end

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


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

i=1
j=0
counter=0
while j~=2 do
    j=a[i]
    if a[i]==j and a[j]==i then
        print(-1)
        return
    end
    i=j
    counter=counter+1
end
print(counter)

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


local n = io.read("*n")
local x, y = {}, {}
local xidx, yidx = {}, {}
for i = 1, n do
  x[i], y[i] = io.read("*n", "*n")
  xidx[i], yidx[i] = i, i
end
table.sort(xidx, function(a, b) return x[a] < x[b] end)
table.sort(yidx, function(a, b) return y[a] < y[b] end)
local edge = {}
for i = 1, n - 1 do
  local v1, v2 = xidx[i], xidx[i + 1]
  edge[i] = {v1, v2, x[v2] - x[v1]}
end
for i = 1, n - 1 do
  local v1, v2 = yidx[i], yidx[i + 1]
  edge[n - 1 + i] = {v1, v2, y[v2] - y[v1]}
end

local function uf_initialize(n)
  local parent = {}
  for i = 1, n do parent[i] = i end
  return parent
end

local function uf_findroot(idx, parent)
  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

local function kruskal(n, line)
  table.sort(line, function(a, b) return a[3] < b[3] end)
  local parent = uf_initialize(n)
  local totalcost = 0
  local used = 0
  local linenum = #line
  for i = 1, linenum do
    local c, i1, i2 = line[i][3], line[i][1], line[i][2]
    local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent)
    parent[i1], parent[i2] = r1, r2
    if r1 ~= r2 then
      parent[r2] = r1
      parent[i2] = r1
      totalcost = totalcost + c
      used = used + 1
      if used == n - 1 then break end
    end
  end
  return totalcost
end

print(kruskal(n, edge))

Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8