慕雪少年的魔法括号挑战
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Problem Background
Muxue the youth discovered a kind of magical bracket: each '(' contains +1 magical energy, each ')' contains -1 magical energy. By flipping brackets, he can adjust the total magical energy of the whole sequence.
Problem Description
Muxue has a kind of magical bracket: '(' has a magic value of 1, ')' has a magic value of -1. The magic value of a bracket sequence is defined as the sum of the magic values of all brackets.
Muxue can perform magic: choose a position in the sequence and flip the bracket at that position ('(' becomes ')', ')' becomes '('), consuming 1 point of magical energy.
Now there is a bracket sequence of length n. Muxue wants to know the minimum amount of magical energy he needs to consume to make the magic value of the sequence exactly equal to k.
Note: The flipped sequence does not need to be a valid bracket sequence; it only needs to have a magic value equal to k.
Input Format
First line: two integers n and k (1 ≤ n ≤ 10^5, -n ≤ k ≤ n)
Second line: a bracket string of length n
Output Format
An integer, representing the minimum magical energy; if impossible, output -1
Sample Input and Output #1
Input #1
5 1
()()(
Output #1
1
Explanation/Notes
Explanation for Sample 1: The current magic value is 1+(-1)+1+(-1)+1=1, which already equals k, so no operation is needed.
【XJS-C5-Div4】XJSOI 春节大月赛 Round 3 & 勰码可达鸭合作赛 Round 1
- 状态
- 已结束
- 规则
- IOI
- 题目
- 5
- 开始于
- 2026-2-7 0:00
- 结束于
- 2026-2-14 0:00
- 持续时间
- 3 小时
- 主持人
- 参赛人数
- 7