Problem p03731 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef pair<int, int> P;
#define mp make_pair
#define FOR(i, a, b) for (int(i) = a; (i) < (b); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort((v).begin(), (v).end());              // 小さい順
#define rvsort(v) sort(v.begin(), v.end(), greater<int>()); // 大きい順
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define all(c) (c).begin(), (c).end()
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout << (p) << endl
#define F first
#define S second
#define pb push_back
#define mod 1000000007LL
#define INF 12345678901234567

const int maxn = 1e5 + 10;

ll dp[1005] = {-INF};
vector<pair<ll, ll>> g[5000];
bool mugen = false;

void dfs(ll to, ll from, ll cost, vb check) {
  if (check[to]) {
    mugen = true;
  } else {
    dp[to] = max(dp[to], cost);
    // cout << to << " " << dp[to] << endl;
    check[to] = true;
    for (auto i : g[to]) {
      dfs(i.first, to, cost + i.second, check);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // your code is here

  ll n, T;
  cin >> n >> T;

  vi t(20005);

  rep(i, n) cin >> t[i];

  ll sum = T;
  FOR(i, 1, n) {
    if (-t[i - 1] + t[i] < T)
      sum += t[i] - t[i - 1];
    else
      sum += T;
  }

  Print(sum);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef pair<int, int> P;
#define mp make_pair
#define FOR(i, a, b) for (int(i) = a; (i) < (b); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort((v).begin(), (v).end());              // 小さい順
#define rvsort(v) sort(v.begin(), v.end(), greater<int>()); // 大きい順
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define all(c) (c).begin(), (c).end()
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout << (p) << endl
#define F first
#define S second
#define pb push_back
#define mod 1000000007LL
#define INF 12345678901234567

const int maxn = 1e5 + 10;

ll dp[1005] = {-INF};
vector<pair<ll, ll>> g[5000];
bool mugen = false;

void dfs(ll to, ll from, ll cost, vb check) {
  if (check[to]) {
    mugen = true;
  } else {
    dp[to] = max(dp[to], cost);
    // cout << to << " " << dp[to] << endl;
    check[to] = true;
    for (auto i : g[to]) {
      dfs(i.first, to, cost + i.second, check);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // your code is here

  ll n, T;
  cin >> n >> T;

  vi t(200005);

  rep(i, n) cin >> t[i];

  ll sum = T;
  FOR(i, 1, n) {
    if (-t[i - 1] + t[i] < T)
      sum += t[i] - t[i - 1];
    else
      sum += T;
  }

  Print(sum);
  return 0;
}