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 Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(non_snake_case)]
use std::io::{stdin, Read};
use std::str::FromStr;
fn read_option<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok()
}
fn read<T: FromStr>() -> T {
    let opt = read_option();
    opt.expect("failed to parse token")
}

fn main() {
    let N: usize = read();
    let mut vec: Vec<usize> = vec![];
    for _ in 0..N {
        vec.push(read());
    }
    vec.sort();
    let counts_size = 1001;
    let mut counts = vec![0_i64; counts_size];
    for v in vec.iter() {
        counts[*v] += 1;
    }
    let mut now_val = 0;
    let mut counts2 = vec![0; counts_size];
    for i in 0..counts_size {
        now_val += counts[i];
        counts2[i] = now_val;
    }
    let mut ans: i64 = 0;
    let mut counts3 = vec![0; counts_size];
    for i in 0..N {
        for j in i + 1..N {
            let a = vec[i];
            let b = vec[j];
            let sum = std::cmp::min(a + b, counts_size);
            let sum_index = match vec.binary_search(&sum) {
                Ok(a) => a,
                Err(b) => b,
            };
            // b <= c < sum となるcの数をO(1)で探す
            // counts[b]が2以上だと、本来b==cで数え上げられるべきものも以下では数えられない
            // だからと言って毎回数えてはいけない。counts[b]が2以上のものは消費されていく
            // counts[b]が2以上でも、それがaとbの場合は数えてはいけない
            // let increment_count = counts2[sum - 1] - counts2[b];
            // if counts[b] >= 2 && a != b {
            //     ans += std::cmp::max(counts[b] - 1 - counts3[b], 0);
            //     counts3[b] += 1;
            // }
            let increment_count = std::cmp::max((sum_index - j - 1) as i64, 0);
            assert_eq!(increment_count >= 0, true);
            ans += increment_count;
        }
    }
    println!("{}", ans);
}