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

import numpy as np
import sys
sys.setrecursionlimit(10000000)

N, M, P = map(int, input().split())
ABC_d = {}
for m in range(M):
    A, B, C = map(int, input().split())
    if A in ABC_d:
        ABC_d[A].append([B, C - P])
    else:
        ABC_d[A] = [[B, C - P]]


cost = np.empty(N, dtype = int) - np.inf
cost[0] = 0

def solve(pos, already):
    global cost

    if not(pos in ABC_d): return
    # print (already)

    for B, C in ABC_d[pos]:
        if already[B - 1] == True:
            # if cost[B - 1] == np.inf:
            #     pass
            # elif cost[B - 1] < cost[pos - 1] + C:
            #     cost[B - 1] = np.inf
            #     solve(B, already)
            if cost[B - 1] == np.inf:
                pass
            elif cost[pos-1] == np.inf or cost[B - 1] < cost[pos - 1] + C:
                cost[B-1] = np.inf
                solve(B, already)
        elif cost[B - 1] < cost[pos - 1] + C:
            cost[B - 1] = cost[pos - 1] + C
            already2 = already.copy()
            already2[B - 1] = True
            solve(B, already2)


already = np.zeros(N, dtype = bool)
already[0] = True
solve(1, already)

if (cost[-1] == np.inf):
    print (-1)
else:
    print (max(0, int(cost[-1])))


### FIXED CODE ###

import sys
sys.setrecursionlimit(10000000)

f_inf = sys.maxsize

N, M, P = map(int, input().split())
ABC_d = {}
for m in range(M):
    A, B, C = map(int, input().split())
    if A in ABC_d:
        ABC_d[A].append([B, C - P])
    else:
        ABC_d[A] = [[B, C - P]]

cost = [-f_inf] * N
cost[0] = 0

def solve(pos, cnt):
    global cost

    if not(pos in ABC_d): return
    if cnt > N: cost[pos-1] = f_inf;

    for B, C in ABC_d[pos]:
        if cost[B - 1] == f_inf or cost[B - 1] >= cost[pos - 1] + C:
            pass
        elif cost[pos-1] == f_inf:
            cost[B-1] = f_inf
            solve(B, cnt + 1)
        elif cost[B - 1] < cost[pos - 1] + C:
            cost[B - 1] = cost[pos - 1] + C
            solve(B, cnt + 1)

solve(1, 1)

if (cost[-1] == sys.maxsize):
    print (-1)
else:
    print (max(0, int(cost[-1])))
### BUGGY CODE ###

S = input()
p = 0
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        S += Qr[2]
      else:
        S = Qr[2]+S
    else:
      if p:
        S = Qr[2]+S
      else:
        S += Qr[2]
if p:
  print(S[::-1])
else:
  print(S)


### FIXED CODE ###

S = input()
p = 0
head = ''
end = ''
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        end += Qr[2]
      else:
        head = Qr[2]+head
    else:
      if p:
        head = Qr[2]+head
      else:
        end += Qr[2]
S = head + S + end
if p:
  print(S[::-1])
else:
  print(S)
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(map(int, list(input())))
num = 0
dict = [0 for i in range(2019)]
dict[0] = 1
ans = 0

for i , n in enumerate(S[::-1]):
    num += n * (pow(10,i) % 2019)
    dict[num % 2019] += 1

for i in range(2019):
    ans += dict[i] * (dict[i] - 1) // 2
print(ans)

### FIXED CODE ###

import sys
from collections import deque
def input():
    return sys.stdin.readline()[:-1]

S = deque(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i in range(len(S)):
    num += S.pop() * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)
### BUGGY CODE ###

n = int(input())
slist = list(input() for i in range(n))
sset = set(slist)
slist2 = list(sset)
slist2.sort()
count_list = []
for i in slist2:
  count_list.append(slist.count(i))
for i in range(len(count_list)):
  if max(count_list) == count_list[i]:
    print(slist2[i])

### FIXED CODE ###

n = int(input())
cnt = {}
mx = 0
for i in range(n):
  s = input()
  if s not in cnt:
    cnt[s] = 0
  cnt[s] += 1
  mx = max(mx,cnt[s])
ans = []
for s in cnt:
  if cnt[s] == mx:
    ans.append(s)
ans.sort()
for i in ans:
  print(i)
### BUGGY CODE ###

