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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
	"strconv"
)

func main() {
	n := readInt()

	m := make(map[int]map[int]struct{})

	l := make([]int, n)
	for i := range l {
		l[i] = readInt()
	}
	sort.Sort(sort.Reverse(sort.IntSlice(l)))

	uniqueL := make([]int, 0)
	mm := make(map[int]struct{})
	for i := range l {
		if _, ok := mm[l[i]]; !ok {
			uniqueL = append(uniqueL, l[i])
			mm[l[i]] = struct{}{}
		}
	}

	vToIdx := make(map[int]int)
	for i := range uniqueL {
		vToIdx[uniqueL[i]] = i
	}

	// fmt.Println(uniqueL)

	ans := 0
	for i := range l {

		for j := i + 1; j < n-1; j++ {
			if l[j+1] <= l[i]-l[j] {
				break
			}
			if _, ok := m[l[i]][l[j]]; ok {
				continue
			}

			if _, ok := m[l[i]]; !ok {
				m[l[i]] = make(map[int]struct{})
			}
			m[l[i]][l[j]] = struct{}{}

			s := vToIdx[l[j+1]]
			// fmt.Println(l[i], l[j])
			e := upperBound(uniqueL, s, len(uniqueL)-1, l[i]-l[j])

			// fmt.Printf("\t%v:%v, %v:%v  %v\n", s, uniqueL[s], e, uniqueL[e], uniqueL[s:e+1])

			ans += e - s + 1
		}
	}

	fmt.Println(ans)
}

func upperBound(a []int, l, r, v int) int {
	for l < r {
		if a[r] > v {
			return r
		}
		m := (l + r) / 2
		if a[m] > v {
			if l == m {
				return l
			}
			l = m
		} else {
			r = m - 1
		}
	}
	return l
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

func (s sortArray) Len() int           { return len(s) }
func (s sortArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s sortArray) Less(i, j int) bool { return s[i].x < s[j].x }

// -----------------------------------------------------------------

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}