Problem p02673 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <random>
using namespace std;
using ll = long long;
using dd = double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vdd = vector<dd>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using vvll = vector<vll>;
using vvdd = vector<vdd>;
using vvpll = vector<vpll>;
using vvtll = vector<vtll>;
using vvvll = vector<vvll>;
using vvvdd = vector<vvdd>;
using vvvpll = vector<vvpll>;
using vvvtll = vector<vvtll>;
using vvvvll = vector<vvvll>;
using vvvvdd = vector<vvvdd>;
using vvvvpll = vector<vvvpll>;
using vvvvtll = vector<vvvtll>;

constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
constexpr dd PI = 3.1415926535897932;

// cin,cout高速化のおまじない＋桁数指定
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast;

#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define dep(i, E, S) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define EACH(e, v) for (auto &&e : v)
#define each(e, v) for (auto &&e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  REPS(i, S, E) chmax(m, v[i]);
  return m;
} // v[S]～v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  REPS(i, S, E) chmin(m, v[i]);
  return m;
} // v[S]～v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
  return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
  return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
  ll m = S;
  REPS(i, S, E) {
    if (v[i] > v[m])
      m = i;
  }
  return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
  ll m = S;
  REPS(i, S, E) {
    if (v[i] < v[m])
      m = i;
  }
  return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
  return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
  return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
  return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
  return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
  T s = v[S];
  REPS(i, S + 1, E) s += v[i];
  return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline ll sz(T &v) { return (ll)v.size(); }
template <class T> inline T POW(T a, ll n) {
  T r = 1;
  for (; n > 0; n >>= 1, a *= a) {
    if (n & 1)
      r *= a;
  }
  return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); }
ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); }
inline ll MSB(ll a) {
  for (ll o = 63, x = -1;;) {
    ll m = (o + x) / 2;
    if (a < (1LL << m))
      o = m;
    else
      x = m;
    if (o - x == 1)
      return x;
  }
}
inline ll Upper2N(ll s) { return 1LL << MSB(s) << 1; } // s<2^nなる最小の2^n
inline vll Bit2Idx(ll m) {
  vll v;
  for (ll i = 0; m; m >>= 1, i++)
    if (m & 1)
      v.push_back(i);
  return move(v);
}
inline ll BitNum(ll m) {
  for (ll c = 0;; m >>= 1) {
    c += m & 1;
    if (!m)
      return c;
  }
}
inline ll Bit(ll s, ll i) { return (s >> i) & 1; } // sの第ibit i=0-63
[[nodiscard]] inline ll BitOn(ll s, ll i) {
  return s | (1LL << i);
} // sの第ibitON i=0-63
[[nodiscard]] inline ll BitOff(ll s, ll i) {
  return s & ~(1LL << i);
} // sの第ibitOFF i=0-63
[[nodiscard]] inline ll BitCut(ll s, ll i) {
  return s & (1LL << ++i) - 1;
} // sの第0～第ibitを抽出
inline vll str2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - '0');
  return move(v);
}
inline vll ALP2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - 'A');
  return move(v);
}
inline vll alp2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - 'a');
  return move(v);
}
template <class T> inline ll FloorIdx(vector<T> &v, T x) {
  return upper_bound(ALL(v), x) - v.begin() - 1;
}
template <class T> inline ll CeilIdx(vector<T> &v, T x) {
  return lower_bound(ALL(v), x) - v.begin();
}
template <class T>
inline ll UnderNumOf(vector<T> &v, T x) { // sort済vのx以下の個数を得る
  return upper_bound(v.begin(), v.end(), x) - v.begin();
}
template <class T, class Pr>
inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { // x以下個数
  return upper_bound(v.begin(), v.end(), x, pred) - v.begin();
}
template <class T>
inline ll OverNumOf(vector<T> &v, T x) { // sort済vのx以上の個数を得る
  return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin());
}
template <class T, class Pr>
inline ll OverNumOf(vector<T> &v, T x, Pr pred) { // x以上の個数
  return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin());
}