N,M = map(int,input().split())
C = []
for _ in range(N):
    A,B = map(int,input().split())
    C.append([A,B])
C = sorted(C,key=lambda x:x[0],reverse=True)
C = sorted(C,key=lambda x:x[1],reverse=True)
rev = 0
hist = [0 for _ in range(M+1)]
for x in C:
    A,B = x[0],x[1]
    try:
        ind = hist[-A-1::-1].index(0)
        hist[M-A-ind]=1
        rev += B
    except:pass
print(rev)

### FIXED CODE ###

import bisect
N,M = map(int,input().split())
C = []
for _ in range(N):
    A,B = map(int,input().split())
    C.append([A,B])
C = sorted(C,key=lambda x:x[0],reverse=True)
C = sorted(C,key=lambda x:x[1],reverse=True)
rev = 0
hist = list(range(0,M+1))
for x in C:
    A,B = x[0],x[1]
    ind = bisect.bisect_right(hist,M-A)
    if ind>0:
        hist.pop(ind-1)
        rev += B
print(rev)
### BUGGY CODE ###

N = int(input())
S = input()
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

print(ans)


### FIXED CODE ###

N = int(input())
S = [s for s in input()]
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

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

n = int(input())
p = [int(a) for a in input().split()]

ans = 1
done = [p[0]]
for i in range(1, n):
  if min(done) < p[i]:
    pass
  else:
    ans += 1
  done.append(p[i])

print(ans)

### FIXED CODE ###

n = int(input())
p = [int(a) for a in input().split()]

ans = 1
min_num = p[0]
for i in range(1, n):
  if min_num < p[i]:
    pass
  else:
    ans += 1
    min_num = p[i]

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

N = int(input())
A_ls = list(map(int, input().split()))
mul = 1
 
for i in A_ls:
  mul *= i
 

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

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 1
if a[0] == 0:
  print(0)
else:
  for i in range(len(a)):
    ans *= a[i]
    if ans > 10**18:
      print("-1")
      exit()
    elif i == len(a)-1:
      print(ans)
### BUGGY CODE ###

import numpy as np

N = int(input())
A = np.array(sorted(list(map(int, input().split()))))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
    A[A==B[i]] = C[i]
    print(np.sum(A))

### FIXED CODE ###

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

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

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

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

min = 10**18+1
flag = True

while(1):
  N = abs(N-K)
  if N < min: min = N
  else: break
    
print(min)

### FIXED CODE ###

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

print(min(n%k, abs(n%k-k)))
### BUGGY CODE ###

import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N=II()
As=LI()

DIG_CUM = [[0]*60 for _ in range(N+1)]

for i in range(N):
    for s in range(60):
        DIG_CUM[i+1][s] = DIG_CUM[i][s] + ((As[i] >> s) & 1)

# for arr in DIG_CUM:
#     print(arr)

res = 0
for i in range(N-1):
    for s in range(60):
        coef = (N - i - 1 - (DIG_CUM[N][s] - DIG_CUM[i+1][s])) if (As[i] >> s) & 1 else DIG_CUM[N][s] - DIG_CUM[i+1][s]
        res += coef * (1 << s)
    res %= DVSR
print(res)


### FIXED CODE ###

import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N=II()
As=LI()

DIG_CUM = [[0]*60 for _ in range(N+1)]

for i in range(N):
    for s in range(60):
        DIG_CUM[i+1][s] = DIG_CUM[i][s] + ((As[i] >> s) & 1)

# for arr in DIG_CUM:
#     print(arr)

res = 0
for i in range(N-1):
    for s in range(60):
        coef = DIG_CUM[N][s] - DIG_CUM[i+1][s]
        if (As[i] >> s) & 1: coef = N - i - 1 - coef
        # coef = (N - i - 1 - (DIG_CUM[N][s] - DIG_CUM[i+1][s])) if (As[i] >> s) & 1 else DIG_CUM[N][s] - DIG_CUM[i+1][s]
        res += coef * (1 << s)
    res %= DVSR
print(res)
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]

mod = 10**9+7

#sum_a = sum(a)

#bin_num = format(2, 'b')
#print(bin_num,len(bin_num))

max_a = max(a)
bin_max_a = format(max_a, 'b')

#print(bin_max_a)
ans = 0

