#include #include #define endl '\n' #define fi first #define se second using namespace std; const double TIME_LIMIT = 4.800; const int MAXN = 1e4 + 5; const int MAXM = 5005; const int INF = 1e9 + 5; const long long INFLL = 1e18 + 5; mt19937 mt(666); int n, m; int c[MAXN]; vector sc[MAXM]; pair rng[MAXN]; long long best = INFLL; int sol[MAXM]; int perm[MAXM]; pair changes; set pos[MAXN]; long long val[MAXN]; int cnt[MAXN]; long long getS(int ind) { if(pos[ind].size()==0) return 0; int l=(*pos[ind].begin()); int r=(*pos[ind].rbegin()); return 1LL*c[ind]*(r-l+1); } long long makeChange() { do { changes={mt()%m+1, mt()%m+1}; }while(changes.fi==changes.se); vector > tmp; tmp.reserve(sc[perm[changes.fi]].size()+sc[perm[changes.se]].size()); for(auto x: sc[perm[changes.fi]]) tmp.push_back({x,1}), cnt[x]=0; for(auto x: sc[perm[changes.se]]) tmp.push_back({x,2}), cnt[x]=0; for(auto x: sc[perm[changes.fi]]) cnt[x]++; for(auto x: sc[perm[changes.se]]) cnt[x]++; long long delta=0; for(auto [x,fl]: tmp) { if(cnt[x]>=2) continue; long long s1=getS(x); if(fl==1) { pos[x].erase(changes.fi); pos[x].insert(changes.se); } else { pos[x].erase(changes.se); pos[x].insert(changes.fi); } long long s2=getS(x); delta+=s2-s1; } swap(perm[changes.fi], perm[changes.se]); return delta; } void revertChange() { vector > tmp; tmp.reserve(sc[perm[changes.fi]].size()+sc[perm[changes.se]].size()); for(auto x: sc[perm[changes.fi]]) tmp.push_back({x,1}), cnt[x]=0; for(auto x: sc[perm[changes.se]]) tmp.push_back({x,2}), cnt[x]=0; for(auto x: sc[perm[changes.fi]]) cnt[x]++; for(auto x: sc[perm[changes.se]]) cnt[x]++; for(auto [x,fl]: tmp) { if(cnt[x]>=2) continue; if(fl==1) { pos[x].erase(changes.fi); pos[x].insert(changes.se); } else { pos[x].erase(changes.se); pos[x].insert(changes.fi); } } swap(perm[changes.fi], perm[changes.se]); } long long getVal(int p[]) { // for(int i=1;i<=m;i++) cout<val[y]; } chrono::time_point tmr_begin; void initClock() { tmr_begin = chrono::steady_clock::now(); } double getTime() { return 1e-3 * chrono::duration_cast(chrono::steady_clock::now() - tmr_begin).count(); } void fileInOut() { freopen("star.in", "r", stdin); freopen("star.out", "w", stdout); } void fastIO() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fastIO(); fileInOut(); initClock(); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int x; cin >> x; sc[i].push_back(x); val[i]+=c[x]; } } iota(perm + 1, perm + m + 1, 1); for(int i=1;i<=m;i++) { sol[i]=perm[i]; for(auto x: sc[perm[i]]) { pos[x].insert(i); } } best=getVal(perm); sort(perm+1,perm+m+1,cmp); for(int i=1;i<=n;i++) pos[i].clear(); for(int i=1;i<=m;i++) { for(auto x: sc[perm[i]]) { pos[x].insert(i); } } // for(int i=1;i<=m;i++) cout<0) { if(changes.fi!=0) { revertChange(); } } else cur+=delta; } if (cur < best) { best = cur; for (int i = 1; i <= m; i++) sol[i] = perm[i]; } shuffle(perm + 1, perm + m + 1, mt); for(int j=1;j<=n;j++) pos[j].clear(); for(int j=1;j<=m;j++) { for(auto x: sc[perm[j]]) { pos[x].insert(j); } } } //cout<