Page not loading? Try clicking here.
Placeholder

#5466

Leaders 1s 32MB

Problems

Farmer John has N cows (2≤N≤10^5). Each cow has a breed that is either Guernsey or Holstein. As is often the case, the cows are standing in a line, numbered 1…N in this order.

Over the course of the day, each cow writes down a list of cows. Specifically, cow i's list contains the range of cows starting with herself (cow i) up to and including cow E_i (i≤E_i≤N).

FJ has recently discovered that each breed of cow has exactly one distinct leader. FJ does not know who the leaders are, but he knows that each leader must have a list that includes all the cows of their breed, or the other breed's leader (or both).

Help FJ count the number of pairs of cows that could be leaders. It is guaranteed that there is at least one possible pair.​


Input

The first line contains N.

The second line contains a string of length N, with the ith character denoting the breed of the ith cow (G meaning Guernsey and H meaning Holstein). It is guaranteed that there is at least one Guernsey and one Holstein.

 

The third line contains E_1…E_N.​ 


Output

Output the number of possible pairs of leaders. 


Example #1

4

GHHG
2 4 3 4
1

Example #2

3

GGH
2 3 3
2


Source

USACO 2023 January Bronze

You must sign in to write code.