Problem p03395 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  cin >> N;
  int A[N], B[N];
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  int memo[51][51];
  rep(i, 51) rep(j, 51) {
    if (i == j) {
      memo[i][j] = 0;
      continue;
    }
    memo[i][j] = inf;
    rep2(k, 1, i) {
      if (i % k == j || i % k >= 2 * j) {
        memo[i][j] = k;
        break;
      }
    }
  }
  bool pote[N][51];
  fill(pote[0], pote[N], false);
  ll ans = 0;
  rep(i, N) pote[i][A[i]] = true;
  while (true) {
    int update = 0;
    rep(i, N) {
      int tmp = inf;
      rep(j, 51) {
        if (pote[i][j])
          chmin(tmp, memo[j][B[i]]);
      }
      chmax(update, tmp);
    }
    if (update == 0)
      break;
    if (update == inf) {
      cout << -1 << endl;
      return 0;
    }
    ans |= 1LL << update;
    rep(i, N) { rep(j, 51) if (pote[i][j]) pote[i][j % update] = true; }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  cin >> N;
  int A[N], B[N];
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  int memo[51][51];
  rep(i, 51) rep(j, 51) {
    if (i == j) {
      memo[i][j] = 0;
      continue;
    }
    memo[i][j] = inf;
    rep2(k, 1, i) {
      if (i % k == j || i % k > 2 * j) {
        memo[i][j] = k;
        break;
      }
    }
  }
  bool pote[N][51];
  fill(pote[0], pote[N], false);
  ll ans = 0;
  rep(i, N) pote[i][A[i]] = true;
  while (true) {
    int update = 0;
    rep(i, N) {
      int tmp = inf;
      rep(j, 51) {
        if (pote[i][j])
          chmin(tmp, memo[j][B[i]]);
      }
      chmax(update, tmp);
    }
    if (update == 0)
      break;
    if (update == inf) {
      cout << -1 << endl;
      return 0;
    }
    ans |= 1LL << update;
    rep(i, N) { rep(j, 51) if (pote[i][j]) pote[i][j % update] = true; }
  }
  cout << ans << endl;
}