Problem:


Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list, item):
    low = 0
    lengyh = len(list)
    if lengyh != 0 and list[lengyh - 1] < item:
        return lengyh - 1
    high = lengyh - 1

    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]

        if guess == item - 1 or (mid < lengyh - 1 and (guess < item and list[mid + 1] > item)) or (mid == lengyh - 1 and guess < item):
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1

    return None


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
for i in range(N):
    a = L[i]
    for j in range(i, N - 1):
        b = L[j + 1]
        index = binary_search(L[j + 2:], a + b)
        if index is None:
            continue
        else:
            for k in range(0, index + 1):
                c = (L[j + 2:])[k]
                if b + c > a and a + c > b:
                    num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list, item):
    low = 0
    lengyh = len(list)
    high = lengyh - 1

    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]

        if guess == item - 1 or (mid < lengyh - 1 and (guess < item and list[mid + 1] > item)) or (mid == lengyh - 1 and guess < item):
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1

    return None


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
for i in range(N):
    a = L[i]
    for j in range(i, N - 1):
        b = L[j + 1]
        index = binary_search(L[j + 2:], a + b)
        if index is None:
            continue
        else:
            for k in range(0, index + 1):
                c = (L[j + 2:])[k]
                if b + c > a and a + c > b:
                    num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
cnt = 0
for i in range(n):
  for j in range(i):
    a = l[i]+l[j]
    for k in l:
      if a > k:
        continue
      cnt += 1
print(n*(n-1)*(n-2)//6-cnt)

Submission status: Time Limit Exceeded

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

N = int(readline())
L = list(map(int,readline().split()))
L = sorted(L)

# 2本固定すると、もう一本として選べるのは、2本の和より長いものだけ
# これを二分探索で数える

#print(L)
import bisect
ans = 0
for i in range(N - 1):
  for j in range(i + 1,N):
    limit = L[i] + L[j]
    ng = len(L)
    ok = j
    while abs(ok - ng) > 1:
      mid = abs(ok + ng) // 2
      if L[mid] < limit:
        ok = mid
      else:
        ng = mid
#    print("i",i,"j",j,"L[i]",L[i],"L[j]",L[j],"limit",limit,ok - j)
    ans += ok - j
        
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[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.sort()
ans=0
for i in range(len(l)-2):
    for j in range(i+1,len(l)-1):
        for k in range(j+1,len(l)):
            if l[i]+l[j]>l[k]:
                ans+=1
            else:
                break
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 i in combinations(l,3):
    if abs(i[1] - i[2]) < i[0] and i[1] + i[2] > i[0]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as com

n=int(input())
L=list(map(int,input().split()))
ans=0
for i,j,k in com(L,3):
    if i+j>k and j+k>i and k+i>j:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as com
def tri(a,b,c):
  if a+b>c and b+c>a and c+a>b:
    return 1
  else:
    return 0
n=int(input())
L=list(map(int,input().split()))
ans=0
for i,j,k in com(L,3):
    ans+=tri(i,j,k)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))


# 条件を満たすかどうか
def is_ok(l, i, key):
    # ここが条件
    if l[i] >= key:
        return True


def binary_seach(l, key) -> int:
    left = -1
    right = len(l)

    while right - left > 1:
        mid = left + (right - left) // 2
        if is_ok(l, mid, key):
            right = mid
        else:
            left = mid

    return right


n = I()
l = LI()
l = sorted(l)
ans = 0
for i in range(n - 2):
    # 三角形のうち一番短い辺
    a = l[i]
    for j in range(i + 1, n - 1):
        # 三角形のうち二番目に短い辺
        b = l[j]
        # 三角形の中で一番大きい辺 かつ
        # a + b より小さい辺 を二分探索で探す
        # a + b 以上の要素の初めのindexを取得
        ind = binary_seach(l, a + b)
        # b 以上の辺のうち、a + b より小さい要素の数を足す
        ans += ind - j - 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):
    for k in range(j+1,n):
      if l[i]+l[j]>l[k]:
        c+=1
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def f(A):
    N = len(A)
    cnt = 0
    
    for i in range(N):
        for j in range(i + 1, N):
            for k in range(j + 1, N):
                cnt += (A[k] < A[i] + A[j])
    return cnt


