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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

cnt = [0]*(10**3+1)
for x in L:
	cnt[x] += 1

cnt = list(accumulate(cnt))

ans = 0
for x, y in combinations(L, 2):
	ans += cnt[min(x+y-1, 1000)] - cnt[abs(x-y)] - (abs(x-y) < x < x+y) - (abs(x-y) < y < x+y)

print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

n = int(input())
l = list(map(int, input().split()))
cnt = 0
for i in combinations(range(n), 3):
    a,b,c = i
    if (l[a] < l[b] + l[c]) and (l[b] < l[c] + l[a]) and (l[c] < l[b] + l[a]):
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:


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

count=0
le=len(S)
S.append(-100000)
for i in range(le-2):
    for j in range(i+1,le-1):
        if S[i]<S[j]+S[j+1]:
            left=j+1
            right=le
            while left<=right:
                mid=(left+right)//2
                if S[i]<S[j]+S[mid] and S[i]>=S[j]+S[mid+1]:
                    count+= mid-(j+1)+1
                    
                    break
                elif S[i]<S[j]+S[mid]:
                    left=mid+1
                elif S[i]>=S[j]+S[mid]:
                    right=mid-1
                else:
                    print("イレギュラーな例につかまりました")
                    exit()
                
print(count)

Submission status: Time Limit Exceeded

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

L_list = sorted(L_list)

count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            if L_list[i] + L_list[j] > L_list[k]:
                count += 1
            else:
                break

print(count)

Submission status: Time Limit Exceeded

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

L_list = sorted(L_list)

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

print(count)

Submission status: Time Limit Exceeded

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

L_list = sorted(L_list)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools 

N=int(input())
S=list(map(int,input().split()))
S.sort()
S.reverse()
C=list(itertools.combinations(S,3))
count=0
for i in C:
    if i[0]<i[1]+i[2]:
        count+=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
R=0
N=int(input())
L=list(map(int,input().split()))
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 and c+b>a and a+c>b:
        R+=1
print(R)

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):
        a = L[i]
        b = L[j]
        l = bisect.bisect_right(L, abs(a-b))
        r = bisect.bisect_left(L, a+b)
        ans += r-l
        if a in L[l:r]:
            ans -= 1
        if b in L[l:r]:
            ans -= 1
        
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for a in range(N):
    for b in range(a+1, N):
        Ll = L[b+1:N]
        la = L[a]
        lb = L[b]
        ab = la + lb
        sa = abs(la-lb)
        first = bisect.bisect_left(Ll, ab) -1 
        second = bisect.bisect_right(Ll, sa)
        count += first - second +1
print(count)

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):
        left=abs(l[i]-l[j])
        right=l[i]+l[j]
        ans+=len(list(filter(lambda x: left<x<right, l[j+1:])))
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[k]+l[i] and l[k]<l[i]+l[j]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

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

"""
nC3
|b - c| < a < |b + c|

nC2
b_i, c_iを除くiの中で|b-c|< a <b+cを満たすaの数をタス
"""

N = int(input())
L = LI()
L.sort()

ans = 0
for i in range(N - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        # print(i, j)
        a,b = L[i], L[j]
        # print(L[:j])
        # print(bisect.bisect_right(L[:j], a- b))
        ans += j - bisect.bisect_right(L[:j], a- b)
        # ans += bisect.bisect_right(L[:j], a-b) - bisect.bisect_left(L[:j], a-b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

def main():
    n = int(input())
    ll = list(map(int, input().split()))
    ll.sort()

    ans = 0
    for i in range(n-2):
        e1 = ll[i]
        for j in range(i+1, n-1):
            e2 = ll[j]
            ans += bisect.bisect_left(ll[j+1:], e1+e2)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
for i in range(n-2):
    e1 = ll[i]
    for j in range(i+1, n-1):
        e2 = ll[j]
        ans += bisect.bisect_left(ll[j+1:], e1+e2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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 a < b + c and b < c + a and c < a + b:
                ans += 1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for i,j,k in itertools.combinations(l, 3):
  if i < j+k and j < i+k and k< j+i:
    count += 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = sorted(list(map(int,input().split())))
lenl = len(L)
count = 0
for i in range(0,lenl-2):
    for j in range(i+1,lenl-1):
        count += bisect.bisect_left(L[j+1:],L[i]+L[j])
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

counter=0
for i in range(2,n):

	for j in range(1,i):
		now=i+1
		while now <= n:
			if llist[j-1] + llist[i-1] > llist[now-1]:
				counter += 1
				now += 1
			else:
				now += n
			

print(counter)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from itertools import combinations
from bisect import bisect_left, bisect_right

def solve(N, lines):
    ans = 0
    for c in range(N-1, -1, -1):
        for b in range(c-1, -1, -1):
            i = bisect_right(lines[:b], lines[c] - lines[b])
            ans += max(0, (b-i))
    return ans


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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):
        sum=l[i]+l[j]
        left=j
        right=n-1
        mid=(left+right)//2
        while right-left>1:
            if l[mid]>=sum:
                right = mid
            else:
                left = mid
            mid = (left+right)//2
        if l[mid+1]<sum:
            mid+=1
        ans+=mid-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):
        sum=l[i]+l[j]
        for k in range(j+1,n):
            if l[k]<sum:
                ans+=1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


