Problem:


Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from itertools import combinations_with_replacement

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

li = list(combinations_with_replacement(l, 2))
li = [x for x in li if x[0] != x[1] and x[1] != max(l)]

ans = 0
for small, mid in li:
    right = bisect_left(l, small + mid)
    left = bisect_left(l, mid + 1)
    ans += max(right - left, 0)

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from itertools import combinations_with_replacement

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

li = list(combinations_with_replacement(l, 2))
li = [x for x in li if x[0] != x[1]]

ans = 0
for small, mid in li:
    right = bisect_right(l, small + mid)
    left = bisect_left(l, mid)
    if right > left + 1:
        ans += right - left - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n=int(input())
l=list(map(int,input().split()))
l.sort()
cnt=0

# print(l)

# print()

for i in range(n-1,1,-1):
    for j in range(i-1, 0, -1):
        jogen=l[j]-1
        kagen=l[i]-l[j]+1
        if jogen>=kagen:
            idx_jogen=bisect.bisect_right(l,jogen)
            idx_kagen=bisect.bisect_left(l,kagen)
            cnt+=idx_jogen-idx_kagen
            # print(l[i], l[j], jogen, kagen, idx_jogen,idx_kagen,cnt)

print(cnt)

Submission status: Wrong Answer

Solution code:
import bisect

n=int(input())
l=list(map(int,input().split()))
l.sort()
cnt=0

# print(l)

# print()

for i in range(n-1,1,-1):
    for j in range(i-1, 0, -1):
        jogen=l[j]
        kagen=l[i]-l[j]+1
        if jogen>=kagen:
            idx_jogen=bisect.bisect_right(l,jogen-1)
            idx_kagen=bisect.bisect_right(l,kagen)
            cnt+=abs(idx_jogen-idx_kagen)
            # print(l[i], l[j], jogen, kagen, idx_jogen,idx_kagen,cnt)

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
*L, = map(int, input().split())

L.sort()

def check(a,b,c):
    if a<b+c and b<a+c and c<a+b:
        return True
    else:
        return False

ans = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        right = N
        left  = b
        while (right-left)>1:
            mid = (right+left)//2
            if check(L[a], L[b], L[mid]):
                right = mid
            else:
                left  = mid

        if right!=N:
            ans = ans + N-right

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

maxL = 10**3

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

nums = [0] * (2*maxL+1)
for L in Ls:
    nums[L] += 1

accNums = list(accumulate(nums))

ans = 0
for A in range(1, maxL):
    if nums[A] == 0: continue
    for B in range(A+1, maxL):
        if nums[B] == 0: continue
        ans += nums[A] * nums[B] * (accNums[A+B-1]-accNums[B])

for A in range(1, maxL):
    if nums[A] < 2: continue
    ans += nums[A] * (nums[A]-1) // 2 * (accNums[2*A-1]-nums[A])

for A in range(1, maxL):
    if nums[A] < 3: continue
    ans += nums[A] * (nums[A]-1) * (nums[A]-2) // 6

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort(reverse = 1)
ans = 0
for i in range(n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        if b * 2 < a:
            break
        for k in range(j+1,n):
            c = l[k]
            if c < a - b:
                break
            if a-b<c:
                ans+=1
                print(a,b,c)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

print(l)
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = l[i]
        b = l[j]
        idx = bisect.bisect_left(l, a + b)
        if idx > j: ans += idx - j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

def cond(length, a, b):
    if length < a + b and a < length + b and b < a + length:
        return True
    else:
        return False

print(l)
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = l[i]
        b = l[j]
        idx = bisect.bisect_left(l, a + b)
        if idx > j: ans += idx - j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

def cond(length, a, b):
    if length < a + b and a < length + b and b < a + length:
        return True
    else:
        return False
print(l)
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = l[i]
        b = l[j]
        if not cond(l[-1], a, b): continue
        se = [0] + l[j+1:] + [10000]
        left = 0
        right = n-j
        while right - left > 1:
            print(i, j, right, left)
            tmp = (right + left) // 2
            if cond(se[tmp], a, b):
                right = tmp
            else:
                left = tmp
        ans += (n-j-2) - left + 1
print(ans)

Submission status: Wrong Answer

Solution code:
"""
素直に実装するとN**9
"""

import itertools

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


count = 0
for a in L:
    l = L[a:]
    comb = list(itertools.combinations(L, 2))
    for b,c in comb:
        if abs(b-c) < a < b+c:
            count += 1
print(count)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()
cnt = 0

for i in range(N-2):
  for j in range(i+1, N-1):
    k = j + 1
    while L[k-1] == L[k]:
      if k == N-1:
        k = j + 1
        break
      k += 1
    cnt += bisect_left(L, L[i]+L[j]) - k

print(cnt)

Submission status: Wrong Answer

Solution code:
# ３重ループではTLE
# a < b < c & c < a + b

import bisect
N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0

print(L)

for i in range(N):
    for j in range(i+1,N):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L,t)
        ans += max(0, idx-j-1) # c < a + b の範囲を総取り
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
l  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

