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 bisect

n = int(input())
l = sorted([int(i) for i in input().split()])
ans = 0
for d in range(n):
    for e in range(d+1, n):
        a, b = l[d], l[e]
        idx = bisect.bisect_right(l, a + b)
        for c in l[e+1:idx]:
            if not c < a + b:
                break
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

def tri_judge(a,b,c):
    if a<b+c and b<c+a and c<a+b:
        return 1
    else:
        return 0
cnt = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        check = L[i]+L[j]
        for k in range(j+1,n):
            if check>L[k]:
                if(tri_judge(L[i],L[j],L[k])):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

lengths.sort()

total = 0
for idx_a, a in enumerate(lengths[:-2]):
    for idx_b, b in enumerate(lengths[idx_a + 1 : -1]):
        borne = a + b
        for c in lengths[idx_a + idx_b + 2 :]:
            if c < borne:
                total += 1
            else:
                break

print(total)

Submission status: Time Limit Exceeded

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


def solve(N: int, L: "List[int]"):
    ans = 0
    L = sorted(L,reverse=True)
    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 ):
                    ans += 1
                else:
                    break
    print(ans)  

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def solve(N: int, L: "List[int]"):
    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            for k in range(j+1,N):
                if (L[i] < L[j] + L[k] and
                   L[j] < L[k] + L[i] and
                   L[k] < L[i] + L[j]):
                       ans += 1
    print(ans)  

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

