#include #include #include #include #include #include #define MAXN 10001 using namespace std; long long n, m, in[MAXN], out[MAXN]; double t = 10000000, k = 0.99999; mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count()); uniform_real_distribution<> distr(0.0, 1.0); struct Actor { long long cost, ind; bool scenes[5005]; } actors[MAXN]; vector answerVector; unsigned long long answer; std::chrono::time_point start; struct ActorCopy { int cost, ind; bool operator< (ActorCopy other) { return cost > other.cost; } } actorsCopy[MAXN]; vector order, nxt; struct Scene { int sz, ind; vector actors; bool operator< (Scene other) { return sz < other.sz; } } scenes[5005]; struct SceneCopy { vector actors; } scenesCopy[5005]; bool vis[5005]; void read() { freopen("star.in", "r", stdin); freopen("star.out", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>m; for(int i = 0; i < n; i ++) { cin>>actors[i].cost; actors[i].ind = i; actorsCopy[i].cost = actors[i].cost; actorsCopy[i].ind = i; } for(int i = 0; i < m; i ++) { cin>>scenes[i].sz; scenes[i].ind = i; for(int j = 0; j < scenes[i].sz; j ++) { int a; cin>>a; actors[a - 1].scenes[i] = 1; scenes[i].actors.emplace_back(a - 1); scenesCopy[i].actors.emplace_back(a - 1); } } start = std::chrono::high_resolution_clock::now(); sort(actorsCopy, actorsCopy + n); sort(scenes, scenes + m); } void findOrder() { memset(vis, 0, sizeof vis); for(int j = 0; j < n; j ++) { ActorCopy actor = actorsCopy[j]; for(int i = 0; i < m; i ++) { Scene scene = scenes[i]; if(actors[actor.ind].scenes[scene.ind] && !vis[scene.ind]) { order.emplace_back(scene.ind); vis[scene.ind] = 1; } } } } unsigned long long calculateScore(vector& vec) { for(int i = 0; i <= n; i ++) in[i] = out[i] = -1; long long timer = 1; for(auto sceneId : vec) { for(auto actorId : scenesCopy[sceneId].actors) { if(in[actorId] != -1) { out[actorId] = timer; } else { in[actorId] = timer; out[actorId] = timer; } } timer ++; } unsigned long long ans = 0; for(int i = 0; i < n; i ++) { ans += (out[i] - in[i] + 1) * actors[i].cost; } return ans; } void print() { for(int i = 0; i < m; i ++) { cout< elapsed = now - start; if (elapsed.count() >= 4.7) return 1; return 0; } void assignVector(vector& other) { for(int i = 0; i < m; i ++) { answerVector[i] = other[i]; } } void neighbor() { nxt = order; int l = mt() % m; int r = mt() % m; if(l != r) swap(nxt[l], nxt[r]); } double P(int old, int nw, double t) { if(nw < old) return 1; return exp((old - nw)/t); } signed main() { read(); findOrder(); answerVector.resize(m); assignVector(order); answer = calculateScore(order); do { neighbor(); unsigned long long orderVal = calculateScore(order); unsigned long long nxtVal = calculateScore(nxt); if(orderVal < answer) { answer = orderVal; assignVector(order); } //cout<= distr(mt)) { order = nxt; } t *= k; }while(!shouldStop()); print(); return 0; } /* 117492512 */