#P9153. 「SvR-2」1+2=3(加强版)
「SvR-2」1+2=3(加强版)
Problem Description
You have some wooden sticks. Each stick has a number on the left and a number on the right. The numbers are natural numbers in . You need to connect all sticks together so that the number of adjacent pairs whose sum is is as large as possible.
For example, when , there are two sticks: and . If you connect them as , then the number of adjacent pairs whose sum is is . But if you connect them as $1\text{ - }\textcolor{red}{\underline{\textbf 2}},\textcolor{red}{\underline{\textbf 1}}\text{ - }0$, then the number of adjacent pairs whose sum is is , because .
Input Format
This problem has multiple test cases.
The first line contains a positive integer , the number of test cases.
For each test case, the first line contains a positive integer . Then follow lines, each containing integers. The -th integer in the -th line, , is the number of sticks of type .
Output Format
Output lines. Each line contains one integer, the answer.
1
3
4 1 3
4 7 7
9 10 3
31
Hint
For of the testdata, , , , .
Note: The testdata for this problem is relatively weak. If you have any hacks, please let the problem setter know.
Translated by ChatGPT 5