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

X, Y ,Z ,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

C.sort(reverse=True)

ab_scores = []
for a in A:
    for b in B:
        score = a + b
        ab_scores.append(score)

cut = min(K, len(ab_scores))
ab_scores.sort(reverse=True)
ab_scores = ab_scores[:cut]
scores = []
for ab in ab_scores:
    for c in C:
        scores.append(ab + c)



scores.sort(reverse=True)

for i in range(K):
    print(scores[i])


### FIXED CODE ###

X, Y ,Z ,K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)


scores = []
for a in range(len(A)):
    for b in range(len(B)):
        if (a + 1) * (b + 1) > K:
            break
        for c in range(len(C)):
            if (a + 1) * (b + 1) * (c + 1) > K:
                break
            score = A[a] + B[b] + C[c]
            scores.append(score)

scores.sort(reverse=True)

for i in range(K):
    print(scores[i])
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    for j in range(i+1,N):
        ans += A[i]*A[j]
ans = ans % (10**9+7)
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = 0
s = sum(A[1:])
for i in range(N-1):
    ans += A[i]*s 
    s = s - A[i+1]
print(ans % (10**9+7))
### BUGGY CODE ###

N = int(input())

ab = [list(map(int, input().split())) for _ in range(N - 1)]

graph = [[] for _ in range(N)]

for ai, bi in ab:
    graph[ai - 1].append(bi - 1)
    graph[bi - 1].append(ai - 1)

from collections import deque

ans = []
col_max = 1
q = deque([(0, -1, 0)])
while len(q) > 0:
    col = 1
    src, prev, col_prev = q.popleft()
    # print("#", src)
    for dst in graph[src]:
        if dst == prev: continue
        #
        if col == col_prev: col += 1
        # print("##", dst)
        # ans[src][dst] = col
        ans.append((dst, src, col))
        q.append((dst, src, col))
        col_max = max(col, col_max)
        col += 1
        
ans.sort(key=lambda x: x[0] * N + x[1])
        
print(col_max)
for ai, bi in ab:
    l = 0
    r = N - 2
    # print("#", ai, bi)
    while l < r:
        m = l + (r - l + 1) // 2
        tmp = ans[m][0] * N + ans[m][1]
        # print("#", (l, r), m, tmp)
        if tmp <= (bi - 1) * N + (ai - 1):
            l = m
        else:
            r = m - 1
    #
    # print("#", l, ans)
    print(ans[l][2])

### FIXED CODE ###

N = int(input())

ab = [list(map(int, input().split())) for _ in range(N - 1)]

graph = [[] for _ in range(N)]

for ai, bi in ab:
    graph[ai - 1].append(bi - 1)
    graph[bi - 1].append(ai - 1)

from collections import deque

ans = []
col_max = 1
q = deque([(0, -1, 0)])
while len(q) > 0:
    col = 1
    src, prev, col_prev = q.popleft()
    # print("#", src)
    for dst in graph[src]:
        if dst == prev: continue
        #
        if col == col_prev: col += 1
        # print("##", dst)
        # ans[src][dst] = col
        ans.append((dst, src, col))
        q.append((dst, src, col))
        col_max = max(col, col_max)
        col += 1
        
ans.sort(key=lambda x: x[0] * N + x[1])
        
print(col_max)
for ai, bi in ab:
    l = 0
    r = N - 2
    # print("#", ai, bi)
    while l < r:
        m = l + (r - l + 1) // 2
        tmp = ans[m][0] * N + ans[m][1]
        # print("#", (l, r), m, tmp)
        if tmp <= (bi - 1) * N + (ai - 1):
            l = m
        else:
            r = m - 1
    #
    # print("#", l, ans)
    print(ans[l][2])
### BUGGY CODE ###

