import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { void run() { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); String str = sc.next(); long ans = N * (N + 1) / 2; long sum = 0; long min = 0; long[][] a = new long[(int) N][2]; for (int i = 0; i < N; ++i) { sum += (str.charAt(i) == '(' ? 1 : -1); ans -= sum; min = Math.min(min, sum); a[i][0] = sum; a[i][1] = min; } Arrays.sort(a, new Comparator() { @Override public int compare(long[] o1, long[] o2) { return -Long.compare(Math.min(o1[1], o1[0] + o2[1]), Math.min(o2[1], o2[0] + o1[1])); } }); sum = 0; min = 0; for (int i = 0; i < N; ++i) { min = Math.min(min, a[i][1] + sum); sum += a[i][0]; } System.out.println(ans + 2 * min); } public static void main(String[] args) { new Main().run(); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }