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=list(map(int,input().split()))
l.sort()
cnt=0

# print(l)

# print()

for i in range(n-1,1,-1):
    for j in range(i-1, 0, -1):
        kagen=l[i]-l[j]
        if l[j]>kagen:
            idx_jogen=j
            idx_kagen=bisect.bisect_right(l,kagen)
            cnt+=idx_jogen-idx_kagen
            # print(l[i], l[j], kagen, idx_jogen,idx_kagen,cnt)

print(cnt)

Submission status: Accepted

Solution code:
"""
keyword: 二分探索

初めにLをソートして、a<b<cとしてa,bを決めたとき、a+b>cとなるようなcの個数を計算する
cの最大値を求めるには二分探索が高速
さらに、二分探索の探索範囲の左端は、前のループで求めたcの最大値で十分である（∵前のループのa+b <= 今ループのa+b）ことを考慮すれば、さらに高速化できる

pythonでTLE, pypyでAC
"""

import sys
sys.setrecursionlimit(10**6)

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

L.sort()
ans = 0

for i in range(n-2):
    l_prev = 0 # 前のループで求めたcの最大値を覚えておく
    for j in range(i+1,n-1):
        ab = L[i]+L[j]
        if not l_prev:
            l = j; r = n
        else:
            l = l_prev; r = n
        while l+1 < r:
            c = (l+r)//2
            if L[c] < ab:
                l = c
            else:
                r = c
        ans += l-j
        l_prev = l
        
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
tris = list(map(int,input().split()))
tris.sort()
cnt = 0
for i in range(n-2):
    a = tris[i]
    for j in range(i+1,n-1):
        b = tris[j]
        r = bisect.bisect_left(tris, a+b)
        cnt += r - j - 1
print(cnt)

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-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:
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:
from bisect import bisect_right
n = int(input())
l = list(map(int, input().split()))

l = sorted(l)
ans = 0
for i in range(n - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        x = l[i] - l[j]
        index = bisect_right(l, x, 0, n)
        ans += max(0, j - index)

print(ans)

Submission status: Accepted

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

l = sorted(l)
ans = 0
for i in range(n - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        x = l[i] - l[j]
        index = bisect_right(l, x, 0, n)
        #print(l[i], l[j], i, j, index, max(0, i - index - 1))
        ans += max(0, j - index)

print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
res = 0
for i in range(N): #aを決定
    for j in range(i+1, N): #bを決定
        c = L[i] + L[j] #cの最大値を決定
        pos = bisect.bisect_left(L, c)
        res += max(0, pos-j-1)
print(res)

Submission status: Accepted

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

#棒の長さa,bを固定して、あとはc<a+bとなるcを2部探索で探せばよい
L.sort()
ans = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1,n):
        b = L[j]
        tmp = bisect.bisect_left(L,a+b)
        if j<tmp:
            ans+=tmp-j-1
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

maxL = 10**3

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

nums = [0] * (2*maxL+1)
for L in Ls:
    nums[L] += 1

accNums = list(accumulate(nums))

ans = 0
for A in range(1, maxL+1):
    if nums[A] == 0: continue
    for B in range(A+1, maxL+1):
        if nums[B] == 0: continue
        ans += nums[A] * nums[B] * (accNums[A+B-1]-accNums[B])

for A in range(1, maxL+1):
    if nums[A] < 2: continue
    ans += nums[A] * (nums[A]-1) // 2 * (accNums[2*A-1]-nums[A])

for A in range(1, maxL+1):
    if nums[A] < 3: continue
    ans += nums[A] * (nums[A]-1) * (nums[A]-2) // 6

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))

    Ls.sort()

    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            k = bisect_left(Ls, Ls[i]+Ls[j]) - 1
            ans += k-j

    print(ans)


solve()

Submission status: Accepted

Solution code:
from bisect import *
n,*l=map(int,open(0).read().split())
l.sort()
ans=0
for i in range(1,n):
  for j in range(i+1,n):
    ans+=max(i-bisect_right(l,l[j]-l[i]),0)
print(ans)

Submission status: Accepted

Solution code:
# a < b + c
# b < a + C
# c < a + b

# 4 3 2 1
# a b c 

# abs(a - b) < c < a + b
# a < b < c
# b - a < b < c
# c < a + b 

