Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        s = s[::-1]
        cnt += 1
    else:
        if li[1] == '1':
            head.append(li[2])
        else:
            tail.append(li[2])

ans = head[::-1] + s + tail
print(*ans , sep='')

### FIXED CODE ###

s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        cnt += 1
    else:
        if li[1] == '1':
            head += li[2]
        else:
            tail += li[2]

if cnt % 2 != 0:
    s = s[::-1]
    
ans = head[::-1] + s + tail
print(*ans , sep='')
### BUGGY CODE ###

k,s=map(int,input().split())
print([x+y+z for x in range(k+1) for y in range(k+1) for z in range(k+1)].count(s))


### FIXED CODE ###

k,s=map(int,input().split())
print(len([1 for y in range(k+1) for z in range(k+1) if 0<=s-y-z<=k]))
### BUGGY CODE ###

from collections import deque
H, W = map(int, input().split())
a = [list(input()) for _ in range(H)]
q = deque()
inf_ = float("inf")
grid = [[inf_] * W for _ in range(H)]
dire = ((1,0),(0,1),(-1,0),(0,-1))
d = 0
 
for i in range(H):
    for j in range(W):
        if a[i][j] == "#":
            q.append([i,j])
            grid[i][j] = 0
 
while q:
    i, j = q.popleft()
    d = max(d,grid[i][j]) 
    for dy, dx in dire:
        ny,nx = i+dy, j+dx
        if ny>=0 and ny<H and nx>=0 and nx<W and grid[ny][nx] > grid[i][j]:
            q.append([ny, nx])
            grid[ny][nx] = grid[i][j] + 1
 
print(d)

### FIXED CODE ###

from collections import deque
H, W = map(int, input().split())
A = [input() for _ in range(H)]
q = deque([])
grid = [[-1] * W for _ in range(H)]
dire = ((1,0),(0,1),(-1,0),(0,-1))
d = 0
 
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            q.append((i,j))
            grid[i][j] = 0
 
while q:
    i, j = q.popleft()
    d = grid[i][j]
    for dy, dx in dire:
        ny,nx = i+dy, j+dx
        if ny>=0 and ny<H and nx>=0 and nx<W and grid[ny][nx] < 0:
            q.append((ny, nx))
            grid[ny][nx] = grid[i][j] + 1
 
print(d)
### BUGGY CODE ###

from collections import deque
import sys

H,W = map(int,input().split())
maze = [list(input())for i in range(H)]
que = deque([])

"""
append corrdinates of blacktiles to que and change the tile to int
"""
for i in range(H):
  for j in range(W):
    if maze[i][j] == "#":
      que.append([i,j])
      maze[i][j]=0      
"""
with BFS, change the white tile to int which represents from the black tile
"""
patterns = [[1,0],[-1,0],[0,1],[0,-1]]
while que:
  now = que.popleft()
  nowY = now[0]
  nowX = now[1]
  nowStep = maze[nowY][nowX]
  for pattern in patterns:
    nextY = nowY + pattern[0]
    nextX = nowX + pattern[1]
    nextStep = nowStep + 1
    if 0 <= nextY < H and 0 <= nextX < W and maze[nextY][nextX]==".":
      maze[nextY][nextX]=nextStep
      que.append([nextY,nextX])
      
print(nowStep)

### FIXED CODE ###

import numpy as np
h,w=map(int,input().split())
maze = []
for i in range(h):
  maze.append([0 if x=="#" else float('INF') for x in list(input())])
maze = np.array(maze)

for i in range(1,h):
  maze[i,:] = np.minimum(maze[i,:],maze[i-1,:]+1)
for j in range(h-2,-1,-1):
  maze[j,:] = np.minimum(maze[j,:],maze[j+1,:]+1)
for k in range(1,w):
  maze[:,k] = np.minimum(maze[:,k],maze[:,k-1]+1)
for l in range(w-2,-1,-1):
  maze[:,l] = np.minimum(maze[:,l],maze[:,l+1]+1)


print(int(np.max(maze)))
### BUGGY CODE ###

MOD = 1000000007

S = input()

a = 0
ab = 0
abc = 0
all_s = 1

for i, s in enumerate(S):
    if s == "A":
        a += all_s
    elif s == "B":
        ab += a
    elif s == "C":
        abc += ab
    elif s == "?":
        abc = abc * 3 + ab
        ab = ab * 3 + a
        a = a * 3 + all_s
        all_s *= 3

print(abc % MOD)


### FIXED CODE ###

MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a = (a + all_s) % MOD
    elif s == "B":
        ab = (ab + a) % MOD
    elif s == "C":
        abc = (abc + ab) % MOD
    elif s == "?":
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + all_s) % MOD
        all_s = (all_s * 3) % MOD

print(abc)
### BUGGY CODE ###

from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd
import sys
#input = sys.stdin.readline

import copy

N,M=map(int,input().split())
UV=[list(map(int,input().split())) for i in range(M)]
S,T=map(int,input().split())

data=[[] for i in range(N+1)]
for u,v in UV:
    data[u].append(v)

visited={S}
visited1=set()
visited2=set()
stack=deque([[S,0]])
while stack:
    a,m=stack.popleft()
    for p in data[a]:
        z=(m+1)

        if z%3==0:
            if p in visited:
                continue
            elif p == T:
                print(z//3)
                exit()
        if z%3==1:
            if p in visited1:
                continue
            else:
                visited1.add(p)
        if z%3==2:
            if p in visited2:
                continue
            else:
                visited1.add(2)
        stack.append([p,z])

print(-1)


### FIXED CODE ###

from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd
import sys
#input = sys.stdin.readline

import copy

N,M=map(int,input().split())
UV=[list(map(int,input().split())) for i in range(M)]
S,T=map(int,input().split())

data=[[] for i in range(N+1)]
for u,v in UV:
    data[u].append(v)

visited={S}
visited1=set()
visited2=set()
stack=deque([[S,0]])
while stack:
    a,m=stack.popleft()
    for p in data[a]:
        z=(m+1)

        if z%3==0:
            if p in visited:
                continue
            elif p == T:
                print(z//3)
                exit()
            else:
                visited.add(p)
        if z%3==1:
            if p in visited1:
                continue
            else:
                visited1.add(p)
        if z%3==2:
            if p in visited2:
                continue
            else:
                visited2.add(p)
        stack.append([p,z])

print(-1)
### BUGGY CODE ###

lst = list(map(int, input().split()))
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
c_lst = list(map(int, input().split()))

k = lst[3]
candidate = []
for n, a in enumerate(sorted(a_lst, key = lambda x: -x)):
    for m, b in enumerate(sorted(b_lst, key = lambda x: -x)):
        for l, c in enumerate(sorted(c_lst, key = lambda x: -x)):
            if (n+1)*(m+1)*(l+1) <= k:
                candidate.append(a+b+c)
for i in sorted(candidate, key = lambda x: -x)[:k]:
    print(i)

### FIXED CODE ###

x,y,z,k = map(int, input().split())
a_lst = sorted(list(map(int, input().split())), reverse=True)
b_lst = sorted(list(map(int, input().split())), reverse=True)
c_lst = sorted(list(map(int, input().split())), reverse=True)

candidate = []
for n in range(x):
    for m in range(y):
        if (n+1)*(m+1) > k:
            break
        else:
            for l in range(z):
                if (n+1)*(m+1)*(l+1) > k:
                    break
                else:
                    candidate.append(a_lst[n]+b_lst[m]+c_lst[l])
for i in sorted(candidate, reverse=True)[:k]:
    print(i)
### BUGGY CODE ###

N = int(input())
S = list(input())
ans = 0
for i in range(1000):
  if i<10:
    cnt0= 0
    cnt1 = 0
    for j in range(N):
      if S[j]=="%d"%i and 2<=cnt0 :
        ans+=1
        break
      if S[j]=="0":
        cnt0+=1
        continue
  elif 10<=i<=99:
    cnt0=0
    cnt1=0
    cnt2=0
    i = list(str(i))
    for j in range(N):
      if S[j]=="%s"%i[1] and 1<=cnt0 and 1<=cnt1:
        ans+=1
        break
      if S[j]=="0" and cnt0!=1:
        cnt0+=1
        continue
      if S[j]=="%s"%i[0] and 1<=cnt0:
        cnt1 +=1
        continue
  elif 100<=i:
    cnt0=0
    cnt1=0
    cnt2=0
    i = list(str(i))
    for j in range(N):
      if S[j]=="%s"%i[2] and 1<=cnt0 and 1<=cnt1:
        ans+=1
        #print(i)
        break
      if S[j]=="%s"%i[0] and cnt0!=1:
        cnt0+=1
        continue
      if S[j]=="%s"%i[1] and 1<=cnt0:
        cnt1 +=1
        continue
print(ans)
      
      
      


### FIXED CODE ###

def main():
  N = int(input())
  S = tuple(input())
  ans = 0
  for i in range(1000):
    if i<10:
      cnt0= 0
      cnt1 = 0
      for j in range(N):
        if 2<=cnt0 and S[j]=="%d"%i:
          ans+=1
          break
        if S[j]=="0":
          cnt0+=1
          continue
    elif 10<=i<=99:
      cnt0=0
      cnt1=0
      cnt2=0
      i = list(str(i))
      for j in range(N):
        if 1<=cnt0 and 1<=cnt1 and S[j]=="%s"%i[1]:
          ans+=1
          break
        if S[j]=="0" and cnt0!=1:
          cnt0+=1
          continue
        if 1<=cnt0 and S[j]=="%s"%i[0]:
          cnt1 +=1
          continue
    elif 100<=i:
      cnt0=0
      cnt1=0
      cnt2=0
      i = list(str(i))
      for j in range(N):
        if 1<=cnt0 and 1<=cnt1 and S[j]=="%s"%i[2]:
          ans+=1
          #print(i)
          break
        if S[j]=="%s"%i[0] and cnt0!=1:
          cnt0+=1
          continue
        if 1<=cnt0 and S[j]=="%s"%i[1]:
          cnt1 +=1
          continue
  print(ans)
main()
### BUGGY CODE ###

def main():
    n = [int(i) for i in input().split()]
    bridge = set()
    counter = 0
    reqs = [[int(j) for j in input().split()] for i in range(n[1])]
    reqs.sort(key=lambda x:(x[0],x[1]))
    
    for i in range(1, n[0]+1):
        if i in bridge:
            bridge.clear()
            counter += 1
        for j in range(len(reqs)):
            if reqs[j][0] == i:
                bridge.add(reqs[j][1])
            else:
                reqs = reqs[j:]
                break
    
    print(counter)
 
main()

### FIXED CODE ###

def main():
    n = [int(i) for i in input().split()]
    bridge = set()
    counter = 0
    reqs = [[int(j) for j in input().split()] for i in range(n[1])]
    reqs.sort(key=lambda x:(x[0],x[1]))
    prefix = 0
    
    for i in range(1, n[0]+1):
        if i in bridge:
            bridge.clear()
            counter += 1
        for j in range(prefix, len(reqs)):
            if reqs[j][0] == i:
                bridge.add(reqs[j][1])
            else:
                prefix = j
                break
    
    print(counter)
 
main()
### BUGGY CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
memo = 10 **10
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        if count > N:
            if c >= memo:
                cb += [c] * b
        else:
            count += b
            cb += [c] * b
            memo = min(memo, c)

ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1
### BUGGY CODE ###

b = 10**9+7
L = int(input(),2)
K = 2**100000
ans = 0
t = 0
c = 100001
for p in [None]*100001:
	c -= 1
	if K == (L & K):
		ans += (2**t)*(3**c)%b
		t += 1
	K //= 2
print((ans+2**t)%b)

### FIXED CODE ###

L = input()
length = len(L)
a = 2
b = 1
c = 4
for l in L[1:]:
	if l  == '1':
		a = (a * 3) % 1000000007
		b = (b * 3) % 1000000007
		c = (c * 2) % 1000000007
	else:
		a = (a * 3 - c) % 1000000007
		b = (b * 3) % 1000000007
print((a + b)%1000000007)
### BUGGY CODE ###

n,k=map(int,input().split())
h=list(map(int,input().split()))
 
dp=[int(10e8)]*n
dp[0]=0
for i in range(1,n):
    for j in range(min(i,k)):
        dp[i]=min(dp[i],dp[i-j-1]+abs(h[i]-h[i-j-1]))
 
print(dp[n-1])

### FIXED CODE ###

n,k=map(int,input().split())
h=list(map(int,input().split()))

dp=[int(10e8)]*n
dp[0]=0
for i in range(1,n):
    for j in range(min(i,k)):
        dp[i]=min(dp[i],dp[i-j-1]+abs(h[i]-h[i-j-1]))

print(dp[n-1])
### BUGGY CODE ###

def rec(i, j):
    if dp[i][j]:
        return dp[i][j]
    if i == n:
        res = 0
    elif j < w[i]:
        res = rec(i + 1, j)
    else:
        res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i])
    dp[i][j] = res
    return res
