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

//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
char s[100003];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }

  long long Jsum = 0, Osum = 0, Isum = 0;
  long long R = 0;
  long long TJ[100000] = {}, TO[100000] = {}, TI[100000] = {};

  /// J
  Jsum = 1;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'J')
      Jsum += 1;
    if (s[i] == 'O')
      Osum += Jsum;
    if (s[i] == 'I')
      Isum += Osum;
  }
  R = max(R, Isum);
  // O
  if (s[0] == 'J')
    TJ[0] = 1;
  for (int i = 1; i < N; i++) {
    TJ[i] = TJ[i - 1];
    TO[i] = TO[i - 1];
    TI[i] = TI[i - 1];
    if (s[i] == 'J')
      TJ[i] += 1;
    if (s[i] == 'O')
      TO[i] += 1;
    if (s[i] == 'I')
      TI[i] += 1;
  }
  long long moto;
  Jsum = 0;
  Osum = 0;
  Isum = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'J')
      Jsum += 1;
    if (s[i] == 'O')
      Osum += Jsum;
    if (s[i] == 'I')
      Isum += Osum;
  }
  moto = Isum;
  long long x;
  for (int i = 0; i < N; i++) {
    x = TJ[i] * (TI[N - 1] - TI[i]);
    R = max(R, moto + x);
  }

  ////I
  Jsum = 0;
  Osum = 0;
  Isum = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'J')
      Jsum += 1;
    else if (s[i] == 'O')
      Osum += Jsum;
    else if (s[i] == 'I')
      Isum += Osum;
  }
  Isum += Osum;
  R = max(R, Isum);
  cout << R << endl;
  return 0;
}

Error: TLE

Fixed submission:

//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
char s[100003];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }

  int Jsum = 0, Osum = 0, Isum = 0;
  int R = 0;
  int TJ[100000] = {}, TO[100000] = {}, TI[100000] = {};

  Jsum = 1;
  Osum = 0;
  Isum = 0;
  /// J
  for (int i = 0; i < N; i++) {
    if (s[i] == 'J')
      Jsum += 1;
    if (s[i] == 'O')
      Osum += Jsum;
    if (s[i] == 'I')
      Isum += Osum;
  }
  R = max(R, Isum);
  // O
  if (s[0] == 'J')
    TJ[0] = 1;
  for (int i = 1; i < N; i++) {
    TJ[i] = TJ[i - 1];
    TO[i] = TO[i - 1];
    TI[i] = TI[i - 1];
    if (s[i] == 'J')
      TJ[i] += 1;
    if (s[i] == 'O')
      TO[i] += 1;
    if (s[i] == 'I')
      TI[i] += 1;
  }
  int moto;
  for (int j = 0; j < N; j++) {
    Jsum = 0;
    Osum = 0;
    Isum = 0;
    for (int i = 0; i < N; i++) {
      if (s[i] == 'J')
        Jsum += 1;
      if (s[i] == 'O')
        Osum += Jsum;
      if (s[i] == 'I')
        Isum += Osum;
    }
  }
  moto = Isum;
  int x;
  for (int j = 0; j < N; j++) {
    x = TJ[j] * (TI[N - 1] - TI[j]);

    R = max(R, moto + x);
  }

  ////I
  Jsum = 0;
  Osum = 0;
  Isum = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'J')
      Jsum += 1;
    else if (s[i] == 'O')
      Osum += Jsum;
    else if (s[i] == 'I')
      Isum += Osum;
  }
  Isum += Osum;
  R = max(R, Isum);
  cout << R << endl;
  return 0;
}