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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    N=ii()
    L=sorted(lmis(),reverse=True)
    ans=0
    for i in range(N-2):
        a=L[i]
        j=i+1
        k=N-1
        while j<k:
            if a<L[j]+L[k]:
                j+=1
                ans+=k-j+1
            else:
                k-=1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

import itertools as it

def main():
    N=ii()
    L=sorted(lmis(),reverse=True)

    ans=0
    for i in range(N-2):
        a=L[i]
        j=i+1
        k=N-1
        while j<k:
            if a<L[j]+L[k]:
                j+=1
                ans+=k-j+1
            else:
                k-=1
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
Ln=list(map(int,input().split()))
Ln.sort(reverse=True)
Ans=0
left=0
right=0
 
for i in range(N-2):
    di=i+1
    for j in range(N-di-1):
        dj=di+j+1
        A=Ln[di-1]
        B=Ln[dj-1]
        if (B+Ln[dj])<A : break
 
        right=N-1
        left=dj
        while left<=right :
            mid=int((right+left)/2)
            if (B+Ln[mid])>A :
                left=mid+1
            else :
                right=mid-1
        Ans+=(left-1)-(dj-1)
 
print(Ans)

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):
    shortest = l[i]
    for j in range(i+1, n-1):
        second = l[j]
        # このスライスしたやつと地味にTLEになる...
        # k = bisect_left(l[j], shortest + second)
        # ans += k

        k = bisect_left(l, shortest + second)
        ans += k - j - 1

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10**9
MOD = 10**9+7

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

Submission status: Accepted

Solution code:
import bisect

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

count = 0
for i in range(N - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        # L[j] は L[i]以下の集合を降順で参照する
        k = bisect.bisect_left(L, L[i] + L[j])
        # L[i]+L[j] > k > L[j]
        count += k - (i + 1)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

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

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l,l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)

Submission status: Accepted

Solution code:
import bisect

def main():
    n, *l = map(int, open(0).read().split())
    l = sorted(l, reverse=True)

    ans = 0
    for a_i in range(n-2):
        a = l[a_i]
        b_i = a_i + 1
        c_i = n - 1
        while b_i < c_i:
            b = l[b_i]
            c = l[c_i]
            if a < b + c:
                b_i += 1
                ans += c_i - b_i + 1
            else:
                c_i -= 1
    print(ans)
    return()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

n = next_int()
a = next_ints()
a.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        pos = bisect.bisect_right(a, a[i] + a[j] - 1, j+1, n)
        ans += pos - j - 1
print(ans)

Submission status: Accepted

Solution code:
import bisect

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

n = next_int()
a = next_ints()
a.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        pos = bisect.bisect_right(a, a[i] + a[j] - 1)
        ans += pos - 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-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:
from bisect import bisect_left

def Triangles(N, L):
    asc = sorted(L)
    cnt = 0
    for i in range(N-2):
        l1 = asc[i]
        for j in range(i+1, N-1):
            l2 = asc[j]
            pos = bisect_left(asc, l1+l2)
            cnt += pos - 1 - j
    return cnt

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

Submission status: Accepted

Solution code:
from bisect import bisect_right

n = int(input())
l = list(map(int,input().split()))
l.sort() # 昇順にソート

count = 0
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1, n-1):
        ab = a+l[j]
        # bisect_left(a, x): x を a に挿入できる点を返す
        # cの最大値は a+b-1
        k = bisect_right(l, ab-1, j+1, n)
        count += k-(j+1)
print(count)

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f(list, n):
  length=len(list)
  if list[-1]<n:
    return length
  elif list[0]>=n:
    return 0
  else:
    a=length//2
    b=length//4
    while b>=1:
      if list[a]<n:
        a+=b
      else:
        a-=b
      b=b//2
      
    if list[a]<n:
      while list[a+1]<n:
        a+=1
    else:
      while list[a]>=n:
        a-=1
        
    return a+1  

n=int(input())
l=list(map(int, input().split()))
l.sort()
counter=0
for i in range(n-2):
  for j in range(i+1, n-1):
    counter+=f(l[(j+1):], l[i]+l[j]) #f(list, n) counts num in the sorted list under n
    
print(counter)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


import sys
#sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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

ans = 0
for i in range(n):
    r = i+1
    for j in range(i+1,n):
        A = a[i] + a[j]
        while r < n and A > a[r]:
            r += 1
        ans += r-j-1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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):
    ans+=max(0,i-bisect_right(L,L[j]-L[i]))
    
print(ans)

Submission status: Accepted

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

