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

a, b, m = map(int, input().split())
a1 = list(map(int, input().split()))
b1 = list(map(int, input().split()))

disc = [list(map(int, input().split())) for i in range(m)]

import itertools
import numpy as np

b2 = np.array(b1)

ans = []
from collections import deque
ans = deque(ans)

for i in a1:
    ans.append( np.min( b2 + np.tile(i, b) ) )


for i in range(m):
    ans.append( a1[disc[i][0]-1] + b1[disc[i][1]-1] - disc[i][2] )

ans = list(ans)
print(min(ans))

### FIXED CODE ###

a, b, m = map(int, input().split())
a1 = list(map(int, input().split()))
b1 = list(map(int, input().split()))

disc = [list(map(int, input().split())) for i in range(m)]
import numpy as np


from collections import deque
ans = []

ans = deque(ans)

for i in range(m):
    ans.append( a1[disc[i][0]-1] + b1[disc[i][1]-1] - disc[i][2] )

ans = list(ans)

print(min( min(ans), min(a1)+min(b1) ))
### BUGGY CODE ###

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

# 部分和の作成
l = []
for i in range(n):
    t=0
    for j in range(i, n):
        t += a[j]
        l.append(t)
ans = 0
for i in range(40, -1, -1):
    x = 2 ** i
    s = []
    for ll in l:
        if (ans + x) & ll == (ans + x):
            s.append(ll)
    if len(s) >= k:
        ans += x
print(ans)



### FIXED CODE ###

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

# 部分和の作成
l = []
for i in range(n):
    t=0
    for j in range(i, n):
        t += a[j]
        l.append(t)
ans = 0
for i in range(40, -1, -1):
    x = 2 ** i
    s = []
    for ll in l:
        if (ans + x) & ll == (ans + x):
            s.append(ll)
    if len(s) >= k:
        ans += x
print(ans)
### BUGGY CODE ###

n,ma,mb = (int(i) for i in input().split())
x = [[int(i) for i in input().split()] for i in range(n)]
dp = [[[float("inf") for i in range(10*n+1)] for i in range(10*n+1)] for i in range(n+1)]
dp[0][0][0],ans = 0,float("inf")
for i in range(1,n+1):
	for j in range(10*(n-1)+1):
		for k in range(10*(n-1)+1):
			dp[i][j][k] = min(dp[i-1][j][k],dp[i][j][k])
			dp[i][j+x[i-1][0]][k+x[i-1][1]] = min(dp[i][j+x[i-1][0]][k+x[i-1][1]],dp[i-1][j][k]+x[i-1][2])
for i in range(1,10*n+1):
	for j in range(1,10*n+1):
		if i*mb==j*ma: ans = min(ans,dp[n][i][j])
if ans==float("inf"): print(-1)
else: print(ans)

### FIXED CODE ###

n, ma, mb = map(int, input().split())
INF = float("inf")
dp = [[[INF for i in range(10*n+1)] for i in range(10*n+1)] for i in range(n+1)]
dp[0][0][0],ans = 0,INF
for i in range(1,n+1):
    a,b,c = (int(l) for l in input().split())
    for j in range(10*(i-1)+1):
        for k in range(10*(i-1)+1):
            if dp[i-1][j][k] != INF:
              dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k])
              dp[i][j+a][k+b] = min(dp[i][j+a][k+b], dp[i-1][j][k]+c)
for i in range(ma,10*n+1,ma):
	if i*mb//ma<=10*n: ans=min(ans,dp[n][i][i*mb//ma])
if ans==INF: print(-1)
else: print(ans)
### BUGGY CODE ###

from math import gcd
N = int(input())
A = [int(i) for i in input().split()]
st=set()
pn = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

for a in A:
  if a == 1:
    continue
  ary=[]
  d=set()
  while a % 2 == 0:
    d.add(2)
    a//=2
  i=1
  n = 3
  while n * n <= a:
    if a % n == 0:
      a //= n
      d.add(n)
    else:
      i += 1
      if i < len(pn):
        n=pn[i]
      else:
        break
  if a > 1:
    ary.append(a)
    d.add(a)
  if st.isdisjoint(d):
    st = st | d
    continue
  # ------------------------
  n=gcd(A[0],A[1])
  for a in A[2:]:
    n=gcd(n,a)
  if n == 1:
    print("setwise coprime")
  else:
    print("not coprime")
  exit()
 
