Shortcut
Time limit2sMemory limit512 MB
Given a main railway line with side branches, add one express edge of length c between two main stations so the network diameter is as small as possible.
- Level
Hard8 of 10
- Topics
- Binary search, Two pointers, Prefix sum
- Solved
- No attempts yet
Problem
Pavel has a toy railway. It is very simple. The railway has one main line of stations, numbered from 0 to in order along the line. The distance between stations and is centimeters ().
Apart from the main line, there may be some secondary lines. Each secondary line connects a station on the main line to a new station that is not on the main line. (The new stations are not numbered.) At most one secondary line starts at each station of the main line. The length of the secondary line starting at station is centimeters. means no secondary line starts at station .

Pavel now plans to build one shortcut: an express line between two different stations (possibly neighbouring ones) of the main line. The express line has length exactly centimeters, regardless of which two stations it connects.
Each segment of the railway, including the new express line, can be used in both directions. The distance between two stations is the length of the shortest route between them along the railways. The diameter of the network is the maximum distance over all pairs of stations, that is, the smallest such that the distance between every pair of stations is at most .
Pavel wants to build the express line so that the diameter of the resulting network is minimized.
Constraints
In all subtasks, , , , and .