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 itertools
import statistics
n = int(input())
list_bou = list(map(int, input().split()))
ans = 0
conbi = itertools.combinations(list_bou, 3)
for tri in conbi:
  if max(tri) < statistics.median(tri) + min(tri):
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

n = int(input())

l_list = [int(x) for x in input().split()]
l_list.sort()

ans = 0
for i in range(n - 2):
    continue_flag = False
    for j in range(i + 1, n - 1):
        l = bisect_right(l_list, l_list[j] - l_list[i])
        l = l if l > j else j + 1
        if l == n:
            continue_flag = True
            break

        r = bisect_left(l_list, l_list[j] + l_list[i])

        ans += r - l

    if continue_flag:
        continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted(list(map(int, input().split())))
cnt = 0
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        for k in range(j + 1, len(L)):
            a, b, c = L[i], L[j], L[k]
            if c >= a + b:
                break
            if a < b + c and b < c + a:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

count=0
for i in range(0,n-2):
    k=L[i]
    for j in range(i+1,n-1):
        t=L[j]
        if k>=t+L[j+1]:
            break
        for m in range(j+1,n):
            if k>=t+L[m]:
                break
            else:
                count+=1
print(count)

Submission status: Time Limit Exceeded

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

count=0
for i in range(0,n-2):
    k=L[i]
    for j in range(i+1,n-1):
        t=L[j]
        if k>=t+L[j+1]:
            break
        for m in range(j+1,n):
            if k>=t+L[m]:
                break
            else:
                count+=1
print(count)

Submission status: Time Limit Exceeded

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

def tri(a,b,c):
    if a<b+c and b<a+c and c<a+b:
        return 1
    else:
        return 0
count=0
for i in range(0,n-2):
    k=L[i]
    for j in range(i+1,n-1):
        t=L[j]
        for m in range(j+1,n):
            if tri(k,t,L[m])==1:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort(reverse=True)
def tri(a,b,c):
    if a<b+c:
        return 1
    else:
        return 0
count=0
for i in range(0,n-2):
    k=L[i]
    for j in range(i+1,n-1):
        t=L[j]
        for m in range(j+1,n):
            if tri(k,t,L[m])==1:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
ans = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    a, b = l[i], l[j]
    s = a+b
    bit_l, bit_r = j+1, n
    while bit_r-bit_l > 1:
      key = (bit_l+bit_r)//2
      if s > l[key]:
        bit_l = key
      else:
        bit_r = key
    if bit_r == n and l[bit_l] >= s:
      bit_r = n-1
    elif bit_l == j+1 and l[bit_l] >= s:
      bit_r = j+1
    ans += bit_r-j-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-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:
N=int(input())
L=list(map(int,input().split()))

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)

CNT = Counter(l)
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        temp0 = l[j] - l[i]
        temp1 = l[j] + l[i]
        for k in range(l[j] + 1, temp1):
            #print(k,CNT[k])
            cnt += CNT[k]
            if k == l[j]:cnt += -1
            if k == l[i]:cnt += -1
print(cnt)
#print(CNT)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

l.sort()
s=0
for i in range(n):
  for j in range(i+1,n):
    k=np.searchsorted(l,l[i]+l[j],side="left")
    s+=k-(j+1)
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted(map(int, input().split()), reverse=True)
cnt = 0

for i in range(N - 2):
    c = L[i]
    
    for j in range(i + 1, N - 1):
        b = L[j]
        l = j
        r = N - 1
        
        while True:
            idx = -(-(l + r) // 2)
            
            if L[idx] > abs(b - c):
                if l == idx:
                    break
                l = idx
            else:
                if r == idx:
                    break
                r = idx
                
        cnt += l - j
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
cnt = 0
for i in range(0, 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 or b >= c + a or c >= a + b:
                cnt += 1
                continue
                
print(N * (N - 1) * (N - 2) //  6 - cnt)

Submission status: Time Limit Exceeded

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

def tri(i,j,k):
  if i+j>k and j+k>i and k+i>j:
    return True
  else:
    return False

cnt=0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if tri(l[i],l[j],l[k]):
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(n,r))
n=input1()
L=sorted(input_array())

combi=combinations(L,3)
count=0
for tri in combi:
	tmp=list(tri)
	a=tmp[0]
	b=tmp[1]
	c=tmp[2]

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

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(n,r))
n=input1()
L=input_array()

combi=combinations(L,3)
count=0
for tri in combi:
	tmp=list(tri)
	a=tmp[0]
	b=tmp[1]
	c=tmp[2]

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j
        while k + 1 < n and l[k + 1] < l[i] + l[j]:
            k += 1
        cnt += k - j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int, input().split())))
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j
        while k + 1 < n and l[k + 1] < l[i] + l[j]:
            k += 1
        cnt += k - j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