template <class T = ll> inline vector<T> cinv(ll N) {
  vector<T> v(N);
  REP(i, N) cin >> v[i];
  return move(v);
}
template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) {
  vector<pair<T, S>> v(N);
  REP(i, N) { cin >> v[i].first >> v[i].second; }
  return move(v);
}
template <class T = ll, class S = ll, class R = ll>
inline vector<tuple<T, S, R>> cinv3(ll N) {
  vector<tuple<T, S, R>> v(N);
  REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
  return move(v);
}
template <class T = ll, class S = ll, class R = ll, class Q = ll>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
  vector<tuple<T, S, R, Q>> v(N);
  REP(i, N) {
    cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
  }
  return move(v);
}
template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) {
  vector<vector<T>> vv(N);
  REP(i, N) vv[i] = cinv(M);
  return move(vv);
}
template <class T> inline void coutv(vector<T> &v, string d = " ") {
  ll N = (ll)v.size();
  REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
  cout << '\\n';
}
template <class T> inline void coutv(deque<T> &v, string d = " ") {
  ll N = (ll)v.size();
  REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
  cout << '\\n';
}
template <class T> void bye(T a) {
  cout << a << '\\n';
  exit(0);
}

#if defined(_DEBUG)
ll dumpW = 5;
template <class T> void Dump(vector<T> &v) {
  REP(i, (ll)v.size()) {
    cerr << ((i == 0) ? "[" : " ") << setw(dumpW) << v[i];
  }
  cerr << "]";
}
void Dump(vll &v) {
  REP(i, (ll)v.size()) {
    cerr << ((i == 0) ? "[" : " ") << setw(dumpW);
    if (v[i] == INF)
      cerr << "INF";
    else if (v[i] == -INF)
      cerr << "-INF";
    else
      cerr << v[i];
  }
  cerr << "]";
}
template <class T> void Dump(vector<vector<T>> &v) {
  ll N = (ll)v.size();
  string d[2][2] = {{" ", "["}, {"\\n", "]\\n"}};
  REP(i, N) {
    cerr << d[0][i == 0];
    Dump(v[i]);
    cerr << d[1][i == N - 1];
  }
}
template <class T> void Dump(vector<vector<vector<T>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  <", "[ <"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  ---<", "[ ---<"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">---\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  ======<", "[ ======<"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">======\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T, class S> void Dump(map<T, S> &m) {
  for (auto e : m)
    cout << e.first << " : " << e.second << '\\n';
}
#else
template <class T> void Dump(vector<T> &v) {}
template <class T> void Dump(vector<vector<T>> &v) {}
template <class T> void Dump(vector<vector<vector<T>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {}
#endif

template <ll MOD> struct mll_ {
  ll val;
  mll_(ll v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  mll_ operator-() const { return -val; }
  mll_ operator+(const mll_ &b) const { return val + b.val; }
  mll_ operator-(const mll_ &b) const { return val - b.val; }
  mll_ operator*(const mll_ &b) const { return val * b.val; }
  mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; }
  mll_ operator+(ll b) const { return *this + mll_(b); }
  mll_ operator-(ll b) const { return *this - mll_(b); }
  mll_ operator*(ll b) const { return *this * mll_(b); }
  friend mll_ operator+(ll a, const mll_ &b) { return b + a; }
  friend mll_ operator-(ll a, const mll_ &b) { return -b + a; }
  friend mll_ operator*(ll a, const mll_ &b) { return b * a; }
  friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; }
  mll_ &operator+=(const mll_ &b) {
    val = (val + b.val) % MOD;
    return *this;
  }
  mll_ &operator-=(const mll_ &b) {
    val = (val + MOD - b.val) % MOD;
    return *this;
  }
  mll_ &operator*=(const mll_ &b) {
    val = (val * b.val) % MOD;
    return *this;
  }
  mll_ &operator/=(const mll_ &b) {
    ll c = b.val, d = MOD, u = 1, v = 0;
    while (d) {
      ll t = c / d;
      c -= t * d;
      swap(c, d);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mll_ &operator+=(ll b) { return *this += mll_(b); }
  mll_ &operator-=(ll b) { return *this -= mll_(b); }
  mll_ &operator*=(ll b) { return *this *= mll_(b); }
  mll_ &operator/=(ll b) { return *this /= mll_(b); }
  bool operator==(const mll_ &b) { return val == b.val; }
  bool operator!=(const mll_ &b) { return val != b.val; }
  bool operator==(ll b) { return *this == mll_(b); }
  bool operator!=(ll b) { return *this != mll_(b); }
  friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; }
  friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; }
  friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; }
  friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; }
  static mll_ Combination(ll a, ll b) {
    chmin(b, a - b);
    if (b < 0)
      return mll_(0);
    mll_ c = 1;
    REP(i, b) c *= a - i;
    REP(i, b) c /= i + 1;
    return c;
  }
};
using mll = mll_<1000000007LL>; // 1000000007LL;// 998244353LL;
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
using vvvvvmll = std::vector<vvvvmll>;

