Excruciating Elevators
문제
It is the 25th of May, 3025. The EEMCS building at TU Delft has grown to floors beyond the ground floor! The floors are now numbered , but there are still only four elevators. Moreover, the elevators have malfunctioned and are currently turned off. As employee of the Building Ascension Plans Company, you are tasked to fix the elevators.
You first have to order some components, which will take a month to arrive at the ground floor. After collecting the components, you must visit floors in order. At each floor , you must replace some component, which takes seconds. After replacing the final component, the elevators will be fixed.
The stairs have long been removed, since people got tired of walking up millions of flights of stairs. You thus have to use the elevators to travel between the floors. You can turn on the elevators, but when you turn one on, you can no longer turn it off. Once turned on, an elevator will move up and down between floors and indefinitely. The elevators move at a speed of one floor every second without stopping, but you are agile enough to enter and exit.
You know exactly when the components will arrive. In the meantime, you can determine when to turn on each elevator. This timing determines the starting configuration of each elevator when the components arrive. Since you have a month, you can enforce any arbitrary starting configuration of the elevators. What is the minimum possible time to fix the elevators?
As an example, consider the first sample input. You can ensure one elevator starts at ground floor going up, and another starts at floor going up. Entering the former elevator immediately, you arrive at floor after seconds. After more seconds, you finish replacing the component at floor . In the meantime, the latter elevator reached the top floor after seconds, when it started going down. Now, seconds later, this elevator is at floor going down. You can enter this elevator immediately and exit seconds later at floor . Finally, after seconds, you finish replacing the component at floor , and the elevators are fixed! The total time is seconds.
입력
The input consists of:
- One line with an integer (), the number of floors to visit.
- One line with integers ( for each ), the numbers of the floors you must visit.
- One line with integers ( for each ), the necessary time spent on each floor in seconds.
No two consecutive floors are equal, and is non-zero.
출력
Output the minimum possible time in seconds to fix the elevators after the components have arrived and you start at the ground floor.
예제
예제 1
2 600000 400000 50000 150000
1000000
예제 2
10 1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 1 1 1 1 1
4000012