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

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

cnt = 0

for i in range(N):
  for j in range(i+1,N):
    cnt += bisect.bisect_left(L,L[i]+L[j]) - (j+1)
print(cnt)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	L = LI();L.sort();
	#a<=b<=c

	def BS(a,c,i,j):
		s,t = i,j
		while s+1 < t:
			mid = (s+t)//2
			if (a+L[mid]>c):
				t = mid
			else:
				s = mid
		return j-t


	ans = 0

	for i in range(N-1):
		for j in range(i+2,N):
			a = L[i]
			c = L[j]
			ans += BS(a,c,i,j)

	print(ans)

main()

Submission status: Accepted

Solution code:
import bisect
n = int(input())
li = list(map(int,input().split()))
ans = 0
li.sort()
for k in range(0,n-2):
  for i in range(k+1,n-1):
    ans += bisect.bisect_left(li,li[k]+li[i])-i-1
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

ans = 0

for i in range(N - 2):
    a = L[i]
    for ii in range(i + 1, N - 1):
        b = L[ii]
        iii = bisect.bisect_left(L, a + b)
        ans += iii - ii - 1
            
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(map(int,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]
        c = bisect.bisect_left(L,a+b)
        ans += c-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
length_list = sorted(list(map(int, input().split())))
result = 0
 
for i in range(n-2):
  for j in range(i+1, n-1):
    upper = length_list[i] + length_list[j] - 1
    search_target = length_list[j+1:]
    less_than_upper = bisect.bisect_right(search_target, upper)
    result += less_than_upper
    
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
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 = bisect.bisect_left(L, L[i]+L[j])
        ans += max(0, k-j-1)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
count = 0
for b in range(n-2):
  for c in range(b+1, n-1):
    count += max(0, bisect.bisect_left(l, l[b]+l[c])-c-1)

print(count)

Submission status: Accepted

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


def solve(N: int, L: "List[int]"):
    L.sort()
    cnt=0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_r = bisect.bisect_left(L, L[a_i]+L[b_i])
            cnt += c_r - b_i - 1
    return cnt


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]"
    print(solve(N, L))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

count = 0
length_list.sort()

for a_idx in range(n - 2):
    for b_idx in range(a_idx + 1, n - 1):
        a = length_list[a_idx]
        b = length_list[b_idx]

        left_count = bisect_left(length_list, a+b)
        count += max(0, left_count - b_idx - 1)


print(count)

Submission status: Accepted

Solution code:
# ソートしたやつの小さい方から2つを決めてもうひとつはにぶたん
# 3, 5としたときに残りの一つは5, 6, 7みたいになる
# このにぶたんなんか実装がめっっっちゃむずくない？サンプル1が通らんのだが
# やったけどTLEなるな

import bisect

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


def lower_bound(arr, x):
    l, r = 0, len(arr)
    while l < r:
        mid = (l + r) // 2
        if arr[mid] < x:
            l = mid + 1
        else:
            r = mid
    return l


ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        idx_l = lower_bound(arr, arr[i] + arr[j]) - 1
        # print(
        #     "arr[{}]+arr[{}]={}より小さくなるはじめてのインデックス {}".format(
        #         i, j, arr[i] + arr[j], idx_l
        #     )
        # )
        # print("idx_l = {}, arr[{}]={}".format(idx_l, idx_l, arr[idx_l]))
        # print("範囲 {}".format(arr[j + 1 : idx_l + 1]))
        ans += len(arr[j + 1 : idx_l + 1])
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
#print(L)
ans = 0
def find(x, arr):
  a = len(arr)//2
  cur = a
  while True:
    a //= 2
    if arr[cur] < x:
      if cur == len(arr)-1:
        return cur+1
      elif arr[cur+1] >=x:
        return cur+1
      else:
        cur += max(1,a)
    else:
      if cur == 0:
        return 0
      else:
        cur -= max(1, a)
for i in range(N-2):
  for j in range(i+1, N-1):
    ans += bisect.bisect_left(L, L[i] + L[j])-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n = int(input())
nums = sorted([int(i) for i in input().split()])

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        tmp = nums[i] + nums[j]
        idx = bisect_left(nums, tmp)
        ans += idx - j - 1
print(ans)

Submission status: Accepted

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

L_list.sort()
import bisect
res = 0
for i in range(N):
    a= L_list[i]
    for j in range(i+1,N):
        b= L_list[j]
        ind = bisect.bisect_left(L_list,a+b)
        res+=(ind-j-1)

print(res)

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int,input().split(" ")))
nums.sort()
total = 0
for i in range(n-1,-1,-1):
    l = 0
    r = i-1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Submission status: Accepted

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_left(l, a + b)
        ans += max(idx - e - 1, 0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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, L[i] + L[j]) - (j + 1)

print(ans)

Submission status: Accepted

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

for a in range(0, N-2):
  for b in range(a+1, N-1):
    t = L[a] + L[b]
    idx = bisect_left(L,t)
    ans += max(0, idx-b-1)

print(ans)

Submission status: Accepted

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, l[i]+l[j], j+1, n) - j - 1
print(ans)

Submission status: Accepted

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, l[i]+l[j]) - j - 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
d=sorted(list(map(int,input().split())))
cnt=0
for i in range(N-1,1,-1):
  for j in range(i-1,0,-1):
    k=bisect.bisect_right(d,d[i]-d[j])
    if j-k>0:
      cnt+=j-k
print(cnt)

Submission status: Accepted

Solution code:
import itertools
import bisect

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

l = sorted(l)

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

cnt = 0

for i in combinations:
    a_b = l[i[0]] + l[i[1]]
    max_ab = max(i[0], i[1])
    c = bisect.bisect_left(l,a_b)
    
    cnt += (c - (max_ab+1))


print(cnt)

Submission status: Accepted

Solution code:
# 要復習
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    L = LIST()
    L.sort()
    ans = 0
    # count = 0
    for i in range(0, N - 2):
        a = L[i]
        for j in range(i + 1, N - 1):
            b = L[j]
            x = a + b - 1
            # a + b > c なので c <= a + b - 1
            # x の入るべき場所にいれた際，そのxの最初のインデックスを返す
            # print(x)
            index = bisect(L, x)
            # print(index)
            # indexの方が大きければ調べる
            if j < index:
                # index が指してるのは，xを入れるべきindex
                # つまり，a + b - 1 の次に大きい数字の一番最初があるindex
                # を指しているので，それを除くための-1が必要
                ans += (index - j - 1)
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

def solve(a):
    s=set()
    for i in a:
        s.add(i)
    s=sorted(list(s))[::-1]
    L=1010
    k=[0]*(L)
    ak=[0]
    for i in a:
        k[i]+=1
    for i in range(L):
        ak.append(ak[-1]+k[i])
    ans,a1,a2,a3,a4=0,0,0,0,0
    un=len(s)
    for i in range(un):
        c=s[i]
        if k[c]>=3:
            a1+=k[c]*(k[c]-1)*(k[c]-2)//6
        if k[c]>=2:
            a2+=ak[c]*k[c]*(k[c]-1)//2
        for j in range(i+1,un):
            sc=s[j]
            a3+=(1 if 2*sc>c else 0)*k[c]*k[sc]*(k[sc]-1)//2
            mm=c-sc+1
            if mm<0:
                continue
            elif mm>sc:
                break
            else:
                a4+=k[c]*k[sc]*(ak[sc]-ak[mm])
            
    ans=a1+a2+a3+a4
    return ans,a1,a2,a3,a4

def naive(a):
    b=sorted(a)[::-1]
    n=len(b)
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            for k in range(j+1,n):
                if b[j]+b[k]>b[i]:
                    ans+=1
    return ans

print(solve(a)[0])

Submission status: Accepted

Solution code:
s=int(input())

li = list(map(int,input().split()))
li.sort()
cnt = 0
import bisect
for a in range(len(li)):
    for b in range(a + 1, len(li)):
       i = bisect.bisect_left(li, li[a]+li[b])
       cnt += i - b - 1

print(cnt)

Submission status: Accepted

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

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

ans = 0
for i in range(N-1,1,-1):
  for j in range(i-1,0,-1):
    t = bisect.bisect(L, L[i]-L[j])
    if j-t>0: ans+=j-t
print (ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
cnt = 0
l = sorted(list(map(int, input().split())))
for a in range(n - 2):
    for b in range(a + 1, n - 1):
        c_min = b + 1
        c_max = bisect.bisect_left(l, l[a] + l[b]) - 1
        cnt += c_max - c_min + 1
print(cnt)

Submission status: Accepted

Solution code:
import itertools
import bisect
N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1,N-1):
        b = L[j]
        ind_cmin = j+1
        ind_cmax = bisect.bisect_left(L,a+b)-1
        ans += ind_cmax - ind_cmin + 1
print(ans)

Submission status: Accepted

Solution code:
import itertools
import bisect
            

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

# Lを小さい順にソート
L = sorted(L)

