#include using namespace std; typedef long long ll; // https://en.wikipedia.org/wiki/Edmonds%27_algorithm // With link-cut trees to improve performance from O(VE) to O(V^2logV) struct LinkCut{/*{{{*/ LinkCut *p,*l,*r; LinkCut(LinkCut* p=0): p(p), l(0), r(0) {} bool root() {return not p or p->l != this and p->r != this;} void rot(LinkCut* LinkCut::*a,LinkCut* LinkCut::*b) { if (not root()) (p->l==this?p->l:p->r)=(this->*a); (this->*a)->p=p, p=(this->*a); (this->*a)=(p->*b), (p->*b)=this; if (this->*a) (this->*a)->p=this; } void rotate() { if (p->l==this) p->rot(&LinkCut::l,&LinkCut::r); else p->rot(&LinkCut::r,&LinkCut::l); } void splay() { while (not root()) { if (not p->root()) ((p->l==this)==(p->p->l==p)? p:this)->rotate(); rotate(); } } void link(LinkCut* x) { splay(); x->splay(); p=x; p->l=this; expose(); } void cut() { splay(); if (r) r->p=p; p=r=0; } void expose() { for (LinkCut* i=this,*j=0; i; j=i,i=i->p) { i->splay(); i->l=j; } splay(); } LinkCut* findroot() { expose(); LinkCut* x=this; while (x->r) x=x->r; x->expose(); return x; } };/*}}}*/ struct Graph{ int const n; vector> edge; Graph(int n): n(n) {edge.assign(n,vector(n,1<<30));} int64_t minimum_cost_arborescence(){ vector,vector>,greater>>> pi(n); for (int i=n; i-->1;) for (int j=n; j-->0;) if (i!=j) pi[i].emplace(edge[j][i],j); vector lc(n); vector exists(n,true),merging(n,false); vector par(n,-1); auto const get_pi=[&pi,&exists,this](int i){ while (not exists[pi[i].top().second] or edge[pi[i].top().second][i]!=pi[i].top().first){ pi[i].pop(); } return pi[i].top().second; }; int64_t res=0; for (int i=1; i del={i}; merging[i]=true; for (int x=j; x!=i; x=par[x]){ del.push_back(x); merging[x]=true; } vector old_pi(n); for (int b: del){ if (b==i){ old_pi[b]=edge[j][b]; }else{ old_pi[b]=edge[par[b]][b]; exists[b]=false; } } res+=edge[j][i]; // Detach edges from the solution that went to deleted nodes. vector cycle_children; for (int a=1; a>n; Graph g(n+1); for (int i=1; i<=n; i++){ int x,t; cin>>x>>t; for (int j=0; j<=n; j++){ cin>>g.edge[j][i]; } if (x!=i){ g.edge[x][i]=t; } } cout<