Under construction

Still being built. Things may change

Return Pickup

Time limit1sMemory limit1024 MB

Summary
Find the minimum time for a truck starting at position 0 to collect every house's item after its release time and return to 0.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

문제

As shown in the figure below, NN houses are on a straight road, numbered 11 to NN from left to right. The position of house ii (1≤i≤N1 \le i \le N) is XiX_i (Xi>0X_i > 0).

A delivery company uses one truck to visit all NN houses and collect returned items. The truck leaves the company at position 00 at time 00. House ii puts out its returned item at time TiT_i. The truck moves at speed 11, so moving a distance dd takes dd time. If needed, the truck can stop and wait in place.

When the truck passes the position of a house with a returned item, it collects the item instantly. Collecting an item takes 00 time. So the truck can collect the item from house ii if it passes position XiX_i at time TiT_i or later.

Given the house positions and the times when each item is put out, write a program that finds the minimum time for the truck to collect all items and return to the company.

입력

The first line contains NN, the number of houses with returned items.

The second line contains the positions X1,X2,⋯ ,XNX_1, X_2, \cdots, X_N, separated by spaces.

The third line contains the times T1,T2,⋯ ,TNT_1, T_2, \cdots, T_N at which each house puts out its item, separated by spaces.

출력

Print the minimum time for the truck to collect all items and return to the company.

제한

  • All given numbers are integers.
  • 1≤N≤30001 \le N \le 3000
  • 1≤X1<X2<⋯<XN≤1081 \le X_1 < X_2 < \cdots < X_N \le 10^8
  • 0≤Ti≤1080 \le T_i \le 10^8 (1≤i≤N1 \le i \le N)

Examples2

  1. Example 1

    Input
    4
    2 5 7 10
    20 4 16 11
    
    Expected output
    23
    
  2. Example 2

    Input
    3
    1 2 3
    3 2 1
    
    Expected output
    6