from math import ceil, floor
from bisect import bisect_left, bisect_right

def solution(N, data): # O(N^2 * logN)
    data.sort()
    c = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            c += bisect_left(data, data[i] + data[j], j) - j - 1
    return c

if __name__ == '__main__':
    N = int(input())
    L_i = list(map(int, input().split()))
    a = solution(N, L_i)
    print(a)

Submission status: Accepted

Solution code:
# a < b + c
# b < a + C
# c < a + b

# 4 3 2 1
# a b c 

# abs(a - b) < c < a + b
# a < b < c
# b - a < b < c
# c < a + b 

from math import ceil, floor
from bisect import bisect_left, bisect_right

def solution(N, data): # O(N^2 * logN)
    data.sort()
    c = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            c += bisect_left(data, data[i] + data[j], j) - j - 1

    return c

if __name__ == '__main__':
    N = int(input())
    L_i = list(map(int, input().split()))
    a = solution(N, L_i)
    print(a)

Submission status: Accepted

Solution code:
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())
L = list(map(int, input().split()))
L = sorted(L)
ans = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        ab = L[a] + L[b]
        r = bisect.bisect_left(L, ab)
        l = b + 1
        ans += r - l
print(ans)

Submission status: Accepted

Solution code:
# ABC143 D Triangles

import bisect

n = int(input())
l_list = [int(x) for x in input().split()]

l_list.sort()
ans = 0

for i in range(n-2):
    _a = l_list[i]
    for j in range(i+1,n-1):
        _b = l_list[j]
        _tmp = bisect.bisect_left(l_list, _a+_b)
        if j < _tmp:
            ans += _tmp - 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()
cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
# ３重ループではTLE
# a < b < c & c < a + b

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):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L,t)
        ans += max(0, idx-j-1) # c < a + b の範囲を総取り
print(ans)

Submission status: Accepted

Solution code:
def fnt(arr,n): 
  
     
     
     
    count = 0
  
     
    for i in range(0,n-2): 
  
         
        k = i + 2
  
        # Fix the second element 
        for j in range(i+1,n): 
  
             
            while (k < n and arr[i] + arr[j] > arr[k]): 
                k += 1
  
             
            if(k>j): 
                count += k - j - 1
  
    return count
 
 
n=int(input())
arr=input()
arr=arr.split()
arr=list(map(int,arr))
arr.sort()
print(fnt(arr,n))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import bisect
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(len(L)):
    for j in range(i+1, len(L)):
        b_i = bisect.bisect_left(L, L[i]+L[j]) # a+bより小さい数
        ans += b_i - (j+1)
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):
    for j in range(i+1, N):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-j-1)
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):
    for j in range(i+1, N):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-j-1)
print(ans)

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
L = rl()

L.sort()

def search(d):
	l, r = 0, len(L)
	while r-l > 1:
		c = (r+l)//2
		if d <= L[c]:
			r = c
		else:
			l = c
	return r

ans = 0
for i in range(N):
	for j in range(i+1, N):
		d = L[i]+L[j]
		k = search(d)
		c = max(k-j-1,0)
		ans += c
print(ans)

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
L = rl()

L.sort()

import bisect

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

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
l = li()

l.sort()

ans = 0
from bisect import bisect_left
for i in range(len(l)-2):
    for j in range(i+1,len(l)-1):
        ab = l[i] + l[j]
        r = bisect_left(l, ab)
        ll = j+1
        ans += max(0, r-ll)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
l = li()
l.sort()

ans = 0

lset = list(set(l))
lset.sort()
l_dup = []
for i in range(len(lset)):
    l_dup.append(l.count(lset[i]))

# 3辺が異なる
for i in range(len(lset)-1):
    for j in range(i+1,len(lset)):
        if lset[i]*2 < lset[j]:
            minside = lset[j] - lset[i] +1 
        else:
            minside = lset[i] + 1

        msi = bisect_left(lset,minside)
        ans += l_dup[i] * l_dup[j] * sum(l_dup[msi:j])


        # print('---')
        # print(lset[i], lset[j],minside)
        # print(lset[msi+1:j])
        
