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 = list(map(int, input().split()))
#logging.debug(l)

ans = 0
import itertools
for j in itertools.combinations(l, 3):
#    logging.debug(j)
    a, b, c = j[0],j[1],j[2]
    if a < b + c and b < a + c and c < a + b:
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted(map(int,input().split()))
count = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        twobar = L[i]+L[j]
        for k in range(j+1,N):
            if twobar>L[k]:
                count += 1
            else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
lis=list(map(int, input().split())) 
out=0
for conb in itertools.combinations(lis, 3):
    l=list(conb)
    a=l[0]
    b=l[1]
    c=l[2]
    if a < b + c and b < c + a and c < a + b:
        out+=1
print(out)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort(reverse=True)
ans = 0
for i in range(n-2):
    a = l[i]
    x = i
    for j in range(i+1,n-1):
        b = l[j]
        y = j
        for k in range(j+1,n):
            if x == k or y == k:
                continue
            c = l[k]
            if a < b + c and b < a + c and 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()
c=0
for i in range(n-2):
    for j in range(i+1,n-1):
        cl = L[i]+L[j]
        for k in range(j+1, n):
            if cl<=L[k]:
                break
            else:
                c+=1

print(c)

Submission status: Time Limit Exceeded

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

ans=0

S =[]

for i in range(N):
    for j in range(1,N):
        for k in range(2,N):
            if i < j and j< k and L[k] > L[i]+L[j]:
                break
            elif i < j and j< k and L[k] < L[i]+L[j]:
                if sorted([L[i],L[j],L[k]]) in S:
                    continue
                else:
                    S.append(sorted([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()))
L=sorted(L)

ans=0
S =[]

for i in range(N):
    for j in range(1,N):
        for k in range(2,N):
            if i < j and j< k and L[k] < L[i]+L[j]:
                if sorted([L[i],L[j],L[k]]) in S:
                    continue
                else:
                    S.append(sorted([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()))

ans=0
S =[]

for i in range(N):
    for j in range(1,N):
        for k in range(2,N):
            if i < j and j< k:
                if L[i] < L[j]+L[k] and L[j] < L[k]+L[i] and L[k] < L[i]+L[j]:
                    if sorted([L[i],L[j],L[k]]) in S:
                        continue
                    else:
                        S.append(sorted([L[i],L[j],L[k]]))
                        ans+=1    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

for i in range(n-2):
    for j in range(i+1, n-1):
        buf = l[i]+l[j]
        Min = j+1
        Max = n
        while(Min!=Max):
            Mid = math.floor((Min+Max)/2)
            if l[Mid] < buf:
                Min = Mid+1
            elif l[Mid] == buf:
                Min = Mid
                Max = Mid
            else:
                Max = Mid
        ans += Min-(j+1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

for i in range(n-2):
    for j in range(i+1, n-1):
        buf = l[i]+l[j]
        Min = j+1
        Max = n
        while(Min!=Max):
            Mid = math.floor((Min+Max)/2)
            if l[Mid] < buf:
                Min = Mid+1
            else:
                Max = Mid
        ans += Min-(j+1)
print(ans)

Submission status: Time Limit Exceeded

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

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())
l = list(map(int, input().split()))
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            triangle = [l[i],l[j],l[k]]
            triangle.sort()
            if triangle[0]+triangle[1] > triangle[2]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

from itertools import combinations

def solve(L):
    ans = 0
    for a, b, c in combinations(L, r=3):
        ans += a + b > c
    return ans

print(solve(L))

Submission status: Time Limit Exceeded

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

s = 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]:
                s += 1

print(s)

Submission status: Time Limit Exceeded

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

#b-a < c < a+b
#a<=b<=c
ans=0
L_a=L[:N-2]
for i,a in enumerate(L_a):
    L_b=L[i+1:N-1]
    for j,b in enumerate(L_b,start=i+1):
        x=bisect.bisect(L[j+1:],a+b)
        ans+=x
        if L[j+x]==a+b:
            ans-=1
        #print(i,a,j,b,x,N-j-1,L[j+1:])
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()
#print(L)

#b-a < c < a+b
#a<=b<=c
ans=0
L_a=L[:N-2]
for i,a in enumerate(L_a):
    L_b=L[i+1:N-1]
    for j,b in enumerate(L_b,start=i+1):
        ans+=sum((b-a < L[j+1:]) & (L[j+1:] < a+b))
        
print(ans)

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)
#print(L)

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