n, W = map(int, input().split())
w, v = [], []
for i in range(n):
    ww, vv = map(int, input().split())
    w.append(ww)
    v.append(vv)
dp = [[0] * (W + 1) for i in range(n + 1)]
print(rec(0, W))

### FIXED CODE ###

n, W = map(int, input().split())
w, v = [], []
for i in range(n):
    w_, v_ = map(int, input().split())
    w.append(w_)
    v.append(v_)
dp = [[0] * (W + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, W + 1):
        if j < w[i - 1]:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1])
print(dp[n][W])
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

dic = {a:0 for a in A}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  if C not in dic.keys():
    dic[C] = 0
  if B in dic.keys():
    dic[C] += dic[B]
    dic[B] = 0
  
  ans = 0
  for k, v in dic.items():
    ans += k*v
  
  print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

ans = sum(A)
dic = {a+1:0 for a in range(10**5)}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  ans += dic[B]*(C-B)
  dic[C] += dic[B]
  dic[B] = 0
    
  print(ans)
### BUGGY CODE ###


n, q = map(int, input().split())

edge = [[] for _ in range(n)]

counter = [0] * n

for _ in range(n-1):
    a, b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

for _ in range(q):
    a, b = map(int, input().split())
    counter[a-1] += b

parent = [0]
visited = []
for i in parent:
    visited.append(i)
    for v in edge[i]:
        if v in visited:
            continue
        parent.append(v)
        counter[v] += counter[i]
        