# 二等辺
# 正△
for i in range(len(lset)):
    if l_dup[i] >= 2:
        msi = bisect_left(lset,lset[i]*2)
        ans += l_dup[i] * (l_dup[i]-1)/2 * (sum(l_dup[:msi]) - l_dup[i])

    if l_dup[i] >= 3:
        ans += l_dup[i] * (l_dup[i]-1) * (l_dup[i]-2)/6

print(int(ans))

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right

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

L.sort()

cnt=0

for a in range(N):
    for b in range(a+1,N):
        right=bisect_right(L,L[a]+L[b]-1)
        left=max(b+1,bisect_left(L,L[b]-L[a]+1))

        cnt+=max(0,right-left)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count//3)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations 
from bisect import bisect_left
def main():
    N = int(readline())
    L = [int(i) for i in readline().split()]
    L.sort()
    sig = 0
    for i in range(N):
        for j in range(i+1, N):
            l = bisect_left(L, L[i]+L[j], j+1)
            sig += l-j-1
    print(sig)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect, bisect_left

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    L = [int(i) for i in readline().split()]
    L.sort()

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

counter = 0
for a_pointer in range(len(L)):
    for b_pointer in range(a_pointer+1, len(L)):
        a = L[a_pointer]
        b = L[b_pointer]
        c_left_pointer = max(bisect.bisect_left(L, b - a + 1), b_pointer + 1)
        c_right_pointer = bisect.bisect_right(L, b + a - 1)
        #print(a, b, c_left_pointer, c_right_pointer)
        counter += max(c_right_pointer - c_left_pointer, 0)
        #if c_right_pointer == len(L) and c_right_pointer == len(L):
            #break
print(counter)

Submission status: Accepted

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

import bisect
for i in range(len(l)):
    for j in range(i+1,len(l)):
        end = bisect.bisect_left(l,l[i]+l[j])
        start = j
        count += end - start -1
print(count)

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):
    #print("i: "+str(i)+"  j: "+str(j))
    #print(max(l[i], l[j])-min(l[i], l[j]))
    #print(l[i]+l[j])
    #print(list(filter(lambda x: max(l[i], l[j])-min(l[i], l[j])<x<l[i]+l[j], l[j+1:])))
    #temp1 = bisect_left(l[j+1:], l[j]-l[i])
    #temp2 = bisect_left(l[j+1:], l[j]+l[i])
    #print(temp1)
    #print(temp2)
    ans += bisect_left(l[j+1:], l[j]+l[i])
    
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = 0
    for i in range(N):
        a = L[i]
        for j in range(i + 1, N):
            b = L[j]
            ans += max(bisect_left(L, a + b) - (j + 1), 0)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect


def resolve():
    n = int(input())
    li = list(map(int, input().split()))
    ans = 0

    li.sort()

    for i in range(n):
        for j in range(i + 1, n):
            cnt = bisect.bisect_left(li, li[i] + li[j])

            ans += cnt - j - 1

    print(ans)

resolve()

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    if list[mid] > item:
        return mid - 1
    else:
        return mid
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        b = binary_search(l,l[i] + l[j+1]-1)
        while True:
            if b == len(l)-1:
                break
            elif l[b] == l[b+1]:
                b = b+1
            else:
                break
        ans += max(0,b-(j+1))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n = int(input())
sides = list(map(int, input().split()))
sides.sort()
ans = 0
for a in range(len(sides)):
    for b in range(a+1,len(sides)):
        bound = bisect_left(sides, sides[a]+sides[b], b+1)
        if bound >= n:
            ans += bound - b - 1
        elif sides[bound] < sides[b]:
            ans += bound - b
        elif bound > b+1:
            ans += bound - b - 1

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
L=list(map(int,input().split()))
L.sort()
ans=0
for i in range(N-2):
 mid =i+1
 l=0
 r=i+2
 while r<N:
  if L[r]-L[l]<L[mid]:
   ans+=(mid-l)
   r+=1
  else:
   if l==mid-1:
    r+=1
   else:
    l+=1
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

print(count)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
from numba import njit, jit
from itertools import combinations


# @jit
# def solve():
#     ans = 0
#     for a, b, c in combinations(L, r=3):
#         ans += a + b > c
#     return ans

@njit
def solve(L):
    ans = 0
    for i in range(N - 2):
        a = L[i]
        for j in range(i + 1, N - 1):
            b = L[j]
            for k in range(j + 1, N):
                ans += a + b > L[k]
    return ans


