#include #define all(x) (x).begin(),(x).end() typedef long long ll; #define rep(i,n) for(ll i=0, i##_len=(n); i=a;i--) #define reprev(i,n) reprrev(i,0,n) using namespace std; #define sz(x) ((int)(x).size()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) #define MEMSET(v, h) memset((v), h, sizeof(v)) templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b par, rank; UnionFind(int n) : par(n, -1), rank(n, 0) { } void init(int n) { par.assign(n, -1), rank.assign(n, 0); } int root(int x) { if (par[x] == -1) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; int main() { ll N; cin >> N; string s; cin >> s; ll ans = 0; vector u(N); vector m(N); vector g(N); ll ui = 0; ll mi = 0; ll gi = 0; rep(i, N) { if (s[i] == 'U') { u[ui] = i; ++ui; } if (s[i] == 'M') { m[mi] = i; ++mi; } if (s[i] == 'G') { g[gi] = i; ++gi; } } u.resize(ui); m.resize(mi); g.resize(gi); rep(i, u.size()) { rep(j, m.size()) { auto it = find(g.begin(), g.end(), u[i] + 2 * (m[j] - u[i])); if (it != g.end()) { ++ans; } else { continue; } } } cout << ans << endl; }