n, h = io.read("*n", "*n")
katana = {}
amax = 0
for i = 1, n do
  a, b = io.read("*n", "*n")
  katana[i] = {}
  katana[i].a = a
  katana[i].b = b
  amax = math.max(amax, a)
end
table.sort(katana, function(x, y)
    return x.b > y.b
  end
)
cnt = 0
for i = 1, n do
  if(amax <= katana[i].b) then
    cnt, h = cnt + 1, h - katana[i].b
    if(h <= 0) then break end
  end
end
if(0 < h) then
  cnt = cnt + math.ceil(h / amax)
end
print(cnt)


N=io.read()
H=io.read()
local number
local a={}
local b={}
for i=1,N do
  a[i]=io.read()
  b[i]=io.read()
end
local amax=0
for i=1,N do
  if a[i]>amax then
    amax=a[i]
  end
end
for i=1,N do
  if b[i]<amax then
    table.remove(a,i)
    table.remove(b,i)
  end
end
local c={}
for i,v in ipairs(b) do
  c[i]=v
end
table.sort(c,function (a,b) return a>b end)
local s=0
for i=1,#c do
  s=s+c[i]
end
local t=0
if s>=H then
  for i=1,#c do
    t=t+c[i]
    if t>=H then
      number=i
    end
    break
  end
