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:
N=int(input())
S=list(map(int,input().split( )))
print(S)
tri=[]
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            a = [S[i],S[j],S[k]]
            a = sorted(a)
            if a[0]+a[1] > a[2]:
                tri.append(a)

for i in range(len(tri)):
    n=0
    for j in range(i+1,len(tri)):
        if tri[i] == tri[j-n]:
            tri.pop(j)
            n=n+1


print(len(tri))

Submission status: Wrong Answer

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

L.sort()

s = 0
#print(L)
for j in range(N-2):
    for k in range(j+1,N-1):
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = L[j]+L[k]
        l = k + 1
        r = N - 1
        while r - l > 1:
            m = (r + l) // 2
            if L[m] < key:
                l = m
            else:
                r = m
        #print(key,r,l,k,N - l - 1)
        s += N - l - 1

print(s)

Submission status: Wrong Answer

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

L.sort()

s = 0
print(L)
for j in range(N-2):
    for k in range(j+1,N-1):
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = L[j]+L[k]
        l = k + 1
        r = N - 1
        while r - l > 1:
            m = (r + l) // 2
            if L[m] < key:
                l = m
            else:
                r = m
        #print(key,r,l,k,N - l - 1)
        s += N - l - 1

print(s)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
print(S)
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        a += bisect.bisect_left(S,b) - (j+1)
print(a)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
print(S)
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        print(i,j,bisect.bisect_left(S,b) )
        a += bisect.bisect_left(S,b) - (j+1)
print(a)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        a += bisect.bisect_right(S,b) - (j+1)
print(a)

Submission status: Wrong Answer

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

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

count = 0
L.sort()
for i in range(N-2):
    for j in range(i+1,N-1):
        k = bisect.bisect_left(L,L[i]+L[j])
        l = bisect.bisect_right(L,L[j])
        count += k - l 
print(count)

Submission status: Wrong Answer

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

lengthlist = [0]
for i in range(0, 1000):
	lengthlist.append(0)
for i in range(0, N):
	lengthlist[L[i]] += 1
cnt = 0
for i in range(1, 1001):
	a = lengthlist[i]
	if a >= 1:
		for j in range(i, 1001):
			b = lengthlist[j]
			if i == j:
				b -= 1
			if b >= 1:
				for k in range(j, 1001):
					if i >= j+k or j >= k+i or k >= i+j:
						break
					c = lengthlist[k]
					if j == k:
						if i == j:
							c -= 2
						else:
							c -= 1
					if c >= 1:
						cnt += a*b*c
print(cnt)

Submission status: Wrong Answer

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

sma=L[-1]+L[-2]

a=0
b=1
c=2

ans=0

for i in range(n-2):
    for q in range(i+1,n-1):
        for j in range(q+1,n):
            if L[i]+L[q]>L[j]:
                ans+=1
                print(i,q,j)
            else:
                break
    
print(ans)

Submission status: Wrong Answer

Solution code:
num = int(input())
pole_list = sorted([int(item) for item in input().split()])

import numpy as np
pole_list = np.array(pole_list)

cnt = 0

for a in pole_list:
    for b in pole_list:
        if a < b:
            temp_list = pole_list[pole_list > b]
            res_list = temp_list[temp_list < a + b]
            cnt += len(res_list)

print(cnt)

Submission status: Wrong Answer

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

start = time.time()
res = 0
for i in range(n):
    for j in range(i+1,n):
        ab = l[i] + l[j]
        c_index = bisect_left(l, ab)
        res += c_index - j - 1
print(res)
end = time.time()
print(end-start)

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

import time
start = time.time()

def binary_search_nearest(list, item):#もっとも近い値のindexをbsで探して返す関数
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return low-1

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

sum = 0
for i in range(N):
    for j in range(i+1,N):
        min_index = j
        max_index = binary_search_nearest(Lmat, Lmat[i]+Lmat[j])#これだとjustの値しか取得できない=>bs自体を実装する必要がある
        sum+= binary_search_nearest(Lmat, Lmat[i]+Lmat[j]) - j - 1
        """
        #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
        for Lk_index in range(min_index+1,max_index+1):
            sum += iftriangle(i,j,Lk_index)#ここは1に置き換え不可。計算が必要
        """