print("pairwise coprime")


### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]
maxA=max(A)
pn=list(range(maxA+1))
n=2
while n*n <= maxA:
  if n == pn[n]:
    for m in range(n, len(pn), n):
      if pn[m] == m:
        pn[m] = n
  n+=1

s=set()
for a in A:
  st = set()
  while a > 1:
    st.add(pn[a])
    a//=pn[a]
  if not s.isdisjoint(st):
    break
  s |= st
else:
  print("pairwise coprime")
  exit()

from math import gcd

n = gcd(A[0], A[1])
for a in A[2:]:
  n=gcd(n,a)
if n == 1:
  print("setwise coprime")
else:
  print("not coprime")
### BUGGY CODE ###

import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
import math
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
#from bisect import insort_left
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
#import itertools
#import heapq
#import numpy as np
#INF = float("inf")
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
flag = 1
s = 0
for i in range(N-1):
    if flag == 0:
        break
    for j in range(i+1,N):
        judge = 1
        if flag == 0:
            break
        a = A[i][j]
        s += a
        for k in range(N):
            if k == i or k == j:
                continue
            elif a > A[i][k] + A[k][j]:
                flag = 0
                break
            elif a == A[i][k] + A[k][j]:
                s -= a
                break
            else:
                continue
if flag == 0:
    print(-1)
else:
    print(s)

### FIXED CODE ###

import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
import math
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
#from bisect import insort_left
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
#import itertools
#import heapq
#import numpy as np
#INF = float("inf")
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
flag = 1
s = 0
for i in range(N-1):
    if flag == 0:
        break
    for j in range(i+1,N):
        judge = 1
        if flag == 0:
            break
        a = A[i][j]
        s += a
        for k in range(N):
            if k == i or k == j:
                continue
            elif a > A[i][k] + A[k][j]:
                flag = 0
                break
            elif a == A[i][k] + A[k][j]:
                s -= a
                break
            else:
                continue
if flag == 0:
    print(-1)
else:
    print(s)
### BUGGY CODE ###

n = int(input())
l = input().split()
max = 10**18

r = 1

for x in l:
  r *= int(x)

r = r if r <= max else -1
print(r)


### FIXED CODE ###

n = int(input())
l = [int(x) for x in input().split()]

if 0 in l:
    r=0
else:
    r = 1
    for x in range(n):
        r = r * l[x]
        if r > 10**18:
            r=-1
            break
  
print(r)
### BUGGY CODE ###

N = int(input())
A = sorted(map(int, input().split()), reverse=False)
m = max(A)+1
s = set(A)
dp = [True] * m
for i in range(N):
    t = A[i] * 2
    while t < m:
        dp[t] = False
        t += A[i]
ans = 0
for a in A:
    if A.count(a) == 1 and dp[a]:
        ans += 1
print(ans)



### FIXED CODE ###

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
p = [True] * (10 ** 6 + 1)
s = list(set(a))
for x in s:
    t = x * 2
    while t <= 10 ** 6:
        p[t] = False
        t += x
ans = 0
for x in a:
    if c[x] == 1 and p[x] == True:
        ans += 1
print(ans)
### BUGGY CODE ###

from collections import defaultdict

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


def prime_decomposition(n):
    i = 2
    table = []
    while i * i <= n:
        while n % i == 0:
            n //= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table

pr = prime_decomposition(M)
di = defaultdict(int)
for p in pr:
    di[p] += 1
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def combination(n, mod):
    lst = [1]
    for i in range(1, n+1):
        lst.append(lst[-1] * (n+1-i) % mod * modinv(i, mod) % mod)
    return lst
mod = 10**9+7
ans = 1
for k, v in di.items():
    ans *= combination(v+N-1, mod)[N-1]
    ans %= mod
print(ans)


### FIXED CODE ###

from collections import defaultdict

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


def prime_decomposition(n):
    i = 2
    table = []
    while i * i <= n:
        while n % i == 0:
            n //= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table

pr = prime_decomposition(M)
di = defaultdict(int)
for p in pr:
    di[p] += 1
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def combination(n, mod):
    lst = [1]
    for i in range(1, n+1):
        lst.append(lst[-1] * (n+1-i) % mod * modinv(i, mod) % mod)
    return lst
