alexjercan/bugnet huggingface dataset:
Problem p01767 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;

class Fenwick_Tree {
private:
  vector<int64> p;

public:
  Fenwick_Tree(int size) { p.resize(size + 2, 0); }
  int64 sum(int x) {
    int64 ret = 0;
    for (++x; x > 0; x -= x & -x)
      ret += p[x];
    return (ret);
  }
  void add(int k, int a) {
    for (++k; k < p.size(); k += k & -k)
      p[k] += a;
  }
};

int main() {
  int N, M;
  Fenwick_Tree BIT(1000004);

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    BIT.add(a[i], a[i]);
  }
  cin >> M;
  vector<int64> b(M), c(M);
  for (int i = 0; i < M; i++)
    cin >> b[i];
  for (int i = 0; i < M; i++)
    cin >> c[i];

  for (int i = 0; i < M; i++) {
    int64 get = BIT.sum(b[i]);
    if (get >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;

class Fenwick_Tree {
private:
  vector<int64> p;

public:
  Fenwick_Tree(int size) { p.resize(size + 2, 0); }
  int64 sum(int x) {
    int64 ret = 0;
    for (++x; x > 0; x -= x & -x)
      ret += p[x];
    return (ret);
  }
  void add(int k, int a) {
    for (++k; k < p.size(); k += k & -k)
      p[k] += a;
  }
};

int main() {
  int N, M;
  Fenwick_Tree BIT(300004);

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    BIT.add(a[i], a[i]);
  }
  cin >> M;
  vector<int64> b(M), c(M);
  for (int i = 0; i < M; i++)
    cin >> b[i];
  for (int i = 0; i < M; i++)
    cin >> c[i];

  for (int i = 0; i < M; i++) {
    int64 get = BIT.sum(b[i]);
    if (get >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p01767 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M, b[300000];
  static long long Counter[1000001] = {};
  long long c[300000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    Counter[a] += a;
  }
  for (int i = 1; i < 1000001; i++) {
    Counter[i] += Counter[i - 1];
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < M; i++) {
    if (Counter[b[i]] >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: -11

Fixed submission:

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

int main() {
  int N, M, b[300000];
  long long Counter[1000001] = {}, c[300000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    Counter[a] += a;
  }
  for (int i = 1; i < 1000001; i++) {
    Counter[i] += Counter[i - 1];
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < M; i++) {
    if (Counter[b[i]] >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p01767 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

int n;
int m;
int b[300000];
long long c[300000];

long long dp[1111111];
long long anum[1111111];

long long getMax(int abili) {
  if (abili == 0) {
    return 0;
  } else {
    if (dp[abili] < 0) {
      int i = abili;
      while (i >= 0 && dp[i] < 0)
        i--;
      if (i < 0)
        i = 0;

      for (int j = i; j < abili; j++) {
        long long res = dp[j];
        if (res < 0)
          res = 0;
        res += anum[j + 1] * (j + 1);
        dp[j + 1] = res;
      }
      return dp[abili];
    } else {
      return dp[abili];
    }
  }
}

int main() {
  for (int i = 0; i < 1111111; i++)
    dp[i] = -1;

  cin >> n;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    anum[a]++;
  }
  cin >> m;
  for (int i = 0; i < m; i++)
    cin >> b[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];

  long long t;
  for (int i = 0; i < m; i++) {
    if (getMax(b[i]) >= c[i]) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
// #include<algorithm>
using namespace std;

int n;
int m;
int b[300000];
long c[300000];

long dp[1111111];
long anum[1111111];

long getMax(int abili) {
  if (abili == 0) {
    return 0;
  } else {
    if (dp[abili] < 0) {
      long long res = getMax(abili - 1);
      res += anum[abili] * abili;
      dp[abili] = res;
      return res;
    } else {
      return dp[abili];
    }
  }
}

int main() {
  for (int i = 0; i < 1111111; i++)
    dp[i] = -1;

  cin >> n;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    anum[a]++;
  }
  cin >> m;
  for (int i = 0; i < m; i++)
    cin >> b[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];

  // sort(a,a+n);

  long long t;
  for (int i = 0; i < m; i++) {
    if (getMax(b[i]) >= c[i]) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}
Problem p01767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define rep(i, x) REP(i, 0, (x))
#define fi first
#define se second
#define each(i, c)                                                             \\
  for (typeof(c)::iterator i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define pb push_back

ll dp[1000001];
int main() {
  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  int m;
  cin >> m;
  vl b(m), c(m);
  rep(i, m) cin >> b[i];
  rep(i, m) cin >> c[i];

  rep(i, n) dp[a[i]] += a[i];
  rep(i, 1000000) dp[i + 1] += dp[i];
  // rep(i,10)cout<<dp[i]<<" ";cout<<endl;
  rep(i, m) {
    if (dp[b[i]] >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define rep(i, x) REP(i, 0, (x))
#define fi first
#define se second
#define each(i, c)                                                             \\
  for (typeof(c)::iterator i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define pb push_back

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  int m;
  cin >> m;
  vi b(m), c(m);
  rep(i, m) cin >> b[i];
  rep(i, m) cin >> c[i];

  rep(i, m) {
    rep(j, n) {
      if (a[j] <= b[i])
        c[i] -= a[j];
    }
  }
  rep(i, m) cout << (c[i] <= 0 ? "Yes" : "No") << endl;
}
Problem p01767 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define int ll

signed main() {
  int n;
  cin >> n;
  static int a[1000001] = {};
  rep(i, n) {
    int x;
    cin >> x;
    a[x] += x;
  }
  for (int i = 1; i < 1000001; i++) {
    a[i] = a[i] + a[i - 1];
  }
  int m;
  cin >> m;
  static int b[300000], c[300000];
  rep(i, m) { cin >> b[i]; }
  rep(i, m) { cin >> c[i]; }
  rep(i, m) {
    if (a[b[i]] >= c[i]) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define int ll

signed main() {
  int n;
  cin >> n;
  int a[1000001] = {};
  rep(i, n) {
    int x;
    cin >> x;
    a[x] += x;
  }
  for (int i = 1; i < 1000001; i++) {
    a[i] = a[i] + a[i - 1];
  }
  int m;
  cin >> m;
  int b[300000], c[300000];
  rep(i, m) { cin >> b[i]; }
  rep(i, m) { cin >> c[i]; }
  rep(i, m) {
    cout << a[b[i]] << endl;
    if (a[b[i]] >= c[i]) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}
Problem p01767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  int M;
  cin >> M;
  vector<int> b(M);
  rep(i, 0, M) { cin >> b[i]; }
  vector<int> c(M);
  rep(i, 0, M) { cin >> c[i]; }

  static int A[1000010] = {};
  rep(i, 0, N) A[a[i]] += a[i];
  rep(i, 1, 1000010) A[i] += A[i - 1];

  rep(i, 0, M) {
    int p = A[b[i]];
    if (p >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  int M;
  cin >> M;
  vector<int> b(M);
  rep(i, 0, M) { cin >> b[i]; }
  vector<int> c(M);
  rep(i, 0, M) { cin >> c[i]; }

  rep(i, 0, M) {
    int p = 0;
    rep(j, 0, N) {
      if (a[j] > b[i])
        continue;
      p += a[j];
    }
    if (p >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01767 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
using namespace std;
int main() {
  int n;
  cin >> n;
  vi out(1000100, 0);
  rep(i, n) {
    int a;
    cin >> a;
    out[a] += a;
  }
  loop(i, 1, 1000100) out[i] += out[i - 1];
  int m;
  cin >> m;
  vi b(m), c(m);
  rep(i, m) cin >> b[i];
  rep(i, m) cin >> c[i];
  rep(i, m) cout << (c[i] <= out[b[i]] ? "Yes" : "No") << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
using namespace std;
int main() {
  int n;
  cin >> n;
  vi out(400000, 0);
  rep(i, n) {
    int a;
    cin >> a;
    out[a] += a;
  }
  loop(i, 1, 400000) out[i] += out[i - 1];
  int m;
  cin >> m;
  vi b(m), c(m);
  rep(i, m) cin >> b[i];
  rep(i, m) cin >> c[i];
  rep(i, m) cout << (c[i] <= out[b[i]] ? "Yes" : "No") << endl;
}
Problem p01767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;

typedef long long ll;
int main() {
  ll n, i, j, tt;
  vector<ll> vec, s, p;
  map<ll, ll> m;
  scanf("%lld", &n);
  while (n--) {
    scanf("%lld", &i);
    vec.push_back(i);
  }
  sort(vec.begin(), vec.end());
  ll lasts = 0;
  for (int i = 0; i < vec.size(); i++) {
    lasts += vec[i];
    m[vec[i]] = lasts;
  }
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &j);
    s.push_back(j);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &j);
    for (tt = s[i]; tt >= 0 && m[tt] == 0; tt--)
      ;
    if (m[tt] < j)
      printf("No\\n");
    else
      printf("Yes\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef long long ll;
int main() {
  ll n, i, j;
  vector<ll> vec, s, p;
  map<ll, ll> m;
  cin >> n;
  while (n--) {
    cin >> i;
    vec.push_back(i);
  }
  sort(vec.begin(), vec.end());
  ll lasts = 0;
  for (int i = 0; i < vec.size(); i++) {
    lasts += vec[i];
    m[vec[i]] = lasts;
  }

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> j;
    s.push_back(j);
  }
  for (int i = 0; i < n; i++) {
    cin >> j;
    ll tt;
    for (tt = s[i]; tt >= 0 && m[tt] == 0; tt--)
      ;
    // cout<<itlow->first<<"   "<<itlow->second<<endl;
    // cout<<tt<<"   "<<m[tt]<<endl;
    if (m[tt] < j) {
      cout << "No" << endl;
    } else
      cout << "Yes" << endl;
  }
}
Problem p01768 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#define f first
#define s second
using namespace std;
typedef pair<string, int> P;
map<string, P> mp;

string find(string x) {
  if (mp[x].f != x)
    mp[x] = mp[find(mp[x].f)];
  return mp[x].f;
}

bool unite(string x, string y) {
  bool flg = 0;
  mp[x] = mp[find(x)], mp[y] = mp[find(y)];
  if (mp[x].s != mp[y].s)
    flg = 1;
  if (mp[x].s < mp[y].s)
    swap(mp[x], mp[y]);
  mp[x] = mp[y];
  return flg;
}

int main() {
  int n, m, a;
  cin >> n;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str >> a;
    mp[str].f = str;
    mp[str].s = a;
  }
  cin >> m;
  string in[2][1001];
  for (int i = 0; i < m; i++)
    cin >> in[0][i] >> in[1][i], unite(in[0][i], in[1][i]);

  for (int i = 0; i < m; i++) {
    bool flg = 0;
    if (unite(in[0][i], in[1][i]))
      flg = 1;
    if (flg)
      i = -1;
  }
  int ans = 0;
  map<string, P>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++)
    ans += mp[find(it->f)].s;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#define f first
#define s second
using namespace std;
typedef pair<string, int> P;
map<string, P> mp;

string find(string x) {
  if (mp[x].f != x)
    mp[x] = mp[find(mp[x].f)];
  return mp[x].f;
}

bool unite(string x, string y) {
  bool flg = 0;
  mp[x] = mp[find(x)], mp[y] = mp[find(y)];
  if (mp[x] != mp[y])
    flg = 1;
  if (mp[x].s < mp[y].s)
    swap(mp[x], mp[y]);
  mp[x] = mp[y];
  return flg;
}

int main() {
  int n, m, a;
  cin >> n;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str >> a;
    mp[str].f = str;
    mp[str].s = a;
  }
  cin >> m;
  string in[2][1001];
  for (int i = 0; i < m; i++)
    cin >> in[0][i] >> in[1][i], unite(in[0][i], in[1][i]);

  for (int i = 0; i < m; i++) {
    bool flg = 0;
    if (unite(in[0][i], in[1][i]))
      flg = 1;
    if (flg)
      i = -1;
  }
  int ans = 0;
  map<string, P>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++)
    ans += mp[find(it->f)].s;
  cout << ans << endl;
  return 0;
}
Problem p01768 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
string a[5000];
int x[5000];
int Min[5000], cnt[5000];
int main() {
  int n;
  scanf("%d", &n);
  map<string, int> mp;
  rep(i, n) {
    cin >> a[i] >> x[i];
    int b = mp.size();
    mp[a[i]] = b;
  }
  int m;
  scanf("%d", &m);
  UnionFind uf(n);
  rep(i, m) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }
  memset(Min, 0x3f, sizeof(Min));
  rep(i, n) {
    Min[uf.find(i)] = min(Min[uf.find(i)], x[i]);
    cnt[uf.find(i)]++;
  }
  int ans = 0;
  rep(i, n) ans += Min[i] * cnt[i];
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
string a[5000];
int x[5000];
int Min[5000], cnt[5000];
int main() {
  int n;
  scanf("%d", &n);
  map<string, int> mp;
  rep(i, n) {
    cin >> a[i] >> x[i];
    mp[a[i]] = mp.size();
  }
  int m;
  scanf("%d", &m);
  UnionFind uf(n);
  rep(i, m) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }
  memset(Min, 0x3f, sizeof(Min));
  rep(i, n) {
    Min[uf.find(i)] = min(Min[uf.find(i)], x[i]);
    cnt[uf.find(i)]++;
  }
  int ans = 0;
  rep(i, n) ans += Min[i] * cnt[i];
  cout << ans << endl;
}
Problem p01768 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using psi = pair<string, int>;
int val[5000];

int N, M;

struct UnionFind {
  vector<int> data;
  int Val(int a) { return val[root(a)]; }
  int root(int a) { return data[a] < 0 ? a : data[a] = root(data[a]); }
  UnionFind(int n) : data(n, -1) {}
  bool Find(int a, int b) { return root(a) == root(b); }
  void Union(int a, int b) {
    int ra = root(a), rb = root(b);
    if (ra == rb)
      return;
    if (data[ra] < data[rb]) {
      data[ra] += data[rb];
      data[rb] = a;
      val[ra] = min(val[ra], val[rb]);
    } else {
      data[rb] += data[ra];
      data[ra] = b;
      val[rb] = min(val[ra], val[rb]);
    }
  }
  int size(int a) { return -data[root(a)]; }
};

int main() {
  string s, t;
  cin >> N;
  vector<psi> ax(N);
  vector<string> a;
  a.reserve(N);
  for (int i = 0; i < N; i++) {
    cin >> ax[i].first >> ax[i].second;
  }
  sort(ax.begin(), ax.end());
  for (int i = 0; i < N; i++) {
    a.push_back(ax[i].first);
    val[i] = ax[i].second;
  }
  UnionFind uf(N);
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    uf.Union(lower_bound(a.begin(), a.end(), s) - a.begin(),
             lower_bound(a.begin(), a.end(), t) - a.begin());
  }
  int res = 0;
  for (int i = 0; i < N; i++) {
    res += uf.Val(i);
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 100000000
using namespace std;
using psi = pair<string, int>;
int val[5000];

int N, M;

struct UnionFind {
  vector<int> data;
  int Val(int a) { return val[root(a)]; }
  int root(int a) { return data[a] < 0 ? a : data[a] = root(data[a]); }
  UnionFind(int n) : data(n, -1) {}
  bool Find(int a, int b) { return root(a) == root(b); }
  void Union(int a, int b) {
    int ra = root(a), rb = root(b);
    if (ra == rb)
      return;
    if (data[ra] < data[rb]) {
      data[ra] += data[rb];
      data[rb] = a;
      val[ra] = min(val[ra], val[rb]);
    } else {
      data[rb] += data[ra];
      data[ra] = b;
      val[rb] = min(val[ra], val[rb]);
    }
  }
  int size(int a) { return -data[root(a)]; }
};

int main() {
  string s, t;
  cin >> N;
  vector<psi> ax(N);
  vector<string> a;
  a.reserve(N);
  vector<vector<int>> magic(N, vector<int>(N, INF));
  for (int i = 0; i < N; i++) {
    cin >> ax[i].first >> ax[i].second;
  }
  sort(ax.begin(), ax.end());
  for (int i = 0; i < N; i++) {
    a.push_back(ax[i].first);
    val[i] = ax[i].second;
  }
  UnionFind uf(N);
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    uf.Union(lower_bound(a.begin(), a.end(), s) - a.begin(),
             lower_bound(a.begin(), a.end(), t) - a.begin());
  }
  int res = 0;
  for (int i = 0; i < N; i++) {
    res += uf.Val(i);
  }
  cout << res << endl;
  return 0;
}