Problem p03627 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define D(n, retu)                                                             \
  REP(i, n) { cin >> retu[i]; }
#define mod 1000000007
#define MIN -93193111451418101
#define INF 931931114518101
using namespace std;
typedef long long int ll;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define MAX_NUM 50
long long comb[MAX_NUM + 1][MAX_NUM + 1];

ll par[100000], depth[100000];
static void calc_comb() {
  for (uint32_t i = 0; i <= MAX_NUM; i++) {
    for (uint32_t j = 0; j <= i; j++) {
      if ((j == 0) || (i == j)) {
        comb[i][j] = 1;
      } else {
        comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
      }
    }
  }
}

void init(ll n) {
  REP(i, n) {
    par[i] = i;
    depth[i] = 0;
  }
  return;
}

ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void merge(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (depth[x] > depth[y]) {
    par[y] = x;
  } else {
    par[x] = y;
    if (depth[x] == depth[y]) {
      depth[y]++;
    }
  }
}

bool same(ll x, ll y) {
  if (find(x) == find(y)) {
    return false;
  }
  return true;
}

//------------------変数-----------------------//
ll n, a, b, c;
string str;
//-------------------関数----------------------//
int main() {
  cin >> n;
  ll r[20001];
  REP(i, n) { cin >> r[i]; }
  sort(r, r + n);
  vector<ll> vec;
  REP(i, n - 1) {
    if (r[i] == r[i + 1]) {
      vec.push_back(r[i]);
      i++;
    }
  }
  if (vec.size() < 2) {
    cout << 0 << endl;
    return 0;
  }
  ll si = vec.size();
  cout << vec[si - 1] * vec[si - 2] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define D(n, retu)                                                             \
  REP(i, n) { cin >> retu[i]; }
#define mod 1000000007
#define MIN -93193111451418101
#define INF 931931114518101
using namespace std;
typedef long long int ll;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define MAX_NUM 50
long long comb[MAX_NUM + 1][MAX_NUM + 1];

ll par[100000], depth[100000];
static void calc_comb() {
  for (uint32_t i = 0; i <= MAX_NUM; i++) {
    for (uint32_t j = 0; j <= i; j++) {
      if ((j == 0) || (i == j)) {
        comb[i][j] = 1;
      } else {
        comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
      }
    }
  }
}

void init(ll n) {
  REP(i, n) {
    par[i] = i;
    depth[i] = 0;
  }
  return;
}

ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void merge(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (depth[x] > depth[y]) {
    par[y] = x;
  } else {
    par[x] = y;
    if (depth[x] == depth[y]) {
      depth[y]++;
    }
  }
}

bool same(ll x, ll y) {
  if (find(x) == find(y)) {
    return false;
  }
  return true;
}

//------------------変数-----------------------//
ll n, a, b, c;
string str;
//-------------------関数----------------------//
int main() {
  cin >> n;
  ll r[200000];
  REP(i, n) { cin >> r[i]; }
  sort(r, r + n);
  vector<ll> vec;
  REP(i, n - 1) {
    if (r[i] == r[i + 1]) {
      vec.push_back(r[i]);
      i++;
    }
  }
  if (vec.size() < 2) {
    cout << 0 << endl;
    return 0;
  }
  ll si = vec.size();
  cout << vec[si - 1] * vec[si - 2] << endl;
  return 0;
}