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 collections

N = int(input())
L = [int(i) for i in input().split()]
M = max(L)
c = collections.Counter(L)
li = [c[i] for i in range(M+1)]

ans = 0
for i in range(1,M+1):
    for j in range(i,M+1):
        if i == j:
            ans += int(li[i] * (li[i] - 1) * (li[i] - 2) / 6)
            ans += int(sum(li[j+1:min(M+1, i+j)]) * li[i] * (li[i] - 1) / 2)
        else:
            ans += int(li[i] * li[j] * (li[j] - 1) / 2)
            ans += li[i] * li[j] * sum(li[j+1:min(M+1, i+j)])
print(int(ans))

Submission status: Accepted

Solution code:
import bisect


def submit():
    n = int(input())
    llist = list(map(int, input().split()))

    llist.sort()
    ans = 0
    for a in range(n):
        for b in range(a + 1, n):
            c_min = llist[b] - llist[a]
            c_max = llist[a] + llist[b]
            cs = bisect.bisect_right(llist, c_min, lo=b + 1)
            ce = bisect.bisect_left(llist, c_max, lo=b + 1)
            ans += ce - cs

    print(ans)


if __name__ == "__main__":
    submit()

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

Submission status: Accepted

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

a = [0 for i in range(1000)]
b = [0 for i in range(1000)]

ans = 0

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

b[0] = a[0]

for i in range(1, 1000):
    b[i] = a[i] + b[i-1]

for i in range(1000):
    ans += a[i] * (a[i]-1) * (a[i]-2) // 6
    ans += a[i] * (a[i]-1) // 2 * (b[min(2*i, 999)]-a[i])

for i in range(1, 998):
    for j in range(i+1, 999):
        ans += a[i] * a[j] * (b[min(999, i+j)]-b[j])

print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
li = [int(x) for x in input().split()]
li.sort()
ans = 0
for i in range(n-1):
    for j in range(n-1):
        if i<j:
            a = bisect.bisect_left(li,li[i]+li[j])
            ans += a-1-j
print(ans)

Submission status: Accepted

Solution code:
import bisect

_n = input()
ls = list(sorted(map(int, input().strip().split())))
total = 0

for i in range(len(ls) - 2):
    for j in range(i + 1, len(ls) - 1):
        a = ls[i]
        b = ls[j]
        left = j + 1
        num = bisect.bisect_left(ls, a + b) - left
        total += num

print(total)

Submission status: Accepted

Solution code:
import bisect


def main(ls):
    total = 0

    for i in range(len(ls) - 2):
        for j in range(i + 1, len(ls) - 1):
            a = ls[i]
            b = ls[j]
            left = j + 1
            num = bisect.bisect_left(ls, a + b) - left
            total += num

    print(total)


_n = input()
ls = list(sorted(map(int, input().strip().split())))

main(ls)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0
A.sort()
from bisect import bisect_left as bi
for i in range(n):
  for j in range(i):
    index=bi(A,A[i]+A[j])
    ans+=index-1-i
    
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):
        a = L[i]
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        if k > j:
            count += k -j -1
print(count)

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:
"""
a < b + c
b < c + a
c < a + b
↓
a < b < c && c < a+b
"""
import bisect
n = int(input())
l = list(map(int, input().split()))

ans = 0
l.sort()

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
# a <= b <= c
for b in range(N):
    for a in range(b):
        ab = L[a] + L[b]
        r = bisect.bisect_left(L, ab) - 1
        l = b
        ans += r - l
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left


n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-2):
    a = l[i]
    for j in range(i+1, n-1):
        b = l[j]
        cnt += bisect_left(l, a+b) - j - 1
        # bisec_left(l, x)でlの中にxを挿入できる点を探す
print(cnt)

Submission status: Accepted

Solution code:
from bisect import bisect_left

n = int(input())
l = [int(_) for _ in input().split()]
l.sort()
ans = 0

for s in range(n - 2):
    for t in range(s + 1, n - 1):
        x = bisect_left(l, l[s] + l[t])
        ans += x - t - 1

