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:
def slove():
    import sys
    import bisect
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    d = collections.defaultdict(list)
    for v in l:
        d[v]
    cnt = 0
    for i in range(1, n - 1):
        for j in range(i + 1, n):
            a, b = [l[i], l[j]]
            tl = [a + b, abs(a - b)]
            mi = min(min(tl) + 1, a - 1)
            ma = min(max(tl) - 1, a - 1)
            if mi < ma:
                pmax = bisect.bisect_right(l, ma)
                pmin = bisect.bisect_right(l, mi)
                if pmax == pmin:
                    cnt += 0
                else:
                    cnt += abs(pmax - pmin)
            elif mi == ma and mi in d:
                cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
n= int(input())
l= list(map(int,input().split()))
l=sorted(l)
count=0
for  i  in range(2,n):
    for j in range(1, i):
        r=range(j-n-1,-n,-1)
        for k in r:
            if l[i]<l[j]+l[k]:
                count+=(n+k+1)
                break
print(count)

Submission status: Wrong Answer

Solution code:
def binary_search(ls,target): 
	result = -1
	left = 0
	right = len(ls) - 1
	while left <= right:
		center = (left + right)//2
		if ls[center] == target:
			result = center-1
			break
		elif ls[center] < target:
			right = center - 1
		elif ls[center] > target:
			left = center + 1
	if result == -1:
		return left
	else:
		return result

from collections import deque
N=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
c=0
for i in range(N-2):
  for j in range(i+1,N-1):
    c+=binary_search(l[j+1:],l[i]-l[j])
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
L=sorted(list(map(int,input().split())))
L_max=L[-1]
memo=[0]*2000
for l in L:
  memo[l-1]+=1
for i in range(1999):
  memo[i+1]=memo[i+1]+memo[i]
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    a=L[i]
    b=L[j]
    if a!=1:
      if a==b:
        ans+=memo[a+b-2]-memo[b-2]-2
      else:
        ans+=memo[a+b-2]-memo[b-2]-1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l_list = list(map(int,input().split()))

l_list.sort()

#print(N)
#print(l_list)


cnt = 0
for a in range(N):
    for b in range(N):
        if a >= b:
            continue
        for c in range(N):
            if b >= c:
                continue
            if l_list[c] > (l_list[a] + l_list[b]):
                break
            #print(a, b, c, ", ", l_list,)
            if l_list[a] < (l_list[b] + l_list[c]) and \
                l_list[b] < (l_list[c] + l_list[a]) and \
                l_list[c] < (l_list[a] + l_list[b]):
                cnt = cnt + 1
                print("!")
            #if not a == b or b == c or c == a:

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [int(i) for i in input().split()]

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j
        right = L_len
        center = int((left+right)/2)

        while right - left <= 1:
            if L[i] < L[j] + L[center]:
                left = center + 1
            else:
                right = center - 1
            
        count += right - center
print(count)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j = i - 1
    while j and a[j - 1] + a[j] > a[i]:
        r += j - bisect_right(a, i - j, 0, j)
        j -= 1
print(r)

Submission status: Wrong Answer

Solution code:
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
A=0
for i in range(n):
    a=l[i]
    for j in range(bisect(l,a),n):
        b=l[j]
        k1=max(j+1,bisect(l,b-a))
        k2=bisect_left(l,a+b)
        A+=k2-k1
print(A)

Submission status: Wrong Answer

Solution code:
import itertools

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

count = 0
for a, b, c in list(itertools.combinations(L, 3)):
  if a + b > c and b + c > a and c + a > b:
    count += 1

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
stick = sorted(list(set(list(map(int, input().split())))), reverse=True)
combi = itertools.combinations(stick, 3)
ans = 0
lst = []
flag = False
for a,b,c in combi:
  if flag and a in lst:
    continue
  else:
    flag = False
  if a < b+c and b < c+a and c < a+b:
    ans += 1
  else:
    flag = True
    lst.append(a)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
