Arranging game levels
Time limit1sMemory limit256 MB
Count rooted tree arrangements of N levels where each level's clear score S_i and the cumulative score K_i along the root-to-level path are given, and children must have larger S than parents.
- Level
Hard8 of 10
- Topics
- Tree, Combinatorics, Sorting, DFS
- Solved
- No attempts yet
Problem
Hyunwook makes games as a hobby, and today he sketched a simple design. The game has levels, and the player earns a fixed number of points for clearing each one. Every level has a fixed list of levels that the player may move to after clearing it, and the player picks one of them.
The design is as follows.
- There is one starting level.
- A way to reach any other level from the starting level always exists, and it is unique.
- Clearing level gives the player points.
- Each level also has a value , the cumulative score the designer intends the player to hold right after clearing level . It is the sum of the clear scores of every level on the way from the starting level to level , including level itself.
- If level has to be cleared after level , then must hold. The game only moves toward levels that award more points.
Hyunwook wants to know how many level arrangements satisfy these rules. Write a program that counts the arrangements of the levels that match all of the given and .
Input
The first line contains the number of levels ().
The second line contains (), separated by spaces.
The third line contains (), separated by spaces.
Output
Print the number of arrangements that satisfy the given and , modulo . Two arrangements count as the same one when every level has the same list of next levels in both, where the order inside a list does not matter.
Hint
The first example has exactly one valid arrangement. Level 1 is the starting level, and after clearing it the player chooses between level 2 and level 3. The second example has no valid arrangement.