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: Time Limit Exceeded

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

L = sorted(L)[::-1] # descending order

def binary_search(descend_list, threshold):
    # 閾値を超えている要素の数を返す
    length = len(descend_list)
    
    # 最小の数が閾値を超えればリストの長さを返す
    if descend_list[-1] > threshold:
        return length
    
    # 最大の数が閾値を超えなければ0を返す
    if descend_list[0] <= threshold:
        return 0
    
    # 二分探索する 少なくとも一つはthresholdを超える要素があり、少なくとも一つはthresholdを超えない要素がある場合
    upper_bound = length
    lower_bound = 0
    index = (upper_bound - lower_bound) // 2
    
    while upper_bound > lower_bound + 1:
        if descend_list[index] > threshold:
            lower_bound = index
        else:
            upper_bound = index
        
        index = (upper_bound + lower_bound) // 2
    
    return lower_bound + 1

i = 0
ret = 0

while i < N - 2:
    edge1 = L[i]
    for j in range(i + 1, N-1):
        edge2 = L[j]
        _ret = binary_search(L[j+1:], edge1 - edge2)
        if _ret == 0:
            break
        else:
            ret += _ret
    
    i += 1

print(ret)

Submission status: Time Limit Exceeded

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

L = sorted(L)[::-1] # descending order

def binary_search(descend_list, threshold):
    # 閾値を超えている要素の数を返す
    length = len(descend_list)
    
    # 最小の数が閾値を超えればリストの長さを返す
    if descend_list[-1] > threshold:
        return length
    
    # 最大の数が閾値を超えなければ0を返す
    if descend_list[0] <= threshold:
        return 0
    
    # 二分探索する 少なくとも一つはthresholdを超える要素があり、少なくとも一つはthresholdを超えない要素がある場合
    upper_bound = length
    lower_bound = 0
    index = (upper_bound - lower_bound) // 2
    
    while upper_bound > lower_bound + 1:
        if descend_list[index] > threshold:
            lower_bound = index
        else:
            upper_bound = index
        
        index = (upper_bound + lower_bound) // 2
    
    return lower_bound + 1

i = 0
ret = 0

while i < N - 2:
    edge1 = L[i]
    for j in range(i + 1, N-1):
        edge2 = L[j]
        ret += binary_search(L[j+1:], edge1 - edge2)
    
    i += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
count=0
count1=0
a=len(L)
#print(L)
for i in range(a-2):
  for j in range(i+1,a-1):
    for k in range(j+1,a):
      if L[i]<L[j]+L[k]:
        count+=1
        #print(L[i],L[j],L[k])
      else:
         break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
input()
print(sum(1for a,b,c in itertools.combinations(map(int,input().split()),3)if all([a<b+c,b<c+a,c<a+b])))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

count = 0
combinations = itertools.combinations(l,3)
for a,b,c in combinations:
  if a < b+c and b < c+a and c < a+b:
    count += 1
    
print(count)

Submission status: Time Limit Exceeded

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

L.sort()
X=0
for i in range(N):
  for k in range(N-(i+1)):
    j=i+1+k
    s=L[i]+L[j]
    d=max(L[i]-L[j],L[j]-L[i])
    lb=0
    ub=N-1
    while lb<ub:
      mn=(lb+ub)//2
      if L[mn]<=d:
        lb=mn+1
      else:
        ub=mn
    dmin=lb
    
    lb=0
    ub=N-1
    while lb<ub:
      mn=(lb+ub)//2
      if mn*2 != lb+ub:
        mn=mn+1
      if L[mn]>=s:
        ub=mn-1
      else:
        lb=mn
    smax=lb
    a=smax-dmin+1
    if dmin<=i and i<=smax:
      a-=1
    if dmin<=j and j<=smax:
      a-=1
    X+=a
print(int(X/3))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left,bisect
n = int(input())
l = list(int(i) for i in input().split())
l_sort = sorted(l)
count = 0
for i in range(n-2):
	for j in range(i+1, n-1):
		count += bisect_left(l_sort[j+1:], l_sort[i] + l_sort[j])

print(count)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        ok, ng = j, n
        while ng - ok > 1:
            mid = (ok + ng) // 2
            if l[mid] < l[i] + l[j]:
                ok = mid
            else:
                ng = mid
        ans += ok - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
out=0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if a[i]<a[j]+a[k] and a[j]<a[i]+a[k] and a[k]<a[i]+a[j]:
        out=out+1
