Language: C++

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <functional>
#include <numeric>
#include <iostream>
#include <string>
#include <time.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

//
//int N, a[212345], b[212345];
//ll aa[212345], bb[212345];
//ll aaa[212345], bbb[212345];

ll la, lb;

int main() {

	ll N;

	scanf("%lld", &N);

	vector<ll> a(N), b(N);
	vector <ll> aa(N), bb(N);
	vector <ll> aaa(N), bbb(N);

	rep(i, N) scanf("%lld", &a[i]);

	rep(i, N) b[i] = a[i] * -2;

	la = N - 1;

	for (int i = N - 1; i >= 0; --i) {
		if (i == N - 1) aa[i] = 0;
		else {
			if (a[i] > a[i + 1]) {
				ll tmp = 4;
				for (int c = 1; ; ++c, tmp *= 4) {
					if (tmp >= a[i] / a[i + 1]) {
						aa[i] = aa[i + 1];
						for (int t = 1;; ++t) {
							if (i + t != la) {
								if (aaa[i + t] <= c) {
									aa[i] += aaa[i + t] * t * 2;
									c -= aaa[i + t];
									aaa[i + t] = 0;
								}
								else {
									aa[i] += c * t * 2;
									aaa[i + t] -= c;
									c = 0;
								}
							}
							else break;
						}
						aa[i] += 2 * c * (N - 1 - i);
						break;
					}
				}
			}
			else {
				ll tmp = 4;
				for (int c = 0; ; ++c, tmp *= 4) {
					if (tmp > a[i + 1] / a[i]) {
						aaa[i] = c;
						break;
					}
				}
				aa[i] = aa[i + 1];
			}
			while (la != i) {
				if (!aaa[la - 1]) --la;
				else break;
			}
		}
	}

	lb = 0;

	for (int i = 0; i < N; ++i) {
		if (i == 0) bb[i] = 1;
		else {
			if (b[i - 1] > b[i]) {
				ll tmp = 4;
				for (int c = 1; ; ++c, tmp *= 4) {
					if (tmp >= b[i] / b[i - 1]) {
						bb[i] = bb[i - 1] + 1;
						for (int t = 1;; ++t) {
							if (i - t != lb) {
								if (bbb[i - t] <= c) {
									bb[i] += bbb[i - t] * t * 2;
									c -= bbb[i - t];
									bbb[i - t] = 0;
								}
								else {
									bb[i] += c * t * 2;
									bbb[i - t] -= c;
									c = 0;
								}
							}
							else break;
						}
						bb[i] += 2 * c * i;
						break;
					}
				}
			}
			else {
				ll tmp = 4;
				for (int c = 0; ; ++c, tmp *= 4) {
					if (tmp > b[i - 1] / b[i]) {
						bbb[i] = c;
						break;
					}
				}
				bb[i] = bb[i - 1] + 1;
			}
			while (lb != i) {
				if (!bbb[lb + 1]) ++lb;
				else break;
			}
		}
	}

	/*

	for (int i = 0; i < N; ++i) {
		if (i == 0) bb[i] = 1;
		else {
			if (b[i - 1] > b[i]) {
				ll tmp = 4;
				for (int c = 1; ; ++c, tmp *= 4) {
					if (tmp >= b[i] / b[i - 1]) {
						bb[i] = bb[i - 1] + 1 + 2 * c * i;
						break;
					}
				}
			}
			else {
				bb[i] = bb[i - 1] + 1;
			}
		}
	}*/

	ll ans = LLONG_MAX;

	ans = min(ans, aa[0]);
	ans = min(ans, bb[N - 1]);

	rep(i, N - 1) {
		ans = min(ans, bb[i] + aa[i + 1]);
	}

	printf("%lld", ans);

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:159:18: error: 'LLONG_MAX' was not declared in this scope
  159 |         ll ans = LLONG_MAX;
      |                  ^~~~~~~~~
a.cc:17:1: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   16 | #include <iostream>
  +++ |+#include <climits>
   17 | #include <string>