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

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApp2
{


    public class Program
    {

        static long D = (long)Math.Pow(10, 9) + 7;

        public static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());

            int[] Ls = Read(Console.ReadLine());

            int[] Ns = new int[N];

            for (int i = 0; i < N; i++)
            {
                Ns[i] = i;
            }

            int[][] orders = Comb.Enumerate(Ns,3,false).ToArray();

            int count = 0;
            for (int i = 0; i < orders.Length; i++)
            {
                if (Ls[orders[i][0]] + Ls[orders[i][1]] > Ls[orders[i][2]])
                {
                    if (Ls[orders[i][2]] + Ls[orders[i][0]] > Ls[orders[i][1]])
                    {
                        if (Ls[orders[i][1]] + Ls[orders[i][2]] > Ls[orders[i][0]])
                        {
                            count++;
                        }
                    }
                }
            }

            Console.WriteLine(count);
            Console.ReadLine();

        }


        public static long GetInv(long a, long mod)
        {
            return PowerMod(a, mod - 2, mod);
        }

        public static long Combination(long a, long b, long mod)
        {
            long numerator = Factorial(a, mod);
            long denominator = (Factorial(b, mod) * Factorial(a - b, mod)) % mod;

            return (numerator * GetInv(denominator, mod)) % mod;
        }

        public static long Factorial(long n, long mod)
        {
            if (n == 0) return 1;
            if (n == 1) return 1;
            long ans = n;
            for (int i = 2; i < n; i++)
            {
                ans = (ans * i) % mod;
            }

            return ans;
        }




        private static long PowerMod(long a, long n, long mod)
        {
            if (n == 1) return a;

            long p = PowerMod(a, n / 2, mod);
            if (n % 2 == 1) return ((p * p) % mod * a) % mod;
            else return (p * p) % mod;
        }

        public static class Comb
        {
            public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items, int k, bool withRepetition)
            {
                if (k == 1)
                {
                    foreach (var item in items)
                        yield return new T[] { item };
                    yield break;
                }
                foreach (var item in items)
                {
                    var leftside = new T[] { item };

                    // item よりも前のものを除く （順列と組み合わせの違い)
                    // 重複を許さないので、unusedから item そのものも取り除く
                    var unused = withRepetition ? items : items.SkipWhile(e => !e.Equals(item)).Skip(1).ToList();

                    foreach (var rightside in Enumerate(unused, k - 1, withRepetition))
                    {
                        yield return leftside.Concat(rightside).ToArray();
                    }
                }
            }
        }

        public class BFSClass
        {
            /// <summary>
            /// 1...道、2...障害物(0は未知)
            /// </summary>
            /// <param name="map"></param>
            public BFSClass(int[][] map)
            {
                Map = map;
            }

            private class TileFrom
            {
                public TileFrom(int[] thisTile, int[] from)
                {
                    ThisTile = thisTile;
                    From = from;
                }
                public int[] ThisTile { get; }
                public int[] From { get; }
            }

            public int[][] Map { get; }

            //調べる予定の位置を入れる
            Queue<int[]> search = new Queue<int[]>();

            //調べ終わった位置
            List<TileFrom> done = new List<TileFrom>();

            /// <summary>
            /// 
            /// </summary>
            /// <param name="map"></param>
            public int BFS(int[] startPos, int[] goalPos)
            {
                done.Add(new TileFrom(startPos, null));

                //現在の位置
                int[] currentPos = startPos;

                return StartBFS(0,currentPos, goalPos);
            }

            /// <summary>
            /// 終わったらtrue
            /// </summary>
            /// <param name="currentPos"></param>
            /// <param name="goalPos"></param>
            private int StartBFS(int currentCount,int[] currentPos, int[] goalPos)
            {
                if (currentPos.SequenceEqual(goalPos)) return currentCount;

                List<int[]> around = GetAroundTile(currentPos);
                foreach (var item in around)
                {
                    if (!done.Exists(i => i.ThisTile.SequenceEqual(item)))
                    {
                        done.Add(new TileFrom(item, currentPos));
                        search.Enqueue(item);
                        Console.WriteLine(item[0] + "," + item[1] + "を調べます");
                    }
                    else
                    {
                        Console.WriteLine(item[0] + "," + item[1] + "は済んでいます");
                    }
                }

                while (search.Count > 0)
                {
                    var nextStartPos = search.Dequeue();
                    if (Map[nextStartPos[0]][nextStartPos[1]] == 1)
                    {
                        return StartBFS(currentCount+1,nextStartPos, goalPos);
                    }
                }

                return 0;
            }

            private List<int[]> GetAroundTile(int[] pos)
            {
                List<int[]> ans = new List<int[]>();
                int[][] deltas = {  new int[2] { 0, 1 },
                                new int[2] { 1, 0 },
                                new int[2] { 0, -1 },
                                new int[2] { -1, 0 } };

                foreach (var delta in deltas)
                {
                    int[] newPos = { pos[0] + delta[0], pos[1] + delta[1] };
                    if (OnMap(newPos)) ans.Add(newPos);
                }
                return ans;
            }

            private bool OnMap(int[] pos)
            {
                if (pos[0] >= 0 && pos[0] < Map.Length)
                {
                    if (pos[1] >= 0 && pos[1] < Map[0].Length)
                    {
                        return true;
                    }
                }
                return false;
            }

            private int FindRoot(int[] goalPos)
            {
                Stack<int[]> root = new Stack<int[]>();
                root.Push(goalPos);

                TileFrom f = done.Find(i => i.ThisTile[0] == goalPos[0] && i.ThisTile[1] == goalPos[1]);
                while (true)
                {
                    root.Push(f.From);
                   // Console.WriteLine("[" + f.ThisTile[0] + "," + f.ThisTile[1] + "]" + "から[" + f.From[0] + "," + f.From[1] + "]");

                    TileFrom test = done.Find(i => i.ThisTile[0] == f.From[0] && i.ThisTile[1] == f.From[1]);

                    if (test.From == null) break;
                    f = test;
                }
                //while (root.Count > 0)
                //{
                //    int[] r = root.Pop();
                //    Console.WriteLine("[" + r[0] + "," + r[1] + "]");
                //}
                return root.Count - 1;
            }
        }


      

        static long Com(int n, int m)
        {
            if (m == 0 || m == n) return 1;

            if (n < m) return 0;

            int[] a = new int[m];

            for (int i = 0; i < m; i++)
            {
                a[i] = n - i;
            }
            for (int i = 1; i <= m; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (a[j] % i == 0)
                    {
                        a[j] /= i;
                        break;
                    }
                }
            }

            long ans = 1;
            for (int i = 0; i < m; i++)
            {
                ans *= a[i];
                ans %= D;
            }
            return ans;
        }


        /// <summary>
        /// 最小公倍数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static long Lcm(long a, long b)
        {
            return a * b / Gcd(a, b);
        }

        /// <summary>
        /// 最大公約数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static long Gcd(long a, long b)
        {
            if (a < b)
                // 引数を入替えて自分を呼び出す
                return Gcd(b, a);
            while (b != 0)
            {
                var remainder = a % b;
                a = b;
                b = remainder;
            }
            return a;
        }


        private static bool S(int[] data,int K ,int l ,int x)
        {
            int sum = 0;
            int max = Math.Min(x+1, data.Length);
            for (int i = l; i < max; i++)
            {
                sum += data[i];
            }
            Console.WriteLine(sum);
            return sum>=K;
        }


        public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items)
        {
            if (items.Count() == 1)
            {
                yield return new T[] { items.First() };
                yield break;
            }
            foreach (var item in items)
            {
                var leftside = new T[] { item };
                var unused = items.Except(leftside);
                foreach (var rightside in Enumerate(unused))
                {
                    yield return leftside.Concat(rightside).ToArray();
                }
            }
        }




        private static string ReplaceOneChar(string S, int index, string newS)
        {
            string s1 = S.Substring(0, index - 1);
            string s2 = S.Substring(index);
            return s1 + newS + s2;
        }

        private static bool PartialSum(int target, int[] Ns)
        {
            //if (target == 0) return true;
            bool[][] ps = new bool[Ns.Length + 1][];

            //i=0では、どんなWに対してもfalse
            ps[0] = new bool[target+1];
            for (int i = 0; i < ps[0].Length; i++) ps[0][i] = false;

            ps[0][0] = true;


            //考慮に入れるitemを、前から順に追加していく
            for (int i = 0; i < Ns.Length; i++)
            {
                //初期化
                ps[i + 1] = new bool[target+1];

                for (int t = 0; t <= target; t++)
                {
                    if (t >= Ns[i]) ps[i + 1][t] = ps[i][t - Ns[i]] || ps[i][t];
                    else ps[i + 1][t]=ps[i][t];
                }
            }

            return ps[Ns.Length][target];
        }

        /// <summary>
        /// 引数は、W(重さ制限)、items[重さ][価値]
        /// </summary>
        /// <param name="W"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        private static int DP(int W,int[][] items)
        {
            //データ
            int[][] dp = new int[items.Length+1][];

            //i=0では、どんなWに対しても0
            dp[0] = new int[W + 1];

            //考慮に入れるitemを、前から順に追加していく
            for (int i = 0; i < items.Length; i++)
            {
                //初期化
                dp[i+1] = new int[W + 1];

                for (int w = 0; w <= W; w++)
                {
                    if (w >= items[i][0]) dp[i + 1][w] = Math.Max(dp[i][w - items[i][0]]+items[i][1], dp[i][w]);
                    else dp[i + 1][w] = dp[i][w];
                }
            }
            return dp[items.Length][W];
        }

        private static int[] Read(string input)
        {
            string[] strings = input.Split(' ');
            int num = strings.Length;
            int[] ints = new int[num];
            for (int i = 0; i < num; i++)
            {
                ints[i] = int.Parse(strings[i]);
            }
            
            return ints;
        }


    }


}