print(ans)

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:
from bisect import bisect_left
n = int(input())
l = [int(i) for i in 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:
import sys
input = sys.stdin.readline
n = int(input())
l = [int(i) for i in input().split()]
l.sort(reverse=True)
ans = 0
for i in range(n-2):
  c = l[i]
  for j in range(i+1, n-1):
    b = l[j]
    k_max = n-1
    k_min = j
    while k_min!=k_max:
      k = (k_max+k_min)//2+1
      a = l[k]
      if c < a + b:
        k_min = k
      else:
        k_max = k-1
    ans += k_min - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort(reverse=True)
ans = 0
for i in range(n-2):
  c = l[i]
  for j in range(i+1, n-1):
    b = l[j]
    k_max = n-1
    k_min = j
    while k_min<k_max:
      k = (k_max+k_min)//2+1
      a = l[k]
      if c < a + b:
        k_min = k
      else:
        k_max = k-1
    ans += k_min - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 0
for i in range(n-2):
  a = l[i]
  for j in range(i+1, n-1):
    b = l[j]
    k_max = n-1
    k_min = j
    while k_min!=k_max:
      k = (k_max+k_min)//2+1
      c = l[k]
      if c < a + b:
        k_min = k
      else:
        k_max = k-1
    ans += k_min - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

N = int(input())
li = [int(zz) for zz in input().split()]

li.sort()

ans = 0
for i in range(N):
    for j in range(i+1,N):
        l = bisect_right(li,li[j]-li[i])
        r = bisect_left(li,li[i]+li[j])
        ans += r-l
        if l <= i < r:
            ans -= 1
        if l <= j < r:
            ans -= 1
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

N = int(input())
li = [int(zz) for zz in input().split()]

li.sort()

ans = 0
for i in range(N):
    for j in range(i+1,N):
        l = bisect_right(li,li[j]-li[i])
        r = bisect_left(li,li[i]+li[j])
        for k in range(l,r):
            if k != i and k != j:
                ans += 1
print(ans//3)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

combinations = list(itertools.combinations(range(n),3))

c = 0

for i in combinations:
    if l[i[0]] < l[i[1]] + l[i[2]] and l[i[1]] < l[i[0]] + l[i[2]] and l[i[2]] < l[i[1]] + l[i[0]]:
        c += 1
    
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

L_tri_pairs = []
buf = [L[0]]
for i in range(N - 1):
    if L[i+1] / L[i] < 2:
        buf.append(L[i+1])
    else:
        L_tri_pairs.append(buf)
        buf = [L[i+1]]

if len(buf) != 0:
    L_tri_pairs.append(buf)

count = 0

for l_pair in L_tri_pairs:
    ll = len(l_pair)
    
    if ll <= 2:
        continue

    for i in range(ll - 2):
        for j in range(i+1, ll - 1):
            for k in range(j+1, ll):
                a = l_pair[i]
                b = l_pair[j]
                c = l_pair[k]
                if a < b + c and b < c + a and c < a + b:
                    count += 1
                else:
                    break

print(count)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(N - 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
            else:
                break
    
print(count)

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):
            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:
s=int(input())

li = list(map(int,input().split()))
cnt = 0
for a in range(len(li)):
    for b in range(a + 1, len(li)):
        for c in range(b + 1, len(li)):
            if ((li[a] < li[b] + li[c]) and (li[b] < li[c] + li[a]) and (li[c] < li[a] + li[b])):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#ABC143-D:二分探索
n=int(input())
l=list(map(int,input().split()))
l=sorted(l,reverse=True)
ans=0
#print(l)
for i in range(n-2):
    for j in range(i+1,n-1):
        b=l[i]
        c=l[j]

        #二分探索
        ok=j
        ng=n
        while ng-ok>1:
            mid=(ok+ng)//2
            if l[mid]>b-c:
                #b>=cはわかってる
                ok=mid

            else:
                ng=mid

        ans+=ok-j
print(ans)

Submission status: Time Limit Exceeded

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

total = 0
from itertools import combinations as cm
for pair in cm(L, 3):
  a, b, c = pair
  if a < b+c and b < c+a and c < a+b:
    total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

# Input
s = [input() for i in range(2)]

N = int(s[0])
S = list(s[1].split())

# Proccess
def triangle_OK(a,b,c):
	if (int(a) < int(b) + int(c)):
		if (int(b) < int(c) + int(a)):
			if (int(c) < int(a) + int(b)):
				return 1
			else:
				return 0
		else:
			return 0
	else:
		return 0

TS = 0

for i in range(0, N):
	for j in range(0,N):
		for k in range(0,N):
			if i == j or j == k or k == i:
				pass
			else:
				TS = TS + triangle_OK(S[i],S[j],S[k])
# Output
print(int(TS/6))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
cnt = 0
l = list(map(int, input().split()))
for a, b, c in itertools.combinations(l, 3):
    if abs(b - c) < a < b + c:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

    ans = 0

    for i in range(N):
        for j in range(i+1, N):
            X = L[i] + L[j]
            l = bisect.bisect_left(L, X, lo=j+1)
            ans += (l-(j+1))
            #print(L[j + 1:l])
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

    ans = 0

    for i in range(N):
        for j in range(i+1, N):
            X = L[i] + L[j]
            l = bisect.bisect_left(L, X, lo=j+1)
            ans += len(L[j+1:l])
            #print(L[j + 1:l])
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            flag = True
            if l[i] >= l[j] + l[k]:
                flag = False
            if l[j] >= l[i] + l[k]:
                flag = False
            if l[k] >= l[j] + l[i]:
                flag = False
            if flag == True:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

L.sort()

def bsearch(array, value):
  left = 0
  right = len(array)
  while left + 1 < right:
    idx = int(left + (right - left) / 2)
    if array[idx] < value:
      left = idx
    else:
      right = idx
  return left

sum = 0
ai = 0
while ai < len(L) - 2:
  bi = ai + 1
  while bi < len(L) - 1:
    a = L[ai]
    b = L[bi]
    i = bsearch(L, a + b)
    sum += (i - bi)

    bi += 1
  ai += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
sticks.sort()
ans = 0
for a in range(N-2):
  A = sticks[a]
  for b in range(a+1, N-1):
    B = sticks[b]
    c = b+1
    while sticks[c] < A + B:
#      print(A, B, sticks[c])
      c += 1
      ans += 1
      if c == N:
        break
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
sticks.sort()
ans = 0
for a in range(N-2):
  A = sticks[a]
  for b in range(a+1, N-1):
    B = sticks[b]
    for c in range(b+1, N):
      C = sticks[c]
      if A + B > C:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
lenL = len(L)
combi = set()
v2i = {v:i for i, v in enumerate(L)}

for i in range(lenL-2):
    a = L[i]
    for j in range(i+1, lenL-1):
        b = L[j]
        for k in range(j+1, lenL):
            c = L[k]
            if c >= (a+b): break
            if a < (b+c) and b < (c+a) and c < (a+b): combi.add((a,b,c))

print(len(combi))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
lenL = len(L)
combi = set()
for i in range(lenL-2):
    a = L[i]
    for j in range(i+1, lenL-1):
        b = L[j]
        for k in range(j+1, lenL):
            c = L[k]
            if a < (b+c) and b < (c+a) and c < (a+b): combi.add((a,b,c))

print(len(combi))

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
L = sorted(list(map(int, input().split())))
D = list(itertools.combinations(L,3))
M = [1 for a,b,c in D if a + b > c ]
print (sum(M))

Submission status: Time Limit Exceeded

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

s = 0

for i in range(0, n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      
      t = length[i]
      
      if t < length[j]:
        t = length[j]
      if t < length[k]:
        t = length[k]
        
      if t*2 < length[i] + length[j] + length[k]:
        s = s+1      
    
print(s)

Submission status: Time Limit Exceeded

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

    L.sort(reverse=True)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            min = L[i] - L[j] + 1
            max = L[j] - 1
            if min > max:
                break
            tmp = len([k for k in L if (min<=k)&(k<=max)])
            ans += tmp

    print(ans)

    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    L = sorted(L)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            for k in range(j+1, N):
                if L[i]+L[j] > L[k]:
                    ans += 1
                else:
                    break
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

comb = itertools.combinations(l,3)

cnt = 0
for item in comb:
    a = item[0]
    b = item[1]
    c = item[2]
    if a<b+c and b<c+a and c<a+b:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def LI():
  return [int(s) for s in input().split()]

N = int(input())
L_s = LI()

count = 0

for a,b,c in itertools.combinations(L_s,3):
  if a < (b+c) and b < (a+c) and c < (a+b):
    count+=1

print(count)

Submission status: Time Limit Exceeded

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

ans = 0
i = 0
while i < N-2:
    j = i+1
    while j < N-1:
        a = L[i]
        b = L[j]
        if L[j+1] < a+b:
            cOK = j+1
            cNG = N
            while cOK+1<cNG:
                ci = (cOK +cNG)//2
                if L[ci]<a+b:
                    cOK = ci
                else:
                    cNG = ci
            ans += cOK-j
        j += 1
    i += 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):
      if a + b > L[k] and a + L[k] > b and b + L[k] > a:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)
 
def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]
 
# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]
 
# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]
 
# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None
 
def bin_lte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r+1)//2
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    # print(n, arr[c])
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None
 
n = int(input())
lll = get_nums_l()
lll.sort()
 
ans=0
for a in range(n-2):
    for b in range(a+1, n-1):
 
        _min = abs(lll[a]-lll[b]) + 1
        _max = lll[a]+lll[b] - 1
 
        left = bin_gte(lll[b+1:], _min)
        right = bin_lte(lll[b+1:], _max)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        # c = math.ceil((l+r)/2)
        c = (l+r+1)//2
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0

# maxs = [None] * 2002
# mi = 0
# prev = lll[0]
# for l in lll[1:]:
#     for j in range(prev, l):
#         maxs[j] = mi
#     mi += 1
#     prev = l
# for j in range(prev, 2002):
#     maxs[j] = mi

# mins = [None] * 2002
# mi = 0
# prev = 0
# for l in lll:
#     for j in range(prev+1, l+1):
#         mins[j] = mi
#     mi += 1
#     prev = l

# print(maxs)
# print(mins)

for ai, a in enumerate(lll):
    if ai == n-2:
        break
    for bi in range(ai+1, n-1):
        b = lll[bi]

        _min = abs(a-b) + 1
        _max = a+b - 1

        # left = mins[_min]
        # right = maxs[_max]
        left = bin_gte(lll, _min, bi+1)
        right = bin_lte(lll, _max, bi+1)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1
        # ans += right-left+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n, l):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = math.ceil((l+r)/2)
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    # print(n, arr[c])
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0

# mins = [-1] * 1001
# for i,l in enumerate(lll):
#     for j in range(l):
#         mins[j] = i

for ai, a in enumerate(lll):
    if ai == n-2:
        break
    for bi in range(ai+1, n-1):
        b = lll[bi]

        _min = abs(a-b) + 1
        _max = a+b - 1

        left = bin_gte(lll, _min, bi+1)
        right = bin_lte(lll, _max, bi+1)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n, l):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = math.ceil((l+r)/2)
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    # print(n, arr[c])
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0

