## DMPG '16 G1 - Explooooosion!

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Megumin loves magical explosions. Since you are Megumin's most trusted adviser, you want to help her to create the most spectacular explosion ever.

Megumin currently has magical components that must all be combined together to create an explosion. However, Megumin can combine the components in any order she wants, and the specific order which she does this will result in a differing size of the resulting explosion.

Each component has a potency value . There are only two known ways to combine two components and into a single component:

• Controlled combination: the two components will combine together to make a new component with potency .
• Unstable combination: the two components will combine together to make a new component with potency .

After Megumin applies operations times, the size of the resulting explosion is equal to the potency of the only remaining component. Since Megumin appreciates the beauty of both a small and intense and a large and flashy explosion, you would like to determine what the sizes of the smallest and largest explosions Megumin can possibly make are. Since these numbers may be very large, you should output the results modulo .

#### Input Specification

The first line of input will contain the integer .
The second line of input will contain integers, .

#### Output Specification

The first line of output should contain the smallest explosion Megumin can make.
The second line of output should contain the largest explosion Megumin can make.

#### Constraints

##### Subtask 2 [35%]

At most values of will be equal to .

#### Sample Input

3
2 1 3

#### Sample Output

5
9

#### Explanation of Output for Sample Input

To make the smallest explosion, first Megumin should combine the components with potencies 2 and 3 with a controlled combination and then combine the remaining components with unstable combinations.
To make the largest explosion, first Megumin should combine the components with potencies 1 and 2 with a controlled combination and then combine the remaining components with unstable combinations.

• commented on July 24, 2017, 4:45 a.m.

Prevent stack overflow?

How can I prevent my submission from overflowing due to the excessively large integers resulting from multiplying the list?

• commented on July 24, 2017, 4:52 a.m. edit 2

Taking the modulus of a number after multiplication and addition won't affect the answer. (I'm guessing you meant number overflow rather than stack overflow)

• commented on July 24, 2017, 1:35 p.m.

Thank you my friend. Thank you.

• commented on April 27, 2017, 3:01 p.m.

pls help me I don't know why my submission is wrong?

• commented on April 28, 2017, 8:33 p.m.

I would look into what happens when you combine components of size 2 and 1 and how it compares to what happens when you just add 1 to the lowest component.