Problem: 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.

Submission attempts (in order):
N = int(input())
L = [int(i) for i in input().split()]

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j
        right = L_len
        center = int((left+right)/2)

        while right - left <= 1:
            if L[i] < L[j] + L[center]:
                left = center + 1
            else:
                right = center - 1
            
        count += right - center
print(count)

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

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j + 1
        right = L_len - 1
        length = L[i] - L[j]

        while right  >= left:
            center = int((left + right)/2)
            if L[center] > length:
                left = center + 1
            else:
                right = center - 1
                

            count += center - j

print(count)

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

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j + 1
        right = L_len - 1
        length = L[i] - L[j]
        flag = 0

        while right - left >= 1:
            center = int((left + right)/2)
            if L[center] >= length:
                left = center + 1
                flag = 1
            else:
                right = center - 1
        
            if flag == 1:
                if L[left] <= length:
                    index = left - 1
                else:
                    index = left
                
                print(i, j)
                count += index - j

print(count)

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

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j + 1
        right = L_len - 1
        length = L[i] - L[j]

        while right - left >= 1:
            center = int((left + right)/2)
            if L[center] >= length:
                left = center + 1
            else:
                right = center - 1
            
        if L[left] < length:
            index = left - 1
        else:
            index = left
        
        count += index - j

print(count)

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

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j
        right = L_len
        center = int((left+right)/2)

        while right - left <= 1:
            if L[i] < L[j] + L[center]:
                left = center + 1
            else:
                right = center - 1
            
        count += right - center
print(count)

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

L_len = len(L)

count = 0
for i in range(L_len - 2):
    for j in range(i + 1, L_len - 1):
        for k in range(j + 1, L_len):
            f = 0
            if a >= b + c:
                f = 1
            if b >= a + c:
                f = 1
            if c >= a + b:
                f = 1
            
            if f == 0:
                count += 1

print(count)

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

from bisect import bisect_left

L.sort()

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

print(count)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted