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:
def bisect_left_reverse(a, x):
    '''
    reverseにソートされたlist aに対してxを挿入できるidxを返す。
    xが存在する場合には一番左側のidxとなる。
    '''
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    # 二分探索
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok

def bisect_right_reverse(a, x):
    '''
    reverseにソートされたlist aに対してxを挿入できるidxを返す。
    xが存在する場合には一番右側のidx+1となる。
    '''
    if a[0] < x:
        return 0
    if x <= a[-1]:
        return len(a)
    # 二分探索
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

n = int(input())
llis = list(map(int, input().split()))
lis = sorted(llis, reverse=True)
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        ans += max(bisect_left_reverse(lis, lis[i]-lis[j]) - j - 1, 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
l=list(map(int,input().split()))
l.sort()
nag=[0]*(10**3+1)
for i in range(n):
    nag[l[i]]+=1

s=0
for j in range(n):
    for k in range(j+1,n):
        a,b=(l[j],l[k])
        nag[a]-=1
        nag[b]-=1
        rnag=list(itertools.accumulate(nag))
        if abs(a-b)<a+b-1<=10**3:
            s+=rnag[a+b-1]-rnag[abs(a-b)]
        if abs(a-b)<=10**3<a+b-1:
            s+=rnag[10**3]-rnag[abs(a-b)]
        nag[a]+=1
        nag[b]+=1
        #print(s,a,b)
print(s//3)

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[j] < l[i] < l[k]+l[j]:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
cnt = 0
for i in range(n-1, 1, -1):
    for j in range(i-1, 0, -1):
        plus = l[i]+l[j]
        minus = l[i]-l[j]
        lj = l[:j]
        bir = bisect.bisect(lj, plus)
        bil = bisect.bisect(lj, minus)
        cnt += bir-bil
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
cnt = 0
for i in range(n-1, 1, -1):
    for j in range(i-1, 0, -1):
        plus = l[i]+l[j]
        minus = l[i]-l[j]
        bir = min(j, bisect.bisect(l, plus))
        bil = bisect.bisect(l, minus)
        if bir > bil:
            cnt += bir-bil
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
'''catupper
三重ループで大丈夫。関数化することでコードが読みやすくなる。
配列の使い方は基本的。ソートしなくても三重ループでOK。
'''
N=int(input())
L=list(map(int,input().split()))

def ok(A,B,C):
  return A<B+C and B<C+A and C<A+B

ans=0
for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if ok(L[i],L[j],L[k]):
        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):
    for j in range(i+1,n):
        num = abs(l[i]-l[j])
        idx_l = bisect.bisect_right(l,num)
        idx_r = bisect.bisect_left(l,min(l[i],l[j]))
        ans += max(0,idx_r-idx_l)
print(ans)

Submission status: Time Limit Exceeded

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

def binary_search3(A,x):
    OK=-1
    NG=len(A)
    DIV=(OK+NG)//2
    while OK+1<NG:
        if x>=A[DIV]:
            OK=DIV
        else:
            NG=DIV
        DIV=(OK+NG)//2
    return OK 

count=0
for a in range(len(L)):
    for b in range(a+1,len(L)):
        MAX=binary_search3(sort_L,L[a]+L[b]-1)
        MIN=binary_search3(sort_L,max(L[a],L[b]))
        #print(L[a],L[b],MIN,MAX)
        count+=max(0,MAX-MIN)

print(count)

Submission status: Time Limit Exceeded

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

def binary_search2(A,n):
    find=False
    start=0
    end=len(A)
    if A[0]==n:
        find=True
    else:
        while find==False and start<end-1:
            middle=(start+end)//2
            if n>A[middle]:
                start=middle
            elif n<A[middle]:
                end=middle
            else:
                find=True
    if find==True:
        value=A.index(n)
    else:
        value=start
    return value

count=0
for a in range(len(L)):
    for b in range(a+1,len(L)):
        MAX=binary_search2(sort_L,L[a]+L[b]-1)
        MIN=binary_search2(sort_L,max(L[a],L[b]))
        count+=max(0,MAX-MIN)

print(count)

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):
  for j in range(i+1,N):
    summation = L[i] + L[j]
    subtract  = L[j] - L[i]
    D=L.copy()
    del D[j]
    del D[i]
    #E=[ D[k] for k in range(N) if k!= i and k!= j ]
    #求めるものはjよりも長くsummationよりも小さいもの
    ##summationより小さいかを二分探索
    high = N-3
    low = j-2
    if D[0] >= summation:
      upper = 0 #1つも満たさない
      #print(L[i],L[j],D,upper)
    elif D[-1] < summation:
      upper = N-1-j #すべてOK
    else:
      while high > low+1:
        mid =(high + low) // 2
        if D[mid] >= summation: #大きすぎ
          high = mid
          # mid = low
        else: #小さすぎかちょうど
          low = mid
          # mid =high 
      upper = high +1-j
    #print("summation",summation,D,upper)
    count += upper #-lower
