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

#GD0000201. Xóa các cặp AB và BB (ABBB)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xóa các cặp AB và BB (ABBB)

ABBB

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A zookeeper has a string ss consisting only of A and B. One operation removes two adjacent characters if they form either AB or BB.

After deletion, the remaining parts are concatenated, possibly creating new removable pairs. You may perform the operations in any order, stopping when appropriate.

Find the minimum possible length of the remaining string after a sequence of valid operations. Do not print the resulting string itself.

Input

The only line contains a nonempty string ss consisting solely of A and B.

Output

Print one integer: the minimum possible number of characters remaining after valid AB or BB deletions.

Subtasks

  • Subtask 1 (20 points): 1≤∣s∣≤91\le|s|\le9; Every character of ss is A or B.
  • Subtask 2 (30 points): 1≤∣s∣≤20001\le|s|\le2000; Every character of ss is A or B.
  • Subtask 3 (50 points): 1≤∣s∣≤2⋅1051\le|s|\le2\cdot10^5; Every character of ss is A or B.

Examples

Input

BABA

Output

2

Explanation

Start with BABA. Deleting the middle AB produces BA, length 22. No legal pair remains. An empty result would require two deletions, but the leading B cannot be removed using the trailing A. Thus the minimum length is 22.