template <class T> struct CumulativeSumG {
  ll s = 0, e = -1;
  vector<T> cum;
  CumulativeSumG(){};
  CumulativeSumG(vector<T> &v) : e(v.size() - 1), cum(e + 2) { ini(v); }
  CumulativeSumG(vector<T> &v, ll S, ll E) : s(S), e(E), cum(e - s + 2) {
    ini(v);
  }
  void Init(vector<T> &v) {
    e = v.size() - 1;
    cum.resize(e + 2);
    ini(v);
  }
  void Init(vector<T> &v, ll S, ll E) {
    s = S;
    e = E;
    cum.resize(e - s + 2);
    ini(v);
  }
  void Add(T x) {
    e++;
    cum.push_back(cum.back() + x);
  }
  T operator()(ll begin, ll end) { return cum[end + 1 - s] - cum[begin - s]; }
  void ini(vector<T> &v) { REPS(i, s, e) cum[i + 1 - s] = cum[i - s] + v[i]; }
};
using CumulativeSum = CumulativeSumG<ll>;

ll n;
vll a, b;
CumulativeSum cumb;
CumulativeSum cumib;

dd calc(ll l, ll r, ll q) {
  dd L = (dd)a[l - 1];
  dd R = (dd)a[(r + 1) % n];
  if (l == r) {
    dd x = -b[l] + .5 * (L + R);
    return x;
  }
  ll m = r - l + 1;
  dd xr = L + m * R - 2. * cumib(l, r) + 2. * (l - 1) * cumb(l, r);
  xr /= m + 1;
  dd xl = L + R - 2. * cumb(l, r) - xr;

  ll p = q - l;
  dd xq =
      -p * L + (p + 1) * xl + 2. * q * cumb(l, q - 1) - 2. * cumib(l, q - 1);
  return xq;
}

void solve() {
  cin >> n;
  vector<ll> a_ = cinv<ll>(n);
  vector<ll> b_ = cinv<ll>(n);
  ll idx = MaxI(a_);
  a.resize(n);
  b.resize(n);
  rep(i, idx, idx + n - 1) {
    a[i - idx] = a_[i % n];
    b[i - idx] = b_[i % n];
  }

  vll ibi(n);
  rep(i, 0, n - 1) ibi[i] = i * b[i];
  cumb.Init(b);
  cumib.Init(ibi);

  vll stk;
  stk.reserve(n + 10);
  stk.push_back(0);
  stk.push_back(1);
  rep(i, 2, n) {
    while (sz(stk) >= 2) {
      ll len = sz(stk);
      ll l = stk[len - 2] + 1;
      ll m = stk[len - 1];
      ll r = i - 1;

      dd e = calc(l, r, m);
      if (a[m] <= e) {
        stk.pop_back();
      } else
        break;
    }
    stk.push_back(i);
  }

  vdd ex(n);
  rep(i, 0, sz(stk) - 2) {
    ll l = stk[i] + 1;
    ll r = stk[i + 1] - 1;
    ex[l - 1] = (dd)a[l - 1];

    dd L = (dd)a[l - 1];
    dd R = (dd)a[(r + 1) % n];
    if (r < l)
      continue;
    if (l == r) {
      dd x = -b[l] + .5 * (L + R);
      ex[l] = x;
      continue;
    }
    ll m = r - l + 1;
    dd xr = L + m * R - 2. * cumib(l, r) + 2. * (l - 1) * cumb(l, r);
    xr /= m + 1;
    dd xl = L + R - 2. * cumb(l, r) - xr;
    ex[l] = xl;
    ex[r] = xr;
    dd x1, x2;
    x1 = xl;
    x2 = L;
    rep(i, l + 1, r - 1) {
      dd xi = 2. * x1 - x2 + 2. * b[i - 1];
      x2 = x1;
      x1 = xi;
      ex[i] = xi;
    }
  }

  dd ans = 0.;
  rep(i, 0, n - 1) { ans += ex[i]; }
  ans /= n;
  cout << ans << '\\n';
}