for i in range(len(bin_max_a)):
    tmp = 0
    #print(i)
    kazu = 0
    for j in range(n):
        #print(j,kazu)
        kazu += int(format(a[j], 'b').zfill(len(bin_max_a))[i])
        #print(a[j],int(format(a[j], 'b').zfill(len(bin_max_a))[i]))
        #print(format(a[j], 'b').zfill(len(bin_max_a))[i])
        #print(kazu)
    if kazu == 0:
        continue
    tmp = kazu*(n-kazu) % mod
    tmp1 = 1
    """
    for i in range(len(bin_max_a)-1-i):
        tmp1 *= 2
        tmp1 %= mod
    """
    tmp1 = 2**(len(bin_max_a)-1-i) % mod
    tmp = tmp*tmp1%mod
    ans += tmp
    ans %= mod
    #print(ans,kazu)
    
print(ans)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]

mod = 10**9+7

bin_a = []

for i in range(n):
    bin_a.append(format(a[i],'060b'))

ans = 0

for i in range(60):
    one = 0
    zero = 0
    for j in range(n):
        #print(bin_a[j])
        if bin_a[j][i] == '1':
            one += 1
        else:
            zero += 1
    #print(i,zero,one)
    ans += zero * one * (2**(59-i))
    
print(ans%mod)
### BUGGY CODE ###

from bisect import bisect
N = int(input())
H = list(map(int,input().split()))
A = list(map(int,input().split()))

lis_h = [0]
lis_v = [0]

for h,a in zip(H,A):
    i = bisect(lis_h, h)
    v = lis_v[i-1] + a
    j = bisect(lis_v, v)

    lis_h = lis_h[:i] + [h] + lis_h[j:]
    lis_v = lis_v[:i] + [v] + lis_v[j:]
print(lis_v[-1])

### FIXED CODE ###

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