print(out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[int(i) for i in 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[i]+l[k]>l[j] and l[k]+l[j]>l[i]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ans=0
for i in range(n):
  for j in range(i+1,n):
    for k in range(j+1,n):
      if l[i]+l[j]>l[k]:
        ans+=1
      else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ans=0
for i in range(n):
  for j in range(i+1,n):
    for k in range(j+1,n):
      if l[i]+l[j]>l[k]:
        ans+=1
      else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
li = list(map(int, input().split()))
com_li = list(itertools.combinations(li, 3))
count = 0

for i in com_li:
  if (i[0] < (i[1]+i[2])) and (i[1] < (i[0]+i[2])) and (i[2] < (i[0]+i[1])):
    count += 1
  else:
    continue

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

N = int(input())
L = [int(x) for x in input().split()]

#i+j>k and i+k>j and j+k>i

N_2 = int(N/2)+1

L.sort()

ans=0
for i in range(0, N-1):
	for j in range(1, N-1):
		for k in range(N-1, N_2-3, -1):
			#print(i,j,k,'=',L[i],L[j],L[k])
			if i>=j or j>=k or i>=k:
				#print('skip')
				continue
			elif L[i]+L[j]>L[k]:
				ans+=1
				#print('here')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
import math
import heapq

MOD = 10**9+7

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

ans = 0
for a in range(N-2):
    for b in range(a+1,N-1):
        for c in range(b+1,N):
            if(L[c]<L[a]+L[b]):
                ans+=1
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
import math
import heapq

MOD = 10**9+7

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

ans = 0
for a in range(N-2):
    for b in range(a+1,N-1):
        for c in range(b+1,N):
            if(L[c]<L[a]+L[b]):
                ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        k = bisect.bisect_left(L[j + 1:], a + b)
        ans += k
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
lenL = len(L)
ans = 0
for i in range(lenL-2):
    for j in range(i+1, lenL-1):
        a = L[i]
        b = L[j]
        ind = bisect.bisect_left(L[j+1:], a+b)
        ans += ind
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort()
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 c < a + b:
                ans += 1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

L.sort()

count = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        L_copy = L[:]
        a = L_copy.pop(i)
        b = L_copy.pop(j-1)
        start = j-1
        finish = N-3
        if start != 0:
            while L_copy[start] == L_copy[start-1]:
                start -= 1
                if start == 0:
                    break
        start_first = start
        finish_first = finish
        while start < finish:
            n = finish - start + 1
            if L_copy[start + (n//2)] < a + b:
                start += (n//2)
            else:
                finish = start + (n//2) - 1
        if L_copy[start_first] < a + b:
            count += (finish - start_first + 1)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        short_2hen = l[i] + l[j]
        l_long = l[j+1:n]
        bis = bisect.bisect_right(l_long, short_2hen-1)
        count += bis
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(list(map(int,input().split())),reverse= True)
cnt = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        m = n-1
        M = j+1
        while M <= m:
            if L[i] < L[(m+M)//2]+L[j]:
                cnt += (m+M)//2-M+1
                M = (m+M)//2+1
            else:
                m = (m+M)//2-1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(list(map(int,input().split())),reverse= True)
cnt = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if L[k]+L[j]>L[i]:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l0 = [int(i) for i in input().split()]
l = sorted(l0)
ans = 0

for i in range(2,n):
    for j in range(i-1,0,-1):
        for k in range(j-1,-1,-1):
            if l[i] < l[j] + l[k]:
                ans += 1
            else:
                break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l0 = [int(i) for i in input().split()]
l = sorted(l0, reverse = True)
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]:
                ans += 1
            else:
                break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l0 = [int(i) for i in input().split()]
l = sorted(l0, reverse = True)
ans = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if l[i] < l[j] + l[k]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
p,q,r=0,0,0
p=len(L)*(len(L)-1)*(len(L)-2)//6
L.sort()
z=N%3
for i in range(N-z-1):
  for j in range(i+1,N-z):
    for k in range(i+2,N):
      if L[i]+L[j]<=L[k]:
        q=q+1
print(p-q)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
input = sys.stdin.readline

def main():
    N = int(input())
    l = sorted(list(map(int, input().split())))

    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            a = l[i]
            b = l[j]
            left = bisect.bisect_right(l, b - a)
            right = bisect.bisect_left(l, a + b)
            ans += max(0, right - 1 - max(left-1, j))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def BinaryIndexSearch_floor(p, SL):
	"""
	search the minimum number among the larger numbers than the ginven nymber "n". 
	return the index of it under the list sorted.
	"""
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] > p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[l] > p:
				return l
			elif SL[r] > p:
				return r
			else:
				return -1

		center = l + ((r-l) // 2)

		if SL[center] <= p:
			l = center + 1
			continue

		else:
			r = center
			continue

def BinaryIndexSearch_ceil(p, SL):
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] < p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[r] < p:
				return r
			elif SL[l] < p:
				return l
			else:
				return -1
		center = l + ((r-l) // 2)

		if SL[center] < p:
			l = center 
			continue

		else:
			r = center - 1
			continue
ans = 0
for i in range(N-1):
	for j in range(i+1,N):
		wa = L[i] + L[j]
		sa = abs(L[i]-L[j])

		l = BinaryIndexSearch_ceil(wa,L)
		s = BinaryIndexSearch_floor(sa,L)

		oc = 0
		if s <= i <= l:
			oc += 1
		if s <= j <= l:
			oc += 1
		if l >= s:
			ans += max(0, l-s+1-oc)

print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L_list = input().split(" ")
L_list = [int(item) for item in L_list]
L_list.sort()

ans = 0
for i in range(0, len(L_list)-2):
  for j in range(i+1, len(L_list)-1):
    for k in range(j+1, len(L_list)):
      min_num = abs(L_list[i]-L_list[j])
      max_num = L_list[i]+L_list[j]
      if (min_num < L_list[k]) and (max_num > L_list[k]):
        ans += 1
      else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int, input().split()))
a=0
L.sort()
for i in range(0,N-2,1):
  for j in range(i+1,N-1,1):
    ke=L[i]+L[j]
    left=0
    right=N-(j+1)
    while left<right:
      mid=(left+right)//2
      if ke<=L[mid+j+1]:
        right=mid
      else:
        left=mid+1
    a+=left
print(a)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int, input().split()))
a=0
L.sort()
for i in range(0,N-2,1):
  for j in range(i+1,N-1,1):
    ke=L[i]+L[j]
    t=L[j+1:]
    left=0
    right=len(t)
    while left<right:
      mid=(left+right)//2
      if ke<=t[mid]:
        right=mid
      else:
        left=mid+1
    a+=left
print(a)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(lambda l: int(l)*(-1), input().split()))
L.sort()

result = 0
for i, a in enumerate(L[:-2:]):
    for ii, b in enumerate(L[i+1:-1:]):
        c_list = L[i+ii+2::]
        c_index = bisect_left(c_list, a-b)
        result += c_index

print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int, input().split()))
a=0
L.sort(reverse=True)
for i in range(0,N-2,1):
  for j in range(i+1,N-1,1):
    for q in range(j+1,N,1):
        if L[i]<L[j]+L[q] and L[j]<L[i]+L[q] and L[q]<L[i]+L[j]:
          a+=1
        else:
          break
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def binary_search(l, item):

    if len(l) == 0 or l[0] <= item:
        return -1

    if item < l[-1]:
        return len(l) - 1

    list2 = [1001] + l + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2

        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:

            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1


