Scouts

아직 제출이 없습니다시간 제한9초메모리 제한256 MB

문제

Johnny's boy scout team is facing a change of its command structure. The team has nn scouts, numbered from 11 to nn. The command structure is defined as follows:

  • The command structure has a commander.
  • Scouts with numbers lower than their commander's comprise the first subteam, and those with numbers higher than their commander's comprise the second subteam; one or both subteams may be empty.
  • Nonempty subteams have their own command structures.
  • The commander of a team is the supervisor of commanders of its subteams.

The command structure will be used to convey one, very important message: the message is communicated from outside to one of the scouts, who reads it and then passes it on to his supervisor; this procedure is repeated until the message reaches the supervisor of the whole team. Different scouts take different amounts of time to read the message: the ii-th scout takes a_ia\_i time. The time it takes to handle the message is the sum of times it takes for the message to be read by all the scouts who passed it (including the commander of the entire team).

Unfortunately, it is not known which scout is going to receive the message. Johnny should reorganize the command structure of the team so as to minimize the maximum possible time of handling the message. Help him. Write a program which reads the number of scouts and the time it takes each of them to read the message, computes the minimum possible maximum time of handling the message and writes the result to standard output.

입력

The first line of input contains one integer nn (1n2,0001 \le n \le 2\\,000) --- the number of scouts. The second line of input contains a description of the team. It consists of nn space-separated integers a_ia\_i (1a_i1,000,000,0001 \le a\_i \le 1\\,000\\,000\\,000), which are the times of reading the message by the respective scouts.

출력

Your program should write one integer --- the minimum possible maximum time it can take to handle the message.

힌트

In sample 1, optimum command structure is shown below at the left. The numbers in vertices correspond to reading times for the respective scouts. The maximum time of handling the message is 1313.

 

In sample 2, an optimum command structure is shown above at the right. The numbers in vertices correspond to reading times for the respective scouts. The maximum time of handling the message is 44.