Pavel has a toy railway. It is very simple. There is a single main line consisting of stations. These stations are numbered from to in order along the line. The distance between the stations and is centimeters ().
Apart from the main line there may be some secondary lines. Each secondary line is a railway line between a station on the main line and a new station that does not lie on the main line. (These new stations are not numbered.) At most one secondary line can start in each station of the main line. The length of the secondary line starting at station is centimeters. We use to denote that there is no secondary line starting at station .
Pavel is now planning to build one shortcut: an express line between two different (possibly neighbouring) stations of the main line. Express line will have length of exactly centimeters, regardless of what two stations it will connect.
Each segment of the railway, including the new express line, can be used in both directions. The distance between two stations is the smallest length of a route that goes from one station to the other along the railways. The diameter of the whole railway network is the maximum distance among all pairs of stations. In other words, this is the smallest number , such that the distance between every pair of stations is at most .
Pavel wants to build the express line in such a way that the diameter of the resulting network is minimized.
Implementation details
You should implement the function
long long find_shortcut(int n, int l[], int d[], int c)
n
: number of stations on the main line,l
: distances between stations on the main line (array of length ),d
: lengths of secondary lines (array of length ),c
: length of the new express line.- the function should return the smallest possible diameter of the railway network after adding the express line.
Examples
Example 1
For the railway network shown above, the grader would make the following function class:
find_shortcut(4, {10, 20, 20}, {0, 40, 0, 30}, 10)
The optimal solution is the build the express line between stations and , as shown below.
The diameter of the new railway network is centimeters, so the function should return .
Example 2
The grader makes the following function call:
find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10}, {20, 0, 30, 0, 0, 40, 0, 40, 0}, 30)
The optimal solution is to connect stations and , in which case the diameter is .
Example 3
The grader makes the following function call:
find_shortcut(4, {2, 2, 2}, {1, 10, 10, 1}, 1)
The optimal solution is to connect stations and , reducing the diameter to .
Example 4
The grader makes the following function call:
find_shortcut(3, {1, 1}, {1, 1, 1}, 3)
Connection any two stations with the express line of length does not improve the initial diameter of the railway network which is .
Subtasks
In all subtasks, , , , .
- (9 points)
- (14 points)
- (8 points)
- (7 points)
- (33 points)
- (22 points)
- (4 points)
- (3 points)
Comments