This is yet another problem on regular bracket sequences. A bracket sequence is called…
- User
This is yet another problem on regular bracket sequences.
A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.
For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest. Input
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai, bi ≤ 106), where ai is the cost of replacing the i-th character "?" with an opening bracket, and bi — with a closing one.
Output
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.
Print -1, if there is no answer. If the answer is not unique, print any of them.
Examples
Input
(??) 1 2 2 8
Output
4 ()()
Assistant
pythonimport heapq s = input().strip() m = s.count('?') q_a = [] q_b = [] q_indices = [] # Collect indices of '?' in the string for i, c in enumerate(s): if c == '?': q_indices.append(i) # Read the costs for each '?' for _ in range(m): a, b = map(int, input().split()) q_a.append(a) q_b.append(b) existing_open = s.count('(') existing_close = s.count(')') n = len(s) required_open = (n // 2) - existing_open required_close = (n // 2) - existing_close if (required_open < 0 or required_close < 0 or required_open + required_close != m): print(-1) exit() cost = 0 balance = 0 heap = [] res = list(s) flip_count = 0 current_q = 0 # index for q_a and q_b for i in range(n): if s[i] == '(': balance += 1 elif s[i] == ')': balance -= 1 else: a = q_a[current_q] b = q_b[current_q] cost += b # Push the cost difference (a - b) and the index of the '?' heapq.heappush(heap, (a - b, current_q)) current_q += 1 res[i] = ')' balance -= 1 # Ensure balance is non-negative while balance < 0: if not heap: print(-1) exit() delta, idx = heapq.heappop(heap) # Flip this '?' to '(' balance += 2 cost += delta res[q_indices[idx]] = '(' flip_count += 1 # Final checks if balance != 0 or flip_count != required_open: print(-1) else: print(cost) print(''.join(res))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
QwQ-32BCompare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →