#C9443. Balanced Brackets with Limit

    ID: 53537 Type: Default 1000ms 256MiB

Balanced Brackets with Limit

Balanced Brackets with Limit

Problem Statement:

Given a bracket sequence S of length N and an integer K, determine whether the sequence is balanced under a specific constraint. A bracket sequence is considered balanced if:

1. For every prefix of the sequence, the number of opening brackets '(' is not less than the number of closing brackets ')'.
2. The absolute difference between the number of opening and closing brackets in any prefix does not exceed the limit (K). Formally, if we define the balance of a prefix as (balance(i) = #('(') - #(')')), then for all prefixes (i), it must hold that (balance(i) \ge 0) and (|balance(i)| \le K).
3. The overall balance of the entire sequence is zero.

Your task is to output "YES" if S satisfies all these conditions and "NO" otherwise.

inputFormat

Input is provided via standard input (stdin).

The first line contains a single integer (T), representing the number of test cases.
Each of the next (T) lines contains a test case with three values: an integer (N) (the length of the string), an integer (K) (the allowed limit for unmatched brackets), and a string (S) (a sequence consisting solely of characters '(' and ')').

outputFormat

For each test case, output a single line via standard output (stdout) containing either "YES" if the bracket sequence is balanced under the given constraints or "NO" if it is not.## sample

5
6 2 (()())
4 3 ())(
10 1 (()(()(())
4 2 (())
3 1 (()
YES

NO NO YES NO

</p>