Problem p03344 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define INF 3e15
#define mod 1000000007
#define eps 1e-6
#define abs(x) ((x) >= 0 ? (x) : -(x))
#define y1 solai
#define fi first
#define se second
typedef long long ll;
void read(ll &x) { scanf("%lld", &x); }
void read(ll &x, ll &y) { scanf("%lld%lld", &x, &y); }
void read(ll &x, ll &y, ll &z) { scanf("%lld%lld%lld", &x, &y, &z); }
void print(ll x) { printf("%lld ", x); }
void println(ll x) { printf("%lld\n", x); }
const ll N = 100000;
ll n, m, x, y, l, r, mid, ans, sum, a[N + 10], b[N + 10], li[N + 10], w[N + 10],
    p[N + 10], ra[N + 10], su[N + 10];
vector<ll> v[N + 10];
set<pair<ll, ll>> s[N + 10];
/*ll fin(ll a)
{
        if(a==p[a])
                return a;
        return p[a]=fin(p[a]);
}
void uni(ll a, ll b)
{
        a=fin(a),b=fin(b);
        if(a==b)
                return;
        if(r[a]>r[b])
                r[a]+=r[b],su[a]+=su[b],p[b]=a;
        else
                r[b]+=r[a],su[b]+=su[a],p[a]=b;
}
void dfs(ll x)
{
        w[x]=1;
        ll rt=fin(x);
        su[rt]+=b[x];
        for(ll to:v[x])
                if(fin(x)!=fin(to))
                        s[rt].insert({a[to]-b[to],to});
        while(!s[rt].empty())
        {
                ll need=s[rt].begin()->fi,to=s[rt].begin()->se;
                if(fin(x)==fin(to))
                        s[rt].erase(s[rt].begin());
                else if(fin(x)!=fin(to)&&su[rt]>=need)
                {
                        uni(rt,s[rt].begin()->se);
                        dfs(s[rt].begin()->se);
                        rt=fin(x);
                        s[rt].erase(s[rt].begin());
                }
                else
                        return;
        }
}*/
bool cmp(ll x, ll y) { return a[x] - b[x] < a[y] - b[y]; }
int main() {
  // freopen("c.cpp","r",stdin);

  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    read(a[i], b[i]), sum += b[i], li[i] = i, p[i] = i, ra[i] = 1, su[i] = b[i];
  for (ll i = 1; i <= m; i++)
    read(x, y), v[x].pb(y), v[y].pb(x);
  sort(li + 1, li + n + 1, &cmp);
  ll res = INF;
  ll t = ll(1e7) / (n + m);
  for (ll i = 1; i <= min(t, n); i++) {
    priority_queue<pair<ll, ll>> q;
    ll bal = 0;
    ans = 0;
    q.push({-a[li[i]] + b[li[i]], li[i]});
    for (ll j = 1; j <= n; j++)
      w[j] = 0;
    for (; !q.empty();) {
      x = q.top().se, y = -q.top().fi;
      q.pop();
      if (w[x])
        continue;
      w[x] = 1;
      if (bal < y)
        ans += y - bal, bal = y;
      bal += b[x];
      for (ll to : v[x]) {
        ll need = a[to] - b[to];
        if (!w[to])
          q.push({-need, to});
      }
    }
    res = min(res, ans);
  }
  for (ll i = 1; i <= min(t, n); i++) {
    priority_queue<pair<ll, ll>> q;
    ll bal = 0, cur = rand() % n + 1;
    ans = 0;
    q.push({-a[li[cur]] + b[li[cur]], li[cur]});
    for (ll j = 1; j <= n; j++)
      w[j] = 0;
    for (; !q.empty();) {
      x = q.top().se, y = -q.top().fi;
      q.pop();
      if (w[x])
        continue;
      w[x] = 1;
      if (bal < y)
        ans += y - bal, bal = y;
      bal += b[x];
      for (ll to : v[x]) {
        ll need = a[to] - b[to];
        if (!w[to])
          q.push({-need, to});
      }
    }
    res = min(res, ans);
  }
  /*for(ll i=1;i<=n;i++)
          if(!w[li[i]])
                  ans=a[li[i]]-b[li[i]],dfs(li[i]);*/
  cout << res + sum;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define INF 3e15
#define mod 1000000007
#define eps 1e-6
#define abs(x) ((x) >= 0 ? (x) : -(x))
#define y1 solai
#define fi first
#define se second
typedef long long ll;
void read(ll &x) { scanf("%lld", &x); }
void read(ll &x, ll &y) { scanf("%lld%lld", &x, &y); }
void read(ll &x, ll &y, ll &z) { scanf("%lld%lld%lld", &x, &y, &z); }
void print(ll x) { printf("%lld ", x); }
void println(ll x) { printf("%lld\n", x); }
const ll N = 100000;
ll n, m, x, y, l, r, mid, ans, sum, a[N + 10], b[N + 10], li[N + 10], w[N + 10],
    p[N + 10], ra[N + 10], su[N + 10];
vector<ll> v[N + 10];
set<pair<ll, ll>> s[N + 10];
/*ll fin(ll a)
{
        if(a==p[a])
                return a;
        return p[a]=fin(p[a]);
}
void uni(ll a, ll b)
{
        a=fin(a),b=fin(b);
        if(a==b)
                return;
        if(r[a]>r[b])
                r[a]+=r[b],su[a]+=su[b],p[b]=a;
        else
                r[b]+=r[a],su[b]+=su[a],p[a]=b;
}
void dfs(ll x)
{
        w[x]=1;
        ll rt=fin(x);
        su[rt]+=b[x];
        for(ll to:v[x])
                if(fin(x)!=fin(to))
                        s[rt].insert({a[to]-b[to],to});
        while(!s[rt].empty())
        {
                ll need=s[rt].begin()->fi,to=s[rt].begin()->se;
                if(fin(x)==fin(to))
                        s[rt].erase(s[rt].begin());
                else if(fin(x)!=fin(to)&&su[rt]>=need)
                {
                        uni(rt,s[rt].begin()->se);
                        dfs(s[rt].begin()->se);
                        rt=fin(x);
                        s[rt].erase(s[rt].begin());
                }
                else
                        return;
        }
}*/
bool cmp(ll x, ll y) { return a[x] - b[x] < a[y] - b[y]; }
int main() {
  // freopen("c.cpp","r",stdin);

  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    read(a[i], b[i]), sum += b[i], li[i] = i, p[i] = i, ra[i] = 1, su[i] = b[i];
  for (ll i = 1; i <= m; i++)
    read(x, y), v[x].pb(y), v[y].pb(x);
  sort(li + 1, li + n + 1, &cmp);
  ll res = INF;
  ll t = ll(1e6) / (n + m);
  t *= 5;
  for (ll i = 1; i <= min(t, n); i++) {
    priority_queue<pair<ll, ll>> q;
    ll bal = 0;
    ans = 0;
    q.push({-a[li[i]] + b[li[i]], li[i]});
    for (ll j = 1; j <= n; j++)
      w[j] = 0;
    for (; !q.empty();) {
      x = q.top().se, y = -q.top().fi;
      q.pop();
      if (w[x])
        continue;
      w[x] = 1;
      if (bal < y)
        ans += y - bal, bal = y;
      bal += b[x];
      for (ll to : v[x]) {
        ll need = a[to] - b[to];
        if (!w[to])
          q.push({-need, to});
      }
    }
    res = min(res, ans);
  }
  for (ll i = 1; i <= min(t, n); i++) {
    priority_queue<pair<ll, ll>> q;
    ll bal = 0, cur = rand() % n + 1;
    ans = 0;
    q.push({-a[li[cur]] + b[li[cur]], li[cur]});
    for (ll j = 1; j <= n; j++)
      w[j] = 0;
    for (; !q.empty();) {
      x = q.top().se, y = -q.top().fi;
      q.pop();
      if (w[x])
        continue;
      w[x] = 1;
      if (bal < y)
        ans += y - bal, bal = y;
      bal += b[x];
      for (ll to : v[x]) {
        ll need = a[to] - b[to];
        if (!w[to])
          q.push({-need, to});
      }
    }
    res = min(res, ans);
  }
  /*for(ll i=1;i<=n;i++)
          if(!w[li[i]])
                  ans=a[li[i]]-b[li[i]],dfs(li[i]);*/
  cout << res + sum;
}