print(count)

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):
  for j in range(i+1,N):
    summation = L[i] + L[j]
    subtract  = L[j] - L[i]
    D=L.copy()
    del D[j]
    del D[i]
    #E=[ D[k] for k in range(N) if k!= i and k!= j ]
    ##summationより小さいかを二分探索
    high = N-3
    low = 0
    if D[0] >= summation:
      upper = 0 #1つも満たさない
      #print(L[i],L[j],E,upper)
    elif D[-1] < summation:
      upper = N-2 #N-2個すべてOK
    else:
      while high > low+1:
        mid =(high + low) // 2
        if D[mid] >= summation: #大きすぎ
          high = mid
          # mid = low
        else: #小さすぎかちょうど
          low = mid
          # mid =high 
      upper = high
    #print("summation",summation,E,upper)
    ##subtractより大きいかを二分探索
    high = N-3
    low = 0
    if D[0] > subtract:
      lower = 0 #N-2個すべてOK
    elif D[-1] <= subtract:
      lower = N-2 #1つも満たさない
    else:
      while high > low+1:
        mid =(high + low) // 2
        if D[mid] > subtract: #大きすぎ
          high = mid
          # mid = low
        else: #小さすぎかちょうど
          low = mid 
          # mid =high 
      lower = high
    #print("subtract",subtract,E,lower)
    count += upper - lower
print(count//3)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# N = int(input())
# L = sorted(list(map(int, input().split())))
N = int(readline().strip())
L = sorted(list(map(int, read().strip().split())))


def f(v):
    left = 0
    right = N
    while left + 1 < right:
        x = (left + right) // 2
        if v > L[x]:
            left = x
        else:
            right = x
    return right


ans = 0
for i in range(N):
    for j in range(i + 1, N):
        idx = f(L[i] + L[j])
        # print('a, b, idx', a, b, idx)
        # idx = np.searchsorted(L, a + b, sorter=None)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

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


def f(v):
    left = 0
    right = N
    while left + 1 < right:
        x = (left + right) // 2
        if v > L[x]:
            left = x
        else:
            right = x
    return right


ans = 0
for i in range(N):
    for j in range(i + 1, N):
        idx = f(L[i] + L[j])
        # print('a, b, idx', a, b, idx)
        # idx = np.searchsorted(L, a + b, sorter=None)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(sorted(list(map(int, input().split()))))
# print('L', L)


def f(v):
    left = 0
    right = N
    while left + 1 < right:
        x = (left + right) // 2
        if v > L[x]:
            left = x
        else:
            right = x
    return right


ans = 0
for i in range(N):
    for j in range(i + 1, N):
        a = L[i]
        b = L[j]
        idx = f(a + b)
        # print('a, b, idx', a, b, idx)
        # idx = np.searchsorted(L, a + b, sorter=None)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(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]
        idx = np.searchsorted(L, a + b, sorter=None)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(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]
        limit_c = a + b
        idx = np.searchsorted(L, limit_c)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(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]
        if b >= L[-1] + a:
            break
        limit_c = a + b
        idx = np.searchsorted(L, limit_c)
        ans += idx - 1 - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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]
        if b >= L[-1] + a:
            break
        for k in range(j + 1, N):
            c = L[k]
            if c >= a + b:
                break
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

c=0
from itertools import combinations

combs = list(combinations(l, 3))

for comb in combs:
    if comb[0] < comb[1] + comb[2]:
        if comb[1] < comb[2] + comb[0]:
            if comb[2] < comb[0] + comb[1]:
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    l1 = l[j+1:]
    c =bisect.bisect_left(l1,m+u)
    cou += c
