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

total=0
s=0
i=0
while True:
 try:
  a,b = map(int,input().split(','))
  total += a*b
  s+=b
  i+=1
 except:
   result=s/i
   print(total)
   print(result)

### FIXED CODE ###

try:
  total=s=i=0 
  while True:
   a,b = map(int,input().split(','))
   total += a*b
   s += b
   i += 1
except:
 print(total)
 print(round(s/i+0.5))
### BUGGY CODE ###

n = int(input())
s=input()
r=[]
g=[]
b=[]
for i,c in enumerate(s):
    if 'R' == c:
        r.append(i+1)
    elif 'G' == c:
        g.append(i+1)
    elif 'B' == c:
        b.append(i+1)
count=0
for i in r:
    for j in g:
        for k in b:
            l=sorted([i,j,k])
            if l[1]*2-l[0]-l[2]:
                count+=1
print(count)

### FIXED CODE ###

import collections
n = int(input())
s=input()
l=list(collections.Counter(s).values())
if len(l) == 3:
    ans=l[0]*l[1]*l[2]
    for i in range(n):
        for j in range(i+1,n):
            k = 2*j-i
            if n <= k:
                break
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                ans-=1
    print(ans)
else:
    print(0)
### BUGGY CODE ###

N = int(raw_input())
A = [int(raw_input()) - 1 for _ in range(N)]

ans = 0
i = 0
used = set([0, 1])
while True:
    if ans != 0 and i in used:
        break
    i = A[i]
    ans += 1
if i != 1:
    ans = -1
print ans


### FIXED CODE ###

N = int(raw_input())
A = [int(raw_input()) - 1 for _ in range(N)]

ans = 0
i = 0
used = set([0, 1])
while True:
    if ans != 0 and i in used:
        break
    used.add(i)
    i = A[i]
    ans += 1
if i != 1:
    ans = -1
print ans
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    K,S = (int(x) for x in input().split())

    count = 0

    for x in range(K+1):
        for y in range(K+1):
            for z in range(K+1):
                # for make turple (x,y,z)
                # and calculate Sum of turple
                if sum((x,y,z)) == S:
                    count += 1
                    
    print(count)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    K,S = (int(x) for x in input().split())

    count = 0

    for x in range(K+1):
        for y in range(K+1):
            z = S - (x + y)
            if z >= 0 and z <= K:
                count += 1

    print(count)

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

n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
AS=[0]
BS=[0]
r=0
for i in A:
  AS.append(i+AS[-1])
for i in B:
  BS.append(i+BS[-1])
p=n+m+2
f=m+2
for i in range(1,n+2):
  for j in range(1,f):
    if AS[-i]+BS[-j]<=k:
      r=max(r,p-i-j)
      f=j
      break
print(r)

### FIXED CODE ###

n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
AS=[0]
BS=[0]
r=0
for i in A:
  if i+AS[-1]<=k:
    AS.append(i+AS[-1])
  else:
    break
    
for i in B:
  if i+BS[-1]<=k:
    BS.append(i+BS[-1])
  else:
    break
    
j=len(BS)-1
for i in range(len(AS)):
  while AS[i]+BS[j]>k:
    j-=1
  r=max(r,i+j)
print(r)
### BUGGY CODE ###

n = int(input())
l = []

for i in range(n):
  x = int(input())
  if x in l:
    l.remove(x)
  else:
    l.append(x)

print(len(l))

### FIXED CODE ###

import collections

n = int(input())
li = [int(input()) for i in range(n)]
c = collections.Counter(li)
ans = 0

for item in c.items():
    if item[1]%2 == 1:
        ans += 1

print(ans)
### BUGGY CODE ###

