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
N = int(input())
L = sorted([int(s) for s in input().split()])


def check_search(middle, idx_short2, l_lng, l_lows):
    return l_lows[middle] + l_lows[idx_short2] > l_lng


l_lows = L[:2]
triangles = 0


for l_lng in L[2:]:
    for idx, l_low in enumerate(l_lows):
        left = 0
        right = len(l_lows) - 1 - idx
        end = right
        while (left < right):
            middle = (left + right) // 2
            if check_search(middle, len(l_lows) - 1 - idx, l_lng, l_lows):
                right = middle
            else:
                left = middle + 1
        triangles += (end - right)
    l_lows.append(l_lng)

print(triangles)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
lines = list(int(i) for i in input().split())
lines.sort()
counter = 0

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

L.sort()

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

L_list = sorted(L_list)

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

print(total)

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):
        k = 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 = sorted(list(map(int, input().split())))

res = 0
for i in range(n):
    for j in range(i+1, n):
        res += bisect_left(l[j+1:], l[i]+l[j])

print(res)

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

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        l = bisect.bisect_right(L, abs(a-b))
        r = bisect.bisect_left(L, a+b)
        ans += r-l
        if L[l] <= a <= L[r-1]:
            ans -= 1
        if L[l] <= b <= L[r-1]:
            ans -= 1
        
print(ans//3)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
total = []
for a in range(N-2):
    for b in range(a+1, N-1):
        la = L[a]
        lb = L[b]
        ab = la + lb
        sa = abs(la-lb)
        first = bisect.bisect_left(L, ab) -1 
        second = bisect.bisect_right(L, sa)
        if first > second and first >= b+1:
            count += first - max(second, b+1) +1
print(count)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

input = sys.stdin.readline


def main():
    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):
            k = bisect_left(L, a + L[j])
            ans += (k - 1 - j)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

input = sys.stdin.readline


def main():
    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):
            k = bisect_left(L, a + L[j], lo=j)
            ans += (k - 1 - j)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

"""
nC3
|b - c| < a < |b + c|

nC2
b_i, c_iを除くiの中で|b-c|< a <b+cを満たすaの数をタス
"""

N = int(input())
L = LI()
L.sort()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#3本目を選ぶ時に2本目と同じ長さのものを選べることに注意する
import bisect
n=int(input())
l=sorted(list(map(int,input().split())))

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

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

    ans = 0
    for i in range(n-2):
        e1 = ll[i]
        for j in range(i+1, n-1):
            e2 = ll[j]
            # ans += bisect.bisect_left(ll[j+1:], e1+e2)
            comb = bisect.bisect_left(ll, e1+e2)
            comb -= (j+1)
            ans += max(comb,0)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
L = list(sorted(map(int, readline().split())))

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

Submission status: Accepted

Solution code:
import bisect
 

def main():
    n = int(input())
    ls = sorted(list(map(int, input().split())))
 
    ans = 0
    for i in range(n-1):
        for j in range(i+1, n):
            a = ls[i]
            b = ls[j]
            k = bisect.bisect_left(ls, a+b)
            if k > j:
                ans += k - j - 1
    print(ans)
 
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
 
import bisect
 
ans=0
l.sort()
 
for i in range(n):
    a=l[i]#一番短い棒を確定
    for j in range(i+1,n):
        b=l[j]#二番目に短い棒を確定
        ind=bisect.bisect_left(l,a+b)#二辺の和と同じだったら耐えないので，left
        ans+=max(0,ind-(j+1))#三角形の成立条件と，一番長い棒であることを考える

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
#a,b,n=map(int,input().split())
l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

import bisect

ans=0
l.sort()

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    l=list(map(int,readline().split()))
    l.sort()

    s=[]
    indexs=[]
    for i in range(n-2):
        for j in range(i+1,n-1):
            s.append(l[i]+l[j])
            indexs.append(j+1)

    l=np.array(l,dtype=np.int64)
    s=np.array(s,dtype=np.int64)
    indexs=np.array(indexs,dtype=np.int64)
    ans=(np.searchsorted(l,s)-indexs).sum()
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import bisect