def main():
    import sys
    input = sys.stdin.readline
    H, W = [int(x) for x in input().strip().split()]
    Ch, Cw = [int(x)-1 for x in input().strip().split()]
    Dh, Dw = [int(x)-1 for x in input().strip().split()]
    M = [0 for _ in range(H)]
    for h in range(H):
        M[h] = list(input().strip())
        # print(M[h])
    ans = 0
    q = set([(Dh, Dw)])
    dhdw = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    while True:
        newq = set()
        while q:
            q_ = set()
            for h, w in q:
                # print('({}, {}) = {}'.format(h, w, ans))
                if h == Ch and w == Cw:
                    print(ans)
                    return True
                M[h][w] = '#'
                newq.add((h, w))
                for dh, dw in dhdw:
                    if 0 <= h+dh < H and 0 <= w+dw < W:
                        if M[h+dh][w+dw] == '#':
                            continue
                        q_.add((h+dh, w+dw))
                        M[h+dh][w+dw] = '#'
            q = q_

        if newq:
            for h, w in newq:
                for dh in range(-2, 3):
                    if not (0 <= h+dh < H):
                        continue
                    for dw in range(-2, 3):
                        if 0 <= w+dw < W:
                            if M[h+dh][w+dw] == '.':
                                q.add((h+dh, w+dw))
        # for h in range(H):
        #     print(M[h])
        if len(q):
            ans += 1
        else:
            print(-1)
            return True

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.readline
    H, W = [int(x) for x in input().strip().split()]
    Ch, Cw = [int(x)-1 for x in input().strip().split()]
    Dh, Dw = [int(x)-1 for x in input().strip().split()]
    M = [W for _ in range(H)]
    for h in range(H):
        M[h] = list(input().strip())
        # print(M[h])
    ans = 0
    q = set([(Dh, Dw)])
    while True:
        nq = set()
        while q:
            q_ = set()
            for h, w in q:
                # print('({}, {}) = {}'.format(h, w, ans))
                if h == Ch and w == Cw:
                    print(ans)
                    return True
                M[h][w] = '#'
                nq.add((h, w))
                for dh, dw in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    if 0 <= h+dh < H and 0 <= w+dw < W:
                        if M[h+dh][w+dw] == '.':
                            q_.add((h+dh, w+dw))
                            M[h+dh][w+dw] = '#'
            q = q_

        if nq:
            for h, w in nq:
                for dh in range(-2, 3):
                    for dw in range(-2, 3):
                        if 0 <= h+dh < H and 0 <= w+dw < W:
                            if M[h+dh][w+dw] == '.':
                                q.add((h+dh, w+dw))
        # for h in range(H):
        #     print(M[h])
        if len(q):
            ans += 1
        else:
            print(-1)
            return True

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


def gcd(a, b):
  while b:
    a, b = b, a%b
  return a

a, b = map(int, input().split())
max = gcd(a, b)
pf=[]

#print(max)
tmp=max
i=2

while tmp != 1:
    if tmp%i == 0:
        pf.append(i)
        tmp=tmp/i
    else:
        i+=1

#print(pf)
spf = set(pf)
#print(spf)
print(len(spf)+1)


### FIXED CODE ###

def gcd(a, b):
  while b:
    a, b = b, a%b
  return a

a, b = map(int, input().split())
max = gcd(a, b)
pf=[]

#print(max)
tmp=max
i=2
while tmp % 2 == 0:
    pf.append(2)
    tmp //= 2
f = 3
while f * f <= tmp:
    if tmp % f == 0:
        pf.append(f)
        tmp //= f
    else:
        f += 2
if tmp != 1:
    pf.append(tmp)


#print(pf)
spf = set(pf)
#print(spf)
print(len(spf)+1)
### BUGGY CODE ###

n = int(input())
a= input().split()
level = 1000000000000000000
product = 1

for num in range(n):
  product *= int(a[num])

if product > level:
  print("-1")
else:
  print(product)

### FIXED CODE ###

n = int(input())
a= input().split()
level = pow(10, 18)
product = 1

if "0" in a:
  print(0)
else:
  for num in range(n):
    product *= int(a[num])
    if product > level:
      print("-1")
      break
  if product <= level:
    print(product)
### BUGGY CODE ###

from bisect import bisect_left
 
n, m, q = map(int, input().split())
LR = [0]*m
for i in range(m):
    LR[i] = list(map(int, input().split()))
P = [0]*q
Q = [0]*q
for i in range(q):
    P[i], Q[i] = map(int, input().split())
 
