AtCoder Regular Contest 177
F
Two Airlines
AtCoder
⌀ 3810
原题
← E→
翻译中…

Problem Statement

翻译中…

The Republic of AtCoder consists of $L+1$ islands aligned east-west, numbered $0$ to $L$ from west to east. The islands are connected by air routes, where for each $1 \leq i \leq L$, there is a bidirectional air route between islands $i-1$ and $i$, as shown in the figure below. Each air route is owned by company A or company J, and the route between islands $i-1$ and $i$ is owned by company $S_i$.

翻译中…

翻译中…

The nation has $N$ residents, numbered $1$ to $N$. Resident $i$ is currently on island $X_i$.

翻译中…

Each resident has a coupon of one of the companies. Specifically, resident $i$ has a coupon of company $C_i$. With a coupon, a resident can take that company's routes for free any number of times, but taking a route of the other company costs $1$ coin per trip.

翻译中…

Now, there is a treasure chest on island $0$. The residents want to cooperate to carry it to the capital, which is on island $L$. Determine the minimum total number of coins required to achieve this goal.

翻译中…

The residents can pass the treasure chest to each other, but not their coupons.

翻译中…

Constraints

翻译中…
  • $1 \leq L \leq 6 \times 10^4$
  • $1 \leq N \leq 6 \times 10^4$
  • $S_i \ (1 \leq i \leq L)$ is A or J.
  • $0 \leq X_i \leq L \ (1 \leq i \leq N)$
  • $C_i \ (1 \leq i \leq N)$ is A or J.
  • $L$, $N$, and $X_i$ are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

L N
S_1S_2\cdotsS_L
X_1 C_1
X_2 C_2
 \vdots
X_N C_N

翻译中…

Note that the second line is given as a string of length $L$.

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
4 3
AAJJ
3 A
1 J
1 J
Output #1
2
The following procedure carries the treasure chest to island $4$ for a total of $2$ coins. 1. Resident $1$ moves from island $3$ to island $2$. The route is not covered by the coupon, so $1$ coin is paid. 2. Resident $1$ moves from island $2$ to island $1$. The route is covered by the coupon, so no coin is needed. 3. Resident $1$ moves from island $1$ to island $0$. The route is covered by the coupon, so no coin is needed. 4. Resident $1$ picks up the treasure chest. 5. Resident $1$, carrying the treasure chest, moves from island $0$ to island $1$. The route is covered by the coupon, so no coin is needed. 6. Resident $1$ passes the treasure chest to resident $2$. 7. Resident $2$, carrying the treasure chest, moves from island $1$ to island $2$. The route is not covered by the coupon, so $1$ coin is paid. 8. Resident $2$, carrying the treasure chest, moves from island $2$ to island $3$. The route is covered by the coupon, so no coin is needed. 9. Resident $2$, carrying the treasure chest, moves from island $3$ to island $4$. The route is covered by the coupon, so no coin is needed. ![ ](https://img.atcoder.jp/arc177/9545d9fbdd7defc8d56f84380765a3d7.png)
Input #2
8 3
JJAAJJAJ
2 A
6 A
8 J
Output #2
6
Input #3
8 6
JJAAJJAJ
2 A
6 A
8 J
8 J
8 J
8 J
Output #3
4
题目信息
题号
arc177_f
时间限制
5 s
内存限制
2048 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。