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:
#!/usr/bin/env python3
import sys
# import numpy as np
import bisect

def solve(N: int, L: "List[int]"):
    L = sorted(L)
    ct = 0
    for a in range(len(L)-2):
        a_ = L[a]
        for b in range(a+1, len(L)-1):
            b_in = bisect.bisect_left(L,a_+L[b]) - b -1
            if b_in > 0:
                ct+=b_in

            # print("L :{} a+Lb :{}  b_in:{}".format(L[b+1:],a_+L[b],ct))
        # ct += sum([bisect.bisect_left(L[b+1:],a_+L[b]) for b in range(a+1, len(L)-1)])
    print(ct)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        ans += bisect_left(l, l[i] + l[j]) - j - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left
def main():
    n=int(input())
    L=[int(_) for _ in input().split()]
    L.sort()
    ans=0
    for a in range(n-2):
        for b in range(a+1,n-1):
            ans+=bisect_left(L,L[a]+L[b])-b-1
    print(ans)


if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import *
import itertools
n = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for a,b in itertools.combinations(L,2):
    ans += max(0,bisect_left(L,a+b) - bisect_right(L,abs(a-b)))
    ans -= (2*a > b) + (2*b > a)
print(ans//3)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

n, *l = map(int, open(0).read().split())

l.sort()
ans = 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_num = bisect.bisect_left(l, a+b)  # cとしてありうる辺の長さの数。(ただしb以下の長さの辺も含む)
        ans += c_num - (j+1)  # b以下の長さの辺の数(j+1本)を引く
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for a in range(N):
    for b in range(a+1, N):
        c = bisect.bisect_left(L, L[a] + L[b])
        if c > 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


def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    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:
from bisect import bisect_left

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

L.sort()

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(2, N):
        for i in range(1, j):
            d = e[j] - e[i]
            ans += max(0, i - bisect_right(e, d))

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
#
# sys.setrecursionlimit(10 ** 7)
#
# (int(x)-1 for x in input().split())
# rstrip()

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(2, N):
        for i in range(1, j):
            d = e[j] - e[i]
            ans += i - bisect_right(e, d, lo=0, hi=i)

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(N):
        for i in range(j):
            d = e[j] - e[i]
            ans += i - bisect_right(e, d, lo=0, hi=i)

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()

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

print(ans)

Submission status: Accepted

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

L.sort()
from bisect import bisect_left
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        x = L[i]+L[j]
        idx = bisect_left(L,x)
        ans += idx - j - 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left


def main():
    n = int(input())
    l = [int(x) for x in input().split()]
    l.sort()
    answer = 0
    for i in range(n):
        for j in range(i + 1, n):
            index = bisect_left(l, l[j] + l[i])
            answer += index - j - 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
*L, = map(int, input().split())
# n = 4
# L = [3, 4, 2, 1]
# n = 7
# L = [218, 786, 704, 233, 645, 728, 389]
L.sort()
c = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        c += bisect.bisect_left(L, L[i]+L[j])-(j+1)
print(c)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations
import math
import bisect
import numpy as np
from collections import defaultdict, deque
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 6)
# MOD = 10 ** 9 + 7
# INF = float("inf")


def main():
    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_left(l, a+b) - j - 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
from itertools import combinations
N = int(input())
L = sorted(list(map(int,input().split())))
cnt = 0
for x in combinations(range(N),2):
    i,j = x[0],x[1]
    if j<i:
        i,j = j,i
    indr = bisect_left(L,L[i]+L[j])
    indl = bisect_right(L,abs(L[i]-L[j]))
    if j<indl or i>indr:
        cnt += indr-indl
    elif indr>j>=indl and i<indl:
        cnt += indr-indl-1
    elif indr>j and i>=indl:
        cnt += indr-indl-2
    elif j>=indr and indr>i>=indl:
        cnt += indr-indl-1