LR.sort(key=lambda x: x[0])
L = list(map(lambda x: x[0], LR))
 
for i in range(q):
    l = bisect_left(L, P[i])
    r = bisect_left(L, Q[i]+1)-1
    cnt = 0
    for j in range(l, r+1):
        if LR[j][1] <= Q[i]:
            cnt += 1
    print(cnt)

### FIXED CODE ###

from bisect import bisect_left
 
n, m, q = map(int, input().split())
LR = [0]*m
for i in range(m):
    LR[i] = list(map(int, input().split()))
P = [0]*q
Q = [0]*q
for i in range(q):
    P[i], Q[i] = map(int, input().split())
 
LR.sort(key=lambda x: (x[0], x[1]))
L = list(map(lambda x: x[0], LR))
#print(LR)
 
C = [[0]*(n+1) for _ in range(n+1)]
for i in range(1,n+1):
    l = bisect_left(L, i)
    r = bisect_left(L, i+1)-1
    R2 = list(map(lambda x: x[1], LR[l:r+1]))
    #print(l, r, R2)
    for j in range(i,n+1):
        C[i][j] = bisect_left(R2, j+1)
    #print(i, C[i])
 
for i in range(q):
    ans = 0
    for j in range(P[i], Q[i]+1):
        ans += C[j][Q[i]]
    print(ans)
### BUGGY CODE ###

import numpy as np

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

modn = int(1e9 + 7)

A = np.mod(A, modn)

ret = 0
for i in range(0, len(A)-1):
  for j in range(i+1, len(A)):
    ret += A[i]*A[j]

print(np.mod(ret, modn))


### FIXED CODE ###

import numpy as np

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

modn = int(1e9 + 7)

ruiseki = sum(A)

ret = 0
for i in range(0, len(A)-1):
  ruiseki -= A[i]
  ret += A[i] * ruiseki

print(np.mod(ret, modn))
### BUGGY CODE ###

from enum import Enum
from queue import Queue
import collections
import numpy as np


import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

L,R = map(int,input().split())
min = BIG_NUM
if L <= 2019 and R >= 2019:
    print(0)
else:
    for i in range(L,R+1):
        for j in range(i+1,R+1):
            tmp = (i*j)%2019
            if tmp < min:
                min = tmp
            if min == 0:
                break
    print(min)


### FIXED CODE ###

from enum import Enum
from queue import Queue
import collections
import numpy as np


import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

L,R = map(int,input().split())
min = BIG_NUM
if (R/2019 - L/2019)>=1.0:
    print(0)
else:
    for i in range(L,R+1):
        for j in range(i+1,R+1):
            tmp = (i*j)%2019
            if tmp < min:
                min = tmp
            if min == 0:
                break
    print(min)
### BUGGY CODE ###

MOD = 2019
L, R = map(int, input().split())

ans = float('inf')
for i in range(L,R):
    if i%MOD==0:
        print(0)
        exit()
    for j in range(i+1,R+1):
        if i%MOD==0:
            print(0)
            exit()
        ans = min(ans,(i*j)%MOD)
print(ans)

### FIXED CODE ###

MOD = 2019
L, R = map(int, input().split())

if R - L > 2019:
    print(0)
    exit()

ans = float('inf')
for i in range(L,R):
    for j in range(i+1,R+1):
        ans = min(ans,(i*j)%MOD)
print(ans)
### BUGGY CODE ###

import itertools
N = int(input())
L=[]*N
L=map(int,input().split(" "))
ans=0
for a,b,c in itertools.combinations(L,3):
    if (a<b+c) & (b<c+a) & (c<a+b):ans+=1
print(ans)

### FIXED CODE ###

import bisect
n=int(input())
L=[]*n
L=list(map(int,input().split()))
L.sort()
cnt=0
for i in range(n-1):
    for j in range(i+1,n):
        ab=L[i]+L[j]
        num=bisect.bisect_left(L[j+1:],ab)
        cnt+=num
print(cnt)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
query = [input().rstrip().split() for _ in range(q)]

