a,b,c,k=io.read("*n","*n","*n","*n")
if a>=k then
    print(k)
elseif a<k then
    if a+b>=k then
        print(a)
    elseif a+b<k then
        if a+b+c==k then
            print(a-c)
        elseif a+b+c>k then
            print(a-(k-a-b))
        end
    end
end

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local n, m, x = io.read("*n", "*n", "*n")
local c = {}
local a = {}
for i = 1, n do
  c[i] = io.read("*n")
  a[i] = {}
  for j = 1, m do
    a[i][j] = io.read("*n")
  end
end
local ret = -1
local tot = bls(1, n)
for it = 0, tot - 1 do
  local ti = it
  local cost = 0
  local z = {}
  for j = 1, m do
    z[j] = 0
  end
  for j = 1, n do
    if ti % 2 == 1 then
      for k = 1, m do
        z[k] = z[k] + a[j][k]
      end
      cost = cost + c[j]
    end
    ti = brs(ti, 1)
  end
  local valid = true
  for j = 1, m do
    if z[j] < x then
      valid = false
    end
  end
  if valid then
    if ret == -1 then
      ret = cost
    else
      ret = mmi(ret, cost)
    end
  end
end
print(ret)

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

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067

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 mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + 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 fact = {1}
local invs = {1}
local invfact = {1}
for i = 2, 201000 do
  fact[i] = bmul(fact[i - 1], i)
  invs[i] = bmul(mfl(mod / i), mod - invs[mod % i])
  invfact[i] = bmul(invfact[i - 1], invs[i])
end

local function getComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[n], bmul(invfact[k], invfact[n - k]))
end

local ret = 0
local n, m, k = io.read("*n", "*n", "*n")
if m == 1 then
  if k == n - 1 then
    print(1)
  else
    print(0)
  end
  os.exit()
end
for i = 0, k do
  local sep_num = n - 1 - i
  local rem = i--n - sep_num - 1
  local comb = getComb(sep_num + rem, rem)
  local mul = 1
  mul = bmul(m, modpow(m - 1, n - 1 - i))
  ret = badd(ret, bmul(mul, comb))
end
print(ret)

Score : 500 points

Problem Statement

There are N blocks arranged in a row. Let us paint these blocks.

We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.

Find the number of ways to paint the blocks under the following conditions:

For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.

There may be at most K pairs of adjacent blocks that are painted in the same color.

Since the count may be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

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

0 \leq K \leq N - 1

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.

Sample Input 1

3 2 1

Sample Output 1

6

The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks.

Sample Input 2

100 100 0

Sample Output 2

73074801

Sample Input 3

60522 114575 7559

Sample Output 3

479519525


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

local ls=string.byte"("
local rs=string.byte")"

local lr={}