import heapq


N = int(input())
L = []
for l in map(int, input().split(' ')):
    heapq.heappush(L, l*(-1))

L = [heapq.heappop(L)*(-1) for _ in range(N)]
# L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L[:-2:]):
    for j, b in enumerate(L[i+1:-1:]):
        k = binary_search(L[i+j+2::], a-b)
        if k == -1:
            break
        result += k + 1


print(result)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(l, item):

    if len(l) == 0 or l[0] <= item:
        return -1

    if item < l[-1]:
        return len(l) - 1

    list2 = [1001] + l + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2

        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:

            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1


import heapq


N = int(input())
L = []
for l in map(int, input().split(' ')):
    heapq.heappush(L, l*(-1))

L = [heapq.heappop(L)*(-1) for _ in range(N)]
# L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)
        if k == -1:
            break
        result += k + 1


print(result)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(l, item):

    if len(l) == 0 or l[0] <= item:
        return -1

    if item < l[-1]:
        return len(l) - 1

    list2 = [1001] + l + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2

        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:

            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1

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

L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)
        if k == -1:
            break
        result += k + 1


print(result)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(l, item):

    if len(l) == 0 or l[0] <= item:
        return -1

    if item < l[-1]:
        return len(l) - 1

    list2 = [1001] + l + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2

        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:

            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1

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

L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)

        result += k + 1


print(result)

Submission status: Time Limit Exceeded

Solution code:

def binary_search(list, item):

    if len(list) == 0 or list[0] <= item:
        return -1

    list2 = [1001] + list + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2

        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:

            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1

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

L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)

        result += k + 1


print(result)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

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

ans=0
for i in range(2,n):
  c=list(itertools.combinations(L[:i],2))
  for j in range(len(c)):
     if  math.floor(c[j][0]-c[j][1])<L[i]<c[j][0]+c[j][1]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

hq = []
for l in L:
    heapq.heappush(hq, l*(-1))

result = 0
while hq:
    a = heapq.heappop(hq)

    hq1 = []
    while hq:
        b = heapq.heappop(hq)

        hq2 = []
        while hq:
            c = heapq.heappop(hq)
            hq2.append(c)

            if a > b + c:
                result += 1

            else:
                break

        hq1.append(b)
        for v in hq2:
            heapq.heappush(hq, v)

    for v in hq1:
        heapq.heappush(hq, v)

print(result)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

