NOTATRI  Not a Triangle
You have N (3 ≤ N ≤ 2,000) wooden sticks, which are labeled from 1 to N. The ith stick has a length of L_{i} (1 ≤ L_{i} ≤ 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 L_{1}, ..., L_{N}. 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.
hide comments
Pulkit Singhal:
20150527 04:38:08
O(n^2) is easily accepted after preprocessing 

btm:
20150216 14:11:21
Good one!!! 

Yash:
20150211 11:35:53
Time limit too strict for slower languages....for O(n^2.log n)TLE in Java, AC C++ :( 

Madhav:
20150125 19:48:33
very good question!! 

Rang:
20150123 15:16:17
RecursiveSearch Doesnt work, I dont know why ? 

Swapnil Borse:
20141229 22:06:39
This problem was fun!!! got one idiotic TLE


agaurav77:
20141228 13:17:48
Good question. Brute force won't pass, and you'll have to think. :) 

arp_ee:
20140630 20:35:10
easy..my 100th on spoj.. 

simararorarox9:
20140608 14:25:39
did in o(n**2)..instead of n**2 log n... 

shashi roshan:
20140530 18:50:38
100th on spoj :) AC on naive solution ... O( n^2 * log2(n) ) ... Last edit: 20140530 18:51:19 
Added by:  Neal Wu 
Date:  20080803 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ERL JSRHINO 