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

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

local count = 1
local sum = 0
while day > 0 and count <= #input do
  if day >= input[count].day then
    sum = sum + input[count].result
    day = day - 1
  end
  count = count + 1
end

print(sum)


A=io.read"*n"
B=io.read"*n"
C=io.read"*n"
print(math.max(0,C-(A-B)))

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

Score : 100 points

Problem Statement

We have two bottles for holding water.

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

Bottle 2 contains C milliliters of water.

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

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

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

Sample Input 1

6 4 3

Sample Output 1

1

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

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0

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 x =nil
local y =nil

A=1<=B<=A>=20
B=1<=B<=A>=20
C=1<=C<=20

x=B
y=C

y=C-(A-B)

print(y)


N=io.read"*n"
s=0
for i=1,N do
	if(math.floor(math.log10(i)+1)%2==1)then s=s+1 end
end
print(s)

Score : 200 points

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

Sample Input 1

11

Sample Output 1

9

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.

Sample Input 2

136

Sample Output 2

46

In addition to 1, 2, \ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \ldots, 136.

Sample Input 3

100000

Sample Output 3

90909


local N = io.read("n")
local a = 0
for i=1,N do
    if i < 10 then
        a = a + 1
    elseif i >= 100 then
        if i < 1000 then
            a = a + 1
        elseif i >= 10000 then
            if i < 100000 then
                a = a + 1
            end
        end
    end
end
print(a)


