Problem p02941 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "{" << p.first << ", " << p.second << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

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

  int n;
  ll ans = 0;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> que;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i])
      que.push(P(b[i], i));
  }

  while (!que.empty()) {
    int v, m, l, r, s, k;
    tie(v, m) = que.top();
    que.pop();
    l = m == 0 ? n - 1 : m - 1;
    r = m == n - 1 ? 0 : m + 1;
    s = b[l] + b[r];
    k = a[m] >= s ? (b[m] - a[m]) / s : b[m] / s;

    if (b[m] == a[m])
      continue;
    if (b[m] <= s)
      continue;

    b[m] -= k * s;
    if (b[m] != a[m])
      que.push(P(b[m], m));
    ans += k;
  }

  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "{" << p.first << ", " << p.second << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

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

  int n;
  ll ans = 0;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> que;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i])
      que.push(P(b[i], i));
  }

  while (!que.empty()) {
    int v, m, l, r, s, k;
    tie(v, m) = que.top();
    que.pop();
    l = m == 0 ? n - 1 : m - 1;
    r = m == n - 1 ? 0 : m + 1;
    s = b[l] + b[r];
    k = a[m] >= s ? (b[m] - a[m]) / s : b[m] / s;

    if (b[m] == a[m])
      continue;
    if (b[m] <= s)
      continue;

    b[m] -= k * s;
    if (b[m] != a[m] && a[m] < s)
      que.push(P(b[m], m));
    ans += k;
  }

  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;

  return 0;
}