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 C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i<(n); i++)

using namespace std;

const int MAX = 310;
const long long INF = 1LL << 50;

long long G[MAX][MAX];
using pll = pair<long long, long long>;

int main(void) {
    int N, M;
    long long L;
    cin >> N >> M >> L;

    rep(i, MAX) {
        rep(j, MAX) G[i][j] = INF;
        G[i][i] = 0;
    }

    rep(i, M) {
        int a, b;
        long long c;
        cin >> a >> b >> c;
        --a;
        --b;
        G[a][b] = G[b][a] = c;
    }

    vector<vector<pll>> dist(N, vector<pll>(N, {INF, INF}));
    rep(s, N) {
        dist[s][s] = {0, 0};
        priority_queue<
                pair<pll, int>,
                vector<pair<pll, int>>,
                greater<pair<pll, int>>> que;

        que.push(make_pair(pll(0, 0), s));
        while (!que.empty()) {
            auto p = que.top();
            que.pop();

            long long v = p.second;
            long long kaisuu = p.first.first;
            long long used = p.first.second;

            if (p.first > dist[s][v])continue;

            rep(nv, N) {
                if (nv == v) continue;
                if (G[v][nv] > L) continue;

                long long nused = used + G[v][nv];
                pll np = {0, 0};
                if (nused > L) {
                    np = {kaisuu + 1, G[v][nv]};
                } else {
                    np = {kaisuu, nused};
                }

                if (dist[s][nv] > np) {
                    dist[s][nv] = np;
                    que.push(make_pair(np, nv));
                }
            }

        }
    }

    int Q;
    cin >> Q;
    while (Q--) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;

        long long res = dist[a][b].first;
        if (res >= INF) {
            cout << -1 << endl;
        } else {
            cout << res << endl;
        }
    }
}