Problem p02975 (C++)

Buggy submission (status: Runtime Error):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int N = 8e4;

int cas, n;
long long A[N + 3];
map<long long, int> cnt;

bool Solve2() {
  int stk[5] = {}, nstk = 0;
  for (map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++) {
    if (it->second != n / 3 * 2 && it->second != n / 3)
      return false;
    if (it->second == n / 3 * 2)
      stk[++nstk] = it->first, stk[++nstk] = it->first;
    else
      stk[++nstk] = it->first;
  }
  sort(stk + 1, stk + 4);
  do {
    if ((stk[3] ^ stk[1]) == stk[2] && (stk[1] ^ stk[2]) == stk[3] &&
        (stk[2] ^ stk[3]) == stk[1])
      return true;
  } while (next_permutation(stk + 1, stk + 4));
  return false;
}
bool Solve3() {
  int stk[5] = {}, nstk = 0;
  for (map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++) {
    if (it->second != n / 3)
      return false;
    stk[++nstk] = it->first;
  }
  sort(stk + 1, stk + 4);
  do {
    if ((stk[3] ^ stk[1]) == stk[2] && (stk[1] ^ stk[2]) == stk[3] &&
        (stk[2] ^ stk[3]) == stk[1])
      return true;
  } while (next_permutation(stk + 1, stk + 4));
  return false;
}
int main() {
  //	freopen("circle.in","r",stdin);
  //	freopen("circle.out","w",stdout);
  //	scanf("%d",&cas);
  cas = 1;
  while (cas--) {
    cnt.clear();
    scanf("%d", &n);
    //		bool fail=false;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &A[i]);
      cnt[A[i]]++;
    }
    if (cnt.size() > 3) {
      printf("No\n");
      continue;
    }
    switch (cnt.size()) {
    case 1:
      if (cnt.begin()->first == 0)
        printf("Yes\n");
      else
        printf("No\n");
      break;
    case 2:
      printf("%s\n", Solve2() ? "Yes" : "No");
      break;
    case 3:
      printf("%s\n", Solve3() ? "Yes" : "No");
      break;
    }
  }
  return 0;
}
/*
15: 001111
24: 011000
44: 101100
15 * 10
24 * 10
15 24 15 24 44 44 24 24 15 44 44 15 15 44 15 15 15 24 44 24 44 44 44 24 24 24 15
24 15 44
*/

Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int N = 1e5;

int cas, n;
long long A[N + 3];
map<long long, int> cnt;

bool Solve2() {
  int stk[5] = {}, nstk = 0;
  for (map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++) {
    if (it->second != n / 3 * 2 && it->second != n / 3)
      return false;
    if (it->second == n / 3 * 2)
      stk[++nstk] = it->first, stk[++nstk] = it->first;
    else
      stk[++nstk] = it->first;
  }
  sort(stk + 1, stk + 4);
  do {
    if ((stk[3] ^ stk[1]) == stk[2] && (stk[1] ^ stk[2]) == stk[3] &&
        (stk[2] ^ stk[3]) == stk[1])
      return true;
  } while (next_permutation(stk + 1, stk + 4));
  return false;
}
bool Solve3() {
  int stk[5] = {}, nstk = 0;
  for (map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++) {
    if (it->second != n / 3)
      return false;
    stk[++nstk] = it->first;
  }
  sort(stk + 1, stk + 4);
  do {
    if ((stk[3] ^ stk[1]) == stk[2] && (stk[1] ^ stk[2]) == stk[3] &&
        (stk[2] ^ stk[3]) == stk[1])
      return true;
  } while (next_permutation(stk + 1, stk + 4));
  return false;
}
int main() {
  //	freopen("circle.in","r",stdin);
  //	freopen("circle.out","w",stdout);
  //	scanf("%d",&cas);
  cas = 1;
  while (cas--) {
    cnt.clear();
    scanf("%d", &n);
    //		bool fail=false;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &A[i]);
      cnt[A[i]]++;
    }
    if (cnt.size() > 3) {
      printf("No\n");
      continue;
    }
    switch (cnt.size()) {
    case 1:
      if (cnt.begin()->first == 0)
        printf("Yes\n");
      else
        printf("No\n");
      break;
    case 2:
      printf("%s\n", Solve2() ? "Yes" : "No");
      break;
    case 3:
      printf("%s\n", Solve3() ? "Yes" : "No");
      break;
    }
  }
  return 0;
}
/*
15: 001111
24: 011000
44: 101100
15 * 10
24 * 10
15 24 15 24 44 44 24 24 15 44 44 15 15 44 15 15 15 24 44 24 44 44 44 24 24 24 15
24 15 44
*/