local function reada(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return table.unpack(r)end
N=io.read"*n"
K=io.read"*n"
A=reada(N)
s=0
for i=1,N do s=s+A[i]end
d={}
for i=1,math.sqrt(s)+1 do
	if(s%i==0)then
		table.insert(d,i)
		if(i~=math.floor(s/i))then
			table.insert(d,math.floor(s/i))
		end
	end
end
table.sort(d,function(a,b)return a>b end)
print(table.concat(d," "))
for _,d in ipairs(d)do
	t=0
	for i=1,N do
		t=t+math.abs(math.min(A[i]%d,d-A[i]%d))
	end
	if(t<=K)then print(d) return end
end

Score : 500 points

Problem Statement

We have a sequence of N integers: A_1, A_2, \cdots, A_N.

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

Choose two integers i and j such that i \neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_{N-1} A_{N}

Output

Print the maximum possible positive integer that divides every element of A after the operations.

Sample Input 1

2 3
8 20

Sample Output 1

7

7 will divide every element of A if, for example, we perform the following operation:

Choose i = 2, j = 1. A becomes (7, 21).

We cannot reach the situation where 8 or greater integer divides every element of A.

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

Choose i = 2, j = 1. A becomes (2, 6).

Choose i = 2, j = 1. A becomes (1, 7).

Choose i = 2, j = 1. A becomes (0, 8).

Choose i = 2, j = 1. A becomes (-1, 9).

Choose i = 1, j = 2. A becomes (0, 8).

Then, 0 = 8 \times 0 and 8 = 8 \times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


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

Score: 100 points

Problem Statement

We have two distinct integers A and B.

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

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

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

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

Sample Input 1

2 16

Sample Output 1

9

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

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


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

Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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


local N = io.read("n")
local A = {} for i=1,N+1 do A[i] = io.read("n") end
local B = {} for i=1,N do B[i] = io.read("n") end
B[N+1]=0
local count = 0
local prev_power = 0
for i=1,N+1 do
  local mon = A[i]
  if mon <= prev_power then
    count = count + mon
    mon = 0
  else
    count = count + prev_power
    mon = mon - prev_power
  end
  local power = B[i]
  if mon <= power then
    prev_power = power - mon
    count = count + mon
  else
    prev_power = 0
    count = count + power
  end
end
print(count)

Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

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

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

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

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


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

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

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

ans=0
for i=1,n do
  local s=math.min(a[i], b[i])
  ans=ans+s
  a[i]=a[i]-s
  b[i]=b[i]-s
  if b[i]>0 then
    local s2=math.min(a[i+1], b[i])
    ans=ans+s2
    a[i+1]=a[i+1]-s2
    b[i]=b[i]-s2
  end
end

print(ans)


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

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

local map_newm = {}
local map_oldm = {}
for nd=0,9 do
    for oldm=0,12 do
        local x = oldm * 10 + nd
        local y = (oldm * 10 + nd) % 13
        map_newm[x] = y
        map_oldm[x] = oldm
    end
end
local N = #S
local ti = array(1, 0)
local tio = array(1, 0)
tio[0] = 1
for i=1,N do
    if S[i] == '?' then
        for m=0,12 do
            ti[m] = 0
        end
        for x=0,129 do
            local newm = map_newm[x]
            local oldm = map_oldm[x]
            ti[newm] = (ti[newm] + tio[oldm]) % MOD
        end
    else
        for oldm=0,12 do
            local newm = (oldm * 10 + S[i]) % 13
            ti[newm] = tio[oldm]
        end
    end
    tio, ti = ti, tio
end
print(tio[5])

Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

??2??5

Sample Output 1

768

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

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

153716888


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

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

local map_newm = {}
local map_oldm = {}
for nd=0,9 do
    for oldm=0,12 do
        local x = oldm * 10 + nd
        local y = (oldm * 10 + nd) % 13
        --print(nd, oldm, x, y)
        map_newm[x] = y
        map_oldm[x] = oldm
    end
end
local N = #S
local tbl = array(2, 0)
tbl[0][0] = 1
for i=1,N do
    local tio = tbl[i-1]
    local ti = tbl[i]
    if S[i] == '?' then
        for x=0,129 do
            local newm = map_newm[x]
            local oldm = map_oldm[x]
            ti[newm] = (ti[newm] + tio[oldm]) % MOD
        end
    else
        for oldm=0,12 do
            local newm = (oldm * 10 + S[i]) % 13
            ti[newm] = tio[oldm]
        end
    end
end
print(tbl[N][5])


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

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


Score : 600 points

Problem Statement

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

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

Notes

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

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

Constraints

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

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

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

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

Sample Input 1

abcabab
ab

Sample Output 1

3

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

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

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

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

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

Sample Input 3

aba
baaab

Sample Output 3

0

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


Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


n = io.read("*n")
t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local mma = math.max
left = {t[1]}
for i = 2, n do
  left[i] = mma(left[i - 1], t[i])
end
local right = {}
for i = 1, n do right[i] = 0 end
right[n] = t[n]
for i = n - 1, 1, -1 do
  right[i] = mma(right[i + 1], t[i])
end
print(right[2])
for i = 2, n - 1 do
  print(math.max(left[i - 1], right[i + 1]))
end
print(left[n - 1])

Score : 300 points

Problem Statement

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

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

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

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

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

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

Sample Input 2

2
5
5

Sample Output 2

5
5


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

local INF=math.maxinteger
local dp={}
for i=1,n do
    dp[i]=INF
end

local len=0
for i=n,1,-1 do
    local k=upper_bound(a[i],dp)
    dp[k]=a[i]
    len=math.max(k,len)
end
print(len)

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


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=math.floor((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={}
for i=1,n do
    a[i]=io.read("n")
end

local INF=math.maxinteger
local dp={}
for i=1,n do
    dp[i]=INF
end

local len=0
for i=n,1,-1 do
    local k=lower_bound(a[i],dp)
    dp[k]=a[i]
    len=math.max(k,len)
end
print(len)


local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

-- binary search
-- https://golang.org/src/sort/search.go?s=2246:2286#L49
local function find_min_true(n, f)
    local function loop(i, j)
        if i == j then
            assert(i >= 1 and i <= n + 1)
            return i
        end
        local h = (i + j) // 2
        assert(h >= 1 and h <= n)
        if f(h) then
            return loop(i, h)
        else
            return loop(h + 1, j)
        end
    end
    return loop(1, n + 1)
end

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

local colors = {} -- descending
local function search(a)
    local function f(i)
        return colors[i] < a
    end
    return find_min_true(#colors, f)
end
table.insert(colors, A[1])

for i=2,N do
    local a = A[i]
    local j = search(a)
    dbgpr_t(colors)
    colors[j] = a -- replace or append to last
    dbgpr(i, a, j, #colors)
end

print(#colors)


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local colnum = 1
local allcol = {1}
for i = 2, n do
  local added = false
  for j = 1, colnum do
    if a[allcol[j]] < a[i] then
      added = true
      allcol[j] = i
      break
    end
  end
  if not added then
    colnum = colnum + 1
    table.insert(allcol, i)
  end
end
print(colnum)


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

if a[0]==n then
    print("Yes")
elseif n%3==0 then
    local b={}
    local thirds=true
    for hat,number in pairs(a) do
        if number~=n//3 then
            thirds=false
        end
        table.insert(b,hat)
    end
    if a[0]==n//3 and #b==2 then
        print("Yes")
    elseif #b==3 then
        if thirds and b[1]~b[2]~b[3]==0 then
            print("Yes")
        else
            print("No")
        end
    else
        print("No")
    end
else
    print("No")
end

Score : 300 points

Problem Statement

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

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

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

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

What is XOR?

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

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

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

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

Sample Input 2

4
1 2 4 8

Sample Output 2

No

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


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

local flag=true
for i=1,n do
    if a[i-1]~a[i+1]~=a[i] then
        flag=false
        break
    end
end
print(flag and "Yes" or "No")


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

local n = io.read("*n")
local a = {}
local allzero = true
for i = 1, n do
  a[i] = io.read("*n")
  if a[i] ~= 0 then allzero = false end
end
if allzero then
  print("Yes")
elseif n % 3 ~= 0 then
  print("No")
else
  local x, y, z = a[1], a[2], a[3]
  if getxor(x, y) ~= z then
    print("No")
  else
    local f = true
    for i = 2, n // 3 do
      if a[i * 3 - 2] ~= a[1] or a[i * 3 - 1] ~= a[2] or a[i * 3] ~= a[3] then
        f = false
        break
      end
    end
    print(f and "Yes" or "No")
  end
end


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, n do self.stage[stagenum][i] = i end
  for i = n + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx, value)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local edge = {}
local edgenum = {}
local offset = {}
for i = 1, n do
  edge[i] = {}
  edgenum[i] = 0
  offset[i] = 0
end
local inf = 1000000007
edgenum[n + 1] = inf
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
  edgenum[a] = edgenum[a] + 1
  edgenum[b] = edgenum[b] + 1
end
if m % 2 == 1 then
  print(-1)
  os.exit()
end
local st = SegTree.new(n, function(a, b) return edgenum[a] < edgenum[b] and a or b end, n + 1)
for i = 1, n do
  local src = st.stage[1][1]
  local dsts = {}
  for dst, _u in pairs(edge[src]) do
    table.insert(dsts, dst)
  end
  local lim = mfl(#dsts / 2)
  for j = 1, lim * 2 do
    local dst = dsts[j]
    print(src .. " " .. dst)
    edge[dst][src] = nil
    edgenum[dst] = edgenum[dst] - 1
    st:update(dst)
  end
  if #dsts % 2 == 1 then
    local dst = dsts[#dsts]
    if offset[src] == 0 then
      print(dst .. " " .. src)
      offset[dst] = (offset[dst] + 1) % 2
    else
      print(src .. " " .. dst)
    end
    edge[dst][src] = nil
    edgenum[dst] = edgenum[dst] - 1
    st:update(dst)
  end
  edgenum[src] = inf
  st:update(src)
end

Score : 700 points

Problem Statement

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

Notes

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

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

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

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

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

C_1 D_1
:
C_M D_M

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

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

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

Sample Input 2

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

Sample Output 2

-1


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


Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


function isInteger(inVal)
    return inVal%1 ..""=="0.0"
end

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

counter = 0
for i = 1, N do
    for j = 1, N do
        if i < j then
            sum = 0
            for k = 1, D do
                sum = sum + (X[i][k] - X[j][k])^2
                if k == D and isInteger(math.sqrt(sum)) then
                    counter = counter + 1
                end
            end
        end
    end
end
print(counter)

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n, d = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = {}
  for j = 1, d do
    t[i][j] = io.read("*n")
  end
end
local cnt = 0
for i = 1, n - 1 do
  for j = i + 1, n do
    local sumsq = 0
    for k = 1, d do
      sumsq = sumsq + (t[i][k] - t[j][k]) * (t[i][k] - t[j][k])
    end
    local cand = mfl(msq(sumsq))
    if cand * cand == sumsq or (cand + 1) * (cand + 1) == sumsq then
      cnt = cnt + 1
    end
  end
end
print(cnt)


Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


local N = io.read("*n")
local d = {}
for i = 1, N do
    d[i] = io.read("*n")
end
table.sort(d)
print(d[(N/2)+1]-d[N/2])

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


local N = io.read("n")
local D = {}
for i=1, N do
    D[i] = io.read("n")
end
table.sort(D)
local half = N // 2
local low = D[half]
local high = D[half+1]
local ans = high - low
print(ans)


local mod=1000000007

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

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

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

local function bmod(x)
    x=tostring(x)
    local ten=10
    local cry=1
    local xod=0
    for i=#x,1,-1 do
        local xub=tonumber(x:sub(i,i))
        xod=badd(xod,bmul(xub,cry))
        cry=bmul(cry,ten)
    end
    return xod
end

local function bpow(x,y)
    x=bmod(x)
    y=bmod(y)
    local xpy=1
    while 0<y do
        if y%2==1 then
            xpy=bmul(xpy,x)
            y=y-1
        end
        x=bmul(x,x)
        y=y//2
    end
    return xpy
end

local function comb(x,y)
    local xyi=1
    local yii=1
    for i=1,y do
        xyi=bmul(xyi,x+1-i)
        yii=bmul(yii,i)
    end
    return bmul(xyi,bpow(yii,mod-2))
end

local n,k=io.read("n","n")
for i=1,k do
    print(comb(n-k+1,i)*comb(k-1,i-1)%mod)
end

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
-- returns (b^n) % m
local function modpow(b, n, m)
    if n == 0 then
        return 1
    elseif n % 2 == 0 then
        -- be careful about overflow of b*b
        return modpow(b * b % m, n//2, m)
    else
        return b * modpow(b, n - 1, m) % m
    end
end

-- returns n! % m
local function modfactorial(n, m)
    local a = 1
    for i=1, n do
        a = a * i % m
    end
    return a
end

local function make_mod_nCr(nmax, m)
    -- Create n! table
    local mfac = {}
    mfac[0] = 1
    for i=1,nmax do
        mfac[i] = mfac[i-1] * i % m
    end
    -- Create (n!)^(-1) table
    -- (n!)^(-1) ≡ (n!)^(m - 2)  <mod m>
    local invmfac = {}
    invmfac[nmax] = modpow(mfac[nmax], m - 2, m)
    for i=nmax,0,-1 do
        -- ((a-1)!)^(-1) ≡ a * (a!)^(-1)  <mod m>
        invmfac[i-1] = i * invmfac[i] % m
    end
    -- returns nCr % m
    local function mod_nCr(n, r)
        if r > n then
            return 0
        end
        if r > n/2 then
            r = n - r
        end
        if not mfac[n] or not invmfac[r] or not invmfac[n-r] then
            print("n=",n,"mfac[n]",mfac[n])
            print("r=",r,"invmfac[r]",invmfac[r])
            print("n-r=",r,"invmfac[n-r]",invmfac[n-r])
            error()
        end
        -- nCr ≡ n! * (r!)^(-1) * ((n-r)!)^(-1)  <mod m>
        local a = mfac[n] * invmfac[r] % m
        a = a * invmfac[n-r] % m
        return a
    end
    return mod_nCr
end

-- larger n and small r
local function make_mod_nCr_2(rmax, m)
    -- Create n! table
    local mfac = {}
    mfac[0] = 1
    for i=1,rmax do
        mfac[i] = mfac[i-1] * i % m
    end
    -- Create (n!)^(-1) table
    -- (n!)^(-1) ≡ (n!)^(m - 2)  <mod m>
    local invmfac = {}
    invmfac[rmax] = modpow(mfac[rmax], m - 2, m)
    for i=rmax,1,-1 do
        -- ((a-1)!)^(-1) ≡ a * (a!)^(-1)  <mod m>
        invmfac[i-1] = i * invmfac[i] % m
    end
    -- returns nCr % m
    local function mod_nCr(n, r)
        if r > n then
            return 0
        end
        if r > n/2 then
            r = n - r
        end
        -- nCr ≡ (n * (n - 1) * ... * (n-r+1)) * (r!)^(-1)  <mod m>
        local a = 1
        for i=n, n-r+1, -1 do
            a = (a * i) % m
        end
        a = a * invmfac[r] % m
        return a
    end
    return mod_nCr
end

----
local MOD = math.floor(10^9)+7
local N, K = read.nn()
local L = N - K

local nCr = make_mod_nCr(N, MOD)

local function f(x, y)
    return nCr(y-1, x-1)
end

local function solve(i)
    if i == 1 then
        local blue = 1
        local red = nCr(L+1,1)
        return (blue * red) % MOD
    else
        local blue = f(i, K)
        local red = 0
        red = (red + f(i-1, N-K)) % MOD
        red = (red + (2 * f(i, N-K) % MOD)) % MOD
        red = (red + f(i+1, N-K)) % MOD
        return (blue * red) % MOD
    end
end

for i=1,K do
    print(solve(i))
end


local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

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

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

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

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

local function getComb(n, k)
  local ret = 1
  for i = 1, k do
    ret = bmul(ret, n + 1 - i)
    ret = bmul(ret, modinv(i))
  end
  return ret
end

local n, k = io.read("*n", "*n")
local red = n - k
local redres = {}
for i = 0, k do
  redres[1 + i] = getComb(red - 1, i)
end
local blue_cnt = 1
for i = 1, k do
  if 1 < i then
    blue_cnt = blue_cnt * (k - i + 1) / (i - 1)
  end
  local red_1 = redres[i]
  local red_2 = redres[i + 1]
  local red_3 = 0
  if i ~= 1 then
    red_3 = redres[i - 1]
  end
  local sum = bmul(blue_cnt, (red_1 * 2 + red_2 + red_3) % mod)
  print(sum)
end


local n, l = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = i + l - 1
end
local rm = t[1]
local sum = 0
for i = 1, n do
  sum = sum + t[i]
  if math.abs(t[i]) < math.abs(rm) then
    rm = t[i]
  end
end
print(sum - rm)

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


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

local mmi = math.min
-- local isok = true
local cur = t[1].c
for i = 2, n do
  cur = mmi(t[i].c, cur - t[i].a)
end
print(0 <= cur and "Yes" or "No")

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


local n, k = io.read("*n", "*n")
local rem = (n - 2) * (n - 1) // 2 - k
if rem < 0 then print(-1) os.exit() end
print(n - 1 + rem)
for i = 1, n - 1 do
  print(i .. " " .. n)
end
if 0 < rem then
  for i = 1, n - 2 do
    for j = i + 1, n - 1 do
      print(i .. " " .. j)
      rem = rem - 1
      if rem == 0 then os.exit() end
    end
  end
end

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


local n, k = io.read("*n", "*n")
local rem = (n - 2) * (n - 1) // 2 - k
if rem < 0 then print(-1) os.exit() end
print(n - 1 + rem)
for i = 1, n - 1 do
  print(i .. " " .. n)
end
for i = 1, n - 2 do
  for j = i + 1, n - 1 do
    print(i .. " " .. j)
    rem = rem - 1
    if rem == 0 then os.exit() end
  end
end


x,a=io.read("*n","*n")
if x>=a then
  print(10)
else
  print(0)
end

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n", "*l")
-- local left, right, up, down = {}, {}, {}, {}
local cxmin, cxmax, cymin, cymax = nil, nil, nil, nil
local xinc_max, xinc_min, xdec_max, xdec_min = nil, nil, nil, nil
local str = ""
for i = 1, n do
  str = io.read()
  local x, y, d = str:match("(-?%d+) (-?%d+) (%w)")
  x, y = tonumber(x), tonumber(y)
  -- print(x, y, d)
  if d == "R" then
    if not cymin then
      cymin, cymax = y, y
    else
      cymin, cymax = mmi(cymin, y), mma(cymax, y)
    end
    if not xinc_min then
      xinc_min, xinc_max = x, x
    else
      xinc_min, xinc_max =mmi(xinc_min, x), mma(xinc_max, x)
    end
  elseif d == "L" then
    if not cymin then
      cymin, cymax = y, y
    else
      cymin, cymax = mmi(cymin, y), mma(cymax, y)
    end
    if not xdec_min then
      xdec_min, xdec_max = x, x
    else
      xdec_min, xdec_max =mmi(xdec_min, x), mma(xdec_max, x)
    end
  elseif d == "U" then
    if not cxmin then
      cxmin, cxmax = x, x
    else
      cxmin, cxmax = mmi(cxmin, x), mma(cxmax, x)
    end
    if not yinc_min then
      yinc_min, yinc_max = y, y
    else
      yinc_min, yinc_max =mmi(yinc_min, y), mma(yinc_max, y)
    end
  else
    if not cxmin then
      cxmin, cxmax = x, x
    else
      cxmin, cxmax = mmi(cxmin, x), mma(cxmax, x)
    end
    if not ydec_min then
      ydec_min, ydec_max = y, y
    else
      ydec_min, ydec_max =mmi(ydec_min, y), mma(ydec_max, y)
    end
  end
end
-- print(xinc_min, xinc_max, xdec_min, xdec_max)
-- print(yinc_min, yinc_max, ydec_min, ydec_max)
-- print(cxmin, cxmax, cymin, cymax)

local candidate_t = {0}
if xinc_min then
  if xdec_min then
    if xinc_min < xdec_min then
      table.insert(candidate_t, (xdec_min - xinc_min) / 2)
    end
    if xinc_min < xdec_max then
      table.insert(candidate_t, (xdec_max - xinc_min) / 2)
    end
    if xinc_max < xdec_min then
      table.insert(candidate_t, (xdec_min - xinc_max) / 2)
    end
    if xinc_max < xdec_max then
      table.insert(candidate_t, (xdec_max - xinc_max) / 2)
    end
  end
  if cxmin then
    if xinc_min < cxmin then
      table.insert(candidate_t, cxmin - xinc_min)
    end
    if xinc_min < cxmax then
      table.insert(candidate_t, cxmax - xinc_min)
    end
    if xinc_max < cxmin then
      table.insert(candidate_t, cxmin - xinc_max)
    end
    if xinc_max < cxmax then
      table.insert(candidate_t, cxmax - xinc_max)
    end
  end
end
if xdec_min then
  if cxmin then
    if xdec_min > cxmin then
      table.insert(candidate_t, -cxmin + xdec_min)
    end
    if xdec_min > cxmax then
      table.insert(candidate_t, -cxmax + xdec_min)
    end
    if xdec_max > cxmin then
      table.insert(candidate_t, -cxmin + xdec_max)
    end
    if xdec_max > cxmax then
      table.insert(candidate_t, -cxmax + xdec_max)
    end
  end
end
-- y
if yinc_min then
  if ydec_min then
    if yinc_min < ydec_min then
      table.insert(candidate_t, (ydec_min - yinc_min) / 2)
    end
    if yinc_min < ydec_max then
      table.insert(candidate_t, (ydec_max - yinc_min) / 2)
    end
    if yinc_max < ydec_min then
      table.insert(candidate_t, (ydec_min - yinc_max) / 2)
    end
    if yinc_max < ydec_max then
      table.insert(candidate_t, (ydec_max - yinc_max) / 2)
    end
  end
  if cymin then
    if yinc_min < cymin then
      table.insert(candidate_t, cymin - yinc_min)
    end
    if yinc_min < cymax then
      table.insert(candidate_t, cymax - yinc_min)
    end
    if yinc_max < cymin then
      table.insert(candidate_t, cymin - yinc_max)
    end
    if yinc_max < cymax then
      table.insert(candidate_t, cymax - yinc_max)
    end
  end
end
if ydec_min then
  if cymin then
    if ydec_min > cymin then
      table.insert(candidate_t, -cymin + ydec_min)
    end
    if ydec_min > cymax then
      table.insert(candidate_t, -cymax + ydec_min)
    end
    if ydec_max > cymin then
      table.insert(candidate_t, -cymin + ydec_max)
    end
    if ydec_max > cymax then
      table.insert(candidate_t, -cymax + ydec_max)
    end
  end
end

local ret = nil
for i = 1, #candidate_t do
  local time = candidate_t[i]
  local qxmin, qxmax = nil, nil
  local qymin, qymax = nil, nil
  if xdec_min then
    if not qxmin then qxmin, qxmax = xdec_min - time, xdec_max - time
    else qxmin, qxmax = mmi(qxmin, xdec_min - time), mma(qxmax, xdec_max - time)
    end
  end
  if xinc_min then
    if not qxmin then qxmin, qxmax = xinc_min + time, xinc_max + time
    else qxmin, qxmax = mmi(qxmin, xinc_min + time), mma(qxmax, xinc_max + time)
    end
  end
  if cxmin then
    if not qxmin then qxmin, qxmax = cxmin, cxmax
    else qxmin, qxmax = mmi(qxmin, cxmin), mma(qxmax, cxmax)
    end
  end

  if ydec_min then
    if not qymin then qymin, qymax = ydec_min - time, ydec_max - time
    else qymin, qymax = mmi(qymin, ydec_min - time), mma(qymax, ydec_max - time)
    end
  end
  if yinc_min then
    if not qymin then qymin, qymax = yinc_min + time, yinc_max + time
    else qymin, qymax = mmi(qymin, yinc_min + time), mma(qymax, yinc_max + time)
    end
  end
  if cymin then
    if not qymin then qymin, qymax = cymin, cymax
    else qymin, qymax = mmi(qymin, cymin), mma(qymax, cymax)
    end
  end
  local tmp = 0
  if qxmin and qymin then
    tmp = (qxmax - qxmin) * (qymax - qymin)
  else
    tmp = 0
  end
  -- print(time, tmp)
  if not ret then ret = tmp
  else ret = mmi(ret, tmp)
  end
end
print(ret)

Score : 600 points

Problem Statement

There are N points in a two-dimensional plane. The initial coordinates of the i-th point are (x_i, y_i). Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x- or y- axis. You are given a character d_i that represents the specific direction in which the i-th point moves, as follows:

If d_i = R, the i-th point moves in the positive x direction;

If d_i = L, the i-th point moves in the negative x direction;

If d_i = U, the i-th point moves in the positive y direction;

If d_i = D, the i-th point moves in the negative y direction.

You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let x_{max} and x_{min} be the maximum and minimum among the x-coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y-coordinates of the N points, respectively.

Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it.

Constraints

1 \leq N \leq 10^5

-10^8 \leq x_i,\ y_i \leq 10^8

x_i and y_i are integers.

d_i is R, L, U, or D.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 d_1
x_2 y_2 d_2
.
.
.
x_N y_N d_N

Output

Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}).

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

Sample Input 1

2
0 3 D
3 0 L

Sample Output 1

0

After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment.

Sample Input 2

5
-7 -10 U
7 -6 U
-8 7 D
-3 3 D
0 -6 R

Sample Output 2

97.5

The answer may not be an integer.

Sample Input 3

20
6 -10 R
-4 -9 U
9 6 D
-3 -2 R
0 7 D
4 5 D
10 -10 U
-1 -8 U
10 -6 D
8 -5 U
6 4 D
0 3 D
7 9 R
9 -4 R
3 10 D
1 9 U
1 -6 U
9 -8 R
6 7 D
7 -3 D

Sample Output 3

273


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

local n = io.read("*n")
local ga, sa, ba = io.read("*n", "*n", "*n")
local gb, sb, bb = io.read("*n", "*n", "*n")

local dp = {}
for i = 1, n do
  dp[i] = i
end
if ga < gb then
  local max = mfl(n / ga)
  for i = 1, max do
    dp[i * ga] = i * gb
  end
end
if sa < sb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / sa)
    for j = 1, addable do
      dp[i + j * sa] = mma(dp[i + j * sa], dp[i] + j * sb)
    end
  end
  local max = mfl(n / sa)
  for i = 1, max do
    dp[i * sa] = mma(dp[i * sa], i * sb)
  end
end
if ba < bb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / ba)
    for j = 1, addable do
      dp[i + j * ba] = mma(dp[i + j * ba], dp[i] + j * bb)
    end
  end
  local max = mfl(n / ba)
  for i = 1, max do
    dp[i * ba] = mma(dp[i * ba], i * bb)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i] + n - i)
end

local changed = n ~= newn
n = newn

local function remchange(xb, xa, yb, ya)
  if xa * yb < ya * xb then
    xb, xa, yb, ya = yb, ya, xb, xa
  end
  local maxmul = mfl(n / xb)
  local maxrem = n % xb
  local maxrem_mul = mfl(maxrem / yb)
  local maxrem_rem = maxrem % yb
  local cand = maxrem_rem + maxrem_mul * ya + maxmul * xa
  -- if maxrem_rem == 0 then print(cand)
  -- else
    for i = maxmul, 0, -1 do
      local base = i * xa
      local rem_mul = mfl((n - i * xb) / yb)
      local rem_rem = (n - i * xb) % yb
      -- if maxrem_rem == rem_rem then break end
      cand = mma(cand, base + rem_mul * ya + rem_rem)
    end
    print(cand)
  -- end
end

-- B to A
if changed then
  if gb < ga then
    if sb < sa then
      remchange(gb, ga, sb, sa)
    elseif bb < ba then
      remchange(gb, ga, bb, ba)
    else
      local rem, mul = n % gb, mfl(n / gb)
      print(rem + mul * ga)
    end
  elseif sb < sa then
    if bb < ba then
      remchange(sb, sa, bb, ba)
    else
      local rem, mul = n % sb, mfl(n / sb)
      print(rem + mul * sa)
    end
  elseif bb < ba then
    local rem, mul = n % bb, mfl(n / bb)
    print(rem + mul * ba)
  else
    print(newn)
  end
else
  for i = 1, n do
    dp[i] = i
  end
  if gb < ga then
    local max = mfl(n / gb)
    for i = 1, max do
      dp[i * gb] = i * ga
    end
  end
  if sb < sa then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / sb)
      for j = 1, addable do
        dp[i + j * sb] = mma(dp[i + j * sb], dp[i] + j * sa)
      end
    end
    local max = mfl(n / sb)
    for i = 1, max do
      dp[i * sb] = mma(dp[i * sb], i * sa)
    end
  end
  if bb < ba then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / bb)
      for j = 1, addable do
        dp[i + j * bb] = mma(dp[i + j * bb], dp[i] + j * ba)
      end
    end
    local max = mfl(n / bb)
    for i = 1, max do
      dp[i * bb] = mma(dp[i * bb], i * ba)
    end
  end
  newn = n
  for i = 1, n do
    newn = mma(newn, dp[i] + n - i)
  end
  print(newn)
