#K36352. Dormitory Assignment Based on Student Preferences

    ID: 25735 Type: Default 1000ms 256MiB

Dormitory Assignment Based on Student Preferences

Dormitory Assignment Based on Student Preferences

In this problem, you are given several dormitories with fixed capacities and a list of students with their dormitory preferences. Your task is to assign each student to a dormitory based on their stated preferences and the remaining capacities. If a student’s preferred dormitory is full or if the student does not have any valid preference (indicated by -1), the student must be assigned to dormitory 1 by default.

Constraints:
- Dormitories are numbered from 1 to M.
- It is guaranteed that the total dormitory capacity is at least the number of students, i.e., $$\sum_{j=1}^{M} c_j \geq N$$ where $$c_j$$ is the capacity of dormitory j and N is the total number of students.

Solve this assignment problem taking into account all constraints.

inputFormat

Input is read from standard input (stdin). The input consists of multiple lines:
1. The first line contains an integer M, representing the number of dormitories.
2. The second line contains M space-separated integers indicating the capacities of the dormitories.
3. The third line contains an integer N, representing the total number of students.
4. This is followed by N lines, each containing M space-separated integers representing the dormitory preferences for each student. A value of -1 indicates a lack of preference at that position.

outputFormat

Output a single line to standard output (stdout) containing N space-separated integers. The i-th integer represents the dormitory number assigned to the i-th student.## sample

3
2 1 1
4
1 2 3
2 1 3
3 1 2
3 -1 -1
1 2 3 1