l = sorted(l)
#print(l)

ans = 0
for i1 in range(len(l)-2):
    a = l[i1]
    if a >= l[i1+1] + l[i1+2]:
        continue

    for i2 in range(i1+1, len(l)-1):
        b = l[i2]
        if b >= a + l[i2+1]:
            continue

        for i3 in range(len(l)-1, i2, -1):
            if l[i3] < a + b:
                #print(a, b, l[i3])
                ans += 1
            else:
                break

        #l_ = [l[i3] for i3 in range(i2+1, len(l[i2+1:]))]

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
l  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

l = sorted(l)[::-1]
print(l)

ans = 0
for i1 in range(len(l)-2):
    a = l[i1]
    if a < l[i1+1] + l[i1+2]:
        continue
    for i2 in range(i1+1, len(l)-1):
        b = l[i2]
        if b < a + l[i2+1]:
            continue

        l_ = [l[i3] for i3 in range(i2+1, len(l[i2+1:]))]

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
l  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

l = sorted(l)[::-1]

ans = 0
for i1 in range(len(l)-2):
    a = l[i1]
    for i2 in range(i1+1, len(l)-1):
        b = l[i2]
        l_ = [l[i3] for i3 in range(i2+1, len(l[i2+1:]))]

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    for j in range(i+1, N):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-j-1)
        print(i, j, idx, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
data = [0 for i in range(10**3+1)]
ans = 0
for i in map(int,input().split()):
    data[i] += 1
D = list(accumulate(data))
for i in range(1,10**3-1):
    for j in range(i,10**3):
        if j == i:
            if data[i] >= 3:
                ans += data[i]*(data[i]-1)*(data[i]-2)//6
            if data[i] >= 2:
                ans += data[i]*(data[i]-1)//2*(D[min(i+j-1,10**3)]-D[j])
        else:
            ans += data[i]*data[j]*(D[min(i+j-1,10**3)]-D[j])
            if data[j] >= 2:
                ans += data[i]*data[j]*(data[j]-1)//2
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
data = [0 for i in range(10**3+1)]
ans = 0
for i in map(int,input().split()):
    data[i] += 1
D = list(accumulate(data))
for i in range(1,10**3-1):
    for j in range(i,10**3):
        if j == i:
            if data[i] >= 3:
                ans += data[i]*(data[i]-1)*(data[i]-2)//6
            if data[i] >= 2:
                ans += data[i]*(data[i]-1)//2*(D[min(i+j-1,10**3)]-D[j])
        else:
            ans += data[i]*data[j]*(D[min(i+j-1,10**3)]-D[j])
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
listL=list(map(int,input().split()))
listL.sort()
ans=0
for i in range(N-1,2,-1):
  for k in range(i-1,1,-1):
    t=k//2
    d=listL[i]-listL[k]
    while (d-listL[t]<=0 or d-listL[t-1]>0) and t!=0 and t!=k-1:
      third=listL[t]
      if d-third>0:
        t=t//2
      else:
        t=t+(k-t)//2
    if t<k:    
      ans+=k-t
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        print(L[i],L[j],left,right)
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        print(-bisect.bisect_right(L,left)+bisect.bisect_left(L,right))
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count//3)

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        print(L[i],L[j],left,right)
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        print(-bisect.bisect_right(L,left)+bisect.bisect_left(L,right))
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count/3)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return mid-1
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        ans += max(0,binary_search(l,l[i] + l[j+1]-1)-(j+1))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return mid-1
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        ans += max(0,binary_search(l,l[i] + l[j+1]-1)-(j+1))
s = [2,3,4,5,6,8]
print(binary_search(s,7))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return mid
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        ans += max(0,binary_search(l,l[i] + l[j+1]-1)-(j+1))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
ll = [[0 for i in range(N-1)] for j in range(N-1)]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return mid
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        ll[i][j] = l[i] + l[j+1]
        ans += max(0,binary_search(l,ll[i][j]-1)-(j+1))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
