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)

#![allow(non_snake_case)]
fn main() {

    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n:usize = sc.read();
    let mut l:Vec<i32> = sc.vec(n);
    // let t:i32 = sc.read();
    let mut ans:i64 = 0;
    l.sort();
    // println!("{}", bisect_left(&l,t,n));
    // println!("{}", bisect_right(&l,t,n));
    for i in 0..(n-2) {
        for j in (i+1)..(n-1) {
            ans += bisect_right(&l, l[i] + l[j] - 1, n) as i64 -j as i64-1;
        }
    }
    println!("{}", ans);
    
}


// fn bisect_left(v: &[i32], t: i32, n: usize)->i32{
//     if v[0] > t{
//         return 0
//     }
//     let mut l:usize =0;
//     let mut r:usize =n;
//     let mut m;
//     while r-l >= 2{
//         m = (l+r)/2;
//         if v[m] >= t{
//             r = m;
//         }else{
//             l = m;
//         }
//     }
//     r as i32
// }

fn bisect_right(v: &[i32], t: i32, n: usize)->i32{
    if v[0] > t{
        return 0
    }
    let mut l:usize =0;
    let mut r:usize =n;
    let mut m;
    while r-l >= 2{
        m = (l+r)/2;
        if v[m] <= t{
            l = m;
        }else{
            r = m;
        }
    }
    r as i32
}


pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
        .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}