for i in range(n):
    print(counter[i], end=" ")
print("")

### FIXED CODE ###


# n, q = map(int, input().split())

# edge = [[] for _ in range(n)]

# counter = [0] * n

# for _ in range(n-1):
#     a, b = map(int, input().split())
#     edge[a-1].append(b-1)
#     edge[b-1].append(a-1)

# for _ in range(q):
#     a, b = map(int, input().split())
#     counter[a-1] += b

# parent = [0]
# visited = []

# for i in parent:
#     visited.append(i)
#     for v in edge[i]:
#         if v in visited:
#             continue
#         parent.append(v)
#         counter[v] += counter[i]
        
# for i in range(n):
#     print(counter[i], end=" ")
# print("")

import sys
input = sys.stdin.readline
 
N, Q = [int(x) for x in input().split()]
 
edge = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    edge[a - 1].append(b - 1)
    edge[b - 1].append(a - 1)
 
cost = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    cost[p - 1] += x
 
next = [0]
s_next = {0}
for i in next:
    for v in edge[i]:
        if v in s_next:
            continue
        s_next.add(v)
        next.append(v)
        cost[v] += cost[i]
 
print(" ".join(map(str, cost)))
### BUGGY CODE ###

M = 10 ** 9 + 7
s = input().strip()

num = 0
q_list = []
d = 1
l = len(s)
for i in range(l - 1, -1, -1):
	c = s[i]
	if c != "?":
		num = (num + int(c) * d) % 13
	else:
		q_list.append(d)
	d *= 10

counts = [0] * 13
counts[0] = 1
for d in q_list:
	new_counts = [0] * 13
	for n in range(10):
		r = (d * n) % 13
		for m in range(13):
			k = (r + m) % 13
			new_counts[k] = (new_counts[k] + counts[m]) % M
	counts = new_counts

print(counts[(13 + 5 - num) % 13])


### FIXED CODE ###


M = 10 ** 9 + 7
s = raw_input().strip()

num = 0
q_list = []
d = 1
for i in range(len(s) - 1, -1, -1):
	c = s[i]
	if c != "?":
		num = (num + int(c) * d) % 13
	else:
		q_list.append(d)
	d = (d * 10) % 13

counts = [0] * 13
counts[0] = 1
counts_save = [0] * 13
for d in q_list:
	for i in range(13):
		counts_save[i] = counts[i] % M
	for n in range(1, 10):
		r = (d * n) % 13
		for m in range(13):
			counts[(r + m) % 13] += counts_save[m]

print(counts[(13 + 5 - num) % 13] % M)
### BUGGY CODE ###

def isP(n):
	for i in prime:
		if i >= n:
			break
		if n%i == 0:
			return False
	return True
		
r = int(10000001**0.5)
s = int(r**0.5)
p = [1]*r
p[0] = p[1] = 0
for i in range(2,s):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]
prime = []
for i in range(r):
	if p[i]:
		prime.append(i)

while 1:
	n = input()
	if n == 0: break
	for i in range(n-(1-n%2),10,-2):
		if isP(i) and isP(i-2) and isP(i-6) and isP(i-8):
			print i
			break

### FIXED CODE ###

ref = [[[10,8],[4,4]],[[10,2],[4,2]]]
r =10000001
p = [1]*r
p[0]= p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0]*len(p[2*i::i])
		
while 1:
	try:
		n = input()
		if n == 0: break
		n -= 1-n%2
		while any(not p[n-i] for i in [0,2,6,8]):
			n -= ref[p[n-2]][p[n-6]][p[n-8]]
		print n
	except:
		pass
### BUGGY CODE ###

s = input()
k = int(input())
l = []
for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        l.append(s[i:j])
print(sorted(list(set(l)))[k-1])