int main() {
#if 1
  solve();
#else
  ll t;
  cin >> t;
  rep(i, 0, t - 1) { solve(); }
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
using namespace std;
using ll = long long;
using dd = double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vdd = vector<dd>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using vvll = vector<vll>;
using vvdd = vector<vdd>;
using vvpll = vector<vpll>;
using vvtll = vector<vtll>;
using vvvll = vector<vvll>;
using vvvdd = vector<vvdd>;
using vvvpll = vector<vvpll>;
using vvvtll = vector<vvtll>;
using vvvvll = vector<vvvll>;
using vvvvdd = vector<vvvdd>;
using vvvvpll = vector<vvvpll>;
using vvvvtll = vector<vvvtll>;

constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
constexpr dd PI = 3.1415926535897932;

// cin,cout高速化のおまじない＋桁数指定
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
  }
} fast;

#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define dep(i, E, S) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define EACH(e, v) for (auto &&e : v)
#define each(e, v) for (auto &&e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  REPS(i, S, E) chmax(m, v[i]);
  return m;
} // v[S]～v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  REPS(i, S, E) chmin(m, v[i]);
  return m;
} // v[S]～v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
  return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
  return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
  ll m = S;
  REPS(i, S, E) {
    if (v[i] > v[m])
      m = i;
  }
  return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
  ll m = S;
  REPS(i, S, E) {
    if (v[i] < v[m])
      m = i;
  }
  return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
  return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
  return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
  return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
  return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
  T s = v[S];
  REPS(i, S + 1, E) s += v[i];
  return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline ll sz(T &v) { return (ll)v.size(); }
template <class T> inline T POW(T a, ll n) {
  T r = 1;
  for (; n > 0; n >>= 1, a *= a) {
    if (n & 1)
      r *= a;
  }
  return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); }
ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); }
inline ll MSB(ll a) {
  for (ll o = 63, x = -1;;) {
    ll m = (o + x) / 2;
    if (a < (1LL << m))
      o = m;
    else
      x = m;
    if (o - x == 1)
      return x;
  }
}
inline ll Upper2N(ll s) { return 1LL << MSB(s) << 1; } // s<2^nなる最小の2^n
inline vll Bit2Idx(ll m) {
  vll v;
  for (ll i = 0; m; m >>= 1, i++)
    if (m & 1)
      v.push_back(i);
  return move(v);
}
inline ll BitNum(ll m) {
  for (ll c = 0;; m >>= 1) {
    c += m & 1;
    if (!m)
      return c;
  }
}
inline ll Bit(ll s, ll i) { return (s >> i) & 1; } // sの第ibit i=0-63
[[nodiscard]] inline ll BitOn(ll s, ll i) {
  return s | (1LL << i);
} // sの第ibitON i=0-63
[[nodiscard]] inline ll BitOff(ll s, ll i) {
  return s & ~(1LL << i);
} // sの第ibitOFF i=0-63
[[nodiscard]] inline ll BitCut(ll s, ll i) {
  return s & (1LL << ++i) - 1;
} // sの第0～第ibitを抽出
inline vll str2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - '0');
  return move(v);
}
inline vll ALP2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - 'A');
  return move(v);
}
inline vll alp2num(string &s, ll N) {
  vll v(N);
  REP(i, N) v[i] = (ll)(s[i] - 'a');
  return move(v);
}
template <class T> inline ll FloorIdx(vector<T> &v, T x) {
  return upper_bound(ALL(v), x) - v.begin() - 1;
}
template <class T> inline ll CeilIdx(vector<T> &v, T x) {
  return lower_bound(ALL(v), x) - v.begin();
}
template <class T>
inline ll UnderNumOf(vector<T> &v, T x) { // sort済vのx以下の個数を得る
  return upper_bound(v.begin(), v.end(), x) - v.begin();
}
template <class T, class Pr>
inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { // x以下個数
  return upper_bound(v.begin(), v.end(), x, pred) - v.begin();
}
template <class T>
inline ll OverNumOf(vector<T> &v, T x) { // sort済vのx以上の個数を得る
  return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin());
}
template <class T, class Pr>
inline ll OverNumOf(vector<T> &v, T x, Pr pred) { // x以上の個数
  return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin());
}