print(cou)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    l1 = l[j+1:]
    c =[t for t in l1 if max(m-u,u-m)<t and t<m+u]
    cou += len(c)
print(cou)

Submission status: Time Limit Exceeded

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

count = 0
for a,b in combinations(l_ls,2):
    diff = abs(a-b)
    Sum = a + b
    for c in l_ls:
        if c > diff and c < Sum:
            count += 1
    if a > diff and a < Sum:
            count -= 1
    if b > diff and a < Sum:
            count -= 1
print(count//3)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int, input().split()))
r = 0
b.sort()
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            if b[i] + b[j] <= b[k]:
                break
            if b[j] + b[k] <= b[i]:
                continue
            if b[i] + b[j] > b[k] and b[j] + b[k] > b[i] and b[k] + b[i] > b[j]:
                r += 1
print(r)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int, input().split()))
r = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if b[i]+b[j]>b[k] and b[j]+b[k]>b[i] and b[k]+b[i]>b[j]:
                r+=1
print(r)

Submission status: Time Limit Exceeded

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


def main():
    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):
            if not is_triangle(L[j + 1], L[i], L[j]):
                # cannot construct with L[k] for all k > j
                continue
            if is_triangle(L[N - 1], L[i], L[j]):
                # can construct with L[k] for all k > j
                cnt += N - 1 - j
                continue
            # binary search
            ok = j + 1
            ng = N - 1
            while abs(ok - ng) > 1:
                mid = (ok + ng) // 2
                if is_triangle(L[mid], L[i], L[j]):
                    ok = mid
                else:
                    ng = mid
            cnt += ok - j
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Edges=sorted(Edges)
cnt =0
# for i in c:
#     e1=(i[1] + i[2])
#     e2=(i[0] + i[2])
#     e3=(i[0] + i[1])

#     if i[0] < e1 and i[1] < e2 and i[2] < e3:
#         cnt+=1

for a in range(N-2):
    for b in range(a+1,N-1):
        ab = Edges[a] + Edges[b]

        list_ab = list(filter(lambda x : x > Edges[b] and x < ab ,Edges))
        cnt += max(0,len(list_ab))


print(cnt)

Submission status: Time Limit Exceeded

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

res = 0
for a, b, c in cmb(L, 3):
    if a < b + c and b < c + a and c < a + b:
        res += 1

print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations

c =list(combinations(Edges,3))

cnt =0
for i in c:
    e1=(i[1] + i[2])
    e2=(i[0] + i[2])
    e3=(i[0] + i[1])

    if i[0] < e1 and i[1] < e2 and i[2] < e3:
        cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
L = sorted(map(int,input().split()))
ans = []
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[k] + L[i] and L[k] < L[i] + L[j]:
        A = [i,j,k]
        A.sort()
        s = ''.join([str(n) for n in A])
        ans.append(s)
cnt = set(ans)  
print(len(cnt))

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc143/tasks/abc143_d

N = int(input())
L_list = list(map(int, input().split()))
L_list = sorted(L_list)
count = 0

for a in range(N-2):
    for b in range(a + 1, N-1):
        c_min = max(L_list[a] - L_list[b], L_list[b] - L_list[a], L_list[b])
        
        c_max = L_list[a] + L_list[b]
        count_c = len([i for i in L_list if ((c_min < i) and (i < c_max))])
        count += count_c

print(count)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc143/tasks/abc143_d

N = int(input())
L_list = 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):
            if ((L_list[i] < L_list[j]+L_list[k]) 
            and (L_list[j] < L_list[k]+L_list[i]) 
            and (L_list[k] < L_list[i]+L_list[j])):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right, bisect_left

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

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

        sub_L = L[j + 1:]

        idx_1 = bisect_right(sub_L, a - b)
        idx_2 = bisect_left(sub_L, a + b)

        ans += len(sub_L[idx_1:idx_2])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right, bisect_left

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

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

        idx_1 = bisect_right(L[j + 1:], a - b)
        idx_2 = bisect_left(L[j + 1:], a + b)

        sub_L = L[j + 1:][idx_1:idx_2]
        ans += len(sub_L)

print(ans)

Submission status: Time Limit Exceeded

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