d={}
for li in l:
  if li in d:
    d[li]+=1
  else:
    d[li]=1
l=list(set(l))
l.sort()
ans=0
for i in range(len(l)):
  for j in range(i+1):
    ls=[li for li in l[:j+1] if li>l[i]-l[j]]
    for lsi in ls:
      if l[i]==l[j] and l[j]==lsi:
        ans+=d[l[i]]*(d[l[i]]-1)*(d[l[i]]-2)//6
        #print(str(ans)+' '+str(l[i])+' '+str(l[j])+' '+str(lsi))
      elif l[i]==l[j]:
        ans+=d[l[i]]*(d[l[i]]-1)*d[lsi]//2
      elif l[j]==lsi:
        ans+=d[l[i]]*d[l[j]]*(d[l[j]]-1)//2
      else:
        ans+=d[l[i]]*d[l[j]]*d[lsi]
print(ans)

Submission status: Time Limit Exceeded

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

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        l = L[j + 1:]
        index = bisect.bisect_left(l, L[i] + L[j])
        ans += index

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L_li = list(map(int,input().split()))
from itertools import combinations
cnt = 0
for x in combinations(L_li,3):
    if (x[0]<x[1]+x[2]) and (x[1]<x[0]+x[2]) and (x[2]<x[1]+x[0]):
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
*L, = map(int, input().split())
L.sort()
count = 0
for a in reversed(range(0, N)):
  for b in range(1, a):
    range_c = L[:b]
    key = L[a] - L[b]
    left = -1
    right = len(range_c)
    while right - left > 1:
      mid = int((left + right) / 2)
      if range_c[mid] > key:
        right = mid
      else:
        left = mid
    count += len(range_c)-right
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
import sys

input = sys.stdin.buffer.readline

n = int(input())
L = list(map(int, input().split()))
L.sort()
# a,b,cの順序に大きいとして、6倍…はしなくていいのか。組み合わせらしい
# a-c<b<a+c
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        p = L[i]
        q = L[j]
        hidari = max(i + 1, bisect_left(L, q - p + 1))
        migi = min(j - 1, bisect_right(L, p + q - 1))
        delta = migi - hidari
        if delta >= 0:
            cnt += delta + 1
print(cnt)

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()
# a,b,cの順序に大きいとして、6倍…はしなくていいのか。組み合わせらしい
# a-c<b<a+c
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        p = L[i]
        q = L[j]
        hidari = max(i + 1, bisect_left(L, q - p + 1))
        migi = min(j - 1, bisect_right(L, p + q - 1))
        delta = migi - hidari
        if delta >= 0:
            cnt += delta + 1
print(cnt)

Submission status: Time Limit Exceeded

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

L = sorted(L)

# print(L)

# 三角形なのでa>b>cになるように選べば, a+b>cとなる
ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        # print("i:{}, j:{}".format(i, j))
        b = L[j]
        left = j+1
        right = N-1
        c = L[N-1]
        if a+b>c:
            # print("true - k:{}".format(N-1))
            # print("num_c:{}".format())
            ans += N-j-1
        else:
            mid = right
            while left < right:
                if mid == (left+right)//2:
                    break
                mid = (left+right)//2
                # print("left:{}, mid:{}, right:{}".format(left, mid, right))
                c = L[mid]
                if a+b>c:
                    left = mid
                else:
                    right = mid
                # time.sleep(0.05)
            # print("false - k:{}".format(mid))
            if a+b>c:
                # print("a+b>c:{}".format(True))
                ans += mid - j
            else:
                # print("a+b>c:{}".format(False))
                ans += mid - j - 1
        # print("ans of ({},{}): {}".format(i,j,ans))

print(ans)

Submission status: Time Limit Exceeded

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

L = sorted(L)

# print(L)