for i,s in ipairs(S)do
	local n
	repeat
		s,n=s:gsub("%(%)","")
	until(n==0)
	local l,r=s:match("%)*()%(*()")
	r=r-l
	l=l-1
	--[[
	local b={s:byte(1,#s)}
	local l,r=0,0
	for j,v in ipairs(b) do
		if(v==ls)then
			r=r+1
		else
			if(r>0)then r=r-1
			else l=l+1 end
		end
	end
	]]
	if(l~=0 or r~=0)then table.insert(lr,{l,r})end
end

if(#lr==0)then print"Yes"return end

do
local s=0
for _,v in ipairs(lr)do
	s=s+v[1]-v[2]
end
if(s~=0)then print"No"return end
end

local bl,br=-1,-1
for i,v in ipairs(lr)do
	if(v[1]==0)then bl=i end
	if(v[2]==0)then br=i end
end
if(bl==-1 or br==-1)then print"No"return end

local s=lr[bl][2]
table.remove(lr,math.max(bl,br))
table.remove(lr,math.min(bl,br))
table.sort(lr,function(a,b)return a[1]-a[2]<b[1]-b[2]end)

for i,v in ipairs(lr)do
	s=s-v[2]
	if(s<0)then print"No"return end
	s=s+v[1]
end

print"Yes"

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


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

local ls=string.byte"("
local rs=string.byte")"

local lr={}

for i,s in ipairs(S)do
	local n
	repeat
		s,n=s:gsub("%(%)","")
	until(n==0)
	local l,r=s:match("%)*()%(*()")
	r=r-l
	l=l-1
	if(l~=0 or r~=0)then table.insert(lr,{l,r})end
end

if(#lr==0)then print"Yes"return end

do
local s=0
for _,v in ipairs(lr)do
	s=s+v[1]-v[2]
end
if(s~=0)then print"No"return end
end

local bl,br=-1,-1
for i,v in ipairs(lr)do
	if(v[1]==0)then bl=i end
	if(v[2]==0)then br=i end
end
if(bl==-1 or br==-1)then print"No"return end

table.sort(lr,function(a,b)return a[1]-a[2]<b[1]-b[2]end)
local s=0
for i,v in ipairs(lr)do
	s=s-v[1]
	if(s<0)then print"No"return end
	s=s+v[2]
end

print"Yes"


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local up, down = {}, {}
for i = 1, n do
  local s = io.read()
  local cur = 0
  local min = 0
  for j = 1, #s do
    if s:sub(j, j) == "(" then
      cur = cur + 1
    else
      cur = cur - 1
    end
    min = mmi(min, cur)
  end
  local dec = -min
  if 0 <= cur then
    table.insert(up, {dec, cur})
  else
    table.insert(down, {dec, cur})
  end
end

local function sort1(a, b)
  return a[1] < b[1]
end
local function sort2(a, b)
  return a[1] > b[1]
end
table.sort(up, sort1)
table.sort(down, sort2)
local cur = 0
local valid = true
for i = 1, n do
  local dec, tot = 0, 0
  if i <= #up then
    dec, tot = up[i][1], up[i][2]
  else
    local p = i - #up
    dec, tot = down[p][1], down[p][2]
  end
  if cur - dec < 0 then
    valid = false
    break
  end
  cur = cur + tot
  if cur < 0 then valid = false break end
end
if valid then
  valid = cur == 0
end
-- valid = valid and cur == 0
print(valid and "Yes" or "No")


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local s = {}
local t = {}
for i = 1, n do
  s[i] = io.read()
  local cur = 0
  local min = 0
  for j = 1, #s[i] do
    if s[i]:sub(j, j) == "(" then
      cur = cur + 1
    else
      cur = cur - 1
    end
    min = mmi(min, cur)
  end
  t[i] = {min, cur}
end

local function sortfunc(a, b)
  if 0 < a[2] and 0 < b[2] then
    return a[1] > b[1]
  elseif a[2] <= 0 and b[2] <= 0 then
    return a[1] < b[1]
  else
    return a[2] > b[2]
  end
end
table.sort(t, sortfunc)
-- for i = 1, n do
--   print(i, t[i][1], t[i][2])
-- end
local cur = 0
local valid = true
for i = 1, n do
  local min, last = t[i][1], t[i][2]
  if cur + min < 0 then
    valid = false
    break
  end
  cur = cur + last
end
valid = valid and cur == 0
print(valid and "Yes" or "No")


local mmi, mma = math.min, math.max
local n = io.read("*n", "*l")
local s = {}
local t = {}
local up, down = {}, {}
for i = 1, n do
  s[i] = io.read()
  local cur = 0
  local min = 0
  for j = 1, #s[i] do
    if s[i]:sub(j, j) == "(" then
      cur = cur + 1
    else
      cur = cur - 1
    end
    min = mmi(min, cur)
  end
  t[i] = {min, cur}
  -- if 0 <= cur then
  --   table.insert(up, {min, cur})
  -- else
  --   table.insert(down, {min, cur})
  -- end
end

local function sortfunc(a, b)
  if 0 <= a[2] and 0 <= b[2] then
    return a[1] > b[1]
  elseif a[2] < 0 and b[2] < 0 then
    return a[1] < b[1]
  else
    return a[2] > b[2]
  end
end
table.sort(t, sortfunc)
local cur = 0
local valid = true
for i = 1, n do
  local min, last = t[i][1], t[i][2]
  if cur + min < 0 then
    valid = false
    break
  end
  cur = cur + last
end
valid = valid and cur == 0
print(valid and "Yes" or "No")


S = io.read()

if S == "ABC" then
	print("ARC")
else
	print("ABC")
end

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


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

for i = 1, K do
	local d = io.read("*n")
	for j = 1, d do
		tricked[io.read("*n")] = false
	end
end

local count = 0
for i = 1, N do
	if tricked[i] then
		count = count + 1
	end
end

print(count)

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


N=io.read"*n"
K=io.read"*n"

a={}
for i=1,K do
	d=io.read"*n"
	for i=1,d do
		a[io.read"*n"]=0
	end
end
s=0
for i=1,N do
	if not(a[i])then s=s+1 end
end
print(s)


n,m=io.read("*n","*n","*l")
h={}
t={}
for i=1,n do
    h[i]=io.read("*n")
    t[i]=0
end
for i=1,m do
    a,b=io.read("*n","*n")
    if h[a]>h[b] then
        t[a]=1
        t[b]=0
    else
        t[a]=0
        t[b]=1
    end
end

counter=0
for i=1,n do
    counter=counter+t[i]
end
print(counter)

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


local N, M = io.read("*n", "*n")
local hights, is_good = {}, {}
for i = 1, N do
	hights[i] = io.read("*n")
	is_good[i] = true
end

for i = 1, M do
	local A, B = io.read("*n", "*n")

	if is_good[A] and hights[A] <= hights[B] then
		is_good[A] = false
	end
	if is_good[B] and hights[B] <= hights[A] then
		is_good[B] = false
	end
end

local count = 0
for i = 1, N do
	if is_good[i] then
		count = count + 1
	end
	print(is_good[i])
end

print(count)


X=io.read"*n"
for i=-6208,6208 do
for j=-6208,6208 do
	if(i*i*i*i*i-j*j*j*j*j==X)then print(i,j)return end
end
end

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


n=io.read("*n","*l")
m={}
combination=0
for i=1,n do
    a=io.read("*n")
    if not m[i+a] then
        m[i+a]=0
    end
    if m[i-a] then
        combination=combination+m[i-a]
    end
    m[i+a]=m[i+a]+1
end
print(combination)

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

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

Sample Output 3

22


n=io.read("*n","*l")
m={}
combination=0
for i=1,n do
    a=io.read("*n")
    combination=combination+m[i-a]
    m[i+a]=m[i+a]+1
end
print(combination)


k = io.read("*n")
a, b = io.read("*n", "*n")
for i = a, b do
  if i % k == 0 then print("OK") os.exit() end
end
print("NG")

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


local k, a, b = io.read("*n", "*n", "*n")
if a % k == 0 then
  print("OK")
else
  if b // k == a // k then
    print("NG")
  else
    print("OK")
  end
end


local x=io.read("n")
local yokin=100
local counter=0
while yokin<x do
    yokin=yokin+yokin//100
    counter=counter+1
end
print(counter)

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


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

local t={}
for i=1,10 do
    t[i]={}
end
for i=1,m do
    table.insert(t[1],{i})
end

for i=1,n-1 do
    for j,_ in pairs(t[i]) do
        local x=t[i][j]
        local p=x[#x]
        for k=p,m do
            local U={table.unpack(x)}
            table.insert(U,k)
            table.insert(t[i+1],U)
        end
    end
end

local answer=0
for _,T in pairs(t[n]) do
    local total=0
    for i=1,q do
        if T[b[i]]-T[a[i]]==c[i] then
            total=total+d[i]
        end
    end
    answer=math.max(answer,total)
end
print(answer)

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


local A, B, N = io.read("*n", "*n", "*n")
local flo = math.floor

local max = 0
for x = 1, N do
	local result = flo(A*x/B) - A * flo(x/B)
	if result > max then
		max = result
	end
end

print(max)

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


local min=math.min
N=io.read"*n"
M=io.read"*n"

if(N%2==1)then
	for i=1,M do
		print(i,N-i)
	end
else
	if(N%4==2)then
		a=(N-2)/4
		for i=1,min(a,M) do
			print(i,a+a+2-i)
		end
		if(M<=a)then return end
		M=M-a
		for i=1,min(a,M) do
			print(a+a+1+i,a*4+2-i)
		end
	else
		a=N/4
		for i=1,min(a-1,M) do
			print(i,a+a-i)
		end
		if(M<=a-1)then return end
		M=M-a+1
		for i=1,min(a,M) do
			print(a+a-1+i,a*4-i)
		end
	end
end

Score : 500 points

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)
N players will participate in this competition, and they are given distinct integers from 1 through N.
The arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).
You cannot assign the same integer to multiple playing fields.
The competition consists of N rounds, each of which proceeds as follows:

For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.

Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.

You want to ensure that no player fights the same opponent more than once during the N rounds.
Print an assignment of integers to the playing fields satisfying this condition.
It can be proved that such an assignment always exists under the constraints given.

Constraints

1 \leq M

M \times 2 +1 \leq N \leq 200000

Input

Input is given from Standard Input in the following format:

N M

Output

Print M lines in the format below.
The i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


local n, m = io.read("*n", "*n")
do
  local oddm = m
  if oddm % 2 == 0 then oddm = oddm - 1 end
  local spos = 1
  for i = oddm, 1, -2 do
    print(spos .. " " .. spos + i)
    spos = spos + 1
  end
end
if 1 < m then
  local evm = m
  if evm % 2 == 1 then evm = evm - 1 end
  local epos = n
  for i = evm, 2, -2 do
    print(epos - i .. " " .. epos)
    epos = epos - 1
  end
end


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

print((S > W) and "safe" or "unsafe")

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


s, w = io.read("*n", "*n")
print(s > w and "safe" or "unsafe")


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

local counter=0
for k,v in pairs(smap) do
    counter=counter+1
end
print(counter)

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


N=io.read("*n","*l")
ans=1
L={}
for arg in io.read("*a"):gmatch("[^%s]+") do table.insert(L, arg) end

table.sort(L)

for i=1,N-1,1 do if L[i]~=L[i+1] then ans=ans+1 end end

print(ans)


local function readas(N)local l=io.read()local r={}for s in l:gmatch"(%a+)"do table.insert(r,s)end return r end
N=io.read"*n"
io.read()
t={}
for i=1,N do
	t[io.read()]=0
end
s=0
for _,_ in pairs(t)do s=s+1 end
print(s)


local function lltonumber(str)
  local ret = 0ULL
  for i = 1, #str do
    ret = ret * 10ULL + (1ULL * str:sub(i, i):byte() - 48ULL)
  end
  return ret
end

local n = io.read("*n")
local s = {}
for i = 1, n do
  s[i] = io.read("*n") == 1
end
local t = {}
for i = 1, n do
  t[i] = io.read("*n") == 1
end
io.read()
local u = {}
do
  local str = io.read()
  local i = 1
  for w in str:gmatch("%d+") do
    u[i] = lltonumber(w)
    i = i + 1
  end
end
local v = {}
do
  local str = io.read()
  local i = 1
  for w in str:gmatch("%d+") do
    v[i] = lltonumber(w)
    i = i + 1
  end
end
local ret = {}
for i = 1, n do
  ret[i] = {}
  for j = 1, n do
    ret[i][j] = 0ULL
  end
end
local function solveStage(mul)
  local z = {}
  for i = 1, n do
    z[i] = {}
    -- -1: unknown
    for j = 1, n do
      z[i][j] = -1
    end
  end
  local need_0_row = {}
  local need_1_row = {}
  local need_0_col = {}
  local need_1_col = {}
  for i = 1, n do
    -- ROW
    local flag = u[i] % 2ULL == 1ULL
    if s[i] then
      -- OR
      if not flag then
        for j = 1, n do
          if z[i][j] == 1 then
            return false
          end
          z[i][j] = 0
        end
      else
        table.insert(need_1_row, i)
      end
    else
      -- AND
      if flag then
        for j = 1, n do
          if z[i][j] == 0 then
            return false
          end
          z[i][j] = 1
        end
      else
        table.insert(need_0_row, i)
      end
    end
    -- COLUMN
    local flag = v[i] % 2ULL == 1ULL
    if t[i] then
      -- OR
      if not flag then
        for j = 1, n do
          if z[j][i] == 1 then
            return false
          end
          z[j][i] = 0
        end
      else
        table.insert(need_1_col, i)
      end
    else
      -- AND
      if flag then
        for j = 1, n do
          if z[j][i] == 0 then
            return false
          end
          z[j][i] = 1
        end
      else
        table.insert(need_0_col, i)
      end
    end
  end
  -- refresh
  local any = false
  local function flushcol(row)
    for k = #need_0_col, 1, -1 do
      local col = need_0_col[k]
      z[row][col] = 0
      table.remove(need_0_col)
    end
    for k = #need_1_col, 1, -1 do
      local col = need_1_col[k]
      z[row][col] = 1
      table.remove(need_1_col)
    end
    for j = 1, n do
      if z[row][j] == -1 then
        z[row][j] = 0
      end
    end
    any = true
  end
  local function flushrow(col)
    for k = #need_0_row, 1, -1 do
      local row = need_0_row[k]
      z[row][col] = 0
      table.remove(need_0_row)
    end
    for k = #need_1_row, 1, -1 do
      local row = need_1_row[k]
      z[row][col] = 1
      table.remove(need_1_row)
    end
    for j = 1, n do
      if z[j][col] == -1 then
        z[j][col] = 0
      end
    end
    any = true
  end
  for i = #need_0_row, 1, -1 do
    local row = need_0_row[i]
    local found = false
    for j = 1, n do
      if z[row][j] == 0 then
        table.remove(need_0_row, i)
        found = true
        break
      end
    end
    if found then
      flushcol(row)
    end
  end
  for i = #need_1_row, 1, -1 do
    local row = need_1_row[i]
    local found = false
    for j = 1, n do
      if z[row][j] == 1 then
        table.remove(need_1_row, i)
        found = true
        break
      end
    end
    if found then
      flushcol(row)
    end
  end
  for i = #need_0_col, 1, -1 do
    local col = need_0_col[i]
    local found = false
    for j = 1, n do
      if z[j][col] == 0 then
        table.remove(need_0_col, i)
        found = true
        break
      end
    end
    if found then
      flushrow(col)
    end
  end
  for i = #need_1_col, 1, -1 do
    local col = need_1_col[i]
    local found = false
    for j = 1, n do
      if z[j][col] == 1 then
        table.remove(need_1_col, i)
        found = true
        break
      end
    end
    if found then
      flushrow(col)
    end
  end
  local remrow = #need_0_row + #need_1_row
  local remcol = #need_0_col + #need_1_col
  if any then
    for i = 1, #need_0_row do
      local row = need_0_row[i]
      for j = 1, n do
        if z[row][j] == -1 then z[row][j] = 0 end
      end
    end
    for i = 1, #need_1_row do
      local row = need_1_row[i]
      for j = 1, n do
        if z[row][j] == -1 then z[row][j] = 1 end
      end
    end
    for i = 1, #need_0_col do
      local col = need_0_col[i]
      for j = 1, n do
        if z[j][col] == -1 then z[j][col] = 0 end
      end
    end
    for i = 1, #need_1_col do
      local col = need_1_col[i]
      for j = 1, n do
        if z[j][col] == -1 then z[j][col] = 1 end
      end
    end
  elseif remrow == 0 and 0 < remcol then
    return false
  elseif remcol == 0 and 0 < remrow then
    return false
  elseif 0 < remrow and 0 < remcol then
    if #need_0_row == 0 and #need_1_row == 1 then
      if #need_1_col == 0 then
        return false
      end
      local row = need_1_row[1]
      for i = 1, #need_1_col do
        z[row][need_1_col[i]] = 1
      end
      for i = 1, #need_0_col do
        z[row][need_0_col[i]] = 0
      end
    elseif #need_0_row == 1 and #need_1_row == 0 then
      if #need_0_col == 0 then
        return false
      end
      local row = need_0_row[1]
      for i = 1, #need_1_col do
        z[row][need_1_col[i]] = 1
      end
      for i = 1, #need_0_col do
        z[row][need_0_col[i]] = 0
      end
    elseif #need_0_col == 0 and #need_1_col == 1 then
      if #need_1_row == 0 then
        return false
      end
      local col = need_1_col[1]
      for i = 1, #need_1_row do
        z[need_1_row[i]][col] = 1
      end
      for i = 1, #need_0_row do
        z[need_0_row[i]][col] = 0
      end
    elseif #need_0_col == 1 and #need_1_col == 0 then
      if #need_0_row == 0 then
        return false
      end
      local col = need_0_col[1]
      for i = 1, #need_1_row do
        z[need_1_row[i]][col] = 1
      end
      for i = 1, #need_0_row do
        z[need_0_row[i]][col] = 0
      end
    else
      local rows = {}
      local cols = {}
      for i = 1, #need_0_row do
        table.insert(rows, need_0_row[i])
      end
      for i = 1, #need_1_row do
        table.insert(rows, need_1_row[i])
      end
      for i = 1, #need_0_col do
        table.insert(cols, need_0_col[i])
      end
      for i = 1, #need_1_col do
        table.insert(cols, need_1_col[i])
      end
      for i = 1, #rows do
        for j = 1, #cols do
          local r, c = rows[i], cols[j]
          if i * j == 1 then
            z[r][c] = 1
          elseif i == 1 or j == 1 then
            z[r][c] = 0
          else
            z[r][c] = 1
          end
        end
      end
    end
  end
  for i = 1, n do
    for j = 1, n do
      assert(z[i][j] ~= -1)
      if z[i][j] == 1 then
        ret[i][j] = ret[i][j] + mul
      end
    end
  end
  return true
end
local function reduce()
  for i = 1, n do
    u[i] = u[i] / 2ULL
    v[i] = v[i] / 2ULL
  end
end
local add = 1ULL
for i = 0, 63 do
  local ret = solveStage(add)
  if not ret then
    print(-1)
    os.exit()
  end
  reduce()
  add = add * 2LL
end
for i = 1, n do
  for j = 1, n do
    local str = tostring(ret[i][j]):gsub("ULL", "")
    io.write(str)
    io.write(j == n and "\n" or " ")
  end
end

Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j} \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

All values in input are integers.

1 \leq N \leq 500

0 \leq S_{i} \leq 1

0 \leq T_{i} \leq 1

0 \leq U_{i} \lt 2^{64}

0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ... S_{N}
T_{1} T_{2} ... T_{N}
U_{1} U_{2} ... U_{N}
V_{1} V_{2} ... V_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

In Sample Input 1, we need to find a matrix such that:

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11


local n, m = io.read("*n", "*n")
for i = 1, m do
  n = n - io.read("*n")
end
print(n < 0 and -1 or n)

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


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

for i=1,n do
    if not t[i] then
        print(0)
    else
        print(t[i])
    end
end

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


n,k=io.read("*n","*n")
combination=0
for i=k,n+1 do
    min=i*(i-1)//2
    max=(2*n-i+1)*i//2
    combination=(combination+(max-min+1))%1000000007
end
print(combination)


local mmi, mma = math.min, math.max
local mab = math.abs
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {io.read("*n")}
  t[i][2] = i
end
table.sort(t, function(a, b) return a[1] > b[1] end)
local dp = {}
for i = 1, n do
  local val, idx = t[i][1], t[i][2]
  dp[i] = {}
  for j = 1, i + 1 do
    -- j: left use count - 1
    dp[i][j] = 0
  end
  if i == 1 then
    dp[i][1] = val * (n - idx)
    dp[i][2] = val * (idx - 1)
  else
    for j = 1, i do
      -- dst: j
      dp[i][j + 1] = mma(dp[i][j + 1], dp[i - 1][j] + val * mab(idx - j))
      -- dst: n - (i - (j - 1)) + 1
      dp[i][j] = mma(dp[i][j], dp[i - 1][j] + val * mab(n - i + j - idx))
    end
  end
  -- print(table.concat(dp[i], " "))
end
local ret = 0
for i = 1, n + 1 do
  ret = mma(ret, dp[n][i])
end
print(ret)

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


local n = io.read("*n")
local ret = 0
for i = 1, n do
  if i % 3 ~= 0 and i % 5 ~= 0 then
    ret = ret + i
  end
end
print(ret)

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs
local mod = 1000000007
local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 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 n, k = io.read("*n", "*n")
local ret = 0

local primecnt = {}
local t = {}
for i = 1, k do t[i] = 0 end
for i = k, 1, -1 do
  local c = mfl(k / i)
  local z = modpow(c, n)
  for j = 2, c do
    z = bsub(z, t[j * i])
  end
  t[i] = z
  ret = badd(ret, bmul(i, z))
end
print(ret)

Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

Be sure to print the sum modulo (10^9+7).


local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
if n % 2 == 0 then
  local hn = brs(n, 1)
  local p1, p2 = {t[1]}, {t[2]}
  for i = 2, hn do
    p1[i] = p1[i - 1] + t[2 * i - 1]
    p2[i] = p2[i - 1] + t[2 * i]
  end
  local ret = mma(p1[hn], p2[hn])
  for i = 1, hn - 1 do
    local cand = p1[i] + p2[hn] - p2[i]
    ret = mma(ret, cand)
  end
  if ret < 100000000000000 then
    print(ret)
  else
    print("100000000000000")
  end
else
  -- use0, nouse0, use1, nouse1, use2, nouse2
  local dp = {t[1], false, false, 0, false, false}
  for i = 2, n do
    local dst = {false, false, false, false, false, false}
    if dp[1] then
      dst[2] = dp[1]
    end
    if dp[2] then
      dst[1] = dp[2] + t[i]
      dst[4] = dp[2]
    end
    if dp[3] then
      if dst[4] then
        dst[4] = mma(dst[4], dp[3])
      else
        dst[4] = dp[3]
      end
    end
    if dp[4] then
      dst[3] = dp[4] + t[i]
      dst[6] = dp[4]
    end
    if dp[5] then
      if dst[6] then
        dst[6] = mma(dst[6], dp[5])
      else
        dst[6] = dp[5]
      end
    end
    if dp[6] then
      dst[5] = dp[6] + t[i]
    end
    for j = 1, 6 do
      dp[j] = dst[j]
      -- io.write(tostring(dp[j]))
      -- io.write(j == 6 and "\n" or " ")
    end
  end
  if not dp[5] then
    print(dp[6])
  elseif not dp[6] then
    print(dp[5])
  else
    print(mma(dp[5], dp[6]))
  end
end

Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


local n, k = io.read("*n", "*n")
n = n % k
print(math.min(n, k - n))

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


local n, k, c = io.read("*n", "*n", "*n", "*l")
local s = io.read()
local t = {}
local okday = {}
for i = 1, n do
  t[i] = s:sub(i, i) == "o"
  if t[i] then
    table.insert(okday, i)
  end
end
local score = {}
for i = 1, #okday do
  score[i] = 1
end
local dayleft, dayright = {}, {}
for i = 1, k do
  dayleft[i], dayright[i] = 0, 0
end
local curscore, curday = 1, okday[1]
dayleft[1] = 1
for i = 2, #okday do
  if curday + c < okday[i] then
    curscore = curscore + 1
    if curscore == k + 1 then break end
    dayleft[curscore] = okday[i]
    curday = okday[i]
  end
end

curscore, curday = k, okday[#okday]
dayright[k] = curday
for i = #okday - 1, 1, -1 do
  if okday[i] + c < curday then
    curscore = curscore - 1
    if curscore == 0 then break end
    dayright[curscore] = okday[i]
    curday = okday[i]
  end
end
for i = 1, k do
  if 0 < dayleft[i] * dayright[i]
  and dayleft[i] == dayright[i] then
    print(dayleft[i])
  end
end

Score : 500 points

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

After working for a day, he will refrain from working on the subsequent C days.

If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.

Find all days on which Takahashi is bound to work.

Constraints

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

1 \leq K \leq N

0 \leq C \leq N

The length of S is N.

Each character of S is o or x.

Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.

Input

Input is given from Standard Input in the following format:

N K C
S

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

Sample Input 1

11 3 2
ooxxxoxxxoo

Sample Output 1

6

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

Thus, he is bound to work on Day 6.

Sample Input 2

5 2 3
ooxoo

Sample Output 2

1
5

There is only one possible choice for his workdays: Day 1,5.

Sample Input 3

5 1 0
ooooo

Sample Output 3

There may be no days on which he is bound to work.

Sample Input 4

16 4 3
ooxxoxoxxxoxoxxo

Sample Output 4

11
16


local n, k, c = io.read("*n", "*n", "*n", "*l")
local s = io.read()
local t = {}
local okday = {}
for i = 1, n do
  t[i] = s:sub(i, i) == "o"
  if t[i] then
    table.insert(okday, i)
  end
end
local score = {}
for i = 1, #okday do
  score[i] = 1
end
local curscore, curday = 1, okday[#okday]
for i = #okday - 1, 1, -1 do
  if okday[i] + c < curday then
    curday = okday[i]
    curscore = curscore + 1
  end
  score[i] = curscore
end
local scoreleft = {}
local scoreright = {}
if 0 < #score then
  local maxscore = score[1]
  for i = 1, maxscore do
    scoreleft[i] = 0
    scoreright[i] = 0
  end
  local prvscore = -1
  for i = 1, #score do
    local s = score[i]
    if s ~= prvscore then
      prvscore = s
      scoreleft[s] = i
    end
    scoreright[s] = i
  end
  -- print(table.concat(scoreleft, " "))
  -- print(table.concat(scoreright, " "))
end

local ret = {}
if score[1] == k then
  for iok = #okday, 1, -1 do
    local rem = k
    if iok ~= #okday then
      if k <= score[iok + 1] then break end
      rem = rem - score[iok + 1]
    end
    local s = score[iok]
    if scoreleft[s] == scoreright[s] then
      table.insert(ret, okday[iok])
    elseif s < k then
      local idx = scoreleft[s + 1]
      local prvday = okday[idx]
      local idx2 = scoreright[s]
      -- print(idx, prvday, idx2)
      if idx2 == iok and okday[idx2 - 1] <=prvday + c then
        table.insert(ret, okday[iok])
      end
    end
  end
end
table.sort(ret)
print(table.concat(ret, "\n"))


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

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


n=io.read"*n"
r=math.floor(n/500)*1000
n=n%500
r=r+math.floor(n/5)*5
print(r)


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) assert(type(default_val) ~= 'table') 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 X = read.n()
local c500 = X // 500
local r = X % 500
local c5 = r // 5
local p = c500 * 1000 + c5 * 5
print(p)


local n,x,y=io.read("*n","*n","*n")

local ma=math.abs
local t={}
for i=1,n do
    for j=i+1,n do
        local mindist=math.min(ma(j-i),ma(x-i)+1+ma(j-y),ma(y-i)+1+ma(j-x))
        t[mindist]=(t[mindist] or 0)+1
    end
end

for i=1,n-1 do
    print(t[i] or 0)
end

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


local n,x,y=io.read("n","n","n")

local ma=math.abs
local t={}
for i=1,n do
    for j=i+1,n do
        local mindist=math.min(ma(j-i),ma(x-i)+1+ma(j-y),ma(y-i)+1+ma(j-x))
        t[mindist]=(t[mindist] or 0)+1
    end
end

for i=1,n-1 do
    print(t[i] or 0)
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 n = io.read("*n")

local fact = {1}
for i = 2, n do
  fact[i] = bmul(fact[i - 1], i)
end
local function getComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[n], modinv(bmul(fact[k], fact[n - k])))
end
local function getInvComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[k], bmul(fact[n - k], modinv(fact[n])))
end

local edge = {}
local parent = {}
local childcnt = {}
local child_asked_cnt = {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  childcnt[i] = 0
  child_asked_cnt[i] = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
for itask = 1, n do
  local src = tasks[itask]
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
    end
  end
end
tasks = {}
-- find leaf
for i = 2, n do
  if #edge[i] == 1 then
    table.insert(tasks, i)
  end
end
for itask = 1, n - 1 do
  local child = tasks[itask]
  local p = parent[child]
  childcnt[p] = childcnt[p] + 1 + childcnt[child]
  child_asked_cnt[p] = child_asked_cnt[p] + 1
  if child_asked_cnt[p] + 1 == #edge[p] then
    table.insert(tasks, p)
  end
end
local allmult = 1
local multiple = {}
for i = 1, n do
  multiple[i] = 1
end
tasks = { {1, 1} }
for itask = 1, n do
  local src, mul = tasks[itask][1], tasks[itask][2]
  -- when start from parents
  multiple[src] = bmul(multiple[src], mul)
  local childall = childcnt[src]
  local otherall = n - 1
  local prog_mul = 1
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      prog_mul = bmul(prog_mul, getComb(otherall, 1 + childcnt[dst]))
      otherall = otherall - 1 - childcnt[dst]
    end
  end
  -- when start from src
  multiple[src] = bmul(multiple[src], prog_mul)
  -- affect to parents
  local affect = bmul(prog_mul, getInvComb(n - 1, n - 1 - childcnt[src]))
  allmult = bmul(allmult, affect)
  -- print(src, prog_mul, allmult)
  local afinv = modinv(affect)
  mul = bmul(mul, afinv)
  multiple[src] = bmul(multiple[src], afinv)
  -- when start from children
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      local prog = bmul(prog_mul, mul)
      prog = bmul(prog, getInvComb(n - 1, 1 + childcnt[dst]))
      table.insert(tasks, {dst, prog})
    end
  end
end
for i = 1, n do
  -- print(i, allmult, multiple[i])
  local z = bmul(allmult, multiple[i])
  print(z)
end

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

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

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

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

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:


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 n = io.read("*n")

local fact = {1}
for i = 2, n do
  fact[i] = bmul(fact[i - 1], i)
end
local function getComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[n], modinv(bmul(fact[k], fact[n - k])))
end
local function getInvComb(n, k)
  if k == 0 or k == n then return 1 end
  return bmul(fact[k], bmul(fact[n - k], modinv(fact[n])))
end

local edge = {}
local parent = {}
local childcnt = {}
local child_asked_cnt = {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  childcnt[i] = 0
  child_asked_cnt[i] = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
for itask = 1, n do
  local src = tasks[itask]
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
    end
  end
end
tasks = {}
-- find leaf
for i = 2, n do
  if #edge[i] == 1 then
    table.insert(tasks, i)
  end
end
for itask = 1, n - 1 do
  local child = tasks[itask]
  local p = parent[child]
  childcnt[p] = childcnt[p] + 1 + childcnt[child]
  child_asked_cnt[p] = child_asked_cnt[p] + 1
  if child_asked_cnt[p] + 1 == #edge[p] then
    table.insert(tasks, p)
  end
end
local allmult = 1
local multiple = {}
for i = 1, n do
  multiple[i] = 1
end
tasks = { {1, 1} }
for itask = 1, n do
  local src, mul = tasks[itask][1], tasks[itask][2]
  -- when start from parents
  multiple[src] = bmul(multiple[src], mul)
  local childall = childcnt[src]
  local otherall = n - 1
  local prog_mul = 1
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      prog_mul = bmul(prog_mul, getComb(otherall, 1 + childcnt[dst]))
      otherall = otherall - 1 - childcnt[dst]
    end
  end
  -- when start from src
  multiple[src] = bmul(multiple[src], prog_mul)
  -- affect to parents
  local affect = bmul(prog_mul, getInvComb(n - 1, n - 1 - childcnt[src]))
  allmult = bmul(allmult, affect)
  -- print(src, prog_mul, allmult)
  mul = bmul(mul, modinv(affect))
  multiple[src] = bmul(multiple[src], modinv(affect))
  -- when start from children
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if parent[src] ~= dst then
      local prog = bmul(prog_mul, mul)
      prog = bmul(prog, getInvComb(n - 1, 1 + childcnt[dst]))
      table.insert(tasks, {dst, prog})
    end
  end
end
for i = 1, n do
  -- print(i, allmult, multiple[i])
  local z = bmul(allmult, multiple[i])
  print(z)
end


local function no()
    print("No")
    os.exit()
end

local s = ""..io.read("*l")
if s ~= s:reverse() then
    no()
end
local front_s = s:sub(1, (s:len() - 1) / 2)
if front_s ~= front_s:reverse() then
    no()
end
local back_s = s:sub((s:len() + 3) / 2, s:len())
if back_s ~= back_s:reverse() then
    no()
end

print("Yes")

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


local function no()
    print("no")
    os.exit()
end

local s = ""..io.read("*l")
if s ~= s:reverse() then
    no()
end
local front_s = s:sub(1, (s:len() - 1) / 2)
if front_s ~= front_s:reverse() then
    no()
end
local back_s = s:sub((s:len() + 3) / 2, s:len())
if back_s ~= back_s:reverse() then
    no()
end

print("yes")


local s = io.read()
local function j(z)
  local n = #z
  for i = 1, n do
    if z:sub(i, i) ~= z:sub(n + 1 - i, n + 1 - i) then
      return false
    end
  end
  return true
end

local s1 = s:sub(1, (#s - 1) // 2)
local s2 = s:sub((#s + 3) // 2, #s)
local f = j(s) and j(s1) and j(s2)
print(f and "Yes" or "No")


local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local z = {}
for i = 1, n do
  local v = t[i]
  if not z[v] then z[v] = 1
  else z[v] = z[v] + 1
  end
end
local tot = 0
for k, val in pairs(z) do
  tot = tot + (val * (val - 1)) // 2
end

for i = 1, n do
  local ret = tot
  local v = t[i]
  ret = ret - (z[v] * (z[v] - 1)) // 2
  ret = ret + ((z[v] - 1) * (z[v] - 2)) // 2
  print(ret)
end

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local h, w, k = io.read("*n", "*n", "*n", "*l")
local map = {}
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    local ss = s:sub(j, j)
    map[(i - 1) * w + j] = ss == "1"
  end
end
local ans = false
local tot = 2^(h - 1)
for it = 0, tot - 1 do
  local tmpi = it
  local box = {1}
  local pos = 1
  for j = 2, h do
    if it % 2 == 1 then
      pos = pos + 1
    end
    box[j] = pos
    it = brs(it, 1)
  end
  local bsize = pos
  local ret = bsize - 1
  -- print(tmpi, ret)
  local bcnt = {}
  for i = 1, bsize do
    bcnt[i] = 0
  end
  local valid = true
  local start_w = 1
  for j = 1, w do
    for i = 1, h do
      local idx = (i - 1) * w + j
      if map[idx] then
        bcnt[box[i]] = bcnt[box[i]] + 1
      end
    end
    local over = false
    for ib = 1, bsize do
      if k < bcnt[ib] then
        over = true
        break
      end
    end
    if over then
      if start_w == j then
        valid = false
        break
      end
      start_w = j
      ret = ret + 1
      for ib = 1, bsize do
        bcnt[ib] = 0
      end
      for i = 1, h do
        local idx = (i - 1) * w + j
        if map[idx] then
          bcnt[box[i]] = bcnt[box[i]] + 1
        end
      end
    end
  end
  if valid then
    if not ans then ans = ret
    else ans = mmi(ans, ret)
    end
  end
end
print(ans)

Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

H W K
S_{1,1}S_{1,2}...S_{1,W}
:
S_{H,1}S_{H,2}...S_{H,W}

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') 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 function open(...) for _,t in pairs{...} do for k,v in pairs(t) do _G[k]=v end end end
----
open(table, math)
local A = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
local N = read.n()

local ans = {}
local build = {}
local function count(s)
    return #s
end
local function pop(s)
    return remove(s)
end
local function push(s, a)
    insert(s, a)
end
local stack_data = {}
local stack_frame = {}
push(stack_frame, {["a"]="d"})
push(stack_data, {0, 0})
do
    ::entry::
    --print("entry", count(stack_data), count(stack_frame))
    local i, mx = unpack(pop(stack_data))
    --print("i,mx",i,mx)
    local j, lim, f
    if i == N then
        insert(ans, concat(build, ""))
        --print("going to branch_end")
        goto branch_end
    end
    j = 1
    lim = mx + 1
    ::loop_start::
    --print("loop_start")
    if j>lim then
        goto loop_end
    end
    build[i+1] = A[j]
    f = {}
    f.a = "r"
    f.i = i
    f.mx = mx
    f.j = j
    f.lim = lim
    --print("recursive")
    push(stack_frame, f)
    push(stack_data, {i+1, max(j,mx)})
    goto entry
    ::respawn::
    --print("respawn")
    goto loop_start
    ::loop_end::
    ::branch_end::
    --print("#stack_frame", count(stack_frame))
    local upper_frame = pop(stack_frame)
    if upper_frame.a == "d" then
        goto done
    elseif upper_frame.a == "r" then
        i = upper_frame.i
        mx = upper_frame.mx
        j = upper_frame.j
        lim = upper_frame.lim
        j = j + 1
        goto respawn
    end
end
::done::

for i=1,#ans do
    print(ans[i])
end

Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab


local mfl = math.floor
local n = io.read("*n")
local t = {string.rep("a", n)}
if 1 < n then
  t[2] = string.rep("a", n - 1) .. "b"

  local function solve(prefix, used, rem)
    if rem == 0 then
      table.insert(t, prefix)
    return end
    local mul = used
    for i = 1, rem - 1 do
      mul = mul * (used + 1)
    end
    for i = 0, mul - 1 do
      local str = ""
      for j = 1, rem do
        str = string.char(97 + i % (used + 1)) .. str
        i = mfl(i / (used + 1))
      end
      table.insert(t, prefix .. str)
    end
    used = used + 1
    prefix = prefix .. string.char(96 + used)
    solve(prefix, used, rem - 1)
  end

  for i = n - 2, 1, -1 do
    local prefix = string.rep("a", i) .. "b"
    solve(prefix, 2, n - i - 1)
  end
end
print(table.concat(t, "\n"))


local mfl = math.floor
local n = io.read("*n")
local t = {string.rep("a", n)}
if 1 < n then
  t[2] = string.rep("a", n - 1) .. "b"

  local function solve(prefix, used, rem)
    if rem == 0 then
      table.insert(t, prefix)
    return end
    local mul = 1
    for i = 1, rem do
      mul = mul * used
    end
    for i = 0, mul - 1 do
      local str = ""
      for j = 1, rem do
        str = string.char(97 + i % used) .. str
        i = mfl(i / used)
      end
      table.insert(t, prefix .. str)
    end
    used = used + 1
    prefix = prefix .. string.char(96 + used)
    solve(prefix, used, rem - 1)
  end

  for i = n - 2, 1, -1 do
    local prefix = string.rep("a", i) .. "b"
    solve(prefix, 2, n - i - 1)
  end
end
print(table.concat(t, "\n"))


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) assert(type(default_val) ~= 'table') 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 A = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
local N = read.n()

local function dfs(i, mx, s)
    if i == N then
        local a = {}
        while s do
            table.insert(a, s.val)
            s = s.next
        end
        local ss = table.concat(a, "")
        print(ss:reverse())
    else
        for j=1,mx+1 do
            local new = {}
            new.val = A[j]
            new.next = s
            dfs(i+1, math.max(j,mx),new)
        end
    end
end
dfs(0, 0, nil)


a = io.read()
a = a:gsub("hi", "")
print(a == "" and "Yes" or "No")

Score : 100 points

Problem Statement

A Hitachi string is a concatenation of one or more copies of the string hi.

For example, hi and hihi are Hitachi strings, while ha and hii are not.

Given a string S, determine whether S is a Hitachi string.

Constraints

The length of S is between 1 and 10 (inclusive).

S is a string consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a Hitachi string, print Yes; otherwise, print No.

Sample Input 1

hihi

Sample Output 1

Yes

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

Sample Input 2

hi

Sample Output 2

Yes

Sample Input 3

ha

Sample Output 3

No


local mmi, mma = math.min, math.max
a, b, m = io.read("*n", "*n", "*n")
x = {}
y = {}
xmin, ymin = 1000000007, 1000000007
for i = 1, a do
  x[i] = io.read("*n")
  xmin = mmi(xmin, x[i])
end
for i = 1, b do
  y[i] = io.read("*n")
  ymin = mmi(ymin, y[i])
end
ret = xmin + ymin
for i = 1, m do
  xi, yi, c = io.read("*n", "*n", "*n")
  ret = mmi(ret, x[xi] + y[yi] - c)
end
print(ret)

Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

1 \le A \le 10^5

1 \le B \le 10^5

1 \le M \le 10^5

1 \le a_i , b_i , c_i \le 10^5

1 \le x_i \le A

1 \le y_i \le B

c_i \le a_{x_i} + b_{y_i}

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.


local A, B, M = io.read("*n", "*n", "*n")
local a, b = {}, {}
for i = 1, A do
    a[i] = io.read("*n")
end
for i = 1, B do
    b[i] = io.read("*n")
end
local min_a = 10^5
local min_b = 10^5
for i = 1, A do
    min_a = math.min(min_a, a[i])
end
for i = 1, B do
    min_b = math.min(min_b, b[i])
end

local min_value = min_a + min_b
for i = 1, M do
    local x, y, c = io.read("*n", "*n", "*n")
    min_value = math.min(min_value, a[x] + b[y] - c)
end

print(min_value)


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

local min_value = a[1]+b[1]
for i = 1, M do
    local x, y, c = io.read("*n", "*n", "*n")
    min_value = math.min(min_value, a[x] + b[y] - c)
end

print(min_value)


local a, b, m = io.read("*n", "*n", "*n")
local at = {}
local bt = {}
local mina, minb = 0, 0
for i = 1, a do
  at[i] = io.read("*n")
  if i == 1 then mina = at[1]
  elseif at[i] < mina then mina = at[i] end
end
for i = 1, b do
  bt[i] = io.read("*n")
  if i == 1 then minb = bt[1]
  elseif bt[i] < minb then minb = bt[i] end
end
local ret = mina + minb
for i = 1, m do
  local x, y, c = io.read("*n", "*n", "*n")
  local r = at[x] + bt[y] - c
  ret = math.min(ret, r)
end
print(ret)


local n = io.read("*n")
local edge = {}
local num = {}
local len = {}
local lim, use = {}, {}
lim[1] = math.ceil(n / 3)
lim[2] = math.ceil((n - 1) / 3)
lim[3] = math.floor(n / 3)
use[1], use[2], use[3] = 0, 0, 0

for i = 1, n do
  edge[i] = {}
  num[i] = 0
  len[i] = -1
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
end
local tasks = {1}
len[1] = 0
num[1] = 2
use[2] = 1
for i = 1, n do
  local src = tasks[i]
  local srcnum = num[src] % 3
  for dst, _u in pairs(edge[src]) do
    if len[dst] == -1 then
      len[dst] = len[src] + 1
      if len[dst] % 2 == 0 then
        if use[2] < lim[2] then
          use[2] = use[2] + 1
          num[dst] = use[2] * 3 - 1
        elseif use[3] < lim[3] then
          use[3] = use[3] + 1
          num[dst] = use[3] * 3
        else
          use[1] = use[1] + 1
          num[dst] = use[1] * 3 - 2
        end
      else
        if use[1] < lim[1] then
          use[1] = use[1] + 1
          num[dst] = use[1] * 3 - 2
        elseif use[3] < lim[3] then
          use[3] = use[3] + 1
          num[dst] = use[3] * 3
        else
          use[2] = use[2] + 1
          num[dst] = use[2] * 3 - 1
        end
      end
      table.insert(tasks, dst)
    end
  end
end
print(table.concat(num, " "))

Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

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

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.


local n = io.read("*n")
local edge = {}
local num = {}
local len = {}
local lim, use = {}, {}
lim[1] = math.ceil(n / 3)
lim[2] = math.ceil((n - 1) / 3)
use[1], use[2], use[3] = 0, 0, 0

for i = 1, n do
  edge[i] = {}
  num[i] = 0
  len[i] = -1
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edge[a][b], edge[b][a] = true, true
end
local tasks = {1}
len[1] = 0
num[1] = 1
use[1] = 1
for i = 1, n do
  local src = tasks[i]
  local srcnum = num[src] % 3
  for dst, _u in pairs(edge[src]) do
    if len[dst] == -1 then
      len[dst] = len[src] + 1
      if len[dst] % 2 == 0 then
        if use[2] < lim[2] then
          use[2] = use[2] + 1
          num[dst] = use[2] * 3 - 1
        else
          use[3] = use[3] + 1
          num[dst] = use[3] * 3
        end
      else
        if use[1] < lim[1] then
          use[1] = use[1] + 1
          num[dst] = use[1] * 3 - 2
        else
          use[3] = use[3] + 1
          num[dst] = use[3] * 3
        end
      end
      table.insert(tasks, dst)
    end
  end
end
print(table.concat(num, " "))


s = io.read("*line")

if string.sub(s, 1, 1) == string.sub(s, 2, 2) and string.sub(s, 2, 2) == string.sub(s, 3, 3) then
  print("No")
else
  print("Yes")
end

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


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 N, A, B = read.nnn()
local n, rem = N // (A+B), N % (A+B)
local b = n * A + math.min(A, rem)
print(b)

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


s=string.gsub(io.read()," ","")
q=io.read("*n","*l")
should_reverse=0
left,right={},{}
for i=1,q do
    t=io.read("*n")
    if t==1 then
        should_reverse=should_reverse+1
    else
        f=io.read("*n")
        c=string.gsub(io.read()," ","")
        if f==1 then
            if should_reverse%2==1 then
                table.insert(right,c)
            else
                table.insert(left,c)
            end
        else
            if should_reverse%2==1 then
                table.insert(left,c)
            else
                table.insert(right,c)
            end
        end
    end
end
s=string.reverse(table.concat(left,""))..s..table.concat(right,"")
print(should_reverse%2==1 and string.reverse(s) or s)

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


n,p=io.read("*n","*n","*l")
s=io.read()

combination=0
if p==2 or p==5 then
    for i=1,n do
        x=tonumber(s:sub(i,i))
        if x%p==0 then
            combination=combination+1
        end
    end
    print(combination)
    return
end

t={}
for i=1,p do
    t[i]=0
end
t[1]=1
digit=1
smod=0
for i=1,n do
    x=tonumber(s:sub(i,i))
    smod=(smod+x*digit)%p
    combination=combination+t[smod+1]
    t[smod+1]=t[smod+1]+1
    digit=(digit*10)%p
end

print(combination)

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

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

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

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

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


function mark(a_rev, b)
  local marked = {false, false, false,
                  false, false, false,
                  false, false, false}
  for i = 1, #b do
    local bi = b[i]
    if a_rev[bi] then
      marked[a_rev[bi]] = true
    end
  end
  return marked
end

function bingo(marked)
  local bingos = {
    {1, 4, 7}, {2, 5, 8}, {3, 6, 9},
    {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
    {1, 5, 9}, {3, 5, 7}
  }

  for i = 1, 8 do
    local c = bingos[i]
    if marked[c[1]] and marked[c[2]] and marked[c[3]] then
      return true
    end
  end

  return false
end

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

local n = io.read("*n")

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

local marked = mark(a_rev, b)
if bingo(marked) then
  print("Yes")
else
  print("No")
end