flag = False
for i in range(q):
    qq = query[i]
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                s = s + qq[2]
            else:
                s = qq[2] + s
        else:
            if qq[1] == '1':
                s = qq[2] + s
            else:
                s = s + qq[2]

if flag:
    print(s[::-1])
else:
    print(s)


### FIXED CODE ###

import sys
from collections import deque

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
d = deque()
d.append(s)

flag = False
for i in range(q):
    qq = input().split()
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                d.append(qq[2])
            else:
                d.appendleft(qq[2])
        else:
            if qq[1] == '1':
                d.appendleft(qq[2])
            else:
                d.append(qq[2])

ans = ''.join(list(d))
if flag:
    print(ans[::-1])
else:
    print(ans)
### BUGGY CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  # j-i=Ai+Aj
  # Ai+i=j-Aj

  a=[]
  b=[]
  for i in range(n):
    x=l[i]
    a.append(x+(i+1))
    b.append((i+1)-x)

  ans=0
  for i,x in enumerate(a[i:]):
    ans+=b.count(x)

  for i,x in enumerate(b[i:]):
    ans+=a.count(x)

  return ans

# main()
print(main())


### FIXED CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  # j-i=Ai+Aj
  # Ai+i=j-Aj

  a=[]
  b={}
  for i in range(n):
    x=l[i]
    a.append(x+i)
    y=i-x
    if y in b:
      b[y]+=1
    else:
      b[y]=1

  ans=0
  for x in a:
    if x in b:
      ans+=b[x]

  return ans

# main()
print(main())
### BUGGY CODE ###

n = int(input())
ans = 1 + n
if n % 2 == 0:
    max = int(n / 2)
else:
    max = int((n - 1) / 2)

for i in range (2, max):
    if n % i == 0 and i + n / i < ans:
        ans = i + n / i

print(int(ans - 2))


### FIXED CODE ###

import math
n = int(input())
ans = 1 + n

max = math.ceil(n**0.5)

for i in range (2, max+1):
    if n % i == 0 and i + n / i < ans:
        ans = i + n / i

print(int(ans - 2))
### BUGGY CODE ###

import heapq
import sys
sys.setrecursionlimit(10000000)

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

lv =[[] for _ in range(n)]
ans=[0] * (n)

def dfs(v,p=-1):
    for u in lv[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)

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

for _ in range(q):
    p,x = map(int,input().split())
    p-=1
    ans[p] += x

dfs(0)

for i in ans:print(i)

### FIXED CODE ###

I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
 
 
from sys import stdin,setrecursionlimit
#from copy import deepcopy
 
setrecursionlimit(10**6)
input=stdin.readline
 
def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)
 
n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
 
for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    
 
dfs(0)
 
print(*counter)
### BUGGY CODE ###

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


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

ans = 0
for i in range(N):
    if A[i] % 2 == 0:
        ans += prime_decomposition(A[i])

print(ans)

### FIXED CODE ###

def gu(x):
    counter = 0
    while x % 2 == 0 and x != 0:
        x /= 2
        counter += 1
    return counter


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

ans = 0
for i in range(N):
    if A[i] % 2 == 0:
        ans += gu(A[i])

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

import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)


N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(lambda x: int(x) - 1, input().split())
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, input().split())


def bfs1(s):
    q = deque()
    q.append((s, 0))

    while len(q) > 0:
        v, step = q.popleft()
        if step == 3:
            edge3[s].add(v)
        else:
            for nv in edge[v]:
                q.append((nv, step + 1))


def bfs2(n):
    global T
    q = deque()
    status[n] = 0
    q.append(n)

    while len(q) > 0:
        v = q.popleft()
        for nv in edge3[v]:
            if status[nv] == -1:
                status[nv] = status[v] + 1
                q.append(nv)


edge3 = [set() for _ in range(N)]
for i in range(N):
    bfs1(i)


status = [-1] * N
status[S] = 0
bfs2(S)
print(status[T])


### FIXED CODE ###

import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