# 三角形なのでa>b>cになるように選べば, a+b>cとなる
ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        # print("i:{}, j:{}".format(i, j))
        b = L[j]
        left = j+1
        right = N-1
        c = L[N-1]
        if a+b>c:
            # print("true - k:{}".format(N-1))
            # print("num_c:{}".format())
            ans += N-j-1
        else:
            mid = right
            while left < right:
                if mid == (left+right)//2:
                    break
                mid = (left+right)//2
                # print("left:{}, mid:{}, right:{}".format(left, mid, right))
                c = L[mid]
                if a+b>c:
                    left = mid
                else:
                    right = mid
                time.sleep(0.05)
            # print("false - k:{}".format(mid))
            if a+b>c:
                # print("a+b>c:{}".format(True))
                ans += mid - j
            else:
                # print("a+b>c:{}".format(False))
                ans += mid - j - 1
        # print("ans of ({},{}): {}".format(i,j,ans))

print(ans)

Submission status: Time Limit Exceeded

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

L = sorted(L, reverse=True)

# print(L)

ans = 0
for i in range(N):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        for k in range(j+1, N):
            c = L[k]
            if not c < a + b:
                break
            if a < b + c and b < c + a:
                # print("a:{}, b:{}, c:{}".format(a, b, c))
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

L = sorted(L)

# print(L)

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 and b < c + a and c < a + b:
                # print("a:{}, b:{}, c:{}".format(a, b, c))
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
import bisect
for i in range(N-2):
    for j in range(i,N-1):
        if i==j:
            continue
        a = L[i]
        b = L[j]
        k = bisect.bisect_left(L[j+1:],a+b)
        l = bisect.bisect_left(L[j+1:],abs(a-b))
        if k<N-j:
            c = L[k+j]
            d = L[l+j+1]
            
            if 0<d<=c:
                
                ans += k-l
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,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())
*L, = map(int, input().split())
L.sort()
cnt = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        b = L[j]
        l = max(j+1, bisect.bisect_left(L, b-a))
        r = bisect.bisect_left(L, b+a)
        cnt += r-l
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
cnt=0
for i in range(n-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())
L = [int(i) for i in input().split()]
L.sort()
ans = 0

for ind_a, a in enumerate(L):
    for ind_b, b in enumerate(L[ind_a+1:]):

        x = bisect.bisect_left(L[ind_b+ind_a+2:], a+b)
        ans += x

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
S=input().split()
list=[]
for i in range(0,N):
  list.append(int(S[i]))
list.sort()
ans=0
for i in range(0,N-2):
  a=list[i]
  for j in range(i+1,N-1):
    b=list[j]
    L=j+1
    R=N-1
    while True:
      if L==R or R==L+1:
        break
      if list[math.floor((L+R)/2)]>=a+b:
        R=math.floor((L+R)/2)
      else:
        L=math.floor((L+R)/2)
    if list[L]>=a+b:
      ans=ans+L-j-1
    else:
      if list[R]>=a+b:
        ans=ans+L-j
      else:
        ans=ans+R-j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

n = int(input())
sticks = list(map(int, input().split()))
com = combinations(sticks, 3)
amount = 0
for m in com:
    if m[0] + m[1] > m[2] and m[0] + m[2] > m[1] and m[2] + m[1] > m[0]:
        amount += 1
print(amount)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input().split()
list=[]
for i in range(0,N):
  list.append(int(S[i]))
list.sort()
ans=0
for i in range(0,N-2):
  a=list[i]
  tempk=0
  for j in range(i+1,N-1):
    b=list[j]
    flag=0
    for k in range(max(tempk,j+1),N):
      if list[k]>=a+b:
        break
      if k==N-1:
        flag=1
    ans=ans+k-j-1
    if flag==1:
      ans=ans+1
    tempk=k
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
#棒を長さでソートし、
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L)

#3本選ぶ組み合わせ,1番長いもの2本決めると、短いものは最低の長さが決まる=0
import bisect

count =0
for i in range(N-1,1,-1): #一番長い
    for j in range(i-1,0,-1):#真ん中
        x =L_sorted[i] - L_sorted[j]
        index =bisect.bisect_right(L_sorted,x,lo=0,hi=j)
        a=0
        #if 0<=index +a and index + a <= N:
        #count.append(j-(index+a))
        count += j- index
            #print(L_sorted[i],L_sorted[j],L_sorted[index+a],index,x)

print(count)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
#棒を長さでソートし、
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L)

#3本選ぶ組み合わせ,1番長いもの2本決めると、短いものは最低の長さが決まる=0
import bisect

count =[]
for i in range(N-1,1,-1): #一番長い
    for j in range(i-1,0,-1):#真ん中
        x =L_sorted[i] - L_sorted[j]
        index =bisect.bisect_right(L_sorted,x,lo=0,hi=j)
        a=0
        #if 0<=index +a and index + a <= N:
        count.append(j-(index+a))
            #print(L_sorted[i],L_sorted[j],L_sorted[index+a],index,x)

