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: Wrong Answer

Solution code:
def bin_search(list,value,low,high):
    middle = int((low + high) / 2)
    if low > high :
        return 'nothing'
    else:
        if list[middle] == value:
            return middle
        elif list[middle] > value:
            return bin_search(list,value,low,middle - 1)
        elif list[middle] < value:
            return bin_search(list,value,middle + 1,high)


n = int(input())
list = [int(x) for x in input().split()]
list.sort()
print(list)
count = 0
for i in range(n - 1):
    for j in range(i,n-1):
        a = list[n - i - 1]
        b = list[n - j - 2]
        c = a - b + 1
        while b > c :
         if bin_search(list,c,0,len(list)-1) != 'nothing':
             count += 1
         c += 1
print(count)

Submission status: Wrong Answer

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

for i in range(N-2):
    for j in range(i+1,N-1):
        l1 = L[i]
        l2 = L[j]
        MAX_d = j+1
        MIN_d = N-1
        if MAX_d == MIN_d:
            if l1 < l2 + L[MIN_d]:
                ans += 1
            break
        if MIN_d-MAX_d == 1:
            if l1 < l2 + L[MIN_d]:
                ans += 1
            elif l1 < l2 + L[MAX_d]:
                ans += 1
            break
        while True:
            MID_d = (MAX_d+MIN_d)//2
            MID = L[MID_d]
            if l1 < l2 + MID:
                MAX_d = MID_d
            else:
                MIN_d = MID_d
            if MIN_d-MAX_d == 1:
                if l1 < l2 + L[MIN_d]:
                    ans += MIN_d-j
                else:
                    ans += MAX_d-j
                break
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
count=0
for i in range(N-1):
    for j in range(i+1,N):
        right=len(L)
        left=j
        while right-left>1:
            mid=(right+left)//2
            if L[i]+L[j]>L[mid]:
                left=mid
            else:
                right=mid
        print(L[i],L[j],L[left])
        count=count+left-j
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
count=0
print(L)
for i in range(N-1):
    for j in range(i+1,N):
        right=len(L)
        left=j
        while right-left>1:
            mid=(right+left)//2
            if L[i]+L[j]>L[mid]:
                left=mid
            else:
                right=mid
        print(L[i],L[j],L[left])
        count=count+left-j
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
count=0
a=len(L)
#print(L)
for i in range(a-2):
  count1=0
  for j in range(i+1,a-1):
    for k in range(j+1,a):
      if L[i]<L[j]+L[k]:
        count+=1
        #print(L[i],L[j],L[k])
      else:
        count1+=1
        break
    if count1==1:
         break
print(count)

Submission status: Wrong Answer

Solution code:
import bisect

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

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 + 1 - j

print(count)

Submission status: Wrong Answer

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

L = sorted(L)[::-1] # descending order

def binary_search(descend_list, threshold):
    # 閾値を超えている要素の数を返す
    length = len(descend_list)
    
    # 最小の数が閾値を超えればリストの長さを返す
    if descend_list[-1] > threshold:
        return length
    
    # 最大の数が閾値を超えなければ0を返す
    if descend_list[0] < threshold:
        return 0
    
    # 二分探索する 少なくとも一つはthresholdを超える要素があり、少なくとも一つはthresholdを超えない要素がある場合
    upper_bound = length
    lower_bound = 0
    index = (upper_bound - lower_bound) // 2
    
    while upper_bound > lower_bound + 1:
        if descend_list[index] > threshold:
            lower_bound = index
        else:
            upper_bound = index
        
        index = (upper_bound + lower_bound) // 2
    
    return lower_bound + 1

i = 0
ret = 0

while i < N - 2:
    edge1 = L[i]
    for j in range(i + 1, N-1):
        edge2 = L[j]
        ret += binary_search(L[j+1:], edge1 - edge2)
    
    i += 1

print(ret)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
count=0
a=len(L)
#print(L)
for i in range(a-2):
  count1=0
  for j in range(i+1,a-1):
    for k in range(j+1,a):
      if L[i]<L[j]+L[k]:
        count+=1
        #print(L[i],L[j],L[k])
      else:
        count1+=1
        break
    if count1==1:
         break
print(count)

Submission status: Wrong Answer

Solution code:
import bisect
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):
    c += bisect.bisect_right(l,l[j]+l[i]-1) - bisect.bisect_left(l,l[j])-1