end

Score : 600 points

Problem Statement

The squirrel Chokudai has N acorns.
One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.

His plan is as follows:

Get out of the nest with N acorns in his hands.

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order:

Lose g_{X} acorns and gain 1 gram of gold.

Gain g_{X} acorns and lose 1 gram of gold.

Lose s_{X} acorns and gain 1 gram of silver.

Gain s_{X} acorns and lose 1 gram of silver.

Lose b_{X} acorns and gain 1 gram of bronze.

Gain b_{X} acorns and lose 1 gram of bronze.

Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.

What is the maximum number of acorns that he can bring to the nest?
Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

Print the maximum number of acorns that Chokudai can bring to the nest.

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

He can bring 46 acorns to the nest, as follows:

In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }

In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }

In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }

He cannot have 47 or more acorns, so the answer is 46.


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

local n = io.read("*n")
local ga, sa, ba = io.read("*n", "*n", "*n")
local gb, sb, bb = io.read("*n", "*n", "*n")

local dp = {}
for i = 1, n do
  dp[i] = i
end
if ga < gb then
  local max = mfl(n / ga)
  for i = 1, max do
    dp[i * ga] = i * gb
  end
end
if sa < sb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / sa)
    for j = 1, addable do
      dp[i + j * sa] = mma(dp[i + j * sa], dp[i] + j * sb)
    end
  end
  local max = mfl(n / sa)
  for i = 1, max do
    dp[i * sa] = mma(dp[i * sa], i * sb)
  end