print(sum(count))

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
#棒を長さでソートし、
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L)

#3本選ぶ組み合わせ,1番長いもの2本決めると、短いものは最低の長さが決まる=0
import bisect

count =[]
for i in range(N-1,1,-1): #一番長い
    for j in range(i-1,0,-1):#真ん中
        x =L_sorted[i] - L_sorted[j]
        index =bisect.bisect_right(L_sorted,x,lo=0,hi=j)
        a=0
        if 0<=index +a and index + a <= N:
            count.append(j-(index+a))
           # print(L_sorted[i],L_sorted[j],L_sorted[index+a],index,x)

print(sum(count))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.reverse()
count = 0
wa = []
for i in range(n-3,-1,-1):
    
    for j in range(i+2,n):
        wa.append(l[j]+l[i+1])
    
    for j in range(len(wa)):
        if (wa[j] > l[i]):
            count += 1
        else:
            wa[j] = 0
    wa = [k for k in wa if n!=0]
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.reverse()
count = 0
wa = []
for i in range(n-3,-1,-1):
    
    for j in range(i+2,n):
        wa.append(l[j]+l[i+1])
    
    for j in range(len(wa)):
        if (wa[j] > l[i]):
            count += 1
    
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
#棒を長さでソートし、長いものはわかるようにした。残り2本を選ぶことで、組み合わせを減らした
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L,reverse=True)

#3本選ぶ組み合わせ,1番長いものに対して、一番短い物2本から比較していくとダメなものが速く選べる
from itertools import combinations
count =0
for i in range(N-2): #一番長い
    for j in range(1,N-1):#一番短い
        for k in range(j+1,N-1+1):#真ん中
            if L_sorted[j*(-1)] + L_sorted[k*(-1)] <= L_sorted[i]:
            #    print(L_sorted[i],L_sorted[ k*(-1) ],L_sorted[ j*(-1) ] )
                count +=1
            else:
                break
# N本の中から3本選ぶ組み合わせ
from math import factorial
N_3_all =factorial(N) // (factorial(N - 3) * factorial(3))

kotae =N_3_all -count
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L,reverse=True)

#3本選ぶ組み合わせ,1番長いものに対して、一番短い物2本から比較していくとダメなものが速く選べる
from itertools import combinations
count =0
for i in range(N-2):
    for t in combinations(L_sorted[i+1:], 2):
        if L_sorted[i] < sum(t):
            #print(L_sorted[i],t)
            count +=1

print(count)

Submission status: Time Limit Exceeded

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

#3本選ぶ組み合わせ
from itertools import combinations
count =0
for t in combinations(L, 3):
    if max(t)*2 < sum(t): #最大の1辺＊２＜3辺の合計なら三角形作れる
        count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)
def X(n,j):
    L1 = j+1
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            while C[a] == C[a-1]:
                a = a-1
            return len(C[j+1:a])
        elif L2 - L1 <= 1:
            if C[L2] < n:
                return len(C[j+1:L2+1])
            elif C[L1] < n <= C[L2]:
                return len(C[j+1:L2])
            while C[L1] == C[L1-1]:
                if j+1 <= L1-1:
                    L1 = L1-1
                else:
                    break
            if C[L1] > n:
                return len(C[j+1:L1])
            elif C[L1] == n:
                return len(C[j+1:L1])
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
    if C[L1] < n:
        return 1
    else:
        return 0
ans = 0
# print(C)
for i in range(N-2):
    for j in range(i+1,N-1):
        # print(X((C[i]+C[j]),j))
        ans += X((C[i]+C[j]),j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)
def X(n,j):
    L1 = j+1
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            while C[a] == C[a-1]:
                a = a-1
            return len(C[j+1:a])
        elif L2 - L1 <= 1:
            while C[L1] == C[L1-1]:
                if j+1 <= L1-1:
                    L1 = L1-1
                else:
                    break
            if C[L1] > n:
                return len(C[j+1:L1])
            elif C[L2] < n:
                return len(C[j+1:L2+1])
            elif C[L1] < n <= C[L2]:
                return len(C[j+1:L2])
            elif C[L1] == n:
                while C[L1] == C[L1-1]:
                    L1 = L1-1
                return len(C[j+1:L1])
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
    if C[L1] < n:
        return 1
    else:
        return 0
ans = 0
# print(C)
for i in range(N-2):
    for j in range(i+1,N-1):
        # print(X((C[i]+C[j]),j))
        ans += X((C[i]+C[j]),j)
print(ans)

Submission status: Time Limit Exceeded

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

s = 0
for i in range(N-2):
    for j in range(i + 1, N-1):        
        if L[j + 1] > abs(L[i] - L[j]):
            s += N - (j + 1)
            k = N - 1
            while True:
                if L[k] > abs(L[i] - L[j]):
                    break
                else:
                    s -= 1
                    k -= 1
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0

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

print(ans)
exit()
from itertools import permutations, combinations,combinations_with_replacement,product

C = list(combinations(L,3))

ans = 0

for i in C:
    a, b, c = i
    if a<b+c and b<c+a and c<a+b:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

from itertools import permutations, combinations,combinations_with_replacement,product

C = list(combinations(L,3))

ans = 0

for i in C:
    a, b, c = i
    if a<b+c and b<c+a and c<a+b:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  l=sorted(list(map(int,input().split())))
  ans=0
  for a in range(n-2):
    for b in range(a+1,n-1):
      p,q=l[a]+l[b],l[b+1:]
      ans+=sum(x<p for x in q)
  print(ans)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

l = list(itertools.combinations(L,3))
t_sum = 0
for li in l:
  li = list(li)
  li.sort()
  if li[0]+li[1] > li[2]:
    t_sum+=1
print(t_sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())),reverse=True)
ans=0
for c in range(n-2):
    for a in range(n-2):
        flag=0
        b=c+1
        while flag!=1 and b<n-1-a:
            if l[c]<l[b]+l[n-1-a]:
                ans+=1
                b+=1
            else:
                flag=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())),reverse=True)
ans=0
for c in range(n-2):
    for b in range(c+1,n-1):
        for a in range(b+1,n):
            if l[c]<l[a]+l[b] and l[a]+l[b]<=l[c]*2:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from bisect import bisect_left, bisect_right

N = int(input()) #2*10**3
L_list = [int(e) for e in input().split()]
index_list = [i for i in range(N)]

L_list.sort(reverse=False)
#print(L_list)

ans = 0

for i in range(0,N-1):
    for j in range(i+1,N):
        a = L_list[i]
        b = L_list[j]
        
        #a < b+c ∴a-b < c
        #b < c+a ∴b-a < c
        if a-b > 0:
            low_limit = a-b
        else:
            low_limit = b-a
        index_left = bisect_right(L_list, low_limit) #log(2*10**6)≒6.3

        #c < a+b
        index_right = bisect_left(L_list, a+b) #log(2*10**6)
        
        #print(a,b,low_limit,a+b)
        #print(L_list[index_left:index_right])
        
        OK_c = index_right - index_left
        
        if index_left <= i <= index_right:
            OK_c -= 1
        if index_left <= j <= index_right:
            OK_c -= 1
            
        #print(OK_c)
        ans += OK_c
    
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from bisect import bisect_left, bisect_right

N = int(input()) #2*10**3
L_list = [int(e) for e in input().split()]
index_list = [i for i in range(N)]

L_list.sort(reverse=False)
#print(L_list)

ans = 0

for comb in combinations(index_list, 2): #(2*10*3)*(2*10*3 -1)/2 ≒ 2*10*6
    a = L_list[comb[0]]
    b = L_list[comb[1]]
    
    #a < b+c ∴a-b < c
    #b < c+a ∴b-a < c
    if a-b > 0:
        low_limit = a-b
    else:
        low_limit = b-a
    index_left = bisect_right(L_list, low_limit) #log(2*10**6)≒6.3

    #c < a+b
    index_right = bisect_left(L_list, a+b) #log(2*10**6)
    
    #print(a,b,low_limit,a+b)
    #print(L_list[index_left:index_right])
    
    OK_c = index_right - index_left
    
    if index_left <= comb[0] <= index_right:
        OK_c -= 1
    if index_left <= comb[1] <= index_right:
        OK_c -= 1
        
    #print(OK_c)
    ans += OK_c
    
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
O = sorted(L, reverse=True)
def X(n,j):
    C = sorted(L)
    L1 = 0
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            nn = len(C[0:N-1-j])-len(C[0:a+1])
            if nn > 0:
                return nn
            else:
                return 0
        if L2 - L1 <= 1:
            if C[L1] > n:
                nn = len(C[0:N-1-j])-len(C[0:L1])
            else:
                nn = len(C[0:N-1-j])-len(C[0:L2])
            if nn > 0:
                return nn
            else:
                return 0
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += X((O[i]-O[j]),j)
print(ans)