Submission status: Time Limit Exceeded

Solution code:

from itertools import combinations

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

Lc = combinations(L, 3)

ans = 0

for (a,b,c) in Lc:
    if a<b+c and b<c+a and c<a+b:
        ans += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D - Triangles

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

L_sorted = sorted(L, reverse = False)#昇順

def judge(A,B,x):#cが三角形になるか判定する関数
    if A + B <= L_sorted[x]:#a + b = cの時はfinを持ってくる
        return 'high'
    else :
        return 'low'

count = 0
for i in range(N):#aの選び方
    for j in range(i+1,N):#bの選び方
        a = L_sorted[i]
        b = L_sorted[j]#b <= c < a+b
        sta = 0
        fin = N#Lの個数+1
        while fin > sta + 1:#cの選び方
            m = ((fin - sta) // 2) + sta
            if judge(a,b,m) == 'high':
                fin = m
            elif judge(a,b,m) == 'low':
                sta = m
        count += sta  - j    
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L= list(map(int,input().split()))
L_sorted=sorted(L,reverse=False)#昇順
count=0
for i in range(N):
    for j in range(i+1,N):
        a=L_sorted[i]
        b=L_sorted[j]
        n=a+b
        fin=N
        sta=0
        while sta+1<fin:
            m=((fin-sta)//2)+sta
            if L_sorted[m]<n:
                sta=m       
            else:
                fin=m   
        count+=sta-j
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
l_ls = list(map(int, input().split()))
l_ls.sort()
stick_count = [0] * (10**3+1)
for i in range(n):
    stick_count[l_ls[i]] += 1
stick_csum = list(np.cumsum(stick_count))
ans = 0
for first_i in range(n):
    for second_i in range(first_i+1, n):
        #境界は含まない
        greater_than = abs(l_ls[first_i] - l_ls[second_i])
        smaller_than = min(10**3,l_ls[first_i] + l_ls[second_i])
        num_canmake = max(0, stick_csum[smaller_than-1] - stick_csum[greater_than])
        if greater_than < l_ls[first_i] < smaller_than:
            num_canmake -= 1
        if greater_than < l_ls[second_i] < smaller_than:
            num_canmake -= 1
        num_canmake = max(0,num_canmake)
        ans += num_canmake
print(ans//3)

Submission status: Time Limit Exceeded

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

anslst = set([])
for v in itertools.combinations(l,3):
    a,b,c, = v[0],v[1],v[2]
    if a + b > c and a+c > b and b + c > a:
        anslst.add(v)
print(len(anslst))

Submission status: Time Limit Exceeded

Solution code:
def Binary_Search_Small_Count(A,x,sort=True,equal=False):
    """2分探索によって,x未満の要素の個数を調べる.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"未満"がx"以下"になる
    """
    if sort:
        A.sort()
        
    N=len(A)
    if A[-1]<x:
        return N
    elif x<A[0] or (not equal and x==A[0]):
        return 0

    L,R=0,N
    while R-L>1:
        C=L+(R-L)//2
        if x<A[C] or (not equal and x==A[C]):
            R=C
        else:
            L=C
    return R
#=======================================
N=int(input())
L=list(map(int,input().split()))
L.sort()

K=0
for i in range(N):
    for j in range(i+1,N):
        a,b=L[i],L[j]
        X=Binary_Search_Small_Count(L,a+b,False,False)
        Y=Binary_Search_Small_Count(L,b,True,False)
        K+=X-Y

        if b<=a<a+b:
            K-=1
        if b<=b<a+b:
            K-=1
print(K)

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]:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls=sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    if a-b>ls[y+1]:
      break
    else:
      for z in range(y+1,N):
        if a-b>=ls[z]:
          res += z-y-1
          break
        if z ==N-1:
          res += N-y-1
print(res)

Submission status: Time Limit Exceeded

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

N = int(input().rstrip('\n'))
ls = [int(x) for x in input().rstrip('\n').split()]
ls = sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    for z in range(y+1,N):
      if ls[z]>a-b:
        res += 1
      else:
        break
print(res)

Submission status: Time Limit Exceeded

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

N = int(input().rstrip('\n'))
ls = [int(x) for x in input().rstrip('\n').split()]
ls = sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    l = a-b
    for z in range(y+1,N):
      c = ls[z]
      if c>l:
        res += 1
      else:
        break
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ls = list(map(int,input().split()))
ls.sort()
p = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        di = [e for e in ls[j+1:] if ls[i]+ls[j] > e]
        p += len(di)
print(p)

Submission status: Time Limit Exceeded

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

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):
        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:
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)

