Language: C++

#include <bits/stdc++.h>
using namespace std;
struct Seg {
  Seg *ch[2];
  long long minValue, sumValue;
  long long addTag;
  int l, r, m;
  static constexpr long long INF = 1<<60;
  void pull() {
    minValue = min(ch[0]->minValue, ch[1]->minValue);
    sumValue = ch[0]->sumValue + ch[1]->sumValue;
  }
  void len() {
    return r - l;
  }
  void push() {
    if (addTag) {
      for (int i = 0; i < 2; ++i) {
        ch[i]->addTag += addTag;
        ch[i]->sumValue += addTag * ch[i]->len();
        ch[i]->minValue += addTag;
      }
      addTag = 0;
    }
  }
  Seg(int l, int r) : l(l), r(r), m(l + r >> 1) {
    minValue = sumValue = 0;
    addTag = 0;
    if (r - l > 1) {
      ch[0] = new Seg(l, m);
      ch[1] = new Seg(m, r);
    }
  }
  void addValue(int ql, int qr, long long qv) {
    if (ql <= l and r <= qr) addTag += qv, minValue += qv, sumValue += qv;
    else {
      push();
      if (ql < m) ch[0]->addValue(ql, qr, qv);
      if (m < qr) ch[1]->addValue(ql, qr, qv);
      pull();
    }
  }
  long long getMinValue(int ql, int qr) {
    if (ql <= l and r <= qr) return minValue;
    else {
      push();
      long long ans = INF;
      if (ql < m) ans = min(ans, ch[0]->getMinValue());
      if (m < qr) ans = min(ans, ch[1]->getMinValue());
      pull();
      return ans;
    }
  }
  long long getSumValue(int ql, int qr) {
    if (ql <= l and r <= qr) return sumValue;
    else {
      push();
      long long ans = 0;
      if (ql < m) ans = min(ans, ch[0]->getSumValue());
      if (m < qr) ans = min(ans, ch[1]->getSumValue());
      pull();
      return ans;
    }
  }
};
class Solution {
  int n;
  vector<long long> A;
  int climb(long long a, long long b) {
    if (b == a) return 0;
    assert(b <= a);
    int ans = 0;
    while (b < a) return b *= 4, ++ans;
    return ans;
  }
  int reach(long long a, long long b) {
    if (b == a) return 0;
    assert(b <= a);
    int ans = 0;
    while (b * 4 <= a) return b *= 4, ++ans;
    return ans;
  }
  long long test(int idx) {
    long long ans = idx != 0;
    for (int i = idx + 1, col = 0; i < n; ++i) {
      if (A[i - 1] >= A[i]) {
        col += 2 * climb(A[i - 1], A[i]);
        ans += col;
      }
      else {
        col = max(0, col - 2 * reach(A[i], A[i - 1]));
        ans += col;
      }
    }
    for (int i = idx - 2, col = 0; i >= 0; --i) {
      ++ans;
      if (A[i - 1] >= A[i]) {
        col = max(0, col - 2 * climb(A[i - 1], A[i]));
      }
      else {
        col += 2 * reach(A[i], A[i - 1]);
      }
      ans += col;
    }
    return ans;
  }
public:
  void run(istream &in, ostream &out) {
    in >> n;
    A.resize(n);
    for (int i = 0; i < n; ++i) in >> A[i];
    long long ans = 1ll<<60;
    for (int i = 0; i < n; ++i) {
      ans = min(ans, test(i));
    }
    out << ans << '\n';
  }
  
  
};
int main() {
  ios_base::sync_with_stdio(false);
  Solution().run(cin, cout);
  return 0;
}


Compiler output:
a.cc:8:37: warning: left shift count >= width of type [-Wshift-count-overflow]
    8 |   static constexpr long long INF = 1<<60;
      |                                    ~^~~~
a.cc:8:37: error: right operand of shift expression '(1 << 60)' is greater than or equal to the precision 32 of the left operand [-fpermissive]
a.cc: In member function 'void Seg::len()':
a.cc:14:14: error: return-statement with a value, in function returning 'void' [-fpermissive]
   14 |     return r - l;
      |            ~~^~~
a.cc: In member function 'void Seg::push()':
a.cc:20:35: error: invalid operands of types 'long long int' and 'void' to binary 'operator*'
   20 |         ch[i]->sumValue += addTag * ch[i]->len();
      |                            ~~~~~~ ^ ~~~~~~~~~~~~
      |                            |                  |
      |                            long long int      void
a.cc: In member function 'long long int Seg::getMinValue(int, int)':
a.cc:48:52: error: no matching function for call to 'Seg::getMinValue()'
   48 |       if (ql < m) ans = min(ans, ch[0]->getMinValue());
      |                                  ~~~~~~~~~~~~~~~~~~^~
a.cc:43:13: note: candidate: 'long long int Seg::getMinValue(int, int)'
   43 |   long long getMinValue(int ql, int qr) {
      |             ^~~~~~~~~~~
a.cc:43:13: note:   candidate expects 2 arguments, 0 provided
a.cc:49:52: error: no matching function for call to 'Seg::getMinValue()'
   49 |       if (m < qr) ans = min(ans, ch[1]->getMinValue());
      |                                  ~~~~~~~~~~~~~~~~~~^~
a.cc:43:13: note: candidate: 'long long int Seg::getMinValue(int, int)'
   43 |   long long getMinValue(int ql, int qr) {
      |             ^~~~~~~~~~~
a.cc:43:13: note:   candidate expects 2 arguments, 0 provided
a.cc: In member function 'long long int Seg::getSumValue(int, int)':
a.cc:59:52: error: no matching function for call to 'Seg::getSumValue()'
   59 |       if (ql < m) ans = min(ans, ch[0]->getSumValue());
      |                                  ~~~~~~~~~~~~~~~~~~^~
a.cc:54:13: note: candidate: 'long long int Seg::getSumValue(int, int)'
   54 |   long long getSumValue(int ql, int qr) {
      |             ^~~~~~~~~~~
a.cc:54:13: note:   candidate expects 2 arguments, 0 provided
a.cc:60:52: error: no matching function for call to 'Seg::getSumValue()'
   60 |       if (m < qr) ans = min(ans, ch[1]->getSumValue());
      |                                  ~~~~~~~~~~~~~~~~~~^~
a.cc:54:13: note: candidate: 'long long int Seg::getSumValue(int, int)'
   54 |   long long getSumValue(int ql, int qr) {
      |             ^~~~~~~~~~~
a.cc:54:13: note:   candidate expects 2 arguments, 0 provided