N = int(input())
A = np.int32(input().split())
A.sort()
print(f(A))

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n):
    for j in range(i+1,n):
        l = j
        r = n
        while r-l>1:
            mid = (r+l)//2
            if m[mid] < m[i]+m[j] and m[i] < m[mid]+m[j] and m[j] < m[mid]+m[i]:
                l = mid
            else:
                r = mid
        ans += l-j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

def binary_search(l,r,v):
    while r >= l:
        h = (l+r) // 2
        if A[h] < v:
            l = h+1
        else:
            r = h-1
    return r

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        a = binary_search(j,n-1,A[i]+A[j])
        ans += a-j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

def binary_search(l,r,v):
    while r >= l:
        h = (l+r) // 2
        if A[h] < v:
            l = h+1
        else:
            r = h-1
    return r

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        a = binary_search(0,n-1,A[i]+A[j])
        ans += a-j

print(ans)

Submission status: Time Limit Exceeded

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

    ans = 0
    for ia in range(N):
        for ib in range(ia+1, N-1):
            a = L[ia]
            b = L[ib]

            c_min = max(a-b, b-a)
            c_max = a + b

            for ic in range(ib+1, N):
                c = L[ic]
                if c >= c_max:
                    break
                if c <= c_min:
                    continue
                ans += 1

    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

def triangle_exists(a, b, c):
    return ((a < b + c) and 
            (b < c + a) and
            (c < a + b))

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

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

main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        #print(l[i],l[j])
        for z in range(j+1,n):
            if l[i]+l[j]>l[z]:
                cnt  += 1
                #print(l[i],l[j],l[z])
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        #print(l[i],l[j])
        for z in range(j+1,n):
            if l[i]+l[j]>l[z] and l[z]+l[i]>l[j]:
                cnt  += 1
                #print(l[i],l[j],l[z])
print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1, N-1):
        left = j
        right = N
        target = L[i]-L[j]
        while right-left > 1:
            mid = (right + left)//2
            #print(mid)

            if L[mid] > target:
                left = mid
            else:
                right = mid

        ans += left-j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
numbers = [int(i) for i in input().split()]
numbers = sorted(numbers)
counter = 0

for a in range(len(numbers)-2):
    for b in range(a+1, len(numbers)-1):
        for c in range(b+1, len(numbers)):
            if numbers[c] < numbers[a] + numbers[b]:
                counter += 1

print(counter)

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 - 1):
    for j in range(i + 1, N):
        p = l[:i]
        a, b = l[i], l[j]
        low = b - a + 1
        high = a + b
        ans += bisect.bisect_left(p, high) - bisect.bisect_left(p, low)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

arr.sort()
ans = 0
for i in range(n):
    for j in range(i+1,n):
        a,b = arr[i],arr[j]

        cmin = abs(a-b)
        cmax = a+b

        l = bisect.bisect_left(arr,cmin+1,lo=j+1)
        r = bisect.bisect_right(arr,cmax-1,lo=j+1)
        # print(cmin,l,cmax,r)
        if r-l > 0:
            ans += r-l

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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