MOD = 10**9 + 7
def solve(s):
    n = len(s)
    dp = [[0,0,0,0] for i in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        t = 2 * int(s[i] == "?") + 1
        for j in range(4):
            dp[i+1][j] = t * dp[i][j]
        if s[i] == "A":
            dp[i+1][1] = (dp[i+1][1] + dp[i][0]) % MOD
        elif s[i] == "B":
            dp[i+1][2] = (dp[i+1][2] + dp[i][1]) % MOD
        elif s[i] == "C":
            dp[i+1][3] = (dp[i+1][3] + dp[i][2]) % MOD
        else:
            dp[i+1][1] = (dp[i+1][1] + dp[i][0]) % MOD
            dp[i+1][2] = (dp[i+1][2] + dp[i][1]) % MOD
            dp[i+1][3] = (dp[i+1][3] + dp[i][2]) % MOD
    return dp[n][3] % MOD

s = input()
print(solve(s))

### FIXED CODE ###

import numpy as np
MOD = 10**9 + 7
def solve(s):
    n = len(s)
    dp = np.zeros((n+1, 4), dtype=int)
    dp[0,0] = 1
    for i in range(n):
        t = 2 * int(s[i] == "?") + 1
        dp[i+1] = t * dp[i] % MOD
        if s[i] == "A":
            dp[i+1,1] += dp[i,0]
        elif s[i] == "B":
            dp[i+1,2] += dp[i,1]
        elif s[i] == "C":
            dp[i+1,3] += dp[i,2]
        else:
            dp[i+1,1:] += dp[i,:-1]
    return dp[n][3] % MOD

s = input()
print(solve(s))
### BUGGY CODE ###

N,T=map(int,input().split())
A=tuple(map(int,input().split()))
B={}
for i in range(1,N):
  tmp=A[i]-min(A[:i])
  if tmp not in B:
    B[tmp]=1
  else:
    B[tmp]+=1
maxB=max(B)
print(B[maxB])

### FIXED CODE ###

n,t=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*n
x[0]=a[0]
for i in range(1,n):
    x[i]=min(x[i-1],a[i])
d=dict()
for i in range(1,n):
    y=a[i]-x[i-1]
    if y>0:
        if y in d:
            d[y]+=1
        else:
            d[y]=1
d=list(d.items())
d.sort(reverse=True)
print(d[0][1])
### BUGGY CODE ###

n = int(input())
s = str(input())

r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)
        
ans = len(r) * len(g) * len(b)
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in [j + diff_ji, i - diff_ji, (j + i) / 2]:
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)

### FIXED CODE ###

n = int(input())
s = str(input())

r, g, b = set(), set(), set()
for i in range(len(s)):
    if s[i] == "R":
        r.add(i)
    elif s[i] == "G":
        g.add(i)
    else:
        b.add(i)
        
ans = len(r) * len(g) * len(b)
for r_idx in r:
    for g_idx in g:
        j, i = max(r_idx, g_idx), min(r_idx, g_idx)
        diff_ji = abs(j-i)
        cnt = 0
        for v in [j + diff_ji, i - diff_ji, (j + i) / 2]:
            if v in b:
                cnt += 1            
        ans -= cnt
print(ans)
### BUGGY CODE ###

from collections import Counter

def mapdict(itemfunc, dictionary):
    return dict(map(itemfunc, dictionary.items())) 

def minus(tup):
    return (tup[0]-1,tup[1]) 
def plus(tup):
    return (tup[0]+1,tup[1]) 

def count(dic):
    count = 0
    for k,v in dic.items():
        count += abs(k*v)
    return count

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

A2 = [A[i-1]-i for i in range(1,N+1)]
Adic = Counter(A2)
Ax  = Adic.most_common()[0][0]
Adic = mapdict(lambda t: (t[0]+Ax,t[1]), Adic)

res = count(Adic)
pl = count(mapdict(plus,Adic))
mn = count(mapdict(minus,Adic))


if pl<res:
    tmp = float("inf")
    while res <= tmp:
        tmp = res
        Adic = mapdict(plus, Adic)
        res = count(Adic)
    res = tmp

elif mn<res:
    tmp = float("inf")
    while res <= tmp:
        tmp = res
        Adic = mapdict(minus, Adic)
        res = count(Adic)
    res = tmp

print(res)

### FIXED CODE ###

from collections import Counter

def mapdict(n, dictionary):
    return dict(map(lambda t:(t[0]-n,t[1]), dictionary.items()))

def count(dic):
    count = 0
    for k,v in dic.items():
        count += abs(k*v)
    return count

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

A2 = sorted([A[i-1]-i for i in range(1,N+1)])