### FIXED CODE ###

s = input()
k = int(input())
l = []
for i in range(len(s)):
    for j in range(i+1,min(i+k+1, len(s)+1)):
        l.append(s[i:j])
print(sorted(list(set(l)))[k-1])
### BUGGY CODE ###

# coding: utf-8
import math
 
 
def calc():
    n = int(input())
 
    tops = list(map(int, input().split()))
    middles = list(map(int, input().split()))
    lowers = list(map(int, input().split()))
 
    lowers = sorted(lowers, reverse=True)
 
    max_lower = max(lowers)
    middles = sorted(list(filter(lambda x: x < max_lower, middles)), reverse=True)
 
    if middles == []:
        return 0
 
    max_middle = max(middles)
    tops = sorted(list(filter(lambda x: x < max_middle, tops)), reverse=True)
    
    if tops == []:
        return 0
 
    counter = 0
    for top in tops:
        for middle in middles:
            if middle <= top:
                break
 
            for lower in lowers:
                if lower <= middle:
                    break
                
                counter += 1
                
    return counter
 
count = calc()
print(count)

### FIXED CODE ###

# coding: utf-8
import math
import bisect

def calc():
    n = int(input())
    tops = list(map(int, input().split()))
    middles = list(map(int, input().split()))
    lowers = list(map(int, input().split()))
 
    tops = sorted(tops)
    lowers = sorted(lowers)
 
    counter = 0
 
    for i in range(n):
        # A < B
        top_num = bisect.bisect_left(tops, middles[i])
        
        # B < C
        lower_num = n - bisect.bisect_right(lowers, middles[i])
        
        counter += top_num * lower_num
    
    return counter
 
print(calc())
### BUGGY CODE ###

from heapq import heappush,heappop
h,w=map(int,input().split())
sx,sy=map(int,input().split())
sx-=1
sy-=1
gx,gy=map(int,input().split())
gx-=1
gy-=1
g=[list(input()) for _ in range(h)]
v=[[0 for j in range(w)] for i in range(h)]
q=[]
heappush(q,[0,sx,sy])
k=[[0,1],[0,-1],[1,0],[-1,0]]
while q:
    c,x,y=heappop(q)
    if x==gx and y==gy:
        print(c)
        exit()
    if v[x][y]==1:
        continue
    v[x][y]=1
    for a,b in k:
        nx=x+a
        ny=y+b
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:
            heappush(q,[c,nx,ny])
    for a in range(-2,3):
        for b in range(-2,3):
            if a*b==0 and abs(a+b)<=1:
                continue
            nx=x+a
            ny=y+b
            if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:
                heappush(q,[c+1,nx,ny])
print(-1)

### FIXED CODE ###

from collections import deque
def main():
    h,w=map(int,input().split())
    sx,sy=map(int,input().split())
    sx-=1
    sy-=1
    gx,gy=map(int,input().split())
    gx-=1
    gy-=1
    g=[tuple(input()) for _ in range(h)]
    v=[[0 for j in range(w)] for i in range(h)]
    q=deque([[sx,sy]])
    v[sx][sy]=1
    ans=0
    k=[[0,1],[0,-1],[1,0],[-1,0]]
    while True:
        l=[]
        while q:
            x,y=q.popleft()
            l.append([x,y])
            if x==gx and y==gy:
                print(ans)
                exit()
            for a,b in k:
                nx=x+a
                ny=y+b
                if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:
                    q.append([nx,ny])
                    v[nx][ny]=1
        if len(l)==0:
            break
        for x,y in l:              
            for a in range(-2,3):
                for b in range(-2,3):
                    if a*b==0 and abs(a+b)<=1:
                        continue
                    nx=x+a
                    ny=y+b
                    if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:
                        q.append([nx,ny])
                        v[nx][ny]=1
        ans+=1 
    print(-1)
if __name__=='__main__':
    main()
### BUGGY CODE ###

n = int(input())
value = list(map(int, input().split()))
cost = list(map(int, input().split()))
ret = 0
for state in range((1 << n) - 1):
    values = costs = 0
    for shift in range(n):
        if (state >> shift) & 1 == 1:
            values += value[shift]
            costs += cost[shift]
    ret = max(ret, values - costs)
print(ret)

### FIXED CODE ###

n = int(input())
value = list(map(int, input().split()))
cost = list(map(int, input().split()))
ret = 0
for state in range((1 << n)):
    values = costs = 0
    for shift in range(n):
        if (state >> shift) & 1 == 1:
            values += value[shift]
            costs += cost[shift]
    ret = max(ret, values - costs)
print(ret)