print(sum)

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Wrong Answer

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

l.sort(reverse = 1)
ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            a,b,c = l[i],l[j],l[k]
            #print(i,j,k)
            if a >= b+c:
                break
            else:
                ans += 1

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

import time
start = time.time()

def binary_search_nearest(list, item):#もっとも近い値のindexをbsで探して返す関数
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return low-1

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

N2patern = list(itertools.combinations(range(N), 2)) #大小関係はこの時点で決まってしまう(小、大)のペアが出てくる

sum = 0
for patern in N2patern:
    sum+=binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]])-patern[1]
    """
    #print("hoge")
    min_index = patern[1]
    max_index = binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]])#これだとjustの値しか取得できない=>bs自体を実装する必要がある
    #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
    for Lk_index in range(min_index+1,max_index+1):
        sum += iftriangle(patern[0],patern[1],Lk_index)#ここは1に置き換え不可。計算が必要
    """

print(sum)

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Wrong Answer

Solution code:
from bisect import bisect

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

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += max(0, bisect(l[j+1:], l[i]+l[j]) - bisect(l[j+1:], abs(l[i]-l[j])))

print(c)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = sorted(map(int, input().split()))
imos = [0] * (3 * 10**3)
for i in range(n):
    imos[a[i]] += 1
for i in range(len(imos) - 1):
    imos[i + 1] += imos[i]
ans = 0
for l in range(n - 2):
    for m in range(l + 1, n - 1):
        ans += imos[a[l] + a[m] - 1] - imos[a[m]]
        c = max(0, imos[a[m]] - imos[a[m] - 1] - 1)
        if a[l] == a[m]:
            c = max(0, c - 1)
        ans += c
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect
from heapq import heappop, heappush

import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
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()))

n = INT()
l = sorted(LIST(), reverse = True)
res = 0

for i in range(n-2):
    flag = False
    for j in range(i+1, n-1):
        if flag:
            break
        for k in range(j+1, n):
            if l[i] == l[j] and l[i] == l[k]:
                continue
            if l[i] >= l[j] + l[k]:
                flag = True
                break
            res += 1
    

print(res)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect
from heapq import heappop, heappush

import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
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()))

n = INT()
l = sorted(LIST(), reverse = True)
res = 0

for i in range(n-2):
    flag = False
    for j in range(i+1, n-1):
        if flag:
            break
        for k in range(j+1, n):
            if l[i] >= l[j] + l[k]:
                flag = True
                break
            res += 1
    

print(res)

Submission status: Wrong Answer

Solution code:
# ABC143D - Triangles
def main():
    # fix the longest edge -> binary search
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans, prev, prev_cnt = 0, 0, 0
    for i in range(N - 1, 1, -1):
        x, ng, ok, cnt = L[i], 0, i - 1, 0
        if x == prev:
            ans += prev_cnt
            continue
        while ng < ok:
            if L[ng] + L[ok] > x:
                cnt += ok - ng
                ok -= 1
            else:
                ng += 1
        ans += cnt
        prev, prev_cnt = x, cnt
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# ABC143D - Triangles
def main():
    # fix one of edges -> binary search
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = 0
    for i in range(N - 1, 1, -1):
        x, ok, ng = L[i], 0, i - 1
        while ng < ok:
            if L[ng] + L[ok] > x:
                ans += ok - ng
                ok -= 1
            else:
                ng += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# ABC143D - Triangles
from bisect import bisect_left as bsl, bisect_right as bsr


def main():
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = 0
    for i, x in enumerate(L):
        for y in L[i + 1 :]:
            z = y - x + 1
            leftmost_x, leftmost_z = bsl(L, x), bsl(L, z)
            ans += max(0, leftmost_x - leftmost_z)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-1-j)
            print(edges[i],edges[j],max(0,pos-1-j))
    return ans

