Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define VIN(n, v)                                                              \
  for (int64_t i = 0; i < (n); i++) {                                          \
    cin >> (v).at(i);                                                          \
  }

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

int main() {
  I n;
  cin >> n;
  V a(n), b(n);
  VIN(n, a);
  VIN(n, b);
  REP(i, n) {
    if (a.at(i) > b.at(i)) {
      cout << -1 << endl;
      return 0;
    }
    I prev;
    I next;
    if (i == 0) {
      prev = n - 1;
      next = 1;
    } else if (i == n - 1) {
      prev = n - 2;
      next = 0;
    } else {
      prev = i - 1;
      next = i + 1;
    }
    if (a.at(i) != b.at(i) && a.at(prev) + a.at(i) + a.at(next) > b.at(i)) {
      cout << -1 << endl;
      return 0;
    }
  }
  I t = 0;
  while (true) {
    bool chk = true;
    REP(i, n) {
      I prev;
      I next;
      if (i == 0) {
        prev = n - 1;
        next = 1;
      } else if (i == n - 1) {
        prev = n - 2;
        next = 0;
      } else {
        prev = i - 1;
        next = i + 1;
      }
      if (b.at(prev) + b.at(next) < b.at(i) && b.at(i) != a.at(i)) {
        b.at(i) -= b.at(prev) + b.at(next);
        t++;
        chk = false;
      }
    }
    if (chk == true)
      break;
  }
  bool chk2 = true;
  REP(i, n) {
    if (a.at(i) != b.at(i))
      chk2 = false;
  }
  if (chk2) {
    cout << t << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define VIN(n, v)                                                              \
  for (int64_t i = 0; i < (n); i++) {                                          \
    cin >> (v).at(i);                                                          \
  }

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

int main() {
  I n;
  cin >> n;
  V a(n), b(n);
  VIN(n, a);
  VIN(n, b);
  REP(i, n) {
    if (a.at(i) > b.at(i)) {
      cout << -1 << endl;
      return 0;
    }
    I prev;
    I next;
    if (i == 0) {
      prev = n - 1;
      next = 1;
    } else if (i == n - 1) {
      prev = n - 2;
      next = 0;
    } else {
      prev = i - 1;
      next = i + 1;
    }
    if (a.at(i) != b.at(i) && a.at(prev) + a.at(i) + a.at(next) > b.at(i)) {
      cout << -1 << endl;
      return 0;
    }
  }
  I t = 0;
  while (true) {
    bool chk = true;
    REP(i, n) {
      I prev;
      I next;
      if (i == 0) {
        prev = n - 1;
        next = 1;
      } else if (i == n - 1) {
        prev = n - 2;
        next = 0;
      } else {
        prev = i - 1;
        next = i + 1;
      }
      if (b.at(prev) + b.at(next) <= (b.at(i) - a.at(i))) {
        t += (b.at(i) - a.at(i)) / (b.at(prev) + b.at(next));
        b.at(i) -= (b.at(i) - a.at(i)) / (b.at(prev) + b.at(next)) *
                   (b.at(prev) + b.at(next));
        chk = false;
      }
    }
    if (chk == true)
      break;
  }
  bool chk2 = true;
  REP(i, n) {
    if (a.at(i) != b.at(i))
      chk2 = false;
  }
  if (chk2) {
    cout << t << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}