print(c)

Submission status: Wrong Answer

Solution code:
import bisect
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):
    c += bisect.bisect_right(l,l[j]+l[i]-1) - bisect.bisect_right(l,l[j])
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int, input().split()))
a=0
b=0
c=0
count =0
#作れる三角形の数
ans =0
#降順に並び替える
L.reverse()
#最大の長さを持つ棒をaとする


while(len(L) !=2):
    a = L.pop(0)
    for i in range(0,len(L)-1):
        b = L[i]
        for j in range(i+1,len(L)):
            c = L[j]
            if(b+c > a):
                ans +=1

print(ans)

Submission status: Wrong Answer

Solution code:
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):
        gap = l[i] + l[j]
        index = bisect.bisect_right(l, gap)

        ans += max(0, index-j-1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0

l.sort()
print(l)

for i in range(n):
    for j in range(i+1, n):
        gap = l[i] + l[j]
        index = bisect.bisect_right(l, gap)

        ans += max(0, index-j-1)

print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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):
        c = bisect.bisect_left(L,a+b)
        ans += c-b-1
            
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

L.sort()

count = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        L_copy = L[:]
        a = L_copy.pop(i)
        b = L_copy.pop(j-1)
        print(L_copy)
        start = j-1
        finish = N-3
        if start != 0:
            while L_copy[start] == L_copy[start-1]:
                start -= 1
                if start == 0:
                    break
        start_first = start
        finish_first = finish
        while start < finish:
            n = finish - start + 1
            if L_copy[start + (n//2)] < a + b:
                start += (n//2)
            else:
                finish = start + (n//2) - 1
        if L_copy[start_first] < a + b:
            count += (finish - start_first + 1)
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
import bisect
count=0
for i in range(n-1):
  for j in range(1,n):
    if i==j:
      pass
    else:
      left=bisect.bisect_right(l,abs(l[i]-l[j]))
      right=bisect.bisect_left(l,l[i]+l[j])
      count+=right-left
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
import bisect
count=0
for i in range(n-1):
  for j in range(1,n):
    if i==j:
      pass
    else:
      left=bisect.bisect_left(l,abs(l[i]-l[j]))
      right=bisect.bisect_right(l,l[i]+l[j])
      count+=right-left
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = S.split(" ")
l = []
for i in L:
    l.append(int(i))
l.sort()
count = 0
for a in range(N-2):
    for b in range(a+1,N-1):
        for c in range(b+1,N):
            if l[a]+l[b]<l[c]:
                break
            count += 1
print(count)

Submission status: Wrong Answer

Solution code:
import bisect

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

L.sort()

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

Submission status: Wrong Answer

Solution code:
n = int(input())
l0 = [int(i) for i in input().split()]
l = sorted(l0)
ans = 0

for i in range(2,n):
    for j in range(1,i-1):
        if l[i] == l[j]:
            break
        for k in range(j-1):
            if l[i] < l[j] + l[k]:
                ans += 1
            else:
                break


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l0 = [int(i) for i in input().split()]
l = sorted(l0)
ans = 0

for i in range(n):
    for j in range(i-1):
        for k in range(j-1):
            if l[i] < l[j] + l[k]:
                ans += 1
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
p,q,r=0,0,0
p=len(L)*(len(L)-1)*(len(L)-2)//6
L.sort()
z=N%3
for i in range(N-z-1):
  for j in range(i+1,N-z):
    for k in range(i+2,N):
      if L[i]+L[j]<=L[k]:
        q=q+1
  if L[i]+L[j]>1000:
    break
print(p-q)

Submission status: Wrong Answer

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

def main():
    N = int(input())
    l = sorted(list(map(int, input().split())))
    memo = [-1] * (l[-2] + l[-1] + 1)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            a = l[i]
            b = l[j]
            if memo[b - a] == -1:
                left = bisect.bisect_right(l, b - a)
                memo[b - a] = left
            else:
                left = memo[b - a]
            if memo[a + b] == -1:
                right = bisect.bisect_left(l, a + b)
                memo[a + b] = right
            else:
                right = memo[a + b]
            ans += max(0, right - max(left, j+1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    N = int(input())
    l = sorted(list(map(int, input().split())))
    memo = [-1] * (l[-2] + l[-1] + 1)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            a = l[i]
            b = l[j]
            if memo[b-a] == -1:
                left = bisect.bisect_right(l, b - a)
                memo[b-a] = left
            else:
                left = memo[b-a]
            if memo[a+b] == -1:
                right = bisect.bisect_left(l, a + b)
                memo[a+b] = right
            else:
                right = memo[a+b]
            ans += max(0, right - 1 - max(left-1, j))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-18 23:58:23(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, *l = [int(x) for x in sys.stdin.read().split()]
    l.sort()

    c = collections.defaultdict(int)
    for i in range(n):
        c[l[i]] += 1
    
    # cumsum: count of {l[j]|l[j] <= l[i]}
    s = [0 for _ in range(l[-3] + l[-2] + 1)] # a+bの最大値以下まで
    for i in range(l[-3] + l[-2]):
        s[i+1] = s[i] + c[i+1]

    count = 0
    # a <= b <= c
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            count += s[a+b-1] - s[b]

    print(count)




    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def binary_search(list, item):

    if len(list) == 0 or item > list[-1]:
        return -1

    list2 = [1001] + list + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2
        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:
            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1

    return -1

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

L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)

        result += k + 1


print(result)

Submission status: Wrong Answer

Solution code:

def binary_search(list, item):

    if len(list) == 0 or item > list[-1]:
        return -1

    list2 = [1001] + list + [0]

    low = 0
    high = len(list2) - 1

    while low <= high:
        mid = (low + high) // 2
        bigger = list2[mid]
        smaller = list2[mid+1]

        if smaller <= item and item < bigger:
            return mid - 1

        if smaller > item:
            low = mid + 1

        else:
            high = mid - 1


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

L = sorted(L, reverse=True)


result = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1::]):
        k = binary_search(L[i+j+2::], a-b)

        result += k + 1


print(result)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
n = int(input())
l = [int(i) for i in input().split()]
cnt = 0
for i in combinations(set(l), 3):
  a, b, c = i
  if a < (b + c) and b < (c + a) and c < (a + b):
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
#print(L)
t_num=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if L[i]==L[j]:
            t_num=t_num+(N-1-j)
            #print('Eq:'+str(N-1-j))
            break
        else:
            rst=L[i]-L[j]
            #print('rst:'+str(rst))
            for k in range(j+1,N):
                if L[k]<=rst:
                    break
                else:
                    #print(str(L[i])+'-'+str(L[j])+'-'+str(L[k]))
                    t_num=t_num+1
print(t_num)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
t_num=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if L[i]==L[j]:
            t_num=t_num+(N-1-j)
            break
        else:
            rst=L[i]-L[j]
            for k in range(j+1,N):
                if L[k]<=rst:
                    break
                else:
                    t_num=t_num+1
print(t_num)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L.reverse()
print(L)
t_num=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if L[i]>=L[j]+L[k]:
                break
            else:
                t_num=t_num+1
                
print(t_num)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=list(map(int,input().split()))
lst.sort()
count=0
for i in range(n-2):
    for j in range(i+2,n):                             
        for k in range (i+1,j):
            a=lst[i]
            c=lst[j]
            b=lst[k]
            if b>a-c:
                count+=1
                
            elif b<=a-c:
                break
        
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=list(map(int,input().split()))
lst.sort()
count=0
for i in range(n-2):
    for j in range(i+2,n):                             
        for k in range (i+1,j):
            a=lst[i]
            c=lst[j]
            b=lst[k]
            if b>a-c:
                    count+=1
            elif b<=a-c:
                break
print(count)

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

N = 7
L = [218, 786, 704, 233, 645, 728, 389]

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))
# print(cb)

rslt = 0
m = 100

if N-2 < m:
    m = N

for i in cb:
    set_L = L.copy()
    set_L.remove(i[0])
    set_L.remove(i[1])
    split_list = [set_L[i:i+m] for i in range(0, len(set_L), m)]

    for j in split_list:
        if i[0]-i[1]<j[0] and i[1]>=j[0]:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                # 上界fit, 下界fit
                rslt = rslt + len(j)
            else:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
        else:
            if i[0]-i[1]<j[-1] and i[1]>=j[-1]:
                for k in j:
                    if i[0]-i[1]<k and i[1]>=k:
                        rslt = rslt + 1
                        #print(i, k)
            else:
                # 上界fitしない, 下界しない
                None

print(rslt)

Submission status: Wrong Answer

Solution code:
n = 7
l = sorted(list(map(int, "218 786 704 233 645 728 389".split())))
res = 0
for i in range(len(l)-2):
    for j in range(i+1, len(l)-1):
        #res += len([x for x in l[j+1:] if x < l[i]+l[j]])
        lst = l[j+1:]
        while lst:
            low, high = 0, len(lst)-1
            mid = (low + high)//2
            if lst[mid] < l[i]+l[j]:
                res += mid+1
                lst = lst[mid+1:]
            else:
                lst = lst[:mid]
print(res)

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

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

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))

