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
# a <= b <= c となる3数を探す
for i in range(N - 1):
    for j in range(i + 1, N):
        T = bisect.bisect_left(L, L[i] + L[j])  # c < a + bを満たすcを探している
        cnt += T - j - 1

print(cnt)

Submission status: Accepted

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

res = 0
# l[i] >= l[j] > p
# a = 
for i in range(2, N):
    cnt = 0
    for j in range(i):
        p = L[i] - L[j]
        k = bisect.bisect_right(L, p)
        cnt += i - k
        if k <= j:
          cnt -= 1
    res += cnt
print(res//2)

Submission status: Accepted

Solution code:
import sys
import math
from itertools import accumulate as acc
import bisect

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

x.sort()
ans=0
for i in range(2,N):
    for j in range(1,i):
        p=x[i]-x[j]
        if p<x[j]:
            k=bisect.bisect_right(x,p)
            ans+=j-k
print(ans)

Submission status: Accepted

Solution code:
import bisect
# 三角形の条件: c < a+b 

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

ans = 0
for i in range(N-1, 1, -1):
  a = 0
  b = i-1
  while a<b:
    if L[a]+L[b] > L[i]:
      ans += b-a
      b -= 1
    else:
      a += 1
          
print(ans)

Submission status: Accepted

Solution code:
import bisect
# 三角形の条件: c < a+b 

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

ans = 0
# a と b を固定
for a in range(N):
  for b in range((a+1), N):
    # c の動ける範囲の右端を求める
    c = L[a]+L[b]
    k = bisect.bisect_left(L, c)
    # c の動ける範囲は、[j+1, k)
    ans += k - b - 1
print(ans)

Submission status: Accepted

Solution code:
import bisect

n=int(input())
L=[int(i) for i in 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:
from bisect import bisect_right, 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):
        a = L[i]
        b = L[j]
        left = abs(a-b)
        right = a+b
        q = max(j+1,bisect_left(L,right))
        w = max(j+1,bisect_right(L,left))
        ans += q-w
print(ans)

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

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

L.sort()

ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

#リストをソートする
bar_list.sort()

cnt = 0

#１本目の棒を決める
for i in range(n):
    #２本目の棒を決める
    for j in range(i+1,n):
        #２本の棒を足して値がリストのどこに挿入できるかを調べる
        x = bisect.bisect_left(bar_list,bar_list[i]+bar_list[j])
        
        #挿入できる箇所よりも左側の棒は全て三角形に使える
        cnt += x-j-1
 
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):
    for j in range(i+1,n):
        T=bisect.bisect_left(L,L[i]+L[j])
        count += T-j-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):
    for j in range(i+1,N):
        T=bisect.bisect_left(L,L[i]+L[j])
        count+=T-j-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):
    for j in range(i+1,N):
        T=bisect.bisect_left(L,L[i]+L[j])
        count+=T-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)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

l.sort()
ans_set = set()
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l.sort()
  ln=len(l)

  ans=0
  for i,x in enumerate(l):
    for j,y in enumerate(l[i+1:]):
      ans+=bisect.bisect_left(l,x+y)-(i+j+2)

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = bisect.bisect_left(ListL, c)
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			b = ListL[indexb]
			c = a - b + 1
			indexc = bisect.bisect_left(ListL, c)

print(ans)

Submission status: Accepted

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = 0
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			indexc += 1

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = bisect.bisect_left(ListL, c)
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			indexc += 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):
    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:
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	for j in reversed(range(1, i)):
		b = ListL[j]
		index = bisect.bisect_right(ListL, a-b)
		#print(a, b)
		if (index >= j):
			break
		else:
			ans += j - index

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

total = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        c = bisect.bisect_left(l, l[a] + l[b]) - b - 1
        total += c

print(total)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    numbers = list(map(int, input().split()))
    numbers.sort()
    sol = 0
    counter = 0
    for i in range(n):
        for j in range(i+ 1, n):
            x= numbers[i] + numbers[j]
            left = j + 1
            right = n - 1
            ok = -1
            while left <= right:
                mid = (left + right) // 2
                if numbers[mid] < x:
                    ok = mid
                    left = mid + 1
                else:
                    right = mid - 1
            sol = ok
            if sol != -1:
                counter = counter - j + sol
    print(counter)

Submission status: Accepted

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

