#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b struct Dinic{ //適当にフローを流す //流した辺のcapを減らし,逆辺のcapをその分増やす //これを繰り返す //O(EV^2),通常こんなにかからない const T TINF; struct edge{ int to; //容量 T cap; int rev; bool isrev; int idx; }; vector> g; vector min_cost, iter; Dinic(int V):TINF(numeric_limits::max()),g(V){} void add_edge(int from, int to, T cap, int idx=-1){ g[from].push_back((edge){to, cap, (int)g[to].size(), false, idx}); g[to]. push_back((edge){from, 0, (int)g[from].size()-1, true, idx}); } bool bfs(int s, int t){ min_cost.assign(g.size(), -1); queue q; min_cost[s]=0; q.push(s); while(!q.empty() and min_cost[t]==-1){ int p=q.front();q.pop(); for(auto &e:g[p]){ //流せるなら if(e.cap>0 and min_cost[e.to]==-1){ min_cost[e.to]=min_cost[p]+1; q.push(e.to); } } } //tまで流せるパスが1つでもあるならtrueを返す return min_cost[t]!=-1; } //dfsでtまでフローを流せるだけ流すdfs T dfs(int idx, const int t, T flow){ if(idx==t) return flow; for(int &i=iter[idx];i0){ //流せるならcapの量を減らし,逆辺のcapを増やす e.cap-=d; g[e.to][e.rev].cap+=d; //どこか1つ見つければいいのでd>0で //dが見つかれば即リターン //次はi以降を観ればいいので&iでiを更新してiterに記憶 return d; } } } return 0; } T max_flow(int s,int t){ T flow=0; while(bfs(s,t)){ iter.assign(g.size(),0); T f=0; while((f=dfs(s,t,INF))>0) flow+=f; } return flow; } }; signed main(){ cin.tie(0); ios::sync_with_stdio(0); int n;cin>>n;n--; ll a[n],b[n],sum=0; rep(i,n){ cin>>a[i]>>b[i]; sum+=a[i]; } int st=2*n,gl=2*n+1; Dinic flow(2*n+2); rep(i,n){ flow.add_edge(st,i,a[i]); flow.add_edge(i+n,gl,b[i]); rep(j,n)if(i!=j){ flow.add_edge(i,j+n,INF); } } int sabakenai=sum-flow.max_flow(st,gl); cout<