def main():
    n = int(input())
    ls = sorted(list(map(int, input().split())))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

counter=0
for i in range(2,n):
	now=i+1
	tmpcounter=0
	for j in range(1,i):
		while now <= n:
			if llist[j-1] + llist[i-1] > llist[now-1]:
				tmpcounter += 1
				now += 1
			else:
				break
		counter += tmpcounter

print(counter)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from bisect import bisect_right

def solve(N, lines):
    return sum(b - bisect_right(lines[:b], lines[c] - lines[b]) for c in range(N - 1, -1, -1) for b in range(c - 1, -1, -1))


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from bisect import bisect_right

def solve(N, lines):
    ans = 0
    for c in range(N-1, -1, -1):
        for b in range(c-1, -1, -1):
            i = bisect_right(lines[:b], lines[c] - lines[b])
            ans += b-i
    return ans


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# 34:14
from bisect import bisect_left


N = int(input())
L = sorted(map(int, input().split()))
ans = sum(
    j - bisect_left(L, L[i] - L[j] + 1, 0, j) for i in range(2, N) for j in range(i)
)
print(ans)

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():

    n = gete(int)
    l = list(geta(int))
    l.sort()

    l_max = 2000
    # p[k]: number of sticks with length >= k
    p = [0] * (l_max + 1)

    cur = 0
    acc = n
    for i in range(l_max + 1):
        p[i] = acc

        if cur < n and i == l[cur]:
            while cur < n and l[cur] == i:
                acc -= 1
                cur += 1

    ans = 0
    for i, a in enumerate(l):
        for b in l[i + 1:]:
            ans += p[abs(a - b) + 1] - p[a + b]

            if abs(a - b) < a:
                ans -= 1

            if abs(a - b) < b:
                ans -= 1
    print(ans // 3)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

def main():
    n = int(input())
    l = sorted(list(map(int, input().split())))
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            kmax = bisect.bisect_left(l, a+b)
            #print("k", kmax, "j", j, "k-j", kmax-(j+1), a, b)
            count += max(kmax-(j+1),0)
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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-1):
    k = L[i]+L[j]
    l,r = 0,N
    while (r-l) > 1:
      m = (l+r)//2
      if L[m] >= k:
        r = m
      else:
        l = m

    ans += r-j-1

print(ans)

Submission status: Accepted

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

L.sort()
ans = 0
"""
#二分探索ver　-> import bisectと同じでもTLE.
#importはC言語veっで実行してるらしい．積極的に使おう 
for a in range(len(L)):
    for b in range(a+1,len(L)):
        c_max = L[a]+L[b]-1
        left = b+1
        right = len(L)
        mid = (right+left)//2
        while left<right:
            mid = (right+left)//2
            if c_max < L[mid]:
                right = mid
            else:
                left = mid+1
        ans += left-(b+1)
"""
for i in range(0,n-2):
    j = i+1
    k = j+1
    while j<n-1:
        while(k<n):
            if (L[j]-L[i]<L[k] and L[k]<L[i]+L[j]):
                k+=1
            else:
                break
        ans += k-(j+1)
        j += 1

        
print(ans)

Submission status: Accepted

Solution code:
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):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le = m + 1
            else:
                ri = m
        cnt += le - j - 1
print(cnt)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
# abc143_d.py
def can_make(i, j, k):
    if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[j] + L[i]:
        return True
    return False


def find(i, j, L):
    left = j
    right = N
    mid = (right + left)//2
    flg = False
    while right - left > 1:
        mid = (right + left)//2
        # print("left,mid,right", left, mid, right)
        if can_make(i, j, mid):
            # print("can make")
            left = mid
            flg = True
        else:
            right = mid
    # print("j,mid", j, mid)
    # print(flg)
    if flg:
        return left-j
    else:
        return 0


