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(unused_imports)]
#![allow(unused_variables)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(deprecated)]

use std::cell::{Cell, Ref, RefCell, RefMut};
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;
use std::usize;

const MOD_10_9_7: u64 = 1_000_000_007;
const INF: i64 = 1_000_000_000_000;
const MIN_INF: i64 = -1_000_000_000_000;

/// FYI: https://github.com/vain0x/scan-bench
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    buf.trim_right().to_owned()
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}

#[allow(unused_macros)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn a() {
    let (a, b) = read!(i32, i32);
    println!("{}", max(0, a - 2 * b));
}

fn b() {
    let n = read!(usize);
    let d: Vec<usize> = read![[usize]];

    let mut ans = 0;
    for i in 0..(n - 1) {
        for j in (i + 1)..n {
            ans += d[i] * d[j];
        }
    }
    println!("{}", ans);
}

fn c() {
    let n = read!(usize);
    let s: String = read!(String);

    let c: Vec<char> = s.chars().collect();

    let mut prev = c[0];
    let mut ans = 0;
    for i in 0..n {
        if prev == c[i] {
            continue;
        } else {
            ans += 1;
            prev = c[i];
        }
    }
    println!("{}", ans + 1);
}

fn d() {
    let n = read!(usize);
    let mut l: Vec<usize> = read![[usize]];
    l.sort();
    l.reverse();

    let mut dp = vec![0; 3000]; // lから2つ選んで和がxになるような組合せの数

    for i in 0..(n - 1) {
        for j in (i + 1)..n {
            let s = l[i] + l[j];
            dp[s] += 1;
        }
    }

    let mut under = vec![0; 3000];
    under[0] = 0;
    under[1] = 0;
    for i in 2..dp.len() {
        let t = under[i - 1];
        under[i] = t + dp[i];
    }

    let mut ans = n * (n - 1) * (n - 2) / 6;
    for i in 0..n {
        let max_v = l[i];
        ans -= under[max_v];
    }
    println!("{}", ans);
}

fn e() {
    unimplemented!();
}

fn f() {
    unimplemented!();
}

fn main() {
    d();
}