Problem p02773 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define _GLIBCXX_DEBUG
#define int long long
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1) {
  unsigned day;
  if (dow < dow1)
    dow += 7;
  day = dow - dow1;
  day += 7 * n - 6;
  return day;
}
unsigned DayToWeekNumber(unsigned day) { return (day - 1) / 7 + 1; }
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0) {
  return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main() {
  int N;
  cin >> N;
  vector<string> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());
  int ans = 1;
  vector<int> B(N + 1);
  REP(i, N) {
    if (A[i] == A[i - 1])
      ans++;
    else {
      B[i] = ans;
      ans = 1;
    }
  }
  if (ans != 1)
    B[N - 1] = ans;
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  ans = 1;
  rep(i, N) {
    if (A[i] == A[i - 1])
      ans++;
    else {
      if (ans == B[0])
        cout << A[i - 1] << endl;
      ans = 1;
    }
  }
  if (ans == B[0])
    cout << A[N - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define _GLIBCXX_DEBUG
#define int long long
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1) {
  unsigned day;
  if (dow < dow1)
    dow += 7;
  day = dow - dow1;
  day += 7 * n - 6;
  return day;
}
unsigned DayToWeekNumber(unsigned day) { return (day - 1) / 7 + 1; }
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0) {
  return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main() {
  int N;
  cin >> N;
  vector<string> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());
  int ans = 1;
  vector<int> B(N + 1);
  REP(i, N) {
    if (A[i] == A[i - 1])
      ans++;
    else {
      B[i] = ans;
      ans = 1;
    }
  }
  if (ans != 1)
    B[N - 1] = ans;
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  ans = 1;
  REP(i, N) {
    if (A[i] == A[i - 1])
      ans++;
    else {
      if (ans == B[0])
        cout << A[i - 1] << endl;
      ans = 1;
    }
  }
  if (ans == B[0])
    cout << A[N - 1] << endl;
}