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

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 # Code 1 
    # ans+=bisect.bisect_left(L,L[i]+L[j],j)-j-1 # Code 2 
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for a in range(0, n-1):
    for b in range(a+1, n):
        right = bisect.bisect_left(l, l[a]+l[b])
        left = b + 1
        ans += right - left
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):
        n = bisect.bisect_left(L, L[i] + L[j])
        ans += max(0, n - j - 1)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

n = int(input())
lst = sorted([int(i) for i in input().split()])

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

count = 0
n = int(input())
list = sorted([int(x) for x in input().split()])

for i in range(n):
    for j in range(i + 1, n) :
        sum = list[i] + list[j]
        a = bisect_left(list,sum)
        count += a - j - 1
print(count)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
L = sorted(((map(int,input().split()))))
#print(L)
cnt = 0
for i in range(n):
  for j in range(i+1,n):
    num = L[i] + L[j]
    b = bisect.bisect_left(L, num)
    #print(L[i], L[j], b, cnt)
    if b-1 > j:
      cnt += b-1-j
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

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

counter = 0

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

print(counter)

Submission status: Accepted

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

L = sorted(L)[::-1] # descending order

def binary_search(descend_list, threshold):
    # 閾値を超えている要素の数を返す
    length = len(descend_list)
    
    # 最小の数が閾値を超えればリストの長さを返す
    if descend_list[-1] > threshold:
        return length
    
    # 最大の数が閾値を超えなければ0を返す
    if descend_list[0] <= threshold:
        return 0
    
    # 二分探索する 少なくとも一つはthresholdを超える要素があり、少なくとも一つはthresholdを超えない要素がある場合
    upper_bound = length
    lower_bound = 0
    index = (upper_bound - lower_bound) // 2
    
    while upper_bound > lower_bound + 1:
        if descend_list[index] > threshold:
            lower_bound = index
        else:
            upper_bound = index
        
        index = (upper_bound + lower_bound) // 2
    
    return lower_bound + 1

i = 0
ret = 0

while i < N - 2:
    edge1 = L[i]
    for j in range(i + 1, N-1):
        edge2 = L[j]
        _ret = binary_search(L[j+1:], edge1 - edge2)
        if _ret == 0:
            break
        else:
            ret += _ret
    
    i += 1

print(ret)

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
import bisect

# 入力
def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_

# 探索
def Search(N, L):
    ans = 0
    L.sort()
   # a, b は固定する
    for a_index in range(N):
       for b_index in range(a_index + 1, N):
           sum_of_a_and_b = L[a_index] + L[b_index]
           # c の可動範囲の右端を求める
           c_right_id = bisect.bisect_left(L, sum_of_a_and_b)
           # c の可動範囲は[b_index + 1, c_right)
           ans += max(0, c_right_id - b_index - 1)
    print(ans)

N, L = read_data()
Search(N, L)

Submission status: Accepted

Solution code:
import bisect

# 入力
def read_data():
   N_ = int(input())
   L_ = list(map(int, input().split()))
   return N_, L_

# 探索
def Search(N, L):
   ans = 0
   L.sort()
   # a, b は固定する
   for a_index in range(N):
       for b_index in range(a_index + 1, N):
           sum_of_a_and_b = L[a_index] + L[b_index]
           # c の可動範囲の右端を求める
           c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
           # c の可動範囲は[b_index + 1, c_right_index)
           ans += max(0, c_right_index - b_index - 1)
   print(ans)

N, L = read_data()
Search(N, L)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

# 入力
def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_

#探索
def binarySearch(N :int, L):
    ans = 0
    L.sort()
    # a,b は固定する
    for a_index in range(N):
        for b_index in range(a_index + 1, N):
            sum_of_a_and_b = L[a_index] + L[b_index]
            # c の可動範囲の右端を求める
            c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
            # c の可動範囲は[b_index + 1, c_right_index)
            ans += max(0, c_right_index - b_index - 1)
    print(ans)

N, L = read_data()
binarySearch(N, L)

Submission status: Accepted

Solution code:
import bisect

# 入力
def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_

#探索
def binarySearch(N :int, L):
    ans = 0
    L.sort()
    # a,b は固定する
    for a_index in range(N):
        for b_index in range(a_index + 1, N):
            sum_of_a_and_b = L[a_index] + L[b_index]
            # c の可動範囲の右端を求める
            c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
            # c の可動範囲は[b_index + 1, c_right_index) 右端が)となっているので-1している
            ans += max(0, c_right_index - b_index - 1)
    print(ans)

N, L = read_data()
binarySearch(N, L)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


import bisect

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

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())
s = list(map(int,input().split()))
l = sorted(s)
count = 0
for i in range(n-1):
    for j in range(i+1,n):
        limit = l[i] + l[j]
        count += bisect.bisect_left(l,limit) - (j+1)

print(count)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ans=0
ans1=0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+ans1,n):
      ans1=0
      if l[i]+l[j]>l[-1]:
        ans+=n-j-1
        break
      elif l[i]+l[j]<=l[k] :
        ans+=k-j-1
        ans1+=k-j-1
        break
      
      
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
 