end
if ba < bb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / ba)
    for j = 1, addable do
      dp[i + j * ba] = mma(dp[i + j * ba], dp[i] + j * bb)
    end
  end
  local max = mfl(n / ba)
  for i = 1, max do
    dp[i * ba] = mma(dp[i * ba], i * bb)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i] + n - i)
end

local changed = n ~= newn
n = newn

local function remchange(xb, xa, yb, ya)
  if xa * yb < ya * xb then
    xb, xa, yb, ya = yb, ya, xb, xa
  end
  local maxmul = mfl(n / xb)
  local maxrem = n % xb
  local maxrem_mul = mfl(maxrem / yb)
  local maxrem_rem = maxrem % yb
  local cand = maxrem_rem + maxrem_mul * ya + maxmul * xa
  if maxrem_rem == 0 then print(cand)
  else
    for i = maxmul, 0, -1 do
      local base = i * xa
      local rem_mul = mfl((n - base) / yb)
      local rem_rem = (n - base) % yb
      if maxrem_rem == rem_rem then break end
      cand = mma(cand, base + rem_mul * ya + rem_rem)
    end
    print(cand)
  end
end

-- B to A
if changed then
  if gb < ga then
    if sb < sa then
      remchange(gb, ga, sb, sa)
    elseif bb < ba then
      remchange(gb, ga, bb, ba)
    else
      local rem, mul = n % gb, mfl(n / gb)
      print(rem + mul * ga)
    end
  elseif sb < sa then
    if bb < ba then
      remchange(sb, sa, bb, ba)
    else
      local rem, mul = n % sb, mfl(n / sb)
      print(rem + mul * sa)
    end
  elseif bb < ba then
    local rem, mul = n % bb, mfl(n / bb)
    print(rem + mul * ba)
  else
    print(newn)
  end