print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = 0
for b in range(n):
    for a in range(b):
        tmp = l[a] + l[b]
        r = bisect.bisect_left(l, tmp)
        ll = b + 1
        ans += max(0, r - ll)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
li = 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(li, li[i] + li[j]) - j - 1
print(ans)

Submission status: Accepted

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

print(result)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
 
for i in range(n-2):
    for j in range(i+1, n-1):
        ans += bisect.bisect_left(l,l[i] + l[j]) - j - 1
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
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
from bisect import bisect_left

def main():
    N = int(input())
    L = sorted(input_nums())
    count = 0
    for i in range(N-2):
        a = L[i]
        for j in range(i+1, N-1):
            count += bisect_left(L, a+L[j]) - j - 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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):
        a = l[i] + l[j]

        mi = 0
        ma = n - 1
        while mi != ma:
            m = (mi + ma) // 2 + 1
            if l[m] < a:
                mi = m
            else:
                ma = m - 1
        ans += max(0, mi - j)
print(ans)

Submission status: Accepted

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

cnt=0
import math
for i in range(n):
    for j in range(i+1,n):
        #i<j 
        x=lists[i]
        y=lists[j]
        left=i 
        right=j 
        index=bisect.bisect_right(lists,y-x)
        cnt+=min(j-i-1,j-index)

print(cnt)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

L.sort()

ans = 0
# a(一番短い辺)を固定して，b, cで尺取
for a in range(N):
    c = a + 1
    for b in range(a + 1, N):
        while c < N and L[c] < L[a] + L[b]:
            c += 1
        ans += c - (b + 1)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

L.sort()
ans = 0
# 辺a, b, c に関して，a, bを全探索．cが最大長の辺を想定
# cはa, bよりも大きく，a+b より小さければ良い
for i in range(N):
    for j in range(i):
        ans += bisect.bisect_left(L, L[i] + L[j]) - i - 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):
          ans += bisect.bisect_left(l,l[i] + l[j]) - j - 1
print(ans)

Submission status: Accepted

Solution code:
#143_D
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 += max(bisect_left(l,l[i]+l[j]) - j - 1, 0)
        
print(res)

Submission status: Accepted

Solution code:
import sys
import bisect


def input():
    return sys.stdin.readline()[:-1]


if __name__ == "__main__":
    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):
            b = bisect.bisect_left(L, L[i]+L[j])
            cnt += b-j-1
    print(cnt)

Submission status: Accepted

Solution code:
import bisect

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

    ans = 0
    L = sorted(L)

    for i in range(N):
        a = L[i]
        for j in range(i+1, N):
            b = L[j]
            target = a+b

            c = bisect.bisect_left(L, target)
            ans = ans + (c-j-1)

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

Submission status: Accepted

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

ans = 0

for a in range(N - 2):
    b = a + 1
    c = a + 2
    while c < N and b < N - 1:
        if L[c] >= L[a] + L[b]:
            ans += c - 1 - b
            b += 1
            if b == c:
                c += 1
        else:
            if c == N - 1:
                ans += (c - b) * (c - b + 1) // 2 
                break
            else:
                c += 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+1, N):
    sum = L[i]+L[j]
    count = bisect_left(L, sum)
    ans += max(0, count-j-1)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = sorted(inpl())
res = 0
for i in range(n-1):
    for j in range(i+1,n):
        c = bisect.bisect_left(a,a[i]+a[j]) 
        res += c-j-1
print(res)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
import bisect

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

ans = 0
for m in range(N):
    for s in range(m):
        #このfor文の時点で、時点でs<mを満たす。
        # L[m] <= L[l] < L[s] + L[m]
        # 使用している辺は使えず、sortしてあるので、m<lという条件が加わる。
        # m<lの時点で、L[m] <= L[l]は満たすので、m<l とL[l] < L[s] + L[m]で考える。
        ans += bisect.bisect_left(L,L[s]+L[m]) - (m+1)
        
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
L = sorted(list(map(int,input().split())),reverse = False)
cnt = 0
for a in range(0,N-2):
    A = L[a]
    for b in range(a+1,N-1):
        B = L[b]
        cnt += bisect_left(L,A+B)-b-1
