#include #include #include #include using namespace std::chrono_literals; int N, M; std::vector C; std::vector> A, S; int64_t total(const std::vector& s, std::chrono::steady_clock::time_point end) { int64_t t = 0; for (int i = 0; i < N; i++) { auto& SI = S[i]; int b, e; for (b = 0; b < M; b++) { if (end < std::chrono::steady_clock::now()) return std::numeric_limits::max(); if (SI[s[b]]) break; } for (e = M - 1; e >= b; e--) { if (end < std::chrono::steady_clock::now()) return std::numeric_limits::max(); if (SI[s[e]]) break; } int d = e - b + 1; if (d > 0) t += d * C[i]; } return t; } int main() { std::ifstream fin("star.in"); fin >> N >> M; C.resize(N); A.resize(M); S.resize(N); for (int i = 0; i < N; i++) { S[i].resize(M); fin >> C[i]; } for (int i = 0; i < M; i++) { int b; fin >> b; A[i].resize(N); for (int j = 0; j < b; j++) { int a; fin >> a; A[i][--a] = true; S[a][i] = true; } } std::vector res(M); for (int i = 0; i < M; i++) res[i] = i; auto end = std::chrono::steady_clock::now() + std::chrono::duration_cast(4800ms); int64_t old_total = total(res, end); while (std::chrono::steady_clock::now() < end) { int os = std::rand() % M; int ns = std::rand() % M; while (ns == os && std::chrono::steady_clock::now() < end) ns = std::rand() % M; std::swap(res[os], res[ns]); auto nt = total(res, end); if (old_total < nt) std::swap(res[os], res[ns]); else old_total = nt; } std::ofstream fout("star.out"); for (int i = 0; i < M; i++) fout << res[i] + 1 << std::endl; }