Problem p02984 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int updiv(int a, int b) { return (a + b - 1) / b; }

constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout.setf(ios_base::fixed);
  cout.precision(12);

  int n;
  cin >> n;

  vector<int> a(n);

  rep(i, n) { cin >> a[i]; }

  auto Isok = [&](int key) {
    int now = key;
    auto x = a;
    x[0] -= key / 2;
    rep(i, n) {
      x[(i + 1) % n] -= now / 2;
      now = x[(i + 1) % n] * 2;
      x[(i + 1) % n] -= now / 2;

      /*
      rep(j,n){
          cout << x[j] << ' ';
      }
      cout << now << '\n';
      */

      if (x[(i + 1) % n] < 0) {
        return false;
      }
    }
    if (now < 0)
      return false;
    return (true);
  };

  int low = -1, high = a[1] + 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (Isok(mid * 2))
      low = mid;
    else
      high = mid;
  }

  vector<int> res(n);

  int now = low * 2;
  a[0] -= now / 2;
  res[0] = now;
  rep(i, n) {
    a[(i + 1) % n] -= now / 2;
    now = a[(i + 1) % n] * 2;
    res[i + 1] = now;
    a[(i + 1) % n] -= now / 2;
  }

  cout << res.back();
  for (int i = 0; i < n - 1; ++i) {
    cout << ' ' << res[i];
  }
  cout << '\n';

  return (0);
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int updiv(int a, int b) { return (a + b - 1) / b; }

constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout.setf(ios_base::fixed);
  cout.precision(12);

  int n;
  cin >> n;

  vector<int> a(n);

  rep(i, n) { cin >> a[i]; }

  auto Isok = [&](int key) {
    int now = key;
    auto x = a;
    x[0] -= key / 2;
    rep(i, n) {
      x[(i + 1) % n] -= now / 2;
      now = x[(i + 1) % n] * 2;
      x[(i + 1) % n] -= now / 2;

      /*
      rep(j,n){
          cout << x[j] << ' ';
      }
      cout << now << '\n';
      */

      if (x[(i + 1) % n] < 0) {
        return false;
      }
    }
    if (now < 0)
      return false;
    return (true);
  };

  int low = -1, high = a[1] + 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (Isok(mid * 2))
      low = mid;
    else
      high = mid;
  }

  vector<int> res(n);

  int now = low * 2;
  a[0] -= now / 2;
  res[0] = now;
  rep(i, n) {
    a[(i + 1) % n] -= now / 2;
    now = a[(i + 1) % n] * 2;
    if (i != n - 1) {
      res[i + 1] = now;
    }
    a[(i + 1) % n] -= now / 2;
  }

  cout << res.back();
  for (int i = 0; i < n - 1; ++i) {
    cout << ' ' << res[i];
  }
  cout << '\n';

  return (0);
}