import numpy as np


def main():

    n = gete(int)
    l = list(geta(int))
    l.sort()

    l_max = 2000
    # p[k]: number of sticks with length >= k
    p = np.zeros((l_max + 1), dtype=np.int64)

    for li in l:
        p[:li + 1] += 1

    ans = 0
    for i, a in enumerate(l):
        for b in l[i + 1:]:
            ans += p[abs(a - b) + 1] - p[a + b]

            if abs(a - b) < a:
                ans -= 1

            if abs(a - b) < b:
                ans -= 1
    print(ans // 3)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    l = sorted(list(map(int, input().split())), reverse=True)
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1,n-1):
            b = l[j]
            x = a-b
            z = a+b
            for k in range(j+1, n):
                c = l[k]
                if x >= c:
                    break
                if c < z:
                    count += 1
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def main():
    n = int(input())
    l = sorted(list(map(int, input().split())), reverse=True)
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1,n-1):
            b = l[j]
            x = a-b
            z = a+b
            for k in range(j+1, n):
                c = l[k]
                if x >= c:
                    break
                if c < z:
                    count += 1
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def main():
    n = int(input())
    l = map(int, input().split())
    ts = set()
    for a,b,c in itertools.combinations(l, 3):
        if a < b+c and b < c+a and c < a+b:
            ts.add((a,b,c))
    print(len(ts))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

L.sort()

ans = 0
for a in range(len(L)):
    for b in range(a+1,len(L)):
        c_max = L[a]+L[b]-1
        left = b+1
        right = len(L)
        mid = (right+left)//2
        while left<right:
            mid = (right+left)//2
            if c_max < L[mid]:
                right = mid
            else:
                left = mid+1
        ans += left-(b+1)
       
        
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int, input().split()))
L.sort()
count = 0
for a in range(n-2):
  for b in range(a+1,n-1):
    if L[a] + L[b] > L[n-1]:
      count += n - b - 1
      continue
    left = b + 1
    right = n - 1
    while right - left > 0:
      c = (left+right) // 2
      if L[c] < L[a] + L[b]:
        left = c + 1
      else:
        right = c
    count += left - b - 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int, input().split()))
count = 0
for a in range(n):
  for b in range(a+1,n):
    for c in range(b+1,n):
      if L[a] < L[b] + L[c]:
        if L[b] < L[a] + L[c]:
          if L[c] < L[a] + L[b]:
            count += 1
print(count)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# 二分探索をするためにはリストをソートする
l.sort()

#これは作らなくてもいい（bisectを使えばいい）
def binary_search(a, x):
    left = 0
    right = len(a)
    while left < right:
        mid = (left + right) // 2
        if a[mid] < x:
            left = mid + 1
        else:
            right = mid
    return left

