PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000188. Hai người cùng đọc sách (Reading Books)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hai người cùng đọc sách (Reading Books)

Reading Books

Source: CSES

Version: Phuoc Hung OJ (single-case adaptation where applicable)

Problem Statement

There are nn books, book ii requiring tit_i time units to read completely. Two readers must each read every book. They cannot read the same book simultaneously, and each person reads at most one book at a time; the orders may differ. Find the earliest time by which both finish.

Input

The first line contains nn; the next line contains nn positive integers tit_i.

Output

Print the minimum total time.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤ti≤1001\le t_i\le 100 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 1≤ti≤10000001\le t_i\le 1000000 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ti≤10000000001\le t_i\le 1000000000 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

3
2 8 3

Output

16

Explanation

The sum of all reading times is 13, but the longest book takes 8 units and must be read by both people at separate times. Hence at least 16 units are needed, and a schedule achieving 16 exists.

Example 2

Input

4
1 2 3 4

Output

10

Explanation

The sum is 10 and twice the longest time is 8. By interleaving the orders, both readers can finish after exactly 10 units.