hq = []
for l in L:
    heapq.heappush(hq, l*(-1))

result = 0
while hq:
    a = heapq.heappop(hq)

    hq1 = []
    while hq:
        b = heapq.heappop(hq)

        hq2 = []
        while hq:
            c = heapq.heappop(hq)
            heapq.heappush(hq2, c)

            if a > b + c:
                result += 1

            else:
                break

        heapq.heappush(hq1, b)
        while hq2:
            heapq.heappush(hq, heapq.heappop(hq2))

    while hq1:
        heapq.heappush(hq, heapq.heappop(hq1))

print(result)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = [int(i) for i in input().split()]
cnt = 0
h = []
for i in set(combinations(l, 3)):
  a, b, c = sorted(i)
  if c < (a + b):
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = [int(i) for i in input().split()]
cnt = 0
h = []
for i in set(combinations(l, 3)):
  a, b, c = i
  if a < (b + c) and b < (c + a) and c < (a + b):
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
#print(L)
t_num=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if L[i]==L[j]:
            t_num=t_num+(N-1-j)
            #print('Eq:'+str(N-1-j)
        else:
            rst=L[i]-L[j]
            #print('rst:'+str(rst))
            for k in range(j+1,N):
                if L[k]<=rst:
                    break
                else:
                    #print(str(L[i])+'-'+str(L[j])+'-'+str(L[k]))
                    t_num=t_num+1
print(t_num)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
t_num=0
for i in range(N):
    for j in range(i+1,N):
        if i<N-2 and L[i]>=L[i+1]+L[i+2]:
            break
        for k in range(j+1,N):
            if L[i]>=L[j]+L[k]:
                break
            else:
                t_num=t_num+1
                
print(t_num)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
t_num=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if L[i]>=L[j]+L[k]:
                break
            else:
                t_num=t_num+1
                
print(t_num)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=[int(x) for x in input().split()]
L.sort()
count = 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[j]+L[k]>L[i] and L[k]+L[i]>L[j]:
        count +=1
      else:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=[int(x) for x in input().split()]
count = 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[j]+L[k]>L[i] and L[k]+L[i]>L[j]:
        count +=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().split()))
count=0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):              
            a=lst[i]
            b=lst[j]
            c=lst[k]
            if a<b+c and b<c+a and c<a+b:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().split()))
count=0
for i in range(n-2):
    for j in range(1,n-1):
        for k in range(2,n):
            if i<j<k:                
                a=lst[i]
                b=lst[j]
                c=lst[k]
                if a<b+c and b<c+a and c<a+b:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
R = []
out = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      R.append([L[i], L[j], L[k]])
for i in range(len(R)):
  if R[i][0] < R[i][1] + R[i][2] and R[i][1] < R[i][0] + R[i][2] and R[i][2] < R[i][1] + R[i][0]:
    out += 1

print(out)

Submission status: Time Limit Exceeded

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

cnt = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1:]):
        for c in L[i+j+2:]:
            if a < b + c and b < c + a and c < a + b:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1:]):
        for c in L[i+j+2:]:
            if a < b + c and b < c + a and c < a + b:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N):
    for j in range(N - i - 1):
        for k in range(N - i - j - 2):
            a = L[i]
            b = L[i + j + 1]
            c = L[i + j + k + 2]
            if a < b + c and b < c + a and c < a + b:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
out = 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[j] < L[i] + L[k] and L[k] < L[j] + L[i]:
        out += 1
print(out)

Submission status: Time Limit Exceeded

Solution code:
### D - Triangles ###

# N = 7
# L = [218, 786, 704, 233, 645, 728, 389]

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

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))
# print(cb)

rslt = 0
m = 10000

if N-2 < m:
    m = N-2

for i in cb:
    set_L = L.copy()
    set_L.remove(i[0])
    set_L.remove(i[1])
    split_list = [set_L[i:i+m] for i in range(0, len(set_L), m)]

    for j in split_list:
        if i[0]-i[1]<j[0] and i[1]>=j[0]:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                # 上界fit, 下界fit
                rslt = rslt + len(j)
            else:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
        else:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
            else:
                # 上界fitしない, 下界しない
                None

print(rslt)

Submission status: Time Limit Exceeded

Solution code:
### D - Triangles ###

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

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))
# print(cb)

rslt = 0
m = 1000

if N-2 < m:
    m = N-2

for i in cb:
    set_L = L.copy()
    set_L.remove(i[0])
    set_L.remove(i[1])
    split_list = [set_L[i:i+m] for i in range(0, len(set_L), m)]

    for j in split_list:
        if i[0]-i[1]<j[0] and i[1]>=j[0]:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                # 上界fit, 下界fit
                rslt = rslt + len(j)
            else:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
        else:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
            else:
                # 上界fitしない, 下界しない
                None

print(rslt)