0% found this document useful (0 votes)
24 views2 pages

Brackets

The document describes a problem where Baby Bob wants to bracketize a sequence of positive integers so that the concatenated brackets form a valid mathematical expression when operators and operands are inserted. The input is the number of integers N followed by the sequence. The output must be a valid bracketization or -1 if none exists. The task is scored in subtasks of increasing difficulty and examples are provided.

Uploaded by

Cezar Dumitrica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Brackets

The document describes a problem where Baby Bob wants to bracketize a sequence of positive integers so that the concatenated brackets form a valid mathematical expression when operators and operands are inserted. The input is the number of integers N followed by the sequence. The output must be a valid bracketization or -1 if none exists. The task is scored in subtasks of increasing difficulty and examples are provided.

Uploaded by

Cezar Dumitrica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

I ( O T IIOT2024 – Round 1

+ I ) ; Online, November 13-14th, 2023 brackets • EN

Baby Bob’s Bracket Sequence (brackets)


Baby Bob is learning about mathematical expressions. He despises operands and operators, and only
likes round brackets.
He’s got a sequence A of positive integers A1 , A2 , . . . , AN . He wants to bracketize the sequence. A
bracketized sequence created from A is a sequence of strings B1 , B2 , . . . , BN such that each Bi has length
Ai , and Bi consists only of either opening brackets ’(’, or closing brackets ’)’, but not both.
For example let A = (1, 3, 4).

• A possible bracketized sequence created from A is ”)”, ”)))”, ”((((”.


• The sequence ”)”, ”)()”, ”((((” is not a bracketized sequence created from A, because the second
element consists of both opening and closing brackets.
• The sequence ”(”, ”))))”, ”((((” is not a bracketized sequence created from A, because the length of
the second element is not 3.
• The sequence ”(”, ”)” is not a bracketized sequence created from A, because it consists of only 2
strings.

Take the string B1 + B2 + . . . + BN (i.e., concatenate the elements of the bracketized sequence). Bob
wonders whether he can bracketize A so that the resulting string is a valid bracket sequence. A bracket
sequence is valid if ’1’ and ’+’ characters can be inserted into it so that it becomes a valid mathematical
expression. For example, ”(((())))” is a valid bracket sequence if A = (1, 3, 4).
Write a program that finds such a bracket sequence or determines that it’s impossible!

☞ Among the attachments of this task you may find a template file brackets.* with a sample
incomplete implementation.

Input
The first line contains the only integer N . The second line contains N integers Ai .

Output
You need to print a valid bracket sequence created from A or −1 if it’s not possible to create one.
If there are multiple correct bracket sequences, output any.

Constraints
• 1 ≤ N ≤ 500.
• 1 ≤ Ai for each i = 0 . . . N − 1.
• A1 + A2 + . . . + AN ≤ 50 000.

Scoring
Your program will be tested against several test cases grouped in subtasks. In order to obtain the score
of a subtask, your program needs to correctly solve all of its test cases.

brackets Page 1 of 2
– Subtask 1 (0 points) Examples.

– Subtask 2 (20 points) N ≤2

– Subtask 3 (30 points) N ≤ 20 and A1 + A2 + . . . + AN ≤ 200.

– Subtask 4 (50 points) No additional limitations.


Examples
input output

3 (((() ) ) )
1 3 4

4 (() ) ()
2 2 1 1

2 -1
2 1

Explanation
The first sample case is explained in the statement.
In the second sample case the bracketized sequence is ”((”, ”))”, ”(”, ”)”.

brackets Page 2 of 2

You might also like