print(cnt//3)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a = tuple(sorted(a))
ans = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    chk = a[i]+a[j]
    ind = bisect.bisect_left(a,chk)
    ans += (ind-j-1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    from bisect import bisect_left
    N = int(input())
    L = sorted(list(map(int, input().split())))
    ans = 0
    for a in range(N-2):
        for b in range(a+1, N-1):
            ans += bisect_left(L, L[a]+L[b]) - b - 1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
# https://atcoder.jp/contests/abc143/tasks/abc143_d
N = read_a_int()
L = read_ints()
L.sort()
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        a = L[i]
        b = L[j]
        bigest_idx = bisect_left(L, a + b, lo=j + 1)
        smallest_idx = bisect_right(L, b - a, lo=j + 1)
        tmp = bigest_idx - smallest_idx
        # if smallest_idx <= i < bigest_idx:
        #     tmp -= 1
        # if smallest_idx <= j < bigest_idx:
        #     tmp -= 1
        ans += tmp
        # print(i, j, smallest_idx, bigest_idx, tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    A.sort()

    def is_ok(l1, l2, l3):
        if A[l2] < A[l1] + A[l3]:
            return True
        else:
            return False

    def binary_search_meguru(l1, l2):
        ng = l1
        ok = l2
        while abs(ok - ng) > 1:
            mid = ng + (ok - ng) // 2
            if is_ok(l1, l2, mid):
                ok = mid
            else:
                ng = mid
        return ok

    ans = 0
    for l1 in range(N):
        for l2 in range(l1+1, N):
            # l2 を最大辺とする
            ans += l2 - binary_search_meguru(l1, l2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect


n = int(input())
l = list(map(int, input().split()))
ans = 0
l = sorted(l)
for i in range(n):
    for j in range(i + 1, n):
        sum_ = l[i] + l[j]
        diff = l[j] - l[i]
        right = bisect.bisect_left(l, sum_)
        left = bisect.bisect_right(l, diff)
        if left <= i < right:
            ans -= 1
        if left <= j < right:
            ans -= 1
        ans += right - left
print(ans // 3)

Submission status: Accepted

Solution code:
from bisect import bisect_left
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):
        ans += bisect_left(l, l[a]+l[b]) - b - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from bisect import bisect_left

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
def ok(a,b,c):
    return c < a+b
for i in range(n):
    for j in range(i+1,n):
        bot = j
        top = n
        while top-bot > 1:
            mid = (top+bot)//2
            if ok(l[i],l[j],l[mid]):bot = mid
            else:top = mid
        ans += bot - j
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

n = int(input())

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

n = int(input())

l_list = [int(x) for x in input().split()]
l_list.sort()
# print(l_list)

ans = 0
for i in range(n - 2):
    continue_flag = False
    for j in range(i + 1, n - 1):
        ans += bisect_left(l_list, l_list[j] + l_list[i]) - (j + 1)
print(ans)

Submission status: Accepted

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


n = int(input())
li1 = [0]
li2 = list(map(int, input().split()))
li = li1+li2
count = 0
li.sort()
for i in range(1, n-1):
    for j in range(1, n):
        if j>i:
            k = bisect_left(li, li[i]+li[j])
            if k - j -1> 0:
                count += k - j - 1
print(count)

Submission status: Accepted

Solution code:
# solution
import io
import math
import heapq
import bisect

MOD = 10**9+7

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

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

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

Submission status: Accepted

Solution code:
def low(list, j, tag):
    ok = j
    ng = n
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if list[mid] >= tag:ok = mid
        else:ng = mid
    return ok

n = int(input())
l = list(map(int,input().split()))
l = sorted(l, reverse = True)
#print(l)
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        temp0 = l[i] - l[j]
        temp1 = l[j] + l[i]
        cnt += low(l, j, temp0 + 1) - j
        #print((l[i],l[j]), j, low(l, j, temp0 + 1) - j, cnt)
print(cnt)

Submission status: Accepted

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

l.sort()
from bisect import bisect_left as bl

ans = 0
for i in reversed(range(2,n)):
    for k in range(1 ,i):
        ng = l[i] - l[k] + 1
        tmp = bl(l[:k],ng)
        ans += max(0,k-tmp)
print(ans)

Submission status: Accepted

Solution code:

n=int(input())
l=list(map(int , input().split()))
#尺取り法
l.sort()
s=0
for i in range(n):
  if i+2<n:
    k=i+2
  else:
    break
  for j in range(i+1,n):
    while (k<n and l[k]<l[i]+l[j]):
      k+=1
    s+=k-(j+1)
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect


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

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

Submission status: Accepted

Solution code:
N = int(input())
L = sorted(map(int, input().split()), reverse=True)
cnt = 0

for i in range(N - 2):
    c = L[i]
    
    for j in range(i + 1, N - 1):
        b = L[j]
        l = j
        r = N - 1
        
        while True:
            idx = -(-(l + r) // 2)
            
            if L[idx] > c - b:
                if l == idx:
                    break
                l = idx
            else:
                if r == idx:
                    break
                r = idx
                
        cnt += l - j
        
print(cnt)

Submission status: Accepted

Solution code:
import bisect

N = int(input())

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

def calculate(n,arr):
    arr = sorted(arr)
    sum = 0
    for i in range(n-1):
        for j in range(i+1,n):
            mmm = arr[j] + arr[i] - 1
            nnn = bisect.bisect_right(arr,mmm)
            sum = sum + nnn - (j + 1)

    print(sum)


calculate(N,ARR)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
import bisect
ans=0
for i in range(n):
  for j in range(i):
    index=bisect.bisect_right(l,l[i]-l[j])#このindexよりも右側のものでかつjより左のものが当てはまるもの
    ans += max(j - index,0)
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
L=list(map(int,input().split()))
i=0
L=sorted(L)
for a in range(N):
  for b in range(a+1,N):
    c=bisect.bisect_left(L,L[a]+L[b])
    if c>b:
      i+=c-b-1
    else:
      i=i
print(i)

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):
        left = bisect.bisect_right(L, L[j] - L[i])
        right = bisect.bisect_left(L, L[j] + L[i])
        res = right - left
        if left <= i and i <= right:
            res -= 1
        if left <= j and j <= right:
            res -= 1
        ans += max(0, res)

print(ans // 3)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
import bisect
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)
        l = bisect.bisect_left(L,b-a)
        ans += k-max(j+1,l)
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 ind_a, a in enumerate(L):
    for ind_b, b in enumerate(L[ind_a+1:]):
        x = bisect.bisect_left(L[ind_b+ind_a+2:], a+b)
        ans += x

print(ans)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
L=sorted(map(int,input().split()))
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
n = int(input())
l = sorted(list(map(int,input().split())))
 
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 - j - 1
    
print(count)

Submission status: Accepted

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

count = 0
for i in range(n):
  for j in range(i+1,n):
    index = bs.bisect_left(l,l[i]+l[j])
    count += index - j - 1
    
print(count)

Submission status: Accepted

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

nums.sort()
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        a = nums[i]
        b = nums[j]
        ans += bisect_left(nums, a + b) - j - 1 
print(ans)

Submission status: Accepted

Solution code:
# 初期入力
#棒を長さでソートし、
N = int(input())
L = list(map(int, input().split()))
L_sorted =sorted(L)

#3本選ぶ組み合わせ,1番長いもの2本決めると、短いものは最低の長さが決まる　はだめだった
#短い2本選んで、長いものの境界を考えると「lo,hi」がなくなる
import bisect

count =0
for i in range(0,N-1): #一番長い
    for j in range(i+1,N):#真ん中
        x =L_sorted[i] + L_sorted[j]
        index =bisect.bisect_left(L_sorted,x)
        a=1
        #count.append(index -j-a)
        count += max(0,index -j-a)
        #print(L_sorted[i],L_sorted[j],L_sorted[index-a],index,x)

print(count)

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 - 1):
  for j in range(i + 1, N):
    index = bisect.bisect_left(L, L[i] + L[j]) #AC:約2.9秒
    count += max(0, index - j - 1)

"""
    失敗:回答は出るがTLE:ランダム配列1000本で約6.2秒
    index = bisect.bisect_left(L[j+1:],L[i] + L[j]) #TLEの原因?
    count += index
"""
print(count)

Submission status: Accepted

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())),reverse=True)
ans=0
for c in range(n-2):
    for b in range(c+1,n-1):
        flag=0
        if l[c]-l[b]+1>l[b+1]:
            flag=1
        elif l[c]-l[b]+1<=l[n-1]:
            ans+=(n-1-b)
            flag=1
        m=n-1
        h=b+1
        while flag!=1:
            x=(h+m)//2
            bef=x
            if l[x]>l[c]-l[b]+1:
                h=x
                x=(m+h)//2
            elif l[x]<l[c]-l[b]+1:
                m=x
                x=(m+h)//2
            else:
                if x==n-1:
                    ans+=(x-b)
                    break
                if l[x+1]==l[c]-l[b]+1:
                    h=x
                    x=(m+h)//2
                else:
                    ans+=(x-b)
                    break
            if bef==x:
                ans+=(x-b)
                break
print(ans)

Submission status: Accepted

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

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

print(s)

Submission status: Accepted

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

ans = 0

import bisect

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
l=sorted(map(int,input().split()))
ans=0
for a in range(n):
    for b in range(a):
        A=l[a]
        B=l[b]
        c=bisect.bisect_left(l,A+B)
        ans+=c-a-1
print(ans)

Submission status: Accepted

Solution code:
def main():
  import bisect
  n=int(input())
  l=sorted(list(map(int,input().split())))
  ans=0
  for a in range(n-2):
    for b in range(a+1,n-1):
      ans+=bisect.bisect_left(l,l[a]+l[b])-b-1
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import bisect

n = int(input())

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

for i in range(n):
  for j in range(i+1, n):
    k = bisect.bisect_left(l, l[i]+l[j])
    #if k > j:
    ans += k - j - 1

print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

sls = sorted(Ls)
count = 0
for i in range(N - 1):
    for j in range(i+1, N):
        a, b = sls[i], sls[j]
        right = bisect.bisect_left(sls, b + a)
        count += right - j - 1
print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    k = i
    for j in range(i + 1, N):
        while k < N and A[k] < A[j] + A[i]:
            k += 1
        ans += (k - 1) - j
print(ans)

Submission status: Accepted

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