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: Runtime Error

Solution code:
N = int(input())
L = list(map(int,input().split()))
from math import factorial
a = factorial(N) / factorial(3) / factorial(N - 3)
result=0
for i in range(N):
    for j in range(N-i-2):
        for k in range(N-i-j-2):
            if L[i]+L[j+1+i]<=L[k+2+i+j]:
                result +=1

        
print(int(a-result))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)

from itertools import accumulate


def factorial(x):
    if x <= 0:
        return 1

    return factorial(x - 1) * x


def comb(n, r):
    return factorial(n) / factorial(r) / factorial(n - r)


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

    nums = [0] * 1001

    for e in l:
        nums[e] += 1

    acm = list(accumulate(nums))

    l.sort()
    ans = 0
    tmp = [0] * 1001
    tmp2 = [[0] * 1001 for _ in range(1001)]
    for i in range(n-2):
        for j in range(i+1, n-1):
            a = l[i]
            b = l[j]
            k = min(1000, a+b-1)

            # b < c のときの数
            cnt = acm[k] - acm[b]

            # b == c のときの補正
            if nums[b] >= 3 and a == b and tmp[b] == 0:
                cnt += int(comb(nums[b], 3))
                tmp[b] = 1

            if nums[b] >= 2 and a != b and tmp2[a][b] == 0:
                cnt += int(comb(nums[b], 2)) * nums[a]
                tmp2[a][b] = 1

            ans += cnt

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

sum = 0
for i,k in enumerate(L):
  for j in range(i+1, N):
    for l in range(j+1, N):
      if L[l] < L[j] + k:
     　 print(bisect.bisect_right(L,L[j] + k))
        
        sum += 1
      else:
        break;
print(sum)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def triangles(L, s, c, n):
    if c == len(L):
        return(n)
    if L[c] < s:
        n += 1
        return(triangles(L, s, c + 1, n))
    else:
        return(n)

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

Submission status: Runtime Error

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:
        big = i.index(max(i))
        if sum(i) - 2 * i[big] > 0:
            ans_count += 1

    print(ans_count)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
A = list(map(int,input().split()))
sorted(A,reverse = True)
ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      if A[i] >= A[j] + A[k]:
        k = N
        j = N
      else:
        if A[j] < A[i] + A[k] and A[k] < A[i] + A[j]:
          ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)

from itertools import accumulate


def factorial(x):
    if x <= 0:
        return 1

    return factorial(x - 1) * x


def comb(n, r):
    return factorial(n) / factorial(r) / factorial(n - r)


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

    nums = [0] * 1001

    for e in l:
        nums[e] += 1

    acm = list(accumulate(nums))

    l.sort()
    ans = 0
    tmp = [0] * 1001
    tmp2 = [0] * 1001
    for i in range(n-2):
        for j in range(i+1, n-1):
            a = l[i]
            b = l[j]
            k = min(1000, a+b-1)

            # b < c のときの数
            cnt = acm[k] - acm[b]

            # b == c のときの補正
            if nums[b] >= 3 and a == b and tmp[b] == 0:
                cnt += int(comb(nums[b], 3))
                tmp[b] = 1

            if nums[b] >= 2 and a != b and tmp2[b] == 0:
                cnt += int(comb(nums[b], 2)) * nums[a]
                tmp2[b] = 1

            ans += cnt

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
L = list(map(int,input().split()))
from scipy.special import comb
a = comb(N, 3, exact=True)
result=0
for i in range(N):
    for j in range(N-i-2):
        for k in range(N-i-j-2):
            if L[i]+L[j+1+i]<=L[k+2+i+j]:
                result +=1

        
print(a-result)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
L=list(map(int,input().split()))
ans=0
len_L=len(L)
for i in range(len_L):
    a=L[i] 
    for j in range(i+1,len_L):
        b=L[j]
        for k in range(j+1,len_L):
            c=L{k}
            if a<b+c and b<c+a and c<a+b:
                ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def triangles(L, s, c, n):
    if c == len(L):
        return(n)
    if L[c] < s:
        n += 1
        return(triangles(L, s, c + 1, n))
    else:
        return(n)

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

Submission status: Runtime Error

