#K92027. Maximizing Participant Preference in Activity Assignment

    ID: 38106 Type: Default 1000ms 256MiB

Maximizing Participant Preference in Activity Assignment

Maximizing Participant Preference in Activity Assignment

You are given m activities and p participants. Each activity has a limited number of available slots described by an array slots of length m, and each participant has given their preferences for the activities in the form of a list of m ranking values. The ranking value indicates the preference order for each activity (with lower numbers representing higher priority).

The task is to assign each participant to at most one activity such that the total number of participants who get assigned their higher-preference activity is maximized. The assignment procedure works in rounds. In the first round, every participant is considered for their most-preferred activity (ranking = 1). In the next round, participants who were not assigned are considered for the activity for which their ranking is 2, and so on. An activity can only be assigned to as many participants as available slots. If a participant cannot be assigned any activity due to lack of available slots, then they are assigned 0.

The activities in the output are numbered from 1 to m and participants are considered in the order of input.

This problem involves greedy assignment based on ranking priorities and capacity constraints, and requires you to maximize the number of top preferences fulfilled.

inputFormat

The input is read from standard input (stdin) in the following format:

  1. The first line contains two integers, m (the number of activities) and p (the number of participants), separated by a space.
  2. The second line contains m integers representing the available slots for each activity.
  3. Then follow p lines, each containing m integers. The i-th line contains the preference rankings for the activities for the i-th participant. Each ranking is between 1 and m, with 1 being the highest preference.

outputFormat

Output a single line containing p integers separated by a space. The i-th integer is the 1-indexed activity assigned to the i-th participant, or 0 if no activity could be assigned.

## sample
3 5
2 1 2
1 2 3
2 1 3
3 2 1
1 3 2
2 3 1
1 2 3 1 3