{"raw_statement":[{"iden":"statement","content":"The narrow bus is so narrow it doesn't have any seats and the passengers have to stand in a row. The bus has two doors that can be used for entering or leaving the bus: one in the front and one in the back. People can't easily swap in the bus because it is too narrow, so the order people are standing in does not change while the bus is running.\n\nAt each bus stop, either a single person enters the bus or a single person leaves the bus. \n\nGiven a description of people entering and leaving the bus, count how many people will have to get out when somebody leaves the bus. \n\nThe first line of input contains a single integer n, the number of actions at bus stops (1 ≤ n ≤ 105). The next n lines will contain one of the three types of actions each. The actions happen consecutively as they are given in the input.\n\nEach action happens at a single bus stop. After the action is completed, the bus leaves for the next stop.\n\nPrint a single integer for each type \"_O_\" query, the number of people who will have to get out and go back in the bus when a person leaves at the stop.\n\nIn the example, after everyone gets in the bus during the first 5 stops, the people are ordered 1, 2, 3, 4, 5.\n\nAt the 6th stop, the 2nd person leaves through the front door and the 1st person is forced to get out of the bus. The 1st person then uses the back door to enter the bus again. The order changes to 3, 4, 5, 1.\n\nDuring the next two stops two people use the front door to enter the bus, and the order becomes 7, 6, 3, 4, 5, 1.\n\nAt the 9th stop, the 4th person leaves through the back door and now the 5th and the 1st person are forced to get out of the bus. They then use the front door to get back in the bus. The order changes to 5, 1, 7, 6, 3.\n\n"},{"iden":"input","content":"The first line of input contains a single integer n, the number of actions at bus stops (1 ≤ n ≤ 105). The next n lines will contain one of the three types of actions each. The actions happen consecutively as they are given in the input.  \"_F_\" — a person enters the bus using the front door.  \"_B_\" — a person enters the bus using the back door.  \"_O_ i\" — the i-th person that has entered the bus today leaves. Each action happens at a single bus stop. After the action is completed, the bus leaves for the next stop."},{"iden":"output","content":"Print a single integer for each type \"_O_\" query, the number of people who will have to get out and go back in the bus when a person leaves at the stop."},{"iden":"examples","content":"Input9FBBBBO 2FFO 4Output12"},{"iden":"note","content":"In the example, after everyone gets in the bus during the first 5 stops, the people are ordered 1, 2, 3, 4, 5.At the 6th stop, the 2nd person leaves through the front door and the 1st person is forced to get out of the bus. The 1st person then uses the back door to enter the bus again. The order changes to 3, 4, 5, 1.During the next two stops two people use the front door to enter the bus, and the order becomes 7, 6, 3, 4, 5, 1.At the 9th stop, the 4th person leaves through the back door and now the 5th and the 1st person are forced to get out of the bus. They then use the front door to get back in the bus. The order changes to 5, 1, 7, 6, 3."}],"translated_statement":null,"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of actions.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of actions, where each $ a_i \\in \\{ \\text{\"I\"}, \\text{\"F\"}, \\text{\"B\"} \\} $:  \n- \"I\" denotes a person entering the bus (at the end of the line).  \n- \"F\" denotes a person leaving through the **front** door (the first person in line exits).  \n- \"B\" denotes a person leaving through the **back** door (the last person in line exits).  \n\nLet $ L = (p_1, p_2, \\dots, p_m) $ be the current line of passengers, ordered from front to back, where $ p_1 $ is the frontmost and $ p_m $ is the rearmost. Initially, $ L = \\emptyset $.  \n\nLet $ \\text{exit\\_count} $ be the total number of people forced to exit and re-enter the bus during all \"F\" and \"B\" actions.\n\n**Constraints**  \n1. $ 1 \\le n \\le 10^5 $  \n2. For each action $ a_i $:  \n   - If $ a_i = \\text{\"I\"} $, a new person enters at the back of $ L $.  \n   - If $ a_i = \\text{\"F\"} $, the frontmost person $ p_1 $ exits; all persons from $ p_2 $ to $ p_m $ must exit and re-enter via the back door.  \n   - If $ a_i = \\text{\"B\"} $, the rearmost person $ p_m $ exits; all persons from $ p_1 $ to $ p_{m-1} $ must exit and re-enter via the front door.  \n\n**Objective**  \nFor each action $ a_i = \\text{\"F\"} $ or $ a_i = \\text{\"B\"} $, compute the number of people forced to exit and re-enter:  \n- If $ a_i = \\text{\"F\"} $: output $ |L| - 1 $  \n- If $ a_i = \\text{\"B\"} $: output $ |L| - 1 $  \n\nOutput the **sum** of all such values over all \"F\" and \"B\" actions.","simple_statement":"When someone leaves the bus through front or back, all people between them and that door must step out and re-enter through the opposite door. Count how many people must move out each time someone leaves.","has_page_source":false}