Problem p02873 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;

ll NumCount(ll Count0, ll Count1, string S, ll AC0, ll AC1) {
  ll AllCount = 0;
  if (Count0 <= Count1) {
    AllCount += AC1;
    AllCount += AC0 - Count0;
  } else {
    AllCount += AC1 - Count1;
    AllCount += AC0;
  }
  return AllCount;
}
int main() {
  string S;
  cin >> S;
  ll Count0 = 0;
  ll ACount0 = 0;
  ll Count1 = 0;
  ll ACount1 = 0;
  ll AllCount = 0;
  if (S.size() == 1) {
    cout << 1;
  } else {
    if (S[0] == '<') {
      Count0++;
      ACount0 += Count0;
    } else {
      Count1++;
      ACount1 += Count1;
    }
    for (int i = 1; i < S.size() - 1; i++) {
      if (S[i - 1] == '<' && S[i] == '<') {
        Count0++;
        ACount0 += Count0;
      } else if (S[i - 1] == '>' && S[i] == '<') {
        AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
        Count0 = 1;
        ACount0 = Count0;
        Count1 = 0;
        ACount1 = 0;
      } else {
        Count1++;
        ACount1 += Count1;
      }
    }
    if (S[S.size() - 2] == '>' && S[S.size() - 1] == '<') {
      AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
      AllCount++;
    } else if (S[S.size() - 1] == '>') {
      Count1++;
      ACount1 += Count1;
      AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
    } else {
      Count0++;
      ACount0 += Count0;
      AllCount += ACount0;
    }
    cout << AllCount;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;

ll NumCount(ll Count0, ll Count1, string &S, ll AC0, ll AC1) {
  ll AllCount = 0;
  if (Count0 <= Count1) {
    AllCount += AC1;
    AllCount += AC0 - Count0;
  } else {
    AllCount += AC1 - Count1;
    AllCount += AC0;
  }
  return AllCount;
}
int main() {
  string S;
  cin >> S;
  ll Count0 = 0;
  ll ACount0 = 0;
  ll Count1 = 0;
  ll ACount1 = 0;
  ll AllCount = 0;
  if (S.size() == 1) {
    cout << 1;
  } else {
    if (S[0] == '<') {
      Count0++;
      ACount0 += Count0;
    } else {
      Count1++;
      ACount1 += Count1;
    }
    for (int i = 1; i < S.size() - 1; i++) {
      if (S[i - 1] == '<' && S[i] == '<') {
        Count0++;
        ACount0 += Count0;
      } else if (S[i - 1] == '>' && S[i] == '<') {
        AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
        Count0 = 1;
        ACount0 = Count0;
        Count1 = 0;
        ACount1 = 0;
      } else {
        Count1++;
        ACount1 += Count1;
      }
    }
    if (S[S.size() - 2] == '>' && S[S.size() - 1] == '<') {
      AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
      AllCount++;
    } else if (S[S.size() - 1] == '>') {
      Count1++;
      ACount1 += Count1;
      AllCount += NumCount(Count0, Count1, S, ACount0, ACount1);
    } else {
      Count0++;
      ACount0 += Count0;
      AllCount += ACount0;
    }
    cout << AllCount;
  }
}