Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rp(i, n) for (int i = 0; i < n; i++)
#define rep(i, m, n) for (int i = m; i <= n; i++)
#define llrp(i, n) for (long long i = 0; i < n; i++)
#define llrep(i, m, n) for (long long i = m; i <= n; i++)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define vcin(a)                                                                \
  ;                                                                            \
  rp(i, a.size()) { cin >> a[i]; }
#define vcout(a)                                                               \
  ;                                                                            \
  rp(i, a.size()) { cout << a[i] << endl; }
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef long long ll;
typedef string S;
typedef pair<int, int> P;

bool isOK(int index, int key, vi a) {
  if (a[index] >= key)
    return true;
  else
    return false;
}

int binary_search(int key, vi a) {
  int left = -1;
  int right = (int)a.size();
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key, a))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int modpow(int a, int b, int p) {
  if (b == 0) {
    return 1;
  }
  elif (b % 2 == 0) { return modpow((a * a) % p, b / 2, p); }
  else {
    return (a * modpow(a, b - 1, p)) % p;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vll a(n), b(n);
  llrp(i, n) { cin >> a[i] >> b[i]; }
  ll plus = 0;
  llrp(i, n) {
    while ((a[n - 1 - i] + plus) % b[n - 1 - i]) {
      plus++;
    }
  }
  cout << plus << endl;
  return 0;
}


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

#define rp(i, n) for (int i = 0; i < n; i++)
#define rep(i, m, n) for (int i = m; i <= n; i++)
#define llrp(i, n) for (long long i = 0; i < n; i++)
#define llrep(i, m, n) for (long long i = m; i <= n; i++)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define vcin(a)                                                                \
  ;                                                                            \
  rp(i, a.size()) { cin >> a[i]; }
#define vcout(a)                                                               \
  ;                                                                            \
  rp(i, a.size()) { cout << a[i] << endl; }
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef long long ll;
typedef string S;
typedef pair<int, int> P;

bool isOK(int index, int key, vi a) {
  if (a[index] >= key)
    return true;
  else
    return false;
}

int binary_search(int key, vi a) {
  int left = -1;
  int right = (int)a.size();
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key, a))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int modpow(int a, int b, int p) {
  if (b == 0) {
    return 1;
  }
  elif (b % 2 == 0) { return modpow((a * a) % p, b / 2, p); }
  else {
    return (a * modpow(a, b - 1, p)) % p;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vll a(n), b(n);
  llrp(i, n) { cin >> a[i] >> b[i]; }
  ll plus = 0;
  rp(i, n) {
    plus += ((a[n - 1 - i] + plus) % b[n - 1 - i]
                 ? (1 + (a[n - 1 - i] + plus) / b[n - 1 - i]) * b[n - 1 - i] -
                       (a[n - 1 - i] + plus)
                 : 0);
  }
  cout << plus << endl;
  return 0;
}