a=0
for i in range(N-1):
    for k in range(1,N-i-1):
        a=a+sum(x<L[i]+L[i+k] for x in L[i+k+1:N])

print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = sorted(map(int, input().split()),reverse=True)
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            if L[a] > 2*L[b]:
                break
            for c in range(b + 1, N):
                if L[a] < L[b] + L[c] and L[b] < L[c] + L[a]:
                    ans += 1
                else:
                    break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = sorted(map(int, input().split()),reverse=True)
    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[a] < L[b] + L[c] and L[b] < L[c] + L[a]:
                    ans += 1
                else:
                    break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = sorted(map(int, input().split()))
    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[a] < L[b] + L[c] and L[b] < L[c] + L[a] and L[c] < L[a] + L[b]:
                    ans += 1
                elif L[c] > L[a] + L[b]:
                    break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from itertools import combinations
n, *L = map(int, open(0).read().split())
print(sum(a < b + c and b < c + a and c < b + a for a, b, c in combinations(L, 3)))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
L = LI()
# L = np.array(_L)
#C = np.zeros(N + 1)

# def test(a,b,c):
#     if a<b+c and b<c+a and c<a+b:
#         return True
#     return False

# count = 0
# for pair in itertools.combinations(L, 3):
#     # print(pair)
#     if test(*pair):
#         count += 1
# print(count)

# for i in range(N):
#     for j in range(i+1,N):

from numba import njit
 
# @njit
def f(A):
  count = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        count += (A[k] < A[i] + A[j])
  return count
 
A = np.array(L)
A.sort()
print(f(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
L = LI()
# L = np.array(_L)
#C = np.zeros(N + 1)

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

count = 0
for pair in itertools.combinations(L, 3):
    # print(pair)
    if test(*pair):
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
l_comb = itertools.combinations(l, 3)
ans = 0

for i in l_comb:
  if i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1]:
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
import bisect
#=================================================#
n=int(int(input()))
A=INPUT()
A.sort()
i=0
j=i+1
ans=0
while(i<n-2):
    k=j+1
    while(k<n and A[k]<(A[i]+A[j])):
        k+=1
    ans+=k-j-1
    j+=1
    if j>=n:
        i+=1
        j=i+1
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):
  for j in range(i+1,n):
    kmax=n
    kmin=j
    while True:
      k=(kmax+kmin)//2
      if l[i]+l[j]>l[k]:
        ans=ans+k-kmin
        kmin=k
      else:
        kmax=k
      if kmax-kmin==1:
        break
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):
  for j in range(i+1,n):
    for k in range(j+1,n):
      if l[i]+l[j]>l[k]:
        ans=ans+1
      else:
        break
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-1):
    for j in range(i+1, n):
        for k in range(j+1,n):
            if l[k] < l[i] + l[j]:
                count += 1

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-1):
    for j in range(i+1, n):
        for k in range(j+1,n):
            if l[j] - l[i] < l[k] and l[k] < l[i] + l[j]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[5]:


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