l=[int(_) for _ in input().split()]
l=set(l)
c_l=list(itertools.combinations(l,3))
cnt=0
for i in range(len(c_l)):
	if c_l[i][0]<c_l[i][1]+c_l[i][2] and c_l[i][1]<c_l[i][0]+c_l[i][2] and c_l[i][2]<c_l[i][0]+c_l[i][1]:
		cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
listl = [int(i) for i in input().split()]

listl.sort(reverse=True)

print(listl)
ans=0
if n==3:
	if listl[0]>=listl[1]+listl[2]:
		ans=0
	else:
		ans=1
else:
	for i in range(n-2):
		for j in range(i+1,n-1):
			for k in range(j+1,n):
				if listl[i]<listl[j]+listl[k]:
					#print(listl[i],listl[j],listl[k])
					ans=ans+1
					
print(ans)

Submission status: Wrong Answer

Solution code:
def check(a,b,c):
    if a < b+c:
        if b < a+c:
            if c < a+b:
                return 1
    return 0

N = int(input())
l = input()
l = list(set(list(map(int,l.split()))))
N = len(l)
#all_list = list()
all_num = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            all_num += check(l[i],l[j],l[k])
            #if check(l[i],l[j],l[k]) == 1:
                #all_list.append([l[i],l[j],l[k]].sort)
#print(len(all_list))
print(all_num)

Submission status: Wrong Answer

Solution code:
import sys


def main():
    n = int(sys.stdin.readline().strip())
    l = [int(s) for s in sys.stdin.readline().strip().split()]

    table = [0] * 1001

    for i in range(n):
        length = l[i]
        table[length] += 1

    total_so_far = [0] * 1001
    total = 0
    for i in range(1001):
        total += table[i]
        total_so_far[i] = total

    def aC2(a):
        return a * (a-1) // 2

    # [l1, l2)
    def range_total(l1, l2):
        assert l1 > 0
        if l2 > 1001:
            l2 = 1001
        return total_so_far[l2-1] - total_so_far[l1-1]

    ans = 0
    for i in range(1, 1001):
        if table[i] == 0:
            continue
        if table[i] > 1:
            a_plus_b = 2 * i
            inc = aC2(table[i]) * (range_total(i, a_plus_b) - 2)
            ans += inc

        for j in range(i+1, 1001):
            if table[j] == 0:
                continue
            if table[j] > 1:
                inc = aC2(table[j])
                ans += inc
            a_plus_b = i + j
            inc = range_total(j+1, a_plus_b)
            ans += inc
    print(ans)


main()

Submission status: Wrong Answer

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

l.sort()
l.reverse()
c = 0
f = 0
for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            #print(l[i],l[j],l[k])
            if l[i] < l[j]+l[k]:
                c += 1
            else:
                #print('********')
                f = 1
                break
        if f ==1:
            f = 0
            break
        

print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
b = 0
l.sort()
l.reverse()
while len(l) >= 3:
  b = 0
  max_length = l.pop(0)
  for i in range(len(l)-1):
    if b == 1:
      break
    for j in range(i+1,len(l)):
      if max_length < l[i] + l[j]:
        ans += 1
      else:
        b = 1
        break
        
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
 
N = int(input())
edge = list(map(int, input().split()))
edge.sort()
counter = 0

for i in range(len(edge) - 2):
    A = edge[i]
    for k in range(i + 1, len(edge) - 1):
        B = edge[k]
        Cmin = max(A - B, B - A)
        Cmax = A + B
        foot = [x for x in edge if x < Cmax]
        head = [y for y in edge if y > Cmin]
        counter += min(len(foot), len(head)) - 2
print(counter)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import bisect
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    d = collections.defaultdict(list)
    for v in l:
        d[v]
    cnt = 0
    for i in range(1, n - 1):
        for j in range(i + 1, n):
            a, b = [l[i], l[j]]
            tl = [a + b, abs(a - b)]
            mi = min(tl) + 1
            ma = min(max(tl) - 1, a - 1)
            if mi < ma:
                pmax = bisect.bisect_right(l, ma)
                pmin = bisect.bisect_right(l, mi)
                if pmax == pmin:
                    cnt += 0
                else:
                    cnt += abs(pmax - pmin)
            elif mi == ma and mi in d:
                cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys


def create_triple(triple, L):
  if len(triple)==3:
    if triple[0]<triple[1]+triple[2] and triple[1]<triple[0]+triple[2] and triple[2]<triple[0]+triple[1]:
      ans.append(triple)
  
  for l in L:
    if l in triple: break
    create_triple(triple+[l], L)
    

N = int(sys.stdin.readline().strip())
L = [int(n) for n in sys.stdin.readline().strip().split()]

L.sort()
ans = []
create_triple([], L)
print(len(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l=sorted(l,reverse=True)

ans=0


for i in range(n-2):
  flag=False
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if l[i]<l[j]+l[k]:
        ans+=1
      else:#それ以降のj,kで三角形作れるのは無いので最初のfor文に戻る
        flag=True
    if flag==True:
      break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
L=sorted(L, reverse=True)
count=0
for i in range(N-2):
    for j in range(i+1,N-1):
        #if add<L[i]:
        #    break
            
        for k in range(j+1,N):
            add  = L[j]+L[k]
            #diff = L[j]-L[k]
            #(diff>=L[i]) or
            if  (add<=L[i]):
                #print(L[i],L[j],L[k],diff<L[i],add>L[i])
                break
                
            #print(L[i],L[j],L[k],diff<L[i],add>L[i])
            count += 1
            
        else:
            if add<L[i]:
                break
            else:
                continue
        break
print(count)

Submission status: Wrong Answer

Solution code:
import heapq

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

h = []

for i in range(N):
    heapq.heappush(h, L[i])

triangles = 0

for i in range(N-2):
    first = h[i]
    for j in range(i+1, N-1):
        second = h[j]
        fs = first + second

        if second > first + h[N-1]:
            continue

        for k in range(j+1, N):
            third = h[k]
            if second >= first + third:
                continue
            if third >= fs:
                break
            triangles = triangles + 1

print(triangles)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
a=0
b=0
c=0
cnt=0
my_bool=False
for i in range(n-2):
    for j in range(i+1,n-1):
        for m in range(j+1,n):
            if l[i]<l[j]+l[m]:
                #print(l[i],l[j],l[m])
                cnt+=1
            else:
                my_bool=True
                break
        if my_bool:
            my_bool=False
            break
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect
def main():
    _ = int(input())
    l = list(map(int,input().split()))
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    dk = sorted(list(d.keys()))
    dk2 = [i*2 for i in dk]
    ans = 0
    while dk:
        x = dk.pop(-1)
        _ = dk2.pop(-1)
        bs = bisect.bisect(dk2,x)
        tmp = dk[bs:]
        for i in tmp:
            for j in dk:
                if i+j > x and i >= j:
                    if i == j:
                        ans += d[x]*d[i]*(d[i]-1)
                    else:
                        ans += d[x]*d[i]*d[j]
        if len(dk) < 3:
            break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
import bisect
for i in range(n):
    for j in range(n):
        if i<j:
            a=l[i]
            b=l[j]
            x=bisect.bisect_left(l,a+b)
            y=bisect.bisect_right(l,b-a)
            if b-a>a:
                ans+=max(0,x-y-2)
            else:
                ans+=max(0,x-y-1)
print(ans//3)

Submission status: Wrong Answer

Solution code:
#D
import sys 
input = sys.stdin.readline
import heapq
def main():
    n = int(input())
    L = list(map(int, input().split()))
    ans = 0
    L.sort()
    if L[0] == L[-1]:
        print(1)
    else:
        for i in range(n-2):
            if L[i] == L[i-1]:
                continue
            else:
                for j in range(i+1, n-1):
                    Z = set(L)
                    if L[i] == L[j]:
                        Z.remove(L[i])
                    else:
                        Z.remove(L[i])
                        Z.remove(L[j])
                    ans += len(list(filter(lambda x:L[j] <= x < L[i]+L[j], Z)))
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
cnt = [0]*(3*10**3+1)
for i in range(N):
    cnt[L[i]] += 1
cum = [0]*(3*10**3+1)
for i in range(3*10**3):
    cum[i+1] = cum[i]+cnt[i+1]

ans = 0
for a in range(1,10**3+1):
    if cnt[a] == 0:
        continue
    for b in range(a+1,10**3+1):
        if cnt[b] == 0:
            continue
        ans  += (cum[a+b-1]-cum[b])*cnt[a]*cnt[b]

for a in range(10**3+1):
    if 2*a-1 > 10**3:
        break
    if cnt[a] < 2:
        continue
    ans += ((cum[2*a-1]-cnt[a])*cnt[a]*(cnt[a]-1))//2

for a in range(10**3+1):
    if cnt[a] < 3:
        continue
    ans += (cnt[a]*(cnt[a]-1)*(cnt[a]-2))//6

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
l=map(int,input().split())
L=list(l)
a=0
for i in range(N):
 for j in range(N):
  if L[i]>L[j]:
   for k in range(N-j-1):
    if L[i]>L[k+j+1]:
     if L[i]<L[j]+L[k+j+1]:
      a+=1
print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from bisect import bisect_left, bisect, insort_left, insort
def solve():
    n = int(input())
    L = sorted(map(int, input().split()))
    SET = set(L)
    len_s = len(SET)
    SS = sorted(SET)
    C = Counter(L)
    ans = 0
    for i in range(0, len_s - 2):
        if C[SS[i]] >= 3:
            ans += 1
        if C[SS[i]] >= 2:
            ans += bisect_left(SS, SS[i] * 2) - i - 1
        for j in range(i + 1, len_s - 1):
            ans += bisect_left(SS, SS[i] + SS[j]) - j - 1
    print(ans)
solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False
input = sys.stdin.readline
N  = int(input())
Ls = list(map(int, input().split()))
if em: sys.stdin = open('/dev/tty')
L_map = [0 for _ in range(1000 + 1)]
for L in Ls:
  L_map[L] += 1
ans = 0
for i in range(1, 1000):
  for j in range(i+1, 1000 + 1):
    a, b = L_map[i], L_map[j]
    ans += a * b * sum(L_map[max(j-i+1, j+1):i+j])
print(ans)

Submission status: Wrong Answer

Solution code:
def binary_search(ls,target): 
	result = -1
	left = 0
	right = len(ls) - 1
	while left <= right:
		center = (left + right)//2
		if ls[center] == target:
			result = center
			break
		elif ls[center] < target:
			right = center - 1
		elif ls[center] > target:
			left = center + 1
	if result == -1:
		return left
	else:
		return result

from collections import deque
N=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
c=0
for i in range(N-2):
  for j in range(i+1,N-1):
    c+=binary_search(l[j+1:],l[i]-l[j])
print(c)

Submission status: Wrong Answer

Solution code:
import sys


def create_triple(triple, L):
  if len(triple)==3 and triple not in ans:
    if triple[0]<triple[1]+triple[2] and triple[1]<triple[0]+triple[2] and triple[2]<triple[0]+triple[1]:
      ans.append(triple)
  
  for l in L:
    if l in triple: break
    create_triple(triple+[l], L)
    

N = int(sys.stdin.readline().strip())
L = [int(n) for n in sys.stdin.readline().strip().split()]

L.sort()
ans = []
create_triple([], L)
print(len(ans))

Submission status: Wrong Answer

Solution code:
n  = int(input())
ls = sorted([int(x) for x in input().split()])
ts = set()

for i in range(0, n -2):
	a = ls[i]
	for j in range(i + 1, n - 1):
		b = ls[j]
		c = ls[j + 1]
		
		if a + b <= c or a + c <= b:
			break
		
		ts.add((a, b, c))
		
		for k in range(j + 2, n):
			c = ls[k]
		
			if a + b <= c or a + c <= b:
				break
		
			ts.add((a, b, c))
		
print(ts)

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)[::-1]

ans = 0
for i in range(n):
    max_l = l[i]
    for j in range(i+1, n):
        next_l = l[j]
        ans += n-j-1 - bisect.bisect_left(l[j+1:n][::-1], (max_l-next_l))
print(ans)

Submission status: Wrong Answer

Solution code:
def isTriangle(x,y,z):
    return x+y>z 

n = int(input())

l = [int(x) for x in input().split()]
l.sort()
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1, N):
      if (L[i]+L[j]>L[k]) and (L[k]>L[j]-L[i]) and (L[k]>L[i]-L[k]):
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
flagC = False

preL = l[0]
l2 = []
l2Cnt = 1
for i in range(1,n):
	if preL == l[i]:
		l2Cnt += 1
	else:
		l2.append([l[i-1], l2Cnt])
		l2Cnt = 1
		preL = l[i]

l2.append([l[n-1], l2Cnt])

# print(l)
# print(l2)
n = len(l2)

# c-b < a
for i in range(n):
	a = l2[i][0]
	for j in range(i+1,n):
		b = l2[j][0]
		if b < a: break
		for k in range(j+1,n):
			c = l2[k][0]
			# print(a,b,c)
			if c - b < a: cnt += l2[i][1] * l2[j][1] * l2[k][1]
			else:
				flagC = True
				break

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
from itertools import accumulate
def main():
    N = int( input())
    L = list( map( int, input().split()))
    d = defaultdict( int)
    ans = 0
    
    for l in L:
        d[l] += 1
    L.sort()
    R = list( set(L))
    R.sort()

    for e in d:
        if d[e] >= 3:
            ans += 1
        if d[e] >= 2:
            cnt = 0
            for l in R:
                if l == e:
                    continue
                if l >= 2*e:
                    break
                cnt += 1
            ans += cnt
    T = [0]*(10**4)
    M = len(R)
    Z = [0]*(10**4)
    for i in range(M):
        for j in range(i+1, M):
            e = R[i]
            f = R[j]
            T[ abs(e-f)+1] += 1
            T[ e+f] -= 1
            if abs(e-f) < e:
                Z[e] += 1
            if abs(e-f) < f:
                Z[f] += 1
    S = list( accumulate(T))
    a = 0
    for e in d:
        if S[e] > 0:
            a += S[e] - Z[e]
    print(ans+a//3)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
A=k=0
for i in range(n):
    a=l[i]
    for j in range(bisect(l,a),n):
        b=l[j]
        k1=bisect(l,b-a)
        k2=bisect(l,a+b)
        A+=max(0,k2-k1-(k1<=i<=k2)-(k1<=j<=k2))
print(A//3)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l=sorted(l,reverse=True)

anset=set()


for i in range(n-2):
  flag=False
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if l[i]<l[j]+l[k]:
        temp=""
        temp+=str(l[i])+str(l[j])+str(l[k])
        #print(temp)
        anset.add(temp)
      else:#それ以降のj,kで三角形作れるのは無いので最初のfor文に戻る
        flag=True
    if flag==True:
      break

#ansetの長さを出力
print(len(anset))

Submission status: Wrong Answer

Solution code:
import collections
import heapq
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
nums = list(map(int,input().split(" ")))
nums.sort()
ans = 0
di = collections.defaultdict(lambda :0)
di2= collections.defaultdict(lambda : collections.defaultdict(lambda :0))
for i in range(N):
    for j in range(i+1,N):
        key = nums[i] + nums[j]
        di[key] += 1
        di2[i][key] += 1
co = 0
for k,v in di.items():
    for temp in nums:
        if k <= temp:
            co += 1

temp = combinations_count(N,3)
print(temp- co)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()])

ans = 0
for i in range(N - 2):
    j, k = i + 1, i + 2
    a = L[i]

    while True:
        if k == N:
            break

        b, c = L[j], L[k]
        if abs(b - c) >= a or b + c <= a:
            if k - j == 1:
                k += 1
            else:
                j += 1
            continue

        ans += 1
        if k - j == 1:
            k += 1
        else:
            j += 1

print(ans)

Submission status: Wrong Answer

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

R = []
for i in L:
    for j in L:
        if i == j or i > j:
            continue
        for k in L:
            if i == k or j == k:
                continue
            if i < j+k and j < i+k and k < i+j:
                r = {i, j, k}
                if not r in R:
                    R.append(r)

print(len(R))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
cnt = [0]*(3*10**3+1)
for i in range(N):
    cnt[L[i]] += 1
cum = [0]*(3*10**3+1)
for i in range(3*10**3):
    cum[i+1] = cum[i]+cnt[i+1]

ans = 0
for a in range(1,10**3+1):
    if cnt[a] == 0:
        continue
    for b in range(a+1,10**3+1):
        if cnt[b] == 0:
            continue
        ans  += (cum[a+b-1]-cum[b])*cnt[a]*cnt[b]

for a in range(5*10**2+1):
    if cnt[a] < 2:
        continue
    ans += ((cum[2*a-1]-cnt[a])*cnt[a]*(cnt[a]-1))//2

for a in range(10**3+1):
    if cnt[a] < 3:
        continue
    ans += (cnt[a]*(cnt[a]-1)*(cnt[a]-2))//6

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j = i - 1
    while j >=0 and a[j - 1] + a[j] > a[i]:
        r += j - bisect_right(a, i - j, 0, j)
        j -= 1
print(r)

Submission status: Wrong Answer

Solution code:

N = int(input())
D = input().split(' ')

D.sort(reverse=True)

count = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N - j - 1):
            
            if int(D[i]) < int(D[j]) + int(D[N - k - 1]):
                # print(f"{D[i]} {D[j]} {D[N - k - 1]}")
                count += 1