input()
print(find(list(map(int,input().strip().split(" ")))))

Submission status: Wrong Answer

Solution code:
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-j-1)
    return ans

input()
find(list(map(int,input().strip().split(" "))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N-2):
    for j in range(N-i-2):
        for k in range(N-j-1):
            if k+j+1<j+i+1+i:
                ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = sorted(list(map(int, input().rstrip('\n').split())))
    cnt = 0
    print(l)
    for i, iv in enumerate(l[:n-2]):
        for j, jv in enumerate(l[i+1:n-1]):
            t = iv + jv
            for k, kv in enumerate(l[i+j+2:n]):
                if t > kv:
                    cnt += 1
                else:
                    break
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        r = L[i] + L[j]
        # 同じ長さでも違う棒はOK
        ri = bisect.bisect_left(L, r, lo=j + 1)
        ans += ri - j - 1

Submission status: Wrong Answer

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
#print(L)
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=0
        u=b-1
        while u-l>1:
            a=(l+u)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
        #print(b,c,b-u)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
print(L)
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=0
        u=b-1
        while u-l>1:
            a=(l+u)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
        print(b,c,b-u)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
import heapq
cnt=0
l=sorted(l)
#print(l)
while len(l)>2:
  a=heapq.heappop(l)
  for i in range(len(l)-1):
    for j in range(i+1, len(l)):
      if a<l[j]-l[i]:
        break
      if l[j]-l[i]<a<l[i]+l[j]:
        cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
#あとで見直しとけ

import copy

def j(n):
    if n:print("yes")
    else:print("no")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printrow(a):
    for i in a:
        print(i)


n = ip()
a = iprow()
a.sort()
print(a)
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        left = j+1
        right = n-1
        short = a[i]+a[j]
        mid = (left + right) // 2
        while left > right:
            mid = (left+right)//2
            if a[mid] > short:
                right = mid-1
            if a[mid] < short:
                left = mid+1
        print(i, j, left, right, "aiaj", a[i] + a[j], a[mid],mid)
        while a[i]+a[j] <= a[mid] and j < mid:
            mid-=1
        while a[i]+a[j] > a[mid] and j >=mid:
            mid-=1
        mid-=1
        if j == mid :mid  = 0
        print(mid,"mid")
        ans+=max(mid-j+1,0)
print(ans)
"""
7
218 786 704 233 645 728 389
23

        while a[i] + a[j] > a[mid] and mid!=n-1:
            print(mid)
            mid += 1
"""

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
def main():
	N = int(input())
	L = list(map(int, input().split(" ")))
	L.sort()
	k = 0
	for i in reversed(range(2, N)):
		a = L[i]
		for j in reversed(range(1, i)):
			b = L[j]
			index = bisect_left(L, a-b, hi=j)
			k += j - index
	print(k)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l) - 1):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        left = bisect.bisect_right(l, b)
        right = bisect.bisect_left(l, ab)
        ans += (right - left)

print(max(0, ans))

Submission status: Wrong Answer

Solution code:
# Atcoder 2019/10/19
"""
# A
A, B = map(int, input().split())

ans = A - 2 * B

if ans >= 0:
    print(ans)
else:
    print(0)
"""

"""
# B
import sys
n = int(input())
d = list(map(int, sys.stdin.readline().strip().split()))

ans = 0

for i in range(n - 1):
    for j in range(i + 1, n):
        ans += d[i] * d[j]

print(ans)
"""

"""
# C
n = int(input())
s = input()

count = 1

for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        count += 1

print(count)
"""
"""
# D
import sys

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

    global big_ans
    big_ans = []

    def angle(l, count, ans, i, n):
        if len(ans) == 3:
            big_ans.append(ans)
            return ans

        if i == n:
            # print('i end')
            return

        if len(ans) + n - i < 3:
            return

        passa = ans.copy()
        angle(l, count, passa, i + 1, n)
        ans.append(l[i])
        passb = ans.copy()
        angle(l, count + 1, passb, i + 1, n)

    angle(l, 0, [], 0, len(l))

    ans_count = 0

    for i in big_ans:
        if sum(i) - 2 * max(i) > 0:
            ans_count += 1

    print(ans_count)


if __name__ == '__main__':
    main()

"""

