Data Types and Variables - More Exercises -> 15. Balanced Brackets

Problem 1.* Balanced Brackets

You will receive n lines. On those lines, you will receive one of the following:

  • Opening bracket – “(“,
  • Closing bracket – “)” or
  • Random string

Your task is to find out if the brackets are balanced. That means after every closing bracket should follow an opening one. Nested parentheses are not valid, and if two consecutive opening brackets exist, the expression should be marked as unbalanced.

Input

  • On the first line, you will receive n – the number of lines, which will follow
  • On the next n lines, you will receive “(”, “)” or another string

Output

You have to print “BALANCED”, if the parentheses are balanced and “UNBALANCED” otherwise.

Constraints

  • n will be in the interval [1…20]
  • The length of the stings will be between [1…100] characters

Examples

Input

Output

 

Input

Output

8

(

5 + 10

)

* 2 +

(

5

)

-12

BALANCED

 

6

12 *

)

10 + 2 -

(

5 + 10

)

 

 

UNBALANCED

 

 

Здравейте. Опитвам се да реша тази задача и успях, но един от тестовете в judge не минава. Някой да има някакви идеи?

 

using System;

namespace BalancedBrackets
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = int.Parse(Console.ReadLine());
            string input = string.Empty;

            for (int i = 0; i < number; i++)
            {
                string temp = Console.ReadLine();

                if (temp == input)
                {
                    Console.WriteLine("UNBALANCED");
                    return;
                }

                if (temp == "(" || temp == ")")
                {
                    if (input == string.Empty && temp == ")")
                    {
                        Console.WriteLine("UNBALANCED");
                        return;
                    }
                    input = temp;
                }
            }

            Console.WriteLine("BALANCED");
        }
    }
}
 

https://judge.softuni.bg/Contests/Practice/Index/570#14