N = int(input())
L = list(map(int, input().split()))
cnt = 0
L = sorted(L, reverse=True)
for i in range(N):
    for j in range(i+1, N):
        # print(L)
        # print("i,j", i, j, L[i], L[j])
        cnt += find(i, j, L)
print(cnt)

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, a in enumerate(l):
    for j, b in enumerate(l[i+1:], i + 1):
        k = bisect_left(l, a + b)
        ans += k - j - 1

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    L = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    L.sort()
    import bisect

    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            k = bisect.bisect_left(L, (L[i]+L[j]))
            if k > j:
                cnt += k - j -1

    print(cnt)


resolve()

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

Submission status: Accepted

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

import bisect

L.sort()
# print(L)

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

N=int(input())
L=[int(i) for i in 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])
        count+=max(0,index-j-1) #indexは条件に当てはまらない最も左の値から１を引く
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())

in_ar = [0 for _ in range(n)]
in_ar = [int(s) for s in input().split()]
in_ar.sort()
res = 0
#print(in_ar)
for i in range(n):
    for j in range(i,n):
        if(i == j):
            continue

        a = in_ar[i]
        b = in_ar[j]
        st = bisect.bisect_left(in_ar, abs(a-b))
        en = bisect.bisect_left(in_ar, abs(a+b))
        #print(a,b,j,en)
        res += en - j - 1
        '''
        if(abs(a-b) < a):
            res -= 1
        if(abs(a-b) < b):
            res -= 1
        '''

print(res)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect

input = lambda: sys.stdin.readline().rstrip()


def solve():
    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):
            a, c = L[i], L[j]
            amc = abs(a - c)
            apc = a + c
            low = bisect.bisect_right(L, amc)
            low = max(j + 1, low)
            upp = bisect.bisect_left(L, apc)
            upp -= 1
            if low <= upp:
                ans += (upp - low) + 1

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
p.sort()
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j<n-2:
            l=j+1
            r=n-1
            while r-l>1:
                c=(l+r)//2
                if p[c]>=p[i]+p[j]:
                    r=c
                else:
                    l=c
            if p[l]<p[i]+p[j]:
                ans+=l-j
            if p[r]<p[i]+p[j]:
                ans+=1
        if j==n-2:
            if p[i]+p[n-2]>p[n-1]:
                ans+=1
print(ans)

Submission status: Accepted

Solution code:
import bisect
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)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = tuple(map(int, input().split()))
L = sorted(L)

counts = 0
for i in range(n-2):
    a = L[i]
    ci = i+2
    for bi in range(i+1, n-1):
        while ci < n and L[ci] <  a + L[bi]:
            ci += 1
        counts += ci - 1 - bi
print(counts)

Submission status: Accepted

Solution code:
n = int(input())
L = tuple(map(int, input().split()))
L = sorted(L, reverse=True)

counts = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        cc = a - L[j]
        left = j
        right = n
        while right - left > 1:
            mid = (right+left)//2
            if L[mid] > cc:
                left = mid
            else:
                right = mid
        counts += left - j
