from collections import deque

def solution(arrangement):
    answer = 0
    s = 0
    arrangement = deque(arrangement.replace('()','0'))

    while len(arrangement):
        now = arrangement.popleft()
        if arrangement and now == '0':
            answer += s
        elif now == '(':
            s += 1
        else:
            s -= 1
            answer += 1

    return answer


print(solution("()(((()())(())()))(())"))
4a5fa51d088a69e10ef1cb5c6e771c9d0a405a94
<mask token>

<mask token>


def solution(arrangement):
    answer = 0
    s = 0
    arrangement = deque(arrangement.replace('()', '0'))
    while len(arrangement):
        now = arrangement.popleft()
        if arrangement and now == '0':
            answer += s
        elif now == '(':
            s += 1
        else:
            s -= 1
            answer += 1
    return answer


<mask token>

<mask token>


def solution(arrangement):
    answer = 0
    s = 0
    arrangement = deque(arrangement.replace('()', '0'))
    while len(arrangement):
        now = arrangement.popleft()
        if arrangement and now == '0':
            answer += s
        elif now == '(':
            s += 1
        else:
            s -= 1
            answer += 1
    return answer


print(solution('()(((()())(())()))(())'))

from collections import deque


def solution(arrangement):
    answer = 0
    s = 0
    arrangement = deque(arrangement.replace('()', '0'))
    while len(arrangement):
        now = arrangement.popleft()
        if arrangement and now == '0':
            answer += s
        elif now == '(':
            s += 1
        else:
            s -= 1
            answer += 1
    return answer


print(solution('()(((()())(())()))(())'))

from collections import deque

def solution(arrangement):
    answer = 0
    s = 0
    arrangement = deque(arrangement.replace('()','0'))

    while len(arrangement):
        now = arrangement.popleft()
        if arrangement and now == '0':
            answer += s
        elif now == '(':
            s += 1
        else:
            s -= 1
            answer += 1

    return answer


print(solution("()(((()())(())()))(())"))