Solution code:
N=int(input())
L=list(map(int,input().split()))
d=[]
s=0
def triagles(a,b,c):
  if a<b+c and b<a+c and c<a+b:
    d+=set(a,b,c)
    return 1
  else:
    return 0
for i in L:
  for j in L:
    for k in L:
      q=index[i];w=index[j];e=index[k]
      if q!=w and w!=e and e!=q:
        if set(i,j,k) not in d:
          s+=triangles(i,j,k)
print(s)

Submission status: Runtime Error

Solution code:
nCr = {}
def cmb(a, b):
    if a == 0 or a == b: return 1
    if a == 1: return b
    if (a,b) in nCr: return nCr[(a,b)]
    nCr[(a,b)] = cmb(a-1,b) + cmb(a-1,b-1)
    return nCr[(a,b)]
n = int(input())
h = list(map(int, input().split()))
def triangle(n,h):
    x = cmb(n, 3)
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            for k in range(j+1, n):
                if h[i] >= h[j] + h[k] or h[j] >= h[i] + h[k] or h[k] >= h[j] + h[i]:
                    x = x - 1
    return x
print(triangle(n,h))

Submission status: Runtime Error

Solution code:
import itertools
import timeit
import pandas as pd

def main():
    N = int(input())
    L = sorted([int(x) for x in input().split()])
    data = []
    for a, b in itertools.combinations(L, 2):
        data.append([a, b, abs(a - 1)])
    df = pd.DataFrame(data)
    ans = 0
    for i, b in enumerate(L):
        ans += ((df.iloc[:, 0] < i) & (df.iloc[:, 2] < b)).sum()
    print(ans)

if __name__ == "__main__":
    # print(timeit.timeit(lambda: main(), number=1))
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
L = sorted(list(map(int,input().split())))
s = 0
if N < 10^3:
    for i in range(0,N-2):
        for k in range(i+1,N-1):
            for t in range(k+1,N):
                if L[t] >= L[i] + L[k]:
                    break
                else:
                    s += 1
else:
s = N*(N-1)*(N-2)//6
    for i in range(N-400,400,-1):
        for k in range(399,i-1):
            for t in range(k+1,i):
                if L[i] < L[k] + L[t]:
                    break
                else:
                    s -= 1
print(s)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
from bisect import bisect, bisect_left
from fractions import Fraction

def comb(n, k):
    if n < 0 or k < 0:
        pdt = 0
    else:
        pdt = 1
        for s in range(1, k + 1):
            pdt *= Fraction(n - s + 1, s)
    return pdt.numerator

input = sys.stdin.readline
N = int(input())
L = sorted([int(x) for x in input().split()])
c = 0
D = {}
Lcnt = Counter(L)
Lset = sorted(list(set(L)))
imoset = [0] * len(Lset)
t = 0
for i, lset in enumerate(Lset):
    t += lset
    imoset[i] = t
Nset = len(Lset)
for i in range(Nset):
    if Lcnt[Lset[i]] >= 3:
        c += comb(Lcnt[Lset[i]], 3)

    if Lcnt[Lset[i]] >= 2:
        fl = bisect(L, Lset[i])
        fr = bisect_left(L, Lset[i] * 2)
        c += comb(Lcnt[Lset[i]], 2) * (fr - fl)
    for j in range(i + 1, Nset):
        fl = bisect(L, Lset[j])
        fr = bisect_left(L, Lset[i] + Lset[j])
        c += Lcnt[Lset[i]] * Lcnt[Lset[j]] * (fr - fl)

if Lcnt[Lset[-1]] >= 2:
    c += Lcnt[Lset[-2]] * comb(Lcnt[Lset[-1]], 2)
print(c)

Submission status: Runtime Error

Solution code:
import numpy as np
import collections as cl
n=int(input())
l=np.asarray(list(map(int,input().split())))
rng=np.arange(n)
i=np.tile(rng,(n**2,1)).T.ravel()
j=np.tile(np.tile(rng.copy(),(n,1)).ravel(),(n,1)).T.ravel()
k=np.tile(rng.copy(),(n**2,1)).ravel()
tmp=(l[i]<l[j]+l[k])*(l[j]<l[i]+l[k])*(l[k]<l[i]+l[j])*(i!=j)*(i!=k)*(j!=k)
tmp=np.asarray(np.where(tmp.reshape(n,n,n))).T.astype(str)
tmp.sort(axis=-1)
ans=[]
for t in tmp:
    ans.append(t[0]+t[1]+t[2])
print(len(cl.Counter(ans)))

Submission status: Runtime Error

Solution code:
import numpy as np

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

lsorted = np.sort(np.array(l))[::-1]

for longest in range(len(l)-2):

    for second in range(longest+1, len(l)):
        if lsorted[longest] - 2*lsorted[second] >= 0:
            break 
        ans += np.count_nonzero(lsorted[second+1:] > (lsorted[longest] - lsorted[second]) )
print(ans)

Submission status: Runtime Error

Solution code:
import sys
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()
    # print('l : {}  count : {}  ans : {}  i : {}  l[i] : {}'.format(l, count, ans, i, l[i]))
    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:
    big = i.index(max(i))
    if sum(i) - 2 * i[big] > 0:
        ans_count += 1

print(ans_count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from itertools import accumulate


def factorial(x):
    if x <= 0:
        return 1

    return factorial(x - 1) * x


def comb(n, r):
    return factorial(n) / factorial(r) / factorial(n - r)


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

    nums = [0] * 1001

    for e in l:
        nums[e] += 1

    acm = list(accumulate(nums))

    l.sort()
    ans = 0
    tmp = [0] * 1001
    tmp2 = [0] * 1001
    for i in range(n-2):
        for j in range(i+1, n-1):
            a = l[i]
            b = l[j]
            k = min(1000, a+b-1)

            # b < c のときの数
            cnt = acm[k] - acm[b]

            # b == c のときの補正
            if nums[b] >= 3 and a == b and tmp[b] == 0:
                cnt += int(comb(nums[b], 3))
                tmp[b] = 1

            if nums[b] >= 2 and a != b and tmp2[b] == 0:
                cnt += int(comb(nums[b], 2))
                tmp2[b] = 1

            ans += cnt

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

lsorted = sorted(l, reverse=True)

for longest in range(len(l)-2):

    for second in range(longest+1, len(l)):
        se
        if lsorted[longest] - 2*lsorted[second] >= 0:
            break 
        for third in lsorted[second+1:]:
            if lsorted[longest] - lsorted[second] - third >= 0:                
                break
            else:
                ans += 1

print(ans)

Submission status: Runtime Error

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

l_l.sort()
#print(l_l)

count=0
for i in range(n):
  for j in range(i+1,n):
    for k in range (j+1,n):
      #print(l_l[i],l_l[j],l_l[k])
      if(l_l[i]+l_l[j]<l_l[k]):
        #print(l_l[i],l_l[j],l_l[k])
        count +=1a
      #else:
        #break
      
print(int(n*(n-1)*(n-2)/6-count))

Submission status: Runtime Error

Solution code:
import numpy as np

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

cnt = 0

for i in range(n):
    a = l[i]
    for j in range(i+1, n):
        b = l[j]
        cnt += sum(nl[j+1:] < a + b)

print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
L = list(map(int, input().split()))
sinL = deepcopy(L)
# ans = 0
ansList = []
for x in range(N):
    L = deepcopy(sinL)
    a = L.pop(x) # ★
#     print("a={}".format(a))
    lengthOfL1 = len(L) # 注意 実際の-1
    
    for y in range(lengthOfL1):
#         print(L)
        b = L.pop(y) # ★ 後で挿入しないと！！！
        lengthOfL2 = len(L)
        # a+b を超えないやつを全部切る。
        wa = a + b
        L_censored = [i for i in L if i < wa]
#         print(L_censored)
        if len(L_censored) == 0:
            pass
        else:
            lengthOfL2 = len(L_censored)

            for c in L_censored:
                if b+c <= a:
                    pass
                else:
                    if c+a <= b:
                        pass
                    else:
                        #ans += 1
                        ansList.append([a,b,c])
#                         print(ansList)
        L.insert(y, b)
# print("ahoo")
# print(ansList)
sortedAnsList = map(sorted, ansList)
def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]
print(len(get_unique_list(sortedAnsList)))

Submission status: Runtime Error

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

for i in range(n):
    for j in range(i, n - i):
        a = l[i]
        b = l[j]
        for k in range(j, n - i - j):
            c = l[k]
            if (a < b + c) and (b < c + a) and (c < a + b):
                ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import collections as cl
n=int(input())
l=np.asarray(list(map(int,input().split())))
i=np.tile(np.arange(len(l)),(n**2,1)).T.ravel()
j=np.tile(np.tile(np.arange(len(l)),(n,1)).ravel(),(n,1)).T.ravel()
k=np.tile(np.arange(n),(n**2,1)).ravel()
tmp=(l[i]<l[j]+l[k])*(l[j]<l[i]+l[k])*(l[k]<l[i]+l[j])*(i!=j)*(i!=k)*(j!=k)
tmp=np.asarray(np.where(tmp.reshape(n,n,n))).T.astype(str)
tmp.sort(axis=-1)
ans=[]
for t in tmp:
    ans.append(t[0]+t[1]+t[2])
print(len(cl.Counter(ans)))

Submission status: Runtime Error

Solution code:
n = int(input())

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

ans = int(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] and l[j] < l[i]+l[k] and l[k] < l[j]+l[i]:
                    ans += 1
                
print(ans)

Submission status: Runtime Error

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
n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

global big_ans
big_ans = []

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

    if i == len(l):
        # print('i end')
        return
    passa = ans.copy()
    # print('l : {}  count : {}  ans : {}  i : {}  l[i] : {}'.format(l, count, ans, i, l[i]))
    angle(l, count, passa, i + 1)
    ans.append(l[i])
    passb = ans.copy()
    angle(l, count + 1, passb, i + 1)


angle(l, 0, [], 0)

ans_count = 0

for i in big_ans:
    big = i.index(max(i))
    if sum(i) - 2 * i[big] > 0:
        ans_count += 1

print(ans_count)

Submission status: Runtime Error

Solution code:
import cython
import bisect
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
count=0
for i in range(n):
    for j in range(n):
        if i!=j:
            index1=bisect.bisect_left(listss, abs(listss[j]-listss[i])+1)
            index2=bisect.bisect_right(listss,listss[i]+listss[j]-1)
            count+=(index2-index1)
            x=abs(listss[j]-listss[i])
            if  x+1<=listss[i]:
                count-=1
            if x+1<=listss[j]:
                count-=1
print(count//6)

Submission status: Runtime Error

Solution code:
N = int(input())
d = input().split()
d.sort()

li = []

temp = ''
for i in range(N):
    if(temp != d[i]):
        temp = d[i]
        li.append(int(d[i]))

sum = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if(int(li[i])<int(li[j])+int(li[k]) and int(li[j])<int(li[i])+int(li[k]) and int(li[k])<int(li[i])+int(li[j])):
                sum += 1
            else:
                break
print(sum)

Submission status: Runtime Error

Solution code:
from math import factorial
n = int(input())
num =list(map(int,input().split()))
 
    
a = factorial(n) / factorial(3) / factorial(n - 3)
s_num = sorted(num, reverse=True)
 
c = 0
for i in range(n-2):
    b = False
    for j in range(2,n-i):
        if b == True:
            break
        else:
            for k in range(1,j):
                if s_num[i] - s_num[-j] < s_num[-k]:
                    ans = a-c
                    b = True
                    break
 
                else:
                    c += 1
print(int(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]


N=int(input())
L=input()
L=L.split()
L=[int(item) for item in L]
L.sort()
ans=cmb(N,3)
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]:
                ans-=(N-k)
                break


print(int(ans))

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
while (l[-1]>l[-2]+l[-3]) and (len(l)>3):
	del l[-1]
n=len(l)
ans=0
i=0
while i<n-2:
	j=i+1
	while j<n-1:
		k=j+1
		while k<n:
			if l[k]>l[i]+l[j]:
				ans=ans+k-j-1
				k=n
			else if k==n-1:
				ans=ans+k-j
			k=k+1
		j=j+1
	i=i+1
print(ans)

Submission status: Runtime Error

Solution code:
import sys

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]


N=int(input())
L=input()
L=L.split()
L=[int(item) for item in L]
L.sort()
if N<=3:
    ans=1
else:
    ans=cmb(N,3)
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]:
                ans-=(N-k)
                break


print(int(ans))

Submission status: Runtime Error

Solution code:
from math import factorial
n = int(input())
num =list(map(int,input().split()))
 
    
a = factorial(n) / factorial(3) / factorial(n - 3)
s_num = sorted(num, reverse=True)

c = 0
for i in range(n-2):
    b = False
    for j in range(2,n-i):
        if b == True:
            break
        else:
            for k in range(1,j):
                if s_num[i] - s_num[-j] < s_num[-k]:
                    ans = a-c
                    b = True
                    break

                else:
                    c += 1

print(int(ans))

Submission status: Runtime Error

Solution code:
# coding: utf-8

def triangles(L, s, c, n):
    if c == len(L):
        return(n)
    if L[c] < s:
        n += 1
        return(triangles(L, s, c + 1, n))
    else:
        return(n)

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
dp = []
for i in range(L[-2] + L[-1] + 1):
    dp.append(-1)
for i in range(N):
    for j in range(i + 1, N - 1):
        if dp[L[i] + L[j]] >= 0:
            ans += dp[L[i] + L[j]]
        else:
            dp[L[i] + L[j]] = triangles(L, L[i] + L[j], j + 1, 0)
            ans += dp[L[i] + L[j]]
print(ans)

Submission status: Runtime Error

Solution code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int tri_judge(int i, int j, int k);

int compare_int(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

int main()
{
    int n;
    scanf("%d", &n);
    int length[n];
    int i;
    for (i = 0; i < n; i++)
    {
        scanf("%d", &length[i]);
    }

    qsort(length, n, sizeof(int), compare_int);

    int j, k;
    int ans = 0;

    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            for (k = j + 1; k < n; k++)
            {
                if (tri_judge(length[i], length[j], length[k]))
                {
                    ans++;
                    //printf("%d,%d,%d\n", length[i], length[j], length[k]);
                }
                else
                {
                    break;
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

int tri_judge(int i, int j, int k)
{
    if (i + j > k && j + k > i && k + i > j)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Submission status: Runtime Error

Solution code:
nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]
n = int(input())
h = list(map(int, input().split()))
def triangle(n,h):
    x = cmb(n, 3)
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            for k in range(j+1, n):
                if h[i] >= h[j] + h[k] or h[j] >= h[i] + h[k] or h[k] >= h[j] + h[i]:
                    x = x - 1
    return x
print(triangle(n,h))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from itertools import accumulate


def factorial(x):
    if x == 0:
        return 1

    return factorial(x - 1) * x


def comb(n, r):
    return factorial(n) / factorial(r) / factorial(n - r)


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

    nums = [0] * 1001

    for e in l:
        nums[e] += 1

    acm = list(accumulate(nums))

    l.sort()
    ans = 0
    tmp = [0] * 1001
    tmp2 = [0] * 1001
    for i in range(n-2):
        for j in range(i+1, n-1):
            a = l[i]
            b = l[j]
            k = min(1000, a+b-1)

            # b < c のときの数
            cnt = acm[k] - acm[b]

            # b == c のときの補正
            if nums[b] >= 3 and a == b and tmp[b] == 0:
                cnt += int(comb(nums[b], 3))
                tmp[b] = 1

            if nums[b] >= 2 and a != b and tmp2[b] == 0:
                cnt += int(comb(nums[b], 2))
                tmp2[b] = 1

            ans += cnt

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
cnt = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        cnt += np.count_nonzero(np.array(l[j+1:])>l[i]-l[j])
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
c = 0

for i in range(0, n-2):
  for x in range(1, n -2):
    if l[-x] + l[-x-1] <= l[i]:
      a = n - x + 1
      break
    else:
      continue
  for j in range(i+1, a-1):
    for k in range(j+1, a):
          if (l[i] < l[j] + l[k]) and (l[j] < l[i] + l[k]) and (l[k] < l[i] + l[j]):
            c = c + 1
          else:
            break
print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split(" ")))
count = 0
l = sorted(l)
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j + 1,n-1):
      if l[n-i-1] >= l[n-j-1]+l[n-k-1]:
        i_over = True
        break
      count += 1
    if i_over == True:
      break
  i_over = False
print(count)

Submission status: Runtime Error

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

import itertools
l = list(itertools.combinations(L, 3))

a = 0
n = 0

for x in range(len(l)):
  if max(l[n]) < sum(l[n]) - max(l[n]):
    a += 1
    n += 1
  else:
    n += 1
    
print(a)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def triangles(L, s, c, n):
    if c == len(L):
        return(n)
    if L[c] < s:
        n += 1
        return(triangles(L, s, c + 1, n))
    else:
        return(n)

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
dp = []
for i in range(2001):
    dp.append(-1)
for i in range(N):
    for j in range(i + 1, N - 1):
        if dp[L[i] + L[j]] >= 0:
            ans += dp[L[i] + L[j]]
        else:
            dp[L[i] + L[j]] = triangles(L, L[i] + L[j], j + 1, 0)
            ans += dp[L[i] + L[j]]
print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial
N=int(input())
L=input()
L=L.split()
L=[int(item) for item in L]
L.sort()
if N<=3:
    ans=1
else:
    ans=factorial(N)/factorial(3)/factorial(N-3)

Submission status: Runtime Error

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

l = sorted(l, reverse = True)
count = 1
same = []
two = []
ans = 0
for i in range(n-1):
    if l[i] == l[i+1]:
        count += 1
        if l[i] not in same:
            same.append(l[i])
    else:
        if count >= 2:
            two.append(l[i])
        count = 1
    
if count >= 2:
    two.append(l[i])

         
for i in range(len(same)):
    l = [j for j in l if j != same[i]]
    l.append(same[i])


l = sorted(l, reverse = True)
for i in range(len(l)-2):
    for j in range(len(l)-2-i):
        if l[i] > 2 * l[i+j+1]:
            break
        for k in range(len(l)-2-i-j):
            if l[i] < l[i+j+1] + l[i+j+k+2]:
                ans += 1
            else:
                break

for i in range(len(two)):
    for j in range(len(l)):
        if 2 * l[two[i]] > l[j] > int(l[two[i]] / 2):
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
c = 0

for i in range(0, n-2):
  for x in range(1, n -2):
    if l[-x] + l[-x-1] <= l[i]:
      a = n - x + 1
      break
    else:
      continue
  for j in range(i+1, a-1):
    for k in range(j+1, a):
          if (l[i] < l[j] + l[k]) and (l[j] < l[i] + l[k]) and (l[k] < l[i] + l[j]):
            c = c + 1
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int, input().split()))
l=sorted([x for x in set(l) if l.count(x)<4])
ans=0
x=[]
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,(n-sum((x>l[j]+l[i]) for x in l))):
            if (l[i]<(l[j]+l[k])and l[j]<(l[i]+l[k]) and x.count([l[i],l[j],l[k]])==0):
                x.append([l[i],l[j],l[k]])
                ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# 入力
N = int(input())
L = list(map(int, input().split()))

L.sort(reverse=True)
sum = 0
for i in range(N):
  half = L[i]/2.0
  for j in range(i+1, N):
    if(L[j] < half):
      jend = j
      break
  for j in range(i+1, jend+1):
    for k in range(j+1, N):
      if(L[i] < L[j] + L[k]):
        sum +=1

print("{}".format(sum))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from math import factorial
N=int(input())
L=input()
L=L.split()
L=[int(item) for item in L]
L.sort()
if N<=3:
    ans=1
else:
    ans=factorial(N)/factorial(3)/factorial(N-3)
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]:
                ans-=(N-k)
                break


print(int(ans))

Submission status: Runtime Error

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

from itertools import accumulate
from math import factorial


def comb(n, r):
    return factorial(n) / factorial(r) / factorial(n - r)


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

    nums = [0] * 1001

    for e in l:
        nums[e] += 1

    acm = list(accumulate(nums))

    l.sort()
    ans = 0
    tmp = [0] * 1001
    tmp2 = [0] * 1001
    for i in range(n-2):
        for j in range(i+1, n-1):
            a = l[i]
            b = l[j]
            k = min(1000, a+b-1)

            # b < c のときの数
            cnt = acm[k] - acm[b]

            # b == c のときの補正
            if nums[b] >= 3 and a == b and tmp[b] == 0:
                cnt += int(comb(nums[b], 3))
                tmp[b] = 1

            if nums[b] >= 2 and a != b and tmp2[b] == 0:
                cnt += int(comb(nums[b], 2))
                tmp2[b] = 1

            ans += cnt

    print(ans)


if __name__ == "__main__":
    main()