template <class T = ll> inline vector<T> cinv(ll N) {
  vector<T> v(N);
  REP(i, N) cin >> v[i];
  return move(v);
}
template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) {
  vector<pair<T, S>> v(N);
  REP(i, N) { cin >> v[i].first >> v[i].second; }
  return move(v);
}
template <class T = ll, class S = ll, class R = ll>
inline vector<tuple<T, S, R>> cinv3(ll N) {
  vector<tuple<T, S, R>> v(N);
  REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
  return move(v);
}
template <class T = ll, class S = ll, class R = ll, class Q = ll>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
  vector<tuple<T, S, R, Q>> v(N);
  REP(i, N) {
    cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
  }
  return move(v);
}
template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) {
  vector<vector<T>> vv(N);
  REP(i, N) vv[i] = cinv(M);
  return move(vv);
}
template <class T> inline void coutv(vector<T> &v, string d = " ") {
  ll N = (ll)v.size();
  REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
  cout << '\\n';
}
template <class T> inline void coutv(deque<T> &v, string d = " ") {
  ll N = (ll)v.size();
  REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
  cout << '\\n';
}
template <class T> void bye(T a) {
  cout << a << '\\n';
  exit(0);
}

#if defined(_DEBUG)
ll dumpW = 5;
template <class T> void Dump(vector<T> &v) {
  REP(i, (ll)v.size()) {
    cerr << ((i == 0) ? "[" : " ") << setw(dumpW) << v[i];
  }
  cerr << "]";
}
void Dump(vll &v) {
  REP(i, (ll)v.size()) {
    cerr << ((i == 0) ? "[" : " ") << setw(dumpW);
    if (v[i] == INF)
      cerr << "INF";
    else if (v[i] == -INF)
      cerr << "-INF";
    else
      cerr << v[i];
  }
  cerr << "]";
}
template <class T> void Dump(vector<vector<T>> &v) {
  ll N = (ll)v.size();
  string d[2][2] = {{" ", "["}, {"\\n", "]\\n"}};
  REP(i, N) {
    cerr << d[0][i == 0];
    Dump(v[i]);
    cerr << d[1][i == N - 1];
  }
}
template <class T> void Dump(vector<vector<vector<T>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  <", "[ <"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  ---<", "[ ---<"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">---\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {
  ll N = (ll)v.size();
  string d[2] = {"  ======<", "[ ======<"};
  REP(i, N) {
    cerr << d[i == 0] << i << ">======\\n";
    Dump(v[i]);
  }
  cerr << "]\\n";
}
template <class T, class S> void Dump(map<T, S> &m) {
  for (auto e : m)
    cout << e.first << " : " << e.second << '\\n';
}
#else
template <class T> void Dump(vector<T> &v) {}
template <class T> void Dump(vector<vector<T>> &v) {}
template <class T> void Dump(vector<vector<vector<T>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {}
#endif

template <ll MOD> struct mll_ {
  ll val;
  mll_(ll v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  mll_ operator-() const { return -val; }
  mll_ operator+(const mll_ &b) const { return val + b.val; }
  mll_ operator-(const mll_ &b) const { return val - b.val; }
  mll_ operator*(const mll_ &b) const { return val * b.val; }
  mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; }
  mll_ operator+(ll b) const { return *this + mll_(b); }
  mll_ operator-(ll b) const { return *this - mll_(b); }
  mll_ operator*(ll b) const { return *this * mll_(b); }
  friend mll_ operator+(ll a, const mll_ &b) { return b + a; }
  friend mll_ operator-(ll a, const mll_ &b) { return -b + a; }
  friend mll_ operator*(ll a, const mll_ &b) { return b * a; }
  friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; }
  mll_ &operator+=(const mll_ &b) {
    val = (val + b.val) % MOD;
    return *this;
  }
  mll_ &operator-=(const mll_ &b) {
    val = (val + MOD - b.val) % MOD;
    return *this;
  }
  mll_ &operator*=(const mll_ &b) {
    val = (val * b.val) % MOD;
    return *this;
  }
  mll_ &operator/=(const mll_ &b) {
    ll c = b.val, d = MOD, u = 1, v = 0;
    while (d) {
      ll t = c / d;
      c -= t * d;
      swap(c, d);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mll_ &operator+=(ll b) { return *this += mll_(b); }
  mll_ &operator-=(ll b) { return *this -= mll_(b); }
  mll_ &operator*=(ll b) { return *this *= mll_(b); }
  mll_ &operator/=(ll b) { return *this /= mll_(b); }
  bool operator==(const mll_ &b) { return val == b.val; }
  bool operator!=(const mll_ &b) { return val != b.val; }
  bool operator==(ll b) { return *this == mll_(b); }
  bool operator!=(ll b) { return *this != mll_(b); }
  friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; }
  friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; }
  friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; }
  friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; }
  static mll_ Combination(ll a, ll b) {
    chmin(b, a - b);
    if (b < 0)
      return mll_(0);
    mll_ c = 1;
    REP(i, b) c *= a - i;
    REP(i, b) c /= i + 1;
    return c;
  }
};
using mll = mll_<1000000007LL>; // 1000000007LL;// 998244353LL;
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
using vvvvvmll = std::vector<vvvvmll>;