ll = [[0 for i in range(N-1)] for j in range(N-1)]
l.sort()
lll = [[0 for i in range(N-1)] for j in range(N-1)]
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return mid
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        ll[i][j] = l[i] + l[j+1]
        lll[i][j] = max(0,binary_search(l,ll[i][j])-(j+1))
        ans += max(0,binary_search(l,ll[i][j])-(j+1))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
L=list(map(int,input().split()))
L.sort()
ans=0
for i in range(N-2):
 mid =i+1
 l=0
 r=i+2
 while l<mid-1 or r<N-1:
  if L[r]-L[l]<L[mid]:
   ans+=1
   if r==N-1:
    l+=1
   else:
    r+=1
  else:
   l+=1
 if L[r]-L[l]<L[mid]:
  ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
print(L)
ans=0
for i in range(0,N-1):
    for j in range(i+1,N):
        D=L[i]+L[j]
        ans+=bisect.bisect_left(L[j+1:],D)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
from itertools import combinations

ans = 0
for a, b, c in combinations(L, r=3):
    ans += a + b < c
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from numba import njit

def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] > A[i] + A[j])
  return cnt

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('f', '(i4[::1],)')(f)
    cc.compile()

if sys.argv[-1] == 'ONLINE_JUDGE':
  cc_export()
  exit()
from my_module import f

N = int(input())
A = np.array(input().split(), np.int32)
A.sort()
print(f(A))

Submission status: Wrong Answer

Solution code:
import numpy as np
from numba import njit

@njit('(i4[::1],)', cache=True)
def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] > A[i] + A[j])
  return cnt

N = int(input())
A = np.array(input().split(), np.int32)
A.sort()
print(f(A))

Submission status: Wrong Answer

Solution code:
import math
import itertools

N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
cnt = 0
def search(n, m, l):
    left = 0
    right = len(l)-1
    mid = 0
    while right >= left:
        mid = left + (right - left) // 2
        if l[mid] + m >= n:
            left = mid + 1
        elif l[mid] + m < n:
            right = mid - 1
    
    if l[mid] + m > n: 
        return mid+1
    else:
        return mid

for i in range(N):
    for j in range(i+1, N):
        if len(L[j+1:N]) > 0:
            cnt += search(L[i], L[j], L[j+1:N])
        
        

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
r = [0 for _ in range(1000)]
for i in l:
    r[i-1] += 1
tr = []
for i in range(1000):
    if r[i]:
        tr.append([i+1,r[i]])
def triangle(a,b,c):
    if a < (b+c) and b < (a+c) and c < (a+b):
        return True
    else:
        return False
ans = 0
for i in range(len(tr)-2):
    for j in range(i+1,len(tr)-1):
        for k in range(j+1,len(tr)):
            if triangle(tr[i][0],tr[j][0],tr[k][0]):
                ans += tr[i][1]*tr[j][1]*tr[k][1]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
def find_min_fuka_idx(lst, e1, e2):
  left = 0
  right = len(lst)
  while right-left>1:
    mid = (left+right)//2
    e3 = lst[mid]
    if e1+e2 > e3 and e2+e3>e1 and e3+e1>e2:
      left = mid
    else:
      right = mid
  return right
c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += find_min_fuka_idx(a[j+1:], i, j)
print(c)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import*
import collections

