Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Standard Round 8
  3. 26F

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

26F. Carpet Exhibition

3000 pts·3s·256 MB

Constraints

Input

Output

Samples

Input
t
2
Case 1
3
3 6 3
Case 2
3
3 3 6
Explanation
Case 1
Case 2
Output
Case 1
7
Case 2
6
Input
t
3
Case 1
3
8 10 3
Case 2
4
4 3 3 4
Case 3
1
7
Explanation
Case 1
Case 2
Case 3

With only one carpet, the whole corridor must be covered by that carpet.

Output
Case 1
11
Case 2
6
Case 3
7

You are given nnn carpets, numbered from 111 to nnn. Carpet iii has length aia_iai​.

You want to lay all the carpets along a straight corridor of the same width as the carpets. You may choose their positions and the order in which you lay them. Wherever carpets overlap, only the one laid last is visible from above.

Every carpet must lie entirely inside the corridor, without being cut or folded. Both endpoints of every carpet must be at integer distances from the left end of the corridor.

The carpets must cover the entire corridor. Viewed from above, the corridor must consist of exactly nnn consecutive sections, each of length at least 111. In the iii-th section from the left, only carpet iii must be visible.

Find the minimum possible length of the corridor.

  • 1≤t≤50001 \le t \le 50001≤t≤5000
  • 1≤n≤50001 \le n \le 50001≤n≤5000
  • 1≤ai≤1091 \le a_i \le 10^91≤ai​≤109
  • ∑n2≤25 000 000\sum n^2 \le 25\ 000\ 000∑n2≤25 000 000 over all test cases.

ttt

na1a2⋯an}×t\left. \begin{array}{l} n \\ a_1 \quad a_2 \quad \cdots \quad a_n \end{array} \right\} \times tna1​a2​⋯an​​}×t
answer}×t\left. \begin{array}{l} \mathrm{answer} \end{array} \right\} \times tanswer​}×t

The first arrangement can expose carpets 111, 222, and 333 on segments of lengths 111, 333, and 333, respectively. No corridor shorter than 777 can accommodate this top view.

A corridor of length 666 is sufficient for the second sequence, and it is impossible to use a shorter corridor because carpet 333 itself has length 666.

A corridor of length 111111 can display the three carpets in the required order. In particular, placing the longest carpet at the bottom is not necessary.

The four carpets can be arranged so that the required top view has total length 666; any shorter corridor cannot contain either carpet of length 444 together with the required visible parts.