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# (.NET Core 3.1.201) (judge verdict: Runtime Error)

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

namespace AtCoder.Contests.ABC167
{
    /// <summary>
    /// 
    /// </summary>
    static class F
    {
        static void Main(string[] args)
        {
            var n = NextInt();
            var list = new List<K>(n);

            for (int i = 0; i < n; i++)
            {
                var s = Next();
                var open = 0;
                var close = 0;
                for (int j = 0; j < s.Length; j++)
                {
                    var c = s[j];
                    if (c == '(')
                    {
                        open++;
                    }
                    else
                    {
                        if (open > 0)
                        {
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }

                }
                list.Add(new K() { Open = open, Close = close });
            }

            //open と close の数がずれてる場合
            if (list.Sum(k => k.Open - k.Close) != 0)
            {
                Console.WriteLine("No");
                return;
            }

            //open, closeできない
            if (!list.Any(k => k.Close == 0 && k.Open > 0) || !list.Any(k => k.Open == 0 && k.Close > 0))
            {
                Console.WriteLine("No");
                return;
            }


            var comp = Comparer<K>.Create((a, b) => a.Close.CompareTo(b.Close));
            //実際に作る
            var set = new Set<K>(comp) { IsMultiSet = true };
            foreach (var item in list)
            {
                set.Add(item);
            }

            var o = 0;
            //var set = new Set<K>((a, b) => a.Close.CompareTo()) { IsMultiSet = true };

            while (set.Count > 0)
            {
                var dum = new K { Close = o };

                var index = set.UpperBound(dum) - 1;

                var items = set.Items;
                if (index == 0 && items[0].Close > o)
                {
                    Console.WriteLine("No");
                    return;
                }

                var max = int.MinValue;
                var maxi = -1;
                for (int i = 0; i <= index; i++)
                {
                    var k = items[i];
                    if (k.Close > o) continue;
                    if (max < k.Open - k.Close)
                    {
                        maxi = i;
                        max = k.Open - k.Close;
                    }
                }

                o += max;
                set.RemoveAt(maxi);
            }

            Console.WriteLine("Yes");
        }

        struct K
        {
            public int Open;
            public int Close;
        }
        public class Set<T>
        {
            Node root;
            readonly IComparer<T> comparer;
            readonly Node nil;
            public bool IsMultiSet { get; set; }
            public Set(IComparer<T> comparer)
            {
                nil = new Node(default(T));
                root = nil;
                this.comparer = comparer;
            }
            public Set(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
            public Set() : this(Comparer<T>.Default) { }
            public bool Add(T v)
            {
                return insert(ref root, v);
            }
            public bool Remove(T v)
            {
                return remove(ref root, v);
            }
            public T this[int index] { get { return find(root, index); } }
            public int Count { get { return root.Count; } }
            public void RemoveAt(int k)
            {
                if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
                removeAt(ref root, k);
            }
            public T[] Items
            {
                get
                {
                    var ret = new T[root.Count];
                    var k = 0;
                    walk(root, ret, ref k);
                    return ret;
                }
            }
            void walk(Node t, T[] a, ref int k)
            {
                if (t.Count == 0) return;
                walk(t.lst, a, ref k);
                a[k++] = t.Key;
                walk(t.rst, a, ref k);
            }

            bool insert(ref Node t, T key)
            {
                if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
                var cmp = comparer.Compare(t.Key, key);
                bool res;
                if (cmp > 0)
                    res = insert(ref t.lst, key);
                else if (cmp == 0)
                {
                    if (IsMultiSet) res = insert(ref t.lst, key);
                    else return false;
                }
                else res = insert(ref t.rst, key);
                balance(ref t);
                return res;
            }
            bool remove(ref Node t, T key)
            {
                if (t.Count == 0) return false;
                var cmp = comparer.Compare(key, t.Key);
                bool ret;
                if (cmp < 0) ret = remove(ref t.lst, key);
                else if (cmp > 0) ret = remove(ref t.rst, key);
                else
                {
                    ret = true;
                    var k = t.lst.Count;
                    if (k == 0) { t = t.rst; return true; }
                    if (t.rst.Count == 0) { t = t.lst; return true; }


                    t.Key = find(t.lst, k - 1);
                    removeAt(ref t.lst, k - 1);
                }
                balance(ref t);
                return ret;
            }
            void removeAt(ref Node t, int k)
            {
                var cnt = t.lst.Count;
                if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
                else if (cnt > k) removeAt(ref t.lst, k);
                else
                {
                    if (cnt == 0) { t = t.rst; return; }
                    if (t.rst.Count == 0) { t = t.lst; return; }

                    t.Key = find(t.lst, k - 1);
                    removeAt(ref t.lst, k - 1);
                }
                balance(ref t);
            }
            void balance(ref Node t)
            {
                var balance = t.lst.Height - t.rst.Height;
                if (balance == -2)
                {
                    if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
                    rotL(ref t);
                }
                else if (balance == 2)
                {
                    if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
                    rotR(ref t);
                }
                else t.Update();
            }

            T find(Node t, int k)
            {
                if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
                for (; ; )
                {
                    if (k == t.lst.Count) return t.Key;
                    else if (k < t.lst.Count) t = t.lst;
                    else { k -= t.lst.Count + 1; t = t.rst; }
                }
            }
            public int LowerBound(T v)
            {
                var k = 0;
                var t = root;
                for (; ; )
                {
                    if (t.Count == 0) return k;
                    if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
                    else { k += t.lst.Count + 1; t = t.rst; }
                }
            }
            public int UpperBound(T v)
            {
                var k = 0;
                var t = root;
                for (; ; )
                {
                    if (t.Count == 0) return k;
                    if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
                    else t = t.lst;
                }
            }

            void rotR(ref Node t)
            {
                var l = t.lst;
                t.lst = l.rst;
                l.rst = t;
                t.Update();
                l.Update();
                t = l;
            }
            void rotL(ref Node t)
            {
                var r = t.rst;
                t.rst = r.lst;
                r.lst = t;
                t.Update();
                r.Update();
                t = r;
            }


            class Node
            {
                public Node(T key)
                {
                    Key = key;
                }
                public int Count { get; private set; }
                public sbyte Height { get; private set; }
                public T Key { get; set; }
                public Node lst, rst;
                public void Update()
                {
                    Count = 1 + lst.Count + rst.Count;
                    Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
                }
                public override string ToString()
                {
                    return string.Format("Count = {0}, Key = {1}", Count, Key);
                }
            }
        }


        #region Console
        static int _index = 0;
        static string[] _buffer;

        static string Next()
        {
            if (_buffer == null || _index >= _buffer.Length)
            {
                _buffer = Console.ReadLine().Split();
                _index = 0;
            }

            return _buffer[_index++];
        }
        static int NextInt()
        {
            return int.Parse(Next());
        }
        static long NextLong()
        {
            return long.Parse(Next());
        }
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        static int[] ReadIntArray(int n)
        {
            var a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadInt();
            }
            return a;
        }
        static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        static long[] ReadLongArray(int n)
        {
            var a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadLong();
            }
            return a;
        }
        static List<T> ReadObjects<T>(int n, Func<int[], T> creator)
        {
            var l = new List<T>(n);

            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                l.Add(creator(a));
            }

            return l;
        }

        static int[,] ReadMatrix(int n, int m)
        {
            var r = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                for (int j = 0; j < m; j++)
                {
                    r[i, j] = a[j];
                }
            }
            return r;
        }
        #endregion
    }
}