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:
import collections
import math
import operator as op
from functools import reduce

import numpy as np
import math
import bisect


def main():
    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):
            a = L[i]
            b = L[j]

            # l = bisect.bisect_right(L[j+1:], abs(a-b))
            l = 0
            # print(l)
            r = bisect.bisect_left(L[j+1:], a+b)-1
            # candidates: [l, r]

            ans += max(0, r - l + 1)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
count=0
import itertools
Lists=list(itertools.combinations(L,3))
for abc in Lists:
    if (abs(abc[0]-abc[1])<abc[2]) and (abc[2]<abc[1]+abc[0]):
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l1 = l[:-2]

ans = 0
for i, li in enumerate(l1):
    l2 = l[i+1:-1]
    for j, lj in enumerate(l2):
        l3 = l[i+j+2:]
        for k, lk in enumerate(l3):
            if (lk - lj) >= li:
                break
            else:
                ans += 1



print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ret = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ret += bisect(L[j+1:],L[i]+L[j]-1)
print(ret)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted(list(map(int,input().split()))) #昇順
res=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[i]+l[j]:
                res+=1
            else:
                break
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
res=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]:
                res+=1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect
#abs(a-b)<c<a+b
N=int(input())
L=sorted([int(i) for i in input().split()])
ans=0
#print(L)
for i in range(N):
    for j in range(i+1,N):
        A=bisect.bisect_left(L,L[i]+L[j])
        B=bisect.bisect_right(L,abs(L[i]-L[j]))
        tmp=A-B
        if B<=i<A:
            tmp-=1
        if B<=j<A:
            tmp-=1
        ans+=tmp
        #print(i,j,A,B,tmp)
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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):
		for k in range(j+1, bisect(L, L[i]+L[j]-1)):
			ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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):
		for k in range(j+1, bisect(L, L[i]+L[j]-1)):
			if k != i and k != j:
				ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
ans = 0
L_count = 0
L_dict = dict()
for i in range(N):
    for j in range(i+1,N):
        for l in range(j+1,N):
            a = L[i]
            b = L[j]
            c = L[l]
            if a < (b+c):
                if b < (c+a):
                    if c < (a+b):
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted([int(i) for i in input().split()],reverse=True)

#print(l)
co=0
for i in range(n-2):
    c=l[i]
    x=0
    for j in range(i+1,n-1):
        b=l[j]
        for k in range(j+1,n):
            a=l[k]
            if c<(a+b):
                co+=1
            else:
                if k==j+1:
                    x=1
                break
        if x==1:
            break

print(co)

Submission status: Time Limit Exceeded

Solution code:
def bin_search1(L1,L2,l,N):#長すぎる辺の個数を返す
    low = 0
    high = N-1
    while high-low>1:
        mid = (low+high)//2
        if (L1+L2<=l[mid]):#長すぎ
            high = mid
        else:
            low = mid
    if L1+L2>l[high]:#全部ok
        return 0
    elif L1+L2<=l[low]:#全部ng
        return N
    else:
        return N-high

def bin_search2(L1,L2,l,N):#短すぎる辺の個数を返す
    low = 0
    high = N-1
    while high-low>1:
        mid = (low+high)//2
        if (abs(L1-L2)>=l[mid]):
            low = mid
        else:
            high = mid
    if abs(L1-L2)<l[low]:
        return 0
    elif abs(L1-L2)>=l[high]:
        return N
    else:
        return high

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

ans = 0

for i in range(N):
    for j in range(N):
        if i!=j:
            L_temp = L#修正面倒なため
            a = L[i]
            b = L[j]
            if i<j:
                L_temp.pop(j)
                L_temp.pop(i)
                ans += (N-2-bin_search1(a,b,L_temp,N-2)-bin_search2(a,b,L_temp,N-2))
                L.insert(i,a)
                L.insert(j,b)
            else:
                L_temp.pop(i)
                L_temp.pop(j)
                ans += (N-2-bin_search1(a,b,L_temp,N-2)-bin_search2(a,b,L_temp,N-2))
                L.insert(j,b)
                L.insert(i,a)
                
ans = ans//6

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bin_search1(L1,L2,l):#長すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (L1+L2<=l[mid]):#長すぎ
            high = mid
        else:
            low = mid
    if L1+L2>l[high]:#全部ok
        return 0
    elif L1+L2<=l[low]:#全部ng
        return len(l)
    else:
        return len(l)-high