def quicksort(a, left: int, right: int):
    #a[left]〜a[right]をクイックソート
    pl=left
    pr=right
    pivot= a[(left +right)//2]

    #print(f'a[{left}]~a[{right}] : ', *a[left : right +1])

    while pl<=pr:
        while a[pl] <pivot: pl+=1
        while a[pr] >pivot: pr-=1
        if pl <= pr:
            a[pl], a[pr]= a[pr], a[pl]
            pl+=1
            pr-=1

    if left < pr: quicksort(a, left, pr)
    if pl < right: quicksort(a, pl, right)

def quick_sort(a):
    quicksort(a, 0, len(a)-1)

quick_sort(l)
count=0
for i in range(n-2):
    for j in range(i+1,n-1):
        x=l[i]+l[j]
        for k in range(j+1,n):
            if x<=l[k]:
                break
            count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=sorted(list(map(int,input().split())))
cnt=0
for i in range(N-1):
    l=A[i]
    for j in range(i+1,N):
        m=A[j]
        for a in A[:i]:
            if a<l+m and l<a+m and m<a+l:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
l=list(mi())
l=sorted(l)
s=list(combinations(l,3))
ans=0
for i,j,k in s:
  if i+j>k:
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
sort_l = sorted(l)
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):        
        for k in range(j+1, n):
            two_line = sort_l[i] + sort_l[j]
            one_line = sort_l[k]
            if two_line > one_line:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = list(map(int, input().split()))
combination_line = list(combinations(l, 3))
count = 0
for i in combination_line:
    line1 = i[0]
    line2 = i[1]
    line3 = i[2]
    if line1 < line2 + line3 and line2 < line1 + line3 and line3 < line1 + line2:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
triangle=[]
count=0
L=list(map(int,input().split()))
for i in combinations(L,3):
    triangle.append(list(i))
for i in triangle:
    #print(i)
    if i[0]<i[1]+i[2] and i[1]<i[2]+i[0] and i[2]<i[1]+i[0]:
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools 

n = int(input())

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

cnt = 0
for a,b,c in itertools.combinations(L,3):
    if a >= b+c:
        continue
    if b >= a+c:
        continue
    if c >= a+b:
        continue
    
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

for a,b in itertools.combinations(A,2):
    total = a + b
    l = bisect.bisect(A,max(a,b))
    r = bisect.bisect_left(A,total) 
    ans += max(0,r-l)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools 

n = int(input())

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

cnt = 0
for a,b,c in itertools.combinations(L,3):
    if a >= b+c:
        continue
    if b >= a+c:
        continue
    if c >= a+b:
        continue
    
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i,j,k in combinations(L, 3):
    res = sorted([i,j,k])
    if sum(res[:2]) > res[2]:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = list(map(int, input().split()))
ans = 0
gr = itertools.groupby(l)
check = list(itertools.combinations(l, 3))
for i in range(len(check)):
    if 2 * max(check[i]) - sum(check[i]) < 0:
        ans += 1
print(ans)

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):
    for j in range(i):
        if ls[j]*2 <= ls[i]:
            continue
        for k in range(j):
            if ls[i] < ls[k] + ls[j]:
                p += 1
print(p)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
n = int(input())
L = list(map(int, input().split()))
L.sort()

cnt = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        for c in range(b+1, bisect_left(L,L[a]+L[b])):
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
n = int(input())
L = list(map(int, input().split()))
L.sort()

cnt = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        for c in range(b+1, bisect_left(L,L[a]+L[b])):
            if L[c]>=L[a]+L[b]:
                break
            else:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

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):
    x = a[i]
    for j in range(i+1,n-1):
        y = a[j]
        for k in range(j+1,n):
            if a[k] < x+y:
                ans += 1
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
 
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)

ans = 0
for i in range(n-2):
    x = a[i]
    for j in range(i+1,n-1):
        y = a[j]
        if x/2 > y:
            break
        for k in range(j+1,n):
            if y+a[k] > x:
                ans += 1
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
 
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

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

c = itertools.combinations(a,3)

ans = 0
for v in c:
    x,y,z = v[0],v[1],v[2]
    if x < y+z and y < x+z and z < x+y:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input().split()