mod = 10**9+7
ans = 1
for k, v in di.items():
    ans *= combination(v+N-1, mod)[N-1]
    ans %= mod
print(ans)
### BUGGY CODE ###

import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
e = dd(list)
for _ in range(N - 1):
  a, b = map(int, input().split())
  e[a].append(b)
  e[b].append(a)
d = [[N for _ in range(N + 1)] for _ in range(N + 1)]
for s in range(1, N + 1):
  Q = dq([s])
  d[s][s] = 0
  while len(Q):
    p = Q.popleft()
    for q in e[p]:
      if d[s][q] > d[s][p] + 1:
        d[s][q] = d[s][p] + 1
        Q.append(q)
vis = set()
table = [[0] * (N + 1) for _ in range(N + 1)]
lntable = [0] * (N + 1)
for i in range(1, N + 1):
  for j in range(1, N + 1):
    if d[i][j] > K:
      table[i][j] = 1
      lntable[i] += 1
while True:
  t = 0
  for i in range(1, N + 1):
    if i in vis: continue
    if lntable[i] > lntable[t]: t = i
  if t == 0: break
  for i in range(1, N + 1):
    if table[i][t] == 1:
      table[i][t] = 0
      lntable[i] -= 1
  vis.add(t)
print(len(vis))

### FIXED CODE ###

import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
e = dd(list)
for _ in range(N - 1):
  a, b = map(int, input().split())
  e[a].append(b)
  e[b].append(a)
d = [[N for _ in range(N + 1)] for _ in range(N + 1)]
for s in range(1, N + 1):
  Q = dq([s])
  d[s][s] = 0
  while len(Q):
    p = Q.popleft()
    for q in e[p]:
      if d[s][q] > d[s][p] + 1:
        d[s][q] = d[s][p] + 1
        Q.append(q)
vis = set()
table = [[0] * (N + 1) for _ in range(N + 1)]
lntable = [0] * (N + 1)
for i in range(1, N + 1):
  for j in range(1, N + 1):
    if d[i][j] > K:
      table[i][j] = 1
      lntable[i] += 1
while True:
  t = 0
  for i in range(1, N + 1):
    if i in vis: continue
    if lntable[i] > lntable[t]: t = i
  if t == 0: break
  for i in range(1, N + 1):
    if table[i][t] == 1:
      table[i][t] = 0
      lntable[i] -= 1
  vis.add(t)
print(len(vis))
### BUGGY CODE ###

# coding: utf-8

n = int(input().rstrip())
numbers = [int(i) for i in input().rstrip().split(" ")]


def get_gcm(num1: int, num2: int):
    if num1 % num2 == 0:
        return num2
    return get_gcm(num2, num1 % num2)


l_list = [numbers[0]]
for i in range(1, n):
    l_list.append(get_gcm(l_list[i - 1], numbers[i]))

r_list = [numbers[len(numbers) - 1]]
for i in reversed(range(0, n - 1)):
    r_list.insert(0, get_gcm(r_list[0], numbers[i]))

ans = 0
for i in range(n):
    l = 0
    if i > 0:
        l = l_list[i - 1]
    r = 0
    if i < n - 1:
        r = r_list[i + 1]
    if l == 0:
        l = r
    if r == 0:
        r = l
    ans = max(ans, get_gcm(l, r))
print(ans)


### FIXED CODE ###

# coding: utf-8
import fractions

n = int(input().rstrip())
numbers = [int(i) for i in input().rstrip().split(" ")]

l_list = [0 for i in range(n)]
l_list[0] = numbers[0]
for i in range(1, n):
    l_list[i] = fractions.gcd(l_list[i-1], numbers[i])

r_list = [0 for i in range(n)]
r_list[n-1] = numbers[n-1]
for i in reversed(range(0, n - 1)):
    r_list[i] = fractions.gcd(r_list[i+1], numbers[i])

ans = 0
for i in range(n):
    l = 0
    if i > 0:
        l = l_list[i - 1]
    r = 0
    if i < n - 1:
        r = r_list[i + 1]
    if l == 0:
        l = r
    if r == 0:
        r = l
    ans = max(ans, fractions.gcd(l, r))
print(ans)
### BUGGY CODE ###