Adic = Counter(A2)
Ax  = A2[N//2]
Adic = mapdict(Ax, Adic)

res = count(Adic)

if N==1:
    print(0)

elif N==2:
    print(sum(map(abs,map(lambda x:x-min(A2),A2))))

else:
    pl = count(mapdict(A2[N//2 +1],Adic))
    mn = count(mapdict(A2[N//2 -1],Adic))
    if pl<=res:
        tmp = pl
        tmpc = 1
        while tmp <= res:
            tmpc += 1
            if N//2 + tmpc > N-1:
                break
            res = tmp
            Adic = mapdict(A2[N//2 +tmpc], Adic)
            tmp = count(Adic)
    elif mn<=res:
        tmp = mn
        tmpc = 1
        while tmp <= res:
            tmpc += 1
            if N//2 - tmpc < 0:
                break
            res = tmp
            Adic = mapdict(A2[N//2 -tmpc], Adic)
            tmp = count(Adic)

    print(res)
### BUGGY CODE ###

from collections import defaultdict
n,p = map(int,input().split())
s = input()

ans = 0
if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)
    exit()

sur = []
for i in range(n-1, -1, -1):
    sur += [int(s[i:]) % p]

d = defaultdict(int)
for x in sur:
    d[x] += 1

for k,v in d.items():
    if k == 0:
        ans += v * (v+1) // 2
    else:
        ans += v * (v-1) // 2
print(ans)

### FIXED CODE ###

from collections import defaultdict
n,p = map(int,input().split())
s = input()

sur1 = [int(s[0]) % p]
sur2 = [int(s[-1]) % p]
sur10 = [1 % p]
for i in range(n-1):
    sur1 += [(sur1[i] * 10 + int(s[i+1])) % p]
    sur10 += [(sur10[i] * 10) % p]
    sur2 += [(sur2[i] + sur10[i+1] * int(s[-i-2])) % p]

ans = 0
if p == 2:
    for i in range(n):
        if int(s[i]) % 2 == 0:
            ans += i + 1
    print(ans)
    exit()

if p == 5:
    for i in range(n):
        if int(s[i]) % 5 == 0:
            ans += i + 1
    print(ans)
    exit()

d = defaultdict(int)
for x in sur2:
    d[x] += 1

for k,v in d.items():
    if k == 0:
        ans += v * (v+1) // 2
    else:
        ans += v * (v-1) // 2
print(ans)
### BUGGY CODE ###

import numpy as np
import sys
input = sys.stdin.readline
n=int(input())
a = tuple(map(int,input().split()))
cnt = [0,0,0] #1個の個数、二個の個数、三個の個数
for i in range(n):
    cnt[a[i]-1] += 1
    
def f(i,j,k):
    if [i,j,k] == [0,0,0]:
        return 0.0
    if dp[i][j][k] != 0.0:
        return dp[i][j][k]
    tmp = 0.0
    if i>0:
        tmp += f(i-1,j,k)*float(i)/float(i+j+k)
    if j>0:
        tmp += f(i+1,j-1,k)*float(j)/float(i+j+k)
    if k>0:
        tmp += f(i,j+1,k-1)*float(k)/float(i+j+k)
    tmp += float(n)/float(i+j+k)
    
 
    dp[i][j][k] = tmp
    return tmp
dp = np.zeros((n+1,n+1,n+1))
#dp = [[[0.0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
 
print(f(cnt[0],cnt[1],cnt[2]))


### FIXED CODE ###

import sys
input = sys.stdin.readline
n = int(input())
a = tuple(map(int,input().split()))
c1 = a.count(1)
c2 = a.count(2)
c3 = a.count(3)

dp = [[[0.0]*(n+1) for i in range(n+1)] for j in range(n+1)] 
dp[0][0][0] = 0
for i in range(c3+1):
    for j in range(c2+c3-i+1):
        for k in range(n-i-j+1):
            if i == j == k == 0:
                continue
            tmp = n
            if k != 0:
                tmp += k*dp[k-1][j][i]
            if j != 0:
                tmp += j*dp[k+1][j-1][i]
            if i != 0:
                tmp += i*dp[k][j+1][i-1]
            dp[k][j][i] = tmp/(i+j+k)

print(dp[c1][c2][c3])
### BUGGY CODE ###

N = int(input())
uw = []
S = []
c = 0
for i in range(N):
    s = sorted(input())
    if s not in uw:
        uw.append(s)
        
    S.append(s)

for w in uw:
    n = S.count(w)
    if n == 1:
        c += 0
    elif n == 2:
        c += 1
        
    else:
        c += (n - 1)*(2 + (n - 2))//2
    
print(int(c))

### FIXED CODE ###

N = int(input())
c = {}
for i in range(N):
    s = ''.join(sorted(input()))
    if s not in c:
        c[s] = 0
    else:
        c[s] += 1
r = sum([(v/2)*(2 + v - 1) for v in c.values()])
print(int(r))
### BUGGY CODE ###

N = int(input())
S = input()

sleft = S[:N]
sright = S[2*N:N-1:-1]
ldict,rdict = {},{}
count = 0
for i in range(2**N):
    red = bin(i)[2:].zfill(N)
    leftred, rightred, leftblue, rightblue = "","","",""
    for j in range(len(red)):
        if red[j] == "1":
            leftred += sleft[j]
            rightred += sright[j]
        else:
            leftblue += sleft[j]
            rightblue += sright[j]
    ldict[red] = leftred + "-" + leftblue
    rdict[red] = rightred + "-" + rightblue

for i in range(N+1):
    lchoose = set(v for k, v in ldict.items() if k.count("1") == i)
    rchoose = set(v for k, v in rdict.items() if k.count("1") == i)
    sameset = list(lchoose & rchoose)
    for j in range(len(sameset)):
        lnum = len(list(k for k, v in ldict.items() if v == sameset[j]))
        rnum = len(list(k for k, v in rdict.items() if v == sameset[j]))
        count += lnum * rnum
print(count)

### FIXED CODE ###

N = int(input())
S = input()

sleft = S[:N]
sright = S[2*N:N-1:-1]
ldict,rchoise = {},[]
count = 0
for i in range(2**N):
    red = bin(i)[2:].zfill(N)
    leftred, rightred, leftblue, rightblue = "","","",""
    for j in range(N):
        if red[j] == "1":
            leftred += sleft[j]
            rightred += sright[j]
        else:
            leftblue += sleft[j]
            rightblue += sright[j]
    if leftred + "-" + leftblue in ldict.keys():
        ldict[leftred + "-" + leftblue] += 1
    else:
        ldict[leftred + "-" + leftblue] = 1
    rchoise.append(rightred + "-" + rightblue)
    
for i in rchoise:
    count += ldict.get(i, 0)
print(count)
### BUGGY CODE ###

n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

'''
fact = [1]
i = 1
while i < n:
    fact.append(fact[i-1] * i)
    i += 1 

def comb_fac(n, k):
    return fact[n] // (fact[n-k] * fact[k])
'''
def comb(n, k):
    n1 = n+1
    r = min(k, n-k)
    numer = denom = 1
    i = 1
    while i < r+1:
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
        i += 1
    
    #memo[n][k] = numer * pow(denom, mod - 2, mod) % mod
    #return numer // denom
    return numer * pow(denom, mod - 2, mod) % mod


amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

### FIXED CODE ###

n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)
### BUGGY CODE ###

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

### FIXED CODE ###

n=int(input())
s=input()
ans=0
for i in range(1000):
    num=str(i).zfill(3)
    if s.find(num[0])==-1:
        continue
    s1=s[s.find(num[0])+1:]
    if s1.find(num[1])==-1:
        continue
    s2=s1[s1.find(num[1])+1:]
    if s2.find(num[2])==-1:
        continue
    ans+=1
print(ans)
### BUGGY CODE ###

def solve():
  L, R = map(int, input().split())
  ans = 2019
  for _i in range(L, R + 1):
    i = _i % 2019
    if i == 0: return 0
    v = i * (i + 1) % 2019
    for _j in range(_i + 1, R + 1):
      ans = min(ans, v)
      v += i
      v %= 2019
  return ans

print(solve())

### FIXED CODE ###

def solve():
  L, R = map(int, input().split())
  ans = 2019
  for i in range(L, R):
    for j in range(i + 1, R + 1):
      v = i * j % 2019
      if v < ans: ans = v
      if ans == 0: return 0
  return ans

print(solve())
### BUGGY CODE ###

n = int(input())
d = list(map(int, input().split()))
 
d.sort()

ans = 0
 
for i in range(n):
    for j in range(i, n):
        for k in range(j, n):
            if i == j or j == k or i == k:
                continue
            elif d[i] + d[j] <= d[k]:
                break
            else:
                ans += 1
print(ans)

### FIXED CODE ###

import bisect

n = int(input())
d = list(map(int, input().split()))
 
d.sort()

ans = 0

for b in range(n):
    for a in range(b):
        ab = d[a] + d[b]
        r = bisect.bisect_left(d, ab)
        l = b+1
        ans += max(0, r-l)

print(ans)
### BUGGY CODE ###

def winList(t, r, s, p):
    if t == "r":
        return [0, 0, p]
    elif t == "s":
        return [r, 0, 0]
    else:
        return [0, s, 0]

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

dp = []
for i in range(N):
    wList = winList(T[i], R, S, P)
    if i <= K-1:
        if i == 0:
            dp.append(wList)
        else:
            dp.append([wList[0] + max(dp[i - 1]),
                       wList[1] + max(dp[i - 1]),
                       wList[2] + max(dp[i - 1])])
    else:
        tmpRPointList = []
        tmpSPointList = []
        tmpPPointList = []
        wKList = winList(T[i - K], R, S, P)
        for j in range(K):
            if dp[i - K][0] > dp[i - K][1] and dp[i - K][0] > dp[i - K][2]:
                tmpRPointList.append(wList[0] + max(dp[i - K + j]) - wKList[0])
            else:
                tmpRPointList.append(wList[0] + max(dp[i - K + j]))
            if dp[i - K][1] > dp[i - K][0] and dp[i - K][1] > dp[i - K][2]:
                tmpSPointList.append(wList[1] + max(dp[i - K + j]) - wKList[1])
            else:
                tmpSPointList.append(wList[1] + max(dp[i - K + j]))
            if dp[i - K][2] > dp[i - K][0] and dp[i - K][2] > dp[i - K][1]:
                tmpPPointList.append(wList[2] + max(dp[i - K + j]) - wKList[2])
            else:
                tmpPPointList.append(wList[2] + max(dp[i - K + j]))
        dp.append([max(tmpRPointList), max(tmpSPointList), max(tmpPPointList)])

print(max(dp[N-1]))


### FIXED CODE ###

def winList(t, r, s, p):
    if t == "r":
        return [0, 0, p]
    elif t == "s":
        return [r, 0, 0]
    else:
        return [0, s, 0]

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

dp = []
for i in range(N):
    wList = winList(T[i], R, S, P)
    if i <= K-1:
        if i == 0:
            dp.append(wList)
        else:
            dp.append([wList[0] + max(dp[i - 1]),
                       wList[1] + max(dp[i - 1]),
                       wList[2] + max(dp[i - 1])])
    else:
        wKList = winList(T[i - K], R, S, P)
        tmpR = 0
        tmpS = 0
        tmpP = 0
        if dp[i - K][0] > dp[i - K][1] and dp[i - K][0] > dp[i - K][2]:
            tmpR = wList[0] + max(dp[i - 1]) - wKList[0]
        else:
            tmpR = wList[0] + max(dp[i - 1])

        if dp[i - K][1] > dp[i - K][0] and dp[i - K][1] > dp[i - K][2]:
            tmpS = wList[1] + max(dp[i - 1]) - wKList[1]
        else:
            tmpS = wList[1] + max(dp[i - 1])

        if dp[i - K][2] > dp[i - K][0] and dp[i - K][2] > dp[i - K][1]:
            tmpP = wList[2] + max(dp[i - 1]) - wKList[2]
        else:
            tmpP = wList[2] + max(dp[i - 1])
        dp.append([tmpR, tmpS, tmpP])

print(max(dp[N-1]))
### BUGGY CODE ###

N = int(input())
A = [int(str) for str in input().split()]

[print(a, end=' ') for a in [A.index(i) + 1 for i in range(1, N + 1)]]

### FIXED CODE ###

import numpy as np


N = int(input())
A = np.array([int(str) for str in input().split()])
[print(i + 1, end=' ') for i in np.argsort(A)]
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    li, ri = map(int, input().split())
    l += [li]
    r += [ri]

for ind in range(Q):
    substr = S[max(0, l[ind]-1) : min(r[ind], N)]
    print(sum([substr[ind:ind+2] == "AC" for ind in range(r[ind]-l[ind]+1)]))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    li, ri = map(int, input().split())
    l += [li]
    r += [ri]

S_tf = [0]*(N+1)
for ind in range(N):
    S_tf[ind + 1] = S_tf[ind] + (1 if S[ind:ind+2] == "AC" else 0)
for ind in range(Q):
    print(S_tf[max(0, r[ind]-1)] - S_tf[min(l[ind]-1, N)])
### BUGGY CODE ###

n, x, y = map(int, input().split())

dis_list = [0] * (n - 1)

for i in range(0, n):
    for j in range(i + 1, n):
        dis = min(abs(j - i), abs(x - i - 1) + 1 + abs(y - j - 1), abs(y - i - 1) + 1 + abs(x - j - 1))
        # print(dis)
        dis_list[dis - 1] += 1

for i in range(n - 1):
    print(dis_list[i])

### FIXED CODE ###

n, x, y = map(int, input().split())

dis_list = [0] * (n - 1)

for i in range(1, n):
    for j in range(i + 1, n + 1):
        dis_list[min(abs(j - i), abs(x - i) + 1 + abs(y - j), abs(y - i) + 1 + abs(x - j)) - 1] += 1

for i in dis_list:
    print(i)
### BUGGY CODE ###

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

def func(i):
  if i >= 0: return i
  else: return -i

for _ in range(K):
  if func(X + D) <= func(X - D): X += D
  else: X -= D

print(func(X))

### FIXED CODE ###

X, K, D = map(int,input().split())
if X < 0: X *= -1
n = X // D
if K <= n: print(X - K * D)
else:
  X -= n * D
  K -= n
  if K % 2 == 0: print(X)
  else: print(-X + D)
### BUGGY CODE ###

from bisect import bisect_left

n = int(input())
A = list(map(int,input().split()))
A = [i for i in A if A.count(i) >= 2]
if len(A) == 0:
  print(0)
  exit()

if A.count(max(A)) >= 4:
  print(max(A)**2)
  exit()
elif 2<= A.count(max(A)) < 4:
  print(max(A)*A[bisect_left(A,max(A))-1])



### FIXED CODE ###

from collections import Counter

n = int(input())
A = list(map(int,input().split()))
A = Counter(A).most_common()
ans = 0

for i,j in sorted(A,reverse=True):
  if j >=4 and ans ==0:
    ans = i*i
    break
  elif j >= 2 and ans == 0:
    ans = i
  elif j >= 2:
    ans *= i
    break
print(ans)
### BUGGY CODE ###

H,W,M = map(int,input().split())
lsx = [0] + [0]*H
lsy = [0] + [0]*W
lsxy = []
for i in range(M):
    x,y = map(int,input().split())
    lsx[x] += 1
    lsy[y] += 1
    lsxy.append([x,y])
maxx = max(lsx)
maxy = max(lsy)
lsmaxx = [i for i, x in enumerate(lsx) if x == maxx]
lsmaxy = [i for i, x in enumerate(lsy) if x == maxy]
lenlen = len(lsmaxx)*len(lsmaxy)
for i in range(M):
    if lsxy[i][0] in lsmaxx and lsxy[i][1] in lsmaxy:
        lenlen -= 1
if lenlen == 0:
    factor = 1
else:
    factor = 0
print(maxx+maxy-factor)

### FIXED CODE ###

H,W,M = map(int,input().split())
lsx = [0] + [0]*H
lsy = [0] + [0]*W
lsxy = []
for i in range(M):
    x,y = map(int,input().split())
    lsx[x] += 1
    lsy[y] += 1
    lsxy.append([x,y])
maxx = max(lsx)
maxy = max(lsy)
cx = lsx.count(maxx)
cy = lsy.count(maxy)
al = cx*cy
for i in range(M):
    if lsx[lsxy[i][0]] == maxx and lsy[lsxy[i][1]] == maxy:
        al -= 1
if al == 0:
    factor = 1
else:
    factor = 0        
print(maxx+maxy-factor)
### BUGGY CODE ###

S = input()
Q = int(input())
pre = []
suf = []
rev = False
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        pre, suf = suf[::-1], pre[::-1]
        rev = not rev
    else:
        if q[1] == '1':
            pre = pre[::-1]
            pre.append(q[2])
            pre = pre[::-1]
        else:
            suf.append(q[2])
if rev:
    S = S[::-1]
print(''.join(pre) + S + ''.join(suf))


### FIXED CODE ###

from collections import deque
S = input()
Q = int(input())
pre = deque()
suf = deque()
rev = False
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        rev = not rev
    else:
        if (not rev and q[1] == '1') or (rev and q[1] == '2'):
            pre.appendleft(q[2])
        else:
            suf.append(q[2])
if rev:
    S = S[::-1]
    p = list(suf)[::-1]
    s = list(pre)[::-1]
else:
    p = list(pre)
    s = list(suf)
print(''.join(p) + S + ''.join(s))