def meguru_nibutan(ar,m,key):
    a = m
    b = len(ar)
    while(abs(a-b) > 1):
        c = (a+b)//2
        if (ar[c] > key):
            a = c
        else:
            b = c
    return a

def main():
    cnt = 0
    l.sort()
    for a in range(0,n-2):
        for b in range(a+1,n-1):
            #cnt += meguru_nibutan(l,b,l[a]-l[b]) - b
            cnt += bisect.bisect_left(l,l[a]+l[b]) - 1 - b
    print(cnt)

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

Submission status: Accepted

Solution code:
import bisect
# ソートしている(a < b < c)として，三角形の存在条件は以下になる
def triangle_check(a, b, c):
    if c < a + b:
        return True
    else:
        return False
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
cnt = 0
for i in range(N - 2):
    for k in range(i + 1, N - 1):
        tmp = bisect.bisect_left(L, L[i] + L[k])
        ans += max(tmp - 1 - k, 0)
print(ans)

Submission status: Accepted

Solution code:
# ABC143D - Triangles
def binary_search(cur: int, upper_bound: int, lower_bound: int) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while abs(ok - ng) >= 1:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    # fix the longest edge -> binary search
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect as b

n = int(input().strip())
l = list(map(lambda x: int(x.strip()), input().split()))

l.sort()
ans = 0

for i in range(n - 2):
    for j in range((i + 1), (n - 1)):
        k = b.bisect_left(l, (l[i] + l[j]))
        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()))
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:
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
n = int(input())
l = sorted(list(map(int, input().split())))
res = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        res += bisect.bisect_left(l[j+1:], l[i] + l[j])
print(res)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(1,n):
  for j in range(i):
    if j > bisect.bisect_right(L,L[i]-L[j]):
      ans += j-bisect.bisect_right(L,L[i]-L[j])
print(ans)

Submission status: Accepted

Solution code:
# a - b < c
# b - a < c
# a + b > c

import bisect

n = int(input())

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

ret = 0

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

print(ret)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    l = list(map(int,input().split()))
    l.sort()
    l.append(10**7)
    ans=0

    for i in range(n-2):
        for j in range(i+1,n-1):
            ok=j
            ng=n
            while ng-ok>1:
                mid = (ok + ng)//2
                if l[mid] < l[i] + l[j]:
                    ok=mid
                else:
                    ng=mid  
            ans+=ok-j
            
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left


def p_d():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = N * (N - 1) * (N - 2) // 6

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

    print(ans)
    
p_d()

Submission status: Accepted

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

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

def isOK(key, index):
    if L[index] >= key:
        return True
    else:
        return False
        
def binary_search(key):
    left = -1
    right = len(L)
    
    while right - left > 1:
        mid = left + int((right - left) / 2)
        if isOK(key, mid):
            right = mid
        else:
            left = mid
    return right

count = 0
for a in range(N):
    for b in range(a + 1, N):
        key = L[a] + L[b]
        count += (binary_search(key) - (b + 1))

print(count)

Submission status: Accepted

Solution code:
from itertools import *

def binary_search(t, v, first, last):
	if last - first == 1:
		if v[first] > t:
			return first
		else:
			return last
	
	mid = (first + last) / 2
	if v[mid] <= t:
		return binary_search(t, v, mid, last)
	else:
		return binary_search(t, v, first, mid)

def ABC143D():
	N = int(raw_input())
	v = sorted(map(int, raw_input().split()))
	
	counter = 0
	for k, l in combinations(range(1, N), 2):
		# a <= b and a+b > c
		# a : (c-b, b]
		b = v[k]
		c = v[l]
		if b*2 < c:
			continue
		counter += k - binary_search(c-b, v, 0, k)
	print counter

ABC143D()

Submission status: Accepted

Solution code:
import bisect

n = int(input())
li = list(map(int,input().split()))
li2 = sorted(li)
cnt = 0

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

Submission status: Accepted

Solution code:
n = int(input().strip())
a = list(map(int, input().strip().split()))
na = [0]*1024
for x in a:
	na[x] += 1
sa = [0] * 1024
for i in range(1023):
	sa[i+1] = sa[i] + na[i]