def main():
    N = int(stdin.readline().rstrip())
    L = [int(x) for x in stdin.readline().rstrip().split()]
    L.sort()
    A = 0

    S = collections.Counter(L)
    K = list(S.keys())
    V = list(S.values())
    NS = len(K) 
    S2 = []
    
    #3つとも異なる
    if len(K) >= 3:
        C = list(combinations(K, 3))
        for i in range(len(C)):
            C[i] = list(C[i])
            C[i].sort()
            if C[i][0] + C[i][1] > C[i][2]:
                A += 1
        
    #3つ同じ　＆　2つ同じ
    if NS != N:
        for i in range(NS):
            if V[i] >= 3:
                A += 1
            if V[i] >= 2:
                S2 += [K[i]]
        C = list(combinations(S2, 2))
        for i in range(len(C)):
            C[i] = list(C[i])
            C[i].sort()
            if C[i][0]*2 > C[i][1]:
                A += 1
     
    print(A)
    
main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ln = list(map(int, input().split()))

ln.sort()

def binary_search(x, r):
    left = r + 1
    right = n - 1
    while left <= right:
        mid = (left + right) // 2
        if ln[mid] >= x:
            right = mid - 1
        else:
            left = mid + 1
    if ln[mid] > x:
        return mid - 1
    else:
        return mid


ans = 0
for a in range(n-2):
    for b  in range(a+1, n-1):
        cmax = ln[b] + ln[a] - 1
        ans += max(0, binary_search(cmax, b) - b)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ln = list(map(int, input().split()))

ln.sort()

def binary_search(x, r):
    left = r + 1
    right = n - 1
    while left < right:
        mid = (left + right) // 2
        if ln[mid] == x:
            return mid
        elif ln[mid] > x:
            right = mid - 1
        else:
            left = mid + 1
    mid = (left + right) // 2
    if ln[mid] > x:
        return mid - 1
    else:
        return mid


ans = 0
for a in range(n-2):
    for b  in range(a+1, n-1):
        cmax = ln[b] + ln[a] - 1
        ans += max(0, binary_search(cmax, b) - b)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ln = list(map(int, input().split()))

ln.sort()

def binary_search(x, r):
    left = r + 1
    right = n - 1
    while left <= right:
        mid = (left + right) // 2
        if ln[mid] == x:
            return mid
        elif ln[mid] > x:
            right = mid - 1
        else:
            left = mid + 1
    if ln[mid] > x:
        return mid - 1
    else:
        return mid


ans = 0
for a in range(n-2):
    for b  in range(a+1, n-1):
        cmax = ln[b] + ln[a] - 1
        ans += max(0, binary_search(cmax, b) - b)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
l=list(set(list(map(int,input().split()))))
ans=0
for i in itertools.combinations(l,3):
	a,b,c=map(int,i)
	if a<b+c and b<c+a and c<a+b:
		ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c]:
        if s[a]!=s[b] and s[b]!=s[c]:
          count += aa[s[a]]*aa[s[b]]*aa[s[c]]
          continue
        if s[a] == s[c]:
          count += aa[s[a]]*(aa[s[a]]-1)*(aa[s[a]]-2)//6
          continue
        if s[a] == s[b]:
          count += aa[s[a]]*(aa[s[a]]-1)*aa[s[c]]//2
          continue
        if s[b] == s[c]:
          count += aa[s[a]]*(aa[s[b]]*aa[s[b]]-1)//2
          continue
        
print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c]:
        if s[a]!=s[b] and s[b]!=s[c]:
          count += aa[s[a]]*aa[s[b]]*aa[s[c]]
          continue
        if s[a] == s[c]:
          count += aa[s[a]]*(aa[s[a]]-1)*(aa[s[a]]-2)//6
          continue
        if s[a] == s[b]:
          count += aa[s[a]]*(aa[s[a]]-1)*aa[s[c]]//2
        if s[b] == s[c]:
          count += aa[s[a]]*(aa[s[b]]*aa[s[b]]-1)//2
        
print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c]:
        if s[a]==s[b] and  aa[s[a]] < 2:
          continue
        if s[b]==s[c] and  aa[s[b]] < 2:
          continue
        if s[a]==s[c] and  aa[s[a]] < 3:
          continue
        count += aa[s[a]]*aa[s[b]]*aa[s[c]]
print(count)