#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } #include using namespace atcoder; using mint = modint998244353; ostream &operator<<(ostream &os,mint a){return os< ostream &operator<<(ostream &os,const pair &p){ return os< ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } void solve() { ll N,M; cin>>N>>M; ll src=N; ll tar=src+1; mf_graph mf(tar+1); ll get=0; rep(i,N){ ll a,b; cin>>a>>b; get+=a+b; mf.add_edge(src,i,a); mf.add_edge(i,tar,b); } rep(i,M){ ll u,v,c; cin>>u>>v>>c; u--; v--; mf.add_edge(u,v,c); mf.add_edge(v,u,c); } ll f=mf.flow(src,tar); ll ans=get-f; cout<sync_with_stdio(0); ll T=1; while (T--){ solve(); } return 0; }