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 (Clang 3.8.0) (judge verdict: Accepted)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

template<typename T> class segtree {
private:
    int n,sz,h; vector<T> node, lazy_update, lazy_add; vector<bool> lazyFlag;
public:
    segtree(const vector<T>& v) : n(1), sz((int)v.size()), h(0){
        while(n < sz) n *= 2, h++;
        node.resize(2*n, numeric_limits<T>::max());
        lazy_update.resize(2*n, 0); lazyFlag.resize(2*n, false);
        lazy_add.resize(2*n, 0);
        for(int i = 0; i < sz; i++) node[i+n] = v[i];
        for(int i=n-1; i>=1; i--) node[i] = min(node[2*i],node[2*i+1]);
    }
    void eval(int k) {
        if(lazyFlag[k]){
            lazy_update[k] += lazy_add[k];
            node[k] = lazy_update[k];
            if(k < n) {
                lazy_add[2*k] = lazy_add[2*k+1] = 0;
                lazy_update[2*k] = lazy_update[2*k+1] = lazy_update[k];
                lazyFlag[2*k] = lazyFlag[2*k+1] = true;
            }
            lazy_add[k] = 0, lazyFlag[k] = false;
        }else if(lazy_add[k] != 0){
            node[k] += lazy_add[k];
            if(k < n){
                lazy_add[2*k] += lazy_add[k]; lazy_add[2*k+1] += lazy_add[k];
            }
            lazy_add[k] = 0;
        }
    }
    void update(int a, int b, T x, int k=1, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            lazy_add[k] = 0; lazy_update[k] = x; lazyFlag[k] = true; eval(k);
        }else{
            update(a, b, x, 2*k, l, (l+r)/2); update(a, b, x, 2*k+1, (l+r)/2, r);
            node[k] = min(node[2*k],node[2*k+1]);
        }
    }
    void add(int a, int b, T x, int k=1, int l=0, int r=-1){
        if(r < 0) r = n;
        eval(k);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            lazy_add[k] += x; eval(k);
        }else{
            add(a, b, x, 2*k, l, (l+r)/2); add(a, b, x, 2*k+1, (l+r)/2, r);
            node[k] = min(node[2*k],node[2*k+1]);
        }
    }
    T query(int a, int b) {
        a += n, b += n - 1;
        for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
        b++;
        T res1 = numeric_limits<T>::max(), res2 = numeric_limits<T>::max();
        while(a < b) {
            if(a & 1) eval(a), res1 = min(res1, node[a++]);
            if(b & 1) eval(--b), res2 = min(res2, node[b]);
            a >>= 1, b >>= 1;
        }
        return min(res1, res2);
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};


int main(){
    ll n,e,t;
    cin >> n >> e >> t;
    //if(n>=2001)return 0;
    vector<ll> a(n+1);
    a[0] = 0;
    rep(i,n){
        cin >> a[i+1];
    }
    a.push_back(e);
    n+=2;
    vector<ll>dp(n,1LL<<60);
    segtree<ll> sg(dp);
    int p = 0;
    rep(i,n-1){
        if(i==0){
            sg.update(0,1,a[1]-a[0]);
            continue;
        }
        ll k = sg.query(0,i);
        //k += a[i+1]-a[i];
        sg.update(i,i+1,k+t);
        
        // rep(j,i-1){
        //     dp[i] = min(dp[i],dp[j]+(a[j+1]-a[j])+max(3*(a[i]-a[j+1]),t+(a[i]-a[j+1])));
        // }
        sg.add(0,p,3*(a[i+1]-a[i]));
        sg.add(p,i+1,(a[i+1]-a[i]));
        while(1){
            if(3*(a[i+1]-a[p+1]) > t+(a[i+1]-a[p+1])){
                sg.add(p,p+1,-(t+(a[i+1]-a[p+1])));
                sg.add(p,p+1,3*(a[i+1]-a[p+1]));
                p++;
            }else{
                break;
            }
        }
        cerr << p << endl;
        // if(i!=n-1){
        //     dp[i] = min(dp[i],dp[i-1]+a[i]-a[i-1]+t);
        //     k = min(k,sg.query(i-1,i)+a[i]-a[i-1]+t);
        // }else{
        //     dp[i] = min(dp[i],dp[i-1]+a[i]-a[i-1]);    
        // }
        //sg.print();
    }
    cout << sg.query(n-2,n-1) << endl;
    return 0;
}