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: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ABC143D
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var l = input.NextInt(n);
            //var l = new int[n];

            var map = new int[1001];
            for (int i = 0; i < n; i++)
            {
                map[l[i]]++;
            }

            Array.Sort(l);

            long count = 0;
            for (int i = 0; i < map.Length; i++)
            {
                for (int j = i + 1; j < map.Length; j++)
                {
                    for (int k = j + 1; k < map.Length; k++)
                    {
                        if (i >= j + k) continue;
                        if (j >= k + i) continue;
                        if (k >= i + j) continue;

                        count += map[i] * map[j] * map[k];
                    }
                }
            }

            Console.WriteLine(count);
        }

        /// <summary>
        /// 区間加算と区間総和
        /// セグメント木による
        /// </summary>
        class RangeAddQuery
        {
            readonly int N;
            readonly int[] All;
            readonly int[] Part;

            public long this[int i]
            {
                get { return Sum(i, i + 1); }
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="n">節点数</param>
            /// <param name="v">ノードの初期値</param>
            public RangeAddQuery(int n, int v = 0)
            {
                // 2のべき乗まで切り上げる
                N = 1;
                while (N < n) N *= 2;

                this.All = Enumerable.Repeat(v, 2 * N - 1).ToArray();
                this.Part = Enumerable.Repeat(v, 2 * N - 1).ToArray();
            }

            /// <summary>
            /// [b,e)の範囲にvを加算する
            /// </summary>
            /// <param name="b"></param>
            /// <param name="e"></param>
            /// <param name="v"></param>
            /// <param name="k"></param>
            /// <param name="l"></param>
            /// <param name="r"></param>
            void Add(int b, int e, int v, int k, int l, int r)
            {
                if (b <= l && r <= e)
                {
                    // [l,r)が[b,e)に内包されていれば、全体に足すだけ
                    All[k] += v;
                }
                else if (l < e && b < r)
                {
                    // [l,r)が[b,e)と交差している
                    Part[k] += (Math.Min(e, r) - Math.Max(b, l)) * v;
                    Add(b, e, v, k * 2 + 1, l, (l + r) / 2);
                    Add(b, e, v, k * 2 + 2, (l + r) / 2, r);
                }
            }


            /// <summary>
            /// [b,e)の範囲にvを加算する
            /// </summary>
            /// <param name="b"></param>
            /// <param name="e"></param>
            /// <param name="v"></param>
            public void Add(int b, int e, int v)
                => Add(b, e, v, 0, 0, N);

            /// <summary>
            /// [b,e)の総和を求める
            /// </summary>
            /// <param name="b"></param>
            /// <param name="e"></param>
            /// <param name="k">index(0-based)</param>
            /// <param name="l"></param>
            /// <param name="r"></param>
            /// <returns></returns>
            long Sum(int b, int e, int k, int l, int r)
            {
                // [a,b)と[l,r)が交差していない
                if (e <= l || r <= b)
                {
                    return 0;
                }
                else if (b <= l && r <= e)
                {
                    // 内包している
                    return All[k] * (r - l) + Part[k];
                }
                else
                {
                    // 交差
                    long sum = (Math.Min(e, r) - Math.Max(b, l)) * All[k];
                    sum += Sum(b, e, k * 2 + 1, l, (l + r) / 2);
                    sum += Sum(b, e, k * 2 + 2, (l + r) / 2, r);

                    return sum;
                }
            }

            /// <summary>
            /// [b,e)の総和を求める
            /// </summary>
            /// <param name="b"></param>
            /// <param name="e"></param>
            /// <returns></returns>
            public long Sum(int b, int e)
                => Sum(b, e, 0, 0, N);

            /// <summary>
            /// 全体の総和
            /// </summary>
            /// <returns></returns>
            public long Sum()
                => Sum(0, N);
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();

            // 仮想的に標準入力をセットする
            // テスト入力したまま提出することがままあるので……
#if DEBUG
            //input.SetText("5 2 2 2 2 3");
#endif

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); }
                else { sr = new StreamReader(path); }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                    SetText(item);
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (string.IsNullOrEmpty(s)) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    queue.Enqueue(elem);

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any() => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine())) return false;

                if (!queue.Any()) return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read()) return "";

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                    if (!Read()) return false;

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n)) return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}