#include <bits/stdc++.h>
constexpr long long mod = 998244353ll;
constexpr double PI = 3.141592653589793238462643383279;
using namespace std;
inline long long modpow(long long x, long long y, long long m = mod) {
  long long res = 1;
  while (y) {
    if (y & 1) {
      res *= x;
      res %= m;
    }
    x = x * x % m;
    y /= 2;
  }
  return res;
}
long long mypow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
    }
    x = x * x;
    y /= 2;
  }
  return res;
}
bool prime(long long x) {
  if (!x || x == 1) {
    return false;
  }
  if (x == 2) {
    return true;
  }
  if (!(x & 1)) {
    return false;
  }
  for (long long i = 3; i * i <= x; i += 2) {
    if (!(x % i)) {
      return false;
    }
  }
  return true;
}
inline long long gcd(long long x, long long y) {
  if (!y) {
    return x;
  }
  return gcd(y, x % y);
}
long long keta(long long x) {
  long long ans = 0;
  while (x) {
    x /= 10;
    ans++;
  }
  return ans;
}
long long bitketa(long long x) {
  long long ans = 0;
  while (x) {
    x >>= 1;
    ans++;
  }
  return ans;
}
long long ketasum(long long x) {
  long long ans = 0;
  while (x) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}
inline long long lcm(long long x, long long y) {
  long long ans = x / gcd(x, y) * y;
  return ans;
}
long long twobeki(long long x) {
  long long ans = 0;
  while (1) {
    if (!(x & 1)) {
      ans++;
      x >>= 1;
    } else {
      break;
    }
  }
  return ans;
}
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
long long kai(long long x, long long y) {
  long long res = 1;
  for (long long i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
long long comb(long long x, long long y) {
  if (y > x) return 0;
  return kai(x, y) * modpow(kai(y, y), mod - 2) % mod;
}
template <class T>
class SegTree {
  long long n;
  vector<T> node;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;

 public:
  SegTree(unsigned long long _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    node = vector<T>(n * 2, def);
  }
  SegTree(vector<long long>& initvec, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : operation(_operation), update(_update) {
    n = 1;
    while (n < initvec.size()) {
      n *= 2;
    }
    node = vector<T>(n * 2, def);
    for (long long i = n; i < n + initvec.size(); i++) {
      node[i] = initvec[i - n];
    }
    for (long long i = n - 1; i >= 1; i--) {
      node[i] = operation(node[i * 2], node[i * 2 + 1]);
    }
  }
  void change(long long i, T x) {
    i += n;
    node[i] = update(node[i], x);
    while (i >= 1) {
      i >>= 1;
      node[i] = operation(node[i * 2], node[i * 2 + 1]);
    }
  }
  T query(long long l, long long r) {
    l += n;
    r += n;
    T rx = def, lx = def;
    while (l < r) {
      if (l & 1) {
        lx = operation(lx, node[l]);
        l++;
      }
      if (r & 1) {
        r--;
        rx = operation(node[r], rx);
      }
      l >>= 1;
      r >>= 1;
    }
    return operation(lx, rx);
  }
  T operator[](const long long& x) { return node[x + n]; }
  void fill(T x) { std::fill(node.begin(), node.end(), x); }
  void print() {
    for (long long i = 0; i < n; i++) std::cout << operator[](i) << " ";
    std::cout << std::endl;
  }
  T queryForALL() { return node[1]; }
};
class Union_Find {
  vector<long long> par;
  vector<long long> ookisa;

 public:
  Union_Find(long long size) {
    par = vector<long long>(size);
    ookisa = vector<long long>(size);
    for (long long i = 0; i < size; i++) {
      par[i] = i;
      ookisa[i] = 1;
    }
  }
  long long find(long long x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = find(par[x]);
  }
  void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (ookisa[x] < ookisa[y]) {
      par[x] = y;
      ookisa[y] += ookisa[x];
      ookisa[x] = 0;
    } else {
      par[y] = x;
      ookisa[x] += ookisa[y];
      ookisa[y] = 0;
    }
  }
  long long size(long long i) {
    i = find(i);
    return ookisa[i];
  }
  bool same(long long x, long long y) { return find(x) == find(y); }
};
class BIT {
  vector<long long> data;
  long long size = 0;

 public:
  BIT(long long _size) {
    data = vector<long long>(_size + 1);
    size = _size;
  }
  void add(long long i, long long x) {
    while (i <= size) {
      data[i] += x;
      i += i & -i;
    }
  }
  long long sum(long long i) {
    assert(i <= size);
    long long s = 0;
    while (i > 0) {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }
  long long lower_bound(long long x) {
    if (x <= 0) {
      return 0;
    } else {
      long long i = 0;
      long long r = 1;
      while (r < size) r = r << 1;
      for (long long len = r; len > 0; len = len >> 1) {
        if (i + len < size && data[i + len] < x) {
          x -= data[i + len];
          i += len;
        }
      }
      return i + 1;
    }
  }
};
long long perm[2000005];
void init_perm() {
  perm[0] = 1;
  for (long long i = 1; i <= 2000004; i++) {
    perm[i] = perm[i - 1] * i % mod;
  }
}
long long nCk(long long x, long long y) {
  if (y > x) {
    return 0;
  }
  if (x < 0) {
    return 0;
  }
  return perm[x] * modpow(perm[x - y], mod - 2) % mod *
         modpow(perm[y], mod - 2) % mod;
}
struct Dot {
  double x;
  double y;
  Dot(double _x = 0.0, double _y = 0.0) {
    x = _x;
    y = _y;
  }
};
struct Dot_i {
  long long x;
  long long y;
  Dot_i(long long _x = 0, long long _y = 0) {
    x = _x;
    y = _y;
  }
};
double kyori(pair<long long, long long> f, pair<long long, long long> s) {
  double ans = 0;
  double t = fabs(f.first - s.first);
  double y = fabs(f.second - s.second);
  ans = sqrt(t * t + y * y);
  return ans;
}
double kyori(Dot f, Dot s) {
  double ans = 0;
  double t = fabs(f.x - s.x);
  double y = fabs(f.y - s.y);
  ans = sqrt(t * t + y * y);
  return ans;
}
inline bool stringmax(string& x, string& y) {
  if (x.size() != y.size()) {
    return x.size() < y.size();
  }
  return x < y;
}
struct edge {
  long long to;
  long long length;
  edge(long long _to, long long _length) {
    to = _to;
    length = _length;
  }
};
vector<long long> djkstra(vector<vector<edge>>& road, long long start) {
  vector<long long> kyo(road.size(), (long long)(3e18));
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push({0, start});
  kyo[start] = 0;
  while (q.size()) {
    long long x = q.top().second;
    long long now = q.top().first;
    q.pop();
    if (kyo[x] < now) {
      continue;
    }
    for (auto& i : road[x]) {
      if (kyo[i.to] > now + i.length) {
        kyo[i.to] = now + i.length;
        q.push({kyo[i.to], i.to});
      }
    }
  }
  return kyo;
}
template <class T>
void change_to_unique(vector<T>& v) {
  std::sort(v.begin(), v.end());
  auto k = unique(v.begin(), v.end());
  if (k != v.end()) v.erase(k, v.end());
}
double kodo_dosuu(double r) { return 180.0 * r / (double)PI; }
double dosuu_kodo(double r) { return r * (double)PI / 180.0; }
double kakudo(Dot a, Dot b1, Dot b2) {
  double size1 = kyori(a, b1), size2 = kyori(a, b2);
  double nai = (b1.x - a.x) * (b2.x - a.x) + (b1.y - a.y) * (b2.y - a.y);
  nai /= size1 * size2;
  return kodo_dosuu(acos(nai));
}
struct fraction {
  long long shi;
  long long bo;
  fraction(long long bunshi, long long bunbo) : bo(bunbo), shi(bunshi) {
    if (bunbo && bunshi) {
      long long g = gcd(bo, shi);
      bo /= g;
      shi /= g;
    }
  };
  explicit inline operator double() const { return (double)shi / (double)bo; }
  explicit inline operator long double() const {
    return (long double)shi / (long double)bo;
  }
};
bool operator<(const fraction& b1, const fraction& b2) {
  return b1.shi * b2.bo < b2.shi * b1.bo;
}
bool operator>(const fraction& b1, const fraction& b2) {
  return b1.shi * b2.bo > b2.shi * b1.bo;
}
template <class T>
void vecout(vector<T>& v) {
  if (v.size()) {
    cout << v[0];
  }
  for (long long i = 1; i <= (long long)v.size() - 1; i++) {
    cout << ' ' << v[i];
  }
  cout << endl;
}
inline long long zeronCk(long long x, long long y) {
  return nCk(y + x - 1, x - 1);
}
inline long long modinv(long long x, long long MOD = mod) {
  return modpow(x, MOD - 2);
}
using long long = long long;
class SegmentTree {
  long long n, n0;
  long long max_v[4 * 300003], smax_v[4 * 300003];
  long long sum[4 * 300003], max_c[4 * 300003];
  void update_node_max(long long k, long long x) {
    sum[k] += (x - max_v[k]) * max_c[k];
    max_v[k] = x;
  }
  void push(long long k) {
    if (max_v[k] < max_v[2 * k + 1]) {
      update_node_max(2 * k + 1, max_v[k]);
    }
    if (max_v[k] < max_v[2 * k + 2]) {
      update_node_max(2 * k + 2, max_v[k]);
    }
  }
  void update(long long k) {
    sum[k] = sum[2 * k + 1] + sum[2 * k + 2];
    if (max_v[2 * k + 1] < max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 2];
      max_c[k] = max_c[2 * k + 2];
      smax_v[k] = max(max_v[2 * k + 1], smax_v[2 * k + 2]);
    } else if (max_v[2 * k + 1] > max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1];
      smax_v[k] = max(smax_v[2 * k + 1], max_v[2 * k + 2]);
    } else {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1] + max_c[2 * k + 2];
      smax_v[k] = max(smax_v[2 * k + 1], smax_v[2 * k + 2]);
    }
  }
  void _update_min(long long x, long long a, long long b, long long k,
                   long long l, long long r) {
    if (b <= l || r <= a || max_v[k] <= x) {
      return;
    }
    if (a <= l && r <= b && smax_v[k] < x) {
      update_node_max(k, x);
      return;
    }
    push(k);
    _update_min(x, a, b, 2 * k + 1, l, (l + r) / 2);
    _update_min(x, a, b, 2 * k + 2, (l + r) / 2, r);
    update(k);
  }
  long long _query_max(long long a, long long b, long long k, long long l,
                       long long r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return max_v[k];
    }
    push(k);
    long long lv = _query_max(a, b, 2 * k + 1, l, (l + r) / 2);
    long long rv = _query_max(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(lv, rv);
  }
  long long _query_sum(long long a, long long b, long long k, long long l,
                       long long r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return sum[k];
    }
    push(k);
    long long lv = _query_sum(a, b, 2 * k + 1, l, (l + r) / 2);
    long long rv = _query_sum(a, b, 2 * k + 2, (l + r) / 2, r);
    return lv + rv;
  }

 public:
  SegmentTree(long long n) { SegmentTree(n, nullptr); }
  SegmentTree(long long n, long long* a) : n(n) {
    n0 = 1;
    while (n0 < n) n0 <<= 1;
    for (long long i = 0; i < n; ++i) {
      max_v[n0 - 1 + i] = sum[n0 - 1 + i] = (a != nullptr ? a[i] : 0);
      smax_v[n0 - 1 + i] = -(long long)(3e18);
      max_c[n0 - 1 + i] = 1;
    }
    for (long long i = n; i < n0; ++i) {
      max_v[n0 - 1 + i] = smax_v[n0 - 1 + i] = -(long long)(3e18);
      sum[n0 - 1 + i] = max_c[n0 - 1 + i] = 0;
    }
    for (long long i = n0 - 2; i >= 0; i--) update(i);
  }
  void update_min(long long a, long long b, long long x) {
    return _update_min(x, a, b, 0, 0, n0);
  }
  long long query_max(long long a, long long b) {
    return _query_max(a, b, 0, 0, n0);
  }
  long long query_sum(long long a, long long b) {
    return _query_sum(a, b, 0, 0, n0);
  }
};
long long n, k, a[300004], ans, memo[300004];
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cin >> n >> k;
  for (long long i = 1; i <= 300003; i++) {
    memo[i] = (long long)(3e18);
  }
  SegmentTree seg(300004, memo);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    seg.update_min(max(0ll, a[i] - k), min(300004ll, a[i] + k + 1),
                   seg.query_sum(a[i], a[i] + 1) - 1);
  }
  for (long long i = 0; i < n; i++) {
    chmax(ans, (long long)(3e18) - seg.query_sum(a[i], a[i] + 1));
  }
  cout << ans << "\n";
}
