Solutions are presented as using the least memory and the fastest execution time. It also takes the top
10 most recent solutions from each language. If you want to limit to a specific index, click the
"Solved" button and go to that problem.
Problems
In this problem, a concatenation of two strings (T) and (U) is denoted by (T+U). A string consisting only of parentheses (opening parentheses ' ( ' or closing parentheses ' ) ') is balanced if and only if it is one of the following. An empty string. The concatenation of two non-empty balanced strings. The concatenation (( + T + )), where (T) is a balanced string. A string is deformed if and only if it is one of the following. The string " ) ". The concatenation (T + ) + U), where (T) and (U) are deformed strings. The concatenation (( + T + (), where (T) is a deformed string. A string (T) has a deformed balance if (T) is deformed and the concatenation (T + )) is balanced. For example, the string " ()( " has a deformed balance. You are given a string (S) of length (n) consisting only of parentheses. Under the given input constraints, it can be shown that there exist strings (X) and (Y) such that the concatenation (X + S + Y) has a deformed balance. Determine the minimum possible value of (|X| + |Y|) (the sum of the lengths of (X) and (Y)). The first line of input contains one integer (t) ((1 \le t \le 10\,000)) representing the number of test cases. After that, (t) test cases follow. Each of them is presented as follows. The first line of each test case contains an integer (n) ((1 \le n \le 10^6)). The second line contains a string (S) of length (n), consisting only of ' ( ' or ' ) '. The sum of (n) across all test cases in one input file does not exceed (10^6). For each test case, output the minimum possible value of (|X|+|Y|) such that the concatenation (X + S + Y) has a deformed balance. Explanation for the sample input/output #1 For the first test case, the given string already has a deformed balance. For the second test case, setting both (X) and (Y) to " ( " yields |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
|
365824841 |
sanjoy_doit028 |
M |
March 8, 2026, 8:59 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
60 |
62 |
102400 |
|
|
|
365813660 |
pyqjw1 |
M |
March 8, 2026, 7:02 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
60 |
46 |
102400 |
|
|
|
365878504 |
rk7028 |
M |
March 8, 2026, 3:31 p.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
46 |
102400 |
|
|
|
365831003 |
Fizonel |
M |
March 8, 2026, 10 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
46 |
102400 |
|
|
|
365804371 |
XVIII |
M |
March 8, 2026, 5:23 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
46 |
102400 |
|
|
|
365803481 |
Momotaros |
M |
March 8, 2026, 5:14 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
46 |
102400 |
|
|
|
365833748 |
makeit422 |
M |
March 8, 2026, 10:28 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
62 |
102400 |
|
|
|
365824254 |
anhkha1004 |
M |
March 8, 2026, 8:54 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
62 |
102400 |
|
|
|
365802470 |
riantkb |
M |
March 8, 2026, 5:02 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
62 |
102400 |
|
|
|
365796104 |
Boboge geospiza 1459007298 |
M |
March 8, 2026, 3:11 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
62 |
16076800 |
|
|
|
365803810 |
jiangly EvenImage qiuzx |
M |
March 8, 2026, 5:18 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
78 |
102400 |
|
|
|
365800093 |
ksun48 ecnerwala |
M |
March 8, 2026, 4:22 a.m. |
OK |
C++23 (GCC 14-64, msys2) |
TESTS |
60 |
93 |
16281600 |
|
|
|
365858893 |
didxga |
M |
March 8, 2026, 2:16 p.m. |
OK |
Java 21 |
TESTS |
60 |
250 |
819200 |
|
|
|
365836805 |
Msgnh |
M |
March 8, 2026, 10:58 a.m. |
OK |
PyPy 3-64 |
TESTS |
60 |
218 |
8192000 |
|
|
|
365856426 |
|
M |
March 8, 2026, 1:52 p.m. |
OK |
Unknown |
TESTS |
0 |
0 |
0 |
|
|
remove filters
Back to search problems