template <class T> struct CumulativeSumG {
  ll s = 0, e = -1;
  vector<T> cum;
  CumulativeSumG(){};
  CumulativeSumG(vector<T> &v) : e(v.size() - 1), cum(e + 2) { ini(v); }
  CumulativeSumG(vector<T> &v, ll S, ll E) : s(S), e(E), cum(e - s + 2) {
    ini(v);
  }
  void Init(vector<T> &v) {
    e = v.size() - 1;
    cum.resize(e + 2);
    ini(v);
  }
  void Init(vector<T> &v, ll S, ll E) {
    s = S;
    e = E;
    cum.resize(e - s + 2);
    ini(v);
  }
  void Add(T x) {
    e++;
    cum.push_back(cum.back() + x);
  }
  T operator()(ll begin, ll end) { return cum[end + 1 - s] - cum[begin - s]; }
  void ini(vector<T> &v) { REPS(i, s, e) cum[i + 1 - s] = cum[i - s] + v[i]; }
};
using CumulativeSum = CumulativeSumG<ll>;

template <class T> struct SegmentTree {
  using F = function<T(T, T)>;
  vector<T> dat; // 第i要素 dat[i+n-1]
  ll n;          // 要素数以上の最小の 2のべき乗
  F f;
  T ti;
  SegmentTree() {}
  SegmentTree(vector<T> &v, F f, T ti) { Init(v, f, ti); }
  void Init(vector<T> &v, F f, T ti) {
    this->f = f;
    this->ti = ti;
    for (n = 1; n < (ll)v.size(); n *= 2)
      ;
    dat.resize(2 * n - 1, ti);
    REP(i, (ll)v.size()) { dat[i + n - 1] = v[i]; }               // 初期化
    DEP(i, n - 1) { dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } // 構築
  }
  void Set(ll i, T x) { // 第i要素にxをセット
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]);
    }
  }
  T Range(ll a, ll b) { return range(a, b + 1, 0, 0, n); }
  T operator[](ll i) { return dat[i + n - 1]; }
  void Dump(ll w = 5) {
    REP(i, n)
    for (ll k = i + n - 1, m = 1, p = 1; k >= 0;
         p = m, m *= k % 2, k = (k == 0) ? -1 : (k - 1) / 2) {
      if (m)
        cerr << ((k < n - 1) ? " ┬ " : "") << setw(w) << dat[k];
      else
        cerr << ((p & ~m) ? " ┘ " : "");
      if (k == 0)
        cerr << '\\n';
    }
    cerr << '\\n';
  }
  T range(ll a, ll b, ll k, ll l, ll r) { // 注目ノードk=[l,r)
    if (r <= a || b <= l)
      return ti; // 完全に外
    if (a <= l && r <= b)
      return dat[k]; // 完全に含まれる
    return f(range(a, b, k * 2 + 1, l, (l + r) / 2),
             range(a, b, k * 2 + 2, (l + r) / 2, r));
  }
  ll FindL(ll a, ll b, T x) { return findL(a, b + 1, x, 0, 0, n); }
  ll findL(ll a, ll b, T x, ll k, ll l, ll r) { //[l,r)内最左 なければb
    if (r <= a || b <= l || f(dat[k], x) != dat[k])
      return b; // 範囲外or満たさない
    if (k >= n - 1)
      return k - (n - 1); // 葉なら見つかった→位置return
    ll i = findL(a, b, x, 2 * k + 1, l, (l + r) / 2); // 左部分木探す
    if (i == b)
      i = findL(a, b, x, 2 * k + 2, (l + r) / 2, r); // 左になければ右
    return i;
  }
  ll FindR(ll a, ll b, T x) { return findR(a, b + 1, x, 0, 0, n); }
  ll findR(ll a, ll b, T x, ll k, ll l, ll r) { //[l,r)内最右 なければa-1
    if (r <= a || b <= l || f(dat[k], x) != dat[k])
      return a - 1; // 範囲外or満たさない
    if (k >= n - 1)
      return k - (n - 1); // 葉なら見つかった→位置return
    ll i = findR(a, b, x, 2 * k + 2, (l + r) / 2, r); // 右部分木探す
    if (i == a - 1)
      i = findR(a, b, x, 2 * k + 1, l, (l + r) / 2); // 右になければ左
    return i;
  }
};