# In[7]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
from collections import Counter
import bisect
readline = sys.stdin.readline


def main():
    cnt = 0
    n = int(readline().rstrip())
    L = sorted(list(map(int, readline().rstrip().split())))
    for a in range(0, n - 2):
        for b in range(a + 1, n - 1):
            k = L[a] + L[b]
            l = sorted(L[b + 1: len(L)])
            index = bisect.bisect_left(l, k)
            cnt += index
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
from collections import Counter
readline = sys.stdin.readline


def main():
    cnt = 0
    n = int(readline().rstrip())
    L = sorted(list(map(int, readline().rstrip().split())))
    for a in range(0, n - 2):
        for b in range(a + 1, n - 1):
            k = L[a] + L[b]
            l = L[b + 1: len(L)]
            l.append(k)
            l.sort()
            cnt += l.index(k)
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
from collections import Counter
readline = sys.stdin.readline


def main():
    cnt = 0
    n = int(readline().rstrip())
    L = sorted(list(map(int, readline().rstrip().split())), reverse=True)
    for a in range(0, n - 2):
        for b in range(a + 1, n - 1):
            for c in range(b + 1, n):
                if L[a] < L[b] + L[c]:
                    cnt += 1
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 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]:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import numpy as np
n=int(input())
l=list(map(int, input().split()))
l=np.array(l)
l.sort()
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    ans+=bisect.bisect_left(l[j+1:], l[i]+l[j]) #n未満の個数を数える
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):
  for j in range(i+1,n-1):
    ans+=bisect.bisect_left(l[j+1:], l[i]+l[j]) #n未満の個数を数える
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Triangles
from itertools import combinations

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

ans=0

l=list(combinations(Li,3))
for i in range(len(l)):
    a,b,c=l[i]
    if a<b+c and b<c+a and c<a+b:
        ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Triangles
import itertools


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

ans=0

l=list(itertools.combinations(Li,3))
for i in range(len(l)):
    a,b,c=l[i]
    if a<b+c and b<c+a and c<a+b:
        ans+=1
    else:
        pass

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# D - Triangles
import itertools


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

ans=0

l=list(itertools.combinations(Li,3))
for i in range(len(l)):
    a=l[i][0]
    b=l[i][1]
    c=l[i][2]
    if a<b+c and b<c+a and c<a+b:
        ans+=1
    else:
        pass

print(ans)

Submission status: Time Limit Exceeded

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

# D - Triangles
def binary_search(i, j):
	global N

	left = j
	right = N
	
	while right - left > 1:
		mid = (left + right) // 2
		
		if is_match(mid, i, j):
			left = mid
		else:
			right = mid
	
	return left
		

def is_match(mid, i, j):
	global L
	
	a = L[i]
	b = L[j]
	c = L[mid]
	
	if b < c + a and c < a + b:
		return True
	else:
		return False


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):
		k = binary_search(i, j)
		
		if k > j and k < N:
			ans += k - j
			
print(ans)

Submission status: Time Limit Exceeded

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

# D - Triangles
N = int(input())
L = list(map(int, input().split()))
L.sort()

triangles = set()

for i in range(N - 2):
	for j in range(i + 1, N - 1):
		for k in range(j + 1, N):
			a = L[i]
			b = L[j]
			c = L[k]			
			
			if b < c + a and c < a + b:
				arr = [a, b, c]
				triangles.add(' '.join(map(str, arr)))
			else:
				break