L_list.sort()

from bisect import bisect_left

for i in range(N):
    for j in range(i+1, N):
        t = L_list[i]+L_list[j]
        idx = bisect_left(L_list, t)
        sum += max(0, idx-(j+1))
        
print(sum)

Submission status: Accepted

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

ans = 0

for i in range(N-1):
  for j in range(i+1,N-1):
      ans +=bisect_left(L_1,L_1[i]+L_1[j])-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
n=int(input())
l=sorted(list(map(int,input().split())))
cnt=0
for x in range(n):
	for y in range(x+1,n):
		i=l[x]
		j=l[y]
		cnt+=br(l,i+j-1)-1-y
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
count = 0
l.sort(reverse = False)
for i in range(len(l)-1, 1, -1):
  for j in range(i-1, 0, -1):
    pt = bisect.bisect_right(l, l[i] - l[j])
    count += max(j - pt, 0)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

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

l.sort()
# print(l)

cnt = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

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

vL.sort()
#print(vL)

res = 0
for x in range(0, N-2):
	a = vL[x]
	for y in range(x+1, N-1):
		b = vL[y]
		# a+b<c in vL[y+1:N]
		cntC = bs.bisect_left(vL, a+b)
		res += cntC - (y+1)
		#print(res, a, b, a+b, cntC)

print(res)

Submission status: Accepted

Solution code:
# ABC143 D - Triangles
import bisect

def lower_bound(sorted_list, value):
    return bisect.bisect_left(sorted_list, value)


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

L = sorted(L)

count = 0

for a in range(N):
    for b in range(a+1,N):
        count += lower_bound(L, L[a] + L[b]) - (b + 1)
            
print(count)

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
# 昇順ソート 
l.sort()
# バイセクションサーチ（二分探索法）のライブラリbisect 
from bisect import bisect_left
# 満たされるべき条件は、a<b<c  a+b>c 
ans = 0
for i in range(n):
  for j in range(i+1, n):
    # ソートした最初の2つの数を足す
    t = l[i]+l[j]
    # bisect_left(対象リスト, 探したい値, lo=左側のインデックス, hi=右側のインデックス)
    # 2つの数の和と同じ数が、リストの何番目に入るかが求められる
    idx = bisect_left(l, t)
    # idxは、a+b=cになる境目
    # idx-(j+1)は a+b>c の条件をみたす、数がいくつあるかを計算している。
    ans += max(0, idx-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):
    for j in range(i+1, n):
        ans += bisect.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()
ans = 0
for i in range(N):
  for j in range(i+1,N):
      add_len = L[i]+L[j]
      idx = bisect.bisect_left(L, add_len)
      ans += max(0, idx-(j+1))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import sys
input = sys.stdin.readline
 
 
def main():
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted(l)
 
    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)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import sys
input = sys.stdin.readline
 
 
def main():
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted(l)
 
    result = 0
    for i in range(n-2):
        for j in range(i + 1, n - 1):
            result += bisect.bisect_left(l[j+1:], l[i] + l[j])
 
    print(result)
 
 
if __name__ == '__main__':
    main()

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(n - i - 2):
        b = l[i + j + 1]
        index = bisect.bisect_left(l, a + b)
        if index > i + j + 2:
            ans += index - (i + j + 2)

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

print(ans)

Submission status: Accepted

Solution code:

N=int(input())
L=list(map(int,input().split()))
counter=0
L.sort()
for i in range(N-2):
    k=i+2
    for j in range(i+1,N-1):    
        while k < N and L[k] < L[i] + L[j]:
            k += 1
        counter += k - j -1
print(counter)

Submission status: Accepted

Solution code:
def solve(string):
    n, *l = map(int, string.split())
    l.sort(reverse=True)
    nums = 0
    for i, a in enumerate(l[:-2]):
        j, k = i + 1, n - 1
        while (j < k):
            if (a < l[j] + l[k]):
                nums += k - j
                j += 1
            else:
                k -= 1
    return str(nums)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

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


print(a)

Submission status: Accepted

Solution code:
def solve(string):
    n, *l = map(int, string.split())
    l.sort()
    nums = 0
    for i, a in enumerate(l[:-2]):
        k = i + 2
        for j, b in enumerate(l[i + 1:-1], i + 1):
            while k < n and l[k] < a + b:
                k += 1
            nums += k - j - 1
    return str(nums)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

L.sort()

count = 0
for i, l1 in enumerate(L):
    for j, l2 in enumerate(L):
        if i < j:
            s = max(j+1, bisect_right(L, abs(l1-l2)))
            e = max(j+1, bisect_left(L, l1+l2))

            # print((i, j), (l1, l2), (abs(l1-l2), l1+l2), (s, e))
            count += max(0, e-s)
            # abs(l1-l2) <= m <= l1+l2

print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n = int(input())
l = [int(_) for _ in input().split()]
l.sort()
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        cnt += max(0, bisect_left(l, l[i] + l[j]) - (j+1))
print(cnt)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n = int(input())
l = [int(_) for _ in input().split()]
l.sort()
cnt = 0
for i in range(n):
	for j in range(i+1,n):
		cnt += max(0, bisect_left(l, l[i] + l[j]) - (j+1))
print(cnt)

Submission status: Accepted

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  l = sorted(list(int(i) for i in input().split()))
  ans = 0
  for i in range(n):
    for j in range(i+1,n):
        ans += max(0,bisect_left(l,l[i]+l[j])-j-1)
  print(ans)
  
solve()

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

Submission status: Accepted

Solution code:
n,*l = map(int, open(0).read().split())
 
for i in range(n):
    min = 1001
    min_j = -1
    for j in range(i, n):
        if l[j] < min:
            min = l[j]
            min_j = j
    l[i], l[min_j] = l[min_j], l[i]
    
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
        
        if l[i] > l[j]:
            small = l[i] - l[j]
        else:
            small = l[j] - l[i]
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < small:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        small_i = ch_min
        
        ans += big_i - small_i
 
        if i < big_i and i >= small_i:
            ans -= 1
        if j < big_i and j >= small_i:
            ans -= 1
                
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = sorted(map(int, input().split()))
 
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
    
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ans += big_i - (j + 1)
                
print(ans)

Submission status: Accepted

Solution code:
number_of_sticks = int(input())

sticks = [int(x) for x in input().split(' ')]

sticks.sort()

triangles = 0

import bisect

for i in range (number_of_sticks - 2):
    for j in range (i+2, number_of_sticks):
        max_range = j
        min_range = max(bisect.bisect(sticks, sticks[j] - sticks[i]), i + 1)
        triangles += max_range - min_range

print(triangles)

Submission status: Accepted

Solution code:
import bisect

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

cnt = 0
for first in range(N):
    for second in range(first+1,N):
        bnd = bisect.bisect_left(L,L[first]+L[second]) - 1
        dif = bnd - second
        if dif > 0:
            cnt += dif

print(cnt)

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
an = [int(i) for i in input().split()]
an.sort()
ans = 0

for i in range(n):
  for j in range(i+1,n):
    k = an[i]+an[j]-1
    ans += bisect.bisect_right(an,k)-j-1
print(ans)

Submission status: Accepted

Solution code:
import bisect
def solve(n, l):
    l = sorted(l)
    res = 0
    for i in range(n):
        for j in range(i+1, n-1):
            a, b = l[i], l[j]
            k = bisect.bisect_left(l, a+b) - 1
            res += max(0, k-j)
    return res

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

Submission status: Accepted

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

for i in range(n-2):
    for j in range(i+1, n-1):
        a = ln[i]
        b = ln[j]
        index = bisect.bisect_left(ln, a+b)-1
        ans += index - j
print(ans)

Submission status: Accepted

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

def binary_search(key,l,n):
    key_number = int((n-1)//2)
    L = 0
    R = n-1
    while True:
        if l[key_number] < key:
            L = key_number
            key_number = (L+R)//2
        else:
            R = key_number
            key_number = (L+R)//2

        if key == l[key_number]:
            return key_number
        if (R-L) == 1:
            return L+1

def binary_search(key,l,n):
    L = 0
    R = n-1
    while L < R:
        key_number = int((n-1)//2)
        if l[key_number] < key:
            L = key_number + 1
        else:
            R = key_number
    return key_number

ans = 0
#print(binary_search(7+8,l,n))

for i in range(n-2):
    for j in range(i+1,n-1):
        a = l[i]
        b = l[j]

        ans += bisect.bisect_left(l,a+b)- j -1

        #print("{} <c< {} b-a = {} a+b = {}".format(key_numberL,key_numberR,b-a,a+b))

        #print('i={} j={} key_numberL={} key_numberR={} count={}'.format(i,j,key_numberL,key_numberR,-key_numberL-double_count))
        #count += key_numberR-key_numberL-double_count
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(lambda x:int(x)*(-1),input().split()))
L.sort()
count = 0
import bisect
for i in range(N-1):
  for j in range(i+1,N-1):
    a = bisect.bisect_left(L,L[i] - L[j])
    if a - j - 1 <= 0:
      count += 0
    else:
      count += a - j - 1
print(count)

Submission status: Accepted

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

L.sort(reverse=True)

#print(L)
for i in range(N):
    L[i] *= -1
count = 0
for i in range(N-2):
    #print(i)
    for j in range(i+1, N-1):
        if -L[i] >= -L[j] * 2:
            break
        data = L[i] - L[j]
        #print(data, L[j + 1 : N])
        #print(bisect.bisect_left(L[j + 1 : N], data))
        count += bisect.bisect_left(L, data) - (j+1)

print(count)

Submission status: Accepted

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()])
from bisect import bisect_left
ans = 0
for i1,a in enumerate(L[:-2]):
    L_temp = L[i1+1:-1]
    for i2,b in enumerate(L_temp):
        L_temp2 = L[i1 + i2 + 2:]
        ans += bisect_left(L_temp2,a+b)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = sorted([int(i) for i in input().split()])
