Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;

using namespace std;

bool match[200000] = {};
ll a[200000] = {};
ll b[200000] = {};
priority_queue<pair<ll, ll>> q;
int n;

ll dfs(int i) {
  ll left = b[(i - 1 + n) % n];
  ll right = b[(i + 1 + n) % n];
  if (left + right == 0) {
    return 0;
  }
  ll ans = (abs(b[i] - a[i]) / (left + right));
  b[i] = b[i] - (abs(b[i] - a[i]) / (left + right)) * (left + right);
  if (b[i] == 0) {
    return 0;
  }
  if (a[i] == b[i])
    match[i] = true;

  if (b[(i - 2 + n) % n] + b[i] < b[(i - 1 + n) % n] &&
      !match[(i - 1 + n) % n]) {
    if (b[(i - 2 + n) % n] + b[i] != 0) {
      q.push(make_pair(b[(i - 1 + n) % n] % (b[(i - 2 + n) % n] + b[i]),
                       (i - 1 + n) % n));
    }
  }
  if (b[i] + b[(i + 2 + n) % n] < b[(i + 1 + n) % n] &&
      !match[(i + 1 + n) % n]) {
    if (b[i] + b[(i + 2 + n) % n] != 0) {
      q.push(make_pair(b[(i + 1 + n) % n] % (b[i] + b[(i + 2 + n) % n]),
                       (i + 1 + n) % n));
    }
  }
  return ans;
}

int main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  REP(i, n) {
    cin >> b[i];
    if (a[i] == b[i]) {
      match[i] = true;
    }
  }

  ll ans = 0;

  REP(i, n) {
    ll left = b[(i - 1 + n) % n];
    ll right = b[(i + 1 + n) % n];
    if (left + right < b[i] && !match[i]) {
      q.push(make_pair(b[i] % (left + right), i));
    }
  }

  while (!q.empty()) {
    auto top = q.top();
    q.pop();
    ans += dfs(top.second);
  }

  REP(i, n) {
    if (!match[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;

using namespace std;

bool match[200000] = {};
ll a[200000] = {};
ll b[200000] = {};
priority_queue<pair<ll, ll>> q;
int n;

ll dfs(int i) {
  ll left = b[(i - 1 + n) % n];
  ll right = b[(i + 1 + n) % n];
  if (left + right == 0) {
    return 0;
  }
  ll ans = (abs(b[i] - a[i]) / (left + right));
  if (ans == 0) {
    return 0;
  }
  b[i] = b[i] - (abs(b[i] - a[i]) / (left + right)) * (left + right);
  if (b[i] == 0) {
    return 0;
  }
  if (a[i] == b[i])
    match[i] = true;

  if (b[(i - 2 + n) % n] + b[i] < b[(i - 1 + n) % n] &&
      !match[(i - 1 + n) % n]) {
    if (b[(i - 2 + n) % n] + b[i] != 0) {
      q.push(make_pair(b[(i - 1 + n) % n] % (b[(i - 2 + n) % n] + b[i]),
                       (i - 1 + n) % n));
    }
  }
  if (b[i] + b[(i + 2 + n) % n] < b[(i + 1 + n) % n] &&
      !match[(i + 1 + n) % n]) {
    if (b[i] + b[(i + 2 + n) % n] != 0) {
      q.push(make_pair(b[(i + 1 + n) % n] % (b[i] + b[(i + 2 + n) % n]),
                       (i + 1 + n) % n));
    }
  }
  return ans;
}

int main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  REP(i, n) {
    cin >> b[i];
    if (a[i] == b[i]) {
      match[i] = true;
    }
  }

  ll ans = 0;

  REP(i, n) {
    ll left = b[(i - 1 + n) % n];
    ll right = b[(i + 1 + n) % n];
    if (left + right < b[i] && !match[i]) {
      q.push(make_pair(b[i] % (left + right), i));
    }
  }

  while (!q.empty()) {
    auto top = q.top();
    q.pop();
    ans += dfs(top.second);
  }

  REP(i, n) {
    if (!match[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}