def bin_search2(L1,L2,l):#短すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (abs(L1-L2)>=l[mid]):
            low = mid
        else:
            high = mid
    if abs(L1-L2)<l[low]:
        return 0
    elif abs(L1-L2)>=l[high]:
        return len(l)
    else:
        return high

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

ans = 0

for i in range(N):
    for j in range(N):
        if i!=j:
            L_temp = L#修正面倒なため
            a = L[i]
            b = L[j]
            if i<j:
                L_temp.pop(j)
                L_temp.pop(i)
                ans += (N-2-bin_search1(a,b,L_temp)-bin_search2(a,b,L_temp))
                L.insert(i,a)
                L.insert(j,b)
            else:
                L_temp.pop(i)
                L_temp.pop(j)
                ans += (N-2-bin_search1(a,b,L_temp)-bin_search2(a,b,L_temp))
                L.insert(j,b)
                L.insert(i,a)
                
ans = ans//6

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bin_search1(L1,L2,l):#長すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (L1+L2<=l[mid]):#長すぎ
            high = mid
        else:
            low = mid
    if L1+L2>l[high]:#全部ok
        return 0
    elif L1+L2<=l[low]:#全部ng
        return len(l)
    else:
        return len(l)-high

def bin_search2(L1,L2,l):#短すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (abs(L1-L2)>=l[mid]):
            low = mid
        else:
            high = mid
    if abs(L1-L2)<l[low]:
        return 0
    elif abs(L1-L2)>=l[high]:
        return len(l)
    else:
        return high

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

ans = 0

for i in range(N):
    for j in range(N):
        if i!=j:
            L_temp = L#修正面倒なため
            a = L[i]
            b = L[j]
            if i<j:
                L_temp.pop(j)
                L_temp.pop(i)
                ans += (N-2-bin_search1(a,b,L_temp)-bin_search2(a,b,L_temp))
                L.insert(i,a)
                L.insert(j,b)
            else:
                L_temp.pop(i)
                L_temp.pop(j)
                ans += (N-2-bin_search1(a,b,L_temp)-bin_search2(a,b,L_temp))
                L.insert(j,b)
                L.insert(i,a)

           
         
        
ans = ans//6

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bin_search1(L1,L2,l):#長すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (L1+L2<=l[mid]):#長すぎ
            high = mid
        else:
            low = mid
    if L1+L2>l[high]:#全部ok
        return 0
    elif L1+L2<=l[low]:#全部ng
        return len(l)
    else:
        return len(l)-high

def bin_search2(L1,L2,l):#短すぎる辺の個数を返す
    low = 0
    high = len(l)-1
    while high-low>1:
        mid = (low+high)//2
        if (abs(L1-L2)>=l[mid]):
            low = mid
        else:
            high = mid
    if abs(L1-L2)<l[low]:
        return 0
    elif abs(L1-L2)>=l[high]:
        return len(l)
    else:
        return high

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

ans = 0

for i in range(N):
    for j in range(N):
        if i!=j:
            L_temp = L.copy()
            if i<j:
                L_temp.pop(j)
                L_temp.pop(i)
            else:
                L_temp.pop(i)
                L_temp.pop(j)

            ans += (N-2-bin_search1(L[i],L[j],L_temp)-bin_search2(L[i],L[j],L_temp))
         
        
ans = ans//6

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
L=sorted(list(map(int,input().split())))
cmb=list(combinations(L,3))
cnt=0
for i in range(len(cmb)):
  if cmb[i][0]+cmb[i][1]>cmb[i][2]:
    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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]
        tmp = l[j+1:]
        ans += bisect_left(tmp,a+b)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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]
        ans += bisect_left(l[j+1:],a+b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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]
        max_c = a+b
        ans += bisect_left(l[j+1:],max_c)