ans = 0
for a in range(n-2):
    for b in range(a+1,n-1):
        # aとbは固定で考える
        x = l[a] + l[b]
        index = binary_search(l, x)
        ans += max(0, index - b - 1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    L = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    L = sorted(L, reverse=True)

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

    print(cnt)
resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
p = []
for i in range(2**n):
  tmp = [0]*n
  for j in range(n):
    if i>>j & 1:
      tmp[j] = 1
  if(tmp.count(1) == 3):
    p.append(tmp)
    
for i in range(0,len(p)):
  tmp = []
  for j in range(len(p[i])):
    if(p[i][j] == 1):
      tmp.append(l[j])
  if(tmp[0] < tmp[1] + tmp[2] and tmp[1] < tmp[2] + tmp[0] and tmp[2] < tmp[1] + tmp[0]):
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

# import bisect

L.sort()
# print(L)

ans = 0
for l1ix in range(N-1, -1, -1):
    for l2ix in range(l1ix - 1, -1, -1):
        # binsearch
        l = -1
        r = l2ix + 1

        while (r - l) > 1:
            mid = l + (r - l) // 2

            if L[mid] < (L[l1ix] + L[l2ix]) and L[l2ix] < L[mid] + L[l1ix] and L[l1ix] < L[mid] + L[l2ix]:
                #ok
                r = mid
            else:
                #ng
                l = mid
        
        l3ix = r
        # if l2ix != l3ix:
        #     print("1,2,3=", L[l1ix], L[l2ix], L[l3ix])
        
        anst = l2ix - l3ix
        ans += anst if anst > 0 else 0

print(ans)
        


"""
O(N^3)だから間に合わない．明らかに間に合うアルゴリズムを探す
ans = 0
for aix in range(N):
    for bix in range(aix + 1, N):
        for cix in range(bix + 1, N):
            if L[aix] < L[bix] + L[cix] and L[bix] < L[aix] + L[cix] and L[cix] < L[aix] + L[bix]:
                # print("ok:", aix,",", bix,",", cix)
                ans += 1

print(ans)
"""

Submission status: Time Limit Exceeded

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

ans = 0
for aix in range(N):
    for bix in range(aix + 1, N):
        for cix in range(bix + 1, N):
            if L[aix] < L[bix] + L[cix] and L[bix] < L[aix] + L[cix] and L[cix] < L[aix] + L[bix]:
                # print("ok:", aix,",", bix,",", cix)
                ans += 1

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

Submission status: Time Limit Exceeded

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

def check(a, b, c):
    if c<(a+b):
        return True
    else:
        return False

ans = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        l = b
        r = N
        while (r-l)>1:
            mid = (r+l)//2
            if check(L[a], L[b], L[mid]):
                l = mid
            else:
                r = mid
        if l!=(b):
            ans = ans + l-(b)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())

in_ar = [0 for _ in range(n)]
in_ar = [int(s) for s in input().split()]

al_tr = list(itertools.combinations(in_ar,3))
cnt = 0

for ar in al_tr:
    a,b,c = ar[0],ar[1],ar[2]
    if(a+b > c and a+c > b and c+b > a):
        cnt += 1

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):
        k=j+1
        while k<n:
            if l[i]+l[j]>l[k]:
                k+=1
                ans+=1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import itertools
n = int(input())
l = list(map(int, input().split()))
l.sort()
a = 0
for i in itertools.combinations(range(n), 3):
    a += l[i[-1]] < l[i[0]] + l[i[1]]
print(a)

Submission status: Time Limit Exceeded

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

import itertools
import math

ans = 0
for i in  itertools.combinations(d, 3):
    #print(i , ans,i[0] < i[1] + i[2] , i[1] < i[0] + i[2] , i[2] < i[0] + i[1],i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1])
    if i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1]:
        ans += 1

print(int(ans))

Submission status: Time Limit Exceeded

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

import itertools
import math

ans = 0
for i in  itertools.permutations(d, 3):
    #print(i , ans,i[0] < i[1] + i[2] , i[1] < i[0] + i[2] , i[2] < i[0] + i[1],i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1])
    if i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1]:
        ans += 1

print(int(ans/math.factorial(3)))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)
index = [i for i in range(N)]
cnt = 0
for a,b in combinations(index, 2):
    if b != N-1:
        for c in range(b+1,N):
            if L[a] <L[b]+L[c]:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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


from itertools import combinations as Cmb

count = 0
for cmb in Cmb(L, 3):
    a = cmb[0]
    b = cmb[1]
    c = cmb[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 = tuple(map(int, input().split()))
L = sorted(L, reverse=True)

counts = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        cc = a - L[j]
        left = j
        right = n
        while right - left > 1:
            mid = (right+left)//2
            if L[mid] > cc:
                left = mid
            else:
                right = mid
        counts += left - j
print(counts)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = tuple(map(int, input().split()))
L = sorted(L, reverse=True)
counts = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        b = L[j]

        left = j
        right = n
        while right - left > 1:
            mid = (right+left)//2
            if L[mid] > a-b:
                left = mid
            else:
                right = mid
        counts += left - j
print(counts)

Submission status: Time Limit Exceeded

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

counts = 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):
            if a - b < L[k]:
                counts += 1
            else:
                break