# mins = [-1] * 1001
# for i,l in enumerate(lll):
#     for j in range(l):
#         mins[j] = i

for ai, a in enumerate(lll[:n-2]):
    for bi, b in enumerate(lll[ai+1:n-1]):
        bi += ai+1

        _min = abs(a-b) + 1
        _max = a+b - 1

        left = bin_gte(lll, _min, bi+1)
        right = bin_lte(lll, _max, bi+1)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_list = list(int(i) for i in input().split())
list.sort(l_list, reverse=True)
count = 0
for i in range(n):
    a = l_list[i]
    for j in range(i+1, n):
        b = l_list[j]
        for k in range(j+1, n):
            c = l_list[k]
            if a < b + c and b < c + a and c < a + b:
                count += 1
            else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = math.ceil((l+r)/2)
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    # print(n, arr[c])
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0
for ai, a in enumerate(lll[:n-2]):
    for bi, b in enumerate(lll[ai+1:n-1]):
        bi += ai+1

        _min = abs(a-b) + 1
        _max = a+b - 1

        left = bin_gte(lll[bi+1:], _min)
        right = bin_lte(lll[bi+1:], _max)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 昇順にソート済みのarrから、n以上の最小の要素を探索して返します。
# 存在しない場合は、Noneを返します。
def bin_gte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n):
    l = 0
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = math.ceil((l+r)/2)
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    # print(n, arr[c])
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0
for a in range(n-2):
    for b in range(a+1, n-1):

        _min = abs(lll[a]-lll[b]) + 1
        _max = lll[a]+lll[b] - 1

        left = bin_gte(lll[b+1:], _min)
        right = bin_lte(lll[b+1:], _max)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1

print(ans)

Submission status: Time Limit Exceeded

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

def binary(a,b,mid):
  if  L[mid]+L[j]>L[i]:
    return True
  else:
    return False

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

L = list(sorted(map(int, input().split()), key=lambda x: -x))

ret = 0


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


for i in range(N - 2):
    for j in range(i + 1, N - 1):
        p = j + 1
        q = N + 1
        while q - p > 2:
            k = (p + q) // 2
            if is_ok(L[i], L[j], L[k]):
                p = k
            else:
                q = k

        if is_ok(L[i], L[j], L[p]):
            ret += p - j

print(ret)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
l = [int(i) for i in input().split()]
l.sort()

count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        m = l[i] + l[j]
        # j+1~n-1まで二分探索
        low = j+1
        high = n-1
        index = j
        while low <= high:
            mid = (low + high) // 2
            if m > l[mid]:
                low = mid + 1
                index = mid
            else:
                high = mid - 1
        if index != j:
            count += index - (j+1) + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
res = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        tgt = L[i]+L[j] - 0.1
        res += bisect.bisect_right(L, tgt)-(j+1)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
res = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        tgt = L[i]+L[j] - 0.1
        res += bisect.bisect_right(L[j+1:], tgt)
print(res)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, stderr
from itertools import groupby, permutations, combinations
import numpy as np


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

    ret = 0
    # print(L, file=stderr)
    for ij in combinations(range(N - 1), 2):
        i, j = ij
        c = L[i] + L[j]
        k = np.searchsorted(L, c, side='right')
        if L[k - 1] == c:
            k = np.searchsorted(L, c, side='left')
        # print(L[i], L[j], k, ret, file=stderr)
        ret += k - j - 1

    # for i in range(N - 2):
    #     for j in range(i + 1, N - 1):
    #         k = np.searchsorted(L, L[i] + L[j], side='right')
    #         if L[k - 1] == L[i] + L[j]:
    #             k = np.searchsorted(L, L[i] + L[j], side='left')
    #         # print(L[i], L[j], k, ret, file=stderr)
    #         ret += k - j - 1
    return ret


if __name__ == "__main__":
    print(main())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, stderr
from itertools import groupby, permutations
import numpy as np


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

    ret = 0
    # print(L, file=stderr)
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = np.searchsorted(L, L[i] + L[j], side='right')
            if L[k - 1] == L[i] + L[j]:
                k = np.searchsorted(L, L[i] + L[j], side='left')
            # print(L[i], L[j], k, ret, file=stderr)
            ret += k - j - 1
    return ret


if __name__ == "__main__":
    print(main())