print(count)

Submission status: Wrong Answer

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

count = 0
for i in range(N):
  for j in range(i + 1, N):
    x = L[i] + L [j]
    y = L[j]
    count += 1
    #print(count)
print(count)

Submission status: Wrong Answer

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

from collections import Counter
c = Counter(l2)
k = list(c.keys())
k.sort()

from bisect import bisect_left, bisect_right
l2.sort()

ans = 0
for i in range(len(k)):
    for j in range(i+1, len(k)):
        c_min = k[j] - k[i]
        c_max = k[i] + k[j]

        idx = bisect_right(l2, k[j])
        tmp = l2[idx:]

        idx_min = bisect_left(tmp, c_min)
        idx_max = bisect_left(tmp, c_max)

        if idx_min < len(tmp) and tmp[idx_min] == c_min:
            idx_min += 1

        ans += max(0, idx_max - idx_min) * c[k[i]] * c[k[j]]

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for ia, a in enumerate(L):
    for ib, b in enumerate(L[ia+1:], ia):
        print(L, a+b, bisect_left(L, a+b) - ib - 2)
        ans += bisect_left(L, a+b) - ib - 2

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
nums = list(map(int, input().split()))
nums.sort()
print(([num for num in list(itertools.combinations(nums, 3)) if num[0] + num[1] > num[2]]))

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
count = 0
x = False
for i in range(n-2):
  for j in range(i+1, n-1):
    if x:
      x = False
      break
    for k in range(j+1, n):
      if l[i] < l[j]+l[k]:
        count += 1
      else:
        x = True
        break

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[int(i) for i in input().split()]
L.sort()
count=0
for i in range(N-2):
    a=0
    for j in range(i+1,N-1):
        l=L[i]+L[j]
        if l<=L[j+1]:
            a=0
        elif l>L[N-1]:
            a=1
            break
        else:
            for k in range(j+1,N):
                if l>L[k]:
                    count+=1
                else:
                    break
    if a==1:
        b=N-j-1
        B=(b+1)*b//2
        count=count+B
    print(count)
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(sorted(map(int, input().split())))

