#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 200006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedeflonglong ll; int n , m; int A[MAXN] , B[MAXN] , C[MAXN];
structed { int u , v , w; } E[MAXN] ;
int fa[MAXN]; intfind( int x ){ return fa[x] == x ? x : fa[x] = find( fa[x] ); }
vi G[MAXN]; ll dp[MAXN] , S[MAXN]; voiddfs( int u ){ S[u] = B[u] , dp[u] = 0x3f3f3f3f3f3f3f3f; if( !G[u].size() ) { dp[u] = B[u] + C[u]; return; } for( int v : G[u] ) dfs( v ) , S[u] += S[v]; for( int v : G[u] ) dp[u] = min( dp[u] , S[u] - S[v] + max( 1ll * C[u] , dp[v] ) ); }
vi g[MAXN]; int idx[MAXN]; int fk[MAXN];
voidsolve(){ cin >> n >> m; rep( i , 1 , n ) scanf("%d%d",A + i,B + i) , C[i] = max( A[i] - B[i] , 0 ); rep( i , 1 , m ) { int u , v; scanf("%d%d",&u,&v); g[u].pb( v ) , g[v].pb( u ); } rep( i , 1 , n ) idx[i] = fa[i] = i; sort( idx + 1 , idx + 1 + n , [&]( int a , int b ) { return C[a] < C[b]; } ); rep( i , 1 , n ) { int u = idx[i]; for( int v : g[u] ) if( fk[v] ) { int t = find( v ); if( t == u ) continue; G[u].pb( t ); fa[t] = u; } fk[u] = 1; } dfs( idx[n] ); cout << dp[idx[n]] << endl; }