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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import java.util.*;

class Main{ 

	static class RMQ{
		int n;
		long[] data;
		RMQ(int nn){
			n=1;
			while(n<nn)n*=2;
			data = new long[n*2];
			Arrays.fill(data, Long.MAX_VALUE/10);
		}
		void set(int i, long v){
			i+=n-1;data[i]=v;
			for(i/=2;i>0;i/=2)data[i]=Math.min(data[2*i], data[2*i+1]);
		}
		long getMin(int a,int b){
			return getMin(a, b, 1, 1, n+1);
		}
		long getMin(int a, int b, int k, int l, int r){
			if(r<=a||b<=l)return Long.MAX_VALUE/10;
			if(a<=l&&r<=b)return data[k];
			return Math.min(getMin(a,b,2*k,l,(l+r)/2), getMin(a,b,2*k+1,(l+r)/2, r));
		}
	}

	static void solve(){
		int n = ni();
		long E = nl(), T=nl();
		long[] x = new long[n+3];
		for(int i=0;i<n;++i)x[i+2]=nl();
		x[n+2] = E;
		long[] dp = new long[n+2];
		RMQ larea = new RMQ(n+3);
		RMQ tarea = new RMQ(n+3);
		larea.set(1, -2*x[2]);
		tarea.set(1, T);
		int index = 1;
		for(int i=2;i<n+2;++i){
			while((x[i]-x[index+1])*2 > T)++index;
			dp[i] = Math.min(larea.getMin(1, index)+3*x[i], tarea.getMin(index, i)+x[i]);
			dp[i] = Math.min(dp[i], dp[i-1] + x[i]-x[i-1] + T);
			larea.set(i, dp[i]-2*x[i+1]-x[i]);
			tarea.set(i, dp[i]-x[i]+T);
		}
		out.println(dp[n+1] + E-x[n+1]);
	} 
 
 
 
 
	public static void main(String[] args){ 
		 solve(); 
		 out.flush(); 
	 } 
	 private static InputStream in = System.in; 
	 private static PrintWriter out = new PrintWriter(System.out); 
 
	 private static final byte[] buffer = new byte[1<<15]; 
	 private static int ptr = 0; 
	 private static int buflen = 0; 
	 private static boolean hasNextByte(){ 
		 if(ptr<buflen)return true; 
		 ptr = 0; 
		 try{ 
			 buflen = in.read(buffer); 
		 } catch (IOException e){ 
			 e.printStackTrace(); 
		 } 
		 return buflen>0; 
	 } 
	 private static int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1;} 
	 private static boolean isSpaceChar(int c){ return !(33<=c && c<=126);} 
	 private static int skip(){int res; while((res=readByte())!=-1 && isSpaceChar(res)); return res;} 
 
	 private static double nd(){ return Double.parseDouble(ns()); } 
	 private static char nc(){ return (char)skip(); } 
	 private static String ns(){ 
		 StringBuilder sb = new StringBuilder(); 
		 for(int b=skip();!isSpaceChar(b);b=readByte())sb.append((char)b); 
		 return sb.toString(); 
	 } 
	 private static int[] nia(int n){ 
		 int[] res = new int[n]; 
		 for(int i=0;i<n;++i)res[i]=ni(); 
		 return res; 
	 } 
	 private static long[] nla(int n){ 
		 long[] res = new long[n]; 
		 for(int i=0;i<n;++i)res[i]=nl(); 
		 return res; 
	 } 
	 private static int ni(){ 
		 int res=0,b; 
		 boolean minus=false; 
		 while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); 
		 if(b=='-'){ 
			 minus=true; 
			 b=readByte(); 
		 } 
		 for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); 
		 return minus ? -res:res; 
	 } 
	 private static long nl(){ 
		 long res=0,b; 
		 boolean minus=false; 
		 while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); 
		 if(b=='-'){ 
			 minus=true; 
			 b=readByte(); 
		 } 
		 for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); 
		 return minus ? -res:res; 
	} 
}