else
  for i = 1, n do
    dp[i] = i
  end
  if gb < ga then
    local max = mfl(n / gb)
    for i = 1, max do
      dp[i * gb] = i * ga
    end
  end
  if sb < sa then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / sb)
      for j = 1, addable do
        dp[i + j * sb] = mma(dp[i + j * sb], dp[i] + j * sa)
      end
    end
    local max = mfl(n / sb)
    for i = 1, max do
      dp[i * sb] = mma(dp[i * sb], i * sa)
    end
  end
  if bb < ba then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / bb)
      for j = 1, addable do
        dp[i + j * bb] = mma(dp[i + j * bb], dp[i] + j * ba)
      end
    end
    local max = mfl(n / bb)
    for i = 1, max do
      dp[i * bb] = mma(dp[i * bb], i * ba)
    end
  end
  local newn = n
  for i = 1, n do
    newn = mma(newn, dp[i] + n - i)
  end
  print(newn)
end


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

local n = io.read("*n")
local ga, sa, ba = io.read("*n", "*n", "*n")
local gb, sb, bb = io.read("*n", "*n", "*n")

local dp = {}
for i = 1, n do
  dp[i] = i
end
if ga < gb then
  local max = mfl(n / ga)
  for i = 1, max do
    dp[i * ga] = i * gb
  end
