#include #define endl '\n' using namespace std; #define int unsigned long long const int MAXN = 10000 + 5; int n, m; int c[MAXN]; vector scenes[MAXN]; int eval(vector &x) { vector first(n + 1, m); vector last(n + 1, -1); for (int t = 0; t < x.size(); t++) { int scene = x[t]; for (int &actor : scenes[scene]) { if (first[actor] > t) { first[actor] = t; } if (last[actor] < t) { last[actor] = t; } } } int ret = 0; for (int j = 1; j <= n; j++) { if (first[j] <= last[j]) { ret += (last[j] - first[j] + 1) * c[j]; } } return ret; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("star.in", "r", stdin); freopen("star.out", "w", stdout); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> c[i]; vector ans; ans.reserve(m); for (int i = 1; i <= m; i++) ans.push_back(i); for (int i = 1; i <= m; i++) { int sz; cin >> sz; scenes[i].reserve(sz); for (int j = 1; j <= sz; j++) { int actor; cin >> actor; scenes[i].push_back(actor); } } int best = eval(ans); while ((double)(clock()) / CLOCKS_PER_SEC < 4.6) { vector tmp = ans; int a = rand() % m; int b = rand() % m; if (a > b) swap(a, b); random_shuffle(tmp.begin() + a, tmp.begin() + b); // swap(tmp[a], tmp[b]); int result = eval(tmp); if (result < best) { result = best; ans = tmp; } } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; return 0; }