def main():
    num = 0
    _ = input()
    A = sorted(list(map(int, input().split())))
    for i in range(len(A)-1):
        for j in range(i+1, len(A)):
            a = A[i]
            b = A[j]
            thresh = a + b
            ok = j
            ng = len(A)
            idx = binary_search(A, ok, ng, thresh)
            num += idx - j

    print(num)


def binary_search(li, ok, ng, value):
    while abs(ok-ng) > 1:
        mid = (ok + ng) // 2
        if li[mid] < value:
            ok = mid
        else:
            ng = mid

    return ok


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    num = 0
    _ = input()
    A = sorted(list(map(int, input().split())))
    for i in range(len(A)-1):
        for j in range(i+1, len(A)):
            a = A[i]
            b = A[j]
            thresh = a + b
            ok = j
            ng = len(A)
            idx = binary_search(A, ok, ng, thresh)
            num += idx - j

    print(num)


def binary_search(li, ok, ng, value):
    while abs(ok-ng) > 1:
        mid = (ok + ng) // 2
        if li[mid] < value:
            ok = mid
        else:
            ng = mid

    return ok


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import itertools
mod = 10 ** 9 + 7
N = int(input())
L = list(map(int, input().split()))
count = 0
for a, b, c in itertools.combinations(L, 3):
    if ((a < b + c) and (b < a + c) and (c < a + b)):
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb #重複なし ソート済

ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########
def main(n,l):
    res = 0
    for a,b,c in comb(l,3):
        if isValid(a,b,c) : res += 1
    return res
        
def isValid(a,b,c):
    if a < b + c and b < c + a and c < a + b:
        return True
    return False

n = ii()
l = mi()
print(main(n,l))

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[j] + l[k] <= l[i]:
                continue
            elif l[i] + l[j] <= l[k]:
                continue
            elif l[k] + l[i] <= l[j]:
                continue
            
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
length = np.array([int(i) for i in input().split()])
total = 0
for i, a in enumerate(length):
    for j, b in enumerate(length[i + 1:]):
        candidates = length[i + j + 2:]
        candidates = candidates[candidates < a + b]
        candidates = candidates[candidates + a > b]
        candidates = candidates[candidates + b > a]
        total += len(candidates)

print(total)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
length = [int(i) for i in input().split()]
total = 0
for x in itertools.combinations(length, 3):
    a, b, c = x
    if a < b + c and b < c + a and c < a + b:
        total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

SUM = 0
for i,a in enumerate(A):
    for j,b in enumerate(A[i+1:]):
        SUM += bisect.bisect_left(A[i+j+2:], a+b)

print(SUM)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from bisect import bisect_right

def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))
    ans = 0

    for i in range(N-1):
        for j in range(i+1,N):
            diff = L[j] - L[i]
            index = bisect_right(L[:i],diff)
            ans += i-index
            # print(i,j,ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = 0

    for i in range(N-2):
        for j in range(i+1,N-1):
            minA = L[j] - L[i]
            maxA = L[j] + L[i]
            for k in range(j+1,N):
                if minA < L[k] < maxA:
                    ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
import bisect
for i in range(n):
    for j in range(i+1,n):
        a1 = abs(l[i]-l[j])
        a2 = l[i]+l[j]
        x1 = bisect.bisect_right(l,a1)
        x2 = bisect.bisect_left(l,a2)
        buf = 0
        if x1<=i<=x2:buf+=1
        if x1<=j<=x2:buf+=1
        ans += max(0,x2-x1-buf)
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
import bisect
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if abs(l[i]-l[j])<l[k]<l[i]+l[j]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_list = list(map(int,input().split()))
l_list.sort()
import bisect
count = 0
for i in range(1,n):
  for j in range(i+1,n):
    b = l_list[i]
    c = l_list[j]
    a_cand = l_list[:i]
    border = c-b
    count += len(a_cand) - bisect.bisect_right(a_cand,border)
print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = list(map(int, input().split()))
count = 0
for i in list(combinations(sorted(l), 3)):
    if sum(i)-2*max(list(i))>0:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = list(map(int, input().split()))
count = 0
for i in list(combinations(l, 3)):
    if sum(i)-2*i[0]>0 and sum(i)-2*i[1]>0 and sum(i)-2*i[2]>0:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(list(map(int, input().split())))