end
if sa < sb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / sa)
    for j = 1, addable do
      dp[i + j * sa] = mma(dp[i + j * sa], dp[i] + j * sb)
    end
  end
  local max = mfl(n / sa)
  for i = 1, max do
    dp[i * sa] = mma(dp[i * sa], i * sb)
  end
end
if ba < bb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / ba)
    for j = 1, addable do
      dp[i + j * ba] = mma(dp[i + j * ba], dp[i] + j * bb)
    end
  end
  local max = mfl(n / ba)
  for i = 1, max do
    dp[i * ba] = mma(dp[i * ba], i * bb)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i])
end

n = newn

-- B to A
for i = 1, n do
  dp[i] = i
end
if gb < ga then
  local max = mfl(n / gb)
  for i = 1, max do
    dp[i * gb] = i * ga
  end
end
if sb < sa then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / sb)
    for j = 1, addable do
      dp[i + j * sb] = mma(dp[i + j * sb], dp[i] + j * sa)
    end
  end
  local max = mfl(n / sb)
  for i = 1, max do
    dp[i * sb] = mma(dp[i * sb], i * sa)
  end
end
if bb < ba then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / bb)
    for j = 1, addable do
      dp[i + j * bb] = mma(dp[i + j * bb], dp[i] + j * ba)
    end
  end
  local max = mfl(n / bb)
  for i = 1, max do
    dp[i * bb] = mma(dp[i * bb], i * ba)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i])
end
print(newn)


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

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

for now=0,n-1 do
    for next=now+1,math.min(n,now+2) do
        if stair[next] then
            dp[next]=dp[next]+dp[now]
            dp[next]=dp[next]%1000000007
        end
    end
end
print(dp[n])

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local mab = math.abs
local mod = 1000000007
local n, m = io.read("*n", "*n")
local curpos = 0

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function calcfibo(len)
  local a, b = 0, 1
  for i = 1, len do
    a, b = b, (a + b) % mod
  end
  return b
end

local ret = 1
for i = 1, m do
  local anapos = io.read("*n")
  if anapos == curpos then
    ret = 0
    curpos = n
  elseif curpos < anapos then
    local len = (anapos - 1) - curpos
    local fibo = calcfibo(len)
    ret = bmul(ret, fibo)
    curpos = anapos + 1
  end
end
if curpos < n then
  local len = n - curpos
  ret = bmul(ret, calcfibo(len))
end
print(ret)


local h, w = io.read("*n", "*n", "*l")
local t = {}
local s_sharp = string.byte("#")
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    local idx = (i - 1) * w + j
    t[idx] = s:byte(j) ~= s_sharp
  end
end
local score = {}
for i = 1, h * w do score[i] = 0 end
local len = 0
local function func(idx)
  score[idx] = score[idx] + len
  if t[idx] then len = len + 1
  else len = 0
  end
end
for i = 1, h do
  len = 0
  for j = 1, w do
    func((i - 1) * w + j)
  end
end
for i = 1, h do
  len = 0
  for j = w, 1, -1 do
    func((i - 1) * w + j)
  end
end
for j = 1, w do
  len = 0
  for i = 1, h do
    func((i - 1) * w + j)
  end
end
for j = 1, w do
  len = 0
  for i = h, 1, -1 do
    func((i - 1) * w + j)
  end
end
local ret = 0
local mma = math.max
for i = 1, h * w do
  if t[i] then
    ret = mma(ret, score[i])
  end
end
print(ret + 1)

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


local h,w=io.read("n","n","l")
local s={}
for i=1,h do
    local input=io.read()
    s[i]={}
    for j=1,w do
        s[i][j]=input:byte(j)
    end
end

local u,d,l,r={},{},{},{}
for j=1,w do
    for i=1,h do
        u[i]=u[i] or {}
        if s[i][j]==35 then
            u[i][j]=0
        else
            if i==1 then
                u[i][j]=1
            else
                u[i][j]=u[i-1][j]+1
            end
        end
    end
end
for j=1,w do
    for i=h,1,-1 do
        d[i]=d[i] or {}
        if s[i][j]==35 then
            d[i][j]=0
        else
            if i==h then
                d[i][j]=1
            else
                d[i][j]=d[i+1][j]+1
            end
        end
    end
end
for i=1,h do
    l[i]={}
    for j=1,w do
        if s[i][j]==35 then
            l[i][j]=0
        else
            if j==1 then
                l[i][j]=1
            else
                l[i][j]=l[i][j-1]+1
            end
        end
    end
end
for i=1,h do
    r[i]={}
    for j=w,1,-1 do
        if s[i][j]==35 then
            r[i][j]=0
        else
            if j==w then
                r[i][j]=1
            else
                r[i][j]=r[i][j+1]+1
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
    end
end
print(max)


local h,w=io.read("n","n","l")
local s={}
for i=1,h do
    s[i]={io.read():byte(1,w)}
end

local u,d,l,r={},{},{},{}
for j=1,w do
    for i=1,h do
        u[i]=u[i] or {}
        if s[i][j]==35 then
            u[i][j]=0
        else
            if i==1 then
                u[i][j]=1
            else
                u[i][j]=u[i-1][j]+1
            end
        end
    end