memo = [0]*1001
for x in range(1001):
    for i in arr:
        if i < x:
            memo[x] += 1

result = 0
for a in range(N):
    for b in range(a+1, N):
        aa = arr[a]
        bb = arr[b]
        if aa+bb > 1000:
            result += N-b-1
        else:
            result += memo[aa+bb]-b-1
        print(a,b,result)

print(result)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = [int(i) for i in input().split()]
print(sum(1 for x in range(a) for y in range(a) for z in range(a) if x!=y and y!=z and x!=z and (b[x]<b[y]+b[z] and b[y]<b[x]+b[z] and b[z]<b[x]+b[y]))/6)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [int(i) for i in input().split()]
L.sort()
M = [0 for i in range(2*10**3+1)]
C = [0 for i in range(2*10**3+1)]
for i in range(N):
    M[L[i]] += 1
for i in range(1, len(M)):
    C[i] = C[i-1] + M[i]
cnt = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        a = L[i]
        b = L[j]
        # abs(a-b) < c < a+b
        cnt += C[a+b-1] - C[max(abs(a-b), b)]
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
from itertools import accumulate
def main():
    N = int( input())
    L = list( map( int, input().split()))
    d = defaultdict( int)
    ans = 0
    
    for l in L:
        d[l] += 1
    L.sort()
    R = list( set(L))
    R.sort()

    for e in d:
        if d[e] >= 3:
            ans += 1
        if d[e] >= 2:
            cnt = 0
            for l in R:
                if l == e:
                    continue
                if l >= 2*e:
                    break
                cnt += 1
            ans += cnt
    T = [0]*(10**4)
    M = len(R)
    Z = [0]*(10**4)
    for i in range(M):
        for j in range(i+1, M):
            e = R[i]
            f = R[j]
            T[ abs(e-f)+1] += 1
            T[ e+f] -= 1
            if abs(e-f) < e:
                Z[e] += 1
            if abs(e-f) < f:
                Z[f] += 1
    S = list( accumulate(T))

    for e in d:
        if S[e] > 0:
            ans += S[e] - Z[e]
    print(ans//3)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [int(i) for i in input().split()]


from itertools import combinations

ans = 0
for a, b, c in combinations(L,3):
    if a<b+c or b<a+c or c<a+b:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,*l=map(int,open(0).read().split())
l.sort()
print(l)
a=0
for i in range(n):
    for j in range(n):
        for k in range(n):
            print(i,j,k)
            if i==j or j==k or k==i:
                continue
            if l[k]>=l[i]+l[j]:
                break
            if l[i]<l[j]+l[k] and l[j]<l[k]+l[i] and l[k]<l[i]+l[j]:
                a+=1
print(a//6)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
from bisect import bisect_left,bisect_right
l.sort()
a=0
for i in range(n-2):
  for j in range(i+1,n-1):
    a+=bisect_left(l,l[i]+l[j])-bisect_right(l,max(l[j]-l[i],l[j]))
print(a)

Submission status: Wrong Answer

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

ans = 0
for i in range(N-2):
    A = L[i]
    for j in range(i+1,N-1):
        B = L[j]
        for k in range(j+1,N):
            C = L[k]
            if A < B + C and B < C + A and C < A + B:
                ans += 1
                print(A,B,C)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#ABC143D
import sys
import math
import collections
#sys.setrecursionlimit(100000)


n=int(input())

tmp = input().split()
l = list(map(lambda a: int(a), tmp))

sum=0

#print(l)
l.sort()
#print(l)

for i in range(n):
	for j in range(i+1,n):
		tmp1 = l[i]+l[j]
		tmp2 = abs(l[i]-l[j])
		for k in range(j+1,n):
			if(tmp1<l[k]):
				break
			if(tmp2<l[k]):
				sum+=1
print(sum)

Submission status: Wrong Answer

Solution code:
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
A=k=0
for i in range(n):
    a=l[i]
    for j in range(bisect(l,a),n):
        b=l[j]
        k1=bisect(l,b-a)
        k2=bisect_left(l,a+b)
        A+=k2-k1-(k1<=i<=k2)-(k1<=j<=k2)
print(A//3)