L.sort()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ans += np.count_nonzero((L[j] < L) & (L < L[i] + L[j]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(list(map(int, input().split())))

L.sort()
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[j] - L[i] < L[k]) & (L[k] < L[j] + L[i]):
                ans += 1
            elif L[j] + L[i] < L[k]:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import bisect
    n = int(input())
    l = sorted(list(map(int,input().split())))
    ans = 0
    for i in range(n-2):
        c = l[n-1-i]
        l_ = l[0:n-1-i]
        n_ = len(l_)
        for j in range(n_-1):
            l__ = l_[j+1:]
            b = bisect.bisect_left(l__,c+1-l_[j])
            ans += len(l__)-b
    print(ans)


if __name__ == "__main__":
    main()

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

Submission status: Time Limit Exceeded

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

c = itertools.combinations(L, 3)
count=0
for i in c:
  c2=list(i)
  max_index=c2.index(max(c2))
  del c2[max_index]
  if max(i)<sum(c2):
    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

import bisect
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):
            continue
        tmp = l[i] + l[j]
        for k in range(n):
            if(j >= k):
                continue
            if(tmp > l[k]):
                ans = ans + 1
            else:
                break
            
print(ans)

Submission status: Time Limit Exceeded

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

# c < b < a の条件下
def judge(a:int, b:int, c:int):
    if a < b + c:
        pass
    else:
        return False
    return True

def binary_search(l:int, r:int):
    while r - l > 1:
        mid = (l + r) // 2
        c = L[mid]
        if judge(a, b, c) == True:
            l = mid
        else:
            r = mid
    return l

ans = 0
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        f1 = judge(a, b, L[j + 1])
        f2 = judge(a, b, L[n - 1])
        if f1 == True:
            if f2 == False:
                ans += binary_search(j, n - 1) - j 
            else:
                ans += n - j - 1
        else:
            pass
    
print(ans)

Submission status: Time Limit Exceeded

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

def judge(a:int, b:int, c:int):
    if a < b + c:
        pass
    else:
        return False
    if b < c + a:
        pass
    else:
        return False
    if c < a + b:
        pass
    else:
        return False
    return True

def binary_search(l:int, r:int):
    while r - l > 1:
        mid = (l + r) // 2
        c = L[mid]
        if judge(a, b, c) == True:
            l = mid
        else:
            r = mid
    return l

ans = 0
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        f1 = judge(a, b, L[j + 1])
        f2 = judge(a, b, L[n - 1])
        if f1 == True:
            if f2 == False:
                ans += binary_search(j, n - 1) - j 
            else:
                ans += n - j - 1
        else:
            pass
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sorted(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:
                pass
            else:
                break
            if b < c + a:
                pass
            else:
                break
            if c < a + b:
                pass
            else:
                break
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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(i + 1, n):
        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]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
from collections import deque
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
first = deque(L)
count = 0
for i in range(N-2):
    l = first.popleft()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.popleft()
        n = len([i for i in second if i > l-m])
        if n > 0:
            count += n
        else:
            break
print(count)

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
from collections import deque
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
first = deque(L)
count = 0
for i in range(N-2):
    l = first.popleft()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.popleft()
        third = copy(second)
        for k in range(N-i-j-1):
            n = third.popleft()
            if n > l - m:
                count += 1
            else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
from collections import deque
import numpy as np
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
k = np.array(L)
first = deque(L)
count = 0
for i in range(N-2):
    l = first.popleft()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.popleft()
        n = np.count_nonzero(np.array(second) > (l-m))
        if n == 0:
            break
        else:
            count += n
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import numpy as np
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
k = np.array(L)
count = 0
for i in range(N-2):
    l = k[i]
    for j in range(1,N-i-1):
        m = k[i+j]
        n = np.count_nonzero(k[i+j+1:] > (l-m))
        if n == 0:
            break
        else:
            count += n
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(c) for c in input().split()]
l.sort()
def bin_search(l, r, f):
    while r - l > 1:
        c = (l + r) // 2
        if f(c):
            r = c
        else:
            l = c
    return r

llen = len(l)
ans = 0
for i in range(llen):
    a = l[i]
    for j in range(i + 1, llen):
        b = l[j]
        s = a + b
        upper = bin_search(j, n, lambda x: l[x] >= s)
        #print(i, j, upper)
        ans += upper - (j + 1)
print(ans)