class SegTreeMax:
    def __init__(self,arr):
        n = 1
        while n < len(arr):
            n *= 2
        self.n = n
        nodes = [0]*(2*n-1)
        nodes[n-1:n-1+len(arr)] = arr
        for i in range(n-2,-1,-1):
            nodes[i] = max(nodes[2*i+1],nodes[2*i+2])
        self.nodes = nodes

    def update(self,i,val):
        i += self.n-1
        self.nodes[i] = val
        while i:
            i = (i-1)//2
            self.nodes[i] = max(self.nodes[2*i+1],self.nodes[2*i+2])

    def query(self,a,b,k=0,l=0,r=-1):
        if r < 0: r = self.n
        if r <= a or b <= l: return 0
        if a <= l and r <= b: return self.nodes[k]
        return max(self.query(a,b,2*k+1,l,(l+r)//2), self.query(a,b,2*k+2,(l+r)//2,r))

segt = SegTreeMax([0]*N)
ans = 0
for h,a in zip(H,A):
    v = segt.query(0,h) + a
    segt.update(h-1,v)
    ans = max(ans, v)
print(ans)
### BUGGY CODE ###

exec("def nWord(): nWord.cache = raw_input() if not hasattr(nWord, 'cache') or nWord.cache=='' else nWord.cache ; pos = nWord.cache.find(nWord.sep) ; ret, nWord.cache = (nWord.cache, '') if pos==-1 else (nWord.cache[:pos], nWord.cache[pos+1:].lstrip(nWord.sep)) ; return ret") ; exec("nWord.sep = ' '") ; exec("def ___nLine2(): ret, nWord.cache = (raw_input(), nWord.cache if hasattr(nWord, 'cache') else '') if not hasattr(nWord, 'cache') or nWord.cache=='' else (nWord.cache, '') ; return ret") ; exec('def nInt(): return int(nWord())') ; exec('def nFloat(): return float(nWord())') ; exec("def ___nAuto2():\n ret = nWord()\n try: return int(ret)\n except: pass\n try: return float(ret)\n except: return ret") ; exec("def nAuto(n=None): return ___nAuto2() if n is None else [___nAuto2() for i in range(n)]") ; exec("def nLine(n=None): return ___nLine2() if n is None else [___nLine2() for i in range(n)]")

N = nAuto()
S = nAuto(N)

D = {}
for  i in range(N):
    for j in range(N):
        c = S[i][j]
        if c in D:
            D[c] += 1
        else:
            D[c] = 1

max_c = 'a'
max_count = 0
for c in D:
    if max_count < D[c]:
        max_count = D[c]
        max_c = c

L = []
for  i in range(N):
    for j in range(N):
        c = S[i][j]
        if c!=max_c:
            L.append((i, j, c))

def check(A, B):
    for i2, j2, c in L:
        i = (i2-A+N) % N
        j = (j2-B+N) % N
        if c!=S[(A+j) % N][(B+i) % N]:
            return False
    return True

ng = 0
count = 0
for A in range(N):
    for B in range(N):
        if check(A, B):
            count += 1
            if 1000<=count and ng==0:
                print N*N
                exit()
        else:
            ng += 1
print count


### FIXED CODE ###

exec("def nWord(): nWord.cache = raw_input() if not hasattr(nWord, 'cache') or nWord.cache=='' else nWord.cache ; pos = nWord.cache.find(nWord.sep) ; ret, nWord.cache = (nWord.cache, '') if pos==-1 else (nWord.cache[:pos], nWord.cache[pos+1:].lstrip(nWord.sep)) ; return ret") ; exec("nWord.sep = ' '") ; exec("def ___nLine2(): ret, nWord.cache = (raw_input(), nWord.cache if hasattr(nWord, 'cache') else '') if not hasattr(nWord, 'cache') or nWord.cache=='' else (nWord.cache, '') ; return ret") ; exec('def nInt(): return int(nWord())') ; exec('def nFloat(): return float(nWord())') ; exec("def ___nAuto2():\n ret = nWord()\n try: return int(ret)\n except: pass\n try: return float(ret)\n except: return ret") ; exec("def nAuto(n=None): return ___nAuto2() if n is None else [___nAuto2() for i in range(n)]") ; exec("def nLine(n=None): return ___nLine2() if n is None else [___nLine2() for i in range(n)]")
 
N = nAuto()
S = nAuto(N)
 
def check(A):
    for i in range(N):
        for j in range(N):
            if S[(A+i) % N][j]!=S[(A+j) % N][i]:
                return 0
    return N
 
count = 0
for A in range(N):
    count += check(A)
print count
### BUGGY CODE ###

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

# dp = [[0 for i in range(N)] for _ in range(M)]

# sortedA = sorted(A,reverse=True)

for i in range(M):
    m = max(A)
    A[A.index(m)] = m//2

print(sum(A))

### FIXED CODE ###

import heapq as hq

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

# dp = [[0 for i in range(N)] for _ in range(M)]

# sortedA = sorted(A,reverse=True)

A = list(map(lambda x:x*-1, A))

hq.heapify(A)
for i in range(M):
    m=hq.heappop(A)*-1
    hq.heappush(A,(m//2)*-1)
#     print(A)

print(abs(sum(A)))
### BUGGY CODE ###

n, m = map(int, input().split())
ans = m//n

while ans > 0:
    if m%ans == 0:
        if m//ans >= n:
            break
    else:
        ans -= 1

print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
ans = m//n

while ans > 0:
    if m%ans == 0:
        if m//ans >= n:
            break
    else:
        ans -= 1

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

def green_bin(n,s):
    count = 0
 
    x = [sorted(i) for i in s]
    _x = sorted(x)
 
    for i in range(n-1):
        for j in range(i+1,n):
            if _x[i] == _x[j]:
                count += 1
            else:
                break
 
    return count
if __name__ == '__main__':
    n = int(input())
    s = [ input() for i in range(n)]
 
    print(green_bin(n,s))

### FIXED CODE ###

# -*- coding: utf-8 -*-
n = int(raw_input())
d = {}
ans = 0
for i in range(n):
    si = ''.join(sorted(raw_input()))
    if si in d:
        ans += d[si]
        d[si] += 1
    else:
        d[si] = 1
print(ans)
### BUGGY CODE ###

n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
if len(factor) == 0:
    factor.append(n)
if len(factor) == 1:
    print(factor[0] - 1)
else:
    ans = float("inf")
    for i in range(1, 2 ** len(factor)):
        a, b = 1, 1
        for j in range(len(factor)):
            if i & (2 ** j) == 0:
                a *= factor[j]
            else:
                b *= factor[j]
        if a + b - 2 < ans:
            ans = a + b - 2
    print(ans)

### FIXED CODE ###

n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)
### BUGGY CODE ###

def main():
    n=int(input())
    s=input()
    S=[int(x) for x in list(s)]
    Pass=set()
    z=S[0]*100+S[1]*10+S[2]
    Pass.add(z)
    for k in S[3:]:
        s=set()
        for p in Pass:
            p1,p2,p3=p//100,(p//10)%10,p%10
            s|={p2*100+p3*10+k,p1*100+p3*10+k,p1*100+p2*10+k}
        Pass|=s
    print(len(Pass))
    
if __name__=='__main__':
    main()

### FIXED CODE ###

def main():
    n=int(input())
    s=input()
    S=[int(x) for x in list(s)]
    Pass1={S[0],S[1],S[2]}
    Pass2={S[0]*10+S[1],S[0]*10+S[2],S[1]*10+S[2]}
    Pass3={S[0]*100+S[1]*10+S[2]}
    for k in S[3:]:
        for i in Pass2:
            Pass3.add(i*10+k)
        for i in Pass1:
            Pass2.add(i*10+k)
        Pass1.add(k)
    print(len(Pass3))
    
if __name__=='__main__':
    main()
### BUGGY CODE ###

def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2= i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[(x1, y1)].append((x2, y2))
            in_num[(x2, y2)] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[(i, j)] == 0:
                st.append((i, j))
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        print(ans)


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

n=int(input())
dp=[[[0]*(n+1) for i in range(n+1)] for j in range(n+1)]
a=[float(i) for i in input().split()]

p=a.count(1)
q=a.count(2)
r=a.count(3)

l=[1]
for i in range(1,1000):
    l.append(1.0/i)
for k in range(n+1):
    for j in range(n-k+1):
        for i in range(n-k-j+1):
            if i==j==k==0:
                continue
            res=n
            if i>0:
                res+=dp[i-1][j][k]*i
            if j>0:
                res+=dp[i+1][j-1][k]*j
            if k>0:
                res+=dp[i][j+1][k-1]*k
            res*=l[i+j+k]
            dp[i][j][k]=res

print(dp[p][q][r])


### FIXED CODE ###

n=int(input())
dp=[[[0.0]*(n+1) for i in range(n+1)] for j in range(n+1)]
dp[0][0][0]=0
a=[float(i) for i in input().split()]

p=a.count(1)
q=a.count(2)
r=a.count(3)

l=[1]
for i in range(1,1000):
    l.append(1.0/i)

for k in range(r+1):
    for j in range(q+r-k+1):
        for i in range(n-k-j+1):
            if i==j==k==0:
                continue
            res=n
            if i>0:
                res+=i*dp[i-1][j][k]
            if j>0:
                res+=j*dp[i+1][j-1][k]
            if k>0:
                res+=k*dp[i][j+1][k-1]
            res*=1/(i+j+k)
            dp[i][j][k]=res

print(dp[p][q][r])
### BUGGY CODE ###

N=int(input())
result=0
for A in range(1,N):
  for B in range(1,int(N/A)+1):
    if (N-A*B)>=1:
      result+=1
print(result)

### FIXED CODE ###

N=int(input())
result=0
for A in range(1,N):
  result+=int((N-1)/A)
print(result)
### BUGGY CODE ###

S = input()

A = []

for i in range(len(S)):
    for j in range(i+1,len(S)+1):
        if int(S[i:j]) % 2019 == 0:
            A.append((i+1,j))

print(len(A))

### FIXED CODE ###

from collections import Counter
import sys

# input = lambda : sys.stdin.readline().rstrip()

S = input()
n = len(S)

M = []
m = 0

for i in range(len(S)-1,-1, -1):
    
    m = (pow(10, n-i, 2019) * int(S[i]) + m) % 2019
    M.append(m)
    
    # print(S[i:n], m)

M.append(0)  # S[n:n] = "" = 0 が抜けるので、付け加える

c = Counter(M)

sum = 0
for i in c.values():
    sum += i * (i - 1) / 2

print(int(sum))
### BUGGY CODE ###

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

B = []
for i in range(0,N-1,1):
  a = 0
  for j in range(i+1,N,1):
    if A[i] == A[j]:
      a += 1
  B.append(a)

C = []
for i in range(N):
  b = 0
  for j in range(N):
    if A[i] == A[j]:
      b += 1
  C.append(b-1)
  
for i in range(N):
  print(sum(B)-C[i])
      
    

### FIXED CODE ###

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

B = [0]*N
for i in range(N):
  B[A[i]-1] += 1

s = 0
for i in range(N):
  s += B[i]*(B[i]-1)//2

for i in range(N):
  print(s-B[A[i]-1]+1)