Problem:
Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No

Solution in C# (Mono-csc 3.5.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
  class Program
  {
    static void Main(string[] args)
    {
      ABC167.F();
    }
    class ABC167
    {
      public static void A()
      {
        var S = Read.Str();
        var T = Read.Str();
        Console.WriteLine(S == T.Substring(0, S.Length) ? "Yes" : "No");
      }
      public static void B()
      {
        var A = Read.Long();
        var B = Read.Long();
        var C = Read.Long();
        var K = Read.Long();
        if (K <= A) { Console.WriteLine(K); return; }
        if (K <= A + B) { Console.WriteLine(A); return; }
        Console.WriteLine(A - (K - A - B));
      }
      public static void C()
      {
        var N = Read.Int();
        var M = Read.Int();
        var X = Read.Int();
        var CA = Enumerable.Range(0, N).Select(i => Read.IntN()).ToArray();
        int min = (int)1e8;
        for(int i = 0; i < (1 << N); ++i)
        {
          var sum = new int[M];
          var cost = 0;
          for(int j = 0; j < N; ++j)
          {
            if((i & (1 << j)) > 0)
            {
              cost += CA[j][0];
              for(int k = 0; k < M; ++k)
              {
                sum[k] += CA[j][k + 1];
              }
            }
          }
          if (sum.Count(x => x >= X) == M)
          {
            min = Math.Min(min, cost);
          }
        }
        Console.WriteLine(min == (int)1e8 ? -1 : min);
      }
      public static void D()
      {
        var N = Read.Int();
        var K = Read.Long();
        var A = Read.IntN();

        List<int> list = new List<int>();
        var hash = new HashSet<int>();
        int current = 1;
        list.Add(current);
        hash.Add(current);
        while (true)
        {
          var to = A[current - 1];
          list.Add(to);
          if (hash.Contains(to))
          {
            break;
          }
          hash.Add(to);
          current = to;
        }
        var last = list.Last();
        var idx = list.IndexOf(last);
        if (K < list.Count) { Console.WriteLine(list[(int)K]); return; }
        var K2 = (K - idx) % (list.Count - idx - 1);
        Console.WriteLine(list[idx + (int)K2]);
      }
      public static void E()
      {
        var N = Read.Int();
        var M = Read.Int();
        var K = Read.Int();
        var comb = Util.Combin(N + M + 10, Util.P);
        MOD ans = 0;
        for(int i = 0; i <= K; ++i)
        {
          var a = ((MOD)M) * ((MOD)(M - 1)).Power(N - i - 1);
          var b = comb(N - 1, i);
          ans += a * b;
        }
        Console.WriteLine(ans.V);
      }
      public static void F()
      {
        var N = Read.Int();
        var S = Enumerable.Range(0, N).Select(i => Read.Str()).ToArray();
        var X = new List<Tuple<int, int, int>>();
        foreach(var s in S)
        {
          var c1 = s.Count(x => x == '(');
          var c2 = s.Count(x => x == ')');
          int min = 0;
          int cur = 0;
          for(int i = 0; i < s.Length; ++i)
          {
            if (s[i] == '(') { cur--; } else { cur++; }
            min = Math.Max(min, cur);
          }
          X.Add(new Tuple<int, int, int>(c1, c2, min));
        }
        var sum1 = X.Select(x => x.Item1).Sum();
        var sum2 = X.Select(x => x.Item2).Sum();
        if (sum1 != sum2) { Console.WriteLine("No"); return; }
        var X0 = new List<Tuple<int, int, int>>();
        var X1 = new List<Tuple<int, int, int>>();
        var X2 = new List<Tuple<int, int, int>>();
        foreach(var x in X)
        {
          if (x.Item1 > x.Item2)
          {
            X0.Add(x);
          }
          else if (x.Item1 == x.Item2)
          {
            X1.Add(x);
          }
          else
          {
            X2.Add(x);
          }
        }
        X0 = X0.OrderBy(x => x.Item3).ToList();
        int current = 0;
        for(int i = 0; i < X0.Count; ++i)
        {
          var x = X0[i];
          if(current < x.Item3)
          {
            Console.WriteLine("No"); return;
          }
          current += x.Item1 - x.Item2;
        }
        for(int i = 0; i < X1.Count; ++i)
        {
          var x = X1[i];
          if (current < x.Item3)
          {
            Console.WriteLine("No"); return;
          }
        }
        X2 = X2.OrderBy(x => -x.Item3).ToList();
        for (int i = 0; i < X2.Count; ++i)
        {
          var x = X2[i];
          if (current < x.Item3)
          {
            Console.WriteLine("No"); return;
          }
          current += x.Item1 - x.Item2;
        }
        Console.WriteLine("Yes");
      }
    }
    class Util
    {
      public static readonly long P = 998244353;
      public static long[] ASum(long[] array)
      {
        var res = new long[array.Length + 1];
        for (int i = 0; i < array.Length; ++i)
        {
          res[i + 1] = res[i] + array[i];
        }
        return res;
      }
      public static Dictionary<T, long> Frequency<T>(T[] array)
      {
        var dict = new Dictionary<T, long>();
        foreach (var a in array)
        {
          if (!dict.ContainsKey(a)) { dict.Add(a, 0); }
          dict[a]++;
        }
        return dict;
      }
      public static long Power(long a, long n, long p)
      {
        long res = 1;
        long mult = a;
        for (int i = 0; i < 63; ++i)
        {
          if ((n & ((long)1 << i)) > 0)
          {
            res = (res * mult) % p;
          }
          mult = (mult * mult) % p;
        }
        return res;
      }
      public static long Inverse(long a, long p)
      {
        long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0)
        {
          e = a / b;
          f = b; b = a - e * b; a = f;
          f = d; d = c - e * d; c = f;
        }
        c %= p;
        return c + (c < 0 ? p : 0);
      }

      public static long[] Facts(int n, long p)
      {
        var facts = new long[n + 1];
        facts[0] = 1;
        for (int i = 1; i <= n; ++i)
        {
          facts[i] = (i * facts[i - 1]) % p;
        }
        return facts;
      }
      public static Func<int, int, long> Combin(int n, long P)
      {
        var fact = Facts(n, P);
        var inv = fact.Select(x => Inverse(x, P)).ToArray();
        return (N, M) =>
        {
          if (N < 0 || N < M || M < 0) { return 0; }
          return (((fact[N] * inv[M]) % P) * inv[N - M]) % P;
        };
      }

    }
    struct MOD
    {
      public static long P = 998244353;
      public readonly long V;

      public MOD(long a) { this.V = a % P; }
      public static implicit operator MOD(long a) { return new MOD(a); }
      public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
      public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
      public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
      public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
      public MOD Inverse()
      {
        long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; }
        c %= p;
        return c + (c < 0 ? p : 0);
      }
      public MOD Power(long n)
      {
        long a = V, p = P; long res = 1; long mult = a;
        for (int i = 0; i < 63; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; }
        return res;
      }
      public override string ToString() { return V.ToString(); }
    }


    static class Read
{
static Queue<string> queue = new Queue<string>();
static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
public static ulong[] ULongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToUInt64(s)).ToArray(); }
public static string[] StrN() { return Console.ReadLine().Split(' '); }
public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; }
public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
        for (int i = 0; i < M; ++i) { var ab = Read.IntN(); if (!dict[ab[0]].Contains(ab[1])) { dict[ab[0]].Add(ab[1]); } if (!isDirectional && !dict[ab[1]].Contains(ab[0])) { dict[ab[1]].Add(ab[0]); } }
        return dict;
}
public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
{
Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); }
return dict;
}
}
    }
}