cswork / OAOpen practice workspace

JPMorgan ChaseOA #21

Substring Removal

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & HeapsStrings

Each step deletes one contiguous AB or BB and joins the two remaining sides. Find the minimum length after any number of operations.

Input

The first line contains the length n, and the second line contains the AB string; original bounds 1≤n≤200000.

Output

Output a single integer answer.

Example 1

Input:

5
BABBA

Output:

1

Example 2

Input:

3
ABB

Output:

1

Example 3

Input:

4
AAAA

Output:

4

Example notes

The outputs of the three public examples are, in order: 1; 1; 4. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…