Pavel has a toy railway. It is very simple. There is a single main line consisting of
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

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
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
Pavel wants to build the express line in such a way that the diameter of the resulting network is minimized.
Input Specification
Line
Line
Line
Sample Input 1
4 10
10 20 20
0 40 0 30
Sample Output 1
80
Explanation for Sample Output 1
The optimal solution is the build the express line between stations

The diameter of the new railway network is
Sample Input 2
9 30
10 10 10 10 10 10 10 10
20 0 30 0 0 40 0 40 0
Sample Output 2
110
Explanation for Sample Output 2
The optimal solution is to connect stations
Sample Input 3
4 1
2 2 2
1 10 10 1
Sample Output 3
21
Explanation for Sample Output 3
The optimal solution is to connect stations
Sample Input 4
3 3
1 1
1 1 1
Sample Output 4
4
Explanation for Sample Output 4
Connection any two stations with the express line of length
Subtasks
In all subtasks,
- (9 points)
- (14 points)
- (8 points)
- (7 points)
- (33 points)
- (22 points)
- (4 points)
- (3 points)
Comments