print(cnt)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import *

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

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

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        ans += bisect_right(L, L[i] + L[j] - 1, j + 1) - j - 1
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):
        if L[i]+L[j]<=L[j+1]:
            continue
        if L[i]+L[j]>L[n-1]:
            cnt+=n-j-1
            continue
        index=bisect.bisect_left(L,L[i]+L[j])
        cnt+=index-j-1
print(cnt)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
# sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


# show_flg = True
show_flg = False

def f(x):
    return x


# 二分探索 ok ng
def main():
    N = I()
    L = LI()
    # L = L.sort()
    R = sorted(L, reverse=True)
    L = R[::-1]
    # print(R)
    ans = 0

    for i in range(N):
        for j in range(i+1, N):
            a = R[i]
            b = R[j]
            remain = a - b + 1
            idx = bisect.bisect_left(L, remain)
            c_idx = N - (idx + 1)
            if j < c_idx:
                c = (c_idx - j)
                ans += c
                # print('Need', remain, 'c', c)
                # print('a', a)
                # print('b', b)
                # print(i, j, c_idx - j)
                # print('a', a, 'b', b, c_idx, L[:idx])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
c = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        zum = L[i]+L[j]
        start = j+1
        goal = N-1
        now = (start + goal)//2

        if zum > L[goal]:
            c += goal - j
        elif zum <= L[start]:
            c += 0
        else:
            while not L[now] < zum <= L[now+1]:
                now = (start + goal)//2
                if zum > L[now]:
                    start = now
                else:
                    goal = now
            c += now - j

print(c)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 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:
            count += c - b - 1
        else:
            break
print(count)

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-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
import bisect
n = int(input())
ls = list(map(int,input().split()))
ls.sort()
 
ans = 0
for a in range(n-2):
    for b in range(a + 1, n-1):
        c = bisect.bisect_left(ls, ls[a] + ls[b])
 
        ans += c - 1 - b
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import bisect
        
N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
L.sort()

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
L = sorted(map(int,input().split()))
ans = 0
# a<b<cとしてa,bを決めると c < b+aだけ確かめれば良い!
for i in range(n-1):
    for j in range(i+1,n):
        # bisectで初めてa+b以上になる元のindexを見つける
        # j番以降の元であればc>bを満たす.
        ans += bisect.bisect_left(L, L[i]+L[j]) - j - 1
print(ans)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

a.sort()

L = [0] * 2001

co = 0