print(counts)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
素直に実装するとN**9
"""

import bisect
 
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
ans = 0
for ai in range(N):
    for bi in range(ai+1, N):
        ci = bisect.bisect_left(L, L[ai]+L[bi])
        if ci > bi:
            ans += ci - bi - 1
        else:
            pass
 
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
nums = map(int, raw_input().split(' '))
nums.sort()
r = 0


for c in range(2, n):
	a,b = 0, c - 1

	for a in range(c - 1):
		lo,hi = a+1, c-1
		while(lo < hi):
			med = (lo + hi) / 2
			if nums[a] + nums[med] > nums[c]:
				hi = med
			else:
				lo = med + 1
		
		r += (c - lo) if (nums[a] + nums[lo] > nums[c]) else (0)
print r

Submission status: Accepted

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

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

L.sort(reverse=True)
acm_l = []

for i in range(1, N-1):
    acm_l.append([ -(L[i] + L[j]) for j in range(i+1, N)])

# print(L)
# print(acm_l)


ans = 0
for i, l in enumerate(L[:-2]):
    for j in range(i, N-2):
        idx = bisect.bisect_left(acm_l[j], -l)
        # print(acm_l[j], l, idx)
        ans += idx

print(int(ans))

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
L = sorted(list(map(int, input().split())))
 
ans = 0
for i in range(N):  # １番短い棒
    for j in range(i + 1, N):  # 真ん中の長さの棒
        ans += bisect_left(L, L[i] + L[j]) - (j + 1)
 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(n - 1, 1, -1):
    l = 0
    r = i - 1
    while l < r:
        if L[i] < L[r] + L[l]:
            count += r - l
            r -= 1
        else:
            l += 1
print(count)

Submission status: Accepted

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

    n, *l = map(int, open(0).read().split())
    l.sort()
    ans = 0
    for i, x in enumerate(l):
        for y in l[i + 1:]:
            k = y - x
            z = bs(l, k)
            if i - z >= 1:
                ans += (i - z)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

def solve():
  n = int(input())
  L = [int(i) for i in input().split()]
  L.sort()
  total = 0
  for s in range(n-2):
    for m in range(s+1, n-1):
      #二分探索モジュールbisect
      l = bisect.bisect_left(L,L[s]+L[m], 0, n)
      if l>m: total += l-m-1
  print(total)

  return 0

if __name__ == "__main__":
  solve()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

n=int(input())
l=[int(x) for x in input().rstrip().split()]
l.sort()
ans=0
# print(l)
for i in range(n):
  for j in range(i+1,n):
    ans+=bisect_left(l,l[i]+l[j])-j-1
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(n):
    a = L[i]
    for j in range(i+1, n):
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        if k > j:
            ans += k - j - 1
print(ans)

Submission status: Accepted

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

print (ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import combinations
from collections import defaultdict

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

for l in L_:
    d[l] += 1

n = 0
for i in range(2001):
    n += d[i]
    d[i] += n - d[i]

ans = 0

for a, b in combinations(L_, 2):
    #print(a, b, d[a+b-1], d[abs(a-b)], d[a+b-1] - d[abs(a-b)])
    n =  d[a+b-1] - d[abs(a-b)]
    if abs(a-b) < a:
        ans -= 1
    if abs(a-b) < b:
        ans -= 1
    ans += n
    
print(ans//3)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

L.sort()
len_L = len(L)

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()

    l_c = [0] * 2001

    for li in l:
        l_c[li] += 1
    for i in range(2000):
        l_c[i + 1] += l_c[i]

    ans = 0

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

    print(ans)


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

Submission status: Accepted

Solution code:
#関数を持ってくる
from bisect import bisect_left
#入力
n = int(input())
l_list = list(map(int,input().split()))
#ソート
l_list.sort()
#答えの数を数えるものを作る
ans = 0
#三角形の中で一番短い辺aに対して
for i in range(n-2):
  a = l_list[i]
  #2番目に短い辺bが存在し
  for j in range(i+1,n-1):
    b = l_list[j]
    #一番長い辺cの下限値であるa+bのリスト内で入る位置x
    x = bisect_left(l_list,a+b)
    #cとして考えられる値はjとxの間の個数
    ans += x-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ans = 0
for small in range(n - 2):
    for mid in range(small + 1, n - 1):
        div = bisect_left(l, l[small] + l[mid], 0, n)
        if div > mid + 1:
            ans += div - mid - 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
import copy

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


ans = 0
for small in range(n - 2):
    for mid in range(small + 1, n - 1):
        div = bisect_left(l, l[small] + l[mid], 0, n)
        if div > mid + 1:
            ans += div - mid - 1

print(ans)