Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include<random>
#include<array>


using namespace std;

#define REP(i, n) for(int i = 0;i < n;++i)
#define REPR(i, n) for(int i = n-1;i >= 0;--i)
#define FOR(i, m, n) for(int i = m;i < n;++i)
#define FORR(i, m, n) for(int i = m-1;i >= n;--i)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout<<(x)<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define lb(v,n) lower_bound(v.begin(), v.end(), (n))
#define ub(v,n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i,v.size())cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int>P;
typedef array<int, 3> arr3;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 998244353; constexpr int MAX = 2000020;
const double pi = acos(-1); constexpr double EPS = 1e-8;
constexpr ll INF = 1e18;

int dp[200020][25];
int N;
ll A[200020];
bool done[200020][25];
int d[200020];
int calc(int i, int x) {
	//pe(i); print(x);
	if (x > 20) {
		return calc(i, 20) + (N - i)*(x - 20);
	}
	if (done[i][x])return dp[i][x];
	done[i][x] = true;
	if (i >= N - 1) {
		return dp[i][x]=x;
	}
	else {
		return dp[i][x]=x + calc(i + 1, max(0, x + d[i + 1]));
	}
}

ll B[200020];
int dB[200020];
int doneB[200020][25];
int dpB[200020][25];
int calcB(int i, int x) {
	if (x > 20) {
		return calcB(i, 20) + (N - i)*(x - 20);
	}
	if (doneB[i][x])return dpB[i][x];
	doneB[i][x] = true;
	if (i >= N - 1) {
		return dpB[i][x]=x;
	}
	else {
		return dpB[i][x]=x + calcB(i + 1, max(0, x + dB[i + 1]));
	}
}

void solve() {
	cin >> N;
	REP(i, N)cin >> A[i];
	REP(i, N)B[i] = A[N - i - 1];
	REP(i, N-1) {
		if (A[i + 1] < A[i] * 4) {
			int cnt = 0;
			ll tmp = A[i + 1];
			while (tmp < A[i]) {
				cnt++;
				tmp *= 4;
			}
			d[i + 1] = cnt;
		}
		else {
			ll tmp = A[i];
			int cnt = 0;
			while (tmp * 4 <= A[i + 1]) {
				cnt++;
				tmp *= 4;
			}
			d[i + 1] = -cnt;
		}

		if (B[i + 1] < B[i] * 4) {
			int cnt = 0;
			ll tmp = B[i + 1];
			while (tmp < B[i]) {
				cnt++;
				tmp *= 4;
			}
			dB[i + 1] = cnt;
		}
		else {
			ll tmp = B[i];
			int cnt = 0;
			while (tmp * 4 <= B[i + 1]) {
				cnt++;
				tmp *= 4;
			}
			dB[i + 1] = -cnt;
		}
	}
	ll ans = 1e18;
	REP(i, N) {
		ll res = 0;
		res += calc(i, 0) * 2;
		res += i;
		res += calcB(N - i, 0) * 2;
		//pe(i); print(res);
		ans = min(ans, res);
	}
	print(ans);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q; cin >> q;
	//while (q--)
	solve();
}



Compiler output:
a.cc: In function 'long long int calc(long long int, long long int)':
a.cc:77:52: error: no matching function for call to 'max(int, long long int)'
   77 |                 return dp[i][x]=x + calc(i + 1, max(0, x + d[i + 1]));
      |                                                 ~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:77:52: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   77 |                 return dp[i][x]=x + calc(i + 1, max(0, x + d[i + 1]));
      |                                                 ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:7:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:77:52: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   77 |                 return dp[i][x]=x + calc(i + 1, max(0, x + d[i + 1]));
      |                                                 ~~~^~~~~~~~~~~~~~~~~
a.cc: In function 'long long int calcB(long long int, long long int)':
a.cc:95:54: error: no matching function for call to 'max(int, long long int)'
   95 |                 return dpB[i][x]=x + calcB(i + 1, max(0, x + dB[i + 1]));
      |                                                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:95:54: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   95 |                 return dpB[i][x]=x + calcB(i + 1, max(0, x + dB[i + 1]));
      |                                                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:95:54: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   95 |                 return dpB[i][x]=x + calcB(i + 1, max(0, x + dB[i + 1]));
      |                                                   ~~~^~~~~~~~~~~~~~~~~~