logo AlgoBeat OnlineJudge
登录 注册

#216202. [ICPC 2017 NAIPC] Pieces of Parentheses

内存限制:512 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are teaching a class in programming, and you want to cover balanced parentheses. You’ve got a great visual aid, a sign with a very long, balanced string of parentheses. But, alas, somehow, your visual aid has been broken into pieces, and some pieces may be missing! You’ve got to try to put it back together as best you can. Given the string of parentheses on each piece, what is the longest balanced string you can form by concatenating some of them in some order? Each piece may be used at most once, and the pieces cannot be reversed.

A balanced string of parentheses is defined as:

  1. The empty string
  2. where and are both balanced strings of parentheses
  3. where is a balanced string of parentheses

输入格式

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain a single integer (), which is the number of pieces.

Each of the next lines will hold a single string (), which consists only of the characters ‘(’ and ‘)’. This describes one of the pieces.

输出格式

Output a single integer, which is the length of the longest string of balanced parentheses you can form from the pieces. Note that the empty string is technically a balanced string of parentheses, so it is always possible to form a string of length at least 0 (although the empty string is not a very effective visual aid!).

样例

样例输入 1

3
())
((()
)()

样例输出 1

10

样例输入 2

5
)))))
)
((
))((
(

样例输出 2

2