Sphere Online Judge

Computer geeks! Show off your skills and claim the job of your dreams! RecruitCoders.com

SPOJ Problem Set (classical)

2905. Not a Triangle

Problem code: NOTATRI

You have N (3 ≤ N ≤ 2,000) wooden sticks, which are labeled from 1 to N. The i-th stick has a length of Li (1 ≤ Li ≤ 1,000,000). Your friend has challenged you to a simple game: you will pick three sticks at random, and if your friend can form a triangle with them (degenerate triangles included), he wins; otherwise, you win. You are not sure if your friend is trying to trick you, so you would like to determine your chances of winning by computing the number of ways you could choose three sticks (regardless of order) such that it is impossible to form a triangle with them.

Input

The input file consists of multiple test cases. Each test case starts with the single integer N, followed by a line with the integers L1, ..., LN. The input is terminated with N = 0, which should not be processed.

Output

For each test case, output a single line containing the number of triples.

Example

Input:
3
4 2 10
3
1 2 3
4
5 2 9 6
0

Output:
1
0
2

For the first test case, 4 + 2 < 10, so you will win with the one available triple. For the second case, 1 + 2 is equal to 3; since degenerate triangles are allowed, the answer is 0.


Added by:Neal Wu
Date:2008-08-03
Time limit:2s
Source limit:50000B
Languages:All except: ERL JS

hide comments
2012-03-31 12:54:39 معاذ
merge sort + binary search = A.C.
2012-01-11 08:50:20 rahul singh
nice problem..did it in O(n^2 log n) and AC
2011-07-11 06:21:29 Saikat Debnath
@Albert & Grandmaster : My O(n^2 logn) algorithm got me AC!! Write your code neatly... :)
2011-06-25 08:04:16 Grandmaster
what is the preferred complexity for this code ???
2011-05-26 19:11:25 mukul gupta


Last edit: 2011-06-10 16:49:23
2011-05-20 13:43:37 Albert Gevorgyan(Quantum)
My O(n^2) algorithm gets time limit. Help!!!
SPOJ System © 2012 Sphere Research Labs | Projekty informatyczne i aplikacje na zamówienie. All Rights Reserved.