QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#310. Freight

Statistics

The train stations in Upper Bytown and Lower Bytown are connected with a single track rail link. It takes the train s 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, $n$ cargo trains destined for Lower Bytown are to pass through the 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.

Input Format

The first line of the standard input contains two integers $n$, $s$ ($1 ≤ n ≤ 1\,000\,000$, $1 ≤ s ≤ 10^{9}$), separated by a single space, that specify the number of trains and the one-way travel time respectively. The second line contains $n$ integers $t_{1},t_{2},…,t_{n}$($0 ≤ t_{1} ≤ t_{2} ≤ … ≤ t_{n} ≤ 10^{9}$), separated by a single space, that specify the arrival times of successive trains at the Upper Bytown station.

In test worth $50\%$ of the total score, $n ≤ 5\,000$ holds. Moreover, in their subset worth $25\%$ of the total score, $n ≤ 400$ holds in addition.

Output Format

Your program should print out a single line with a single integer to the standard output: the minimum possible time of the last train returning to Upper Bytown.

Example

Input

3 4
1 8 11

Output

20

To attain the minimum time, the trains can depart from Upper Bytown at times 1, 9, and 11, and from Lower Bytown at times 5, 15, and 16.

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.