ll n;
vll a, b;
vdd ex;
CumulativeSum cumb;
CumulativeSum cumib;
SegmentTree<ll> sgt;

dd calc(ll l, ll r, ll q) {
  dd L = (dd)a[l - 1];
  dd R = (dd)a[(r + 1) % n];
  if (l == r) {
    dd x = -b[l] + .5 * (L + R);
    return x;
  }
  ll m = r - l + 1;
  dd xr = L + m * R - 2. * cumib(l, r) + 2. * (l - 1) * cumb(l, r);
  xr /= m + 1;
  dd xl = L + R - 2. * cumb(l, r) - xr;

  dd x1, x2;
  if (q - l < r - q) { // lが近い時
    x1 = xl;
    x2 = L;
    rep(i, l + 1, q) {
      dd xi = 2. * x1 - x2 + 2. * b[i - 1];
      x2 = x1;
      x1 = xi;
    }
  } else { // rが近い時
    x1 = xr;
    x2 = R;
    dep(i, r - 1, q) {
      dd xi = 2. * x1 - x2 + 2. * b[i + 1];
      x2 = x1;
      x1 = xi;
    }
  }
  return x1;
}

void dfs(ll l, ll r) {
  dd L = ex[l - 1];
  dd R = ex[r + 1];
  if (l == r) {
    dd x = -b[l] + .5 * (L + R);
    ex[l] = max((dd)a[l], x);
    return;
  }
  ll m = r - l + 1;
  dd xr = L + m * R - 2. * cumib(l, r) + 2. * (l - 1) * cumb(l, r);
  xr /= m + 1;
  dd xl = L + R - 2. * cumb(l, r) - xr;
  ll stick = -1;
  {
    vdd xls, xrs;
    xls.push_back(L);
    xls.push_back(xl);
    xrs.push_back(R);
    xrs.push_back(xr);
    ll st = l + 1, en = r - 1;
    while (true) {
      ll mx = sgt.Range(l, r);
      if (mx < 0)
        break;
      ll q = sgt.FindL(l, r, mx);
      sgt.Set(q, -INF);
      // x[q]計算
      if (st <= q and q <= en) {
        if (q - st < en - q) { // stが近い時
          rep(i, st, q) {
            dd x_1 = *(xls.end() - 2);
            dd x_i = xls.back();
            dd x_nx = -x_1 + 2. * x_i + 2. * b[i - 1];
            xls.push_back(x_nx);
            if (a[i] <= x_nx)
              sgt.Set(i, -INF);
          }
          st = q + 1;
        } else { // enが近い時
          dep(i, en, q) {
            dd x_1 = *(xrs.end() - 2);
            dd x_i = xrs.back();
            dd x_nx = -x_1 + 2. * x_i + 2. * b[i + 1];
            xrs.push_back(x_nx);
            if (a[i] <= x_nx)
              sgt.Set(i, -INF);
          }
          en = q - 1;
        }
      }
      dd xq;
      if (q < st)
        xq = xls[q - (l - 1)];
      else
        xq = xrs[(r + 1) - q];
      if (a[q] > xq) {
        stick = q;
        break;
      }
    }

    if (stick == -1) { // stickがない→x[*]が期待値、終了
      rep(i, st, en) {
        dd x_1 = *(xls.end() - 2);
        dd x_i = xls.back();
        dd x_nx = -x_1 + 2. * x_i + 2. * b[i - 1];
        xls.push_back(x_nx);
      }
      st = en + 1;

      rep(i, l, st - 1) { ex[i] = xls[i - (l - 1)]; }
      dep(i, r, en + 1) { ex[i] = xrs[(r + 1) - i]; }
      return;
    }
  }

  // stickがあった時：再帰
  ex[stick] = (dd)a[stick];
  if (l <= stick - 1)
    dfs(l, stick - 1);
  if (stick + 1 <= r)
    dfs(stick + 1, r);
}

