(Analysis by David Hu)

Note that each cow visits only cows at or after it. This implies that it is impossible for any pair of two cows to visit each other. Thus, either the leader of the guernseys must have visited all the guernseys or the leader of holsteins must have visited all the holsteins (or both). This means that the leader of the guernseys must be the earliest guernsey or the leader of the holsteins must be the earliest holstein (or both).

Once we fix the leader of the guernseys to be the earliest guernsey and verify that the earliest guernsey has indeed visited all the other guernseys, we can brute force over all holsteins and check whether they can also be a leader together with the earliest guernsey. Likewise, we also consider a similar case where the leader of the holsteins is the earliest holstein.

Make sure to pay special attention to the case where the earliest guernsey and earliest holstein are both leaders.

My C++ code:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e5 + 13;

int N;
string s;
int arr[MAXN];
int eG, eH; //earliest guernsey, earliest holstein
int lG, lH; //latest guernsey, latest holstein
int ans;

int main()
{
cin >> N >> s;
for (int i = 0; i < N; i++)
{
cin >> arr[i];
arr[i]--;
}
for (int i = 0; i < N; i++)
{
if (s[i] == 'G')
{
eG = i;
break;
}
}
for (int i = N - 1; i >= 0; i--)
{
if (s[i] == 'G')
{
lG = i;
break;
}
}
for (int i = 0; i < N; i++)
{
if (s[i] == 'H')
{
eH = i;
break;
}
}
for (int i = N - 1; i >= 0; i--)
{
if (s[i] == 'H')
{
lH = i;
break;
}
}
if (arr[eG] >= lG)
{
//earliest guernsey visited everybody and is the leader.
//holstein leader has to visit earliest guernsey or visit all holsteins.
//handle case where holstein leader has visited earliest guernsey.
for (int i = 0; i < eG; i++)
{
if (i == eH) //ignore the case where the holstein leader is earliest holstein.
{
continue;
}
if (s[i] == 'H' && arr[i] >= eG)
{
ans++;
}
}
}
if (arr[eH] >= lH)
{
//earliest holstein visited everybody.
for (int i = 0; i < eH; i++)
{
if (i == eG)
{
continue;
}
if (s[i] == 'G' && arr[i] >= eH)
{
ans++;
}
}
}
//check whether earliest guernsey and earliest holstein can together be leaders.
if ((arr[eG] >= lG || (eG <= eH && arr[eG] >= eH)) && (arr[eH] >= lH || (eH <= eG && arr[eH] >= eG)))
{
ans++;
}
cout << ans << '\n';
return 0;
}


My Python code:

N = int(input())
s = input()
arr = list(map(int, input().split()))
arr = [x - 1 for x in arr]

eG, eH, lG, lH = -1, -1, -1, -1

for i in range(N - 1, -1, -1):
if (s[i] == 'G'):
eG = i
if (s[i] == 'H'):
eH = i

for i in range(N):
if (s[i] == 'G'):
lG = i
if (s[i] == 'H'):
lH = i

ans = 0

if (arr[eG] >= lG):
for i in range(eG):
if (i == eH):
continue
if (s[i] == 'H' and arr[i] >= eG):
ans += 1

if (arr[eH] >= lH):
for i in range(eH):
if (i == eG):
continue
if (s[i] == 'G' and arr[i] >= eH):
ans += 1

if ((arr[eG] >= lG or (eG <= eH and arr[eG] >= eH)) and (arr[eH] >= lH or (eH <= eG and arr[eH] >= eG))):
ans += 1

print(ans)