ans = 0

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

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            #k以下なら良い
            k =  Ls[i] + Ls[j] -1
            # bisect_right(Ls, k)個の要素が左にある
            # 答え: bisect_right(Ls, k) - j
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            # print(Ls, Ls[i],Ls[j],k, bisect_right(Ls,k)-j-1)
            #012
    print(cnt)
            

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            #k以下なら良い
            k =  Ls[i] + Ls[j] -1
            # bisect_right(Ls, k)個の要素が左にある
            # 答え: bisect_right(Ls, k) - j
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            # print(Ls, Ls[i],Ls[j],k, bisect_right(Ls,k)-j-1)
            #012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
    print(cnt)
            

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            #k以下なら良い
            k =  Ls[i] + Ls[j] -1
            # bisect_right(Ls, k)個の要素が左にある
            # 答え: bisect_right(Ls, k) - j
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            # print(Ls, Ls[i],Ls[j],k, bisect_right(Ls,k)-j-1)
    print(cnt)
            

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
L = [0] * 2001
ans = 0
for i in range(N-2):
    for j in range(i+1):
        L[A[i+1] + A[j]] += 1
    ans += sum(L[A[i+2]+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):
    index = bisect_left(L, L[i]+L[j])
    ans += max(index-j-1, 0)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi

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

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

    print(ans)

main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    l.sort()
    c = 0

    import bisect

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

l.sort()
c = 0

import bisect

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

Submission status: Accepted

Solution code:
from numpy import cumsum
n = int(input())
a = list(map(int,input().split()))
l = [0]*1001
for i in range(n):
    l[a[i]] += 1
cs = cumsum(l)
count = 0
for i in range(1,1001):
    if l[i] != 0:
        for j in range(i+1,1001):
            if l[j] != 0:
                count += l[i]*l[j]*(cs[min(i+j-1,1000)]-cs[j])
for i in range(1,1001):
    n = l[i]
    if n > 1:
        count += n*(n-1)//2*(cs[min(2*i-1,1000)]-n)
    if n > 2:
        count += n*(n-1)*(n-2)//6
print(count)

Submission status: Accepted

Solution code:
import bisect

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

ans = 0

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

print(ans)


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

# from itertools import combinations
# import numpy as np
#
# n = int(input())
# hoge = list(map(int, input().split()))
# hoge.sort()
#
# # hoge = np.sort(l)
# ll = []
# ans = 0
#
# def triangle(target, v1, v2):
#     return target < v1 + v2
#
# for i in range(len(hoge)):
#     for j in range(i + 1, len(hoge)):
#         left = hoge[j]
#         right = hoge[-1] + 1
#         while left + 1 < right:
#             mid = (left + right) / 2
#             if triangle(mid, hoge[i], hoge[j]):
#                 ans += len(hoge[hoge >= mid])
#                 break
#             else:
#                 left = mid
#
# print(ans)

"""3回目
# 事前の各要素xより大きい要素個数を求める
for v in hoge:
    ll.append([v, len(hoge[v <= hoge])])

# 事前にコンビネーションを確保する。ループはしない！
combi = np.array([[sum(v), v[1]] for v in combinations(hoge, 2)])

print(combi)
print(combi[:, 0])
print(ll)

# 要素のループをもう一度回し、各
for v, cnt in ll:
    print(v, cnt, len(combi[(v < combi[:, 0]) & (combi[] < v)]), sep=',')
    ans += cnt * len(combi[v < combi])

print(ans)

"""


"""2回目 comnbinationループがだめ
for v in combinations(hoge, 2):
    s = sum(v)
    ans += len(hoge[(v[1] < hoge) & (hoge < s)])

"""



"""1
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3): # combination を使ってループした時点ででn^2
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[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)

# from itertools import combinations
# import numpy as np
#
# n = int(input())
# hoge = list(map(int, input().split()))
# hoge.sort()
#
# # hoge = np.sort(l)
# ll = []
# ans = 0
#
# def triangle(target, v1, v2):
#     return target < v1 + v2
#
# for i in range(len(hoge)):
#     for j in range(i + 1, len(hoge)):
#         left = hoge[j]
#         right = hoge[-1] + 1
#         while left + 1 < right:
#             mid = (left + right) / 2
#             if triangle(mid, hoge[i], hoge[j]):
#                 ans += len(hoge[hoge >= mid])
#                 break
#             else:
#                 left = mid
#
# print(ans)

"""3回目
# 事前の各要素xより大きい要素個数を求める
for v in hoge:
    ll.append([v, len(hoge[v <= hoge])])

# 事前にコンビネーションを確保する。ループはしない！
combi = np.array([[sum(v), v[1]] for v in combinations(hoge, 2)])

print(combi)
print(combi[:, 0])
print(ll)

# 要素のループをもう一度回し、各
for v, cnt in ll:
    print(v, cnt, len(combi[(v < combi[:, 0]) & (combi[] < v)]), sep=',')
    ans += cnt * len(combi[v < combi])

print(ans)

"""


"""2回目 comnbinationループがだめ
for v in combinations(hoge, 2):
    s = sum(v)
    ans += len(hoge[(v[1] < hoge) & (hoge < s)])

"""



"""1
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3): # combination を使ってループした時点ででn^2
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)
"""