rslt = 0

for el_cb in cb:
    for v in L:
        if v not in el_cb:
            if el_cb[0] - el_cb[1] < v and el_cb[1] >= v:
                print(el_cb, v)
                rslt = rslt + 1

# if のところに2分探索入れる

print(rslt)

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

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

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))

rslt = 0

for el_cb in cb:
    for v in L:
        if el_cb[0] - el_cb[1] < v and el_cb[1] >= v:
            rslt = rslt + 1

# if のところに2分探索入れる

print(rslt)

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

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

# N = 7
# L = [218, 786, 704, 233, 645, 728, 389]

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(lambda x: sorted(x, reverse=True), cb))

rslt = 0

for el_cb in cb:
    for v in L:
        if el_cb[0] - el_cb[1] <= v and el_cb[1] >= v:
            rslt = rslt + 1

# if のところに2分探索入れる

print(rslt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
num=list(map(int, input().split()))
c=0
for i in range(0,N-3,1): 
    for j in range(i+1,N-2,1):
        for k in range(i+2,N-1,1):
            if num[i]<num[j]+num[k] and num[j]<num[i]+num[k] and num[k]<num[j]+num[i]:
                c+=1
print (c)

Submission status: Wrong Answer

Solution code:
N=int(input())
num=list(map(int, input().split()))
c=0
for i in range(0,N-3,1): 
    for j in range(i+1,N-2,1):
        for k in range(i+2,N-1,1):
            if num[i]<num[j]+num[k] and num[j]<num[i]+num[k] and num[k]<num[j]+num[i]:
                c+=1
            else:
                c=c
print (c)

Submission status: Wrong Answer

Solution code:
import bisect
import numpy as np

N = int(input())
L = np.array(list(map(int, input().split())))
L = np.sort(L)
n = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        b = L[j]
        C = L[j+1:]
        n += bisect.bisect_left(C, a+b-1)
        #print(a,b,r,n)
        
print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

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

# N = 7
# L = [218, 786, 704, 233, 645, 728, 389]

L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(sorted, cb))

