PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000158. Đếm đoạn có số A lớn hơn số B (Count Substrings With More A Than B)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đoạn có số A lớn hơn số B (Count Substrings With More A Than B)

Count Substrings With More A Than B

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a string ss of length nn over A and B, count nonempty contiguous substrings containing strictly more occurrences of A than B. Substrings are identified by their endpoints, numbered from 1; distinct positions are counted separately.

Input

The first line contains integer nn. The second line contains the string ss of length nn.

Output

Print one integer: the number of qualifying substrings.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; ∣s∣=n|s|=n; all characters belong to A, B.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; all characters belong to A.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; all characters belong to A, B.

Example 1

Input

3
AAB

Output

4

Explanation

The qualifying substrings are [1,1], [2,2], [1,2], [1,3]. Interval [2,3] has equal counts and is excluded.

Example 2

Input

3
BBB

Output

0

Explanation

No A occurs, so no interval qualifies.