def main():
    X, K, D = map(int, input().split())

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 10
    else :
        limit = 11
    i = 0
    if X > K * D :
        print(X - K * D)
    elif X == K * D :
        print("0")
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (val[i+1] == val[i-1]) and i > limit :
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    X, K, D = map(int, input().split())

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        #print(syo)
        if syo > 3 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 2
            else:
                limit = 3
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()
### BUGGY CODE ###

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

AB = dict()

for _ in range(M):
    a, b = map(int, input().split())
    AB.setdefault(a, [])
    AB[a].append(b)

    AB.setdefault(b, [])
    AB[b].append(a)

a = [-1 for _ in range(N + 1)]

a[1] = 0
current = 1
pos = [1]

while True:
    next_pos = []

    for p in pos:
        for n in AB[p]:
            if a[n] == -1:
                a[n] = p
            next_pos.append(n)

    pos = next_pos

    if not next_pos or -1 not in a[2:]:
        break

if -1 in a[2:]:
    print('No')
else:
    print('Yes')
    for i in a[2:]:
        print(i)


### FIXED CODE ###

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

AB = dict()

for _ in range(M):
    a, b = map(int, input().split())
    AB.setdefault(a, [])
    AB[a].append(b)

    AB.setdefault(b, [])
    AB[b].append(a)

a = [-1 for _ in range(N + 1)]

a[1] = 0
current = 1
pos = [1]

while True:
    next_pos = []

    for p in pos:
        for n in AB[p]:
            if a[n] == -1:
                a[n] = p
                next_pos.append(n)

    pos = next_pos
    if not pos:
        break

if -1 in a[2:]:
    print('No')
else:
    print('Yes')
    for i in a[2:]:
        print(i)
### BUGGY CODE ###

n = int(input())

num = list(map(int, input().split()))

multi = 1
for i in range(n):
  multi *= num[i]

if multi > 10 ** 18:
  print(-1)
else:
  print(multi)

### FIXED CODE ###

n = int(input())
li = list(map(int, input().split()))

if 0 in li:
  print(0)
else:
  result = 1
  flag = True
  for i in range(n):
    result *= li[i]
    if result > 10 ** 18:
      flag = False
      print(-1)
      break
  if flag == True:
    print(result)
### BUGGY CODE ###

import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
bc = []
for _ in range(M):
  b, c = map(int, input().split())
  bc += [-c]*b

heapq.heapify(A)
heapq.heapify(bc)
ans = 0
while True:
  a = heapq.heappop(A)
  c = -1 * heapq.heappop(bc)
  if a < c:
    ans += c
  else:
    ans += a
    break
  if len(bc) == 0:
    break

print(ans+sum(A))




### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
bc = [list(map(int, input().split())) for _ in range(m)]
bc = sorted(bc, key=lambda x:x[1], reverse=True)
st = 0
flag = True
for i in range(m):
  b, c = bc[i]
  if flag and st < n:
    for i in range(st, min(b+st, n)):
      if a[i] < c:
        a[i] = c
      else:
        flag = False
        break
    st += b
  else:
    break

print(sum(a))
### BUGGY CODE ###

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    sum_val = 0
    num = 10 ** 9 + 7
    for i in range(0, n - 1):
        sum_val += a[i] * (sum(a[i+1:]) % num)
        if sum_val > num:
            sum_val %= num
    print(sum_val)



### FIXED CODE ###

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    cumsum_a = [0] * len(a)
    cumsum_a[0] = a[0]
    for i in range(1, len(a)):
        cumsum_a[i] += cumsum_a[i-1] + a[i]

    sum_val = 0
    num = 10 ** 9 + 7
    for i in range(0, n - 1):
        sum_val += a[i] * (cumsum_a[-1] - cumsum_a[i])
        if sum_val > num:
            sum_val %= num
    print(sum_val)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N=int(input())
P=list(map(int,input().split()))

