59 lines
1.2 KiB
Java
59 lines
1.2 KiB
Java
package chelper;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import io.InputReader;
|
|
import io.OutputWriter;
|
|
import solutions.ChelperSolution;
|
|
|
|
|
|
public class TaskC extends ChelperSolution {
|
|
|
|
@Override
|
|
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
|
super.solve(testNumber, in, out);
|
|
}
|
|
|
|
@Override
|
|
public void solve(int testNumber) {
|
|
int n = in.nextInt();
|
|
|
|
int equals = 0;
|
|
Map<Integer, Integer> counts = new HashMap<>();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
String s = in.nextString();
|
|
|
|
int balance = 0;
|
|
int min = 0;
|
|
for (char c : s.toCharArray()) {
|
|
if (c == '(') {
|
|
balance++;
|
|
} else {
|
|
balance--;
|
|
}
|
|
min = Math.min(min, balance);
|
|
}
|
|
|
|
if (min == 0 && balance == 0) {
|
|
equals++;
|
|
continue;
|
|
}
|
|
|
|
if ((min == 0 && balance > 0) || (min < 0 && balance == min)) {
|
|
counts.put(balance, 1 + counts.getOrDefault(balance, 0));
|
|
}
|
|
}
|
|
|
|
int res = equals / 2;
|
|
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
|
|
if (entry.getKey() > 0) {
|
|
res += Math.min(entry.getValue(), counts.getOrDefault(-entry.getKey(), 0));
|
|
}
|
|
}
|
|
|
|
out.println(res);
|
|
}
|
|
}
|