ans = 0
for i in range(1, 1024):
	for j in range(i, 1024):
		mink = max(j, j-i+1)
		maxk = min(1022, i+j-1)
		if maxk < mink:
			continue

		if sa[maxk+1] == sa[mink]:
			continue

		if i != j:
			if mink != j:
				ans += na[i]*na[j]*(sa[maxk+1] - sa[mink])
			else:
				ans += na[i]*na[j]*(sa[maxk+1] - sa[mink+1]) + na[i]*na[j]*(na[j]-1)//2
		else:
			if mink != j:
				ans += na[i]*(na[j]-1)//2*(sa[maxk+1] - sa[mink])
			else:
				ans += na[i]*(na[j]-1)//2*(sa[maxk+1] - sa[mink+1]) + na[i]*(na[j]-1)*(na[j]-2)//6
print(ans)

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

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = [int(x) for x in input().split()]
L = sorted(L)
ans = 0
for one in range(N - 2):
    for two in range(one+1, N-1):
        ans += bisect.bisect_left(L, (L[one] + L[two]))-two-1
        #print(bisect.bisect_left(L, (L[one] + L[two]))-two-1)
print(ans)

Submission status: Accepted

Solution code:
def bs(L, left, right, bc):
    while left != right:
        mid = (left + right)//2
        if L[mid] > bc:
            right = mid
        else:
            left = mid+1
    if L[left] <= bc:
        return -1
    return left


def solve():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    # print(L)
    count = 0
    for i in range(1, N-1):
        for j in range(i + 1, N):
            left = 0
            right = i-1
            div = bs(L, left, right, L[j] - L[i])
            if (div != -1):
                count += i - div
                # print('counted')
                # print(i-div)

    print(count)


if __name__ == "__main__":
    solve()
    # L = [1, 2, 3, 4, 5, 6]
    # print(bs(L, 0, 3, 1))

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
import bisect
 
N=int(input())
L=list(map(int,input().split()))
 
count=0
L.sort()
for i in range(0,N):
  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:
# -*- coding utf-8 -*-
## 途中

import bisect

MOD = 10 ** 9 + 7

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

ans = 0
for i in range(n):
    for j in range(i + 2, n):
        k = bisect.bisect_right(ln, ln[j] - ln[i])
        if k < i + 1:
            c = j - (i + 1)
        elif k > j - 1:
            c = 0
        else:
            c = j - k
        ans += c
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):
    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=list(map(int,input().split()))
 
L.sort()
count=0
for i in range(N):
  for j in range(i+1,N):
    c=L[i]+L[j]
    index=bisect.bisect_left(L,c)
    count+=max(0,index-j-1)
print(count)

Submission status: Accepted

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

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

ll.sort()
ans = 0
for i in range(n - 2):
    for j in range(i+1, n - 1):
        lb = j + 1
        ub = n - 1
        b = ll[i] + ll[j]
        if ll[lb] >= b:
            continue
        if ll[ub] < b:
            ans += ub - lb + 1
            continue
        while True:
            if ub == lb:
                ans += ub - j
                break
            t = (lb + ub) // 2 + 1
            if ll[t] < b:
                lb = t
            else:
                ub = t - 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,1,-1):
  for j in range(i-1,0,-1):
    ans+=j-bisect.bisect_left(l,l[i]-l[j]+1,hi=j)
print(ans)

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:
n=int(input())
a=[int(i) for i in input().split()]
a.sort(reverse=True)
l=len(a)
ans=0
for i in range(l-2):
    e=l-1
    for j in range(i+1,l-1):
        if e<=j:
            break
        while a[i]-a[j]>=a[e] and e>j:
            ans+=(e-j)
            e-=1
ans=(n*(n-1)*(n-2))//6-ans
print(ans)

Submission status: Accepted

Solution code:
import bisect

def main():    
    n = int(input())
    L = list(int(x) for x in input().split())
    ans = 0
    c = 0
    L.sort()

    for i in range(n-2):
        for j in range(i+1, n-1):
            c = L[i]+L[j]
            l = j
            r = bisect.bisect_left(L, c, lo=l) -1
            ans += (r-l)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

def main():    
    n = int(input())
    L = list(int(x) for x in input().split())
    ans = 0
    c = 0

    L.sort()

    for i in range(n-2):
        for j in range(i+1, n):
            c = L[i]+L[j]
            l = j
            r = bisect.bisect_left(L, c, lo=l) -1
            ans += (r-l)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin 
from bisect import bisect_left
n = int(stdin.readline().rstrip())
l = [int(x) for x in stdin.readline().rstrip().split()]
l.sort()
answer = 0
for i in range(len(l)):
    for j in range(i):
        if i != j:
            k = bisect_left(l, l[i]+ l[j])
            answer += k - i - 1
print(answer)

Submission status: Accepted

Solution code:
import bisect

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

for x in range(n):
    for y in range(x + 1, n - 1):
        ans += bisect.bisect_left(l, l[x] + l[y]) - y - 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

L = sorted(L)

ans = 0

for mid in range(N - 1, -1, -1):
    for low in range(mid - 1, -1, -1):
        itr = bisect_left(L, L[low] + L[mid])
        ng = len(L) - itr
        ans += (N - 1 - mid) - ng

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

N = int(input())
A = [int(v) for v in sys.stdin.readline().split()]

A = sorted(A)

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

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

print(cnt)

Submission status: Accepted

Solution code:
input()
a = input().split(' ')
b = []
for i in a:
    b.append(int(i))
b.sort()
b.reverse()
out = 0
for i in range(len(b) - 2):
    l = i + 1
    r = len(b) - 1
    while l < r:
        if b[i] < b[l] + b[r]:
            out += r - l
            l += 1
        else:
            r -= 1

print(out)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

l = sorted(l)

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

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

print(cnt)

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

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


print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

l.sort(reverse = 1)
ans = 0

for i in range(n-2):
    indL = i+1
    indR = len(l)-1
    while 1:
        if indL == indR:
            break
        if l[i] < l[indL] + l[indR]:
            ans += (indR - indL)
            indL += 1
        else:
            indR -= 1
                
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(2, n):
    k = i - 1
    for j in range(i):
        while k > j and a[k] > a[i] - a[j]:
            k -= 1
        if k < j:
            k = j
        ans += i - k - 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 n in range(N-2):
    for m in range(n+1,N-1):
        count += bisect.bisect_left(L,L[m]+L[n]) -m-1

print(count)

Submission status: Accepted

Solution code:
# ABC143D - Triangles
def binary_search(cur: int, upper_bound: int = 1 << 60, lower_bound: int = -1) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while ok > ng:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    # fix the longest edge -> binary search
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC143D - Triangles
def binary_search(cur: int, upper_bound: int = 1 << 60, lower_bound: int = -1) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while abs(ok - ng) > 0:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    # fix the longest edge -> binary search
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def solve():
    N, *Ll = map(int, open(0).read().split())
    Ll.sort()
    ans = 0
    
    for i in range(N-1, 0, -1):
        a = 0
        b = i - 1
        while(a < b):
            if Ll[a] + Ll[b] > Ll[i]:
                ans += b - a
                b -= 1
            else:
                a += 1
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
l = list(map(int,input().split()))
l.sort()
ret = 0
from bisect import bisect_right

for i in range(n-1,1,-1):
    longest = l[i]
    for j in range(i-1,0,-1):
        second_longest = l[j]
        ret += max(j-bisect_right(l,longest-second_longest),0)
print(ret)

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import collections
import bisect

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

Submission status: Accepted

Solution code:
#
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    ll = list(map(int, input().split()))
    ll.sort()

    ans = 0
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            rest = ll[j + 1:]
            a_plus_b = ll[i] + ll[j]
            pos_right = bisect.bisect_left(rest, a_plus_b)
            count = pos_right

            ans += count
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import combinations
from collections import Counter

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

counter = Counter()

for l in L:
    counter[l] += 1

sums = [0]
L_MAX = 10**3

for i in range(1, L_MAX+1):
    sums.append(sums[i-1]+counter[i])

res = 0

for l1, l2 in combinations(L, r=2):
    upper = l1+l2
    lower = abs(l1-l2)

    tmp = sums[min(L_MAX, upper-1)] - sums[lower]
    if l1 < upper and l1 > lower:
        tmp -= 1
    if l2 < upper and l2 > lower:
        tmp -= 1

    res += tmp

print(res//3)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int,input().split()))
import bisect
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        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()
import bisect
# insert_index = bisect.bisect_left(list,x)
ans=0
for i in range(n):
	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()
#print(L)
count=0

for i in reversed(range(2,N)):
  for j in reversed(range(1,i)):
    index=bisect.bisect_right(L,L[i]-L[j])
    #print(index,j,i)
    if index<j:
     count += j-index
            
print(count)