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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
// namespace mp = boost::multiprecision;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string s;
  cin >> s;

  int *sumR = new int[n];
  int *sumW = new int[n];

  sumR[0] = 0;
  sumW[0] = 0;
  if (s[0] == 'R')
    ++sumR[0];
  else
    ++sumW[0];
  REP(i, 1, n) {
    sumR[i] = sumR[i - 1];
    sumW[i] = sumW[i - 1];

    if (s[i] == 'R')
      ++sumR[i];
    else
      ++sumW[i];
  }

  int ans = min(sumR[n - 1], sumW[n - 1]);
  rep(i, n - 1) { ans = min(ans, max(sumW[i], sumR[n - 1] - sumR[i])); }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
// namespace mp = boost::multiprecision;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string s;
  cin >> s;

  int ans = 0;
  int t = n - 1;
  rep(i, n) {
    bool flag = false;
    if (s[i] == 'W') {
      rrep(j, t) {
        if (s[j] == 'R') {
          if (i > j) {
            flag = true;
            break;
          } else {
            ++ans;
            t = j - 1;
            break;
          }
        }
      }

      if (flag) {
        break;
      }
    }
  }

  cout << ans << "\\n";

  return 0;
}