Kaldorian Knights
Time limit2sMemory limit1024 MB
Count rankings of n knights where the last k_1 + ... + k_l places never hold only knights of houses 1 to l, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
The king of Kaldoria traditionally celebrates his birthday by inviting the knights of his realm to a big jousting tournament, and every noble house participates by sending their best knights to win fame and glory. At the end of the tournament, the king does not only choose a winner but ranks all knights from worst to best.
The number of knights belonging to house is denoted by . Each knight serves at most one house. Some knights may not serve any house. The houses are ordered by their influence in the kingdom, with the first one being the most influential. If the knights of the most powerful house take the last places in the tournament, the house will incite a revolt against the king and crown. The members of the second most influential house are not that powerful. Even if all of its knights end up at the bottom of the ranking, it would be a strong provocation, but the house would not be able to start a revolt. However, if the last places are taken by all the knights of the two most influential houses combined, the two houses will unite and fight the king. More generally, if the knights of the most powerful houses occupy the last places in the tournament, they will unite and incite a revolt.
A revolt must be avoided at all cost. The king often chooses the ranking on the spot without much thought, so the chief mathematician of the crown has been asked to count how many rankings will not lead to a revolt.
Input
The first line contains integers () and (), the number of knights and the number of houses. Then lines follow, where the -th line contains an integer (), the number of knights from house . Every house has at least one knight. It holds that .
Output
Output the number of rankings that do not lead to a revolt, modulo , since this number can be quite large.