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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
-----------------------
local N, M, K = read.nnn()
local A = read.N(N)
local B = read.N(M)
local C = {}
for i=1,N do
    C[i] = A[N-i+1]
end
for i=1,M do
    C[i+N] = B[i]
end
local s = 0
local n = 0
local nmax = 0
local left, right -- [left, right)
local function printstate(a)
    --print(a .. ";", "["..left..","..right..")", s, n, nmax)
end
right = N + 1
left = N
s = C[left]
n = 1
nmax = 1
printstate(1)
if s > K then
    s = 0
    left = N + 1
    nmax = 0
end
while true do
    local testleft = left - 1
    if testleft > 0 and s + C[testleft] <= K then
        left = testleft
        s = s + C[left]
        n = n + 1
        nmax = math.max(nmax, n)
        assert(s <= K)
    else
        break
    end
end
assert(s <= K)
printstate(2)
local endflag = false
while not endflag do
    while true do
        local testright = right + 1
        if testright <= N+M+1 and s + C[testright-1] <= K then
            right = testright
            s = s + C[right-1]
            n = n + 1
            nmax = math.max(nmax, n)
            printstate(3)
        else
            break
        end
    end
    assert(s <= K)
    local testleft = left + 1
    if testleft > N+1 then
        endflag = true
        break
    end
    s = s - C[left]
    n = n - 1
    left = testleft
    printstate(4)
    assert(s <= K)
end
assert(s <= K)
print(nmax)


local mmi, mma = math.min, math.max
local n, m, k = io.read("*n", "*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local asum = {a[1]}
for i = 2, n do
  asum[i] = asum[i - 1] + a[i]
end
local b = {}
for i = 1, m do
  b[i] = io.read("*n")
end
local bsum = {b[1]}
for i = 2, m do
  bsum[i] = bsum[i - 1] + b[i]
end
local blim = m
local ret = 0
for i = 0, n do
  local az = i == 0 and 0 or asum[i]
  if k < az then break end
  while 0 < blim and k < az + bsum[blim] do
    blim = blim - 1
  end
  ret = mma(ret, i + blim)
end
print(ret)

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

Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.

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 mmi, mma = math.min, math.max
local n, m, k = io.read("*n", "*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local asum = {a[1]}
for i = 2, n do
  asum[i] = asum[i - 1] + a[i]
end
local b = {}
for i = 1, m do
  b[i] = io.read("*n")
end
local bsum = {b[1]}
for i = 2, m do
  bsum[i] = bsum[i - 1] + b[i]
end
local blim = m
local ret = 0
for i = 1, n do
  local az = asum[i]
  if k < az then break end
  while 0 < blim and k < az + bsum[blim] do
    blim = blim - 1
  end
  ret = mma(ret, i + blim)
end
print(ret)


local n=io.read("n")
local counter=0
for i=1,n do
    counter=counter+n//i*(n//i+1)//2*i
end
print(counter)

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.


local N = io.read("n")
local s = 0
for i=1,N do
    local n = N // i
    local t = (i + n * i) * n // 2
    s = s + t
end
print(s)


local n = io.read("*n")
local ret = 0
for i = 1, n do
  local cnt = n // i
  ret = ret + i * cnt * (cnt + 1) // 2
end
print(ret)


-- RayStark氏のコードを参考にした

local read = io.read
local floor = math.floor
local N = read("*n")
local A = {}
for i = 1, N do
	A[i] = read("*n")
end
table.sort(A)

local A_MAX = A[N]

local cant_div_s = {}
for i = 1, N do
	-- なんでcant_divs[i],(1<=i<=A_MAX)でないか
	-- A[i]にない数字は考えなくて良いから
	cant_div_s[A[i]] = true
end

local last_value = 0
for i = 1, N do
	-- cant_divs全部回したら時間かかるんじゃないのか
	-- []のなかにA[i],(1<=i<=N)を使ってるので1つ目のfor文自体はN回
	if cant_div_s[A[i]] and last_value == A[i] then
		cant_div_s[A[i]] = false
	elseif cant_div_s[A[i]] then
		-- なんでj=1でなく2か
		-- A_MAXまでのA[i]より大きい倍数は倍数だから
		-- TODO: ここがボトルネックになっているためここの最適化したい
		for j = 2, floor(A_MAX / A[i]) do
			cant_div_s[A[i] * j] = false
		end
		-- なんで最後に処理した数字を残しておく必要があるか
		-- Aに同じ数が存在する場合にはじくため
		last_value = A[i]
	end
end

local num = 0
for i = 1, N do
	if cant_div_s[A[i]] then
		num = num + 1
	end
end

print(num)

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5


-- RayStark氏のコードを参考にした

local read = io.read
local floor = math.floor
local N = read("*n")
local A = {}
for i = 1, N do
	A[i] = read("*n")
end
table.sort(A)

local A_MAX = A[N]

local cant_div_s = {}
for i = 1, N do
	-- なんでcant_divs[i],(1<=i<=N)でないか
	-- A[i]にない数字は考えなくて良いから
	cant_div_s[A[i]] = true
end

local last_value = 0
for i = 1, N do
	-- cant_divs全部回したら時間かかるんじゃないのか？
	-- []のなかにA[i],(1<=i<=N)を使ってるのでN回の計算で済む
	if cant_div_s[A[i]] and last_value == A[i] then
		cant_div_s[A[i]] = false
	elseif cant_div_s[A[i]] then
		-- なんでj=1でなく2か
		-- A_MAXまでのA[i]より大きい倍数は倍数だから
		-- TODO: ここがボトルネックになっているためここの最適化
		for j = 2, floor(A_MAX / A[i]) do
			cant_div_s[A[i] * j] = false
		end
		-- なんで最後に処理した数字を残しておく必要があるか
		-- 同じ数が存在する場合にはじくため
		last_value = A[i]
	end
end

local num = 0
for i = 1, N do
	if cant_div_s[A[i]] then
		num = num + 1
	end
end

print(num)


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

local counter=0
for i=1,n do
    local checker=true
    a_map[a[i]]=a_map[a[i]]-1
    for j=1,math.sqrt(a[i]) do
        local div=math.floor(a[i]/j)
        if a[i]%j==0 then
            if (a_map[j] and a_map[j]>0) or (a_map[div] and a_map[div]>0) then
                checker=false
                break
            end
        end
    end
    if checker then
        counter=counter+1
    end
    a_map[a[i]]=a_map[a[i]]+1
end
print(counter)


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

local counter=0
for i=1,n do
    local checker=true
    a_map[a[i]]=a_map[a[i]]-1
    for j=1,math.sqrt(a[i]) do
        local div=math.floor(a[i]/j)
        local ce=(a_map[j] and a_map[j]>0) or (a_map[div] and a_map[div]>0)
        if a[i]%j==0 and ce then
            checker=false
            break
        end
    end
    if checker then
        counter=counter+1
    end
    a_map[a[i]]=a_map[a[i]]+1
end
print(counter)


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

local counter=0
for i=1,n do
    local checker=true
    a_map[a[i]]=a_map[a[i]]-1
    for j=1,math.sqrt(a[i]) do
        local div=math.floor(a[i]/j)
        local ce=(a_map[j] and a_map[j]>0) or (a_map[div] and a_map[div]>0)
        if a[i]%j==0 and ce then
            checker=false
        end
    end
    if checker then
        counter=counter+1
    end
    a_map[a[i]]=a_map[a[i]]+1
end
print(counter)


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

local counter=0
for i=1,n do
    local checker=true
    a_map[a[i]]=a_map[a[i]]-1
    for j=1,math.sqrt(a[i]) do
        if a[i]%j==0 and (a_map[j] and a_map[j]>0) then
            checker=false
        end
    end
    if checker then
        counter=counter+1
    end
    a_map[a[i]]=a_map[a[i]]+1
end
print(counter)


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

local counter=0
for i=1,n do
    local checker=true
    for j=1,math.sqrt(a[i]) do
        if a[i]%j==0 and a_map[j] and a_map[j]-1>0 then
            checker=false
        end
    end
    if checker then
        counter=counter+1
    end
end
print(counter)


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

local counter=0
for i=1,n do
    local checker=true
    for j=1,math.sqrt(a[i]) do
        if a[i]%j==0 and a_map[j] and a_map[j]-1>1 then
            checker=false
        end
    end
    if checker then
        counter=counter+1
    end
end
print(counter)


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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
-----------------------
local floor = math.floor
local yield = coroutine.yield
local insert = table.insert
local remove = table.remove
-------
local N = read.n()
local A = read.N(N)
local S = {}
for i=1,N do
    S[A[i]] = (S[A[i]] or 0) + 1
end
local cnt = 0
for i=1,N do
    local ok = true
    local a = A[i]
    S[a] = S[a] - 1
    local n = a
    for i=1, floor(n ^ 0.5) do
        if n % i == 0 then
            local d = i
            --print(a, d)
            if S[d] and S[d] > 0 then
                ok = false
                break
            end
            d = floor(n / i)
            --print(a, d)
            if S[d] and S[d] > 0 then
                ok = false
                break
            end
        end
    end
    if ok then
        cnt = cnt + 1
    end
    S[a] = S[a] + 1
end
print(cnt)


local read = io.read
local insert = table.insert
local N = read("*n")
local A = {}
local has_A_s = {}

for i = 1, N do
	local num = read("*n")
	if has_A_s[num] == nil then
		has_A_s[num] = true
		insert(A, num)
	end
end

local count = 0
for i = 1, #A do
	for j = 1, #A do
		if i == j then
		elseif A[i] % A[j] == 0 then
			count = count + 1
			break
		end
	end
end

print((#A == 1) and 0 or #A - count)


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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
-----------------------
local floor = math.floor
local function gen_divisor(n)
    return coroutine.wrap(function()
        ds = {}
        for i=1, floor(n ^ 0.5) do
            if n % i == 0 then
                coroutine.yield(i)
                table.insert(ds, floor(n / i))
            end
        end
        if floor(n / ds[#ds]) == ds[#ds] then
            table.remove(ds)
        end
        for i=#ds,1,-1 do
            coroutine.yield(ds[i])
        end
    end)
end
-------
local N = read.n()
local A = read.N(N)
local S = {}
for i=1,N do
    S[A[i]] = (S[A[i]] or 0) + 1
end
local cnt = 0
if DBG then
    print(tostringxx(S))
end
for i=1,N do
    local ok = true
    local a = A[i]
    S[a] = S[a] - 1
    if DBG then
        print("try", a)
    end
    for d in gen_divisor(a) do
        if S[d] and S[d] > 0 then
            ok = false
            if DBG then
                print("d=", d, false)
            end
            break
        else
            if DBG then
                print("d=", d, true)
            end
        end
    end
    if ok then
        cnt = cnt + 1
    end
    S[a] = S[a] + 1
end
print(cnt)


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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
-----------------------
local floor = math.floor
local function gen_divisor(n)
    return coroutine.wrap(function()
        ds = {}
        for i=1, floor(n ^ 0.5) do
            if n % i == 0 then
                coroutine.yield(i)
                table.insert(ds, floor(n / i))
            end
        end
        if floor(n / ds[#ds]) == ds[#ds] then
            table.remove(ds)
        end
        for i=#ds,1,-1 do
            coroutine.yield(ds[i])
        end
    end)
end
-------
local N = read.n()
local A = read.N(N)
local S = {}
for i=1,N do
    S[A[i]] = (S[A[i]] or 0) + 1
end
local cnt = 0
for i=1,N do
    local ok = true
    local a = A[i]
    if S[a] > 1 then
        ok = false
    else
        for d in gen_divisor(a) do
            if d ~= 1 and d ~= a then
                if S[d] then
                    ok = false
                end
            end
        end
    end
    if ok then
        cnt = cnt + 1
    end
end
print(cnt)


local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a = {}
local t = {}
for i = 1, 1000 * 1000 do
  t[i] = 0
end
for i = 1, n do
  a[i] = io.read("*n")
end
local acmp = {}
for i = 1, n do
  local v = a[i]
  if not acmp[v] then acmp[v] = 1
  else acmp[v] = acmp[v] + 1
  end
end
for v, cnt in pairs(acmp) do
  local lim = mfl(1000000 / v)
  for j = 1, lim do
    t[j * v] = t[j * v] + cnt
  end
end
local c = 0
for i = 1, n do
  local v = a[i]
  if t[v] == 1 then
    c = c + 1
  end
end
print(c)


local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a = {}
local t = {}
for i = 1, 1000 * 1000 do
  t[i] = 0
end
for i = 1, n do
  a[i] = io.read("*n")
end
for i = 1, n do
  local v = a[i]
  local lim = mfl(1000000 / v)
  for j = 1, lim do
    t[j * v] = t[j * v] + 1
  end
end
local c = 0
for i = 1, n do
  local v = a[i]
  if t[v] == 1 then
    c = c + 1
  end
end
print(c)


local n=io.read("n")
local answer=1
local length=0
for i=1,n do
    a=io.read("n")
    length=length+math.log(a,10)
    answer=answer*a
end
if length>18 or answer>10^18 then
    print(-1)
else
    print(answer)
end

Score : 200 points

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N.

However, if the result exceeds 10^{18}, print -1 instead.

Constraints

2 \leq N \leq 10^5

0 \leq A_i \leq 10^{18}

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 value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}.

Sample Input 1

2
1000000000 1000000000

Sample Output 1

1000000000000000000

We have 1000000000 \times 1000000000 = 1000000000000000000.

Sample Input 2

3
101 9901 999999000001

Sample Output 2

-1

We have 101 \times 9901 \times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

Sample Input 3

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

Sample Output 3

0


local n=io.read("n")
local a={}
local zero=false
for i=1,n do
    a[i]=io.read("n")
    if a[i]==0 then
        zero=true
    end
end
if zero then
    print(0)
else
    local answer=1
    for i=1,n do
        answer=answer*a[i]
        if answer>10^18 then
            print(-1)
            return
        end
    end
    print(string.format("%d",answer))
end


local n=io.read("n")
local a={}
local zero=false
for i=1,n do
    a[i]=io.read("n")
    if a[i]==0 then
        zero=true
    end
end
if zero then
    print(0)
else
    local answer=1
    for i=1,n do
        answer=answer*a[i]
        if answer>10^18 then
            print(-1)
            return
        end
    end
    print(answer)
end


local n=io.read("n")
local answer=1
for i=1,n do
    answer=answer*io.read("n")
    if answer>10^18 then
        print(-1)
        return
    end
end
print(answer)


local n=io.read("n")
local answer=1
for i=1,n do
    answer=answer*io.read("n")
end
if answer>10^18 then
    print(-1)
else
    print(answer)
end


N=io.read"*n"
A={}
for i=1,N do A[i]=io.read"*n"end
table.sort(A)
if(A[1]==0)then print(0)return end
m=1
lim=10^18
for i=1,N do
	if(m<lim/A[i])then print(-1)return end
	m=m*A[i]
end
print(m)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		print(out)
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

if 0 <= out and out <= max then
	while true do
		out = -1
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		print(out)
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

if not (0 <= out and out <= max) then
	while true do
		out = -1
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
		else
			while true do
				out = 0
			end
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		while true do
			out = -1
		end
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
			while true do
				out = -1
			end
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 1000000000 * 1000000000

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

print(out)


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 N = read.n()
local A = read.N(N)
local p = 1
local ov = false
for i=1,N do
    if A[i] == 0 then
        print(0)
        return
    end
    local np = p * A[i]
    if math.log(p, 10) + math.log(A[i], 10) > 18 then
        ov = true
    end
    if np > 10^18 then
        ov = true
    end
    p = np
end
if ov then
    print(-1)
else
    print(p)
end


local read = io.read
local N = read("n")
local max = 10^18

local out = 1
for _i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if not (0 <= out and out <= max) then
			out = -1
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local max = 10^18

local out = 1
for i = 1, N do
	local num = read("n")
	if num == 0 then
		out = 0
		break
	end
	if out > 0 then
		out = out * num
		if out < 0 or max < out then
			out = -1
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local inf = 10^18

local out = 1
for i = 1, N do
	local num = read("n")
	if out < 0 and num == 0 then
		out = 0
		break
	else
		out = out * num
		if out < 0 or inf < out then
			out = -1
		end
	end
end

print(out)


local read = io.read
local N = read("n")
local inf = 10^18

local out = 1
for i = 1, N do
	if out < 0 then
		local preZ = read("n")
		if preZ == 0 then
			out = 0
			break
		end
	else
		out = out * read("n")
	end
	if out < 0 or inf < out then
		out = -1
	end
end

print(out)


local read = io.read
local N = read("n")
local inf = 10^18

local out = 1
for i = 1, N do
	if out < 0 then
		local preZ = read("n")
		if preZ == 0 then
			out = 0
			break
		end
	else
		out = out * read("n")
	end
	if out > inf then
		out = -1
	end
end

print(out)


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 N = read.n()
local A = read.N(N)
local p = 1
local ov = false
for i=1,N do
    if A[i] == 0 then
        print(0)
        return
    end
    local np = p * A[i]
    if np > 10^18 then
        ov = true
    end
    p = np
end
if ov then
    print(-1)
else
    print(p)
end


n = io.read("*n")
t = 1
a = {}
haszero = false
for i = 1, n do
  a[i] = io.read("*n")
  if a[i] == 0 then haszero = true end
end
if haszero then
  print(0)
  os.exit()
end
for i = 1, n do
  local lim = (1000000000 * 1000000000) // t
  if lim < a[i] then
    t = -1
    break
  end
  t = t * a[i]
end
print(t)


local A, B = io.read("n", "n")

B = math.floor(B * 100 + 0.5)

local int = math.floor(A * B)
print(int // 100)

Score : 300 points

Problem Statement

Compute A \times B, truncate its fractional part, and print the result as an integer.

Constraints

0 \leq A \leq 10^{15}

0 \leq B < 10

A is an integer.

B is a number with two digits after the decimal point.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.

Sample Input 1

198 1.10

Sample Output 1

217

We have 198 \times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

Sample Input 2

1 0.01

Sample Output 2

0

Sample Input 3

1000000000000000 9.99

Sample Output 3

9990000000000000


local A, B = io.read("n", "n")

B = math.floor(B + 0.5)

local int = math.floor(A * B)
print(int)


local A, B = io.read("n", "n")

B = math.floor((B + 0.5) * 100)

local int = math.floor(A * B)
print(int // 100)


local a=io.read("n")
local b=io.read():gsub(" ","")
b=b:sub(1,1)..b:sub(3)
b=tonumber(b)
print(a*b//100)


local a,b=io.read("n","n")
local answer=math.modf(a*b)
print(answer)


local a,b=io.read("n","n")
b=100*b
print(string.format("%d",a*b//100))


local A, B = io.read("n", "n")

B = math.floor(B * 100)

local int = math.floor(A * B)
print(int//100)


local A, B = io.read("n", "n")

B = math.floor(B * 100)
print(B)

local int = math.floor(A * B)
print(int//100)


local A, B = io.read("n", "n")

local int = math.modf(A * B)

print(int)


local A, B = io.read("n", "n")

print(math.floor(A * B))


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, B = read.l():split()
A = tonumber(A)
B = B:totable()
local B100 = tonumber(B[1]) * 100 + tonumber(B[3]) * 10 + tonumber(B[4])
local P100 = A * B100
local s = tostring(P100)
if #s <= 2 then
    print(0)
else
    print(s:sub(1,#s-2))
end


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, B = read.nn()
print(math.floor(A*B))


s = io.read()
a, b, c = s:match("(%d+) (%d+)%.(%d%d)")
a, b, c = math.floor(a), math.floor(b), math.floor(c)
d = a * (b * 100 + c)
print(d // 100)


local n = io.read("*n")
local k = io.read("*n")
local tbA = {}
for i = 1,n do
    table.insert(tbA,io.read("*n"))
end
io.read()

function isNumInTb(num,tb)
    for k,v in ipairs(tb) do
        if v == num then
            return k
        end
    end
    return 0
end

-- function print_tb(tb)
--     for k,v in ipairs(tb) do
--         print(v.." ")
--     end
-- end

local new_tb = {}
local new_new_tb = {}
local idx = 0

local i = 1
while tbA[i] do
    if isNumInTb(i,new_tb) > 0 then

        local j = isNumInTb(i,new_tb)
        idx = isNumInTb(i,new_tb)
        for m=idx,#new_tb do
            table.insert(new_new_tb,new_tb[m])
            j = j + 1
        end
        break
    else
        table.insert(new_tb,i)
        i = tbA[i]
    end
end
k = k - idx + 1
local numTb = #new_new_tb

local kk = math.mod(k,numTb)
if kk==0 then kk = kk + numTb end
print(tbA[new_new_tb[kk]])

-- for i = 1, string.len(a) do
--     table.insert(b,string.sub(a,i,i))
-- end

Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2


local n = io.read("*n")
local k = io.read("*n")
local tbA = {}
for i = 1,n do
    table.insert(tbA,io.read("*n"))
end

function isNumInTb(num,tb)
    for k,v in ipairs(tb) do
        if v == num then
            return k
        end
    end
    return 0
end

-- function print_tb(tb)
--     for k,v in ipairs(tb) do
--         print(v.." ")
--     end
-- end

local new_tb = {}
local new_new_tb = {}
local idx = 0

local i = 1
while tbA[i] do
    if isNumInTb(i,new_tb) > 0 then

        local j = isNumInTb(i,new_tb)
        idx = isNumInTb(i,new_tb)
        for m=idx,#new_tb do
            table.insert(new_new_tb,new_tb[m])
            j = j + 1
        end
        break
    else
        table.insert(new_tb,i)
        i = tbA[i]
    end
end
k = k - idx + 1
local numTb = #new_new_tb

local kk = math.mod(k,numTb)
if kk==0 then kk = kk + numTb end
print(tbA[new_new_tb[kk]])

-- for i = 1, string.len(a) do
--     table.insert(b,string.sub(a,i,i))
-- end


local n = io.read("*n")
local k = io.read("*n"
)
io.read("*l")
local towns = {}
for i = 1, n do
	towns[i] = io.read("*n")
end
io.read("*l")
local visited = {}
local step = 1
local idx = 1
local period
local periodstart
local steps = {}
while(step <= k) do
	if visited[towns[idx]] then
		period = step - visited[towns[idx]]
		periodstart = visited[towns[idx]]
		break
	end
	visited[towns[idx]] = step
	steps[step] = towns[idx]
	idx = towns[idx]
	step = step + 1
end
if period then
	local resttimes = k - step
	resttimes = resttimes % period
	print(steps[periodstart + resttimes])
else
	print(idx)
end


n,k=io.read("*n","*n")
p=1 a={} b={} for i=1,n do a[i]=io.read("*n") b[i]=0 end
while 0<k do if k%2==1 then p=a[p] end k=k//2
for i=1,n do b[i]=a[a[i]] end
for i=1,n do a[i]=b[i] end
end print(p)


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

path={1}
visited={}
position=1
for i=1,n do
    table.insert(path,a[position])
    if not visited[position] then
        visited[position]=true
    else
        break
    end
    if i==k then
        print(a[position])
        return
    end
    position=a[position]
end

loop=0
distance=0
for i=1,#path do
    if path[i]==a[position] then
        loop=#path-i
        distance=i-2
        break
    end
end

x=(k-distance)%loop
for i=1,x do
    position=a[position]
end
print(position)


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

path={1}
visited={}
position=1
for i=1,n do
    table.insert(path,a[position])
    if not visited[position] then
        visited[position]=true
    else
        break
    end
    position=a[position]
end

loop=0
distance=0
for i=1,#path do
    if path[i]==a[position] then
        loop=#path-i
        distance=i-2
        break
    end
end

x=(k-distance)%loop
for i=1,x do
    position=a[position]
end
print(position)


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

t={true}
s={}
for i=1,n do
    if not t[a[i]] then
        t[a[i]]=true
    else
        s={a[i],i}
    end
end

x=1
y=1
for i=1,n do
    if a[i]==s[1] then
        x=i
        y=s[2]-i
        break
    end
end
print(y>0 and a[x+(k-x)%y] or x)


t={true}
s={}
for i=1,n do
    if not t[a[i]] then
        t[a[i]]=true
    else
        s={a[i],i}
    end
end

x=1
y=1
for i=1,n do
    if a[i]==s[1] then
        x=i
        y=s[2]-i
        break
    end
end
print(y>0 and a[x+(k-x)%y] or x)


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

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

local function goToK(town, rem_times)
	if rem_times <= 0 then
		return town
	else
		return goToK(A[town], rem_times - 1)
	end
end

local last_times = {}
local function teleport(town, times)
	if times == K then
		return town
	end

	local last_time = last_times[town]
	if last_time ~= nil then
		local loop_times = times - last_time
		local K_in_loop = K - last_time
		K_in_loop = K_in_loop % loop_times

		return goToK(town, K_in_loop)
	else
		last_times[town] = times

		return teleport(A[town], times + 1)
	end
end

print(teleport(1, 0))


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

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

local function goToK(town, rem_times)
	print(town, rem_times)
	if rem_times <= 0 then
		return town
	else
		return goToK(A[town], rem_times - 1)
	end
end

local last_times = {}
local function teleport(town, times)
	if times == K then
		return town
	end

	local last_time = last_times[town]
	if last_time ~= nil then
		local loop_times = times - last_time
		local K_in_loop = K - last_time
		K_in_loop = K_in_loop % loop_times

		return goToK(town, K_in_loop)
	else
		last_times[town] = times

		return teleport(A[town], times + 1)
	end
end

print(teleport(1, 0))


local N = io.read("*n")
local K = 0LL + io.read("*n")

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

local function goToK(town, rem_times)
	if rem_times <= 0 then
		return town
	else
		return goToK(A[town], rem_times - 1)
	end
end

local have_comes = {}
local function teleport(town, times)
	if times == K then
		return town
	end
	local last_time = have_comes[town]
	if last_time ~= nil then
		local K_in_loop = K - last_time
		local loop_times = times - last_time
		K_in_loop = K_in_loop % loop_times
		return goToK(town, K_in_loop)
	else
		have_comes[town] = times
		return teleport(A[town], times + 1)
	end
end

print(teleport(1, 0))


N=io.read"*n"
K=io.read"*n"
A={}
for i=1,N do
	A[i]=io.read"*n"
end

D={[0]=A}
for i=1,math.log(K)/math.log(2) do
	D[i]={}
	for j=1,N do
		D[i][j]=D[i-1][D[i-1][j]]
	end
end

local a=1
for i=0,math.log(K)/math.log(2)do
	if((K>>i)&1~=0)then
		a=D[i][a]
	end
end
print(a)


N=io.read"*n"
K=io.read"*n"
A={}
for i=1,N do
	A[i]=io.read"*n"
end

B={[1]=0}
C={[0]=1}
for i=1,math.min(N,K) do
	C[i]=A[C[i-1]]
	if(B[C[i]])then
		local j=B[C[i]]
		print(C[j+(K-j)%(i-j)])
		return
	end
	B[C[i]]=i
end
print(C[K])


N=io.read"*n"
K=io.read"*n"
A={}
for i=1,N do
	A[i]=io.read"*n"
end

local function count(t)local r=0 for _,_ in pairs(t)do r=r+1 end return r end

B={[1]=0}
C={[0]=1}
for i=1,math.min(N,K) do
	C[i]=A[C[i-1]]
	if(B[C[i]])then
		t=C[i]
		for j=i-1,1,-1 do
			if(C[j]==t)then
				print(C[j+(K-j)%(i-j)])
				return
			end
		end
	end
	B[C[i]]=0
end
print(C[K])


local n, k = io.read("*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local map = {}
local len = 0
map[1] = 0
local src = 1
local rem = 0
while true do
  len = len + 1
  local dst = a[src]
  if len == k then
    print(dst)
    os.exit()
  end
  if map[dst] then
    local offset = map[dst]
    local loopSize = len - offset
    rem = k - offset
    rem = rem % loopSize
    src = dst
    break
  else
    map[dst] = len
  end
  src = dst
end
for i = 1, rem do
  local dst = a[src]
  src = dst
end
print(src)


local a,b,c,d=io.read("n","n","n","n")
while true do
    c=c-b
    if c<=0 then
        print("Yes")
        return
    end
    a=a-d
    if a<=0 then
        print("No")
        return
    end
end

Score : 200 points

Problem Statement

Takahashi and Aoki will have a battle using their monsters.

The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.

The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...
Here, an attack decreases the opponent's health by the value equal to the attacker's strength.
The monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.

If Takahashi will win, print Yes; if he will lose, print No.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi will win, print Yes; if he will lose, print No.

Sample Input 1

10 9 10 10

Sample Output 1

No

First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.

Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.

Takahashi's monster is the first to have 0 or less health, so Takahashi loses.

Sample Input 2

46 4 40 5

Sample Output 2

Yes


local a,b,c,d=io.read("n","n","n","n")
while a>0 or c>0 do
    c=c-b
    a=a-d
end
if a<=0 then
    print("No")
else
    print("Yes")
end


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

e = math.ceil(c / b)
f = math.ceil(a / d)

print(e <= f and "Yes" or "No")


A,B,C,D=io.read("*n","*n","*n","*n")
flag=true

while flag do
  if C>B then C=C-B
  else
    print("Yes")
    flag=false
    break
  end
  if A>D then A=A-D
  else
    print("No")
    flag=false
  end
end


local m1_hp, m1_at, m2_hp, m2_at = io.read("*n", "*n", "*n", "*n")

while true do
	m2_hp = m2_hp - m1_at
	if m2_hp <= 0 then
		print("Yes")
		break
	end

	m1_hp = m1_hp - m2_at
	if m1_hp <= 0 then
		print("No")
		break
	end
end


A=io.read"*n"
B=io.read"*n"
C=io.read"*n"
D=io.read"*n"

print((
	math.ceil(C/B)<=math.ceil(A/D)
)and"Yes"or"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) 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 T_HP, T_ATK, A_HP, A_ATK = read.nnnn()
local taka = true
while T_HP > 0 and A_HP > 0 do
    if taka then
        A_HP = A_HP - T_ATK
    else
        T_HP = T_HP - A_ATK
    end
    taka = not taka
end
if T_HP <= 0 then
    print("No")
else
    print("Yes")
end


local a, b, c, d = io.read("*n", "*n", "*n", "*n")
while true do
  if c <= b then
    print("Yes")
    break
  else
    c = c - b
  end
  if a <= d then
    print("No")
    break
  else
    a = a - d
  end
end


local s=io.read()
local map={[0]=1}
local digit=1
local ms=0
for i=#s,1,-1 do
    local n=tonumber(s:sub(i,i))
    ms=(ms+n*digit)%2019
    map[ms]=(map[ms] or 0)+1
    digit=(digit*10)%2019
end

local counter=0
for k,v in pairs(map) do
    counter=counter+v*(v-1)//2
end
print(counter)

Score : 400 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.

Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:

Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

Constraints

1 ≤ |S| ≤ 200000

S is a string consisting of digits from 1 through 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

Sample Input 1

1817181712114

Sample Output 1

3

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.


local s=io.read()
local map={[0]=1}
local digit=1
local ms=0
for i=#s,1,-1 do
    local n=tonumber(s:sub(i,i))
    local ms=(ms+n*digit)%2019
    map[ms]=(map[ms] or 0)+1
    digit=digit*10%2019
end
local counter=0
for k,v in pairs(map) do
    counter=counter+v*(v-1)//2
end
print(counter)


local s=io.read()
local map={[0]=1}
for i=#s,1,-1 do
    local n=tonumber(s:sub(i))
    n=n%2019
    map[n]=(map[n] or 0)+1
end
local counter=0
for k,v in pairs(map) do
    counter=counter+v*(v-1)//2
end
print(counter)


local s=io.read()
local n=#s
local p=2019

local t={}
for i=1,p do
    t[i]=(i==1 and 1 or 0)
end

local combination=0
local digit=1
local smod=0
for i=n,1,-1 do
    local 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)


local s = io.read()
local t = {}
for i = 1, 2019 do
  t[i] = 0
end
t[2019] = 1
local mul = 1
local v = 0
local ret = 0
for i = #s, 1, -1 do
  v = v + mul * (s:sub(i, i):byte() - 48)
  v = v % 2019
  if v == 0 then
    ret = ret + t[2019]
    t[2019] = t[2019] + 1
  else
    ret = ret + t[v]
    t[v] = t[v] + 1
  end
  mul = (mul * 10) % 2019
end
print(ret)


local MOD=2019
local function add(a,b)return (a+b)%MOD end
local function sub(a,b)return (a-b)%MOD end
local function mul_(a,b)
	local ret=0
	while(b>0)do
		if(b%2==1)then ret=(ret+a)%MOD end
		a=(a+a)%MOD
		b=math.floor(b*.5)
	end
	return ret
end
local function mul(a,b)
	return a<b and mul_(b,a)or mul_(a,b)
end
local function pow(a,b)
	local ret=1
	while(b>0)do
		if(b%2==1)then ret=mul(ret,a)end
		a=mul(a,a)
		b=math.floor(b*.5)
	end
	return ret
end
local function inv(x)
	local a=x
	local b,u,v=MOD,1,0
	repeat
		local t=math.floor(a/b)
		a,b=b,a-t*b
		u,v=v,u-t*v
	until(b==0)
	return u%MOD
end
local function div(a,b)return mul(a,inv(b))end
t={}
s={0}
while 1 do
	local a=io.read(1)
	if not(a)then break end
	table.insert(t,tonumber(a))
	table.insert(s,s[#s]+t[#t])
end
N=#t

dp={[0]=0}
a={[0]=1}
for i=1,N do
	dp[i]=add(dp[i-1],mul(t[N-i+1],pow(10,i-1)))
	if(a[dp[i]])then
		a[dp[i]]=a[dp[i]]+1
	else
		a[dp[i]]=1
	end
end
c=0
for _,v in pairs(a)do
	c=c+v*(v-1)/2
end
print(c)


ans=0
S=io.read("*l")
for i=2019,200000,2019 do if string.find(S,i)~=nil then ans=ans+1 end end
print(ans)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) 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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
----
local S = read.l():gsub("%s", ""):reverse():totable()
local N = #S
local X = {}
local ten = 1
for i=1,N do
    X[i] = ten * tonumber(S[i]) % 2019
    ten = ten * 10 % 2019
end
local A = {}
A[0] = 0
for i=1,N do
    A[i] = math.floor((A[i-1] + X[i]) % 2019)
end
--print(tostringxx(A))
local set = {}
for i=0,N do
    --print(i, A[i])
    set[A[i]] = (set[A[i]] or 0) + 1
end
--print(tostringxx(set))
local ans = 0
for k,v in pairs(set) do
    ans = ans + v * (v - 1) // 2
end
print(ans)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) 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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
----
local S = read.l():gsub("%s", ""):reverse():totable()
local N = #S
local X = {}
local ten = 1
for i=1,N do
    X[i] = ten * tonumber(S[i]) % 2019
    ten = ten * 10 % 2019
end
local A = {}
A[0] = 0
for i=1,N do
    A[i] = math.floor((A[i-1] + X[i]) % 2019)
end
--print(tostringxx(A))
local set = {}
for i=0,N do
    print(i, A[i])
    set[A[i]] = (set[A[i]] or 0) + 1
end
--print(tostringxx(set))
local ans = 0
for k,v in pairs(set) do
    ans = ans + v * (v - 1) // 2
end
print(ans)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) 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 tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
----
local S = read.l():gsub("%s", ""):reverse():totable()
local N = #S
local X = {}
for i=1,N do
    X[i] = math.floor(10^(i-1)) * tonumber(S[i]) % 2019
end
local A = {}
A[0] = 0
for i=1,N do
    A[i] = (A[i-1] + X[i]) % 2019
end
--print(tostringxx(A))
local set = {}
for i=0,N do
    set[A[i]] = (set[A[i]] or 0) + 1
end
--print(tostringxx(set))
local ans = 0
for k,v in pairs(set) do
    ans = ans + v * (v - 1) // 2
end
print(ans)


t={}
s={0}
while 1 do
	local a=io.read(1)
	if not(a)then break end
	table.insert(t,tonumber(a))
	table.insert(s,s[#s]+t[#t])
end
N=#t

r=0
for i=1,N do
for j=1,i-3 do
	if((s[i+1]-s[j])%3==0)then
		local n=0
		for k=j,i do
			n=n*10+t[k]
		end
		if(n%673==0)then r=r+1 end
	end
end
end
print(r)


S=io.read()
N=#S
t={}
s={0}
for i=1,N do
	t[i]=tonumber(S:sub(i,i))
	s[i+1]=s[i]+t[i]
end

r=0
for i=1,N do
for j=1,i-3 do
	if((s[i+1]-s[j])%3==0)then
		local n=0
		for k=j,i do
			n=n*10+t[k]
		end
		if(n%673==0)then r=r+1 end
	end
end
end
print(r)


local s = io.read()
local mul = 1
local t = {}
for i = 0, 2018 do
  t[i] = 0
end
t[0] = 1
local cur = 0
local ret = 0
for i = #s, 1, -1 do
  local v = s:sub(i, i):byte() - 48
  cur = (cur + mul * v) % 2019
  ret = ret + t[cur]
  t[cur] = t[cur] + 1
  mul = (mul * 10) % 2019
end
print(ret)


local mmi, mma = math.min, math.max
local n, m, s = io.read("*n", "*n", "*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
local line = {}
for i = 1, m do
  local u, v, a, b = io.read("*n", "*n", "*n", "*n")
  edge[u][v] = {a, b}
  edge[v][u] = {a, b}
  line[i] = {u, v, a, b}
end
local coin = {}
local changetime = {}
for i = 1, n do
  coin[i], changetime[i] = io.read("*n", "*n")
end
local dp = {}
for i = 1, n do
  dp[i] = {}
  for j = 1, 2501 do
    dp[i][j] = -1
  end
end
s = mmi(s, 2500)
dp[1][s + 1] = 0
local function updateCity()
  for i = 1, n do
    for j = 1, 2501 do
      if 0 <= dp[i][j] then
        local dst = mmi(2501, j + coin[i])
        if dp[i][dst] < 0 then
          dp[i][dst] = dp[i][j] + changetime[i]
        else
          dp[i][dst] = mmi(dp[i][dst], dp[i][j] + changetime[i])
        end
      end
    end
  end
end
local function moveOn()
  for il = 1, m do
    local u, v, a, b = unpack(line[il])
    for i = 1 + a, 2501 do
      if 0 <= dp[u][i] then
        if dp[v][i - a] < 0 then
          dp[v][i - a] = dp[u][i] + b
        else
          dp[v][i - a] = mmi(dp[v][i - a], dp[u][i] + b)
        end
      end
      if 0 <= dp[v][i] then
        if dp[u][i - a] < 0 then
          dp[u][i - a] = dp[v][i] + b
        else
          dp[u][i - a] = mmi(dp[u][i - a], dp[v][i] + b)
        end
      end
    end
  end
end
for i = 1, 111 do
  updateCity()
  moveOn()
end
for i = 2, n do
  local ret = -1
  for j = 1, 2501 do
    if 0 <= dp[i][j] then
      if ret < 0 then
        ret = dp[i][j]
      else
        ret = mmi(ret, dp[i][j])
      end
    end
  end
  print(ret)
end

Score : 500 points

Problem Statement

There are N cities numbered 1 to N, connected by M railroads.

You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket.

The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes.
You cannot use gold coins to pay the fare.

There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin.
The transaction takes D_i minutes for each gold coin you give.
You can exchange any number of gold coins at each exchange counter.

For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq 100

0 \leq S \leq 10^9

1 \leq A_i \leq 50

1 \leq B_i,C_i,D_i \leq 10^9

1 \leq U_i < V_i \leq N

There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j).

Each city t=2,...,N can be reached from City 1 with some number of railroads.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M S
U_1 V_1 A_1 B_1
:
U_M V_M A_M B_M
C_1 D_1
:
C_N D_N

Output

For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t.

Sample Input 1

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

Sample Output 1

2
14

The railway network in this input is shown in the figure below.

In this figure, each city is labeled as follows:

The first line: the ID number i of the city (i for City i)

The second line: C_i / D_i

Similarly, each railroad is labeled as follows:

The first line: the ID number i of the railroad (i for the i-th railroad in input)

The second line: A_i / B_i

You can travel from City 1 to City 2 in 2 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.

You can travel from City 1 to City 3 in 14 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.

At the exchange counter in City 2, exchange 3 gold coins for 3 silver coins in 6 minutes.

Use the 1-st railroad to move from City 2 to City 1 in 2 minutes.

Use the 2-nd railroad to move from City 1 to City 3 in 4 minutes.

Sample Input 2

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

Sample Output 2

5
5
7

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 4 in 7 minutes, as follows:

At the exchange counter in City 1, exchange 2 gold coins for 6 silver coins in 2 minutes.

Use the 2-nd railroad to move from City 1 to City 3 in 4 minutes.

Use the 4-th railroad to move from City 3 to City 4 in 1 minutes.

Sample Input 3

6 5 1
1 2 1 1
1 3 2 1
2 4 5 1
3 5 11 1
1 6 50 1
1 10000
1 3000
1 700
1 100
1 1
100 1

Sample Output 3

1
9003
14606
16510
16576

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 6 in 16576 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.

At the exchange counter in City 2, exchange 3 gold coins for 3 silver coins in 9000 minutes.

Use the 1-st railroad to move from City 2 to City 1 in 1 minute.

Use the 2-nd railroad to move from City 1 to City 3 in 1 minute.

At the exchange counter in City 3, exchange 8 gold coins for 8 silver coins in 5600 minutes.

Use the 2-nd railroad to move from City 3 to City 1 in 1 minute.

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.

Use the 3-rd railroad to move from City 2 to City 4 in 1 minute.

At the exchange counter in City 4, exchange 19 gold coins for 19 silver coins in 1900 minutes.

Use the 3-rd railroad to move from City 4 to City 2 in 1 minute.

Use the 1-st railroad to move from City 2 to City 1 in 1 minute.

Use the 2-nd railroad to move from City 1 to City 3 in 1 minute.

Use the 4-th railroad to move from City 3 to City 5 in 1 minute.

At the exchange counter in City 5, exchange 63 gold coins for 63 silver coins in 63 minutes.

Use the 4-th railroad to move from City 5 to City 3 in 1 minute.

Use the 2-nd railroad to move from City 3 to City 1 in 1 minute.

Use the 5-th railroad to move from City 1 to City 6 in 1 minute.

Sample Input 4

4 6 1000000000
1 2 50 1
1 3 50 5
1 4 50 7
2 3 50 2
2 4 50 4
3 4 50 3
10 2
4 4
5 5
7 7

Sample Output 4

1
3
5

The railway network in this input is shown in the figure below:

Sample Input 5

2 1 0
1 2 1 1
1 1000000000
1 1

Sample Output 5

1000000001

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 2 in 1000000001 minutes, as follows:

At the exchange counter in City 1, exchange 1 gold coin for 1 silver coin in 1000000000 minutes.

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.


Score : 100 points

Problem Statement

Print the circumference of a circle of radius R.

Constraints

1 \leq R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R

Output

Print the circumference of the circle.
Your output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.

Sample Input 1

1

Sample Output 1

6.28318530717958623200

Since we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not.

Sample Input 2

73

Sample Output 2

458.67252742410977361942


r = io.read("*n")
print(2 * math.pi * r)


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

print(2 * R * math.pi)


local r = io.read("*n")
print(math.pi * r)


if io.read(1)=="7" or io.read(1)=="7" or io.read(1)=="7" then print("Yes") else print("No") end

Score : 100 points

Problem Statement

Given is a three-digit integer N. Does N contain the digit 7?

If so, print Yes; otherwise, print No.

Constraints

100 \leq N \leq 999

Input

Input is given from Standard Input in the following format:

N

Output

If N contains the digit 7, print Yes; otherwise, print No.

Sample Input 1

117

Sample Output 1

Yes

117 contains 7 as its last digit.

Sample Input 2

123

Sample Output 2

No

123 does not contain the digit 7.

Sample Input 3

777

Sample Output 3

Yes


N=tostring(io.read())
if N:sub(1,1) == "7" or N:sub(2,2) == "7" or N:sub(3,3) == "7" then print("Yes") else print("No") end


print((io.read(1)=="7" or io.read(1)=="7" or io.read(1)=="7")and"Yes"or"No")


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

local isYes = false
for i = 1, 3 do
    if N:sub(i, i) == "7" then
        isYes = true
    end
end

print(isYes and "Yes" or "No")


a = io.read()
f = a:find("7")
print(f and "Yes" or "No")


local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end
local r = 0
local k = io.read("*n")
for i = 1, k do
  for j = 1, k do
    local g = getgcd(i, j)
    for q = 1, k do
      r = r + getgcd(g, q)
    end
  end
end
print(r)

Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

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

200

Sample Output 2

10813692


K=io.read()

function gcd(a,b)
    if b ~= 0 then
        return gcd(b, a % b)
    else
        return a
    end
end

X={}
for i=1,K do
    X[i] = {}
end

for i=1,K do
    for j=1,K do
        X[i][j] = gcd(i,j)
    end
end

sum = 0
for i=1,K do
    for j=1,K do
        for k=1, K do
            sum = sum + gcd(X[i][j],k)
        end
    end
end
print(sum)


K=io.read()

function gcd(a,b)
    if b ~= 0 then
        return gcd(b, a % b)
    else
        return a
    end
end

sum = 0
for i=1,K do
    for j=1,K do
        for k=1,K do
            sum = sum + gcd(gcd(i,j),k)
        end
    end
end
print(sum)


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

local function euc(arg1, arg2)
    --print(arg1, arg2)
    if arg1 == arg2 or arg2 == 0 then
        return arg1
    elseif arg1 == 0 then
        return arg2
    elseif arg1 > arg2 then
        return euc(arg2, arg1 % arg2)
    else
        return euc(arg1, arg2 % arg1)
    end
end

local function gcd(i, j, k)
    return euc(euc(i, j), euc(j, k))
end

local total = 0
for i = 1, K do
    for j = 1, K do
        for k = 1, K do
            total = total + gcd(i, j, k)
        end
    end
end

print(total)


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

local k = io.read("*n")
local ret = 0
for a = 1, k do for b = 1, k do for c = 1, k do
  ret = ret + getgcd(a, getgcd(b, c))
end end end
print(ret)


N=io.read("*n", "*l")
S=io.read()

T={}
T["R"]=0
T["G"]=0
T["B"]=0
for i=1,N do
    T[S:sub(i,i)] = T[S:sub(i,i)] + 1
end

M=T["R"]*T["G"]*T["B"]

for i=1,N do
    x=S:sub(i,i)
    for j=1,(N-i)//2 do
        y=S:sub(i+j,i+j)
        z=S:sub(i+2*j,i+2*j)
        if x~=y and y~=z and z~=x then
            M = M-1
        end
    end
end

print(M)

Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


N=io.read("*n", "*l")
S=io.read()

T={}
for i=1,N do
    if T[S:sub(i,i)] == nil then
        T[S:sub(i,i)] = 1
    else
        T[S:sub(i,i)] = T[S:sub(i,i)] + 1
    end
end

M=T["R"]*T["G"]*T["B"]

for i=1,N do
    x=S:sub(i,i)
    for j=1,(N-i)//2 do
        y=S:sub(i+j,i+j)
        z=S:sub(i+2*j,i+2*j)
        if x~=y and y~=z and z~=x then
            M = M-1
        end
    end
end

print(M)


N=io.read("*n", "*l")
S=io.read()

T={}
for i=1,N do
    if T[S:sub(i,i)] == nil then
        T[S:sub(i,i)] = 1
    else
        T[S:sub(i,i)] = T[S:sub(i,i)] + 1
    end
end

M=T["R"]*T["G"]*T["B"]

for i=1,N do
    x=S:sub(i,i)
    for j=1,(N-i)/2 do
        y=S:sub(i+j,i+j)
        z=S:sub(i+2*j,i+2*j)
        if x~=y and y~=z and z~=x then
            M = M-1
        end
    end
end

print(M)


N=io.read("*n", "*l")
S=io.read()

T={}
for i=1,N do
    if T[S:sub(i,i)] == nil then
        T[S:sub(i,i)] = 1
    else
        T[S:sub(i,i)] = T[S:sub(i,i)] + 1
    end
end

M=T["R"]*T["G"]*T["B"]

for i=1,N do
    x=S:sub(i,i)
    for j=1,N do
        y=S:sub(i+j,i+j)
        z=S:sub(i+2*j,i+2*j)
        if x~=y and y~=z and z~=x then
            M = M-1
        end
    end
end

print(M)


N=io.read("*n", "*l")
S=io.read()

counter = 0

for i=1,N do
    for j=1,N do
        if S:sub(i,i)~=S:sub(j,j) then
            for k=1,N do
                if j-i~=k-j then
                    if S:sub(i,i)~=S:sub(k,k) and S:sub(j,j)~=S:sub(k,k) then
                        counter = counter + 1
                    end
                end
            end
        end
    end
end

print(counter)


N=io.read("*n", "*l")
S=io.read()

counter = 0

for i=1,N do
    for j=i+1,N do
        if S:sub(i,i)~=S:sub(j,j) then
            for k=j+2,N do
                if j-i~=k-j then
                    if S:sub(i,i)~=S:sub(k,k) and S:sub(j,j)~=S:sub(k,k) then
                        counter = counter + 1
                    end
                end
            end
        end
    end
end

print(counter)


N=io.read("*n", "*l")
S=io.read()

counter = 0

for i=1,N do
    for j=1,N do
        if i<j then
            for k=1,N do
                if j<k and j-i~=k-j then
                    if S:sub(i,i)~=S:sub(j,j) and S:sub(i,i)~=S:sub(k,k) and S:sub(j,j)~=S:sub(k,k) then
                        counter = counter + 1
                    end
                end
            end
        end
    end
end

print(counter)


N=io.read"*n"
io.read()
S=io.read()
local function count_char(s,c)
	local ret=0
	for _ in s:gmatch(c)do ret=ret+1 end
	return ret
end
c=count_char(S,"R")*count_char(S,"G")*count_char(S,"B")
for i=1,N do
	local I=S:sub(i,i)
	for d=1,(N-i)/2 do
		local J=S:sub(i+d,i+d)
		local K=S:sub(i+d+d,i+d+d)
		if(I~=J and J~=K and K~=I)then c=c-1 end
	end
end
print(c)


local N = io.read("*n")
io.read("*l")
local S = io.read("*l")

local S_bytes = {S:byte(1, N)}
local R_count, G_count, B_count = 0LL, 0LL, 0LL
local R, G, B = string.byte("RGB", 1, 3)

for i = 1, N do
    if S_bytes[i] == R then
        R_count = R_count + 1
    elseif S_bytes[i] == G then
        G_count = G_count + 1
    else
        B_count = B_count + 1
    end
end

local minus_count = 0LL
for i = 1, math.modf(N/2) do
    for j = 1, N - i * 2 do
        if S_bytes[j] ~= S_bytes[j + i]
        and S_bytes[j + i] ~= S_bytes[j + 2 * i]
        and S_bytes[j] ~= S_bytes[j + 2 * i] then
            minus_count = minus_count + 1
        end
    end
end

print(tostring(R_count * G_count * B_count - minus_count):sub(1, -3))


local N = io.read("*n")
io.read("*l")
local S = io.read("*l")

local S_bytes = {S:byte(1, N)}
local R_count, G_count, B_count = 0LL, 0LL, 0LL
local R, G, B = string.byte("RGB", 1, 3)

for i = 1, N do
    if S_bytes[i] == R then
        R_count = R_count + 1
    elseif S_bytes[i] == G then
        G_count = G_count + 1
    else
        B_count = B_count + 1
    end
end

local minus_count = 0LL
for i = 1, math.modf(N/2) do
    for j = 1, N - i * 2 do
        print(i, j)
        if S_bytes[j] ~= S_bytes[j + i]
        and S_bytes[j + i] ~= S_bytes[j + 2 * i]
        and S_bytes[j] ~= S_bytes[j + 2 * i] then
            minus_count = minus_count + 1
        end
    end
end

print(tostring(R_count * G_count * B_count - minus_count):sub(1, -3))


local N = io.read("*n")
io.read("*l")
local S = io.read("*l")

local S_byte = {S:byte(1, N)}
local count = 0
for i = 1, N - 2 do
    for j = i + 1, N - 1 do
        for k = j + 1, N do
            if j-i ~= k-j and S_byte[i] ~= S_byte[j] and S_byte[i] ~= S_byte[k] and S_byte[j] ~= S_byte[k] then
                count = count + 1
            end
        end
    end
end

print(count)


local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i)
end

local r = 0
local g = 0
local b = 0
local rg, rb, gr, gb, br, bg = 0, 0, 0, 0, 0, 0

local ret = 0
for i = 1, n do
  if t[i] == "R" then
    ret = ret + gb + bg
    gr = gr + g
    br = br + b
    r = r + 1
  elseif t[i] == "G" then
    ret = ret + rb + br
    rg = rg + r
    bg = bg + b
    g = g + 1
  else
    ret = ret + rg + gr
    rb = rb + r
    gb = gb + g
    b = b + 1
  end
end
for mid = 1, n do
  if n < 1 + mid + mid then break end
  for left = 1, n do
    local center, right = left + mid, left + mid + mid
    if n < right then break end
    if t[left] ~= t[center] and t[center] ~= t[right] and t[left] ~= t[right] then
      ret = ret - 1
    end
  end
end
print(ret)


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

Score : 100 points

Problem Statement

We have three boxes A, B, and C, each of which contains an integer.

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.

Then, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


local a, b, c = io.read("*n", "*n", "*n")
a, b = b, a
a, c = c, a
print(tostring(a).." "..tostring(b).." "..tostring(c))


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

local counter=m
for i=n,1,-1 do
    if a[i]>=sum/(4*m) then
        counter=counter-1
    end
end
print(counter<=0 and "Yes" or "No")

Score : 200 points

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes.

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes


n, m = io.read("*n", "*n")
a = {}
asum = 0
for i = 1, n do
  a[i] = io.read("*n")
  asum = asum + a[i]
end
c = 0
for i = 1, n do
  if asum <= a[i] * 4 * m then
    c = c + 1
  end
end
print(m <= c and "Yes" or "No")


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

table.sort(points, function(x, y) return x > y end)

if points[m] >= all_point / (4 * m) then
    print("Yes")
else
    print("No")
end


local n, m = io.read("*n", "*n")
local points = {}
local all_point = 0LL
for i = 1, n do
    points[i] = io.read("*n")
    all_point = all_point + points[i]
end

table.sort(points, function(x, y) return x > y end)

if points[m] > all_point / (4 * m) then
    print("Yes")
else
    print("No")
end


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

table.sort(points, function(x, y) return x > y end)

if points[m] > all_point / (4 * m) then
    print("Yes")
else
    print("No")
end


local N,M=io.read("*n","*n")
local A={}
local sum=0
for i=1,N do
	A[i]=io.read"*n"
	sum=sum+A[i]
end
table.sort(A)
if(A[N-M+1]*4*M<sum)then
	print("No")
else
	print("Yes")
end


local n, m = io.read("*n", "*n")
local a = {}
local sum = 0
for i = 1, n do
  a[i] = io.read("*n")
  sum = sum + a[i]
end
table.sort(a, function(x, y) return x > y end)
print(a[m] * 4 * m < sum and "No" or "Yes")


local x, y = io.read("*n", "*n")
local a, b, c = io.read("*n", "*n", "*n", "*l")
local p = {}
local s = io.read()
for w in s:gmatch("%d+") do
  table.insert(p, tonumber(w))
end
s = io.read()
for w in s:gmatch("%d+") do
  table.insert(p, tonumber(w))
end
s = io.read()
for w in s:gmatch("%d+") do
  table.insert(p, tonumber(w))
end
local t = {}
for i = 1, a + b + c do
  t[i] = i
end
table.sort(t, function(f, g) return p[f] > p[g] end)
local ret = 0
local use_x = 0
local use_y = 0
local total = 0
for i = 1, a + b + c do
  local z = t[i]
  if z <= a then
    if use_x < x then
      use_x = use_x + 1
      total = total + 1
      ret = ret + p[z]
    end
  elseif z <= a + b then
    if use_y < y then
      use_y = use_y + 1
      total = total + 1
      ret = ret + p[z]
    end
  else
    total = total + 1
    ret = ret + p[z]
  end
  if total == x + y then break end
end
print(ret)

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74


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

local max=0
local aounter=0
local bounter=0
local counter=0
for i=a+b+c,1,-1 do
    if aounter<x and t[i][2]=="a" then
        aounter=aounter+1
        max=max+t[i][1]
    end
    if bounter<y and t[i][2]=="b" then
        bounter=bounter+1
        max=max+t[i][1]
    end
    if aounter+bounter+counter<x+y and t[i][2]=="c" then
        counter=counter+1
        max=max+t[i][1]
    end
    if aounter+bounter+counter==x+y or aounter+bounter==x+y then
        break
    end
end
print(string.format("%d",max))


local x,y,a,b,c=io.read("n","n","n","n","n")
local A,B={},{}
local t={}

for i=1,a do
    A[i]=io.read("n")
end
table.sort(A,function(a,b)
    return a>b
end)
for i=1,x do
    table.insert(t,A[i])
end

for i=1,b do
    B[i]=io.read("n")
end
table.sort(B,function(a,b)
    return a>b
end)
for i=1,y do
    table.insert(t,B[i])
end

for i=1,c do
    table.insert(t,io.read("n"))
end
table.sort(t,function(a,b)
    return a>b
end)

local max=0
for i=1,x+y do
    max=max+t[i]
end
print(string.format("%d",max))


local x,y,a,b,c=io.read("n","n","n","n","n")
local A,B={},{}
local t={}

for i=1,a do
    A[i]=io.read("n")
end
table.sort(A,function(a,b)
    return a>b
end)
for i=1,x do
    table.insert(t,A[i])
end

for i=1,b do
    B[i]=io.read("n")
end
table.sort(B,function(a,b)
    return a>b
end)
for i=1,y do
    table.insert(t,B[i])
end

for i=1,c do
    table.insert(t,io.read("n"))
end
table.sort(t,function(a,b)
    return a>b
end)

local max=0
for i=1,x+y do
    max=max+t[i]
end
print(max)


--url:https://gist.github.com/LukeMS/89dc587abd786f92d60886f4977b1953
--[[  Priority Queue implemented in lua, based on a binary heap.
Copyright (C) 2017 Lucas de Morais Siqueira <lucas.morais.siqueira@gmail.com>
License: zlib
  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any damages
  arising from the use of this software.
  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:
  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgement in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
]]--

local floor = math.floor

local PriorityQueue = {}
PriorityQueue.__index = PriorityQueue

setmetatable(
    PriorityQueue,
    {
        __call = function (self)
            setmetatable({}, self)
            self:initialize()
            return self
        end
    }
)

function PriorityQueue:initialize()
    --[[  Initialization.
    Example:
        PriorityQueue = require("priority_queue")
        pq = PriorityQueue()
    ]]--
    self.heap = {}
    self.current_size = 0
end

function PriorityQueue:empty()
    return self.current_size == 0
end

function PriorityQueue:size()
    return self.current_size
end

function PriorityQueue:swim()
    -- Swim up on the tree and fix the order heap property.
    local heap = self.heap
    local floor = floor
    local i = self.current_size

    while floor(i / 2) > 0 do
        local half = floor(i / 2)
        if heap[i][2] < heap[half][2] then
            heap[i], heap[half] = heap[half], heap[i]
        end
        i = half
    end
end

function PriorityQueue:put(v, p)
    --[[ Put an item on the queue.
    Args:
        v: the item to be stored
        p(number): the priority of the item
    ]]--
    --

    self.heap[self.current_size + 1] = {v, p}
    self.current_size = self.current_size + 1
    self:swim()
end

function PriorityQueue:sink()
    -- Sink down on the tree and fix the order heap property.
    local size = self.current_size
    local heap = self.heap
    local i = 1

    while (i * 2) <= size do
        local mc = self:min_child(i)
        if heap[i][2] > heap[mc][2] then
            heap[i], heap[mc] = heap[mc], heap[i]
        end
        i = mc
    end
end

function PriorityQueue:min_child(i)
    if (i * 2) + 1 > self.current_size then
        return i * 2
    else
        if self.heap[i * 2][2] < self.heap[i * 2 + 1][2] then
            return i * 2
        else
            return i * 2 + 1
        end
    end
end

function PriorityQueue:pop()
    -- Remove and return the top priority item
    local heap = self.heap
    local retval = heap[1][1]
    heap[1] = heap[self.current_size]
    heap[self.current_size] = nil
    self.current_size = self.current_size - 1
    self:sink()
    return retval
end

--return PriorityQueue

local x,y,a,b,c=io.read("n","n","n","n","n")
local q=PriorityQueue()
local t={}

for i=1,a do
    local input=io.read("n")
    q:put(input,-input)
end
for i=1,x do
    table.insert(t,q:pop())
end

q:initialize()

for i=1,b do
    local input=io.read("n")
    q:put(input,-input)
end
for i=1,y do
    table.insert(t,q:pop())
end

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

table.sort(t,function(a,b)
    return a>b
end)

local max=0
for i=1,x+y do
    max=max+t[i]
end
print(string.format("%d",max))


--url:https://gist.github.com/LukeMS/89dc587abd786f92d60886f4977b1953
--[[  Priority Queue implemented in lua, based on a binary heap.
Copyright (C) 2017 Lucas de Morais Siqueira <lucas.morais.siqueira@gmail.com>
License: zlib
  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any damages
  arising from the use of this software.
  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:
  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgement in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
]]--

local floor = math.floor

local PriorityQueue = {}
PriorityQueue.__index = PriorityQueue

setmetatable(
    PriorityQueue,
    {
        __call = function (self)
            setmetatable({}, self)
            self:initialize()
            return self
        end
    }
)

function PriorityQueue:initialize()
    --[[  Initialization.
    Example:
        PriorityQueue = require("priority_queue")
        pq = PriorityQueue()
    ]]--
    self.heap = {}
    self.current_size = 0
end

function PriorityQueue:empty()
    return self.current_size == 0
end

function PriorityQueue:size()
    return self.current_size
end

function PriorityQueue:swim()
    -- Swim up on the tree and fix the order heap property.
    local heap = self.heap
    local floor = floor
    local i = self.current_size

    while floor(i / 2) > 0 do
        local half = floor(i / 2)
        if heap[i][2] < heap[half][2] then
            heap[i], heap[half] = heap[half], heap[i]
        end
        i = half
    end
end

function PriorityQueue:put(v, p)
    --[[ Put an item on the queue.
    Args:
        v: the item to be stored
        p(number): the priority of the item
    ]]--
    --

    self.heap[self.current_size + 1] = {v, p}
    self.current_size = self.current_size + 1
    self:swim()
end

function PriorityQueue:sink()
    -- Sink down on the tree and fix the order heap property.
    local size = self.current_size
    local heap = self.heap
    local i = 1

    while (i * 2) <= size do
        local mc = self:min_child(i)
        if heap[i][2] > heap[mc][2] then
            heap[i], heap[mc] = heap[mc], heap[i]
        end
        i = mc
    end
end

function PriorityQueue:min_child(i)
    if (i * 2) + 1 > self.current_size then
        return i * 2
    else
        if self.heap[i * 2][2] < self.heap[i * 2 + 1][2] then
            return i * 2
        else
            return i * 2 + 1
        end
    end
end

function PriorityQueue:pop()
    -- Remove and return the top priority item
    local heap = self.heap
    local retval = heap[1][1]
    heap[1] = heap[self.current_size]
    heap[self.current_size] = nil
    self.current_size = self.current_size - 1
    self:sink()
    return retval
end

--return PriorityQueue

local x,y,a,b,c=io.read("n","n","n","n","n")
local q=PriorityQueue()
local t={}

for i=1,a do
    local input=io.read("n")
    q:put(input,-input)
end
for i=1,x do
    table.insert(t,q:pop())
end

q:initialize()

for i=1,b do
    local input=io.read("n")
    q:put(input,-input)
end
for i=1,y do
    table.insert(t,q:pop())
end

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

table.sort(t,function(a,b)
    return a>b
end)

local max=0
for i=1,x+y do
    max=max+t[i]
end
print(max)


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

local max=0
local aounter=0
local bounter=0
local counter=0
for i=a+b+c,1,-1 do
    if aounter<x and t[i][2]=="a" then
        aounter=aounter+1
        max=max+t[i][1]
    end
    if bounter<y and t[i][2]=="b" then
        bounter=bounter+1
        max=max+t[i][1]
    end
    if aounter+bounter+counter<x+y and t[i][2]=="c" then
        counter=counter+1
        max=max+t[i][1]
    end
    if aounter+bounter+counter==x+y or aounter+bounter==x+y then
        break
    end
end
print(max)


X,Y,A,B,C=io.read("*n","*n","*n","*n","*n")
p,q,r={},{},{}
for i=1,A do p[i]=io.read"*n" end
for i=1,B do q[i]=io.read"*n" end
for i=1,C do r[i]=io.read"*n" end
table.sort(p)
table.sort(q)
table.sort(r)
s={}
for i=1,X do table.insert(s,p[A-i+1]) end
for i=1,Y do table.insert(s,q[B-i+1]) end
for i=1,C do table.insert(s,r[i]) end
table.sort(s)
sum=0
for i=#s,#s-(X+Y)+1,-1 do
	sum=sum+s[i]
end
print(sum)


X,Y,A,B,C=io.read("*n","*n","*n","*n","*n")
p,q,r={},{},{}
for i=1,A do p[i]=io.read"*n" end
for i=1,B do q[i]=io.read"*n" end
for i=1,C do r[i]=io.read"*n" end
table.sort(p)
table.sort(q)
table.sort(r)
sum=0
local function mymax(x,y,z,A,B)
	if(x>y)then
		if(x>=z)then
			return 1,x
		else
			return 3,z
		end
	else
		if(y>=z)then
			return 2,y
		else
			return 3,z
		end
	end
end
while(true)do
	if(X==0)then
		for i=1,Y do
			if(q[B]<(r[C] or 0))then
				sum=sum+r[C]
				C=C-1
			else
				sum=sum+q[B]
				B=B-1
			end
		end
		print(sum)break
	elseif(Y==0)then
		for i=1,X do
			if(p[A]<(r[C] or 0))then
				sum=sum+r[C]
				C=C-1
			else
				sum=sum+p[A]
				A=A-1
			end
		end
		print(sum)break
	end
	local n,v=mymax(p[A],q[B],r[C])
	sum=sum+v
	if(n==1)then
		A=A-1
		X=X-1
	elseif(n==2)then
		B=B-1
		Y=Y-1
	else
		if(p[A]<q[B])then
			X=X-1
		else
			Y=Y-1
		end
		C=C-1
	end
end


local r_eat, g_eat, r_num, g_num, a_num
= io.read("*n", "*n", "*n", "*n", "*n")

function writeList(num)
    local list = {}
    for i = 1, num do
        list[i] = io.read("*n")
    end
    return list
end

local r_taste = writeList(r_num)
local g_taste = writeList(g_num)
local a_taste = writeList(a_num)

table.sort(r_taste, function(x, y) return x > y end)
table.sort(g_taste, function(x, y) return x > y end)
for i = 1, r_num - r_eat do
    r_taste[#r_taste] = nil
end
for i = 1, g_num - g_eat do
    g_taste[#g_taste] = nil
end

local all_taste = a_taste
for i = 1, #r_taste do
    all_taste[#all_taste + 1] = r_taste[i]
end
for i = 1, #g_taste do
    all_taste[#all_taste + 1] = g_taste[i]
end
table.sort(all_taste, function(x, y) return x > y end)

local result = 0LL
for i = 1, r_eat + g_eat do
    result = result + all_taste[i]
end
print(tostring(result):sub(1, -3))