Problem p03262 (C++)

Buggy submission (status: Runtime Error):
/**
 * Problem:	C.
 * Author:	hkxadpall.
 * Date:	2018-09-01.
 * Upload:	AtCoder.
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

namespace AuthorName {
template <typename _TpInt> inline _TpInt read();
template <typename _TpRealnumber> inline double readr();
template <typename _TpInt> inline void write(_TpInt x);
//	template <typename _TpSwap> inline void swap(_TpSwap &x, _TpSwap &y);

#define Max_N 10007

int n, k, nk, ns;
int a[Max_N];
long long ans;

int gcd(int a, int b) { return (b == 0) ? (a) : (gcd(b, a % b)); }

int main() {
  n = read<int>();
  a[0] = read<int>(); // int a;
  for (int i = 1; i <= n; i++) {
    a[i] = read<int>();
  }
  sort(a, a + n + 1);
  int max_gcd = (a[1] - a[0]);
  for (int i = 2; i <= n; i++) {
    max_gcd = gcd(max_gcd, a[i] - a[0]);
  }
  write(max_gcd);
  return 0;
}

#define Getchar() getchar()

template <typename _TpInt> inline _TpInt read() {
  register int flag = 1;
  register char c = Getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = Getchar();
  if (c == '-')
    flag = -1, c = Getchar();
  register _TpInt init = (c & 15);
  while ((c = Getchar()) <= '9' && c >= '0')
    init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}

template <typename _TpRealnumber> inline double readr() {
  register int flag = 1;
  register char c = Getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = Getchar();
  if (c == '-')
    flag = -1, c = Getchar();
  register _TpRealnumber init = (c & 15);
  while ((c = Getchar()) <= '9' && c >= '0')
    init = init * 10 + (c & 15);
  if (c != '.')
    return init * flag;
  register _TpRealnumber l = 0.1;
  while ((c = Getchar()) <= '9' && c >= '0')
    init = init + (c & 15) * l, l *= 0.1;
  return init * flag;
}

template <typename _TpInt> inline void write(_TpInt x) {
  if (x < 0) {
    putchar('-');
    write<_TpInt>(~x + 1);
  } else {
    if (x > 9)
      write<_TpInt>(x / 10);
    putchar(x % 10 + '0');
  }
}

//	template <typename _TpSwap>
//	inline void swap(_TpSwap &x, _TpSwap &y)
//	{
//	    _TpSwap t = x;
//	    x = y;
//	    y = t;
//	}
} // namespace AuthorName

int main() {
  AuthorName::main();
  return 0;
}

Fixed submission:
/**
 * Problem:	C.
 * Author:	hkxadpall.
 * Date:	2018-09-01.
 * Upload:	AtCoder.
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

namespace AuthorName {
template <typename _TpInt> inline _TpInt read();
template <typename _TpRealnumber> inline double readr();
template <typename _TpInt> inline void write(_TpInt x);
//	template <typename _TpSwap> inline void swap(_TpSwap &x, _TpSwap &y);

#define Max_N 100007

int n, k, nk, ns;
int a[Max_N];
long long ans;

int gcd(int a, int b) { return (b == 0) ? (a) : (gcd(b, a % b)); }

int main() {
  n = read<int>();
  a[0] = read<int>(); // int a;
  for (int i = 1; i <= n; i++) {
    a[i] = read<int>();
  }
  sort(a, a + n + 1);
  int max_gcd = (a[1] - a[0]);
  for (int i = 2; i <= n; i++) {
    max_gcd = gcd(max_gcd, a[i] - a[0]);
  }
  write(max_gcd);
  return 0;
}

#define Getchar() getchar()

template <typename _TpInt> inline _TpInt read() {
  register int flag = 1;
  register char c = Getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = Getchar();
  if (c == '-')
    flag = -1, c = Getchar();
  register _TpInt init = (c & 15);
  while ((c = Getchar()) <= '9' && c >= '0')
    init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}

template <typename _TpRealnumber> inline double readr() {
  register int flag = 1;
  register char c = Getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = Getchar();
  if (c == '-')
    flag = -1, c = Getchar();
  register _TpRealnumber init = (c & 15);
  while ((c = Getchar()) <= '9' && c >= '0')
    init = init * 10 + (c & 15);
  if (c != '.')
    return init * flag;
  register _TpRealnumber l = 0.1;
  while ((c = Getchar()) <= '9' && c >= '0')
    init = init + (c & 15) * l, l *= 0.1;
  return init * flag;
}

template <typename _TpInt> inline void write(_TpInt x) {
  if (x < 0) {
    putchar('-');
    write<_TpInt>(~x + 1);
  } else {
    if (x > 9)
      write<_TpInt>(x / 10);
    putchar(x % 10 + '0');
  }
}

//	template <typename _TpSwap>
//	inline void swap(_TpSwap &x, _TpSwap &y)
//	{
//	    _TpSwap t = x;
//	    x = y;
//	    y = t;
//	}
} // namespace AuthorName

int main() {
  AuthorName::main();
  return 0;
}