Problem p01880 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define inf (1 << 30) - 1
#define INF (1LL << 62) - 1
#define MOD 1000000007LL
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define PI acos(-1)
#define MEM(x, y) memset(x, y, sizeof(x))
#define debug cout << "A" << '\\n'
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define REPL(i, a, b) for (LL i = (a); i <= (b); i++)
#define PERL(i, a, b) for (LL i = (a); i >= (b); i--)
#define print(x) cout << x << '\\n'
#define dprint(a, x) cout << setprecision(x) << fixed << a << '\\n'
#define itrALL(c, itr)                                                         \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef set<int> SI;
typedef set<LL> SL;
typedef set<string> SS;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef queue<int> QI;

/* Direction Array */

// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};

template <class T> inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}

template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }
template <class T> inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X> inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X> inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X> inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}

inline LL power(LL a, LL b) {
  LL multiply = 1;
  REP(i, 1, b) { multiply *= a; }
  return multiply;
}
inline LL ABS(LL a) { return (a >= 0) ? a : -a; }

/*end of header*/
int n, a[1003];
VI v;

bool valid(int x) {
  VI temp;
  while (x > 0) {
    temp.PB(x % 10);
    x /= 10;
  }
  REP(i, 1, temp.size() - 1) {
    if (temp[i] != temp[i - 1] - 1)
      return 0;
  }
  return 1;
}

int main() {
  // freopen("input_file_name.in","r",stdin);
  // freopen("output_file_name.out","w",stdout);
  scanf("%d", &n);
  REP(i, 1, n) scanf("%d", &a[i]);
  if (n == 1) {
    print(-1);
    return 0;
  }
  REP(i, 1, n) { REP(j, i + 1, n) v.PB(a[i] * a[j]); }
  sort(ALL(v));
  reverse(ALL(v));
  REP(i, 0, v.size() - 1) {
    if (valid(v[i])) {
      print(v[i]);
      return 0;
    }
  }
  print(-1);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[200000], d = 0, m, n, i, j, res, b[200000], cnt = 0, c[200000], f = 0,
                 mx = 0, maxi = -1;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  for (i = 0; i < n; i++) {
    for (j = i; j < n; j++) {
      if (i != j) {
        b[d] = a[i] * a[j];
        d++;
      }
    }
  }

  for (i = 0; i < d; i++) {
    m = b[i];
    cnt = 0;
    f = 0;
    // cout<<b[i]<<" ";
    while (m != 0) {
      res = m % 10;
      c[cnt] = res;
      // cout<<c[cnt]<<" ";
      cnt++;
      m /= 10;
    }
    // cout<<endl;
    for (j = 0; j < cnt - 1; j++) {
      if (c[j] - c[j + 1] == 1) {
        f = 0;
        // cout<<c[j+1]-c[j]<<" ";

      } else {

        f = 1;
        break;
      }
    }
    // cout<<endl;

    if (f == 0) {

      mx = b[i];
      if (mx > maxi)
        maxi = mx;
    }
  }
  if (maxi > 0)
    cout << maxi << endl;
  else
    cout << -1 << endl;

  return 0;
}