from bisect import bisect_left
ans = 0
for i1,a in enumerate(L):
    L_temp = L[i1+1:]
    for i2,b in enumerate(L_temp):
        L_temp2 = L_temp[i2+1:]
        #print(a,b,L_temp2,bisect_left(L_temp2,a+b),a+b,len(L_temp2) - bisect_left(L_temp2,a+b))
        ans += bisect_left(L_temp2,a+b)
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):
        wa = l[i] + l[j]
        indent = bisect.bisect_left(l,wa)
        #print(wa,indent)
        ans += indent - 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 b in range(1, N - 1):
    for a in range(b):
        ans += bisect_left(L, L[a] + L[b]) - b - 1
        
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())
l = list(map(int, input().split()))
ans = 0
length = len(l)
l.sort()
 
for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        ans += max(k - (j+1),0)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

for i in reversed(range(2, n)):
    for j in reversed(range(1, i)):
        fix = bisect_right(order, order[i]-order[j])
        if j > fix:
            ans += j - fix

print(ans)

Submission status: Accepted

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

#N=len(sticks)
sticks = sorted(sticks1, reverse=True)
#sticks.sort(reverse = True)
count = 0
jmax = N-2
kmax = N-1
i=0
while i<N-2:
    j= i+1
    while j<jmax+1:
        k=kmax
        while j<k:
            #sticks[i]>sticks[j]>sticks[k]
            if sticks[i]<sticks[j]+sticks[k]:
                count += k-j
                kmax = k
                break
            else:
                k -= 1
                continue
        jmax = kmax-1
        j += 1
    kmax = N-1
    jmax = N-2
    i += 1
      
print(count)

Submission status: Accepted

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

def solve(N: int, L: "List[int]"):
    L.append(2001)
    L.sort()
    sum=0
    if len(L)<3:
        print(sum)
        return

    for i in range(N-2):
        for j in range(i+1,N-1):
            tansum=L[i]+L[j]
            if tansum<=L[j+1]:
                continue
            sum=sum+bi.bisect_left(L,tansum,j+1,N+1) - (j+1)
    print(sum)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

import bisect

N = int(input())
L =[int(i)for i in input().split()]
L.sort()
ans = 0
for i in range(1,N):
    for j in range(i):
        len = L[i] - L[j] +1
        ind = bisect.bisect_left(L,len) -1
        ans += max(j-ind-1,0)
        #print(L[i],L[j],max(j-ind,0))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
import sys
def input(): return sys.stdin.readline().strip()

def main(bisect_left=bisect_left, bisect_right=bisect_right):
    n=int(input())
    ll=sorted(list(map(int,input().split())))
    c=0
    lll=len(ll)
    for i in range(lll-2):
        for j in range(i+1, lll-1):
            uk = bisect_left(ll, ll[i] + ll[j], j+1)
            lk = bisect_right(ll, abs(ll[j] - ll[i]), j+1, uk)
            c+=uk-lk
    print(c)

main()

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(n):
    for j in range(i+1, n):
        a, b = l[i-1], l[j-1]
        ab = a + b
        c = bisect_left(l, ab)
        ans += c - 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-1):
    for j in range(i+1,n):
        min_c=j+1
        max_c=bisect.bisect_left(l, l[j]+l[i])-1
        num=max_c-min_c+1
#        print(str(l[i])+" "+str(l[j])+":"+str(min_c)+" "+str(max_c)+":"+str(num))
        ans += num if num > 0 else 0
print(ans)