description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
inp = input() res, stack = 0, [-1] for i, val in enumerate(inp): if val == "(": stack.append(i) elif len(stack) > 1: stack.pop() res += 2 print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def equal(s): cnt = 0 for i in s: if i == "(": cnt += 1 else: cnt -= 1 return cnt def find_ans(ans): cnt = 0 answer = 0 for i in ans: if i == "(": cnt += 1 else: cnt -= 1 if cnt < 0: cnt = 0 answer += 1 print(len(ans) - 2 * answer) s = input() n = len(s) ans = [] cnt = equal(s) if cnt > 0: p = 0 for i in range(n - 1, -1, -1): if s[i] == "(": if p == cnt: ans.append("(") else: p += 1 else: ans.append(")") ans.reverse() elif cnt < 0: p = 0 for i in range(n): if s[i] == ")": if p == cnt: ans.append(")") else: p -= 1 else: ans.append("(") else: ans = [i for i in s] find_ans(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
ch = input() l = len(ch) a = 0 s = 0 for i in range(l): if ch[i] == "(": s += 1 a += 1 elif s: s -= 1 a += 1 a = a - s print(a)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
l = input() correct = 0 Aux = 0 for i in l: if i == "(": correct += 1 elif correct == 0: Aux += 1 else: correct -= 1 print(len(l) - correct - Aux)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
br = input() br = list(br) diff = 0 exc = 0 counter = 0 for i in range(len(br)): if br[i] == "(": diff += 1 counter += 1 elif diff > 0: diff -= 1 counter += 1 print(counter - diff)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
b = str(input()) x = [] j = 0 a = len(b) for i in range(0, a): if b[i] == "(": x.append(b[i]) elif b[i] == ")": if len(x) != 0: y = x.pop() s = y + ")" if s == "()": j = j + 1 print(2 * j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() o = 0 c = 0 ans = "" for i in s: if i == "(": o += 1 ans += i if i == ")": if o > c: c += 1 ans += i while o > c + 1: o -= 1 print((c + o) // 2 * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR IF VAR STRING IF VAR VAR VAR NUMBER VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
n = input() l = list(n) p = list() k = 0 for i in l: if i == "(": p.append(i) elif i == ")": if len(p) != 0: k += 1 p.pop() print(2 * k)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
class Stack: def __init__(self): self.items = [] def isEmpty(self): return len(self.items) == 0 def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): if not self.isEmpty(): return self.items[len(self.items) - 1] def size(self): return len(self.items) s = Stack() str = input() cnt = 0 length = len(str) for tmp in str: if tmp == ")": if not s.isEmpty() and s.peek() == "(": cnt += 2 s.pop() else: s.push(tmp) else: s.push(tmp) print(cnt)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() stack = [] length = 0 for i in range(len(s)): if s[i] == "(": stack.append("(") elif stack: stack.pop() length += 1 print(length * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
for t in range(1): bracket = list(input()) open = 0 ans = 0 for i in bracket: if i == "(": open += 1 elif open > 0: open -= 1 ans += 1 print(2 * ans)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
from sys import stdin input = stdin.readline l = list(input()) l.pop() ans = 0 stack = [] for item in l: if item == "(": stack.append(1) elif len(stack): stack.pop() ans += 1 print(2 * ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() l = 0 r = 0 ans = 0 for i in range(len(s)): if s[i] == "(": l += 1 elif l > 0: r += 1 if l >= r and r > 0: ans += 1 r -= 1 l -= 1 print(2 * ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
class Stack: def __init__(self, max_size): self.stack = [None for x in range(max_size)] self.top_pointer = -1 def push(self, new_element): self.top_pointer = self.top_pointer + 1 self.stack[self.top_pointer] = new_element def pop(self): last_element = self.stack[self.top_pointer] self.top_pointer = self.top_pointer - 1 return last_element def peek(self): return self.stack[self.top_pointer] def show(self): for k in self.stack: print(k) def is_empty(self): if self.top_pointer == -1: return True def checkBalance(expression): result = 0 mystack = Stack(len(expression)) for e in expression: if e == "(": mystack.push(e) elif e == ")": if mystack.peek() == "(": mystack.pop() result = result + 2 print(result) test = input() checkBalance(test)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() count = 0 t = [] for i in range(len(s)): if s[i] == "(": t.append(s[i]) elif len(t) > 0: t.pop() else: count += 1 count += len(t) print(len(s) - count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def skobki(s): count1, count2 = 0, 0 for i in range(len(s)): if s[i] == "(": count1 += 1 elif s[i] == ")": count1 -= 1 if count1 < 0: count2 += 1 count1 += 1 if count1 > 0: count2 += count1 return len(s) - count2 t = input() print(skobki(t))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() n = len(s) st = [] ans = 0 for i in range(n): if s[i] == "(": st.append(1) elif len(st) > 0: ans += 2 st.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
count = 0 z = 0 s = input() j = len(s) - 1 for i in range(len(s)): if s[i] == "(": z += 1 elif s[i] == ")" and z > 0: z -= 1 count += 2 print(count)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
line = list(input()) op = cl = i = 0 while i < len(line): if line[i] == "(": op += 1 elif cl != op: cl += 1 i += 1 print(min(op, cl) * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() l = len(s) ans = 0 end = front = 0 flag = True i = 0 while i < l: if s[i] == "(": front += 1 else: end += 1 if end > front and s[i] == ")": ans += front while i < l and s[i] == ")": i += 1 front = 0 end = 0 continue i += 1 ans += min(front, end) print(ans * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR STRING VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = input() a = a[a.find("(") : len(a) - a[::-1].find(")") : 1] i = 0 b = list(a) N = 0 x = 0 y = 0 while i < len(b): if b[i] == "(": x = x + 1 elif b[i] == ")": x = x - 1 if x >= 0: N = N + 1 else: x = 0 i = i + 1 print(N * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def find(s): left = 0 ans = 0 for i in range(len(s)): if s[i] == "(": left += 1 elif left > 0: ans += 1 left -= 1 return 2 * ans print(find(input()))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
from sys import stdin hat = stdin.readlines() seq = hat[0] true = 0 leftbra = 0 for i in seq: if i == "(": leftbra += 1 elif i == ")" and leftbra > 0: leftbra -= 1 true += 2 print(true)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
n = input() r, t = 0, 0 for i, x in enumerate(n): if x == "(": r += 1 if x == ")": if r == 0: t += 1 else: r -= 1 print(len(n) - r - t)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
seq = input() n = len(seq) ob = [] cb = [] ans = 0 t = 0 for i in range(n): if seq[i] == "(": ob.append(i) else: cb.append(i) x = len(cb) for i in range(x): if len(ob) == 0: break try: if ob[t] - cb[i] < 0: ans += 2 t += 1 except: break print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = input() stack = [] extra = [] for i in range(0, len(a)): if a[i] == "(": stack.append("(") elif a[i] == ")" and len(stack) != 0: stack.pop() elif a[i] == ")" and len(stack) == 0: extra.append(")") print(len(a) - len(stack) - len(extra))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
ch = input() n = len(ch) lst = [] nb = 0 i = 0 while i < n: c = ch[i] if c == "(": lst.append("(") if c == ")": if len(lst) == 0: nb += 1 else: lst.pop() i += 1 print(n - (nb + len(lst)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
n = input() a = [] res = 0 for i in n: if i == "(": a.append(i) elif len(a) and a[-1] == "(": res += 2 a.pop() else: a.append(i) print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
i = 0 k = 0 A = str(input()) for j in A: if j == "(": i += 1 if j == ")" and i > 0: i -= 1 k += 1 print(2 * k)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def main(): s = input() stack = [] drop = 0 for i in range(len(s)): if s[i] == "(": stack.append(s[i]) elif not stack: drop += 1 else: stack.pop() drop += len(stack) print(len(s) - drop) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
b = input() i = 0 N = 0 x = 0 y = 0 while i < len(b): if b[i] == "(": x = x + 1 elif b[i] == ")": y = y + 1 x = x - 1 if x >= 0: N = N + 2 else: x = 0 i = i + 1 print(N)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() a = list() cnt = 0 for i in range(len(s)): if s[i] == "(": a.append("(") elif len(a) == 0: cnt += 1 else: a.pop() cnt += len(a) print(len(s) - cnt)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
l = list(input()) x = [] output = 0 for i in l: if len(x) > 0: if x[-1] == "(" and i == ")": x.pop() output += 2 else: x.append(i) else: x.append(i) print(output)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
Brackets, Open, Close = input().lstrip(")"), 0, 0 Temp = "" for i in Brackets: if i == "(": Open += 1 elif Open > Close: Close += 1 print(min(Open, Close) * 2)
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def regular_bracket_sequence(brackets): wrong_open = [] wrong_close = [] for i in range(len(brackets)): if brackets[i] == "(": wrong_open.append(i) elif len(wrong_open) > 0: wrong_open.pop() else: wrong_close.append(i) return len(brackets) - len(wrong_open) - len(wrong_close) b = input() print(regular_bracket_sequence(b))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() ml = 0 l = 0 r = 0 for i in range(len(s)): if s[i] == "(": l += 1 elif l > r: r += 1 ml = max(ml, r) print(2 * r)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
brackets = list(input()) open_brackets = 0 pair_brackets = 0 for bracket in brackets: if bracket == "(": open_brackets += 1 if bracket == ")": if open_brackets >= 1: pair_brackets += 1 open_brackets -= 1 print(pair_brackets * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
p = [] ch = str(input()) x = 0 for i in range(len(ch)): if ch[i] == "(": p.append(ch[i]) elif ch[i] == ")" and len(p) != 0: p.pop() x = x + 2 print(x)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
seq = input() stack = [] inc = 0 for c in seq: if c == "(": stack.append("x") inc += 1 if c == ")": if len(stack) > 0: stack.pop() inc += 1 print(inc - len(stack))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() c = 0 stk = [] for i in s: if i == "(": stk.append(i) elif stk: c += 2 stk.pop() print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
ch = input() p = 0 t = 0 for i in range(len(ch)): if ch[i] == "(": p += 1 if p > t and ch[i] == ")": t += 1 print(2 * min(t, p))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() mo = res = dong = 0 for i in range(len(s)): if s[i] == "(": mo += 1 elif mo > dong: dong += 1 res = max(res, min(mo, dong) * 2) print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() k = 0 bal = 0 for c in s: if c == "(": bal += 1 else: bal -= 1 if bal < 0: bal = 0 k += 1 k = len(s) - k - bal print(k)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
sequence = input() counter = 0 maxn = 0 for x in range(len(sequence)): if sequence[x] == "(": counter += 1 elif counter > 0: counter -= 1 maxn += 2 print(maxn)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
x = input() s = ["a"] count = 0 for i in x: if i == ")": if s[-1] == "(": del s[-1] count += 2 else: s.append(i) else: s.append(i) print(count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = list(input()) b = [0] c = 0 if len(a) == 1: print("0") else: for i in range(len(a)): if a[i] == "(": b.append(a[i]) elif a[i] == ")": if b[len(b) - 1] == "(": c = c + 2 b.pop() print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = input() n = 0 tot = 0 i = 0 while i < len(a): if a[i] == ")" and n == 0: i += 1 elif a[i] == ")" and n > 0: n -= 1 tot += 1 i += 1 elif a[i] == "(": n += 1 i += 1 print(tot * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
b = input() N = 0 x = 0 for i in range(b.find("("), len(b)): if b[i] == "(": x = x + 1 else: x = x - 1 if x >= 0: N = N + 2 else: x = 0 print(N)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s, stack, cnt = input(), [], 0 for i in range(len(s)): if s[i] == "(": stack.append(s[i]) elif len(stack) > 0: if stack.pop() != "(": cnt += 1 else: cnt += 1 print(len(s) - cnt - len(stack))
ASSIGN VAR VAR VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
b = input() i = b.find("(") N = 0 x = 0 while i < len(b): if b[i] == "(": x = x + 1 else: x = x - 1 if x >= 0: N = N + 2 else: x = 0 i = i + 1 print(N)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() n = len(s) l = r = 0 err = 0 for i in s: if i == "(": l += 1 elif l > 0: l -= 1 else: r += 1 print(n - l - r)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
cou = 0 a = input() ans = 0 for j in range(len(a)): if a[j] == "(": cou += 1 elif cou > 0: cou -= 1 ans += 1 print(ans * 2)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def solve(s, n): stack = [] cnt = 0 for i in range(n): if s[i] == "(": stack.append(s[i]) elif len(stack) > 0: p = stack.pop() if p == "(": continue else: cnt += 1 else: cnt += 1 cnt += len(stack) return n - cnt s = input() n = len(s) print(solve(s, n))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR STRING VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() st = [] for i in s: if i == ")": if len(st) != 0 and st[-1] == "(": st.pop() else: st.append(")") else: st.append(i) print(len(s) - len(st))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
L = [] a = 0 s = input() for i in range(len(s)): if s[i] == "(": L.append(s[i]) if s[i] == ")" and len(L) > 0: L.pop() elif len(L) == 0 and s[i] == ")": a += 1 print(len(s) - len(L) - a)
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() stack = [] c = 0 for i in range(len(s)): if s[i] == "(": stack.append("(") else: if stack == []: stack.append(s[i]) if stack[-1] == "(": stack.pop() c += 2 print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR LIST EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def check(n): e = 0 b = 0 for i in n: if i == "(": b += 1 if i == ")" and b > 0: e += 1 b -= 1 return e n = input() print(check(n) * 2)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def solution(): s = input().strip() pos = 0 total = 0 for c in s: if c == "(": pos += 1 else: pos -= 1 if pos >= 0: total += 2 else: pos += 1 print(total) solution()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = input().strip() st = [] c = 0 for i in a: if i == "(": st.append(i) elif len(st) != 0: st.pop() c += 2 print(c)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def main(): ch = str(input()) n = len(ch) L = [] nb = 0 for i in range(n): c = ch[i] if c == "(": L.append(c) elif len(L) == 0: nb += 1 else: L.pop() print(n - nb - len(L)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
count = 0 seq = input() remove = 0 for c in seq: if c == ")": if count <= 0: remove += 1 else: count -= 1 else: count += 1 remove += count print(len(seq) - remove)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def main(): s = input() cnt = 0 req = 0 for c in s: if c == "(": cnt += 1 elif c == ")" and cnt > 0: cnt -= 1 else: req += 1 print(len(s) - (cnt + req)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
a = input("") n = 0 m = 0 for jj in a: if jj == "(": n = n + 1 elif n > 0: n = n - 1 m = m + 1 print(m * 2)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
o = 0 f = 0 s = input() l = [] for i in range(len(s)): l.append(s[i]) if s[i] == "(": o = o + 1 if s[i] == ")" and o == 0: f = f + 1 l.pop() if s[i] == ")" and o != 0: o = o - 1 l.pop() l.pop() print(len(s) - f - len(l))
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() a = [] for i in range(len(s)): if len(a) != 0 and s[a[-1]] == "(" and s[i] == ")": a.pop() else: a.append(i) print(len(s) - len(a))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
sequence = input() def maximum_length(sequence): opened = 0 closed = 0 for bracket in sequence: if bracket == "(": opened += 1 elif opened > 0: closed += 1 opened -= 1 return closed * 2 print(maximum_length(sequence))
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
import sys input = sys.stdin.readline def solve(): b = 0 r = 0 for i in input().strip(): if i == "(": b += 1 r += 1 elif b > 0: r += 1 b -= 1 print(r - b) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
ain = input() n = len(ain) a = [-1] * (n + 1) badplace = -1 counter = 0 cnt = 0 ans = 0 for i in range(0, n): if ain[i] is "(": counter += 1 elif counter > 0: counter -= 1 else: cnt += 1 print(n - cnt - counter)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
import sys input = sys.stdin.readline s = input().strip() d = 0 ans = 0 for c in s: if c == "(": d += 1 elif c == ")": if d > 0: ans += 2 d -= 1 d = max(0, d) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
stack = [] bracket = input() count = 0 for i in bracket: if i == "(": stack.append(i) elif stack != []: stack.pop() count += 2 print(count)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
n = input() comp = 0 long = len(n) if "(" not in n or ")" not in n: print(0) elif long == 1: print(0) else: while n[0] == ")": n = n[1:] long -= 1 if long == 1: break long = len(n) if long == 1: print(0) else: while n[-1] == "(": n = n[:-1] long -= 1 if long == 1: break long = len(n) if long == 1: print(0) else: for i in n: if i == "(": comp += 1 else: comp -= 1 if comp < 0: long -= 1 comp += 1 if comp > 0: print(long - comp) else: print(long)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() S = [] for x in s: if x == "(": S.append(x) elif len(S) == 0 or S[-1] == ")": S.append(x) else: S.pop() print(len(s) - len(S))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
stack = [] t = 0 a = input() for i in a: if i == "(": stack.append(i) t += 1 elif not stack: pass elif i == ")" and stack[-1] == "(": stack.pop() t += 1 else: pass print(t - len(stack))
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR IF VAR STRING VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
p = list() c = 0 def empiler(h, p): p.append(h) def depiler(p): p.pop() s = input() for i in range(len(s)): if s[i] == "(": empiler("(", p) elif len(p) != 0: depiler(p) c = c + 2 print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
ch = input() stk = [] length = len(ch) j = 0 for i in range(length): if ch[i] == ")" and stk == []: j += 1 if ch[i] == "(": stk.append(ch[i]) elif ch[i] == ")" and stk != []: stk.pop() print(length - len(stk) - j)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def bracket(): s = str(input()) c = 0 b = 0 for i in range(len(s)): if s[i] == "(": c += 1 elif s[i] == ")": c -= 1 if c >= 0: b += 2 elif c < 0: c = 0 print(b) bracket()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
m = input() cont = 0 cont2 = 0 B = [] C = [] for i in m: if i == "(": B.append(i) if i == ")": if len(B) != 0: if B.pop() == "(": cont += 1 else: cont2 += 1 print(cont * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() l = [] c = 0 for i in s: if l == []: if i == "(": l.append(i) elif i == "(": l.append(i) else: l.pop() c = c + 2 print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
brackets = [] count = 0 for c in input(): if c == "(": brackets.append(c) else: if not brackets: continue count += 2 brackets.pop() print(count)
ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
string = input() qtd_pares = 0 soma = 0 for s in string: if s == "(": soma += 1 else: if soma > 0: qtd_pares += 1 soma = max(soma - 1, 0) print(qtd_pares * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() a, n, cont = 0, len(s), 0 for i in range(n): if s[i] == "(": a += 1 elif s[i] == ")" and a > 0: a -= 1 cont += 1 print(cont * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
brackets = input() count = len(brackets) brackets_count = 0 for char in brackets: if char == "(": brackets_count += 1 elif char == ")": brackets_count -= 1 if brackets_count < 0: count -= 1 brackets_count = 0 print(count - brackets_count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
def check_bracket(seq: str) -> int: opened, finished = int(), int() for bracket in seq: if bracket == "(": opened += 1 elif bracket == ")" and opened: opened -= 1 finished += 2 return finished print(check_bracket(input()))
FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR VAR NUMBER VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
line = input() open_brackets = 0 closed_brackets = 0 history = [0] for i in line: if i == "(": open_brackets += 1 elif i == ")": closed_brackets += 1 if closed_brackets == open_brackets: history.append(open_brackets * 2) open_brackets = closed_brackets = 0 elif closed_brackets > open_brackets: open_brackets = closed_brackets = 0 if open_brackets > closed_brackets: history.append(closed_brackets * 2) print(sum(history))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
string = input() stack = ["#"] counter = 0 for i in range(0, string.__len__()): if string[i] == "(": stack.append("(") elif string[i] == ")" and stack[-1] == "(": stack.pop() else: counter += 1 print(string.__len__() - (counter + stack.__len__() - 1))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR STRING VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
lst = list(input()) ans = 0 q = [] for i in range(len(lst)): if len(q) == 0: q.append(lst[i]) elif q[len(q) - 1] == "(" and lst[i] == ")": ans += 2 q.pop() else: q.append(lst[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. During a boring night at the restaurant, Chef decided to play with spaghetti. He has $R-L+1$ spaghetti: exactly one spaghetti with each length between $L$ and $R$ inclusive. Chef decided to take some of his spaghetti and stick them together to form one very long spaghetti. The length of this spaghetti is the sum of the lengths of the spaghetti he took. In particular, he may take all spaghetti and he may take only one spaghetti as well. How many different spaghetti can he make? Two spaghetti are considered different if their lengths are different. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$ and $R$. ------ Output ------ For each test case, print a single line containing one integer ― the number of different spaghetti Chef can make. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ L, R ≤ 10^{9}$ ------ Subtasks ------ Subtask #1 (25 points): $L = 1$ Subtask #2 (55 points): $L, R ≤ 10^{4}$ Subtask #3 (20 points): original constraints ----- Sample Input 1 ------ 2 1 2 3 5 ----- Sample Output 1 ------ 3 7
import sys input = iter(sys.stdin.read().splitlines()).__next__ def Choose3(n): return n * (n - 1) * (n - 2) // 6 def A000125(n): return Choose3(n + 1) + n + 1 def sum_1toN(n): return n * (n + 1) // 2 def sum_1toN_squared(n): return n * (n + 1) * (2 * n + 1) // 6 def sum_odd_squares(n): return n * (4 * n * n - 1) // 3 def get_min_start(LEN): if LEN % 2 == 0: i = (LEN - 2) // 2 return i * i + i + 1 else: i = (LEN - 1) // 2 return i * i + 1 def solve(L, R): assert L <= R LEN = R - L + 1 if LEN == 1: return 2 - 1 if LEN == 2: return 4 - 1 res = A000125(LEN) min_start = get_min_start(LEN) if L < min_start: dif = min_start - L if LEN % 2 == 0: lo, hi = 1, 1000000000000000000 bs_res = 0 while lo <= hi: mid = lo + (hi - lo) // 2 if 2 * sum_1toN(mid) <= dif: lo = mid + 1 bs_res = mid else: hi = mid - 1 res -= 4 * sum_1toN_squared(bs_res) idx = min_start - 2 * sum_1toN(bs_res) step = (bs_res + 1) * 2 res -= (idx - L) * step else: lo, hi = 1, 1000000000000000000 bs_res = 0 while lo <= hi: mid = lo + (hi - lo) // 2 if mid * mid <= dif: lo = mid + 1 bs_res = mid else: hi = mid - 1 res -= sum_odd_squares(bs_res) idx = min_start - bs_res * bs_res step = (bs_res + 1) * 2 - 1 res -= (idx - L) * step res -= 1 return res TC = int(input()) for tc in range(TC): L, R = map(int, input().split()) res = solve(L, R) print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. During a boring night at the restaurant, Chef decided to play with spaghetti. He has $R-L+1$ spaghetti: exactly one spaghetti with each length between $L$ and $R$ inclusive. Chef decided to take some of his spaghetti and stick them together to form one very long spaghetti. The length of this spaghetti is the sum of the lengths of the spaghetti he took. In particular, he may take all spaghetti and he may take only one spaghetti as well. How many different spaghetti can he make? Two spaghetti are considered different if their lengths are different. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$ and $R$. ------ Output ------ For each test case, print a single line containing one integer ― the number of different spaghetti Chef can make. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ L, R ≤ 10^{9}$ ------ Subtasks ------ Subtask #1 (25 points): $L = 1$ Subtask #2 (55 points): $L, R ≤ 10^{4}$ Subtask #3 (20 points): original constraints ----- Sample Input 1 ------ 2 1 2 3 5 ----- Sample Output 1 ------ 3 7
def ar(a, n): return (a + a + n - 1) * n // 2 def pref(l, r, ile): return ar(l, ile) def suf(l, r, ile): return ar(r - ile + 1, ile) def sumka(b, a, n): add = a * b * n add += n add -= n * (n - 1) * (2 * n - 1) // 6 heh = b - a add += heh * (n - 1) * n // 2 return add def find_left(l, r, md): left = 1 right = md + 1 while left < right: mid = (left + right) // 2 if pref(l, r, mid + 1) > suf(l, r, mid): left = mid + 1 else: right = mid return left def find_right(l, r, md): left = md right = r - l + 1 while left < right: mid = (left + right) // 2 if pref(l, r, mid + 1) > suf(l, r, mid): right = mid else: left = mid + 1 return left def xdd(l, r, a, b): if b < a: return 0 ile = b - a + 1 s1 = r - a + 1 - l ile1 = a return sumka(s1, ile1, ile) t = int(input()) for asd in range(t): l, r = map(int, input().split()) if l == r: print(1) continue md = (r - l + 1) // 2 a = find_left(l, r, md) b = find_right(l, r, md) if a <= b: ans = 0 ans += suf(l, r, b) ans -= pref(l, r, a) - 1 ans += xdd(l, r, 1, a - 1) ans += xdd(l, r, b + 1, r - l + 1) else: ans = xdd(l, r, 1, r - l + 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps: choose two leaves; add the length of the simple path between them to the answer; remove one of the chosen leaves from the tree. Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex. Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer! -----Input----- The first line contains one integer number n (2 ≤ n ≤ 2·10^5) — the number of vertices in the tree. Next n - 1 lines describe the edges of the tree in form a_{i}, b_{i} (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}). It is guaranteed that given graph is a tree. -----Output----- In the first line print one integer number — maximal possible answer. In the next n - 1 lines print the operations in order of their applying in format a_{i}, b_{i}, c_{i}, where a_{i}, b_{i} — pair of the leaves that are chosen in the current operation (1 ≤ a_{i}, b_{i} ≤ n), c_{i} (1 ≤ c_{i} ≤ n, c_{i} = a_{i} or c_{i} = b_{i}) — choosen leaf that is removed from the tree in the current operation. See the examples for better understanding. -----Examples----- Input 3 1 2 1 3 Output 3 2 3 3 2 1 1 Input 5 1 2 1 3 2 4 2 5 Output 9 3 5 5 4 3 3 4 1 1 4 2 2
import sys def main(): n = int(input()) edges = list(map(int, sys.stdin.read().split())) tree_edges = dict() for i in range(n): tree_edges[i + 1] = set() for i in range(0, len(edges) - 1, 2): tree_edges[edges[i]].add(edges[i + 1]) tree_edges[edges[i + 1]].add(edges[i]) init_distants = [-1] * (n + 1) queue = [1] init_distants[1] = 0 while queue: next_queue = [] for process in queue: for next_vertex in tree_edges[process]: if init_distants[next_vertex] == -1: init_distants[next_vertex] = init_distants[process] + 1 next_queue.append(next_vertex) queue = next_queue head = init_distants.index(max(init_distants)) distants_from_head = [-1] * (n + 1) queue = [head] distants_from_head[head] = 0 while queue: next_queue = [] for process in queue: for next_vertex in tree_edges[process]: if distants_from_head[next_vertex] == -1: distants_from_head[next_vertex] = distants_from_head[process] + 1 next_queue.append(next_vertex) queue = next_queue tail = distants_from_head.index(max(distants_from_head)) distants_from_tail = [-1] * (n + 1) queue = [tail] distants_from_tail[tail] = 0 while queue: next_queue = [] for process in queue: for next_vertex in tree_edges[process]: if distants_from_tail[next_vertex] == -1: distants_from_tail[next_vertex] = distants_from_tail[process] + 1 next_queue.append(next_vertex) queue = next_queue path_len_sum = 0 removal_history = list() process_queue = [] for vertex, adj in list(tree_edges.items()): if len(adj) == 1: process_queue.append(vertex) while process_queue: next_queue = [] for leaf in process_queue: if leaf == head or leaf == tail: continue if distants_from_tail[leaf] > distants_from_head[leaf]: path_len_sum += distants_from_tail[leaf] new_leaves = [] for w in tree_edges[leaf]: tree_edges[w].remove(leaf) if len(tree_edges[w]) == 1: new_leaves.append(w) next_queue.extend(new_leaves) removal_history.append("{0} {1} {0}".format(leaf, tail)) else: path_len_sum += distants_from_head[leaf] new_leaves = [] for w in tree_edges[leaf]: tree_edges[w].remove(leaf) if len(tree_edges[w]) == 1: new_leaves.append(w) next_queue.extend(new_leaves) removal_history.append("{0} {1} {0}".format(leaf, head)) process_queue = next_queue process_queue = [tail] while process_queue: leaf = process_queue[0] if leaf == head: continue path_len_sum += distants_from_head[leaf] new_leaves = [] for w in tree_edges[leaf]: tree_edges[w].remove(leaf) if len(tree_edges[w]) == 1: new_leaves.append(w) process_queue = new_leaves removal_history.append("{0} {1} {0}".format(leaf, head)) print(str(path_len_sum)) sys.stdout.write("\n".join(removal_history)) sys.stdout.write("\n") main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps: choose two leaves; add the length of the simple path between them to the answer; remove one of the chosen leaves from the tree. Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex. Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer! -----Input----- The first line contains one integer number n (2 ≤ n ≤ 2·10^5) — the number of vertices in the tree. Next n - 1 lines describe the edges of the tree in form a_{i}, b_{i} (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}). It is guaranteed that given graph is a tree. -----Output----- In the first line print one integer number — maximal possible answer. In the next n - 1 lines print the operations in order of their applying in format a_{i}, b_{i}, c_{i}, where a_{i}, b_{i} — pair of the leaves that are chosen in the current operation (1 ≤ a_{i}, b_{i} ≤ n), c_{i} (1 ≤ c_{i} ≤ n, c_{i} = a_{i} or c_{i} = b_{i}) — choosen leaf that is removed from the tree in the current operation. See the examples for better understanding. -----Examples----- Input 3 1 2 1 3 Output 3 2 3 3 2 1 1 Input 5 1 2 1 3 2 4 2 5 Output 9 3 5 5 4 3 3 4 1 1 4 2 2
import sys input = sys.stdin.buffer.readline n = int(input()) adj = [[] for i in range(n + 1)] for i in range(n - 1): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) dist1 = [0] * (n + 1) dist2 = [0] * (n + 1) diameter = [] on_diameter = [0] * (n + 1) s = [1] path = [] vis = [0] * (n + 1) while s: c = s[-1] if not vis[c]: vis[c] = 1 path.append(c) dist1[c] = len(path) - 1 for ne in adj[c]: if not vis[ne]: s.append(ne) else: path.pop() s.pop() mx_dist = 0 endpoint1 = 0 for i in range(1, n + 1): if dist1[i] > mx_dist: mx_dist = dist1[i] endpoint1 = i s = [endpoint1] path = [] vis = [0] * (n + 1) while s: c = s[-1] if not vis[c]: vis[c] = 1 path.append(c) dist1[c] = len(path) - 1 for ne in adj[c]: if not vis[ne]: s.append(ne) else: path.pop() s.pop() mx_dist = 0 endpoint2 = 0 for i in range(1, n + 1): if dist1[i] > mx_dist: mx_dist = dist1[i] endpoint2 = i s = [endpoint2] path = [] vis = [0] * (n + 1) while s: c = s[-1] if not vis[c]: vis[c] = 1 path.append(c) if c == endpoint1: diameter = path.copy() dist2[c] = len(path) - 1 for ne in adj[c]: if not vis[ne]: s.append(ne) else: path.pop() s.pop() for i in diameter: on_diameter[i] = 1 mx_answer = 0 operations = [] leaves = [] for i in range(1, n + 1): if len(adj[i]) == 1 and not on_diameter[i]: leaves.append(i) degree = [len(adj[i]) for i in range(n + 1)] while leaves: c = leaves.pop() if dist1[c] > dist2[c]: mx_answer += dist1[c] operations.append([endpoint1, c, c]) else: mx_answer += dist2[c] operations.append([endpoint2, c, c]) for ne in adj[c]: degree[ne] -= 1 if degree[ne] == 1: leaves.append(ne) while len(diameter) > 1: c = diameter.pop() mx_answer += dist2[c] operations.append([endpoint2, c, c]) print(mx_answer) for operation in operations: print(*operation)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are n bus stops located along the street, the i-th of them is located at the distance a_{i} from the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, a_{i} < a_{i} + 1 for all i from 1 to n - 1. The bus starts its journey from the first stop, it passes stops 2, 3 and so on. It reaches the stop number n, turns around and goes in the opposite direction to stop 1, passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop n. During the day, the bus runs non-stop on this route. The bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number. One of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order. For example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5, ends at the stop number 3 and passes the stops as follows: $5 \rightarrow 6 \rightarrow 5 \rightarrow 4 \rightarrow 3$, then the request about this segment of the path will have form: 3, 4, 5, 5, 6. If the bus on the segment of the path from stop 5 to stop 3 has time to drive past the 1-th stop (i.e., if we consider a segment that ends with the second visit to stop 3 on the way from 5), then the request will have form: 1, 2, 2, 3, 3, 4, 5, 5, 6. You will have to repeat the Berland programmers achievement and implement this function. -----Input----- The first line contains integer n (2 ≤ n ≤ 2·10^5) — the number of stops. The second line contains n integers (1 ≤ a_{i} ≤ 10^9) — the distance from the i-th stop to the central square. The numbers in the second line go in the increasing order. The third line contains integer m (1 ≤ m ≤ 4·10^5) — the number of stops the bus visited on some segment of the path. The fourth line contains m integers (1 ≤ b_{i} ≤ n) — the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus. It is guaranteed that the query corresponds to some segment of the path. -----Output----- In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print - 1. -----Examples----- Input 6 2 3 5 7 11 13 5 3 4 5 5 6 Output 10 Input 6 2 3 5 7 11 13 9 1 2 2 3 3 4 5 5 6 Output 16 Input 3 10 200 300 4 1 2 2 3 Output -1 Input 3 1 2 3 4 1 2 2 3 Output 3 -----Note----- The first test from the statement demonstrates the first example shown in the statement of the problem. The second test from the statement demonstrates the second example shown in the statement of the problem. In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely. In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.
r = lambda: list(map(int, input().split())) ri = lambda: int(input()) n, a, m, b = ri(), r(), ri(), r() c = [0] * n for e in b: c[e - 1] += 1 c[0] *= 2 c[-1] *= 2 d = 0 df = 0 r = max(e // 2 for e in c) c = [(e - r * 2) for e in c] if any(c): for i in range(n - 1): de = a[i + 1] - a[i] d += min(c[i], c[i + 1]) * de df += de print(d + r * 2 * df) else: de = a[1] - a[0] for i in range(1, n - 1): if a[i + 1] - a[i] != de: print(-1) break else: print(r * de * 2 * (n - 1) - de)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR
In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are n bus stops located along the street, the i-th of them is located at the distance a_{i} from the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, a_{i} < a_{i} + 1 for all i from 1 to n - 1. The bus starts its journey from the first stop, it passes stops 2, 3 and so on. It reaches the stop number n, turns around and goes in the opposite direction to stop 1, passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop n. During the day, the bus runs non-stop on this route. The bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number. One of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order. For example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5, ends at the stop number 3 and passes the stops as follows: $5 \rightarrow 6 \rightarrow 5 \rightarrow 4 \rightarrow 3$, then the request about this segment of the path will have form: 3, 4, 5, 5, 6. If the bus on the segment of the path from stop 5 to stop 3 has time to drive past the 1-th stop (i.e., if we consider a segment that ends with the second visit to stop 3 on the way from 5), then the request will have form: 1, 2, 2, 3, 3, 4, 5, 5, 6. You will have to repeat the Berland programmers achievement and implement this function. -----Input----- The first line contains integer n (2 ≤ n ≤ 2·10^5) — the number of stops. The second line contains n integers (1 ≤ a_{i} ≤ 10^9) — the distance from the i-th stop to the central square. The numbers in the second line go in the increasing order. The third line contains integer m (1 ≤ m ≤ 4·10^5) — the number of stops the bus visited on some segment of the path. The fourth line contains m integers (1 ≤ b_{i} ≤ n) — the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus. It is guaranteed that the query corresponds to some segment of the path. -----Output----- In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print - 1. -----Examples----- Input 6 2 3 5 7 11 13 5 3 4 5 5 6 Output 10 Input 6 2 3 5 7 11 13 9 1 2 2 3 3 4 5 5 6 Output 16 Input 3 10 200 300 4 1 2 2 3 Output -1 Input 3 1 2 3 4 1 2 2 3 Output 3 -----Note----- The first test from the statement demonstrates the first example shown in the statement of the problem. The second test from the statement demonstrates the second example shown in the statement of the problem. In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely. In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.
n = int(input()) c = [0] * n a = [int(x) for x in input().split()] m = int(input()) b = [(int(x) - 1) for x in input().split()] for e in b: c[e] += 1 c[0] *= 2 c[-1] *= 2 d = 0 df = 0 r = max([(e // 2) for e in c]) c = [(e - r * 2) for e in c] if not any(c): de = a[1] - a[0] for i in range(1, n - 1): if a[i + 1] - a[i] != de: print(-1) break else: print(r * de * 2 * (n - 1) - de) else: for i in range(n - 1): de = a[i + 1] - a[i] d += min(c[i], c[i + 1]) * de df += de print(d + r * 2 * df)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
for _ in range(int(input())): i = 0 z = input() ans = [] while i + 2 < len(z): if z[i] == "t" and z[i + 1] == "w" and z[i + 2] == "o": if i < len(z) - 4 and z[i + 3] == "n" and z[i + 4] == "e": ans.append(i + 3) i += 5 else: ans.append(i + 2) i += 3 elif z[i] == "o" and z[i + 1] == "n" and z[i + 2] == "e": ans.append(i + 2) i += 3 else: i += 1 print(len(ans)) print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
def zf(s): n = len(s) z = [0] * n z[0] = n l = 0 r = 0 for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z t = int(input()) for i in range(t): s = input() ban1 = "one" ban2 = "two" ban21 = "twone" s1 = ban1 + "A" + s s2 = ban2 + "A" + s s21 = ban21 + "A" + s c = 0 l = len(s) z1 = zf(s1) z2 = zf(s2) z21 = zf(s21) ans = [] i = 0 while i < l: if z21[i + 6] == 5: c += 1 ans.append(i + 3) i += 5 continue if z1[i + 4] == 3: c += 1 ans.append(i + 2) i += 3 continue if z2[i + 4] == 3: c += 1 ans.append(i + 2) i += 3 continue i += 1 print(c) print(*ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
t = int(input()) for i in range(1, t + 1): a = [] s = input() j = 0 while j <= len(s) - 1: if s[j] == "o": u = True if j >= 2: if s[j - 2] == "t" and s[j - 1] == "w": if j <= len(s) - 2: if s[j + 1] == "o": a.append(j) else: a.append(j + 1) u = False else: a.append(j) if j <= len(s) - 3: if s[j + 2] == "e" and s[j + 1] == "n": if u: a.append(j + 2) j += 3 else: j += 1 else: j += 1 else: j += 1 print(len(a)) if len(a) != 0: print(*a) else: print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
t = int(input()) for _ in range(t): s = input() len_s = len(s) ans = [0, []] for i in range(len_s): if s[i] == "o": if ( i >= 2 and s[i - 1] == "w" and s[i - 2] == "t" and i + 2 < len_s and s[i + 1] == "n" and s[i + 2] == "e" ): ans[0] += 1 ans[1].append(i + 1) elif i >= 2 and s[i - 1] == "w" and s[i - 2] == "t": ans[0] += 1 ans[1].append(i) elif i + 2 < len_s and s[i + 1] == "n" and s[i + 2] == "e": ans[0] += 1 ans[1].append(i + 2) print(ans[0]) print(*ans[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
import sys input = lambda: sys.stdin.readline() for i in range(int(input())): one = 0 two = 0 twone = 0 s = list(input()) i = 0 ind = [] while i < len(s) - 4: if ( s[i] == "t" and s[i + 1] == "w" and s[i + 2] == "o" and s[i + 3] == "n" and s[i + 4] == "e" ): twone += 1 ind.append(i + 3) i += 5 elif s[i] == "o" and s[i + 1] == "n" and s[i + 2] == "e": one += 1 ind.append(i + 2) i += 3 elif s[i] == "t" and s[i + 1] == "w" and s[i + 2] == "o": two += 1 ind.append(i + 2) i += 3 else: i += 1 while i < len(s) - 2: if s[i] == "o" and s[i + 1] == "n" and s[i + 2] == "e": one += 1 ind.append(i + 2) i += 3 elif s[i] == "t" and s[i + 1] == "w" and s[i + 2] == "o": two += 1 ind.append(i + 2) i += 3 else: i += 1 print(one + two + twone) print(*ind)
IMPORT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
n = int(input()) Ans = [] for j in range(n): Ans_0 = [] s = input() len_s = len(s) s = list(s) for i in range(2, len_s): S = s[i - 2] + s[i - 1] + s[i] if i < len_s - 2: if S + s[i + 1] + s[i + 2] == "twone": Ans_0.append(i + 1) elif S == "one" or S == "two": if Ans_0: if Ans_0[-1] != i - 1: Ans_0.append(i) else: Ans_0.append(i) elif S == "one" or S == "two": if Ans_0: if Ans_0[-1] != i - 1: Ans_0.append(i) else: Ans_0.append(i) Ans.append(Ans_0) for i in range(len(Ans)): print(len(Ans[i])) print(*Ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING IF VAR IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR STRING IF VAR IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
t = int(input()) for i in range(t): s = input() indexes = [] j = 0 while j < len(s) - 2: if s[j] == "o" and s[j + 1] == "n" and s[j + 2] == "e": indexes.append(j + 1 + 1) j += 2 elif s[j] == "t" and s[j + 1] == "w" and s[j + 2] == "o": if j + 3 < len(s) and s[j + 3] == "n": indexes.append(j + 2 + 1) j += 3 else: indexes.append(j + 1 + 1) j += 2 j += 1 print(len(indexes)) print(*indexes)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two". For example: Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time. For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne". What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be? -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given. Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters. It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$. -----Output----- Print an answer for each test case in the input in order of their appearance. The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them. -----Examples----- Input 4 onetwone testme oneoneone twotwo Output 2 6 3 0 3 4 1 7 2 1 4 Input 10 onetwonetwooneooonetwooo two one twooooo ttttwo ttwwoo ooone onnne oneeeee oneeeeeeetwooooo Output 6 18 11 12 1 6 21 1 1 1 3 1 2 1 6 0 1 4 0 1 1 2 1 11 -----Note----- In the first example, answers are: "onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo". In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
t = int(input()) ans = [] for _ in range(t): s = input() p = [] i = 0 n = len(s) while i + 3 <= n: if i + 5 <= n and s[i : i + 5] == "twone": p.append(i + 3) i += 5 elif s[i : i + 3] == "one": p.append(i + 2) i += 3 elif s[i : i + 3] == "two": p.append(i + 2) i += 3 else: i += 1 ans.append(p.copy()) for a in ans: print(len(a)) print(" ".join(list(map(str, a))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR