QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#10280. Graphs, Distances, Optimization

Statistics

Given $n$ non-negative integers $x_1, x_2, \dots, x_n$.

For any undirected connected graph $G$ with $n$ nodes labeled from $1$ to $n$, its score is defined as: $$\text{score}(G) = \sum_{i=1}^{n} \sum_{j=i+1}^{n} \text{dist}_G(i, j)x_ix_j$$ where $\text{dist}_G(i, j)$ denotes the shortest path distance between $i$ and $j$ in graph $G$.

Your task is to output the maximum score among all undirected connected graphs with $n$ nodes.

Input

The input is read from standard input.

There are multiple test cases. The first line contains an integer $t$ ($1 \le t \le 300$), representing the number of test cases.

Each test case is described as follows: The first line contains an integer $n$ ($1 \le n \le 300$). The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 \le x_i \le 2,000$) describing the sequence $x$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $300$.

Output

Output to standard output.

For each test case, output a single integer on a new line, representing the maximum score among all undirected connected graphs.

Examples

Input 1

3
2
1 2
4
1 0 1 1
7
1 2 3 4 5 6 7

Output 1

2
6
1044

Note 1

For the first test case, there is only one valid configuration $G = \{(1, 2)\}$. For the second test case, one optimal configuration is $G = \{(1, 2), (2, 3), (2, 4)\}$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.