ans = 0
for i in range(0,N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      a1 = int(a[i])
      a2 = int(a[j])
      a3 = int(a[k])
      sum = a1 + a2 + a3
      #print(a1, a2, a3, sum)
      if(sum > 2 * a1):
        if(sum > 2 * a2):
          if(sum > 2 * a3):
            ans += 1
      else:
        pass
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def triangleChecker(a, b, c):
    if a >= b + c :
        return False
    else:
        if b >= c + a:
            return False
        else :
            if c >= a + b:
                return False
            else :
                return True
            
N = int(input())
numbers = [int(i) for i in input().split()]

counter = 0

for a in range(len(numbers)-2):
    for b in range(a+1, len(numbers)-1):
        for c in range(b+1, len(numbers)):
            if triangleChecker(numbers[a], numbers[b], numbers[c]):
                # print(combi)
                counter += 1

print(counter)

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-2) :
    for j in range(i+1,N-1) :
        left = j-1
        right = N
        while right - left > 1 :
            mid = (left + right) // 2
            if (L[i] < L[j] + L[mid]) and (L[j] < L[i] + L[mid]) and (L[mid] < L[i] + L[j]) :
                left = mid
            else : right = mid
        ans += max(left - j, 0 )

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) :
            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

N = int(input())
L = [int(n) for n in input().split()]
L = sorted(L)

triples = set()

for i in range(len(L)):
    for j in range(i):
        a = L[i]
        b = L[j]

        right_index = j
        left_index = bisect.bisect_right(L, a-b)
        
        for k in range(left_index, right_index):
            c = L[k]
            triple = frozenset([a,b,c])
            if len(triple) == 3:
                triples.add(triple)

print(len(triples))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = sorted([int(n) for n in input().split()])

triples = set()

for i in range(len(L)):
    for j in range(i):
        a = L[i]
        b = L[j]

        right_index = bisect.bisect_left(L, a + b)
        left_index = bisect.bisect_right(L, abs(a - b))
        
        for k in range(left_index, right_index):
            triple = frozenset([i,j,k])
            if len(triple) == 3:
                triples.add(frozenset([i,j,k]))

print(len(triples))

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest
import os


# 検索用タグ、尺取り、3つの内2つを固定
# https://drken1215.hatenablog.com/entry/2019/10/20/032700

# 実装を行う関数
def resolve():
    n = int(input())
    ls = list(map(int, input().split()))

    ls.sort()

    ans = 0
    # 尺取り
    for i in range(n - 2):
        a = ls[i]
        for j in range(i + 1, n - 1, 1):
            b = ls[j]
            for k in range(j + 1, n, 1):
                c = ls[k]
                if not c < a + b:
                    break
                ans += 1

    print(ans)


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
3 4 2 1"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3
1 1000 1"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """7
218 786 704 233 645 728 389"""
        output = """23"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_li = list(map(int,input().split()))
l_li.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_li[k] >= l_li[i]+l_li[j]:
                break
            if l_li[i] < l_li[j]+l_li[k] and l_li[j] < l_li[i]+l_li[k] and l_li[k] < l_li[i]+l_li[j]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_li = 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):
            if l_li[i] < l_li[j]+l_li[k] and l_li[j] < l_li[i]+l_li[k] and l_li[k] < l_li[i]+l_li[j]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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):
            L = j
            R = n
            ij = l[i] + l[j]
            while R - L != 1:
                if ij <= l[j + 1]:
                    break
                now = (L + R)//2
                if ij > l[now]: #三角形になる
                    L = now
                else:
                    R = now
            ans += L - j

    print(ans)

main()

Submission status: Time Limit Exceeded

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

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        L = j
        R = n
        ij = l[i] + l[j]
        while R - L != 1:
            if ij <= l[j + 1]:
                break
            now = (L + R)//2
            if ij > l[now]: #三角形になる
                L = now
            else:
                R = now
        ans += L - j

print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

L = sorted(L)

count = 0

for i in range(N - 1):
    for j in range(i + 1, N - 1):
        x, y = L[i], L[j]
        top = N
        bottom = j + 1
        tmp = (top + bottom) // 2
        
        while(top - bottom > 1):
            if L[tmp] >= x + y:
                top = tmp
                tmp = (top + bottom) // 2
            elif L[tmp] < x + y:
                bottom = tmp
                tmp = (top + bottom) // 2
        if bottom - j == 1:
            if L[bottom] < x + y:
                count += 1
        else:
            count += (bottom - j)

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

count = 0
for l in list(itertools.combinations(L, 3)):
    if l[0] < l[1] + l[2] and l[1] < l[0] + l[2] and l[2] < l[0] + l[1]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools, bisect

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

ans = 0
for i in range(N):
    for j in range(i+1, N):
        cindex = bisect.bisect_left(L, int(L[i]) + int(L[j]))
        for _ in reversed(range(j+1, cindex)):
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
L = input().split()

cand = itertools.combinations(L,3)

ans = 0
for c in cand:
    a, b, c = int(c[0]), int(c[1]), int(c[2])
    if a < b + c and b < a + c and c < a + b:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = [int(s) for s in input().split(' ')]
L_sorted = sorted(L) + [10**5 + 1]
L_reversed = sorted(L, reverse=True)
ret = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        a = L_reversed[i]
        b = L_reversed[j]
        # c < a + b
        # c > a - b
        # c > b - a 
        index_l = bisect.bisect_right(L_sorted, abs(a - b))
        exclude = 0
        index_r = bisect.bisect_left(L_sorted, a + b)

        if L_sorted[index_l] <= a <= L_sorted[index_r - 1]:
            exclude += 1
        if L_sorted[index_l] <= b <= L_sorted[index_r - 1]:
            exclude += 1
        ret += max(0, index_r - index_l - exclude)

print(ret // 3)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = [int(s) for s in input().split(' ')]
L_sorted = sorted(L) + [10**5 + 1]
ret = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        a = L[i]
        b = L[j]
        # c < a + b
        # c > a - b
        # c > b - a 
        index_l = bisect.bisect_right(L_sorted, abs(a - b))
        exclude = 0
        index_r = bisect.bisect_left(L_sorted, a + b)

        if L_sorted[index_l] <= a <= L_sorted[index_r - 1]:
            exclude += 1
        if L_sorted[index_l] <= b <= L_sorted[index_r - 1]:
            exclude += 1
        ret += max(0, index_r - index_l - exclude)

print(ret // 3)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
N = int(input())
L = sorted([int(s) for s in input().split()])

MAX_SEARCH = max(L) + 1


def check_search(middle, idx_short2, l_lng, l_lows):
    return l_lows[middle] + l_lows[idx_short2] > l_lng


l_lows = L[:2]
triangles = 0


for l_lng in L[2:]:
    for idx, l_low in enumerate(l_lows):
        left = 0
        right = len(l_lows) - 1 - idx
        end = right
        while (left < right):
            middle = (left + right) // 2
            if check_search(middle, len(l_lows) - 1 - idx, l_lng, l_lows):
                right = middle
            else:
                left = middle + 1
        triangles += (end - right)
    l_lows.append(l_lng)

print(triangles)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = [int(s) for s in input().split(' ')]
L_sorted = sorted(L)
ret = 0
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]
            # c < a + b
            # c > a - b
            # c > b - a 
            if a < b + c and b < c + a and c < a + b:
                ret += 1

print(ret)

Submission status: Time Limit Exceeded

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

cnt = 0
for a in range(len(l) - 2):
	for b in range(a + 1, len(l) - 1):
		for c in range(b + 1, len(l)):
			if l[a] + l[b] > l[c]:
				cnt += 1
			else:
				break
print (cnt)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

N = int(input())
L = [int(n) for n in input().split()]

count = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            a = L[i]
            b = L[j]
            c = L[k]
            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())
edges = list(map(int, input().split(' ')))
edges= sorted(edges)[::-1]
ans = 0
for i in range(n):
  for j in range(i+1, n):
    for k in range(j+1, n):
      if edges[i] < edges[j] + edges[k]:
        ans += 1
      else:
        break
print(ans)