#include using namespace std; #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef unsigned long long ull; using P=pair; const ll INF=1e18; const int mod=1e9+7; const int MAX_V=50005; struct edge{ ll to,cap,cost,rev; }; ll V; vectorG[MAX_V]; ll h[MAX_V]; ll dist[MAX_V]; ll prevv[MAX_V],preve[MAX_V]; void add_edge(ll from,ll to,ll cap,ll cost){ G[from].push_back((edge){to,cap,cost,(int)G[to].size()}); G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1}); } ll min_cost_flow(int s,int t,ll f){ ll res=0; fill(h,h+V,0); while(f>0){ priority_queue,greater

>que; fill(dist,dist+V,INF); dist[s]=0; que.push(P(0,s)); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(dist[v]0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to]=dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.push(P(dist[e.to],e.to)); } } } if(dist[t]==INF){return -1;} rep(v,V){h[v]+=dist[v];} ll d=f; for(int v=t;v!=s;v=prevv[v]){ d=min(d,G[prevv[v]][preve[v]].cap); } f-=d; res+=d*h[t]; for(int v=t;v!=s;v=prevv[v]){ edge &e=G[prevv[v]][preve[v]]; e.cap-=d; G[v][e.rev].cap+=d; } } return res; } void solve(){ ll k; cin>>V>>k; vectora(V); vector>b(V); ll mx=1e9; rep(i,V){ int m; cin>>a[i]>>m; b[i].resize(m); rep(j,m){ cin>>b[i][j]; b[i][j]--; } } rep(i,V){ for(auto j:b[i]){ if(a[i]>a[j]){add_edge(j,i,1,a[j]-a[i]+((ll)i-(ll)j)*mx);} } if(i+1