Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  ll ans = 0;
  queue<int> que;
  for (int i = 0; i < N; i++) {
    int v1 = B[(i + N - 1) % N];
    int v2 = B[i];
    int v3 = B[(i + 1) % N];
    if (v1 < v2 and v2 > v3 and v2 > A[i])
      que.emplace(i);
  }

  while (que.size()) {
    int k = que.front();
    que.pop();
    ll ub, lb;
    {
      ll v1 = B[(k + N - 1) % N];
      ll v3 = B[(k + 1) % N];

      ub = 1LL * (B[k] + v1 + v3 - 1) / (v1 + v3) + 10;
      lb = 0;
    }

    while (ub - lb > 1) {
      ll mid = (ub + lb) / 2;
      ll v1 = B[(k + N - 1) % N];
      ll v2 = B[k];
      ll v3 = B[(k + 1) % N];
      v2 -= mid * (v1 + v3);

      bool cond = (v2 >= A[k]);
      if (cond)
        lb = mid;
      else
        ub = mid;
    }

    ll cnt = lb;
    {
      int v1 = B[(k + N - 1) % N];
      int v3 = B[(k + 1) % N];

      ans += cnt;
      B[k] -= cnt * (v1 + v3);
    }

    for (int d = -1; d <= 1; d++) {
      int i = (k + d + N) % N;
      int v1 = B[(i + N - 1) % N];
      int v2 = B[i];
      int v3 = B[(i + 1) % N];
      // fprintf(stderr, "v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3);
      if (v1 < v2 and v2 > v3 and v2 > A[i] and v1 + v3 < B[i]) {
        que.emplace(i);
      }
    }
  }

  bool ok = true;
  for (int i = 0; i < N; i++)
    ok &= (A[i] == B[i]);
  cout << (ok ? ans : -1) << endl;
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  ll ans = 0;
  queue<int> que;
  for (int i = 0; i < N; i++) {
    int v1 = B[(i + N - 1) % N];
    int v2 = B[i];
    int v3 = B[(i + 1) % N];
    if (v1 < v2 and v2 > v3 and v2 > A[i])
      que.emplace(i);
  }

  while (que.size()) {
    int k = que.front();
    que.pop();
    ll ub, lb;
    {
      ll v1 = B[(k + N - 1) % N];
      ll v3 = B[(k + 1) % N];

      ub = 1LL * (B[k] + v1 + v3 - 1) / (v1 + v3) + 10;
      lb = 0;
    }

    while (ub - lb > 1) {
      ll mid = (ub + lb) / 2;
      ll v1 = B[(k + N - 1) % N];
      ll v2 = B[k];
      ll v3 = B[(k + 1) % N];
      v2 -= mid * (v1 + v3);

      bool cond = (v2 >= A[k]);
      if (cond)
        lb = mid;
      else
        ub = mid;
    }

    ll cnt = lb;
    {
      int v1 = B[(k + N - 1) % N];
      int v3 = B[(k + 1) % N];

      ans += cnt;
      B[k] -= cnt * (v1 + v3);
    }

    for (int d = -1; d <= 1; d++) {
      int i = (k + d + N) % N;
      int v1 = B[(i + N - 1) % N];
      int v2 = B[i];
      int v3 = B[(i + 1) % N];
      // fprintf(stderr, "v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3);
      if (cnt > 0 and v1 < v2 and v2 > v3 and v2 > A[i] and v1 + v3 < B[i]) {
        que.emplace(i);
      }
    }
  }

  bool ok = true;
  for (int i = 0; i < N; i++)
    ok &= (A[i] == B[i]);
  cout << (ok ? ans : -1) << endl;
  return 0;
}