Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ui32 = unsigned int;
constexpr ll INFLL = 1e18;

#define CHECK_IMPL1(cond)                                                      \
  if (!(cond)) {                                                               \
    DEBUG("expected cond: " << #cond);                                         \
    assert(cond);                                                              \
  }

#define CHECK_IMPL2(cond, message)                                             \
  if (!(cond)) {                                                               \
    DEBUG("expected cond: " << #cond << " failed with message: " << message);  \
    assert(cond);                                                              \
  }
#define CHECK_IMPL(_1, _2, NAME, ...) NAME
#define CHECK(...)                                                             \
  CHECK_IMPL(__VA_ARGS__, CHECK_IMPL2, CHECK_IMPL1, CHECK_IMPL0)(__VA_ARGS__)

#ifdef __APPLE__
#define DEBUG(message) std::cerr << message << "\n";
#else
#define DEBUG(message)
#endif

void solve() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += ans;
    ll k = (a[i] + b[i] - 1) / b[i];
    ll add = k * b[i] - a[i];
    CHECK(add >= 0);
    CHECK((a[i] + add) % b[i] == 0);
    a[i] += add;
    ans += add;
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  //  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ui32 = unsigned int;
constexpr ll INFLL = 1e18;

#define CHECK_IMPL1(cond)                                                      \
  if (!(cond)) {                                                               \
    DEBUG("expected cond: " << #cond);                                         \
    assert(cond);                                                              \
  }

#define CHECK_IMPL2(cond, message)                                             \
  if (!(cond)) {                                                               \
    DEBUG("expected cond: " << #cond << " failed with message: " << message);  \
    assert(cond);                                                              \
  }
#define CHECK_IMPL(_1, _2, NAME, ...) NAME
#define CHECK(...)                                                             \
  CHECK_IMPL(__VA_ARGS__, CHECK_IMPL2, CHECK_IMPL1, CHECK_IMPL0)(__VA_ARGS__)

#ifdef __APPLE__
#define DEBUG(message) std::cerr << message << "\n";
#else
#define DEBUG(message)
#endif

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += ans;
    ll k = (a[i] + b[i] - 1) / b[i];
    ll add = k * b[i] - a[i];
    CHECK(add >= 0);
    CHECK((a[i] + add) % b[i] == 0);
    a[i] += add;
    ans += add;
  }
  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  //  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}