void solve() {
  cin >> n;
  vector<ll> a_ = cinv<ll>(n);
  vector<ll> b_ = cinv<ll>(n);
  ll idx = MaxI(a_);
  a.resize(n);
  b.resize(n);
  rep(i, idx, idx + n - 1) {
    a[i - idx] = a_[i % n];
    b[i - idx] = b_[i % n];
  }

  vll ibi(n);
  rep(i, 0, n - 1) ibi[i] = i * b[i];
  cumb.Init(b);
  cumib.Init(ibi);

  vll stk;
  stk.push_back(0);
  stk.push_back(1);
  rep(i, 2, n) {
    while (sz(stk) >= 2) {
      ll len = sz(stk);
      ll l = stk[len - 2] + 1;
      ll m = stk[len - 1];
      ll r = i - 1;

      dd e = calc(l, r, m);
      if (a[m] <= e) {
        stk.pop_back();
      } else
        break;
    }
    stk.push_back(i);
  }

  vdd ex(n);
  rep(i, 0, sz(stk) - 2) {
    ll l = stk[i] + 1;
    ll r = stk[i + 1] - 1;
    ex[l - 1] = (dd)a[l - 1];

    dd L = (dd)a[l - 1];
    dd R = (dd)a[(r + 1) % n];
    if (r < l)
      continue;
    if (l == r) {
      dd x = -b[l] + .5 * (L + R);
      ex[l] = x;
      continue;
    }
    ll m = r - l + 1;
    dd xr = L + m * R - 2. * cumib(l, r) + 2. * (l - 1) * cumb(l, r);
    xr /= m + 1;
    dd xl = L + R - 2. * cumb(l, r) - xr;
    ex[l] = xl;
    ex[r] = xr;
    dd x1, x2;
    x1 = xl;
    x2 = L;
    rep(i, l + 1, r - 1) {
      dd xi = 2. * x1 - x2 + 2. * b[i - 1];
      x2 = x1;
      x1 = xi;
      ex[i] = xi;
    }
  }

  dd ans = 0.;
  rep(i, 0, n - 1) { ans += ex[i]; }
  ans /= n;
  cout << ans << '\\n';
}

int main() {
#if 1
  solve();
#else
  ll t;
  cin >> t;
  rep(i, 0, t - 1) { solve(); }
#endif
  return 0;
}