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: Accepted)

// 未回答: TLE
// 標準入力から一行を読み取り、指定の型に変換する
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
    
// 標準入力から一行を読み取り、空白文字で分割し、各要素を指定の型に変換する関数
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

fn lower_bound(v: &Vec<i32>, start: usize, key: i32) -> i32 {
    let mut result_index: i32 = v.len() as i32;

    for i in start..v.len()
    {
        if v[i] >= key
        {
            result_index = i as i32;
            break;
        }
    }

    result_index
}
    
fn main() {
    let number: i32 = read();
    let mut sticks: Vec<i32> = read_vec();
    
    let mut count_of_triangles: i32 = 0;

    // ソート
    sticks.sort();
    
    //a < b < cのとき、a + b > c
    for b in 0..(number - 1) as usize 
    {
        for a in 0..b as usize 
        {
            let ab: i32 = sticks[a] + sticks[b];
            let end_index: i32 = lower_bound(&sticks, b, ab);
            let begin_index: i32 = (b + 1) as i32;
            count_of_triangles += end_index - begin_index;
            //println!("a_index:{} b_index:{} ab_value:{} end_index:{} begin_index:{} count:{}", a, b, ab, end_index, begin_index, count_of_triangles);
        }
    }
    
    println!("{}", count_of_triangles);
}