print(ans)

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(N-1):
    for j in range(i+1,N):
        k=j+1
        while k<N:
            if l[i]+l[j]>l[k]:
                cnt+=1
                k+=1
            else:
                cnt+=0
                k+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect 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]
        max_c = a+b-1
        ans += bisect(l[j+1:],max_c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans = 0
b_c= [0]*n
for i  in range(n-2):
    a = l[i]
    k = 0
    for j in range(i+1,n-1):
        b = l[j]
        max_c = a+b-1
        add_k = bisect(l[j+k+1:],max_c)
        k += add_k
        ans += k
        k = max(k-1,0)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans = 0
b_c= [0]*n
for i  in range(n-2):
    a = l[i]
    pre_k = 2
    for j in range(i+1,n-1):
        b = l[j]
        max_c = a+b-1
        k = bisect(l[j+pre_k-1:],max_c)
        ans += (pre_k-2 + k)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools as it

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

com=list(it.combinations(l,3))
# print(com)
# print(len(com))

cnt=0
for i in com:
    if i[0]+i[1]>i[2] and i[1]+i[2]>i[0] and i[0]+i[2]>i[1]:
        cnt+=1
    else:
        cnt+=0
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations


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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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):
    ma=n-1
    while ma>=j+1:
      if L[ma]<L[i]+L[j]:
        cnt+=ma-j
        break
      else:
        ma-=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
l=list(map(int,input().split()))
l.sort()
L=np.array(l)
cnt=0
for i in range(n-2):
  for j in range(i+1,n-1):
    cnt+=np.count_nonzero(L[j+1:]<L[i]+L[j])
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
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):
    for k in range(j+1,n):
      if L[k]>=L[i]+L[j]:
        break
      else:
        cnt+=1
print(cnt)

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(N - 1 - i):
        a = L[i]
        b = L[i + j + 1]

        for k in range(N - 2 - i - j):
            c = L[i + j + k + 2]
            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):
    for j in range(N - 1 - i):
        for k in range(N - 2 - i - j):
            a = L[i]
            b = L[i + j + 1]
            c = L[i + j + k + 2]

            if a < b + c and b < a + c and c < a + b:
                ans += 1
                
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def lower_bound(a, x):
  l = -1
  r = len(a)
  while r - l > 1:
    h = (l + r) // 2
    if a[h] < x:
      r = h
    else:
      l = h
  return l
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
res = 0
for i in range(n):
  for j in range(i+1, n):
    lb = lower_bound(l[j+1:], l[i]-l[j]+1)
    res += lb + 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())

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

cs = list(itertools.combinations(ls,3))

count = 0
for c in cs:
    d = list(c)
    d.sort()
    if d[2] < d[0] + d[1]:
        count += 1
    

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())

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

sans = list(itertools.combinations(ls,3))
k = len(sans)
count = 0
for i in range(k):
    a = list(itertools.permutations(sans[i]))
    b = 0
    for i in range(6):
        if (a[i])[0] < (a[i])[1] + (a[i])[2]:
            b += 1
    if b == 6:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

l = sorted(l)
ans = 0
for i in range(n):
    a = l[i]
    for j in range(i + 1, n):
        b = l[j]
        shita = max(a - b, b - a)
        ue = a + b
        ans += bisect.bisect_left(l, ue) - bisect.bisect_right(l, shita)
        if shita < a < ue:
            ans -= 1
        if shita < b < ue:
            ans -= 1

