Wizard Junkeun and the Magic Workshop
Time limit1sMemory limit1024 MB
Given N merge steps where each new stone is the sum of the two largest stones so far, print the last stone's value modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Junkeun is a wizard who runs a small magic workshop. At his workshop, he combines several mana stones into better ones. One day he gets tired of the work and decides to build a machine that merges mana stones automatically.
The machine merges mana stones as follows.
- Junkeun puts all of his mana stones into the machine.
- The machine selects of the stones it received.
- It combines the two selected stones into a new mana stone. The mana value of the new stone is the sum of the mana values of the two ingredients. The ingredients are not consumed.
- When it creates a new stone, it always creates the one with the largest mana value among the new stones it can make.
- A stone created this way can be used as an ingredient later.
- It returns all stones that were not used, all stones that were used, and the new stone as the result. Junkeun takes the result back.
Before starting the machine, Junkeun wants to know the mana value of the last stone created after repeating this process times. Given stones with mana values , , , , placed into the machine at the start, write a program that finds the mana value of the last stone created.
Input
The first line contains and , separated by a space.
The second line contains the integers , , , , , separated by spaces.
Output
Print the mana value of the last stone created. The absolute value can be very large, so express the value as using three integers , , and , and print instead. ()