prithivMLmods/Coder-Stat huggingface dataset:
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);

int main() {

  ll N;
  cin >> N;

  vector<ll> A(N), B(N);

  rep(i, N) { cin >> A[i] >> B[i]; }

  ll count = 0;

  for (ll i = N - 1; i >= 0; i--) {
    A[i] += count;
    while (A[i] % B[i] != 0) {
      A[i]++;
      count++;
    }
  }

  cout << count;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);

int main() {

  ll N;
  cin >> N;

  vector<ll> A(N), B(N);

  rep(i, N) { cin >> A[i] >> B[i]; }

  ll count = 0;

  for (ll i = N - 1; i >= 0; i--) {
    A[i] += count;
    if (A[i] % B[i] != 0) {
      count += B[i] - (A[i] % B[i]);
    }
  }

  cout << count;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(N, 0);
  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;
    while (A[i] % B[i] != 0) {
      A[i]++;
      ans++;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(N, 0);
  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;
    if (A[i] % B[i] != 0) {
      if (A[i] > B[i]) {
        B[i] = B[i] * ((A[i] / B[i]) + 1);
      }
      ans += B[i] - A[i];
    }
  }

  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll pre = 0;
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    a[i] += pre;
    if (a[i] % b[i] != 0) {
      if (a[i] > b[i]) {
        int u = 2;
        while (b[i] * u < a[i]) {
          u++;
        }
        ll pl = b[i] * u - a[i];
        ans += pl;
        pre += pl;
      } else {
        ans += b[i] - a[i];
        pre += b[i] - a[i];
      }
    }
    //  cout<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll pre = 0;
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    a[i] += pre;
    if (a[i] % b[i] != 0) {
      if (a[i] > b[i]) {
        ll u = a[i] / b[i] + 1;

        ll pl = b[i] * u - a[i];
        ans += pl;
        pre += pl;
      } else {
        ans += b[i] - a[i];
        pre += b[i] - a[i];
      }
    }
    //  cout<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
const ll M = 1e3 + 5, LG = 19, SM = 600 + 5, inf = 1e18 + 1;
ll mod = 1e9 + 7;

ll a[M], b[M];
ll n;

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  For(i, 0, n) cin >> a[i] >> b[i];
  ll res = 0, sx = 0;
  Ford(i, n - 1, 0) {
    ll s = sx + a[i];
    ll dif = (s / b[i] + (s % b[i] != 0)) * b[i];
    dif -= s;
    sx += dif;
    res += dif;
  }
  RET(res);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
const ll M = 2e5 + 5, LG = 19, SM = 600 + 5, inf = 1e18 + 1;
ll mod = 1e9 + 7;

ll a[M], b[M];
ll n;

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  For(i, 0, n) cin >> a[i] >> b[i];
  ll res = 0, sx = 0;
  Ford(i, n - 1, 0) {
    ll s = sx + a[i];
    ll dif = (s / b[i] + (s % b[i] != 0)) * b[i];
    dif -= s;
    sx += dif;
    res += dif;
  }
  RET(res);
}
Problem p03821 (C++)

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

#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<int> B(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    cin >> B[i];
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    int j = 0;
    while (true) {
      if ((A[i] + j) % B[i] == 0)
        break;
      j++;
    }
    ans += j;
    for (int k = 0; k <= i; ++k)
      A[k] += j;
  }
  cout << ans << endl;
  return 0;
}


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

#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<int> B(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    cin >> B[i];
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    if ((A[i] + ans) % B[i] == 0)
      continue;
    ans += B[i] - (A[i] + ans) % B[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int main() {
  int n;
  cin >> n;
  ll a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += cnt;
    if (a[i] % b[i]) {
      if (a[i] < b[i]) {
        swap(a[i], b[i]);
      }
      while (a[i] % b[i]) {
        cnt++;
        a[i]++;
      }
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int main() {
  int n;
  cin >> n;
  ll a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += cnt;
    if (a[i] % b[i]) {
      if (a[i] / b[i]) {
        cnt += b[i] - a[i] % b[i];
      } else {
        cnt += b[i] - a[i];
      }
    }
  }
  cout << cnt << endl;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  vector<int64_t> B(N);
  for (int i = 0; i < N; i++) {
    int64_t a, b;
    cin >> a >> b;
    A.at(i) = a;
    B.at(i) = b;
  }
  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    A.at(i) += ans;
    ans += ((B.at(i) + A.at(i) - 1) / A.at(i)) * A.at(i) - B.at(i);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  vector<int64_t> B(N);
  for (int i = 0; i < N; i++) {
    int64_t a, b;
    cin >> a >> b;
    A.at(i) = a;
    B.at(i) = b;
  }
  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    A.at(i) += ans;
    ans += ((B.at(i) + A.at(i) - 1) / B.at(i)) * B.at(i) - A.at(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <stdio.h>
#define ll long long
using namespace std;
const int N = 5010, P = 998244353;
inline char gc() {
  static char buf[N], *s = buf, *t = buf;
  return s == t && (t = (s = buf) + fread(buf, 1, N, stdin), s == t) ? EOF
                                                                     : *s++;
}
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
ll ans, a[N];
int n, b[N];
int main() {
  // freopen("1.out","w",stdout);
  int i;
  n = read();
  for (i = 1; i <= n; i++)
    a[i] = read(), b[i] = read();
  for (i = n; i; i--) {
    a[i] += ans;
    a[i] = a[i] % b[i];
    if (a[i])
      ans += b[i] - a[i];
  }
  write(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <stdio.h>
#define ll long long
using namespace std;
const int N = 500100, P = 998244353;
inline char gc() {
  static char buf[N], *s = buf, *t = buf;
  return s == t && (t = (s = buf) + fread(buf, 1, N, stdin), s == t) ? EOF
                                                                     : *s++;
}
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
ll ans, a[N];
int n, b[N];
int main() {
  // freopen("1.out","w",stdout);
  int i;
  n = read();
  for (i = 1; i <= n; i++)
    a[i] = read(), b[i] = read();
  for (i = n; i; i--) {
    a[i] += ans;
    a[i] = a[i] % b[i];
    if (a[i])
      ans += b[i] - a[i];
  }
  write(ans);
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (int i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end())

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

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

  rrep(i, n) {
    while (((a[i] + ans) % b[i]) != 0) {
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (int i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end())

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

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

  rrep(i, n) {
    int surplus = (a[i] + ans) % b[i];

    if (surplus != 0) {
      ans += b[i] - surplus;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(N, 0);
  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;
    if (A[i] % B[i] != 0) {
      while (A[i] % B[i] != 0) {
        A[i]++;
        ans++;
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(N, 0);
  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;
    if (A[i] % B[i] != 0) {
      int tmp = (A[i] / B[i] + 1) * B[i] - A[i];
      A[i] += tmp;
      ans += tmp;
    }
  }
  cout << ans;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

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;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<int, int> P;
ll ma = 1000000000 + 7;
ll h, w, n, k, m;
string s, t;
char maze[60][60];
int dis[101][101];
int graph[50][50];
bool vis[50];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

int main() {
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll res = 0;
  for (ll i = n - 1; i >= 0; i--) {
    a[i] += res;
    while (a[i] % b[i] != 0) {
      a[i]++;
      res++;
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

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;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<int, int> P;
ll ma = 1000000000 + 7;
ll h, w, n, k, m;
string s, t;
char maze[60][60];
int dis[101][101];
int graph[50][50];
bool vis[50];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

int main() {
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll res = 0;
  for (ll i = n - 1; i >= 0; i--) {
    ll k = (a[i] + res) % b[i];
    if (k != 0)
      res += b[i] - k;
  }
  cout << res << endl;
}
Problem p03821 (C++)

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

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999

using namespace std;

int32_t main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  REP(i, n) { cin >> a[i] >> b[i]; }

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

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999

using namespace std;

int32_t main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  REP(i, n) { cin >> a[i] >> b[i]; }

  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += ans;
    if (a[i] % b[i] != 0)
      ans += b[i] - (a[i] % b[i]);
  }

  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 100020
#define ll long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch == '-' && (f = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f ? x : -x;
}
long long a[N], b[N], ans;
int main(int argc, char const *argv[]) {
  // freopen("../../temp/.in", "r", stdin);
  int n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    b[i] = read();
  }
  for (int i = n; i; --i) {
    a[i] += ans;
    while (a[i] % b[i]) {
      ++ans;
      ++a[i];
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100020
#define ll long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch == '-' && (f = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f ? x : -x;
}
long long a[N], b[N], ans;
int main(int argc, char const *argv[]) {
  // freopen("../../temp/.in", "r", stdin);
  int n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    b[i] = read();
  }
  for (int i = n; i; --i) {
    a[i] += ans;
    ans += (b[i] - a[i] % b[i]) % b[i];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using PII = pair<int, int>;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int n;
  vector<ll> a, b;
  ll push = 0;
  cin >> n;
  REP(i, n) {
    ll x, y;
    cin >> x >> y;
    a.push_back(x);
    b.push_back(y);
  }
  reverse(all(a));
  reverse(all(b));
  REP(i, n) {
    while ((a[i] + push) % b[i] != 0)
      push++;
  }
  cout << push << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using PII = pair<int, int>;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int n;
  vector<ll> a, b;
  ll push = 0;
  cin >> n;
  REP(i, n) {
    ll x, y;
    cin >> x >> y;
    a.push_back(x);
    b.push_back(y);
  }
  reverse(all(a));
  reverse(all(b));
  REP(i, n) {
    if ((a[i] + push) % b[i] == 0)
      continue;
    push += b[i] - ((a[i] + push) % b[i]);
  }
  cout << push << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;
const ll inf2 = 1000000000000000000;
const ll mod = 1000000007;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;
const ll inf2 = 1000000000000000000;
const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, 0, n) { cin >> a[n - 1 - i] >> b[n - 1 - i]; }
  ll ans = 0;
  rep(i, 0, n) {
    if ((a[i] + ans) % b[i] == 0)
      continue;
    ans += (b[i] - (a[i] + ans) % b[i]);
    // cout << ans << endl;
  }
  cout << ans << endl;
}
Problem p03821 (C++)

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long a[100000], b[100000], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i > -1; i--) {
    while (1) {
      if ((a[i] + cnt) % b[i]) {
        cnt++;
      } else {
        break;
      }
    }
  }
  cout << cnt << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long a[100000], b[100000], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i > -1; i--) {
    cnt += (b[i] - (a[i] + cnt) % b[i]) % b[i];
  }
  cout << cnt << '\n';
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using ll = long long;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i).first >> a.at(i).second;
  }
  reverse(a.begin(), a.end());
  ll bufa = 0, bufb = 0;
  ll cnt = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    bufa = a.at(i).first, bufb = a.at(i).second;
    bufa += cnt;
    while (bufa % bufb != 0) {
      bufa++;
      cnt++;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using ll = long long;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i).first >> a.at(i).second;
  }
  reverse(a.begin(), a.end());
  ll bufa = 0, bufb = 0;
  ll cnt = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    bufa = a.at(i).first, bufb = a.at(i).second;
    bufa += cnt;
    cnt += (bufb - bufa % bufb) % bufb;
    ans += (bufb - bufa % bufb) % bufb;
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl b(n);
  rep(i, n) cin >> a[i] >> b[i];
  ll ans = 0;
  for (int i = (int)n - 1; i >= 0; i--) {
    int j = 0;
    for (;;)
      if ((a[i] + ans) % b[i] == 0)
        break;
      else
        ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl b(n);
  rep(i, n) cin >> a[i] >> b[i];
  ll ans = 0;
  for (int i = (int)n - 1; i >= 0; i--)
    ans += (b[i] - (a[i] + ans) % b[i]) % b[i];

  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;
typedef long long ll;
const int maxn = 1e6;
ll n, t, a[maxn], b[maxn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (int i = n; i >= 1; i--) {
    while ((a[i] + t) % b[i] != 0)
      t++;
  }
  cout << t << endl;
}


Fixed submission:
#include <iostream>

using namespace std;
typedef long long ll;
const int maxn = 1e6;
ll n, t, a[maxn], b[maxn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (int i = n; i >= 1; i--) {
    while ((a[i] + t) % b[i] != 0)
      t += (b[i] - (a[i] + t) % b[i]);
  }
  cout << t << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1e9 + 7;
const int inf = 1 << 20;
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using namespace std;

int main() {
  int n;
  cin >> n;
  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--) {
    while ((a[i] + ans) % b[i] != 0) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1e9 + 7;
const int inf = 1 << 20;
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using namespace std;

int main() {
  int n;
  cin >> n;
  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--) {
    if ((a[i] + ans) % b[i] == 0)
      continue;
    else {
      ll m = (a[i] + ans) % b[i];
      ans += (b[i] - m);
    }
  }
  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long> A;
  vector<long> B;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }

  long result = 0;
  for (int i = N - 1; i >= 0; i--) {
    while ((A[i] + result) % B[i] != 0) {
      result++;
    }
  }
  cout << result;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long> A;
  vector<long> B;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }

  long result = 0;
  for (int i = N - 1; i >= 0; i--) {
    int amari = (A[i] + result) % B[i];
    int plusnum = amari == 0 ? 0 : B[i] - amari;
    result += plusnum;
  }
  cout << result;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

  long long a[N];
  long long b[N];
  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i];
  }

  long long ret = 0;
  for (int i = N - 1; i >= 0; --i) {
    if (a[i] + ret > b[i] && (a[i] + ret) % b[i] != 0) {
      ret += b[i] * ((a[i] + ret) / b[i] + 1) - (a[i] + ret);
    } else if (a[i] + ret < b[i] && b[i] % (a[i] + ret) != 0) {
      ret += b[i] - (a[i] + ret);
    }
  }

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

  long long a[N];
  long long b[N];
  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i];
  }

  long long ret = 0;
  for (int i = N - 1; i >= 0; --i) {
    if (a[i] + ret == 0)
      continue;
    if (a[i] + ret > b[i] && (a[i] + ret) % b[i] != 0) {
      ret += b[i] * ((a[i] + ret) / b[i] + 1) - (a[i] + ret);
    } else if (a[i] + ret < b[i] && b[i] % (a[i] + ret) != 0) {
      ret += b[i] - (a[i] + ret);
    }
  }

  cout << ret << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

long long n, a[100000], b[100000], d = 0;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] + d) % b[i]) {
      int t = 0, j = 1;
      do {
        t = b[i] * j - a[i];
        j++;
      } while (t < d);
      d = t;
    }
  }
  cout << d << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

long long n, a[100000], b[100000], d = 0;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] + d) % b[i]) {
      long long t = 0, j = (a[i] + d) / b[i];
      do {
        t = b[i] * j - a[i];
        j++;
      } while (t < d);
      d = t;
    }
  }
  cout << d << endl;
  return 0;
}
Problem p03821 (C++)

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

// using ll = long long;
#define int long long

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
/***** MAIN *****/
signed main() {
  int n;
  cin >> n;
  int a[n], b[n];
  REP(i, n) { cin >> a[i] >> b[i]; }
  int ans = 0;
  REPR(i, n - 1) {
    if (b[i] == 1)
      continue;
    a[i] += ans;
    if (b[i] % a[i] == 0)
      continue;
    ans += b[i] - (a[i] % b[i]);
  }
  cout << ans;

  cout << "\n";
  return 0;
}
/***** MAIN *****/


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

// using ll = long long;
#define int long long

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
/***** MAIN *****/
signed main() {
  int n;
  cin >> n;
  int a[n], b[n];
  REP(i, n) { cin >> a[i] >> b[i]; }
  int ans = 0;
  REPR(i, n - 1) {
    if (b[i] == 1)
      continue;
    a[i] += ans % b[i];
    if (a[i] % b[i] == 0)
      continue;
    ans += b[i] - (a[i] % b[i]);
  }
  cout << ans;

  cout << "\n";
  return 0;
}
/***** MAIN *****/
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
const long long P = 1000000007;

int main() {
  int n;
  cin >> n;
  long long a[n], b[n];
  long long count = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    a[i] += count;
    while (a[i] % b[i] != 0) {
      count++;
      a[i]++;
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
const long long P = 1000000007;

int main() {
  int n;
  cin >> n;
  long long a[n], b[n];
  long long count = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    a[i] += count;
    if (a[i] % b[i] != 0) {
      count += b[i] - (a[i] % b[i]);
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03821 (C++)

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
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 vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  REP(i, N) cin >> A[i] >> B[i];
  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    A[i] += ans;
    while (A[i] % B[i] != 0) {
      ans++;
      A[i]++;
    }
  }
  cout << ans << endl;
}


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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
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 vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  REP(i, N) cin >> A[i] >> B[i];
  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    A[i] += ans;
    if (A[i] > B[i]) {
      ll tmp = 0;
      if (B[i] != 1) {
        if ((A[i] % B[i]) == 0) {
        } else {
          tmp = B[i] - (A[i] % B[i]);
        }
      }
      A[i] = A[i] + tmp;
      ans += tmp;
    } else {
      ll tmp = 0;
      if (B[i] != 1) {
        if ((A[i] % B[i]) == 0) {
        } else {
          tmp = B[i] - A[i];
        }
      }
      A[i] = A[i] + tmp;
      ans += tmp;
    }
  }
  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll cnt = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (i != n - 1)
      a[i] += cnt;
    while (a[i] % b[i]) {
      ++a[i];
      ++cnt;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  ll cnt = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (i != n - 1)
      a[i] += cnt;
    int x = a[i] % b[i];
    if (x)
      cnt += b[i] - x;
  }
  cout << cnt << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

const int MAXN = 10010;

int a[MAXN], b[MAXN];

long long update(long long next, long long a, long long b) {
  long long x = (a / b) * b - a;
  if (x < 0)
    x += b;
  if (x < next) {
    x = next;
    if ((x + a) % b > 0)
      x = x + (b - (x + a) % b);
  }
  return x;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  long long ans = 0;
  for (int i = n; i >= 1; i--)
    ans = update(ans, a[i], b[i]);
  cout << ans;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

const int MAXN = 100010;

int a[MAXN], b[MAXN];

long long update(long long next, long long a, long long b) {
  long long x = (a / b) * b - a;
  if (x < 0)
    x += b;
  if (x < next) {
    x = next;
    if ((x + a) % b > 0)
      x = x + (b - (x + a) % b);
  }
  return x;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  long long ans = 0;
  for (int i = n; i >= 1; i--)
    ans = update(ans, a[i], b[i]);
  cout << ans;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const long long INFL = LONG_LONG_MAX;
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int n;
  cin >> n;
  vint A(n), B(n);
  rep(i, n) { cin >> A[i] >> B[i]; }
  ll c = 0;
  rep(i, n) {

    while ((A[n - 1 - i] + c) % B[n - 1 - i] != 0)
      c++;
  }

  cout << c << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const long long INFL = LONG_LONG_MAX;
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int n;
  cin >> n;
  vint A(n), B(n);
  rep(i, n) { cin >> A[i] >> B[i]; }
  ll c = 0;
  for (auto i = n - 1; i >= 0; i--) {
    if ((c + A[i]) % B[i] == 0)
      continue;
    c += (B[i] - ((c + A[i]) % B[i]));
  }

  cout << c << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <numeric>
typedef long long Int;
#define rep(i, a, b) for (Int i = a; i < b; ++i)
#define rrep(i, a, b) for (Int i = a; i >= b; --i)
using namespace std;

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

  Int N;
  cin >> N;
  vector<Int> A(N), B(N);
  rep(i, 0, N) cin >> A[i] >> B[i];

  Int res = 0;
  rrep(i, N - 1, 0) {
    A[i] += res;
    while (A[i] % B[i] != 0) {
      ++res;
      A[i]++;
    }
  }

  cout << res << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <numeric>
typedef long long Int;
#define rep(i, a, b) for (Int i = a; i < b; ++i)
#define rrep(i, a, b) for (Int i = a; i >= b; --i)
using namespace std;

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

  Int N;
  cin >> N;
  vector<Int> A(N), B(N);
  rep(i, 0, N) cin >> A[i] >> B[i];

  Int res = 0;
  rrep(i, N - 1, 0) {
    A[i] += res;
    if (A[i] % B[i] == 0)
      continue;
    res += (B[i] - A[i] % B[i]);
  }

  cout << res << endl;
}