#P6167. [IOI 2016] shortcut

[IOI 2016] shortcut

Description

Pavel has a very simple toy railway. It has a main line with nn stations, numbered consecutively from 00 to n1n-1. Station 00 and station n1n-1 are at the two ends of the main line. The distance between station ii and station i+1i+1 is lil_i centimeters (0i<n10 \le i < n-1).

Besides the main line, the railway may have some branch lines. Each branch line consists of a new piece of track between one station on the main line and a new station outside the main line (these new stations are not numbered). Each station on the main line can have at most one branch line. The length of the branch starting from station ii on the main line is did_i centimeters. We use di=0d_i=0 to indicate that station ii has no branch line.

Pavel is now planning a shortcut: a fast track between two different stations on the main line (they may be adjacent). No matter which two stations it connects, the length of this fast track will be exactly cc centimeters.

Every segment of the railway, including the new fast track, can be traveled in both directions. The distance between any two stations is the length of the shortest path between them along the railway. The largest distance among all pairs of stations is called the diameter of the whole railway network. In other words, there exists a minimum value tt such that the distance between any two stations does not exceed tt.

Pavel wants to build one fast track so that, after adding it, the diameter of the new railway network is as small as possible.

Sample 1

4 10
10 20 20
0 40 0 30

The optimal solution is to build a fast track between station 11 and station 33, as shown in the figure below.

The diameter of the new railway network is 8080 centimeters, so you should output 8080.

Sample 2

9 30
10 10 10 10 10 10 10 10
20 0 30 0 0 40 0 40 0

The optimal solution is to connect station 22 and station 77. The diameter of this solution is 110110.

Sample 3

4 1
2 2 2
1 10 10 1

The optimal solution is to connect station 11 and station 22, which shortens the diameter to 2121.

Sample 4

3 3
1 1 
1 1 1

Building a fast track of length 33 between any two stations will not improve the diameter of the railway network, so the diameter remains the initial value 44.

Input Format

  • Line 1: Two integers nn and cc.

  • Line 2: Integers l0,l1,,ln2l_0,l_1,\cdots,l_{n-2}.

  • Line 3: Integers d0,d1,,dn1d_0,d_1,\cdots,d_{n-1}.

Output Format

Output one line: the minimum possible value of the diameter of the railway network after adding the new fast track.

4 10
10 20 20
0 40 0 30

80

Hint

For 100%100\% of the testdata, 2n1062 \le n \le 10^6, 1li1091 \le l_i \le 10^9, 0di1090 \le d_i \le 10^9, 1c1091 \le c \le 10^9.

Translated by ChatGPT 5