# ここから復習
# D
import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l) - 1):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        left = bisect.bisect_right(l, b)
        right = bisect.bisect_left(l, ab)
        ans += (right - left)

print(ans)

Submission status: Wrong Answer

Solution code:
# Atcoder 2019/10/19
"""
# A
A, B = map(int, input().split())

ans = A - 2 * B

if ans >= 0:
    print(ans)
else:
    print(0)
"""

"""
# B
import sys
n = int(input())
d = list(map(int, sys.stdin.readline().strip().split()))

ans = 0

for i in range(n - 1):
    for j in range(i + 1, n):
        ans += d[i] * d[j]

print(ans)
"""

"""
# C
n = int(input())
s = input()

count = 1

for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        count += 1

print(count)
"""
"""
# D
import sys

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

    global big_ans
    big_ans = []

    def angle(l, count, ans, i, n):
        if len(ans) == 3:
            big_ans.append(ans)
            return ans

        if i == n:
            # print('i end')
            return

        if len(ans) + n - i < 3:
            return

        passa = ans.copy()
        angle(l, count, passa, i + 1, n)
        ans.append(l[i])
        passb = ans.copy()
        angle(l, count + 1, passb, i + 1, n)

    angle(l, 0, [], 0, len(l))

    ans_count = 0

    for i in big_ans:
        if sum(i) - 2 * max(i) > 0:
            ans_count += 1

    print(ans_count)


if __name__ == '__main__':
    main()

"""

# ここから復讐
# D
import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l) - 1):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        left = bisect.bisect_left(l, b)
        right = bisect.bisect_left(l, ab)
        ans += ((right - left) - 1)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l) - 1):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        left = bisect.bisect_right(l, b)
        right = bisect.bisect_left(l, ab)
        ans += (right - left)

print(ans)

Submission status: Wrong Answer

Solution code:
N=raw_input()
LN=raw_input().split()

n = int(N)
ln = [int(i) for i in LN]

c = []
for i in range(n):
    for j in range(i-1):
        for k in range(j-1):
            c.append([i,j,k])


r = []
for i in c:
    x, y, z = i
    a = ln[x]
    b = ln[y]
    c = ln[z]
    if not a < b + c:
        continue
    if not b < c + a:
        continue
    if not c < a + b:
        continue
    r.append(i)

print len(r)

Submission status: Wrong Answer

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

for i in range(N-2):
    for j in range(i+1,N-1):
        start = j+1
        end = N
        while(start + 1 < end):
            if L[i] + L[j] > L[(start + end)//2]:
                start = (start + end)//2
            else:
                end = (start + end)//2
            print(L[i],L[j],L[start],L[end-1])
        print(i,j,start)
        if L[i] + L[j] > L[start]:
            ans += start - j
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4000000)

n = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)

cnt = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        b = L[j]
        if a >= 2*b:
            break
        for k in range(j+1, n):
            c = L[k]
            if c >= a+b:
                break
            if (a < (b+c)) & (b < (c+a)) & (c < (a+b)):
                cnt += 1
                
print('cnt', cnt)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4000000)

n = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)

cnt = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        b = L[j]
        if a >= 2*b:
            break
        for k in range(j+1, n):
            c = L[k]
            if c > a+b:
                break
            if (a < (b+c)) & (b < (c+a)) & (c < (a+b)):
                cnt += 1
                
print('cnt', cnt)

Submission status: Wrong Answer

Solution code:
def lowerBound(nums, target):
    if nums[-1] < target:
        return -1
    lp = 0
    rp = len(nums) - 1
    mp = lp + rp >> 1
    while lp <= rp:
        mp = lp + rp >> 1
        if nums[mp] == target:
            return mp
        if nums[mp] < target:
            lp = mp + 1
        else:
            rp = mp - 1
    return mp if target < nums[mp] else -1

if __name__ == '__main__':
    N = int(input())
    L = [int(l) for l in input().split()]
    L.sort()
    ret = 0
    for idx in range(len(L) - 1, 1, -1):
        for jdx in range(idx - 1, 0, -1):
            c = L[idx] - L[jdx] + 1
            ldx = lowerBound(L[:jdx], c)
            if ldx == -1: continue
            ret += jdx - ldx
    print(ret)

Submission status: Wrong Answer

Solution code:
n=int(input()) #7
e=list(map(int,input().split())) #218 786 704 233 645 728 389 の時
d=sorted(e,reverse=True)
c=0
for i in range(n-2): 
    for j in range(i+1,n-1): 
        for l in range(j+1,n): 
            if (d[l]<d[i]+d[j]):
                c=c+1 
print(c)

Submission status: Wrong Answer

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

def find(p, ls):
  for i in range(len(ls)):
    if p < ls[i]:
      return i
      break
    
    
for i in range(n-1):
  for j in range(i+1, n):
    point = H[i] + H[j]
    if point >= H[n-1]:
      break
    else:
      s = find(point, H)
      count += n - s
print(count)

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

def binary_search_nearest(list, item):#もっとも近い値のindexをbsで探して返す関数
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return low-1

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

N2patern = list(itertools.combinations(range(N), 2)) #大小関係はこの時点で決まってしまう(小、大)のペアが出てくる

sum = 0
for patern in N2patern:
    #print("hoge")
    min_index = patern[1]
    max_index = binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]])#これだとjustの値しか取得できない=>bs自体を実装する必要がある
    #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
    for Lk_index in range(min_index+1,max_index+1):
        #print(iftriangle(patern[0],patern[1],Lk_index))
        sum += 1#iftriangle(patern[0],patern[1],Lk_index)

print(sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
L_list=[int(thing) for thing in input().split()]
L_list.sort(reverse=True)

ret=0
s_list=[]
def my_bisect(l:list,f):
    def bis(begin:int,end:int):
        # print("{}:{}".format(begin,end))
        if end-begin<=1:
            if f(l[end]) ==True:
                return end
            elif f(l[begin])==True:
                return begin
            else:
                return -1
        v=int((end+begin)/2//1)
        res=f(l[v])
        if res:
            return bis(v,end)
        else:
            return bis(begin,v)

    return bis(0,len(l)-1)
for i in range(N):
    n=L_list[i]
    diff=0
    for j in range(i+1,N-1):
        k=L_list[j]
        m=n-k
        l=L_list[j+1:N]
        s=my_bisect(l,lambda x:x>m)+1
        s_list.append(s)
        if s==0:
            break
        else:diff=diff+s
    if diff==0:
        break
    ret=ret+diff
print(ret)

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
L_list=[int(thing) for thing in input().split()]
L_list.sort(reverse=True)

ret=0
s_list=[]
def my_bisect(l:list,f,left_true=True):
    def bis(begin:int,end:int):
        # print("{}:{}".format(begin,end))
        if end-begin<=1:
            if f(l[end]) ==True:
                return end
            elif f(l[begin])==True:
                return begin
            else:
                return -1
        v=int((end+begin)/2//1)
        res=f(l[v])
        if res and left_true:
            return bis(v,end)
        elif not res and left_true:
            return bis(begin,v)
        elif res and not left_true:
            return bis(begin,v)
        elif not res and not left_true:
            return bis(v,end)
        else:
            print("what")
    return bis(0,len(l)-1)
for i in range(N):
    n=L_list[i]
    diff=0
    for j in range(i+1,N-1):
        k=L_list[j]
        m=n-k
        l=L_list[j+1:N]
        s=my_bisect(l,lambda x:x>m)+1
        s_list.append(s)
        if s==0:
            break
        else:diff=diff+s
    if diff==0:
        break
    ret=ret+diff
print(ret)