아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Leaders

시간 제한2초메모리 제한1024 MB

요약
각 소는 자신의 리스트에 같은 품종의 모든 소 또는 다른 품종의 리더가 포함되어야 한다는 조건을 만족하는 (G 리더, H 리더) 쌍의 수를 구한다.
난이도

보통10점 중 4점

유형
배열, 구현, 투 포인터
정답자
아직 제출이 없습니다

문제

Farmer John has NN cows (2≤N≤1052 \leq N \leq 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…N1 \ldots N in this order.

Over the course of the day, each cow writes down a list of cows. Specifically, cow ii's list contains the range of cows starting with herself (cow ii) up to and including cow E_iE\_i (i≤E_i≤Ni \leq E\_i \leq 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.

입력

The first line contains NN.

The second line contains a string of length NN, with the iith character denoting the breed of the iith 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_NE\_1 \dots E\_N.

출력

Output the number of possible pairs of leaders.

예제2

  1. 예제 1

    입력
    4
    GHHG
    2 4 3 4
    
    예상 출력
    1
    
  2. 예제 2

    입력
    3
    GGH
    2 3 3
    
    예상 출력
    2