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)

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8

macro_rules! input {
    (source = $s:expr; $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
#[allow(unused_imports)]
use std::cmp::*;
trait Each: Iterator {
    fn each<F>(self, mut f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item),
    {
        self.fold((), move |(), item| f(item));
    }
}
impl<T: Iterator> Each for T {}

const SEPARATOR: &'static str = " ";

fn solve(source: &str) -> String {
    let mut result: Vec<String> = vec![];
    macro_rules! p {
        ([$x: expr]) => {
            $x.iter().each(|&x| p!(x))
        };
        ([$x: expr]; $s:expr)=>{
            p!($x.iter().map(|x|x.to_string()).collect::<Vec<_>>().join($s));
        };
        ($($x: expr),* $(,)*) => {
            $(result.push($x.to_string()));*
        };
    }
    //---------------------------------------------------------------//

    input! {
        source = source;
        n:usize,
        mut l:[i64;n]
    }
    let mut ans = 0;
    l.sort();
    for i in 0..n {
        for j in i + 1..n {
            let lower = l[j] - l[i];
            let upper = l[i] + l[j];

            let mut low = l.binary_search(&lower).unwrap_or_else(|x| x);
            while l[low] == lower {
                low += 1;
            }
            let mut high = l.binary_search(&upper).unwrap_or_else(|x| x-1);
            /* if n == high {
                high -= 1;
            } */
            while high < n && l[high] == upper {
                high -= 1;
            }
            // dbg!((l[i], l[j], low, high));
            ans += high - low;

            if l[i] * 2 > l[j] {
                ans -= 1;
            }
            // dbg!(ans);
        }
    }
    ans /= 3;
    p!(ans);
    //---------------------------------------------------------------//
    return result.join(SEPARATOR);
}
fn main() {
    let mut s;
    let source = {
        use std::io::Read;
        s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s.as_str()
    };
    println!("{}", solve(source));
}
#[test]
fn testcase() {
    assert_eq!(
        solve(
            r"4
3 4 2 1
"
        ),
        r"1"
    );
    assert_eq!(
        solve(
            r"3
1 1000 1
"
        ),
        r"0"
    );
    assert_eq!(
        solve(
            r"7
218 786 704 233 645 728 389
"
        ),
        r"23"
    );
}