luogu#P5032. 经验
经验
Background
The simplified version has been updated, and the time limit has been changed to 500 ms.
Save up enough experience to enchant~~
Steve often runs into problems in Minecraft: He wants to repair enchanted books, and the level of each book is . He never really understands the anvil repair and experience mechanisms, so he searched for some information on the wiki:

—The figure shows the relationship between experience and level.
After seeing this figure, he thought: the higher my level is, the more experience I need. So if my level is just enough for anvil repairing, wouldn’t I spend the least experience? He continued searching and attached the anvil mechanism below, asking you to help him solve the problem.
Problem Description
Prior Work Penalty
Whether it is renaming, repairing, or combining, the experience cost increases due to the item’s previous operations on an anvil. This extra cost is called the “prior work penalty”. For an item that has never been put on an anvil, the prior work penalty is .
After each anvil operation on an item (excluding renaming), its prior work penalty is multiplied by and then increased by . Therefore, after operations, the prior work penalty is . After operations, the prior work penalty is levels, and in survival mode no further repairing or enchanting can be done. After operations, the penalty level is , and no operations can be done in any mode.
When combining two items, the player suffers the prior work penalties of both items at the same time. The prior work penalty of the combined item is calculated based on the higher one of the two items. For example, combining two items with prior work penalties of and will add an extra penalty cost of levels, and the resulting item’s penalty is ().
The prior work penalty even applies to items that do not wear out, such as enchanted books. Therefore, combining Fortune I enchanted books will produce a Fortune III enchanted book with a prior work penalty of .
| Total operation count | Penalty |
|---|---|
Repairing items using the crafting grid removes all prior work penalties, but it also removes all enchantments.
Combining Items
In the anvil interface, the item in the first slot/on the left is called the target item; the item in the second slot/on the right is called the sacrifice item—it will disappear after combining. If the sacrifice item has enchantments, the anvil will also try to merge the sacrifice item’s enchantments into the target item. Regardless of whether the enchantments on the target item actually change, the anvil will charge the player a level cost based on the enchantments on both the target item and the sacrifice item.
For each enchantment on the sacrifice item, if the target item also has the same enchantment:
-
If the sacrifice item’s enchantment level is higher, the target item’s enchantment level will increase to the sacrifice item’s level.
-
If the sacrifice item’s enchantment level is the same, the target item’s enchantment level will increase by level, unless it is already at the maximum level.
-
If the sacrifice item’s enchantment level is lower, the target item’s enchantment level does not change.
The total cost of combining items is the sum of the following costs:
-
The sum of the target item’s and the sacrifice item’s prior work penalties.
-
If renaming is also performed, an additional renaming cost is added.
-
If the target item’s durability is not full, it costs levels for repairing.
-
If the sacrifice item has enchantments, an enchanting cost is added.
-
If the sacrifice item is an enchanted book, there is no repairing cost. The anvil will attempt to merge the book’s enchantments into the target item. Renaming the target item can also be performed at the same time. In this case, the enchanting cost is usually less than the cost of combining two similar items.
—Adapted from mcwiki with minor edits.
Simplified Version
You are given enchanted books. Only books of the same level can be combined. The cost of combining is the sum of the two books’ accumulated costs. The accumulated cost of the resulting book becomes times the maximum accumulated cost before combining, plus . Find the highest level and the minimum cost (the highest level is the primary objective). Steve is using cheats, so the highest level is unlimited.
Now you are given enchanted books, and each book has its level . Ask how to obtain the maximum level of an enchanted book. On this basis, compute the minimum level cost to combine it (we assume the initial prior work penalty of each book is ).
Steve is lazy. He does not want to read the above text; he only wants you to write a program to compute and . But to prevent it from being spread, he only asks you to output , the multiplicative inverse of modulo . If it does not exist, output .
Input Format
The first line contains an integer .
In the next lines, each line contains an integer , representing the initial level of each enchanted book. The data is not guaranteed to be sorted.
Output Format
Output one integer , representing the multiplicative inverse of modulo . If it does not exist, output .
PS: The multiplicative inverse can also be understood as: is the smallest positive integer such that .
5
1 1 2 3 4
-1
4
1 1 1 1
7
Hint
Sample Explanation
The first sample:
Combine two level books: cost experience, accumulated cost becomes ;
Then combine two level books: cost experience, accumulated cost becomes ;
Then combine two level books: cost experience, accumulated cost becomes ;
Finally combine two level books: cost experience, accumulated cost becomes .
Total experience cost: , maximum level: .
For the first sample: .
For the second sample: .
Constraints

The data is guaranteed to be random, and are within the range of long int.
Friendly Reminder
The input size of this problem is large, so please use a fast input method. Here is an example of fast input (requires including the header <cctype>):
#include<cctype>
inline void read(int &x){
char ch=getchar();x=0;
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
}
Translated by ChatGPT 5