print(counts)

Submission status: Time Limit Exceeded

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

counts = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if a < L[j] + L[k]:
                counts += 1
print(counts)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  l=list(map(int,input().split()))
  l.sort()
  import bisect
  ans=0
  for i in range(n-2):
    for j in range(i+1,n-1):
      a=l[i]
      b=l[j]
      idxl=bisect.bisect_right(l[j+1:],abs(a-b))
      idxr=bisect.bisect_left(l[j+1:],abs(a+b))
      ans+=idxr-idxl
  print(ans)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

ans = 0
for a in range(N - 2):
    for b in range(a + 1, N - 1):
        C = L[b + 1:][L[b + 1:] < L[a] + L[b]]
        for c in range(len(C)):
            if L[a] + C[c] > L[b]:
                ans += len(C) - c
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())

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

Submission status: Time Limit Exceeded

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

count = 0
for i,a in enumerate(L[:N-2]):
    for j in range(i+1, N-1):
        before = abs(a-L[j])
        after = a+L[j]
        for c in L[j+1:]:
            if before < c < after:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

count = 0
for i,a in enumerate(L[:N-2]):
    for j in range(i+1, N-1):
        for c in L[j+1:]:
            if abs(a-L[j]) < c < a+L[j]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

def count(ls, n):
    left = 0
    right = len(ls)
    while right - left > 1:
        mid = (left + right) // 2

        if ls[mid] < n: left = mid
        if ls[mid] >= n: right = mid

    return left

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

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())
L = sorted([int(i) for i in input().split()])

S = [0 for i in range(max(L)+1)]
for i in range(N):
    S[L[i]] += 1
for i in range(1, max(L)+1):
    S[i] += S[i-1]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)

def count(ls, n):
    left = 0
    right = len(ls)
    while right - left > 1:
        mid = (left + right) // 2

        if ls[mid] > n: left = mid
        if ls[mid] <= n: right = mid

    return left

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_s = list(map(int,input().split()))
l_s.append(0)
l_s.sort()
ans = 0
#一番長い辺を選ぶ
#print(l_s)
for j in range(2,n):
    for i in range(j+1,n+1):
        if l_s[i] < l_s[j] + l_s[j-1]:
            
            OK = j-1
            NG = 0
            while (OK - NG) > 1:
                mid = (OK+NG)//2
                if l_s[i] < l_s[j] + l_s[mid]:
                    OK = mid
                else:
                    NG = mid
            ans = ans + j - OK 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)

def count(l, m, ls):
    left = 0
    right = len(ls)
    while right - left > 1:
        mid = (left + right) // 2

        if ls[mid] > l-m: left = mid
        if ls[mid] <= l-m: right = mid

    return right - 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_s = list(map(int,input().split()))
l_s.append(0)
l_s.sort()
ans = 0
#一番長い辺を選ぶ
#print(l_s)
for i in range(3,n+1):
    for j in range(2,i):
        if l_s[i] < l_s[j] + l_s[j-1]:
            
            OK = j-1
            NG = 0
            while (OK - NG) > 1:
                mid = (OK+NG)//2
                if l_s[i] < l_s[j] + l_s[mid]:
                    OK = mid
                else:
                    NG = mid
            ans = ans + j - OK 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)

def count(l, m, ls):
    if ls[0] <= l - m:
        return 0
    if ls[-1] > l - m:
        return len(ls)

    left = 0
    right = len(ls) - 1
    while right - left > 1:
        mid = (left + right) // 2

        if ls[mid] > l-m: left = mid
        if ls[mid] <= l-m: right = mid

    return left + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
L = [int(i) for i in input().split()]
print(sum([1 for a,b,c in combinations(L, 3) if a<b+c and b<c+a and c<a+b]))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

ans=0
for i in range(n):
    for j in range(i+1,n):
        ans+=bisect_left(l[j+1:], l[i]+l[j])

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

print(ans)