C. Coloring Game

Problem Statement

Alice and Bob are playing a game using an integer array aa of size nn.

Initially, all elements of the array are colorless. First, Alice chooses 3 elements and colors them red. Then Bob chooses any element and colors it blue (if it was red — recolor it). Alice wins if the sum of the red elements is strictly greater than the value of the blue element.

Your task is to calculate the number of ways that Alice can choose 3 elements in order to win regardless of Bob’s actions.

Input

The first line contains a single integer tt (1t1000)(1 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains a single integer nn (3n5000)(3 \leq n \leq 5000).

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1a1a2an105)(1 \leq a_1 \leq a_2 \leq \cdots \leq a_n \leq 10^5).

Additional constraint on the input: the sum of nn over all test cases doesn’t exceed 5000.

Output

For each test case, print a single integer — the number of ways that Alice can choose 3 elements in order to win regardless of Bob’s actions.

Solution

First, sort the array aa in non-decreasing order. This ensures that for any three elements ai,aj,aka_i, a_j, a_k (where i<j<ki < j < k), we have aiajaka_i \leq a_j \leq a_k.

For any three elements ai,aj,aka_i, a_j, a_k (where i<j<ki < j < k), Alice can guarantee a win if the following conditions are met:

  1. ai+aj>aka_i + a_j > a_k (this ensures that the sum of the red elements is strictly greater than the blue element). This condition handles the case when Bob chooses the largest element aka_k as the blue element.
  2. ai+aj+ak>ana_i + a_j + a_k > a_n. This ensures that the sum of the red elements is strictly greater than the largest element in the array, which is ana_n. This condition handles the case when Bob chooses the largest element ana_n as the blue element.

As long as these two conditions are satisfied, Alice can guarantee a win regardless of Bob’s choice.

We use two nested loops to iterate over all pairs aia_i and aja_j and check if there exists an aka_k such that both conditions are satisfied. To find aka_k, we can use binary search to find the largest and smallest aka_k that satisfies the conditions.

submission link