Problem p02630 (C++) — buggy submission, verdict: Runtime Error

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define Filein(N) freopen("N", "r", stdin);
#define Fileout(N) freopen("N", "w", stdout);
#define sci(x) scanf("%d", &x)
#define sci2(x, y) scanf("%d%d", &x, &y)
#define scll2(x, y) scanf("%lld%lld", &x, &y)
#define scll(x) scanf("%lld", &x)
#define scd(x) scanf("%lf", &x)
#define scstr(x) scanf("%s", x)
#define pfi(x) printf("%d", x)
#define pfll(x) printf("%lld", x)
#define pfd(x) printf("%f", x)
#define pfstr(x) printf("%s", x)
#define ps printf(" ")
#define pn printf("\\n")
#define pfdt printf("..")

#define For(var, start, end) for (int var = start; var <= end; i++)
#define rFor(var, start, end) for (int var = start; var >= end; i--)
#define TestCase                                                               \\
  int testcase, cs = 1;                                                        \\
  cin >> testcase;                                                             \\
  while (testcase--)

#define mkp make_pair
#define pb push_back
#define fi first
#define se second

#define px(x) cout << "X -> " << x << endl;
#define py(x) cout << "Y = " << x << endl;
#define pz(x) cout << "Z >> " << x << endl;
#define pstr(x) printf("%s\\n", x);

using namespace std;
using ll = long long;
using vi = vector<int>;
using mpii = map<int, int>;
using si = set<int>;
using sll = set<ll>;
using vll = vector<ll>;

#define INF 1000000000
#define LINF 4000000000000000000
#define pi (2 * acos(0))
#define Mod 1000000007

int ar[100100];

int main() {

  ll n, p, q, a, b, sum = 0;

  cin >> n;
  For(i, 0, n - 1) {
    scll(p);
    sum += p;
    ar[p]++;
  }

  scll(q);
  while (q--) {
    scll2(a, b);
    ll dif = b - a;
    ll to = ar[a] * dif;
    ar[b] += ar[a];
    ar[a] = 0;
    sum += to;
    pfll(sum);
    pn;
  }

  return 0;
}


Error: -11

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define Filein(N) freopen("N", "r", stdin);
#define Fileout(N) freopen("N", "w", stdout);
#define sci(x) scanf("%d", &x)
#define sci2(x, y) scanf("%d%d", &x, &y)
#define scll2(x, y) scanf("%lld%lld", &x, &y)
#define scll(x) scanf("%lld", &x)
#define scd(x) scanf("%lf", &x)
#define scstr(x) scanf("%s", x)
#define pfi(x) printf("%d", x)
#define pfll(x) printf("%lld", x)
#define pfd(x) printf("%f", x)
#define pfstr(x) printf("%s", x)
#define ps printf(" ")
#define pn printf("\\n")
#define pfdt printf("..")

#define For(var, start, end) for (int var = start; var <= end; i++)
#define rFor(var, start, end) for (int var = start; var >= end; i--)
#define TestCase                                                               \\
  int testcase, cs = 1;                                                        \\
  cin >> testcase;                                                             \\
  while (testcase--)

#define mkp make_pair
#define pb push_back
#define fi first
#define se second

#define px(x) cout << "X -> " << x << endl;
#define py(x) cout << "Y = " << x << endl;
#define pz(x) cout << "Z >> " << x << endl;
#define pstr(x) printf("%s\\n", x);

using namespace std;
using ll = long long;
using vi = vector<int>;
using mpii = map<int, int>;
using si = set<int>;
using sll = set<ll>;
using vll = vector<ll>;

#define INF 1000000000
#define LINF 4000000000000000000
#define pi (2 * acos(0))
#define Mod 1000000007

ll ar[100100];

int main() {

  ll n, p, q, a, b, sum = 0;

  cin >> n;
  For(i, 0, n - 1) {
    sci(p);
    sum += p;
    ar[p]++;
  }

  scll(q);
  while (q--) {
    scll2(a, b);
    ll dif = b - a;
    ll to = ar[a] * dif;
    ar[b] += ar[a];
    ar[a] = 0;
    sum += to;
    pfll(sum);
    pn;
  }

  return 0;
}