print(ans // 3)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
N = int(input())
sides = sorted(map(int, input().split()))

ans = 0
for i in range(1, N - 1):
    for j in range(i + 1, N + 1):
        ans += N - j - bisect(sides, sides[-i] - sides[-j], hi=N-j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
N = int(input())
sides = sorted(map(int, input().split()))
print(sum(sum(N - j - bisect(sides, sides[-i] - sides[-j], hi=N-j) for j in range(i + 1, N + 1)) for i in range(1, N - 1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
import bisect
import math
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
l = na()

l.sort()

ct = 0
for i in range(n):
    for j in range(i+1, n):
        tmp = l[i] + l[j]
        ct += bisect.bisect_left(l[j+1:], tmp)
print(ct)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
import bisect
import math
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
l = na()

l.sort()

ct = 0
for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            a = l[i]
            b = l[j]
            c = l[k]
            if a < b + c and b < a + c and c < a + b:
                ct += 1
print(ct)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right, bisect_left
N = int(input())
L = [int(_) for _ in input().split()]
ans = 0
L.sort()

for n in range(N - 1, -1, -1):
    for m in range(n - 1, -1, -1):
        if abs(N - 1 - bisect_left(L, L[n] + L[m])) > 0:
            #print(n, m)
            ans += bisect_left(L, L[n] + L[m]) - n - 1
            #print("ans", ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right, bisect_left
N = int(input())
L = sorted([int(_) for _ in input().split()])
ans = 0

for n in range(N - 1, -1, -1):
    for m in range(n - 1, -1, -1):
        if abs(N - 1 - bisect_left(L, L[n] + L[m])) > 0:
            #print(n, m)
            ans += bisect_left(L, L[n] + L[m]) - n - 1
            #print("ans", ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(list(map(int, input().split())))
def isOK(target,a,b):
  return target < a+b

def bin_search(a,b):
  left = 1
  right = n
  while abs(right-left)>1:
    mid = (left+right)//2
    if isOK(L[mid],a,b):
      left = mid
    else:
      right = mid
  return left
ans = 0
for i in range(n-2):
  for j in range(i+1,n):
    a,b = L[i],L[j]
    ans += max(0, bin_search(a,b) - j)
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(list(map(int, input().split())))
def isOK(target,a,b):
  return target > abs(a-b) and target < a+b

def bin_search(a,b):
  left = 1
  right = n
  while right-left>1:
    mid = left+(right-left)//2
    if isOK(L[mid],a,b):
      left = mid
    else:
      right = mid
  return left
ans = 0
for i in range(n):
  for j in range(i+1,n):
    a,b = L[i],L[j]
    ans += max(0, bin_search(a,b) - j)
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(list(map(int, input().split())))
def isOK(target,a,b):
  return target > abs(a-b) and target < a+b

def bin_search(a,b):
  left = 1
  right = n
  while right-left>1:
    mid = (left+right)//2
    if isOK(L[mid],a,b):
      left = mid
    else:
      right = mid
  return left
ans = 0
for i in range(n):
  for j in range(i+1,n):
    a,b = L[i],L[j]
    ans += max(0, bin_search(a,b) - j)
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

x = []

for i in itertools.combinations(l, 3):
    if (i[0] < i[1] + i[2]) and (i[1] < i[0] + i[2]) and (i[2] < i[0] + i[1]):
        x.append(i)

print(len(x))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

x = [sorted(list(i)) for i in itertools.combinations(l, 3)]

y = []

for z in x:
    if (z[0] < z[1] + z[2]) and (z[1] < z[0] + z[2]) and (z[2] < z[0] + z[1]):
        y.append(z)

print(len(y))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = list(map(int, input().split()))

ll = list(combinations(l,3))
cnt = 0
for i in ll:
	a,b,c=i[0],i[1],i[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 = sorted(list(map(int,input().split())), reverse=True)
ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        L1 = L[i]
        L2 = L[j]
        left = j+1
        right = N-1
        if left == right:
            if L[left] > L1-L2:
                ans += 1
            break
        while True:
            if right-left == 1:
                if L[right] > L1-L2:
                    ans += right-j
                elif L[left] > L1-L2:
                    ans += left-j
                else:
                    pass
                break
            mid = (left+right)//2
            if L[mid] > L1-L2:
                left = mid
            else:
                right = mid
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import itertools


def solve(N: int, L: "List[int]"):
    combi = itertools.combinations(L, 3)
    ans = 0
    for obj in combi:
        if obj[0]+obj[1] > obj[2] and obj[1]+obj[2] > obj[0] and obj[2]+obj[0] > obj[1]:
            ans += 1
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache

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

@lru_cache(maxsize=2048)
def can_triangle(a, b, c):
    return a < (b + c) and b < (c + a) and c < (a + b)

triangle_count = 0
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        for k in range(j + 1, len(L)):
            triangle_count += can_triangle(L[i], L[j], L[k])

print(triangle_count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted([int(i) for i in input().split()],reverse=True)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
#import sys
#input = sys.stdin.readline
 
n = int(input())
l = [int(li) for li in input().split(' ')]
l.sort()

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
#import sys
#input = sys.stdin.readline
 
n = int(input())
l = list(map(int, input().split()))
l.sort()

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
input = sys.stdin.readline
 
n = int(input())
l = list(map(int, input().split()))
l.sort()

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [int(li) for li in input().split(' ')]
cnt = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        # print('check', L[i], L[j])
        kcnt = sum([1 for li in L[j + 1:N] if li < L[i] + L[j] and L[i] < li + L[j] and L[j] < li + L[i]])
        # print('ok', [li for li in L[j + 1:N] if li < L[i] + L[j]])
        cnt += kcnt

print(cnt)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

MAX=L[-1]
count=0
for i in range(N-2):
    for  j in range(i+1,N-1):
        # print(i,j)
        # print(L[i+1:-1])
        minInd=j+1
        # print(minInd)
        
        # if L[minInd]> L[i]+L[j]:
        #     continue
        # if MAX<L[i]+L[j]:
        #     count+=N-1-minInd+1
        # else:
        #     ind = bisect.bisect_left(L[minInd:], L[i]+L[j]) # number of < a+b
        #     count+=ind
        
        count+= bisect.bisect_left(L[minInd:], L[i]+L[j]) # number of < a+b
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

MAX=L[-1]
count=0
for i in range(N-2):
    for  j in range(i+1,N-1):
        # print(i,j)
        # print(L[i+1:-1])
        minInd=j+1
        # print(minInd)
        
        # if L[minInd]> L[i]+L[j]:
        #     continue
        # if MAX<L[i]+L[j]:
        #     count+=N-1-minInd+1
        # else:
        #     ind = bisect.bisect_left(L[minInd:], L[i]+L[j]) # number of < a+b
        #     count+=ind
        
        ind = bisect.bisect_left(L[minInd:], L[i]+L[j]) # number of < a+b
        count+=ind
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

MAX=L[-1]
count=0
for i in range(N-2):
    for  j in range(i+1,N-1):
        # print(i,j)
        # print(L[i+1:-1])
        minInd=j+1
        # print(minInd)
        if L[minInd]> L[i]+L[j]:
            continue
        if MAX<L[i]+L[j]:
            count+=N-1-minInd+1
        else:
            ind = bisect.bisect_left(L[minInd:], L[i]+L[j]) # number of < a+b
            count+=ind
        # print(count)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

MAX=L[-1]
count=0
for i,a in enumerate(L[:-2]):
    for  j,b in enumerate(L[i+1:-1]):
        # print(i,j)
        # print(L[i+1:-1])
        minInd=i+1+j+1
        # print(minInd)
        if L[minInd]> a+b:
            continue
        if MAX<a+b:
            count+=N-1-minInd+1
        else:
            ind = bisect.bisect_left(L[minInd:], a+b) # number of < a+b
            count+=ind
        # print(count)
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
def find_lt(a, x):
    'Find rightmost index less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i-1
    # raise ValueError
    return -1

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

count=0
for i,a in enumerate(L[:-2]):
    for  j,b in enumerate(L[i+1:-1]):
        minInd=i+1+j+1
        ind=find_lt(L[minInd:], a+b)
        count+=ind+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
def find_lt(a, x):
    'Find rightmost index less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i-1
    # raise ValueError
    return -1

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

count=0
for i,a in enumerate(L):
    for  j,b in enumerate(L[i+1:]):
        minInd=i+1+j+1
        ind=find_lt(L[minInd:], a+b)
        count+=ind+1
print(count)

Submission status: Time Limit Exceeded

Solution code:

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

count=0
for i,a in enumerate(L):
    for  j,b in enumerate(L[i+1:]):
        MAX=N-1 # index
        MIN=i+1+j+1
        gap=MAX-MIN
        while gap>1:
            mid=(MAX+MIN)//2
            if L[mid] < a+b:
                MIN=mid
            else:
                MAX=mid
            gap=MAX-MIN
        if L[MAX]<a+b:
            count+=MAX-(i+1+j+1)+1
        elif L[MIN]<a+b:
            count+=MIN-(i+1+j+1)+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def check(a, b, c):
    return (a < b+c) and (b < a+c) and (c < a+b)


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

l = sorted(l)

n = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        for c in range(b+1, N):
            if (check(l[a], l[b], l[c])):
                n += 1
            else:
                continue
print(n)

Submission status: Time Limit Exceeded

Solution code:
def check(a, b, c):
    return (a < b+c) and (b < a+c) and (c < a+b)


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

l = sorted(l)

n = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        for c in range(b+1, N):
            if (check(l[a], l[b], l[c])):
                n += 1

print(n)