rslt = 0

for el_cb in cb:
    for v in L:
        if el_cb[1] - el_cb[0] <= v and el_cb[0] >= v:
            rslt = rslt + 1

# if のところに2分探索入れる

print(rslt)

Submission status: Wrong Answer

Solution code:
### D - Triangles ###

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

# N = 7
# L = [218, 786, 704, 233, 645, 728, 389]
L.sort()

from itertools import combinations

cb = combinations(L, 2)
cb = list(map(sorted, cb))

rslt = 0

for el_cb in cb:
    for v in L:
        if el_cb[1] - el_cb[0] <= v and el_cb[0] > v:
            rslt = rslt + 1

# if のところに2分探索入れると速いよ

print(rslt)

Submission status: Wrong Answer

Solution code:
from math import ceil, floor, factorial
n = int(input())
ls = map(int, input().split())
ls = sorted(ls)


counter = 0


def pred(l1, l2, l3):
    return l1 < l2 + l3 and l2 < l1 + l3 and l3 < l1 + l2


def binary_search(arr, l, r, x):
    cut = (l+r) // 2

    if arr[r] < x:
        return r
    if arr[l] > x:
        return l

    if arr[cut] > x and arr[cut-1] < x:
        x = arr[cut]
    if arr[cut] < x and arr[cut+1] > x:
        x = arr[cut+1]
        cut += 1

    if arr[cut] > x:
        return binary_search(arr, cut, r, x)
    elif arr[cut] < x:
        return binary_search(arr, l, cut, x)

    while arr[cut+1] == x:
        cut += 1
    while arr[cut-1] == x:
        cut -= 1

    return cut


for i in range(1, n-1):
    l1 = ls[n-i]
    for j in range(i+1, n):
        l2 = ls[n-j]
        cut = (n-j)//2

        upper_bound = n-j
        lower_bound = 0
        looking_for = l1 - l2

        cut = binary_search(ls, lower_bound, upper_bound, looking_for)

        count_l3s = (n-j) - cut
        counter += count_l3s

print(counter)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
L = input().split()
cnt = 0

for i in range(N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[i] + L[j]:
        cnt += 1
        
print(cnt)

Submission status: Wrong Answer

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

cnt = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if l[i]+l[j]>l[k]:
        ans+=1
        
    
print(ans)

Submission status: Wrong Answer

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

a = [n-1]
for i in range(n-1):
    if l[i] == l[i+1]:
        continue
    else:
        a.append(i)
        

check_j = 0
count = 0 

for i in a:
   
    for j in reversed(range(1,i)):
        
        if l[j] == check_j:
            check_j = l[j]
            continue
        
       
        
        if l[i] - l[j] >= l[j-1]:
            continue
        
        low = 0
        high = j-1
        
        while low < high:
            
            mid = (low + high)//2
            
            if l[i] - l[j] >= l[mid]:
                
                low = mid +1
            
            else:    
                
                high = mid
        else:
           
            count += len(set(l[low:j]))
print(count)

Submission status: Wrong Answer

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

check_i = 0
check_j = 0
count = 0 

for i in reversed(range(0,n)):
    for j in reversed(range(1,i)):
        
        if l[i] == check_i and l[j] == check_j:

            break
        else:
            check_i = l[i]
            check_j = l[j]
        
        if l[i] - l[j] >= l[j-1]:
            break
        
        low = 0
        high = j-1
        
        while low < high:
            
            mid = (low + high)//2
            
            if l[i] - l[j] >= l[mid]:
                
                low = mid +1
            
            else:    
                
                high = mid
        else:
            count += len(set(l[low:j]))
print(count)

Submission status: Wrong Answer

Solution code:
import bisect

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

# shorter first
# c > b >a
count = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    index = bisect.bisect_left(L, L[i]+L[j])
    count += max(0, index-(j+1))

Submission status: Wrong Answer

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

for i in range(n):
  for j in range(i,n):
    for k in range(j,n):
      if l[i]+l[j]>l[k] and l[j]+l[k]<l[i] and l[k]+l[i]<l[j]:
        count+=1

Submission status: Wrong Answer

Solution code:
n = int(input())
D = list(map(int,input().split()))
D.sort(reverse=True)
c= 0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if D[i] >= D[j]+D[k]:
                c +=j-k-1
                break
print(c)

Submission status: Wrong Answer

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

n = int(input())
l = sorted(list(map(int, input().split(' '))), reverse = True)
print(l)

ans = 0
for ai in range(len(l)-2):
    a = l[ai]
    for bj in range(ai+1, len(l)-1):
        b = l[bj]
        for ck in range(bj+1, len(l)):
            c = l[ck]
            if a < b+c:
                ans += 1
            else:
                break

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力 「N」だけの入力のとき
l=sorted(list(map(int, input().split())), reverse= True)
count = 0
for i in range(0, n-2):
   for j in range(i+1, n-1):
       for k in range(j+1, n):
          if l[i] < l[j] + l[k]:
              count = count + (n-k)
              break
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort()
print(L)
count = 0

for i, l1 in enumerate(L):
    for k, l2 in enumerate(L[i+1:]):
        num = i + k + 2
        for l3 in L[num:]:
            if l1 < l2 + l3 and l2 < l1 + l3 and l3 < l2 + l1:
                count += 1
            else:
                break

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
d=[]
def kinds(a,b,c):
  if a<b+c and b<a+c:
    if set([a,b,c]) not in d:
      d.append(set([a,b,c]))
for j in range(N):
  i=0
  while (N-1-j)-i+1>=3:
    if L[N-1-j]<L[i]+L[i+1]:
      kinds(L[i],L[i+1],L[N-1-j])
    i+=1
print(len(d))

Submission status: Wrong Answer

Solution code:
import itertools

N = input()
l = list(map(int, input().split()))
count = 0

c = list(itertools.combinations(l,3))
for i,j,k in c:
    if i < j + k and j < i + k and k < i + j:
        count += 1

Submission status: Wrong Answer

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

ans = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if xs[i] + xs[j] < xs[k]:
        break
      ans += 1
print(ans)