Problem p03733 (C++)

Buggy submission (status: Runtime Error):

/*{{{*/
// #pragma GCC optimize("O3")

#include <bits/stdc++.h>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

typedef long long ll;
typedef std::pair<int, int> pii;

std::string Name = __FILE__;
std::string iput = Name.substr(0, Name.length() - 4) + ".in";
std::string oput = Name.substr(0, Name.length() - 4) + ".out";

template <class T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}

template <class T> inline T &read(T &x) {
  static int f;
  static char c;
  for (f = 1; !isdigit(c = getchar());) {
    if (c == '-')
      f = -1;
  }
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - 48;
  }
  return x *= f;
}

template <class T> inline void write(T x, const char p = '\n') {
  static int top;
  static int s[30];
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  do
    s[++top] = x % 10 + 48;
  while (x /= 10);
  while (top)
    putchar(s[top--]);
  putchar(p);
}
/*}}}*/

const int maxn = 1e5 + 5;

long long n;
long long T;
long long a[maxn];
long long ans;

void exec() {
  read(n);
  read(T);
  read(a[1]);
  for (int i = 2; i <= n; ++i) {
    read(a[i]);
    if (a[i - 1] + T < a[i])
      ans -= a[i] - a[i - 1] - T;
  }
  ans += a[n] + T;
  write(ans);
}

/*{{{*/
int main() {
  if (fopen(iput.c_str(), "r") != NULL) {
    freopen(iput.c_str(), "r", stdin);
    freopen(oput.c_str(), "w", stdout);
  }

  exec();

  fclose(stdin);
  fclose(stdout);

  return 0;
}
/*}}}*/


Fixed submission:

/*{{{*/
// #pragma GCC optimize("O3")

#include <bits/stdc++.h>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

typedef long long ll;
typedef std::pair<int, int> pii;

std::string Name = __FILE__;
std::string iput = Name.substr(0, Name.length() - 4) + ".in";
std::string oput = Name.substr(0, Name.length() - 4) + ".out";

template <class T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}

template <class T> inline T &read(T &x) {
  static int f;
  static char c;
  for (f = 1; !isdigit(c = getchar());) {
    if (c == '-')
      f = -1;
  }
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - 48;
  }
  return x *= f;
}

template <class T> inline void write(T x, const char p = '\n') {
  static int top;
  static int s[30];
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  do
    s[++top] = x % 10 + 48;
  while (x /= 10);
  while (top)
    putchar(s[top--]);
  putchar(p);
}
/*}}}*/

const int maxn = 2e5 + 5;

long long n;
long long T;
long long a[maxn];
long long ans;

void exec() {
  read(n);
  read(T);
  read(a[1]);
  for (int i = 2; i <= n; ++i) {
    read(a[i]);
    if (a[i - 1] + T < a[i])
      ans -= a[i] - a[i - 1] - T;
  }
  ans += a[n] + T;
  write(ans);
}

/*{{{*/
int main() {
  if (fopen(iput.c_str(), "r") != NULL) {
    freopen(iput.c_str(), "r", stdin);
    freopen(oput.c_str(), "w", stdout);
  }

  exec();

  fclose(stdin);
  fclose(stdout);

  return 0;
}
/*}}}*/