print(solve(L))

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit

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

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

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit

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

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

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()
cnt = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    cnt += bisect_left(L, L[i] + L[j]) - j - 1
print(cnt)

Submission status: Accepted

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

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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


def solve():
    ans = 0
    for i in range(N):
        b = L[i]
        for j in range(i):
            a = L[j]
            k = bisect.bisect_left(L, a + b, i + 1) - 1
            if k == i:
                continue
            ans += k - i
    return ans


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

Submission status: Accepted

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

ans = 0
for i in range(1, n - 1):
    for j in range(i + 1, n):
        c_min_len = l[j] - l[i]
        x = i - bisect_right(l, c_min_len)
        ans += max(0, x)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = [int(x) for x in input().split()]
l.sort()

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(0, N - 2):
    a = L[i]
    for j in range(i + 1, N - 1):
        b = L[j]
        ok = j + 1
        ng = N - 1
        if L[ng] < a + b:
            ans += ng - j
            continue
        if L[ok] >= a + b:
            continue
        while ok + 1 < ng:
            c = (ok + ng) // 2
            if L[c] < a + b:
                ok = c
            else:
                ng = c
        ans += ok - j
print(ans)

Submission status: Accepted

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

L = sorted(L)
count = 0
for i in range(1,N-1):
    for j in range(i+1,N):
              posi = bisect.bisect_right(L,L[j]-L[i])
              if i-posi > 0:
                  count += i-posi
print(count)

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

Submission status: Accepted

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

Submission status: Accepted

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

ln.sort()

def binary_search(x, r):
    left = r + 1
    right = n - 1
    while left <= right:
        mid = (left + right) // 2
        if ln[mid] >= x:
            right = mid - 1
        else:
            left = mid + 1
    if ln[mid] >= x:
        return mid - 1
    else:
        return mid


ans = 0
for a in range(n-2):
    for b  in range(a+1, n-1):
        cmax = ln[b] + ln[a]
        ans += max(0, binary_search(cmax, b) - b)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# D - Triangles

from bisect import bisect_left

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

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

print(cnt)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c]:
        if s[a]!=s[b] and s[b]!=s[c]:
          count += aa[s[a]]*aa[s[b]]*aa[s[c]]
          continue
        if s[a] == s[c]:
          count += aa[s[a]]*(aa[s[a]]-1)*(aa[s[a]]-2)//6
          continue
        if s[a] == s[b]:
          count += aa[s[a]]*(aa[s[a]]-1)*aa[s[c]]//2
          continue
        if s[b] == s[c]:
          count += aa[s[a]]*aa[s[b]]*(aa[s[b]]-1)//2
          continue
        
print(count)

Submission status: Accepted

Solution code:
import bisect

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

l.sort()

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a, b = l[i], l[j]
        k = bisect.bisect_right(l, a+b-1)
        ans += k - j - 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-1):
    for j in range(i+1, N):
        a,b = L[i], L[j]
        idx = bisect.bisect_left(L, a+b)
        ans += max(0, idx - j - 1)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

L.sort()

ans = 0
for i in range(N-1, -1, -1):
    for j in range(i-1, -1, -1):
        m = L[i] - L[j]
        k = bisect_right(L, m)
        n = max(0, j-k)
        ans += n

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N-1,-1,-1):
    for j in range(i-1,-1,-1):#jはi未満
        m = L[i]-L[j]#上記c-bを計算
        k = bisect_right(L, m)#二分探索
        #aに相当するkは、上記の通り、mよりも大きい(つまり、右側)
        #同じものがあったら右側
        n = max(0, j-k)#j-kの区間の数は大丈夫
        ans+=n
print(ans)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    d = na()

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
n = int(input())
lis = sorted(list(map(int, input().split())))
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        ans += max(bisect.bisect_left(lis, lis[j]+lis[i]) - j - 1, 0)
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):
    for j in range(i+1, N):
        ans += bisect.bisect_left(L, L[i]+L[j]) -j-1
        #print(L[i]+L[j],L,bisect.bisect_left(L, L[i]+L[j]))
print(ans)

Submission status: Accepted

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

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

        idx = bisect_left(L, a+b)
        ans += max(idx - (j+1), 0)

print(ans)