Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// 素数判定
bool is_prime(const unsigned n) {
  switch (n) {
  case 0: // fall-through
  case 1:
    return false;
  case 2: // fall-through
  case 3:
    return true;
  } // n > 3 が保証された
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false; // (6の倍数)-1
    if (n % (i + 2) == 0)
      return false; // (6の倍数)+1
  }
  return true;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*   *   *   *   *   *   *   *   *   *   *   */

void solve() {
  ll n;
  cin >> n;
  Vl a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];
  ll ans = 0, c = 0;
  for (int i = n - 1; i >= 0; i--) {
    while ((a[i] + c) % b[i] != 0) {
      c++;
      ans++;
    }
  }
  cout << ans << endl;
}

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

  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// 素数判定
bool is_prime(const unsigned n) {
  switch (n) {
  case 0: // fall-through
  case 1:
    return false;
  case 2: // fall-through
  case 3:
    return true;
  } // n > 3 が保証された
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false; // (6の倍数)-1
    if (n % (i + 2) == 0)
      return false; // (6の倍数)+1
  }
  return true;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*   *   *   *   *   *   *   *   *   *   *   */

void solve() {
  ll n;
  cin >> n;
  Vl a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];
  ll ans = 0, c = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (((a[i] + c) % b[i]) == 0)
      continue;
    ans += b[i] - ((a[i] + c) % b[i]);
    c += b[i] - ((a[i] + c) % b[i]);
  }
  cout << ans << endl;
}

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

  solve();
  return 0;
}