Back to School '24 P4 - Candidates

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

There are N candidates who applied for a prestigious scholarship program. Each candidate is evaluated based on K different factors. The rating of the i-th candidate on the j-th factor is r_{i,j}, where higher values indicate better performance.

The administration of the program has a secret importance ordering of the factors, which is a permutation of the numbers from 1 to K. For example, if the importance ordering is (3, 1, 2, 4), it means that the third factor is the most important, followed by the first factor, then the second factor, and finally the fourth factor.

The importance ordering determines how the candidates are ranked. The candidates are first compared based on the most important factor, and ties are broken by the second most important factor, then the third most important, and so on. If candidates have identical ratings for all K factors, the ties will be broken arbitrarily; those candidates can appear in any order. The administration has released the final ranking of the candidates, but not the importance ordering.

Given the ratings of all N candidates on the K factors and the final ranking, A, can you find the secret importance ordering? If multiple possible orderings produce the same ranking, output the lexicographically minimal one. If no such ordering exists, output -1.

Constraints

1 \le N \le 10^5

1 \le K \le 3 \times 10^5

1 \le N \times K \le 3 \times 10^6

1 \le A_i \le N

A_i \not= A_j (i \not= j)

1 \le r_{i,j} \le N

Subtask 1 [30%]

1 \le K \le 30

Subtask 2 [70%]

No additional constraints.

Input Specification

The first line of input contains two integers N, K, the number of candidates and the number of factors, respectively.

The second line of input contains N integers, the array A. The i-th integer is the index of the candidate who ranked i-th.

The next N lines each contain K integers, representing the ratings of the candidates. The i-th line contains the ratings of the i-th candidate, r_{i,1}, r_{i,2}, \ldots, r_{i,K}.

Output Specification

If a valid ordering of the factors exists, then output a single line containing K integers, representing the ordering of the factors. If there are multiple possible orderings that produce the same ranking, output the lexicographically minimal one. Otherwise, output -1.

Sample Input

11 3
11 4 6 1 3 10 5 7 8 2 9
4 4 2
2 1 2
2 4 2
3 4 4
4 3 4
1 4 4
2 3 3
4 1 4
1 1 2
1 4 1
3 4 4

Sample Output

2 3 1

Comments

There are no comments at the moment.