Under construction

Still being built. Things may change

Expedition Plans

Time limit10sMemory limit1024 MB

Summary
Given sailing, diving, and fixing costs along a cable, find the diagnosis order of repeaters that minimizes the worst-case total cost over all faulty segments.
Level

Medium6 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

The International Company of Pipes and Cables runs an internet cable across the Pacific. However, it has stopped working! But wait, no need to panic, this is not unexpected, being sometimes caused by shark attacks. The cable is composed of a sequence of N+1N + 1 segments, with a repeater between each pair of consecutive segments. Repeaters are identified by distinct integers from 11 to NN, from left to right, according to their positions within the cable.

A signal is transmitted from left to right along the cable. A repeater is said to be offline if no signal reaches the repeater; this indicates that there is a faulty segment before the repeater. On the contrary, a repeater is said to be online if the repeater receives data; this indicates that there is a failing segment after the repeater. The technical staff has determined that there is a single faulty segment. To locate and fix it, an expedition is required.

An expedition plan defines how the trip will happen based on the diagnoses that are made along the way. The picture above shows a possible scenario with N=3N = 3 repeaters (that is, N+1=4N + 1 = 4 segments). Five possible expedition plans for this case follow:

  1. Diagnose repeater 2. If it's offline, then diagnose repeater 1 to decide which of the first two segments is the faulty segment. On the contrary, if repeater 2 is online, then diagnose repeater 3 to decide which of the last two segments is failing.
  2. Diagnose in order repeaters 1, 2, and 3, stopping early when the failing segment is found.
  3. Diagnose in order repeaters 1, 3, and 2, stopping early when the failing segment is found.
  4. Diagnose in order repeaters 3, 1, and 2, stopping early when the failing segment is found.
  5. Diagnose in order repeaters 3, 2, and 1, stopping early when the failing segment is found.

The total cost of an expedition is composed of sailing cost, diving cost, and fixing cost. The sailing cost is proportional to the distance sailed. The diving cost for each dive is proportional to the depth of the repeater. The fixing cost for the failing segment depends on the terrain it is laid across. In our example, the costs of the first-mentioned expedition plan would be as follows:

  • If the first segment is failing, the expedition sails to repeater 2, dives, sails back to repeater 1, dives, and repairs the segment. So the sailing cost is 1+1=21 + 1 = 2, the diving cost is 8+3=118 + 3 = 11, and the fixing cost is 77, for a total of 2+11+7=202 + 11 + 7 = 20.
  • If the second segment is failing, the total cost is (1+1)+(8+3)+(1)=14(1 + 1) + (8 + 3) + (1) = 14.
  • If the third segment is failing, the total cost is (1+1)+(8+2)+(2)=14(1 + 1) + (8 + 2) + (2) = 14.
  • If the fourth segment is failing, the total cost is (1+1)+(8+2)+(12)=24(1 + 1) + (8 + 2) + (12) = 24.

Due to the high affinity between network disruptions and Murphy's Law, the most accurate cost estimation for an expedition plan is the maximum it can reach. That is, we should consider that the failing segment is always the one that makes the expedition have maximum cost. Thus, the estimated cost of the first expedition plan is 2424. Your task is to find the minimum estimated cost among all expedition plans. In our example, the expedition plan that diagnoses in order repeaters 1, 3 and 2 would have total costs of 1010, 1717, 1818, or 1919, depending on the failing segment. This means its estimated cost is 1919, which is the minimum among all expedition plans.

Input

The first line contains an integer NN (2≤N≤30002 \le N \le 3000) indicating the number of repeaters. The second line contains N−1N-1 integers S1,S2,…,SN−1S_1, S_2, \ldots, S_{N-1} (0≤Si≤1090 \le S_i \le 10^9 for i=1,2,…,N−1i = 1, 2, \ldots, N-1), where SiS_i is the cost of sailing between repeaters ii and i+1i + 1. The third line contains NN integers D1,D2,…,DND_1, D_2, \ldots, D_N (0≤Di≤1090 \le D_i \le 10^9 for i=1,2,…,Ni = 1, 2, \ldots, N), such that DiD_i is the cost of diving to repeater ii. The last line contains N+1N + 1 integers F1,F2,…,FN+1F_1, F_2, \ldots, F_{N+1} (0≤Fi≤1090 \le F_i \le 10^9 for i=1,2,…,N+1i = 1, 2, \ldots, N + 1), where FiF_i is the cost of fixing the ii-th segment.

Output

Output a single line with an integer indicating the minimum estimated cost among all expedition plans.

Examples2

  1. Example 1

    Input
    3
    1 1
    3 8 2
    7 1 2 12
    
    Expected output
    19
    
  2. Example 2

    Input
    2
    2
    5 1
    1 2 6
    
    Expected output
    12