// #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n, m; cin >> n >> m; vector cap(m); copy_n(istream_iterator(cin), m, cap.begin()); vector> prefer(n, vector(m)); for(auto &x: prefer | ranges::views::join){ cin >> x; } vector>> cur(m); vector exceeded_lab, active_lab; for(auto u = 0; u < n; ++ u){ int i = prefer[u][0]; if((int)cur[i].size() == cap[i]){ exceeded_lab.push_back(i); } cur[i].push({u, 0}); active_lab.push_back(i); } vector res(n, -1); while(!active_lab.empty()){ vector> rem; for(auto i: exceeded_lab){ while((int)cur[i].size() > cap[i]){ auto [u, p] = cur[i].top(); cur[i].pop(); rem.push_back({u, p + 1}); } } for(auto i: active_lab){ assert((int)cur[i].size() <= cap[i]); while(!cur[i].empty()){ auto [u, p] = cur[i].top(); cur[i].pop(); res[u] = i; -- cap[i]; } } exceeded_lab.clear(); active_lab.clear(); for(auto [u, p]: rem){ assert(p < m); int i = prefer[u][p]; if((int)cur[i].size() == cap[i]){ exceeded_lab.push_back(i); } cur[i].push({u, p}); active_lab.push_back(i); } } ranges::copy(res, ostream_iterator(cout, " ")); cout << "\n"; return 0; } /* */