#P7176. [COCI 2014/2015 #4] PRIPREME

[COCI 2014/2015 #4] PRIPREME

Description

Ante and Goran are preparing nn teams. Each of them has one algorithm that needs to be explained to all teams.

Of course, they cannot both explain to the same team at the same time, and neither of them can explain to multiple teams at the same time.

Given the time needed to explain to each team, determine the minimum total time required.

Input Format

The first line contains an integer nn, the number of teams.

The next line contains nn space-separated integers aia_i, where aia_i is the time needed to explain to team ii.

Output Format

Output a single line: the minimum total time required.

3
2 2 2
6
3
4 1 2
8
4
1 3 2 1
7

Hint

Explanation for Sample 1

Each team needs 22 units of time to understand and implement an algorithm. The following is one feasible teaching plan.

Ante Goran Time
Team 1 Team 2 22
Team 2 Team 3
Team 3 Team 1
All finished 66

Explanation for Sample 2

One optimal schedule is that Ante teaches teams 2,3,x,12, 3, {\color{red}x}, 1 in order, but there is a 11 unit time pause at x\color{red}x. Goran will teach teams 1,3,21, 3, 2 in order.

Constraints

  • For 40%40\% of the testdata, 1n71 \le n \le 7.
  • For 100%100\% of the testdata, 1n3×1051 \le n \le 3 \times 10^5.

For all valid aia_i, ai[1,3×105]a_i \in [1, 3 \times 10^5].

Notes

This problem is translated from COCI2014-2015 CONTEST #4 T3 PRIPREME.

Translated by ChatGPT 5