Problem p02673 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int n;
Real a[440000];
Real b[440000];
Real e[440000];
Real c[440000];

Real bi_sum[440000];
Real b_sum[440000];

vector<P> highs;
set<int> done;

int main() {
  cin >> n;
  int mxa = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i];
    if (a[mxa] < a[i])
      mxa = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i + n] = b[i];
  }
  e[mxa] = e[mxa + n] = a[mxa];
  for (int i = 1; i < n; i++) {
    c[mxa + i + 1] = -2 * b[mxa + i] + 2 * c[mxa + i] - c[mxa + i - 1];
    //        cout << c[mxa + i + 1] << endl;
  }

  using RP = pair<Real, Real>;
  vector<RP> ch = {{mxa, a[mxa] + c[mxa]}};

  for (int i = 1; i <= n; i++) {
    RP p = {mxa + i, a[mxa + i] + c[mxa + i]};
    while (ch.size() >= 2) {
      RP bb = ch[ch.size() - 2];
      RP b = ch.back();
      bb.first -= p.first;
      bb.second -= p.second;
      b.first -= p.first;
      b.second -= p.second;
      if (b.first * bb.second - b.second * bb.first <= 0)
        ch.pop_back();
      else
        break;
    }
    ch.push_back(p);
  }
  for (auto p : ch) {
    //        cout << p.first << " " << p.second << endl;
  }
  Real ans = a[mxa];
  for (int i = 1; i < n; i++) {
    int ind = mxa + i;
    auto r = lower_bound(ch.begin(), ch.end(), RP(ind, -INF));
    auto l = prev(r);
    auto l_dist = ind - l->first;
    auto r_dist = r->first - ind;
    //        cout << ind << " "  << (l_dist * r->second + r_dist * l->second)
    //        /(l_dist + r_dist) - c[ind] << endl;
    ans +=
        (l_dist * r->second + r_dist * l->second) / (l_dist + r_dist) - c[ind];
  }
  printf("%.10lf\\n", double(ans / n));

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

int n3[13];
int n;

int last[13][220000];
int s_cnt[550000];
int significant[550000];
int ans[13][550000];

int main() {
  n3[0] = 1;
  for (int i = 1; i < 13; i++)
    n3[i] = n3[i - 1] * 3;
  cin >> n;
  string t;
  cin >> t;

  for (int i = 1; i <= n; i++) {
    int sls = 0;
    for (int j = 0; j < n3[i - 1]; j++) {
      ans[i][j + n3[i - 1]] = ans[i - 1][j];
      ans[i][j + 2 * n3[i - 1]] = ans[i - 1][j];
      significant[j] = 0;
      significant[j + n3[i - 1]] = 1;
      significant[j + n3[i - 1]] = 2;
      s_cnt[j] = 0;
    }
    for (int j = 0; j < t.size(); j++) {
      if (t[j] == 'S')
        sls++;
      else {
      }
      last[i][j] = last[i - 1][j] + significant[last[i - 1][j]] * n3[i - 1];
    }
  }
  return 0;
}