# a <= b <= cを仮定してもおｋ
# a,bを全探索し，条件を満たすcの数をO(N)未満の方法で求めればよい
ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1,N-1):
        b = L[j]

        # Lの中で条件b-a<cを満たす最小のc
        ind_cmin = j+1

        # Lの中で条件L[k]<a+bを満たす最大のk = Lの中で昇順ソートを崩さずa+bを挿入できる最小のインデックス-1
        # 2分法で探す
        ind_cmax = bisect.bisect_left(L,a+b) - 1

        ans += ind_cmax - ind_cmin + 1

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

# N = 2000
# L = list(range(N))

# # 重複を消す
# L = list(set(L))
# N = len(L)

# Lを小さい順にソート
L = sorted(L)

# j=0〜max(L)について，L[i]<jを満たす最大のi: X[j] = i
X = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N-1,-1,-1):
        if L[i] < j:
            X.append(i)
            flag = True
            break
    # 見つからなければ
    if not flag: X.append(None)

# j=0〜max(L)について，L[i]>jを満たす最小のi: Y[j] = i
Y = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N):
        if L[i] > j:
            Y.append(i)
            flag = True
            break
    # 見つからなければ
    if not flag: Y.append(None)

#print(L)

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

        # print(b-a,a+b,Y[b-a],X[a+b])

        # cのindexの範囲 = Y[b-a]〜X[a+b]
        cmin = Y[b-a]
        if a+b <= L[-1]:
            cmax = X[a+b]
        else:
            cmax = N - 1

        # cminはjより大きい
        if cmin is not None and cmin <= j:
            cmin = j + 1

        #print(b-a,a+b,cmin,cmax)

        # if a == 2 and b == 3:
        #     print(b-a,a+b,cmin,cmax)
        #     exit()
        
            
        # どちらかがNoneなら0
        if cmin is not None and cmax is not None and cmin <= cmax:
            ans += cmax - cmin + 1

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        up_bound = bisect.bisect_left(L, a+b)
        ct += max(0, up_bound - (j+1))

print(ct)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import defaultdict
D = [0]*2001

#N =7
#A =[218, 786, 704, 233, 645, 728, 389]

A = sorted(A)
#print(A)
ans = 0

for i in range(N):
    tmp = 0
    for j in range(A[i]+1,2001):
        tmp += D[j]
        ans += D[j]
    #print(tmp)
    for j in range(i):
        D[A[i]+A[j]] += 1
print(ans)

Submission status: Accepted

Solution code:

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

from bisect import bisect_left

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

    if b_index <= a:
      b_index = a + 1
    if c - b_index > 0:
      ans += c - b_index

print(ans)

Submission status: Accepted

Solution code:
import bisect
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:
        kNG = bisect.bisect_left(L,L[i]+L[j])
        if kNG > j+1:
            ans += kNG-1-j
        j += 1
    i += 1

print(ans)

Submission status: Accepted

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

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
        
        if right < bi:
            continue
        left = max(bi+1, left)

        # ans += right[0]-left[0]+1
        ans += right-left+1

print(ans)

Submission status: Accepted

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

Solution code:
from bisect import bisect_right
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):
    c=L[i]+L[j]-1
    ans+=bisect_right(L,c)-j-1
    
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

res = 0

for i in range(N):
    for j in range(i + 1, N):
        k = bisect_left(L, L[i] + L[j])

        res += k - j - 1

print(res)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n=int(input())
l=sorted(list(map(int,input().split())))
ans=0
for b in range(n):
    for a in range(b):
        ab=l[a]+l[b]
        x=bisect_left(l,ab)
        y=b+1
        ans+=x-y
print(ans)
#ABC143D

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

li.sort()

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = li[i]
        b = li[j]
        idx = bisect_left(li, a+b)
        ans += idx - (j+1)
print(ans)

Submission status: Accepted

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

import bisect

n = int(input())
l = [int(i) for i in input().split()]
l.sort() # 二分探索するために整列

# l[i]+l[j]>l[x]を満たすxは
# 昇順に整列した配列を使うと
# 固定した2本の次のインデックスから必ず連続している
# そのため条件を満たす最大のxを二分探索で求めれば良い

count = 0
# 2本を固定して
for i in range(n-2):
    for j in range(i+1, n-1):
        # 3本目を探索
        index = bisect.bisect_left(l, l[i]+l[j])
        count += index - (j+1)

print(count)

Submission status: Accepted

Solution code:
from sys import stdin, stderr
from bisect import bisect_right, bisect_left


def main():
    N = int(input())
    L = sorted([int(x) for x in input().split()])
    ret = 0

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            c = L[i] + L[j] - 1
            k = bisect_right(L, c)
            # print(L[i], L[j], k, ret, file=stderr)
            ret += k - j - 1
    return ret


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

Submission status: Accepted

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]
        res += bisect.bisect_left(L, tgt)-(j+1)
print(res)

Submission status: Accepted

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


def main():
    N = int(input())
    L = sorted([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] - 1
    #     k = bisect_right(L, c)
    # #     k = np.searchsorted(L, c, side='right')
    #     ret += k - j - 1

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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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):
        lp = j+1
        rp = n-1
        while lp <= rp:
            mp = (lp+rp)//2
            if l[mp] < l[i] + l[j]:
                lp = mp+1
            else:
                rp = mp-1
        ans += lp-j-1
print(ans)

Submission status: Accepted

Solution code:
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):
        lp = j+1
        rp = n-1
        while lp < rp:
            mp = (lp+rp)//2
            if l[mp] < l[i] + l[j]:
                lp = mp+1
            else:
                rp = mp-1
        if l[lp] < l[i] + l[j]:
            ans += lp-j
        else:
            ans += lp-j-1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import bisect
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):
        n=L[i]+L[j]
        ans+=bisect.bisect_left(L,n)-j-1
        #print(L[i],L[j],n)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
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, L[i]+L[j]) - 1 - j
print(ans)

Submission status: Accepted

Solution code:
import bisect

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = 0
    for ai in range(N-2):
        a = L[ai]
        for bi in range(ai+1,N-1):
            b = L[bi]
            cmax = a + b
            m_index = bisect.bisect_left(L,cmax)
            ans += max(m_index - bi - 1, 0)

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

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for bi in range(1,N-1):
  for ai in range(0,bi):
    ci = bisect.bisect(L,L[bi]+L[ai]-1) - 1
    if bi<ci:
      ans += ci-bi
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
import itertools

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

ans = 0
for ij in itertools.combinations(range(N), 2):
    i, j = ij
    a, b = L[i], L[j]

    cl = abs(a - b)
    cr = a + b
    l = bisect.bisect_right(L, cl)
    r = bisect.bisect_left(L, cr)
    l = max(l, j+1)
    if r - l > 0:
        ans += r - l
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left


def main():
    N = int(input())
    L = list(map(int, input().split(" ")))
    L.sort()
    result = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = bisect_left(L, L[i] + L[j])
            result += k - j - 1
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import bisect
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
l = na()
l.sort()


c = 0
for i_1 in range(n-2):
    for i_2 in range(i_1+1, n-1):
        m = l[i_1] + l[i_2]
        i_3max = bisect.bisect_left(l, m) - 1
        c += i_3max - i_2

print(c)

Submission status: Accepted

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

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

    def canuse(x):
        index = bisect.bisect_left(A,x)
        return index
        
    ans = 0
    for i in range(N):
        for j in range(N-i-1):
            s = A[i]+A[i+j+1]
            ans += canuse(s)-(i+j+2)
            
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
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):
        idx = bisect.bisect_left(l, l[i]+l[j])
        ans += max(0, idx - 1 - j)        
#print(l)
#while i+2 < n:
#    if l[i] < l[i+1] + l[i+2]:
#        ans+=1
#        i+=3
#    else: i+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
result = 0
for x in range(N):
    z = x + 2
    for y in range(x+1, N):
        while z < N and L[x] + L[y] > L[z]:
            z += 1
        result += z - y - 1
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
S = list(map(int, input().split()))
a = []
S.sort()
ans = 0
for i in range(N):
  for j in range(i+1,N):
    t = S[i]+S[j]
    idx = bisect_left(S, t)
    ans += max(0, idx-j-1)
print(ans)

Submission status: Accepted

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

def binary_search(arr, low, high, x):
    while low < high:
        mid = (low + high) // 2
        if arr[mid] < x: low = mid+1
        else: high = mid

    return low


def rec_binary_search(arr, l, r, x):
    if r-l <= 0:
        return l
    
    else:
        mid = (l + r) // 2
        if arr[mid] == x:
            return mid
    
        elif arr[mid] > x:
            return binary_search(arr, l, mid-1, x)

        else:
            return binary_search(arr, mid+1, r, x)

ll.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        c = ll[i] + ll[j]
        limit = bisect_left(ll, c)
        ans += limit - j - 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
#print(l)
from bisect import bisect_left as bl
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        ans+=bl(l,l[j]+l[i])-j-1
print(ans)

Submission status: Accepted

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, L[i]+L[j]) - 1 - j
print(ans)