using System.Linq; using System.Collections.Generic; using System; public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var m = int.Parse(line[1]); line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.Parse); getAns(n, m, a); } static void getAns(int n, int m, int[] a) { var b = new int[n, m]; for (int i = 0; i < n; i++) { string[] line = Console.ReadLine().Trim().Split(' '); for (int j = 0; j < m; j++) b[i, j] = int.Parse(line[j]); } var ans = new int[n]; var q = new Queue(); var q2 = new Queue(); foreach (var x in Enumerable.Range(0, n)) q.Enqueue(x); var p = 0; while (true) { while (q.Count > 0) { var t = q.Dequeue(); var room = b[t, p]; if (a[room] > 0) { ans[t] = room; a[room]--; } else q2.Enqueue(t); } if (q2.Count == 0) break; while (q2.Count > 0) q.Enqueue(q2.Dequeue()); p++; } Console.WriteLine(string.Join(" ", ans)); } }