N = int(input())
L = list(map(int,input().split()))
L.sort()
 
count = 0
# aとbを固定
for a in range(N-2):
    for b in range(a+1,N-1):
        aADDb = L[a] + L[b]
        maxL = bisect.bisect_left(L,aADDb)
        # cの範囲は、[b+1,maxL)
        count += max(0,maxL-(b+1))
 
print(count)

Submission status: Accepted

Solution code:
import sys
import bisect

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

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

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

Submission status: Accepted

Solution code:
import bisect

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

ans = 0

l.sort()

for i in range(n):
    for j in range(i+1, n):
        gap = l[i] + l[j]
        index = bisect.bisect_left(l, gap)

        ans += max(0, index-j-1)

print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

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

Submission status: Accepted

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

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

    # O(N^2 * logN)の全探索で解く
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()

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]
        k = bisect.bisect_left(L, a + b)
        ans += k - j - 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-2):
    for j in range(i+1, n-1):
        short_2hen = l[i] + l[j]
        bis = bisect.bisect_left(l, short_2hen)
        count += bis - j - 1
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-1):
    for j in range(i+1,n):
        index = bisect.bisect_left(L,L[i]+L[j])
        ans += index - (j+1)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

L.sort()

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

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    l = sorted(list(map(int, input().split())))
    memo = [-1] * (l[-2] + l[-1] + 1)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            a = l[i]
            b = l[j]
            if memo[a + b] == -1:
                right = bisect.bisect_left(l, a + b)
                memo[a + b] = right
            else:
                right = memo[a + b]
            ans += max(0, right - j - 1)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
# 2019-11-18 23:58:23(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, *l = [int(x) for x in sys.stdin.read().split()]
    l.sort()

    c = collections.defaultdict(int)
    for i in range(n):
        c[l[i]] += 1
    
    # cumsum: count of {l[j]|l[j] <= l[i]}
    s = [0 for _ in range(l[-3] + l[-2] + 1)] # a+bの最大値以下まで
    for i in range(l[-3] + l[-2]):
        s[i+1] = s[i] + c[i+1]

    count = 0
    # a <= b <= c
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            # cの候補の数: a+b > cを満たし、c = l[k]となるkがjより大きい棒の数
            count += s[a+b-1] - (j + 1) # b は j+1番目

    print(count)




    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 2019-11-18 23:58:23(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, *l = [int(x) for x in sys.stdin.read().split()]
    l.sort()
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            count += (bi_l(l, a + b) - 1) - j
    
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

L.sort()
a=0
for i in range(N-2):
    for j in range(i+1,N-1):
        a+=bisect_left(L,L[i]+L[j])-j-1
print(a)

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

Submission status: Accepted

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

L.sort()

import bisect
ans = 0

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

Submission status: Accepted

Solution code:
import itertools
import bisect

N = int(input())
Llist  =[int(i) for i in input().split()]
Llist.sort()

#print(Llist)


ans=0
for i in range(len(Llist)):
    for j in range(i+1,len(Llist)):
        a = Llist[i]
        b = Llist[j]
        index = bisect.bisect_right(Llist,b-a)
        #print("i"+str(i)+"  a"+str(a)+"  j"+str(j)+"  b"+str(b)+"  index"+str(index))
        if i>index:
            ans+=i-index

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

Submission status: Accepted

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

def solve(N, L):
    answer = 0
    for i in range(2, N):
        c = L[i]
        a = 0
        b = i-1
        while a < b:
            if c >= L[a] + L[b]:
                a += 1
            else: # c < a + b
                answer += b - a
                b -= 1
                #print(L[a], L[b], c, (a, b, i))
    return answer

def check(N, L):
    from itertools import combinations

    answer = 0
    for a, b, c in combinations(L, 3):
        if a < b + c and b < a + c and c < a + b:
            #print(a, b, c)
            answer += 1
    return answer

#print(check(N, L))
print(solve(N, L))

Submission status: Accepted

Solution code:
from bisect import bisect_left
from bisect import bisect_right

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

l.sort()

ll = []

for i in range(n):
    for j in range(i+1,n):
        ll.append(l[i]+l[j])

ll.sort()

ans = 0

for i in range(2,n):
    s = bisect_right(ll,l[i])
    ans += i*(i-1)//2 - s

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
list = list(map(int, input().split()))
list.sort()
import bisect
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        index = bisect.bisect_left(list, list[i]+list[j])
        ans += index - 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(0,n-1):
  for j in range(i+1,n):
    x=l[i]+l[j]
    y=bisect.bisect_left(l,x)-1
    ans+=max((y-j),0)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

Solution code:
import bisect

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

t=0
c=0
for i in range(N-2):
    for j in range(i+1,N-1):
        t=A[i]+A[j]
        num=bisect.bisect_left(A,t)-1
        c+=max((num-j),0)

print(c)

Submission status: Accepted

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

counter = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        counter += (bisect_left(ls, ls[i] + ls[j]) - 1) - j

print(counter)

Submission status: Accepted

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

L.sort()
result=0
for i in range(N-2):
    for j in range(i+1,N-1):
        result+=bisect_left(L,L[i]+L[j])-j-1
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))
    t = 0
    l = len(L) - 1
    for ai, a in enumerate(L):
        ci = ai + 1
        for bi, b in enumerate(L[ai + 1:], ai + 1):
            ci = bisect.bisect_left(L, a + b, ci) - 1
            t += ci - bi
    print(t)


main()

Submission status: Accepted

Solution code:
import bisect

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

ans = 0

for b in range(n):
    for a in range(b):
        ab = d[a] + d[b]
        r = bisect.bisect_left(d, ab)
        l = b+1
        ans += max(0, r-l)

print(ans)

Submission status: Accepted

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

cnt = 0

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

N = int(input())

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

L.sort()

con = 0

for i in range(0,N):
  for j in range(i + 1,N):
    a = L[i]
    b = L[j]
    #a + bよりでかい最小の数のインデックス
    p = bisect_left(L,a + b)
    #cとbの間の区間を取る
    con += (p - 1) - j


print(con)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = list(map(int,input().split()))
count = 0
L.sort() # 昇順
for i in range(N):
    for j in range(i+1,N):
        ind = bisect.bisect_left(L,L[i]+L[j])
        count += ind-j-1

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


n = inp()
L = inpl()

ans = 0
L.sort()
for i in range(len(L)):
    for l in range(i+1,len(L)):
        ans += bisect.bisect_left(L, L[i]+L[l])-l-1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
INF = 10**4
N = int(input())
nums = list(map(int, input().split()))

nums.sort()
nums.append(INF)
ans = 0
for i in range(N):
  for j in range(i+1, N):
    k = bisect_left(nums, nums[i]+nums[j])
    if k-1 > j:
      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(len(l)-2):
    for j in range(i+1, len(l)-1):
        a1 = l[i] + l[j]
        index = bisect.bisect_left(l, a1)
        if index:
            ans += index - j - 1
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

# shorter first
# c > b >a
count = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    index = bisect.bisect_left(L, L[i]+L[j])
    count += max(0, index-(j+1))
print(count)

Submission status: Accepted

Solution code:
import bisect

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

# shorter first
# c > b >a
count = 0
for i in range(0, N-1):
  for j in range(i+1, N):
    index = bisect.bisect_left(L, L[i]+L[j])
    count += max(0, index-(j+1))
print(count)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left

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

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

        # b < c < idx
        ans += max(0, idx-j-1)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect    
    
N = int(input())
b = list(int(i) for i in input().split())
b.sort()
sum_N = 0
for i,x in enumerate(b):
    s = i+1
    for y in range(i+1,N,1):
        index = bisect.bisect_left(b,x+b[y])
        sum_N += max(0,index - y -1)
print(sum_N)

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):
    for j in range(i):
        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 itertools import combinations
from bisect import bisect_left

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

l.sort()
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

def main():
    n = int(input())
    L = list(map(int,input().split()))
    L = sorted(L)
    ans = 0
    for i in range(2,n):
        for j in range(1,i):
            left = bisect.bisect_left(L,L[i]-L[j]+1)
            if j-left > 0:
                ans += j-left
    print(ans)


if __name__ == '__main__':
    main()

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:
import bisect

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):
        k = bisect.bisect_left(L[j+1:], L[i]+L[j])
        count += k

print(count)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
xs = list(map(int,input().split()))
xs.sort()
ans = 0

for i in range(n-2):
  for j in range(i+1,n-1):
    k = bisect.bisect_left(xs[j+1:], xs[i]+xs[j])
    #print(xs[j+1:], k, xs[i]+xs[j])
    ans += k
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
L = list(map(int, input().split()))
count = 0
L.sort()
for i in range(N):
    for j in range(i):
        m = L[i] - L[j]
        k = bisect_right(L, m)
        n = max(0, j - k)
        count += n
print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)
ans = 0
for c in range(n):
    a=0
    b=c-1
    while a<b:
        if l[a]+l[b]>l[c]:
            ans+=b-a
            b-=1
        else:
            a+=1
print(ans)

Submission status: Accepted

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

ans=0
for c in range(n-1,1,-1):
    a=0
    b=c-1
    while(a<b):
        #cが一番長い
        if(L[a]+L[b]>L[c]):
            ans+=b-a
            b-=1
        else:
            a+=1
            
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    t = 0
    for i in range(n):
        for j in range(i + 1, n - 1):
            p = bisect.bisect_left(l, l[i] + l[j])
            t += p - (j + 1)
    print(t)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left

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

count = 0

for i in range(len(L)-2):
    for j in range(i+1,len(L)-1):
        count += bisect_left(L,L[i]+L[j]) - j - 1
        
print(count)

Submission status: Accepted

Solution code:
import sys
import bisect


input = sys.stdin.readline

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

    bleft = bisect.bisect_left
    bright = bisect.bisect
    ans = 0

    for i, a in enumerate(L):
        if i > N-2:
            break
        for j, b in enumerate(L):
            if j > N-1:
                break
            if j <= i:
                continue
            c_right = bleft(L[j+1:], a+b)
            ans += c_right
    print(ans)

if __name__ == '__main__':
    main()