#P17334. 「TPOI-2D」Still Shining

    ID: 19612 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>贪心洛谷原创O2优化前缀和洛谷比赛斜率维护技巧 slope trick反悔贪心闵可夫斯基和 Minkowski sum

「TPOI-2D」Still Shining

Background

My reflection brightest sunlight

Ran too far forgot my name

Falling tears still shine like dreams

Turning the pain to brighter days

Even if I lose my way

As feelings fade they stay the same

May be beaten when I'm broken

But I know I'm still shining

——Mayonazy "Still Shining".

Problem Description

Given an array aa of length nn.

For a permutation pp of 1n1 \sim n, define $t(p)=\displaystyle\sum_{i=1}^{n}\sum_{j=\min(p_i,p_{i-1})}^{\max(p_i,p_{i-1})}a_j$. In particular, let p0=pnp_0=p_n.

Let SS be the set of all permutations of length nn. Find g(a)=minpS t(p)g(a)=\displaystyle\min_{p \in S}\ t(p).

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

The first line contains a positive integer nn.

The second line contains nn integers aia_i.

Output Format

Output TT lines, each containing one integer, which is the value of g(a)g(a).

2
3
1 -2 3
66
-70 68 -92 74 52 53 -5 -94 1 68 -42 -14 25 6 51 -98 -72 94 -69 -3 -13 75 29 62 74 -33 -3 -32 75 29 -2 -79 -99 -81 14 42 92 -28 -67 -40 -96 35 8 -5 83 -81 5 95 -71 78 54 55 -77 6 71 18 71 -15 -74 98 69 -36 18 -33 10 -79

2
-5788

Hint

[Sample Explanation]

For the first test case, you can construct the permutation p=[1,2,3]p=[1,2,3].

[Constraints]

This problem uses bundled testdata.

Subtask\text{Subtask} Score Special Property
11 1515 n8,T100n\le8, T\leq 100
22 ai>0a_i>0
33 ai<0a_i<0
44 3535 n1000n\le1000
55 2020 None

For 100%100\% of the testdata, it is guaranteed that 1T1041 \le T \le 10^4, 1n2×1051 \le n \le 2\times10^5, n2×106\sum n \le 2\times10^6, and ai109|a_i|\le10^9.

Translated by ChatGPT 5