QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 256 MB Total points: 100

#10624. Lumberjacks

Statistics

Two lumberjacks (Bajtek and Bitek) working at the same speed will chop $n$ pieces of wood, initially arranged in a stack. Chopping the $i$-th piece of wood takes $a_i$ minutes. Whenever one of the lumberjacks finishes chopping their current piece, they take the next one from the stack. If both finish at the same time, Bajtek takes the next piece first.

Calculate the latest possible moment when all wood chopping is finished, if the pieces of wood are arranged in the stack in an exceptionally unfortunate order.

Input

The first line of input contains a single integer $n$ ($1 \le n \le 10^6$) representing the number of pieces of wood. The second line contains a sequence of $n$ positive integers $a_1, a_2, \dots, a_n$ representing the chopping times of the individual pieces of wood.

Let $A = a_1 + a_2 + \dots + a_n$ denote the sum of the chopping times. The inequality $A \le 5\,000\,000$ holds.

Output

In the only line of output, print a single integer representing the longest possible working time for the lumberjacks.

Examples

Input 1

3
2 3 1

Output 1

4

Note

The result 4 is achievable if, for example, the pieces of wood are in the order 1, 2, 3. Then Bajtek will start chopping piece 1, and Bitek will start with piece 2. After 1 minute, Bajtek will take piece 3, and finally, after 4 minutes, all pieces will be chopped.

Evaluation tests

  • Test 1: $n = 6$, $a_i = i$; the answer is 13.
  • Test 2: $n = 1000$, $a_i = 1$; the answer is 500.
  • Test 3: $n = 10$, $a_i = 2^{i-1}$; the answer is 767.
  • Test 4: $n = 2$, $a_i = 2\,500\,000$; the answer is 2\,500\,000.

Subtasks

Subtask Constraints Points
1 $n \le 10$ 5
2 $A \le 500$ 15
3 $A \le 10\,000$ 20
4 $A \le 100\,000$ 20
5 $A \le 1\,000\,000$ 20
6 no additional constraints 20

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.