else
  number=((H-s)//amax)+1+#c
end
print(number)

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

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9

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.


N=io.read("n")
H=io.read("n")
local number
local a={}
local b={}
for i=1,N do
  a[i]=io.read("n")
  b[i]=io.read("n")
end
local amax=0
for i=1,N do
  if a[i]>amax then
    amax=a[i]
  end
end
for i=1,N do
  if b[i]<amax then
    table.remove(a,i)
    table.remove(b,i)
  end
end
local c={}
for i,v in ipairs(b) do
  c[i]=v
end
table.sort(c,function (a,b) return a>b end)
local s=0
for i=1,#c do
  s=s+c[i]
end
local t=0
if s>=H then
  for i=1,#c do
    t=t+c[i]
    if t>=H then
      number=i
    end
    break
  end
else
  number=((H-s)//amax)+1+#c
end
print(number)


N=io.read("n")
H=io.read("n")
local number
local a={}
local b={}
for i=1,N do
  a[i]=io.read("n")
  b[i]=io.read("n")
end
local amax=0
for i=1,N do
  if a[i]>amax then
    amax=a[i]
  end
end
for i=1,N do
  if b[i]<amax then
    table.remove(a,i)
    table.remove(b,i)
  end
end
local c={}
for i,v in ipairs(b) do
  c[i]=v
end
table.sort(c,function (a,b) return a>b end)
local s=0
for i=1,#c do
  s=s+c[i]
end
local t=0
if s>=H then
  for i=1,#c do
    t=t+c[i]
    if t>=H then
      number=i
    end
    break
  end
else
  number=(H//amax)+1+#c
end
print(number)


N, A, B = io.read("*n", "*n", "*n")
sumsum = 0
for i = 1, N do
    i = "0"..tostring(i)
    sum = 0
    for j = 1, #i do
        sum = sum + tonumber(i:sub(j,j))
    end
    if A <= sum and sum <= B then
        sumsum = sumsum + i
    end
end
print(string.format("%d", sumsum))

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


local function sumDigits(num)
    local sum = 0
    while num > 0 do
        sum = sum + num % 10
        num = math.modf(num / 10)
    end
    return sum
end

local N, A, B = io.read("*n", "*n", "*n")
local total = 0
for i = 1, N do
    local sum = sumDigits(i)
    if sum >= A and sum <= B then
        total = total + i
    end
end
print(total)


local function sumDigits(num)
    local sum = 0
    while num > 0 do
        sum = sum + num % 10
        num = math.modf(num / 10)
    end
    return sum
end

local N, A, B = io.read("*n", "*n", "*n")
local total = 0
for i = 1, N do
    local sum = sumDigits(i)
    if sum >= A and sum <= B then
        total = total + 1
    end
end
print(total)


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

local function getsum(x)
  local r = 0
  while(0 < x) do
    r = r + x % 10
    x = x // 10
  end
  return r
end

local ret = 0
for i = 1, n do
  local z = getsum(i)
  if(a <= z and z <= b) then ret = ret + i end
end
print(ret)


N=io.read("n")
A=io.read("n")
B=io.read("n")
function d(a)
  local s=0
  while (a>0) do
    s=s+a%10
    a=a//10
  end
  return s
end
local total=0
for i=1,N do
  if d(i)>=A and d(i)<=B then
    total=total+i
  end
end
print(total)


local n, a, b = io.read("*n", "*n", "*n")
local r = 0
for i = 1, n do
  local s = 0
  local j = i
  while j > 0 do
    s = s + (j % 10)
    j = j / 10
  end
  if a <= s and s <= b then
    r = r + i
  end
end
return print(r)


local n, a, b = io.read("*n", "*n", "*n")
local r = 0
for i = 1, n do
  local s = 0
  local j = i
  while j > 0 do
    s = s + (j % 10)
    j = j // 10
  end
  if a <= s and s <= b then
    r = r + i
  end
end
return print(r)


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

local count = 0
count = count + ((s:sub(1, 1) == "1") and 1 or 0)
count = count + ((s:sub(2, 2) == "1") and 1 or 0)
count = count + ((s:sub(3, 3) == "1") and 1 or 0)
print(count)

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


local ior = io.read
local str = ior()
local cnt = 0
for i = 1, 3 do
  if(str:sub(i, i) == "1") then cnt = cnt + 1 end
end
print(cnt)


local l = io.read("*l")
local r = 0
for _ in l:gmatch("1") do
  r = r + 1
end
return print(r)


local S_dash=io.read()
local S=""
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag then
        if S=="" then
            S=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
            S=S:gsub("?","a")
        else
            local T=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
            T=T:gsub("?","a")
            if S>T then
                S=T
            end
        end
    end
end

print(#S>0 and S or "UNRESTORABLE")

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    local gsubS=subS:gsub("?","")
    if flag and gsubS~="" then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag and subS:gsub("?","")~="" then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        elseif subS:sub(j,j)=="?" then
            flag=false
        elseif subS:sub(j,j)==T:sub(j,j) then
            flag=true
            break
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        elseif subS:sub(j,j)=="?" then
            flag=false
        elseif subS:sub(j,j)==T:sub(j,j) then
            flag=true
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    else
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("%?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=#S_dash-#T+1,1,-1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        break
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("%?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

local i=1
while i<=#S_dash-#T+1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
        i=i+#T-1
    else
        i=i+1
    end
end

if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("%?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=1,#S_dash-#T+1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("%?","a")
    print(S)
end


local S_dash=io.read()
local S_dash_copy=S_dash
local T=io.read()

for i=1,#S_dash-#T+1 do
    local subS=S_dash:sub(i,#T+i-1)
    local flag=true
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        elseif subS:sub(j,j)==T:sub(j,j) then
            flag=true
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
    end
end
if S_dash==S_dash_copy then
    print("UNRESTORABLE")
else
    S=S_dash:gsub("%?","a")
    print(S)
end


local S_dash=io.read()
local T=io.read()

local flag=true
for i=1,#S_dash-#T+1 do
    local subS=S_dash:sub(i,#T+i-1)
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        elseif subS:sub(j,j)==T:sub(j,j) then
            flag=true
        end
    end
    if flag then
        S_dash=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
    end
end
S=S_dash:gsub("%?","a")
print(flag and S or "UNRESTORABLE")


local S_dash=io.read()
local T=io.read()

local S=""
local flag=true
for i=1,#S_dash-#T+1 do
    local subS=S_dash:sub(i,#T+i-1)
    for j=1,#T do
        if subS:sub(j,j)~="?" and subS:sub(j,j)~=T:sub(j,j) then
            flag=false
        elseif subS:sub(j,j)==T:sub(j,j) then
            flag=true
        end
    end
    if flag then
        S=S_dash:sub(1,i-1)..T..S_dash:sub(#T+i)
    end
end
S=S:gsub("%?","a")
print(flag and S or "UNRESTORABLE")


local S, T = io.read("l","l")
local function match(i)
    local ss = S:sub(i, i+#T-1)
    for k=1,#T do
        local c = ss:sub(k,k)
        local d = T:sub(k,k)
        if c == '?' then
        elseif c == d then
        else
            return false
        end
    end
    return true
end
for i=#S-#T+1,1,-1 do
    if match(i) then
        local t = {}
        for k=1,i-1 do
            local c = S:sub(k,k)
            if c == '?' then
                table.insert(t, 'a')
            else
                table.insert(t, c)
            end
        end
        for k=i,i+#T-1 do
            table.insert(t, T:sub(k-i+1,k-i+1))
        end
        for k=i+#T,#S do
            local c = S:sub(k,k)
            if c == '?' then
                table.insert(t, 'a')
            else
                table.insert(t, c)
            end
        end
        print(table.concat(t, ""))
        return
    end
end
print("UNRESTORABLE")


local S, T = io.read("l","l")
local function match(i)
    local ss = S:sub(i, i+#T-1)
    for k=1,#T do
        local c = ss:sub(k,k)
        local d = T:sub(k,k)
        if c == '?' then
        elseif c == d then
        else
            return false
        end
    end
    return true
end
for i=1,#S-#T+1 do
    if match(i) then
        local t = {}
        for k=1,i-1 do
            local c = S:sub(k,k)
            if c == '?' then
                table.insert(t, 'a')
            else
                table.insert(t, c)
            end
        end
        for k=i,i+#T-1 do
            table.insert(t, T:sub(k-i+1,k-i+1))
        end
        for k=i+#T,#S do
            local c = S:sub(k,k)
            if c == '?' then
                table.insert(t, 'a')
            else
                table.insert(t, c)
            end
        end
        print(table.concat(t, ""))
        return
    end
end
print("UNRESTORABLE")


local a = io.read()
local b = io.read()
local alen, blen = #a, #b
local ret = nil

for i = 1, alen + 1 - blen do
  local accept = true
  for j = i, i + blen - 1 do
    local jb = j - i + 1
    local as, bs = a:sub(j, j), b:sub(jb, jb)
    if as ~= "?" and as ~= bs then
      accept = false break
    end
  end
  if accept then
    local str = ""
    for j = 1, alen do
      local jb = j - i + 1
      if jb < 1 or blen < jb then
        local as = a:sub(j, j)
        if as == "?" then as = "a" end
        str = str .. as
      else
        str = str .. b:sub(jb, jb)
      end
    end
    if not ret then
      ret = str
    elseif str < ret then
      ret = str
    end
  end
end
if not ret then print("UNRESTORABLE")
else print(ret) end


S=io.read()
T=io.read()
local s=#S
local t=#T
local res=false
local q=0
local news
local ans
while s>=t do
    if string.sub(S,s,s)=="?" or string.sub(S,s,s)==string.sub(T,t,t) then
        t=t-1
        if t==0 then
            q=s
            res=true
            news=string.sub(S,1,q-1)..T..string.sub(S,q+#T,#S)
            ans=string.gsub(news,"?","a")
            print(ans)
        end
        if string.sub(S,s-1,s-1)~="?" and string.sub(S,s-1,s-1)~=string.sub(T,t,t) then
      		s=s+#T-t-1
            t=#T
        end
    end
    s=s-1
    if res==true then
        break
    end
end
if res==false then
    print("UNRESTORABLE")
end


S=io.read()
T=io.read()
local s=#S
local t=#T
local res=false
local q=0
local news
local ans
while s>=t do
    if string.sub(S,s,s)=="?" or string.sub(S,s,s)==string.sub(T,t,t) then
        t=t-1
        if t==0 then
            q=s
            res=true
            news=string.sub(S,1,q-1)..T..string.sub(S,q+#T,#S)
            ans=string.gsub(news,"?","a")
            print(ans)
        end
        if string.sub(S,s-1,s-1)~="?" and string.sub(S,s-1,s-1)~=string.sub(T,t,t) then
            t=#T
        end
    end
    s=s-1
    if res==true then
        break
    end
end
if res==false then
    print("UNRESTORABLE")
end


S=io.read()
T=io.read()
local s=#S
local t=#T
local res=false
local q=0
local news
local ans
while s>0 do
    if string.sub(S,s,s)=="?" or string.sub(S,s,s)==string.sub(T,t,t) then
        t=t-1
        if t==0 then
            q=s
            res=true
            news=string.sub(S,1,q-1)..T..string.sub(S,q+#T,#S)
            ans=string.gsub(news,"?","a")
            print(ans)
        end
        if string.sub(S,s-1,s-1)~="?" and string.sub(S,s-1,s-1)~=string.sub(T,t,t) then
            t=#T
        end
    end
    s=s-1
    if res==true then
        break
    end
end
if res==false then
    print("UNRESTORABLE")
end


S=io.read()
T=io.read()
print(S,T)
local s=#S
local t=#T
local res=false
local q=0
local news
local ans
while s>0 do
    if string.sub(S,s,s)=="?" or string.sub(S,s,s)==string.sub(T,t,t) then
        t=t-1
        if t==0 then
            q=s
            res=true
            news=string.sub(S,1,q-1)..T..string.sub(S,q+#T,#S)
            ans=string.gsub(news,"?","a")
            print(ans)
        end
        if string.sub(S,s-1,s-1)~="?" and string.sub(S,s-1,s-1)~=string.sub(T,t,t) then
            t=#T
        end
    end
    s=s-1
    if res==true then
        break
    end
end
if res==false then
    print("UNRESTORABLE")
end


s, t = io.read('*l'), io.read('*l')

ans = nil

for i = 1, #s - #t + 1 do
    local ss = s:sub(1, i - 1) .. t .. s:sub(i + #t)
    local valid = true
    for j = 1, #s do
        if s:byte(j) ~= 63 and s:byte(j) ~= ss:byte(j) then
            valid = false; break
        end
    end
    if valid then
        local t = ss:gsub('?', 'a')
        if ans == nil or ans > t then ans = t end
    end
end

print(ans or 'UNRESTORABLE')


h,w=io.read("*n","*n","*l")
s={}
for i=1,h+2 do
    s[i]={}
    if i~=1 and i~=h+2 then
        x=io.read()
    end
    for j=1,w+2 do
        if i==1 or i==h+2 then
            s[i][j]="."
        else
            if j==1 or j==w+2 then
                s[i][j]="."
            else
                s[i][j]=x:sub(j-1,j-1)
            end
        end
    end
end

for i=2,h+1 do
    for j=2,w+1 do
        if s[i][j]=="." then
            bom_counter=0
            for k=i-1,i+1 do
                for l=j-1,j+1 do
                    if s[k][l]=="#" then
                        bom_counter=bom_counter+1
                    end
                end
            end
            s[i][j]=bom_counter
        end
    end
end

for i=2,h+1 do
    print(table.concat(s[i],"",2,w+1))
end

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


local h, w = io.read("*n", "*n", "*l")
local t = {}
for i = 1, (w + 2) * (h + 2) do t[i] = "." end
for i = 2, h + 1 do
  local s = io.read()
  for j = 2, w + 1 do
    t[(i - 1) * (w + 2) + j] = s:sub(j - 1, j - 1)
  end
end
for i = 2, h + 1 do
  for j = 2, w + 1 do
    local idx = (i - 1) * (w + 2) + j
    if t[idx] == "." then
      local cnt = 0
      local lis = {- w - 3, -w - 2, -w  - 1, -1, 1, w + 1, w + 2, w + 3}
      for k = 1, #lis do
        if t[idx + lis[k]] == "#" then
          cnt = cnt + 1
        end
      end
      io.write(cnt)
    else
      io.write("#")
    end
  end
  io.write("\n")
end


n, m = io.read('n'), io.read('n'), io.read('l')
s = {}
for i = 1, n do s[i] = io.read('l') end
ans = {}
for i = 1, n do ans[i] = {} end

local BAKU = 35
local AKI = 46
local MOV = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}

for i = 1, n do
    for j = 1, m do
        if s[i]:byte(j) == AKI then
            local count = 0
            for k = 1, #MOV do
                local ni, nj = i + MOV[k][1], j + MOV[k][2]
                if ni >= 1 and ni <= n and nj >= 1 and nj <= m and
                    s[ni]:byte(nj) == BAKU
                then
                    count = count + 1
                end
            end
            ans[i][j] = string.char(48 + count)
        else
            ans[i][j] = '#'
        end
    end
end

for i = 1, n do
    for j = 1, m do io.write(ans[i][j]) end
    io.write('\n')
end


n,a=io.read('n'),0 for i=1,n do a=a+1-io.read('n')+io.read('n')end print(a)

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


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

local abc = "abcdefghijklmnopqrstuvwxyz"
local have_abc_s = {}
for i = 1, abc:len()  do
	have_abc_s[abc:byte(i)] = false
end

for i = 1, S:len() do
	have_abc_s[S:byte(i)] = true
end

local out = "None"
for i = 1, abc:len() do
	if not have_abc_s[abc:byte(i)] then
		out = abc:sub(i, i)
		break
	end
end

print(out)

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


S = io.read()
T = {}
for i = 1, 26 do
    T[i] = false
end
for i = 1, #S do
    AtoI = S:sub(i, i):byte() - 96
    T[AtoI] = true
end

flag = true
for i = 1, 26 do
    if not T[i] then
        print(string.char(96+i))
        flag = false
        break
    end
end
if flag then
    print("None")
end


S = io.read()
T = {}
for i = 1, 26 do
    T[i] = false
end
for i = 1, #S do
    AtoI = S:sub(i, i):byte() - 96
    T[AtoI] = true
end
for i = 1, 26 do
    flag = true
    if not T[i] then
        print(string.char(96+i))
        flag = false
        break
    end
    if flag then
        print("None")
    end
end


local s = io.read()
local t = {}
for i = 1, 26 do
  t[i] = false
end
for i = 1, #s do
  local z = s:sub(i, i):byte() - 96
  t[z] = true
end
local f = true
for i = 1, 26 do
  if not t[i] then print(string.char(96 + i)) f = false break end
end
if f then print("None") end


local s = io.read()
local n = #s
local t = {}
for i = 1, n do
  t[i] = s:byte(i, i)
end
local alpha = {}
for i = 1, 26 do alpha[i] = false end
for i = 1, n do
  alpha[t[i] - 96] = true
end
local found = false
for i = 1, 26 do
  if not alpha[i] then
    print(string.char(96 + i))
    found = true
    break
  end
end
if not found then print("None") end


local s = io.read()
local n = #s
local t = {s:byte(1, n)}
local alpha = {}
for i = 1, 26 do alpha[i] = false end
for i = 1, n do
  alpha[t[i] - 96] = true
end
local found = false
for i = 1, 26 do
  if not alpha[i] then
    print(string.char(96 + i))
    found = true
    break
  end
end
if not found then print("None") end


local read = io.read

local n = read("n")

local four_times_count = 0
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		four_times_count = four_times_count + 1
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

local sum_count = 0
if two_times_count == n then
	sum_count = n
elseif two_times_count >= 1 then
	sum_count = four_times_count * 2 + two_times_count
else
	sum_count = four_times_count * 2 + 1
end

print((sum_count >= n) and "Yes" or "No")

Score : 400 points

Problem Statement

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

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

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

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

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

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


local read = io.read

local n = read("n")

local four_times_count = 0
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		four_times_count = four_times_count + 1
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

local sum_count = 0
if two_times_count == n then
	sum_count = n
elseif two_times_count >= 1 then
	sum_count = four_times_count * 3 - 1 + two_times_count
else
	sum_count = four_times_count * 3
end

print((sum_count >= n) and "Yes" or "No")


local read = io.read

local n = read("n")

local four_times_count = 0
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		four_times_count = four_times_count + 1
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

local sum_count = 0
if two_times_count ~= n and two_times_count > 1 then
	sum_count = four_times_count * 3 - 1 - two_times_count
elseif two_times_count > 1 then
	sum_count = n
end

print((sum_count >= n) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count ~= n and two_times_count > 1 then
	n_minus = n_minus - two_times_count + 1
elseif two_times_count > 1 then
	n_minus = 0
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count ~= n and two_times_count > 1 then
	n_minus = n_minus - two_times_count + 1
else
	n_minus = 0
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count ~= 0 then
	n_minus = n_minus - two_times_count + 1
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count == 0 then
elseif two_times_count % 2 == 0 then
	n_minus = n_minus - two_times_count
elseif two_times_count % 2 == 1 then
	n_minus = n_minus - (two_times_count - 1)
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count == 0 then
elseif two_times_count % 2 == 0 then
	n_minus = n_minus + 1 - two_times_count // 2 * 2
elseif two_times_count % 2 == 1 then
	n_minus = n_minus - two_times_count // 2 * 2
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count // 2 > 0 then
	n_minus = n_minus + 1 - two_times_count // 2 * 2
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

if two_times_count > 0 then
	n_minus = n_minus + 1 - two_times_count // 2 * 2
end

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")

local n_minus = n
local two_times_count = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 == 0 then
		n_minus = n_minus - 3
	elseif a_i % 2 == 0 then
		two_times_count = two_times_count + 1
	end
end

n_minus = n_minus - two_times_count // 2 * 2

print((n_minus <= 0) and "Yes" or "No")


local read = io.read

local n = read("n")
local found_4_amount = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 ==0 then
		found_4_amount = found_4_amount + 1
	end
end

if found_4_amount == 0 then
	print("No")
else
	print(((n + found_4_amount - 1) // found_4_amount <= 3) and "Yes" or "No")
end


local read = io.read

local n = read("n")
local found_4_amount = 0
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 ==0 then
		found_4_amount = found_4_amount + 1
	end
end

print((n / found_4_amount <= 3) and "Yes" or "No")


local read = io.read

local n = read("n")
local is_found_4_times = false
for i = 1, n do
	local a_i = read("n")
	if a_i % 4 ==0 then
		is_found_4_times = true
		break
	end
end

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


n=io.read("*n", "*l")
odd=0
four=0
for i=1, n do
    a=io.read("*n")
    if a%2==1 then
        odd=odd+1
    elseif a%4==0 then
        four=four+1
    end
end

if four==odd-1 and four+odd==n then
    print("Yes")
elseif four==odd then
    print("Yes")
elseif four==n then
    print("Yes")
else
    print("No")
end


n=io.read("*n", "*l")
odd=0
four=0
for i=1, n do
    a=io.read("*n")
    if a%2==1 then
        odd=odd+1
    elseif a%4==0 then
        four=four+1
    end
end

if four==odd-1 and four+odd==n then
    print("Yes")
elseif four==odd then
    print("Yes")
else
    print("No")
end


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

if n == 1 and a == b then
	print(1)
	os.exit()
elseif n == 1 then
	print(0)
	os.exit()
elseif a > b then
	print(0)
	os.exit()
end

print((n - 2) * (b - a) + 1)

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


n,a,b=io.read("*n","*n","*n")
max=a+(n-1)*b
min=(n-1)*a+b
if max-min+1>0 then
    print(max-min+1)
else
    print(0)
end


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


local format = string.format
local find = string.find
local reverse = string.reverse
local sub = string.sub
local upper = string.upper
local lower = string.lower
local rep = string.rep
local char = string.char
local byte = string.byte
local gsub = string.gsub
-- gsub(s,"\\","/")
-- formula = "20% * 2"
-- formula = string.gsub(formula,"%%","/100")
-- str = " x x 11 "
-- str = string.gsub(str, "[%s%p]", "")
local gmatch = string.gmatch
-- for token in string.gmatch(arr_temp, "[^%s]+") do
local match = string.match

local concat = table.concat
local insert = table.insert
local remove = table.remove
local sort = table.sort

local abs = math.abs
local min = math.min
local max = math.max
local floor = math.floor
local ceil = math.ceil
local sin = math.sin
local cos = math.cos
local tan = math.tan
local deg = math.deg
local log = math.log
local mod = math.mod
local random = math.random
local sqrt = math.sqrt

local lua_print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local format = string.format
function print_long_str(str)
	local len = #str
	local cur = 1
	--每次最大输出15*1024, 超过就会蹦了
	while cur <= len do
		lua_print('\n'..string.sub(str,cur,cur + 15*1000))
		cur = cur + 15*1000 + 1
	end
	lua_print('')
end

function print_r(root)
	local cache = {[root] = "."}
	local function _dump(t,space,name)
		local temp = {}
		for k,v in pairs(t) do
			local key = tostring(k)
			if cache[v] then
				tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
			elseif type(v) == "table" then
				local new_key = name .. "." .. key
				cache[v] = new_key
				tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
			elseif type(v) == "function" then
				tinsert(temp,"+" .. key .. " [".. tostring(v).."]")
			else
				tinsert(temp,"+" .. key .. " [".. type(v) .. " " .. tostring(v).."]")
			end
		end
		return tconcat(temp,"\n"..space)
	end
	local str = _dump(root, "","")
	print_long_str(str)
end

-- @desc 数据长度
local function length(s)
	local l = 0
	for _ in pairs(s) do
		l = l + 1
	end
	return l
end

-- @desc 数据最小值
local function minn(s)
	local result
	for _, v in pairs(s) do
		if not result then
			result = v
		elseif v < result then
			result = v
		end
	end
	return result
end

-- @desc 数据最大值
local function maxn(s)
	local result
	for _, v in pairs(s) do
		if not result then
			result = v
		elseif v > result then
			result = v
		end
	end
	return result
end

-- @desc 数据和
local function sumn(s)
	local result = 0
	for _, v in pairs(s) do
		result = result + v
	end
	return result
end

-- local str = "fsdfas$123$fgds"
-- @desc split(str,'%$') --注意$是特殊字符 前面要加%！！ 结果返回{"fsdfas","123","fgds"}
local function split(str, sep)
	local ret = {}
	local pos = 0
	while true do
		local pp = string.find(str,sep,pos+1)
		if pp == nil then
			table.insert(ret,string.sub(str,pos+1))
			break
		else
			table.insert(ret,string.sub(str,pos+1,pp-1))
			pos = pp
		end
	end
	return ret
end

local specialSplitStr = "().%+-*?[]^$"
local specialSplitT = {}
for i = 1, #specialSplitStr do
	local c = string.sub(specialSplitStr, i, i)
	specialSplitT[c] = '%' .. c
end
-- @desc 按非特殊字符进行分割，如模式分割直接用split
function newSplit(str, symbol)
	symbol = specialSplitT[symbol] or symbol
	return split(str, symbol)
end

-- @desc 根据角度，返回速度方向矢量
local function getDir(angle)
	local rate = angle / 180 * math.pi
	return {math.cos(rate), math.sin(rate)}
end

function string.utf8charlen(char)
	if not char then
		return 0
	elseif char >= 240 then
		return 4
	elseif char >= 224 then
		return 3
	elseif char >= 192 then
		return 2
	else
		return 1
	end
end

-- 二分法获得table里小于等于value的最大下标
function bisectLeft(t, value)
    local left = 1
    local right = #t
    if t[right] <= value then
        return right
    end
    while left < right do
        local mid = math.floor((left + right) / 2)
        if t[mid] > value then
            right = mid
        else
            left = mid + 1
        end
    end
    return math.max(1, left - 1)
end
-- 二分法获得table里大于等于value的最小下标
function bisectRight(t, value)
    local left = 1
    local right = #t
    while left < right do
        local mid = math.floor((left + right) / 2)
        if t[mid] >= value then
            right = mid
        else
            left = mid + 1
        end
    end
    return right
end

-- ***** VJudge acm
-- ** input
-- a = io.read("*number")
-- local s = io.stdin:read("*l")
-- local k = io.stdin:read("*n", "*l")
-- io.read()
-- io.read("*n")

-- n = io.read("*number", "*l")
-- arr = {}
-- arr_temp = io.read()
-- arr_i = 1;
-- for token in string.gmatch(arr_temp, "[^%s]+") do
--    arr[arr_i] = token
--    arr_i = arr_i + 1
-- end
-- *****

n=io.read("*number")
a=io.read("*number")
b=io.read("*number")
function solve()
	if a > b then
		return 0
	end
	if n == 1 then
		if a == b then
			return 1
		else
			return 0
		end
	end
	return a + (n - 1) * b - ( b + (n - 1) * a ) + 1
end
print(solve())


n=io.read("*number")
a=io.read("*number")
b=io.read("*number")
function solve()
	if a > b then
		return 0
	end
	if n == 1 then
		if a == b then
			return 1
		else
			return 0
		end
	end
	return a + (n - 1) * b - ( b + (n - 1) * a ) + 1
end

print(solve())


local n = io.read("*n")
local a = io.read("*n")
local b = io.read("*n")
local function add(n,a,b)
	if b < a then
		print(0)
		return
	end
	if n == 1 then
		if a ~= b then
			print(0)
			return
		else
			print(1)
			return
		end
	elseif n == 2 then
			print(1)
			return
	end
	local total = a + b
	local min = total + (n - 2) * a
	local max = total + (n-2)*b
	print( max - min + 1)
end

add(n,a,b)


local n = io.read("*n")
local a = io.read("*n")
local b = io.read("*n")
local function add(n,a,b)
	local total = a + b
	local min = total + (n - 2) * a
	local max = total + (n-2)*b
	return max - min + 1
end

print(add(n,a,b))


local n = io.read("*number")
local l = io.read("*number")
local b = io.read("*number")

local sum = 0
if l ~= b and n < 2 then
	print(sum)
 	return sum
end

if l > b then
	print(sum)
	return sum
end

local minx = (n - 1)*l + b
local maxx = (n-1)*b + l

local sum = maxx - minx + 1
print(sum)


local n = io.read("*number")
local l = io.read("*number")
local b = io.read("*number")

if l ~= b and n < 2 then
 	return 0
end

if l > b then
	return 0
end

local minx = (n - 1)*l + b
local maxx = (n-1)*b + l

local sum = maxx - minx + 1
print(sum)


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

 if a > b then
 	print(0)
 end

print((n * b + (a-b)) - (n*a + (b - a)) + 1)


n=io.read("*n")
a=io.read("*n")
b=io.read("*n")
function solve()
	if a > b then
		return 0
	end
	if n == 1 then
		if a == b then
			return 1
		else
			return 0
		end
	end
	return a + (n - 1) * b - ( b + (n - 1) * a ) + 1
end

print(solve())


n=io.read()
a=io.read()
b=io.read()
n = tonumber(n)
a = tonumber(a)
b = tonumber(b)
function solve()
	if a > b then
		return 0
	end
	if n == 1 then
		if a == b then
			return 1
		else
			return 0
		end
	end
	return a + (n - 1) * b - ( b + (n - 1) * a ) + 1
end

print(solve())


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

if a > b then
	return 0
end

return (n * b + (a-b)) - (n*a + (b - a)) + 1


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

if a > b then
	return 0

end

local total = a + b
local min = total + (n - 2) * a
local max = total + (n - 2)*b

return max - min + 1


local n = io.read('*n')
local a = io.read('*n')
local b = io.read('*n')
if b < a then
	print(0)
	return
end
if n == 1 then
	if a ~= b then
		print(0)
		return
	else
		print(1)
		return
	end
elseif n == 2 then
	print(1)
	return
end
print((n-2)*(b-a)+1)


local n = io.read('*n')
local a = io.read('*n')
local b = io.read('*n')
if b < a then
	print(0)
	return
end
if n == 1 then
	if a ~= b then
		print(0)
		return
	else
		print(1)
		return
	end
elseif n == 2 then
	print(1)
	return
end
print((n-2)*(b-a))


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

local inf = 1000000007
local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for v in s:gmatch("%d+") do
  table.insert(t, tonumber(v))
end
local used = {}
for i = 1, n do
  used[i] = false
end
for i = n + 1, 3 * n do
  used[i] = true
end
used[n * 3 + 1] = true
t[n * 3 + 1] = inf
local function merge(a, b)
  if used[a] then return b
  elseif used[b] then return a
  elseif t[a] < t[b] then return a
  else return b
  end
end
local st = SegTree.new(n * 2, merge, n * 3 + 1)
local cursum = 0
for i = 1, n do
  cursum = cursum + t[i]
end
local left = {cursum}
for i = 1, n do
  used[n + i] = false
  st:update(n + i)
  local p = st.stage[1][1]
  cursum = cursum + t[n + i] - t[p]
  used[p] = true
  st:update(p)
  left[1 + i] = cursum
end
-- right
cursum = 0

for i = 1, 2 * n do
  t[i] = -t[i + n]
end
for i = 1, n do
  used[i] = true
end
for i = n + 1, 2 * n do
  cursum = cursum + t[i]
  used[i] = false
end
local right = {cursum}

st:updateAll()

for i = n, 1, -1 do
  used[i] = false
  st:update(i)
  local p = st.stage[1][1]
  cursum = cursum + t[i] - t[p]
  used[p] = true
  st:update(p)
  right[n + 2 - i] = cursum
end

local ret = left[1] + right[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, left[i] + right[n + 2 - i])
end
print(ret)

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


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

local inf = 1000000007
local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for v in s:gmatch("%d+") do
  table.insert(t, tonumber(v))
end
local used = {}
for i = 1, n do
  used[i] = false
end
for i = n + 1, 3 * n do
  used[i] = true
end
used[n * 3 + 1] = true
t[n * 3 + 1] = inf
local function merge(a, b)
  if used[a] then return b
  elseif used[b] then return a
  elseif t[a] < t[b] then return a
  else return b
  end
end
local st = SegTree.new(n * 2, merge, n * 3 + 1)
local cursum = 0
for i = 1, n do
  cursum = cursum + t[i]
end
local left = {cursum}
for i = 1, n do
  used[n + i] = false
  st:update(n + i)
  local p = st.stage[1][1]
  cursum = cursum + t[n + i] - t[p]
  used[p] = true
  st:update(p)
  left[1 + i] = cursum
end
-- right
for i = 1, 2 * n do
  used[i] = true
end
cursum = 0
for i = 2 * n + 1, 3 * n do
  cursum = cursum + t[i]
  used[i] = false
end
local right = {cursum}

local function merge2(a, b)
  if used[a] then return b
  elseif used[b] then return a
  elseif t[b] < t[a] then return a
  else return b
  end
end
st.func = merge2

for i = 1, 2 * n do
  st:setValue(i, i + n, true)
end
st:updateAll()

for i = n, 1, -1 do
  used[n + i] = false
  st:update(n + i)
  local p = st.stage[1][1]
  cursum = cursum + t[n + i] - t[p]
  used[p] = true
  st:update(p - n)
  right[n + 2 - i] = cursum
end

local ret = left[1] - right[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, left[i] - right[n + 2 - i])
end
print(ret)


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  if not i then i = #self.v + 1
  else table.remove(self.box)
  end
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalc = function(self, i)
  local kl, kr = self.l[i], self.r[i]
  if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
  else self.lc[i] = 0
  end
  if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
  else self.rc[i] = 0
  end
end
AvlTree.recalcAll = function(self, i)
  while 1 < i do
    self:recalc(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, parent)
  local granp, child = self.p[parent], self.l[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
end

AvlTree.rotL = function(self, parent)
  local granp, child = self.p[parent], self.r[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
end

AvlTree.rotLR = function(self, lparent, rparent)
  local sp, sl, sr = self.p, self.l, self.r
  local granp, d = sp[rparent], sr[lparent]
  sp[lparent], sr[lparent] = d, sl[d]
  sp[rparent], sl[rparent] = d, sr[d]
  sp[sl[d]], sp[sr[d]] = lparent, rparent
  sp[d], sl[d], sr[d] = granp, lparent, rparent
  if 1 < granp then
    if sr[granp] == rparent then sr[granp] = d
    else sl[granp] = d
    end
  else self.root = d
  end
end

AvlTree.rotRL = function(self, rparent, lparent)
  local sp, sl, sr = self.p, self.l, self.r
  local granp, d = sp[lparent], sl[rparent]
  sp[rparent], sl[rparent] = d, sr[d]
  sp[lparent], sr[lparent] = d, sl[d]
  sp[sr[d]], sp[sl[d]] = rparent, lparent
  sp[d], sr[d], sl[d] = granp, rparent, lparent
  if 1 < granp then
    if sl[granp] == lparent then sl[granp] = d
    else sr[granp] = d
    end
  else self.root = d
  end
end

AvlTree.push = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalc(parent)
    local lcp_m_rcp = self.lc[parent] - self.rc[parent]
    if lcp_m_rcp % 2 ~= 0 then -- 1 or -1
      pos = parent
    elseif lcp_m_rcp == 2 then
      if self.lc[child] - 1 == self.rc[child] then
        self:rotR(parent)
        self:recalcAll(parent)
      else
        self:rotLR(child, parent)
        self:recalc(child)
        self:recalcAll(parent)
      end
      break
    elseif lcp_m_rcp == -2 then
      if self.rc[child] - 1 == self.lc[child] then
        self:rotL(parent)
        self:recalcAll(parent)
      else
        self:rotRL(child, parent)
        self:recalc(child)
        self:recalcAll(parent)
      end
      break
    else
      break
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalc(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcAll(self.p[node])
      break
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(node)
        self:recalcAll(node)
        break
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(node)
        self:recalc(node)
        node = nr
      else
        local nr = self.r[node]
        local nrl = self.l[nr]
        self:rotRL(nr, node)
        self:recalc(nr)
        self:recalc(node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
  local obj = {}
  setmetatable(obj, {__index = AvlTree})
  obj:create(lessthan, n)
  return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:push(a[i])
end
for i = 1, n do
  avleft:push(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:push(a[i + 2 * n])
end
for i = 1, n do
  avright:push(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  table.remove(self.box)
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalcCount = function(self, i)
  if 1 < i then
    local kl, kr = self.l[i], self.r[i]
    if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
    else self.lc[i] = 0
    end
    if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
    else self.rc[i] = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    local lcp_m_rcp = self.lc[parent] - self.rc[parent]
    if lcp_m_rcp % 2 ~= 0 then -- 1 or -1
      pos = parent
    elseif lcp_m_rcp == 2 then
      if self.lc[child] - 1 == self.rc[child] then
        self:rotR(child, parent)
      else
        local cr = self.r[child]
        self:rotL(cr, child)
        self:rotR(cr, parent)
      end
      pos = 1
    elseif lcp_m_rcp == -2 then
      if self.rc[child] - 1 == self.lc[child] then
        self:rotL(child, parent)
      else
        local cl = self.l[child]
        self:rotR(cl, child)
        self:rotL(cl, parent)
      end
      pos = 1
    else
      -- self:recalcCountAll(child)
      pos = 1
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
  local obj = {}
  setmetatable(obj, {__index = AvlTree})
  obj:create(lessthan, n)
  return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  table.remove(self.box)
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalcCount = function(self, i)
  if 1 < i then
    local kl, kr = self.l[i], self.r[i]
    if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
    else self.lc[i] = 0
    end
    if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
    else self.rc[i] = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  table.remove(self.box)
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalcCount = function(self, i)
  if 1 < i then
    local kl, kr = self.l[i], self.r[i]
    if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
    else self.lc[i] = 0
    end
    if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
    else self.rc[i] = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


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

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  table.remove(self.box)
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalcCount = function(self, i)
  if 1 < i then
    if 1 < self.l[i] then self.lc[i] = 1 + mma(self.lc[self.l[i]], self.rc[self.l[i]])
    else self.lc[i] = 0
    end
    if 1 < self.r[i] then self.rc[i] = 1 + mma(self.lc[self.r[i]], self.rc[self.r[i]])
    else self.rc[i] = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        added = true
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        added = true
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  if 1 < self.p[node] then
    self.p[self.r[node]] = self.p[node]
    self.l[self.p[node]] = self.r[node]
    self:rmsub(self.p[node])
  else
    self.p[self.r[node]] = 1
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mma = math.max
local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegForAvl = {}
SegForAvl.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.stage[i + 1][j * 2]
    end
  end
end
SegForAvl.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  self.stage[stagenum][1] = false
  for i = 2, mul do self.stage[stagenum][i] = i end
  self:updateAll()
end
SegForAvl.hold = function(self)
  local idx = self.stage[1][1]
  local ridx = idx
  self.stage[self.stagenum][idx] = false
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
  return ridx
end
SegForAvl.release = function(self, idx)
  self.stage[self.stagenum][idx] = idx
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
end
SegForAvl.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegForAvl})
  obj:create(n)
  return obj
end

local SegAvlTree = {}
SegAvlTree.makenode = function(self, val, parent)
  local i = self.seg:hold()
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
SegAvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.seg = SegForAvl.new(n + 1)
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

SegAvlTree.recalcCount = function(self, i)
  if 1 < i then
    if 1 < self.l[i] then self.lc[i] = 1 + mma(self.lc[self.l[i]], self.rc[self.l[i]])
    else self.lc[i] = 0
    end
    if 1 < self.r[i] then self.rc[i] = 1 + mma(self.lc[self.r[i]], self.rc[self.r[i]])
    else self.rc[i] = 0
    end
  end
end
SegAvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

SegAvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        added = true
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        added = true
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        pos = 1
      else
        self:recalcCountAll(child)
        pos = 1
      end
    end
  end
end

SegAvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

SegAvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  if 1 < self.p[node] then
    self.p[self.r[node]] = self.p[node]
    self.l[self.p[node]] = self.r[node]
    self:rmsub(self.p[node])
  else
    self.p[self.r[node]] = 1
    self.root = self.r[node]
  end
  self.seg:release(node)
  return v
end

SegAvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = SegAvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = SegAvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = SegAvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)


local mma = math.max
local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegForAvl = {}
SegForAvl.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.stage[i + 1][j * 2 - 1]
    end
  end
end
SegForAvl.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = i end
  self:updateAll()
end
SegForAvl.hold = function(self)
  local idx = self.stage[1][1]
  local ridx = idx
  self.stage[self.stagenum][idx] = false
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
  return ridx
end
SegForAvl.release = function(self, idx)
  self.stage[self.stagenum][idx] = idx
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
end
SegForAvl.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegForAvl})
  obj:create(n)
  return obj
end

local SegAvlTree = {}
SegAvlTree.makenode = function(self, val, parent)
  local i = self.seg:hold()
  self.v[i], self.u[i], self.p[i] = val, true, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  return i
end
SegAvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.seg = SegForAvl.new(n)
  -- value, leftCount, rightCount, left, right, parent, used
  self.v, self.lc, self.rc, self.l, self.r, self.p, self.u = {}, {}, {}, {}, {}, {}, {}
  for i = 1, n do
    self.v[i], self.u[i], self.p[i] = 0, false, 0
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 0, 0
  end
end

SegAvlTree.recalcCount = function(self, i)
  if self.u[i] then
    if self.u[self.l[i]] then self.lc[i] = 1 + mma(self.lc[self.l[i]], self.rc[self.l[i]])
    else self.lc[i] = 0
    end
    if self.u[self.r[i]] then self.rc[i] = 1 + mma(self.lc[self.r[i]], self.rc[self.r[i]])
    else self.rc[i] = 0
    end
  end
end
SegAvlTree.recalcCountAll = function(self, i)
  while self.u[i] do
    self:recalcCount(i)
    i = self.p[i]
  end
end

SegAvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.l[parent]] then self.p[self.l[parent]] = parent end
  if self.u[granp] then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  if self.u[self.r[parent]] then self.p[self.r[parent]] = parent end
  if self.u[granp] then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

SegAvlTree.add = function(self, val)
  if not self.u[self.root] then self.root = self:makenode(val, 0) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, self.v[pos]) then
      if self.u[self.l[pos]] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        added = true
      end
    else
      if self.u[self.r[pos]] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        added = true
      end
    end
  end
  while self.u[pos] do
    local child, parent = pos, self.p[pos]
    if not self.u[parent] then
      break
    end
    self:recalcCount(parent)
    if self.l[parent] == child then
      if self.lc[parent] - 1 == self.rc[parent] then
        pos = parent
      elseif self.lc[parent] - 2 == self.rc[parent] then
        self:recalcCount(child)
        if self.lc[child] - 1 == self.rc[child] then
          self:rotR(child, parent)
        else
          local cr = self.r[child]
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    else -- parent.r == child
      if self.rc[parent] - 1 == self.lc[parent] then
        pos = parent
      elseif self.rc[parent] - 2 == self.lc[parent] then
        self:recalcCount(child)
        if self.rc[child] - 1 == self.lc[child] then
          self:rotL(child, parent)
        else
          local cl = self.l[child]
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
        pos = nil
      else
        self:recalcCountAll(child)
        pos = nil
      end
    end
  end
end

SegAvlTree.rmsub = function(self, node)
  while self.u[node] do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = nil
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = nil
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

SegAvlTree.pop = function(self)
  local node = self.root
  while self.u[self.l[node]] do
    node = self.l[node]
  end
  local v = self.v[node]
  if self.u[self.p[node]] then
    if self.u[self.r[node]] then self.p[self.r[node]] = self.p[node] end
    self.l[self.p[node]] = self.r[node]
    self:rmsub(self.p[node])
  else
    if self.r[node] then self.p[self.r[node]] = 0 end
    self.root = self.r[node]
  end
  self.seg:release(node)
  return v
end

SegAvlTree.new = function(lessthan, n)
    local obj = {}
    setmetatable(obj, {__index = SegAvlTree})
    obj:create(lessthan, n)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = SegAvlTree.new(function(x, y) return x < y end, n + 1)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = SegAvlTree.new(function(x, y) return x > y end, n + 1)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
end
local ret = leftsum[1] - rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] - rightsum[n + 2 - i])
end
print(ret)