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:
### a<=b<=cととることとし、a,bを固定すると
### cは　c<a+b
N = int(input())
lst = list(map(int, input().split()))
sor = sorted(lst)

def BinarySearch(lst, key): ## lstの値のうち、key以上になる最初のインデックス
    left = -1  ## index=0が条件を満たす場合も存在
    right = len(lst)  ## index = len(lst)-1が条件を満たさないこともある、key以上になるものが存在しない
    
    while right != left + 1:
        mid = (left + right) // 2
        val = lst[mid]
        if val >= key:
            right = mid
        elif val < key:
            left = mid
    return right  ### key以上になるものが存在しない場合はlen(lst)が帰る

res = 0
for i in range(N):
    for j in range(i+1, N):##ここでa<=bを満たしている
        a = sor[i]
        b = sor[j]
        ab = a+b

        highbound = BinarySearch(sor, ab)
        res += highbound - j - 1
print(res)

Submission status: Time Limit Exceeded

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

# 各数値が何個あるか
C = [0]*(2100)
for l in L:
    C[l] += 1

# 累積和を作成する
S = [0]
for i in range(2100):
    S.append(S[i]+C[i])

ans = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue

        left = abs(L[i]-L[j])+1
        right = L[i]+L[j]
        tmp = S[right]-S[left]

        if left <= L[i] <= right:
            tmp -= 1
        if left <= L[j] <= right:
            tmp -= 1
        ans += max(0, tmp)