DP=[[-1]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        DP[i][j]=min(i,j,N-1-i,N-1-j)

USED=[[0]*N for i in range(N)]

ANS=0
    
for p in P:
    q,r=divmod(p-1,N)
    ANS+=DP[q][r]
    DP[q][r]-=1
    USED[q][r]=1

    Q=[(q,r)]

    while Q:
        x,y=Q.pop()

        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N:
                if USED[z][w]==1 and DP[z][w]>DP[x][y]:
                    DP[z][w]=DP[x][y]
                    Q.append((z,w))
                    
                elif DP[z][w]>DP[x][y]+1:
                    DP[z][w]=DP[x][y]+1
                    Q.append((z,w))
    
    #for d in DP:
    #    print(*d)
    #print(ANS)
    #print()
print(ANS)
    


### FIXED CODE ###

import sys
input = sys.stdin.readline

N=int(input())
P=list(map(int,input().split()))

DP=[[0]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        DP[i][j]=min(i,j,N-1-i,N-1-j)

USED=[[1]*N for i in range(N)]
ANS=0

def update(z,w):
    if 0<=z<N and 0<=w<N:
        if DP[z][w]>DP[x][y]+USED[z][w]:
            DP[z][w]=DP[x][y]+USED[z][w]
            Q.append((z,w))
    

for p in P:
    q,r=divmod(p-1,N)
    ANS+=DP[q][r]
    DP[q][r]-=1
    USED[q][r]=0

    Q=[(q,r)]

    while Q:
        x,y=Q.pop()

        update(x+1,y)
        update(x-1,y)
        update(x,y+1)
        update(x,y-1)

print(ANS)
### BUGGY CODE ###

n,*l = map(int, open(0).read().split())

ans = 0
for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if l[i] < l[j]+l[k]:
                if l[j] < l[i]+l[k]:
                    if l[k] < l[i]+l[j]:
                        ans += 1
print(ans)

### FIXED CODE ###

n = int(input())
l = sorted(map(int, input().split()))
 
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
    
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ans += big_i - (j + 1)
                
print(ans)
### BUGGY CODE ###

N = int(input())
A = [0]+list(map(int,input().split()))
A.sort()
B = [0]+list(map(int,input().split()))
B.sort()
C = [0]+list(map(int,input().split()))
C.sort()

bko = [0 for i in range(N+1)]
for i in range(1,N+1):
  b = B[i]
  cnt = 1
  for j in range(cnt,N+1):
    if A[j] >= b:
      bko[i] = j-1
      cnt = j-1
      break
    if j == N:
      bko[i] = N
      cnt = N+1

#bkoを累積和にする
for i in range(N):     
  bko[i+1] += bko[i]

cko = [0 for i in range(N+1)]
for i in range(1,N+1):
  c = C[i]
  cnt = 0
  for j in range(cnt,N+1):
    if B[j] >= c:
      cko[i] = bko[j-1]
      cnt = j-1
      break
    if j == N:
      cko[i] = bko[N]
      cnt = N+1
###
print(sum(cko))        

### FIXED CODE ###

N = int(input())
A = [0]+list(map(int,input().split()))
A.sort()
B = [0]+list(map(int,input().split()))
B.sort()
C = [0]+list(map(int,input().split()))
C.sort()

bko = [0 for i in range(N+1)]
cnt = 1
for i in range(1,N+1):
  b = B[i]
  for j in range(cnt,N+1):
    if A[j] >= b:
      bko[i] = j-1
      cnt = j
      break
    if j == N:
      bko[i] = N
      cnt = N

#bkoを累積和にする
for i in range(N):     
  bko[i+1] += bko[i]

cko = [0 for i in range(N+1)]
cnt = 1
for i in range(1,N+1):
  c = C[i]
  for j in range(cnt,N+1):
    if B[j] >= c:
      cko[i] = bko[j-1]
      cnt = j
      break
    if j == N:
      cko[i] = bko[N]
      cnt = N
###
print(sum(cko))
### BUGGY CODE ###

first_step_string = input()
query_num = int(input())
query_li = list()
ans = first_step_string
for _ in range(query_num):
    query = input().split()
    if query[0] == '1':
        ans = ans[::-1]
    elif query[1] == '1':
        ans = query[2] + ans
    else:
        ans += query[2]

print(ans)


### FIXED CODE ###

from collections import deque

s = deque(input())
query_num = int(input())
query_li = list()
for _ in range(query_num):
    query_li.append(input().split())

rev_flag = False
for query in query_li:
    if query[0] == '1':
        rev_flag = not rev_flag
    elif (query[1] == '1' and rev_flag) or (query[1] == '2' and not rev_flag):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if rev_flag:
    print(''.join(deque(reversed(s))))
else:
    print(''.join(s))