The Strongest Build
Contest: Educational Codeforces Round 114 (Rated for Div. 2)
Difficulty rating: 2000
Tags: binary search, brute force, data structures, dfs and similar, graphs, greedy, hashing, implementation

Ivan is playing yet another roguelike computer game. He controls a single hero in the game. The hero has $$$n$$$ equipment slots. There is a list of $$$c_i$$$ items for the $$$i$$$-th slot, the $$$j$$$-th of them increases the hero strength by $$$a_{i,j}$$$. The items for each slot are pairwise distinct and are listed in the increasing order of their strength increase. So, $$$a_{i,1} < a_{i,2} < \dots < a_{i,c_i}$$$.

For each slot Ivan chooses exactly one item. Let the chosen item for the $$$i$$$-th slot be the $$$b_i$$$-th item in the corresponding list. The sequence of choices $$$[b_1, b_2, \dots, b_n]$$$ is called a build.

The strength of a build is the sum of the strength increases of the items in it. Some builds are banned from the game. There is a list of $$$m$$$ pairwise distinct banned builds. It's guaranteed that there's at least one build that's not banned.

What is the build with the maximum strength that is not banned from the game? If there are multiple builds with maximum strength, print any of them.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10$$$) — the number of equipment slots.

The $$$i$$$-th of the next $$$n$$$ lines contains the description of the items for the $$$i$$$-th slot. First, one integer $$$c_i$$$ ($$$1 \le c_i \le 2 \cdot 10^5$$$) — the number of items for the $$$i$$$-th slot. Then $$$c_i$$$ integers $$$a_{i,1}, a_{i,2}, \dots, a_{i,c_i}$$$ ($$$1 \le a_{i,1} < a_{i,2} < \dots < a_{i,c_i} \le 10^8$$$).

The sum of $$$c_i$$$ doesn't exceed $$$2 \cdot 10^5$$$.

The next line contains a single integer $$$m$$$ ($$$0 \le m \le 10^5$$$) — the number of banned builds.

Each of the next $$$m$$$ lines contains a description of a banned build — a sequence of $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le c_i$$$).

The builds are pairwise distinct, and there's at least one build that's not banned.

Output format:
Print the build with the maximum strength that is not banned from the game. If there are multiple builds with maximum strength, print any of them.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
3
3 1 2 3
2 1 5
3 2 4 6
2
3 2 3
3 2 2

Example output:
2 2 3

Example input:
3
3 1 2 3
2 1 5
3 2 4 6
2
3 2 3
2 2 3

Example output:
1 2 3

Editorial:
Consider the bruteforce solution. You start with a build that contains the most powerful item for each slot. In one move, you swap an item in some slot for the one that is the previous one by power. If a build is not banned, update the answer with its total power (banned builds can be stored in a set, maybe hashset if you hash carefully enough).
Notice that if you reach some unbanned build in this bruteforce, it never makes sense to go further. The answer is already updated with this one, and all the lower ones have smaller power.
If you code that bruteforce in a smart way (or just add memorization), you won't visit any build twice. How many states will you visit, though? Since you can only proceed if you are standing in a banned build, you will check around $$$m + mn$$$ builds.
You can code it like that and get accepted. However, there's another way that's easier to code, in my opinion.
The optimal answer can be one of only two types. Either it contains the last item of each slot. Or it's some banned build with one item swapped with the previous one. It's easy to see from the solution above. So you can check the first type, then iterate over the banned build and try swapping each slot in it, checking if the resulting build is banned or not.
Overall complexity: $$$O(mn)$$$ or $$$O(mn \log m)$$$.