print(ans//6)

Submission status: Time Limit Exceeded

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

s = np.zeros((N, N))
d = np.zeros((N, N))

for i in range(N):
    for j in range(i+1, N):
        s[i, j] += lst[i] + lst[j]
        d[i, j] += abs(lst[i] - lst[j])


res = 0
for k in range(N):
    val = lst[k]
    slow = s>val
    dhigh = d<val
    sandd = slow & dhigh
    
    sandd[k, :] = False
    sandd[:, k] = False
    
    res += np.count_nonzero(sandd)
print(res//3)

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
L=list(map(int,input().split()))
l=[]
L.sort()
lis=[0]*(max(L)+1)
cnt=0
ans=0
c=collections.Counter(L)
values=list(c.values())	#aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key=list(c.keys())	#先のvalue値に相当する要素のリスト(要素1,要素2,…)
l1=0
l2=0
mi=0
ma=0
le=0
for i in range(len(key)):
    l.append([key[i],values[i]])#lは[要素i,n_i]の情報を詰めたmatrix
l.sort(key=lambda x:x[0])
#print(lis,L)
for i in range(max(L)+1):
    #print(cnt)
    cnt=cnt+c[i]
    lis[i]=cnt
for i in range(len(L)-1):
    for j in range(i+1,len(L)):
        l1=L[i]
        l2=L[j]#l1<=l2
        ma=min(l1+l2-1,max(L))
        mi=max(l2-l1+1,min(L))
        le=lis[ma]-lis[mi-1]
        if mi<=l1<=ma:
            le=le-1
        if mi<=l2<=ma:
            le=le-1
        #print(l1, l2,c[l1],c[l2],ma,mi,le)
        ans=ans+c[l1]*c[l2]*max(le,0)
print(ans//3)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            a1, a2, a3 = L[i], L[j], L[k]
            if a2+a3 <= a1:
                break
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
L = lr()
L.sort()
answer = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        candidate = L[j+1:]
        index = bisect_left(candidate, (L[i]+L[j]))
        answer += index

print(answer)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template
#issueから始める
N = int(input())
L = inputlist()
L.sort()
ans = 0
from bisect import bisect_left
for i in range(N-2):
    for j in range(i+1,N-1):
        lia = L[j+1:]
        indexa = bisect_left(lia,L[i]+L[j])
        ans += indexa
print(ans)

Submission status: Time Limit Exceeded

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

L=sorted(L, reverse=True)
count=0
for i in range(0, N-2):
    stop_count = 0
    for j in range(i+1, N-1):
        for k in range(N-1, j, -1):
            if L[i] < L[j] + L[k]:
                count+=k-j
                break


print(count)

Submission status: Time Limit Exceeded

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

L=sorted(L, reverse=True)
count=0
for i in range(0, N-2):
    stop_count = 0
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            #print(i, j, k)
            if L[i] < L[j] + L[k]:
                count+=1
                stop_count=1
        if stop_count==0:
            break


print(count)

Submission status: Time Limit Exceeded

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

L=sorted(L, reverse=True)


count=0
for i in range(0, N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):

            if L[i] < L[j] + L[k]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

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

def binary_search_left(list,target):
    '''
    配列に対してある数nを配列のソート順を崩さないように挿入するためのindexを返す関数。
    同一の数が配列に複数含まれていた場合は最も小さいindexを返す。
    '''
    result = -1
    left = 0
    right = len(list) - 1

    while left <= right:
        center = (left + right)//2
        if list[center] == target:          # 一致した場合
            if list[center - 1] == target:  # -> その一つ前も一致した場合、rightをcenterにしてもう一周
                right = center
            else:                           # -> その一つ前は一致しない場合、一番左で一致したということでcenterを返す
                return center
        elif list[center] < target:
            left = center + 1
        elif list[center] > target:
            right = center - 1
    return left                             # 一致したらcenterを返すが（上記）、なければleftのインデックスを返す
  
L_list = sorted(L_list)

ans = 0
for a_index in range(0, N):
    for b_index in range(a_index+1, N):
        a = L_list[a_index]
        b = L_list[b_index]
        c_right_index = binary_search_left(L_list, a+b)
        if c_right_index > b_index:
            ans += c_right_index - b_index - 1
        else:
            pass

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import copy
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(n):
  for j in range(i+1,n):
    if i==j:
      continue
    b=copy.deepcopy(a)
    b.remove(a[i])
    b.remove(a[j])
    ans+=bisect.bisect_left(b,a[i]+a[j])-j+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(sorted(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]
        high=n-1
        low=j+1
        if L[high]<a+b:
            ans+=n-j-1
            continue
        if L[low]>=a+b:
            continue
        while high-low>1:
            now=(high+low)//2
            c=L[now]
            if c<a+b:
                low=now
            else:
                high=now
        ans+=low-j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(reversed(sorted(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:
                ans+=1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

import itertools

c = itertools.combinations(A, 3)
t = 0
for i in c:
    x, y, z = i
    if x < y + z and y < x + z and z < x + y:
        t += 1
print(t)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
L = [int(i) for i in input().split()]
c = collections.Counter(L)

ans = 0
for i in c:
    for j in c:
        for k in c:
            if i + j <= k or j + k <= i or k + i <= j or i > j or j > k:
                continue
            if i == j and j == k:
                ans += c[i] * (c[i] - 1) * (c[i] - 2) / 6
            elif i == j:
                ans += c[k] * c[i] * (c[i] - 1) / 2
            elif j == k:
                ans += c[i] * c[j] * (c[j] - 1) / 2
            elif i == k:
                ans += c[j] * c[i] * (c[i] - 1) / 2
            else:
                ans += c[i] * c[j] * c[k]
print(int(ans))

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

Submission status: Time Limit Exceeded

Solution code:

import bisect


def submit():
    n = int(input())
    llist = list(map(int, input().split()))

    llist.sort()
    ans = 0
    for a in range(n):
        for b in range(a + 1, n):
            c_min = llist[b] - llist[a]
            c_max = llist[a] + llist[b]
            cs = bisect.bisect_right(llist, c_min, lo=b + 1)
            ce = bisect.bisect_left(llist, c_max, lo=b + 1)
            ans += ce - cs

    print(ans)


if __name__ == "__main__":
    submit()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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 = bisect.bisect_left(l[j + 1 : n], l[i] + l[j])
        ans += a
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

#max wo erabu

lst=sorted(lst, reverse=True)
count=0
for i in range(N-2):
    for pair in itertools.combinations(lst[i+1:], 2):
        if sum(pair)>lst[i]:
            count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import numpy as np
n = int(input())
ls = np.array(list(sorted(map(int, input().strip().split()))))

total = 0

for i in range(len(ls) - 2):
    for j in range(i + 1, len(ls) - 1):
        a = ls[i]
        b = ls[j]
        ls2 = ls[j + 1:]
        if a + b == ls2[0]:
            num = 0
        else:
            num = bisect.bisect_right(ls2, a + b)
        total += num

print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ls = list(sorted(map(int, input().strip().split())))

possibles = set()

for i in range(len(ls)):
    for j in range(len(ls)):
        if i == j:
            continue
        for k in range(len(ls)):
            if i == k or j == k:
                continue
            a = ls[i]
            b = ls[j]
            c = ls[k]

            if a < b + c and b < c + a and c < a + b:
                possibles.add(tuple(sorted((a, b, c))))

print(len(possibles))

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[left]:
        return 0
    elif key > L[len(L)-1]:
        return len(L)
    while left < right:
        mid = (left + right) // 2
        if L[mid] < key and key <= L[mid+1]:
            return mid + 1
        elif L[mid] >= key:
            right = mid
        else:
            left = mid

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += b_search(L,L[i]+L[j],0,len(L))-(j+1)
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[left]:
        return 0
    elif key > L[len(L)-1]:
        return len(L)
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid + 1
        elif L[mid] >= key:
            return b_search(L,key,left,mid)
        else:
            return b_search(L,key,mid,right)

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += b_search(L,L[i]+L[j],0,len(L))-(j+1)
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        a = L[i]
        b = L[j]
        low = max(a-b, b-a)
        hi = a+b
        for k in range(j+1, N):
            c = L[k]
            if low < c and c < hi:
                count += 1
            elif c >= hi:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += bisect_left(L[j+1:],L[i]+L[j])
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right,slice):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[left]:
        return 0
    elif key > L[len(L)-1]:
        return len(L) - slice
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid + 1 - slice
        elif L[mid] >= key:
            return b_search(L,key,left,mid,slice)
        else:
            return b_search(L,key,mid,right,slice)

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += b_search(L,L[i]+L[j],j+1,len(L),j+1)
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[left]:
        return 0
    elif key > L[len(L)-1]:
        return len(L)
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid + 1
        elif L[mid] >= key:
            return b_search(L,key,left,mid)
        else:
            return b_search(L,key,mid,right)

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        L_ = L[j+1:]
        ans += b_search(L_,L[i]+L[j],0,len(L_))
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right,slice):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[left]:
        return 0
    elif key > L[len(L)-1]:
        return len(L) - slice
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid+1-slice
        elif L[mid] >= key:
            return b_search(L,key,left,mid,slice)
        else:
            return b_search(L,key,mid,right,slice)

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += b_search(L,L[i]+L[j],j+1,len(L),j+1)
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[0]:
        return 0
    elif key > L[len(L)-1]:
        return len(L)
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid+1
        elif L[mid] >= key:
            return b_search(L,key,left,mid)
        else:
            return b_search(L,key,mid,right)

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += b_search(L[j+1:],L[i]+L[j],0,len(L[j+1:]))
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] > keyを満たす最小のi
    #L[i] >= key > L[i+1]
    if key > L[0]:
        return len(L)
    elif key <= L[len(L)-1]:
        return 0
    else:
        mid = (left+right)//2
        if L[mid] >= key and key > L[mid+1]:
            return len(L) - mid - 1
        elif L[mid] < key:
            return b_search(L,key,left,mid)
        else:
            return b_search(L,key,mid,right)

ans = 0
for i in reversed(range(2,N)):
    for j in reversed(range(1,i)):
        ans += b_search(L[:j],L[i]+L[j],0,len(L[:j]))
print(ans)

Submission status: Time Limit Exceeded

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

def b_search(L,key,left,right):
    #L[i] < keyを満たす最大のi
    #L[i] < key <= L[i+1]
    if key <= L[0]:
        return 0
    elif key > L[len(L)-1]:
        return len(L)
    else:
        mid = (left+right)//2
        if L[mid] < key and key <= L[mid+1]:
            return mid+1
        elif L[mid] >= key:
            return b_search(L,key,left,mid)
        else:
            return b_search(L,key,mid,right)

ans = 0
for i in range(N):
    for j in range(i+1,N):
        if j < N-1:
            ans += b_search(L[j+1:],L[i]+L[j],0,len(L[j+1:]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations


def check(l):
    for i in range(3):
        a = l[i % 3]
        b = l[(i + 1) % 3]
        c = l[(i + 2) % 3]
        if a >= b + c:
            return False
    return True


n = int(input())
l = list(map(int , input().split()))
cnt = 0
for v in combinations(l, 3):
    if check(v):
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
import string
import heapq
import bisect
import sys
sys.setrecursionlimit(10**7)

# max(a-b,b-a) < c < a+b

N = int(input())
L = list(map(int,input().split()))
L.append(2**63)
L.sort()

ans = 0;
for i in range(N):
    for j in range(N):
        if i >= j:
            continue
        l = bisect.bisect_left(L,max(L[i]-L[j],L[j]-L[i])+1)
        r = bisect.bisect_right(L,L[i]+L[j]-1)
        cnt = r-l;
        if L[l] <= L[i] < L[r]:
            cnt -= 1
        if L[l] <= L[j] < L[r]:
            cnt -= 1
        ans += cnt
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
import sys
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):
        ans += sum(L[i] + L[j] > l for l in L[j+1:])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

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

ans = 0
for i in range(N):
    for j in range(i + 1, N - 1):
        t = np.count_nonzero(L[j + 1:] < L[i] + L[j])
        if not t:
            continue
        else:
            ans += t

print(ans)

Submission status: Time Limit Exceeded

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

dic = {}
ans = 0
for i in range(n-1):
  for j in range(i+1, n):
    tmp = l[i] + l[j]
    
    if tmp in dic.keys():
      index2 = dic[tmp]
    else:
      index2 = bisect.bisect_left(l, tmp)
      dic[tmp] = index2
      
    if l[j] in dic.keys():
      index1 = dic[l[j]]
    else:
      index1 = bisect.bisect_left(l, l[j])
      dic[l[j]] = index1
      
    
    if l[i] == l[j]:
      d = index2 - index1 - 2
    else:
      d = index2 - index1 - 1
    
    ans += max(d,0)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]
len_l = len(l)
ans = 0

for s in range(len_l):
    a = l[s]
    t = s + 1
    while t <= len_l - 1:
        b = l[t]
        u = t + 1
        while u <= len_l - 1:
            c = l[u]
            if a != b and b != c and a != c and a < b + c and b < c + a and c < a + b:
                ans += 1
            u += 1
        t += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

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[i] + L[j] > L[k]:
                ans += 1
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

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[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()))
res = 0

for i in range(N):
  for j in range(i+1, N):
    for k in range(j+1, N):
      if L[i] < L[j]+L[k] and L[j] < L[i]+L[k] and L[k] < L[i]+L[j]:
        res += 1
        
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
res = 0
all_p = N*(N-1)*(N-2)/6

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] or L[j] >= L[i]+L[k] or L[k] >= L[i]+L[j]:
        res += 1
        
print(int(all_p - res))

Submission status: Time Limit Exceeded

Solution code:
n, l = open(0)
n = int(n)
l = list(map(int, l.split()))
l.sort()
print(sum([l[i] < (l[i]+l[j]+l[k]) / 2
           for i in range(n) for j in range(i) for k in range(j)]))

Submission status: Time Limit Exceeded

Solution code:
n, l = open(0)
n = int(n)
l = list(map(int, l.split()))
print(sum([(max([l[i],l[j],l[k]])<sum([l[i],l[j],l[k]])/2) for i in range(n) for j in range(i) for k in range(j)]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = sorted(list(map(int, input().split())))
ans = 0
for i in range(n):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    ans += bisect.bisect_left(l, b+a, lo = j + 1)-j-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = sorted(list(map(int, input().split())))
ans = 0
for i in range(n):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    ans += (bisect.bisect_left(l, b+a, lo = j + 1)-j-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
import bisect
ans = 0
for i in range(n):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    bel = bisect.bisect_left(l, b+a, lo = j + 1)
    ans += (bel-j-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x=int(input())
y=list(map(int, input().split()))

y.sort()

a=0
b=0
c=0
ans=0
for i in range(x):
	a=y[i]
	for j in range(i+1,x):
		b=y[j]
		for k in range(j+1,x):
			c=y[k]
			if a+b>c:
				ans=ans+1
			else:
				break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import itertools as it

def main():
    N = int(input())
    L = input_nums()
    count = 0
    for v in it.combinations(L, 3):
        a, b, c = v
        if a < b + c and b < c + a and c < a + b:
            count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0
for i in range(N):
  for j in range(i+1,N):
    if i != j:
      for k in range(j+1,N):
        if i !=k and j !=k: 
          if S[i] + S[j] > S[k] and S[j] + S[k] > S[i] and S[k] + S[i] > S[j] :

            count +=1

print(count)

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):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    bel1 = j #OK
    up1 = n #OUT
    while up1- bel1 > 1:
      mid = (bel1 + up1)//2
      if a + b > l[mid]:
        bel1 = mid
      else:
        up1 = mid
    ans += (bel1-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):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    bel1 = 0 #OK
    up1 = n #OUT
    while up1- bel1 > 1:
      mid = (bel1 + up1)//2
      if a + b > l[mid]:
        bel1 = mid
      else:
        up1 = mid
    ans += (bel1-j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=input().split()
A=[int(a) for a in L]
B=sorted(A)
answer=0
for a in range(N-1):
    number=1
    while a+number<N-1:
        suuji = number + 1
        saisho=B[a]
        tonari=B[a+number]
        goukei=saisho+tonari
        while a+suuji<N:
            tar=B[a+suuji]
            if goukei>tar:
                answer+=1
            else:
                break
            suuji+=1
        number+=1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
##print(L)
a1 = L.pop(0)
ans = 0
while(True):
    if L==[]:break
    
    for i in range(len(L)-1):
        l = [j for j in L[i:] if j<a1+L[i]]
        #print(l)
        for k in range(len(l)-1):
            if a1<L[i]+l[k] and L[i]<a1+l[k] and l[k]<L[i]+a1:
                ans +=1
        """
        for j in range(len(L)-1-i):
            if a1<L[i]+L[i+j+1] and L[i]<a1+L[i+j+1] and L[i+j+1]<L[i]+a1:
                ans +=1
        """
    
    a1=L.pop(0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
#L.sort()
##print(L)
a1 = L.pop(0)
ans = 0
while(True):
    if L==[]:break
    
    for i in range(len(L)-1):
        for j in range(len(L)-1-i):
            if a1<L[i]+L[i+j+1] and L[i]<a1+L[i+j+1] and L[i+j+1]<L[i]+a1:
                ans +=1
    
    a1=L.pop(0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n=int(input())
lists=list(map(int,input().split()))
lists=sorted(lists)

cnt=0
import math
for i in range(n):
    for j in range(i+1,n):
        #i<j 
        x=lists[i]
        y=lists[j]
        left=i 
        right=j 
        index=bisect.bisect_right(lists,y-x)
        cnt+=min(j-i-1,j-index)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
     for j in range(i + 1,n-1):
          flag = True
          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()))
l.sort()
ans = 0
for i in range(n-2):
     for j in range(i + 1,n-1):
          flag = True
          for k in range(j + 1,n):
               if l[i] + l[j] > l[k] and flag :
                    ans += 1
               else:
                    flag = False
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n-2):
    for j in range(i+1,n-1):
        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]:
                ans += 1
            else:
                break
            
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for a in range(N - 2):
    b = a + 1
    c = a + 2
    while c < N and b < N - 1:
        if L[c] >= L[a] + L[b]:
            ans += c - 1 - b
            b += 1
            if b == c:
                c += 1
        else:
            if c == N - 1:
                ans += c - b
                b += 1
            else:
                c += 1
        
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

from itertools import combinations
from bisect import bisect_right,bisect_left

comb=combinations(L, 2)
cnt=0
for cb in comb:
    b,c=min(cb), max(cb)
    # a must be b or lower
    hi=bisect_left(L, b)
    low=bisect_right(L, c-b)
    #print(b,c,hi,low)
    cnt+=max(0,hi-low)
print(cnt)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from bisect import bisect_right,bisect_left

comb=combinations(L, 2)
cnt=0
for cb in comb:
    b,c=min(cb), max(cb)
    # a must be b or lower
    hi=bisect_left(L, b)
    new_L=L[:hi]
    low=bisect_right(new_L, c-b)
    #print(b,c,hi,low)
    cnt+=max(0,hi-low)
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def main():
    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):
            ans+=bisect.bisect_left(l[j+1:],l[i]+l[j])
    print(ans)

if __name__ == '__main__':
    main()