Problem:
Score : 1200 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

Imagine a game played on a line. Initially, the player is located at position 0 with N candies in his possession, and the exit is at position E. There are also N bears in the game. The i-th bear is located at x_i. The maximum moving speed of the player is 1 while the bears do not move at all.

When the player gives a candy to a bear, it will provide a coin after T units of time. More specifically, if the i-th bear is given a candy at time t, it will put a coin at its position at time t+T. The purpose of this game is to give candies to all the bears, pick up all the coins, and go to the exit. Note that the player can only give a candy to a bear if the player is at the exact same position of the bear. Also, each bear will only produce a coin once. If the player visits the position of a coin after or at the exact same time that the coin is put down, the player can pick up the coin. Coins do not disappear until collected by the player.

Shik is an expert of this game. He can give candies to bears and pick up coins instantly. You are given the configuration of the game. Please calculate the minimum time Shik needs to collect all the coins and go to the exit.

Constraints

1 \leq N \leq 100,000

1 \leq T, E \leq 10^9

0 < x_i < E

x_i < x_{i+1} for 1 \leq i < N

All input values are integers.

Partial Scores

In test cases worth 600 points, N \leq 2,000.

Input

The input is given from Standard Input in the following format:

N E T
x_1 x_2 ... x_N

Output

Print an integer denoting the answer.

Sample Input 1

3 9 1
1 3 8

Sample Output 1

12

The optimal strategy is to wait for the coin after treating each bear. The total time spent on waiting is 3 and moving is 9. So the answer is 3 + 9 = 12.

Sample Input 2

3 9 3
1 3 8

Sample Output 2

16

Sample Input 3

2 1000000000 1000000000
1 999999999

Sample Output 3

2999999996

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1<<20;
ll n, t, e, x[maxn], dp[maxn];
int main() {
	dp[0] = x[0] = 0;
	cin >> n >> e >> t;
	for(int i = 1; i <= n; i++) cin >> x[i];
	int j = 0;
	multiset<ll> p, s;
	for(int j = 1, i = 1; i <= n; i++) {
		while(2*(x[i]-x[j]) > t) {
			s.erase(s.find(dp[j-1] - x[j] + (x[j] - x[j-1])));
			p.insert(dp[j-1] - 3ll*x[j] + (x[j] - x[j-1]));
			//cout << j << " p " << (dp[j-1] - 3ll*x[j] + (x[j] - x[j-1])) << '\n';
			j++;
		}
		dp[i] = dp[i - 1] + (x[i] - x[i-1]) + t;
		if(!s.empty()) dp[i] = min(dp[i], *s.begin() + x[i] + t);
		if(!p.empty()) dp[i] = min(dp[i], *p.begin() + 3ll*x[i]);
		s.insert(dp[i-1] - x[i] + (x[i] - x[i-1]));
		//cout << i << " s " << (dp[i-1] - x[i] + (x[i] - x[i-1])) << '\n';
		//cout << i << " " << dp[i] << " " << x[i] << " " << t << "\n";
	}
	cout << dp[n]+(e-x[n]);
}