end
for j=1,w do
    for i=h,1,-1 do
        d[i]=d[i] or {}
        if s[i][j]==35 then
            d[i][j]=0
        else
            if i==h then
                d[i][j]=1
            else
                d[i][j]=d[i+1][j]+1
            end
        end
    end
end
for i=1,h do
    l[i]={}
    for j=1,w do
        if s[i][j]==35 then
            l[i][j]=0
        else
            if j==1 then
                l[i][j]=1
            else
                l[i][j]=l[i][j-1]+1
            end
        end
    end
end
for i=1,h do
    r[i]={}
    for j=w,1,-1 do
        if s[i][j]==35 then
            r[i][j]=0
        else
            if j==w then
                r[i][j]=1
            else
                r[i][j]=r[i][j+1]+1
            end
        end
    end
end

local max=0
for i=1,h do
    for j=1,w do
        max=math.max(max,u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
    end
end
print(max)


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

local function mk2d()
    local t = {}
    for i=1,H do
        t[i] = {}
    end
    return t
end

local grid = mk2d()
for i=1,H do
    local s = io.read("*l")
    local line = grid[i]
    for j=1,W do
        line[j] = string.sub(s, j, j) == '#' and 1 or 0
    end
end

local U = mk2d()
local D = mk2d()
local L = mk2d()
local R = mk2d()

-- L
for i=1,H do
    local line = grid[i]
    local target = L[i]
    local cnt = 0
    for j=1, W do
        local a = line[j]
        if a == 0 then
            cnt = cnt + 1
        else
            cnt = 0
        end
        target[j] = cnt
    end
end
-- R
for i=1,H do
    local line = grid[i]
    local target = R[i]
    local cnt = 0
    for j=W, 1, -1 do
        local a = line[j]
        if a == 0 then
            cnt = cnt + 1
        else
            cnt = 0
        end
        target[j] = cnt
    end
end
-- U
for j=1,W do
    local cnt = 0
    for i=1,H do
        local a = grid[i][j]
        if a == 0 then
            cnt = cnt + 1
        else
            cnt = 0
        end
        U[i][j] = cnt
    end
end
-- D
for j=1,W do
    local cnt = 0
    for i=H,1,-1 do
        local a = grid[i][j]
        if a == 0 then
            cnt = cnt + 1
        else
            cnt = 0
        end
        D[i][j] = cnt
    end
end

local function count(i, j)
    if grid[i][j] == 1 then
        return -1
    else
        local a = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
        return a
    end
end

local ans = 0
for i=1,H do
    for j=1,W do
        ans = math.max(ans, count(i, j))
    end
end
print(ans)


local n, a, b, mod = io.read("*n", "*n", "*n", "*n")

local function badd(x, y) return (x + y) % mod end
local function bsub(x, y) return (mod + x - y) % mod end
local function bmul(x, y) return (x * y) % mod end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res, pow = bmul(res, src), pow - 1
    end
    src, pow = bmul(src, src), pow // 2
  end
  return res
end

local function func(i)
  local c, z = 0, a + b * (i - 1)
  while 0 < z do
    c, z = c + 1, z // 10
  end
  return c
end

local function getright(d, left)
  local min = left
  local max = n
  if d < func(min) then return left - 1 end
  if func(max) == d then return max end
  while 1 < max - min do
    local mid = (min + max) // 2
    if d < func(mid) then max = mid
    else min = mid
    end
  end
  return min
end

local function solve1(d, n)
  if n == 1 then return 1 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + modpow(tend, n - 1)
    n = n - 1
  end
  n = n // 2
  local v = solve1(d, n)
  v = bmul(v, badd(1, modpow(tend, n)))
  return badd(add, v)
end

local function solve2(d, n)
  if n == 1 then return 0 end
  local add = 0
  local tend = modpow(10, d)
  if n % 2 == 1 then
    add = add + bmul(n - 1, modpow(tend, n - 1))
    n = n - 1
  end
  n = n // 2
  local v1 = solve2(d, n)
  v1 = bmul(v1, badd(1, modpow(tend, n)))
  local v2 = solve1(d, n)
  v2 = bmul(v2, bmul(n % mod, modpow(tend, n)))
  return badd(add, badd(v1, v2))
end

local cur_digsum = 0
local left = 1
local ret = 0
for d = 1, 18 do
  local right = getright(d, left)
  if left <= right then
    local s1 = solve1(d, right - left + 1)
    local v1 = bmul(a % mod, s1)
    local v2 = bmul(b % mod, (right - 1) % mod)
    v2 = bmul(v2, s1)
    local v3 = bmul(b % mod, solve2(d, right - left + 1))
    local tend = modpow(10, d)
    ret = bmul(ret, modpow(tend, right - left + 1))
    ret = badd(ret, badd(v1, bsub(v2, v3)))
    cur_digsum = cur_digsum + (right - left + 1) * d
    left = right + 1
  end
  if n < left then break end
end
print(ret)

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908


local n = io.read("*n")
local a = io.read("*n")
local b = io.read("*n")
local c = io.read("*n")
local d = io.read("*n")
io.read("*l")
local s = io.read("*l")
local empty = string.byte(".", 1)
local rock = string.byte("#", 1)
if a > b then
	a, b = b, a
	c, d = d, c
end
local threeempty = false
for i = 1, math.max(c, d) do
	if a < i and i < c and string.byte(s, i) == rock and string.byte(s, i + 1) == rock then
		print("No")
		return
	end
	if b < i and i < d and string.byte(s, i) == rock and string.byte(s, i + 1) == rock then
		print("No")
	end
	if b <= i and i <= d and i + 2 < n and string.byte(s, i) == empty and string.byte(s, i + 1) == empty and string.byte(s, i + 2) == empty then
		threeempty = true
	end
end

if d < c then
	if not threeempty then
		print('No')
		return
	end
end
print("Yes")

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


local function str2tbl(s)
    -- limit of #s is approximately 999959
    local t = {string.byte(s, 1, #s)}
    local char = string.char
    for i=1, #t do
        if char(t[i]) == '#' then
            t[i] = 1
        else
            t[i] = 0
        end
    end
    return t
end
--
local N, A, B, C, D = io.read("n", "n", "n", "n", "n", "l")
local S = str2tbl(io.read("l"))

local function YesNo(x)
    print(x and 'Yes' or 'No')
end

local function ok1(src, dst)
    local prev = S[src]
    for i=src+1, dst do
        local cur = S[i]
        if prev == 1 and cur == 1 then
            return false
        end
        prev = cur
    end
    return true
end

local function ok2(src, dst)
    for i=src, dst do
        local prev = S[i-1]
        local cur = S[i]
        local nex = S[i+1]
        if prev == 0 and cur == 0 and nex == 0 then
            return true
        end
    end
    return false
end

local fnuke_ok = ok1(B, D)
local snuke_ok = ok1(A, C)
if C < D then
    YesNo(fnuke_ok and snuke_ok)
else
    YesNo(ok2(B, D))
end


local n, a, b, c, d = io.read("*n", "*n", "*n", "*n", "*n", "*l")
local s = io.read()

local function check1(l, r)
  for i = l, r - 1 do
    if s:sub(i, i) == "#" and s:sub(i + 1, i + 1) == "#" then
      return false
    end
  end
  return true
end

local function check2(l, r)
  for i = l, r - 2 do
    if s:sub(i, i) == "." and s:sub(i + 1, i + 1) == "." and s:sub(i + 2, i + 2) == "." then
      return true
    end
  end
  return false
end

if c < d then
  local r = check1(a, c) and check1(b, d)
  print(r and "Yes" or "No")
else
  local r = check1(a, c)
  if not r then print("No")
  else
    r = check2(b - 1, d)
    print(r and "Yes" or "No")
  end
end


Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


local A, P = io.read("n", "n")
local piece = A * 3 + P
local pie = piece // 2
print(pie)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
local str2tbl = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
--
local function tuplecmp(x, y)
    local i = 1
    while true do
        if x[i] == nil and y[i] ~= nil then return true end
        if y[i] == nil and x[i] ~= nil then return false end
        if x[i] == nil and y[i] == nil then return false end -- Don't care => false
        if x[i] ~= y[i] then return x[i] < y[i] end
        i = i + 1
    end
end
local N = read.nl()
local R = {}
for i=1,N do
    local s, p = read.l():split()
    table.insert(R, {s, -1 * tonumber(p), i})
end
table.sort(R, tuplecmp)
for _,v in ipairs(R) do
    local c,pp,i = table.unpack(v)
    print(i)
end

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local ior = io.input()
local n = ior:read("*n", "*n", "*l")
local mise = {}
for i = 1, n do
  local str = ior:read()
  local s, p = str:match("(%w+) (%d+)")
  p = tonumber(p)
  local tmp = {s, p, i}
  table.insert(mise, tmp)
end
for i = 1, n do
  -- print(unpack(mise[i]))
end
-- os.exit()
table.sort(mise, function(x, y)
  -- print("-")
  -- print(unpack(x))
  -- print(unpack(y))
  if(x[1] ~= y[1]) then
    return x[1] < y[1]
  else
    return x[2] > y[2]
  end
  -- if x[1] < y[1] then
  --   return true
  -- elseif y[1] < x[1] then
  --   return true
  -- else
  --   return x[2] < y[2]
  -- end
end)

for i = 1, n do
  print(mise[i][3])
end


local DBG = false
local function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

local function dbgpr_t(tbl, use_pairs)
    if DBG then
        local enum = ipairs
        if use_pairs then
            enum = pairs
        end
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in enum(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

--

local N, M = io.read("n", "n")
local L = {}
for i=1, M do
    local k = io.read("n")
    local li = {}
    for j=1,N do
        li[j] = false
    end
    for j=1,k do
        local s = io.read("n")
        li[s] = true
    end
    table.insert(L, li)
    dbgpr("light", i)
    dbgpr_t(li)
end
local p = {}
for i=1, M do
    p[i] = io.read("n")
end
dbgpr("p")
dbgpr_t(p)
local ans = 0
for i=0,2^N-1 do
    local sw_states = {}
    for j=1,N do
        local state = (i >> (j - 1)) & 1
        sw_states[j] = state
    end
    local on = true
    for j=1,M do
        local sw_count = 0
        for k=1,N do
            if L[j][k] then
                sw_count = sw_count + sw_states[k]
            end
        end
        dbgpr("L", j, "sw_count", sw_count, "p", p[j])
        if sw_count % 2 ~= p[j] then
            on = false
        end
    end
    dbgpr_t(sw_states)
    dbgpr(on)
    if on then
        ans = ans + 1
    end
end
print(ans)

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local n = io.read("*n")
local t = {}
_u = io.read("*n")
for i = 1, n - 1 do
  t[i] = io.read("*n")
end
n = n - 1
local ret = 0
for i = 1, n do
  local c = 0
  for j = 1, n do
    if n % i == 0 then
      if n - i * j <= i * j then break end
    end
    if n - i * j < i then break end
    c = c + t[i * j] + t[n - i * j]
    ret = mma(ret, c)
  end
end
print(ret)

Score : 600 points

Problem Statement

There is an infinitely large pond, which we consider as a number line.
In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1.
On the lotus at coordinate i, an integer s_i is written.

You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:

1. Choose positive integers A and B. Your score is initially 0.

2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.

If y = N-1, the game ends.

If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.

If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.

4. Go back to step 2.

You want to end the game with as high a score as possible.
What is the score obtained by the optimal choice of A and B?

Constraints

3 \leq N \leq 10^5

-10^9 \leq s_i \leq 10^9

s_0=s_{N-1}=0

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_0 s_1 ...... s_{N-1}

Output

Print the score obtained by the optimal choice of A and B.

Sample Input 1

5
0 2 5 1 0

Sample Output 1

3

If you choose A = 3 and B = 2, the game proceeds as follows:

Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.

Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.

Move to coordinate 1 + 3 = 4. The game ends with a score of 3.

There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.

Sample Input 2

6
0 10 -7 -4 -13 0

Sample Output 2

0

The optimal strategy here is to land the final lotus immediately by choosing A = 5 (the value of B does not matter).

Sample Input 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

Sample Output 3

59


local mfl, mce = math.floor, math.ceil
local bls, brs = bit.lshift, bit.rshift
local mmi, mma = math.min, math.max
local n = io.read("*n")
local t = {}
_u = io.read("*n")
for i = 1, n - 1 do
  t[i] = io.read("*n")
end
n = n - 1
local ret = 0
local lim = brs(n - 1, 1)
for i = 1, lim do
  local lim2 = mfl(lim / i)
  local c = 0
  for j = 1, lim2 do
    c = c + t[i * j] + t[n - i * j]
    ret = mma(ret, c)
  end
end
print(ret)


local a,b=io.read("*n","*n")
if a<13 and a>5then
  print(b//2)
elseif a<6then
  print(0)
else
  print(b)
end

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


local r, D, x = io.read("n", "n", "n")
local y = 2000
for y = 2001, 2010 do
    x = r * x - D
    print(x)
end

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


N, M = io.read("*n", "*n", "*l")
L_max = 0
R_min = 10^5
for i = 1, M do
    L, R = io.read("*n", "*n", "*l")
    L_max = math.max(L, L_max)
    R_min = math.min(R, R_min)
end
print(R_min-L_max+1)

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


local n, m  = io.read("*n", "*n")
local l, r = 1, n
local mmi, mma = math.min, math.max
for i = 1, m do
  local a, b = io.read("*n", "*n")
  l = mma(l, a)
  r = mmi(r, b)
end
print(mma(0, r - l + 1))