Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, j, n) for (int i = j; i < n; i++)
#define rrep(i, j, n) for (int i = j - 1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v)                                                         \
  rep(__k, v.size()) {                                                         \
    cout << v[__k];                                                            \
    if (__k != v.size() - 1)                                                   \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define fi first
#define se second
#define endl "\n"
using namespace std;

constexpr int inf = 2147483647;
constexpr int64_t inf64 = 9223372036854775807;
constexpr int mod = 1e9 + 7;

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

  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) cin >> a[i] >> b[i];

  int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (a[i] % b[i] != 0) {
      res++;
      rep(j, i + 1) a[j]++;
    }
  }

  cout << res << endl;
  return 0;
}

Fixed submission:
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, j, n) for (int i = j; i < n; i++)
#define rrep(i, j, n) for (int i = j - 1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v)                                                         \
  rep(__k, v.size()) {                                                         \
    cout << v[__k];                                                            \
    if (__k != v.size() - 1)                                                   \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define fi first
#define se second
#define endl "\n"
using namespace std;

constexpr int inf = 2147483647;
constexpr int64_t inf64 = 9223372036854775807;
constexpr int mod = 1e9 + 7;

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

  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) cin >> a[i] >> b[i];

  int64_t res = 0;
  rrep(i, n, 0) {
    int64_t nex = (res + a[i]) % b[i];
    if (nex != 0)
      res += b[i] - nex;
  }

  cout << res << endl;
  return 0;
}