N = int(input())
T = [{} for i in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    T[a][b] = c
    T[b][a] = c

Q, K = map(int, input().split())
shortest_path = [0 for _ in range(N+1)]
queue = [[v, T[K][v], K] for v in T[K].keys()]

while len(queue) > 0:
    v = queue.pop(0)
    shortest_path[v[0]] = v[1]
    queue += [[u, T[v[0]][u] + v[1], v[0]] for u in T[v[0]].keys() if u != v[2]]

for _ in range(Q):
    x, y = map(int, input().split())
    print(shortest_path[x] + shortest_path[y])

### FIXED CODE ###

from collections import deque

N = int(input())
T = [{} for i in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    T[a][b] = c
    T[b][a] = c

Q, K = map(int, input().split())
shortest_path = [0 for _ in range(N+1)]
queue = deque([[v, T[K][v], K] for v in T[K].keys()])
for _ in range(N-1):
    v = queue.popleft()
    shortest_path[v[0]] = v[1]
    queue.extend([[u, T[v[0]][u] + v[1], v[0]] for u in T[v[0]].keys() if u != v[2]])

for _ in range(Q):
    x, y = map(int, input().split())
    print(shortest_path[x] + shortest_path[y])
### BUGGY CODE ###

n,k=map(int,input().split())
d = tuple(map(int, input().split()))
e = [0]*n

for i in range(n):
    s = (1+d[i])*d[i]/2
    e[i] = s / d[i]

max1 = 0
tmp = 0
for j in range(n):
    if len(e[j:j+k]) < k:
        break
    if j == 0:
        tmp = sum(e[j:j+k])
    else:
        tmp = tmp - e[j-1] + e[j+k-1]
    if max1 < tmp:
        max1 = tmp
print(max1)


### FIXED CODE ###

n,k=map(int,input().split())
d = tuple(map(int, input().split()))
e = [0]*n

for i in range(n):
    s = (1+d[i])*d[i]/2
    e[i] = s / d[i]

max1 = 0
tmp = 0
for j in range(n-k+1):
#    if len(e[j:j+k]) < k:
#        break
    if j == 0:
        tmp = sum(e[j:j+k])
    else:
        tmp = tmp - e[j-1] + e[j+k-1]
    if max1 < tmp:
        max1 = tmp
print(max1)
### BUGGY CODE ###

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect


def main():
    H, W = map(int, input().split())
    A = [list(input()) for _ in range(H)]

    checked = [[False] * W for _ in range(H)]
    ans = -1

    while True:
        checked_count = 0
        for h in range(H):
            for w in range(W):
                if checked[h][w]:
                    checked_count += 1
        if checked_count == H * W:
            break

        # for h in range(H):
        #     print(checked[h])

        ans += 1
        # print(ans)

        que = []
        for h in range(H):
            for w in range(W):
                if A[h][w] == '#' and not checked[h][w]:
                    que.append((h, w))
                    checked[h][w] = True

        while que:
            h, w = que.pop()
            dx4 = [0, 0, 1, -1]
            dy4 = [1, -1, 0, 0]

            for dx, dy in zip(dx4, dy4):
                next_h = h + dy
                next_w = w + dx
                if 0 <= next_h < H and 0 <= next_w < W and not checked[next_h][next_w]:
                    A[next_h][next_w] = '#'

    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect


def main():
    H, W = map(int, input().split())
    A = [list(input()) for _ in range(H)]

    checked = [[False] * W for _ in range(H)]
    ans = 0

    que = []
    for h in range(H):
        for w in range(W):
            if A[h][w] == '#' and not checked[h][w]:
                que.append((h, w))
                checked[h][w] = True
    while que:
        next_que = []
        while que:
            h, w = que.pop()
            dx4 = [0, 0, 1, -1]
            dy4 = [1, -1, 0, 0]

            for dx, dy in zip(dx4, dy4):
                next_h = h + dy
                next_w = w + dx
                if 0 <= next_h < H and 0 <= next_w < W and A[next_h][next_w] == '.' and not checked[next_h][next_w]:
                    A[next_h][next_w] = '#'
                    checked[h][w] = True
                    next_que.append((next_h, next_w))
        que = next_que
        if que:
            ans += 1

    print(ans)


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

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1):
            for x1 in range(ca[1]+ca[2]+ca[3]+1):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()