print(len(triangles))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
N = int(input())
L = list(map(int, input().split()))
res = 0
def dfs(A):
    global res, L
    ans = [i for i, v in enumerate(A) if v == 1]
    if len(A) == N:
        if len(ans) == 3:
            a, b, c = L[ans[0]], L[ans[1]], L[ans[2]]
            if a < (b + c) and b < (c + a) and c < (a + b):
                res += 1
        return
    elif len(ans) == 3:
        a, b, c = L[ans[0]], L[ans[1]], L[ans[2]]
        if a < (b + c) and b < (c + a) and c < (a + b):
            res += 1
        return
    else:
        for v in range(2):
            A.append(v)
            dfs(A)
            A.pop()
dfs([])
print(res)

Submission status: Time Limit Exceeded

Solution code:

def is_triangle(a, b, c):
    if a >= b + c:
        return False

    if b >= a + c:
        return False

    if c >= a + b:
        return False

    return True


def main():
    N = int(input())
    L = list([int(x) for x in input().split()])

    L.sort(reverse=True)

    count = 0
    for index, a in enumerate(L):
        if index + 2 == N:
            break

        for b_i in range(index+1, N):
            b = L[b_i]
            c_i = 1

            while not is_triangle(a, b, L[-c_i]):
                if c_i + b_i == N:
                    break
                c_i += 1

            count += (N - b_i - c_i)

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

def is_triangle(a, b, c):
    if a >= b + c:
        return False

    if b >= a + c:
        return False

    if c >= a + b:
        return False

    return True


def main():
    N = int(input())
    L = list([int(x) for x in input().split()])

    L.sort()

    count = 0
    for index, a in enumerate(L):
        if index + 2 == N:
            break

        for b_i in range(index+1, N):
            b = L[b_i]
            c = 1

            while not is_triangle(a, b, L[-c]):
                c += 1

            count += (N - b_i - c)

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
from math import factorial
N = int(input())
L = tuple(map(int, input().split()))
nums = [0] * (N-3) + [1, 1, 1]
ans = 0
for ptr in itertools.permutations(nums):
    edge = []
    for i, v in enumerate(ptr):
        if v == 1:
            edge.append(L[i])
    if edge[0] < edge[1] + edge[2] and edge[1] < edge[2] + edge[0] and edge[2] < edge[0] + edge[1]:
        ans += 1
ans //= (factorial(3)*factorial(N-3))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
ans = 0


def q(k):
    if A[k] < A[i] + A[j]:
        return True
    else:
        return False


for i in range(n):
    for j in range(i + 1, n):
        l = j
        r = n
        while r - l > 1:
            mid = (l + r) // 2
            if q(mid):
                l = mid
            else:
                r = mid
        ans += (l - j)


print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if l[k] < l[i] + l[j]:
                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 c in itertools.combinations(L, 3):
    if (c[0] < c[1] + c[2]) and (c[1] < c[0] + c[2]) and (c[2] < c[0] + c[1]):
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

def binary_search(func, array):
    left,right=0,len(array)-1
    y_left, y_right = func(array[left]), func(array[right])
    while True:
        middle = (left+right)//2
        y_middle = func(array[middle])
        if y_left==y_middle: left=middle
        else: right=middle
        if right-left==1:break

    return left

SUM = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    l1,l2=L[i],L[j]
    if L[-1]<l1+l2:
      k = N-1
    else:
      k = binary_search(lambda x: x<l1+l2, L)
    SUM += max(0,k-j)

print(SUM)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
L = list(map(int, input().split()))
ans = 0
for l in combinations(L, 3):
    if max(l) < sum(l) - max(l):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1:]):
        l = abs(a - b)
        r = a + b
        top = bisect_left(L[i+j+2:], r)
        bot = bisect_right(L[i+j+2:], l)
        ans += top - bot
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
c = np.zeros(10 ** 3 + 1, dtype=int)
for l in L:
    c[l] += 1
ans = 0
for i, a in enumerate(L):
    for b in L[i+1:]:
        ans += c[a - b + 1:b].sum()
print(ans)