Problem p01839 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cout << v[i] << ' ';                                                     \\
    cout << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cout << v[a][b] << ' ';                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> ivv;

int main() {
  int n;

  cin >> n;

  int num = 0;

  if (n % 2 != 0) {
    cout << "NO" << endl;
    return 0;
  }

  REP(i, n) {
    string tmp;
    cin >> tmp;

    if (tmp == "A")
      num++;
    else
      num--;

    if (num < 0) {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (num == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cout << v[i] << ' ';                                                     \\
    cout << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int a = 0; a < i; a++) {                                              \\
      for (int b = 0; b < j; b++)                                              \\
        cout << v[a][b] << ' ';                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> ivv;

int main() {
  int n;

  while (cin >> n, n) {
    int num = 0;

    if (n % 2 != 0) {
      cout << "NO" << endl;
      goto NEXT;
    }

    REP(i, n) {
      string tmp;
      cin >> tmp;

      if (tmp == "A")
        num++;
      else
        num--;

      if (num < 0) {
        cout << "NO" << endl;
        goto NEXT;
      }
    }

    cout << "YES" << endl;

  NEXT:;
  }
  return 0;
}