Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(a) begin(a), end(a)
#define mii map<int, int>
#define msi map<string, int>
#define vi vector<int>
#define vl vector<ll>
#define MOD 1000000007
#define INF 1e9
#define showVector(v)                                                          \
  rep(i, 0, v.size()) {                                                        \
    p(v[i]);                                                                   \
    p(" ")                                                                     \
  }                                                                            \
  pl("")
#define p(s) std::cout << s;
#define pl(s) std::cout << s << endl;
using namespace std;

// 素数判定 O(√n)
bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  vector<pair<ll, ll>> a(N);
  rep(i, 0, N) {
    cin >> a[i].first;
    cin >> a[i].second;
  }
  ll res = 0;
  for (ll i = N - 1; i >= 0; --i) {
    a[i].first += res;
    ll temp = a[i].second;
    while (a[i].first % a[i].second != 0) {
      if (a[i].first < a[i].second) {
        res += (a[i].second - a[i].first);
        break;
      } else {
        a[i].second += temp;
      }
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(a) begin(a), end(a)
#define mii map<int, int>
#define msi map<string, int>
#define vi vector<int>
#define vl vector<ll>
#define MOD 1000000007
#define INF 1e9
#define showVector(v)                                                          \
  rep(i, 0, v.size()) {                                                        \
    p(v[i]);                                                                   \
    p(" ")                                                                     \
  }                                                                            \
  pl("")
#define p(s) std::cout << s;
#define pl(s) std::cout << s << endl;
using namespace std;

// 素数判定 O(√n)
bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  vector<pair<ll, ll>> a(N);
  rep(i, 0, N) {
    cin >> a[i].first;
    cin >> a[i].second;
  }
  ll res = 0;
  for (ll i = N - 1; i >= 0; --i) {
    a[i].first += res;
    res += (a[i].second - a[i].first % a[i].second) % a[i].second;
  }
  cout << res << endl;
  return 0;
}