Problem p03344 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct UnionFind {
  vector<int> v;
  vector<ll> sum;
  UnionFind(int n) : v(n, -1), sum(n, 0) {}
  void init() {
    for (int i = 0; i < (int)v.size(); i++)
      v[i] = -1;
  }
  int find(int x) { return v[x] < 0 ? x : v[x]; }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-v[x] < -v[y])
      swap(x, y);
    sum[x] += sum[y];
    v[x] += v[y];
    v[y] = x;
    return true;
  }
  bool root(int x) { return v[x] < 0; }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};

ll N, M;
ll A[100010], B[100010];
vector<ll> g[100010];
ll X[100010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  vector<ll> ord;
  rep(i, N) {
    cin >> A[i] >> B[i];
    ord.push_back(i);
  }
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  sort(all(ord),
       [=](const ll &a, const ll &b) { return A[a] - B[a] < A[b] - B[b]; });
  UnionFind uf(N);
  rep(i, N) {
    uf.sum[i] = B[i];
    X[i] = INF;
  }

  for (ll v : ord) {
    X[v] = max(0LL, A[v] - B[v]);
    for (ll nv : g[v]) {
      ll f = uf.find(nv);
      if (X[f] == INF)
        continue;
      ll tot = uf.sum[f] + X[f];
      minch(X[v], X[f] + max(0LL, A[v] - B[v] - tot));
    }
    for (ll nv : g[v]) {
      ll f = uf.find(nv);
      if (X[f] == INF)
        continue;
      uf.unite(v, nv);
    }
    ll f = uf.find(v);
    X[f] = X[v];
  }
  cout << X[uf.find(0)] + uf.sum[uf.find(0)] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct UnionFind {
  vector<int> v;
  vector<ll> sum;
  UnionFind(int n) : v(n, -1), sum(n, 0) {}
  void init() {
    for (int i = 0; i < (int)v.size(); i++)
      v[i] = -1;
  }
  int find(int x) { return v[x] < 0 ? x : find(v[x]); }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-v[x] < -v[y])
      swap(x, y);
    sum[x] += sum[y];
    v[x] += v[y];
    v[y] = x;
    return true;
  }
  bool root(int x) { return v[x] < 0; }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -v[find(x)]; }
};

ll N, M;
ll A[100010], B[100010];
vector<ll> g[100010];
ll X[100010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  vector<ll> ord;
  rep(i, N) {
    cin >> A[i] >> B[i];
    ord.push_back(i);
  }
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  sort(all(ord),
       [=](const ll &a, const ll &b) { return A[a] - B[a] < A[b] - B[b]; });
  UnionFind uf(N);
  rep(i, N) {
    uf.sum[i] = B[i];
    X[i] = INF;
  }

  for (ll v : ord) {
    X[v] = max(0LL, A[v] - B[v]);
    for (ll nv : g[v]) {
      ll f = uf.find(nv);
      if (X[f] == INF)
        continue;
      ll tot = uf.sum[f] + X[f];
      minch(X[v], X[f] + max(0LL, A[v] - B[v] - tot));
    }
    for (ll nv : g[v]) {
      ll f = uf.find(nv);
      if (X[f] == INF)
        continue;
      uf.unite(v, nv);
    }
    ll f = uf.find(v);
    X[f] = X[v];
  }
  cout << X[uf.find(0)] + uf.sum[uf.find(0)] << endl;

  return 0;
}