#P3580. [POI 2014] ZAL-Freight

    ID: 2631 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2014二分树状数组POI

[POI 2014] ZAL-Freight

Description

The train stations in Upper Bytown and Lower Bytown are connected with a single-track rail link.

It takes the train ss minutes to travel between them in either direction.

However, the trains departing a station have to be spaced at least one minute apart.

Moreover, at all times, all the trains on the rail link have to go in the same direction.

According to the timetable at our disposal, nn cargo trains destined for Lower Bytown are to pass through Upper Bytown. They are to be loaded with goods in Lower Bytown and then return to Upper Bytown. For simplicity, we assume that loading the goods on the train takes virtually no time.

We are to determine the minimum possible time of the last train returning to Upper Bytown.

In summary: there are two towns. It takes ss minutes to go from A to B. There are nn trains that need to go from A to B and then come back. At any moment, only trains moving in the same direction may be on the track. Each train has an earliest departure time from A. Find the earliest time by which all trains have returned to station A.

Input Format

The first line of the standard input contains two integers nn, ss (1n1 000 0001\le n\le 1\ 000\ 000, 1s1091\le s\le 10^9), separated by a single space, that specify the number of trains and the one-way travel time respectively.

The second line contains nn integers t1,t2,,tnt_1,t_2,\cdots,t_n (0t1t2tn1090\le t_1\le t_2\le\cdots\le t_n\le 10^9), separated by a single space, that specify the arrival times of successive trains at the Upper Bytown station.

Output Format

Print a single integer: the minimum possible time of the last train returning to Upper Bytown.

3 4
1 8 11

20

Hint

Translated by ChatGPT 5