Problem p03198 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int read() {
  char ch = getchar();
  int f = 0, x = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      x = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    f = (f << 1) + (f << 3) + ch - '0';
    ch = getchar();
  }
  return f * x;
}
ll f[200005][19], g[200005][19];
int n;
ll a[200005], pow1[17];
const ll inf = 1000000000000000000LL;
int main() {
  freopen("1.in", "r", stdin);
  memset(f, 0x3f3f3f3f, sizeof(f));
  memset(g, 0x3f3f3f3f, sizeof(g));
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= 15; j++)
      f[i][j] = g[i][j] = inf;
  }
  n = read();
  pow1[0] = 1;
  for (int i = 1; i <= 15; i++)
    pow1[i] = pow1[i - 1] * 4;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (int i = 0; i <= 15; i++)
    f[1][i] = i;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= 15; j++) {
      for (int k = 15; k >= 0; k--) {
        if (a[i - 1] * pow1[k] >= a[i] * pow1[j]) {
          f[i][j] = min(f[i][j], f[i - 1][k] + j);
          // cout<<k<<endl;
        }
        // else break;
      }
      if (f[i][j] == 4557430888798830399LL)
        f[i][j] = f[i][j - 1] + i;
      // cout<<f[i][j]<<' ';
    }
    // cout<<endl;
  }
  for (int i = 0; i <= 15; i++)
    g[n][i] = i;
  for (int i = n - 1; i; i--) {
    for (int j = 0; j <= 15; j++) {
      for (int k = 15; k >= 0; k--) {
        if (a[i + 1] * pow1[k] >= a[i] * pow1[j]) {
          g[i][j] = min(g[i][j], g[i + 1][k] + j);
        }
        // else break;
      }
      if (g[i][j] == 4557430888798830399LL)
        g[i][j] = g[i][j - 1] + (n - i + 1);
    }
  }
  ll ans = inf;
  f[0][0] = g[0][0] = f[n + 1][0] = g[n + 1][0] = 0;
  for (int i = 0; i <= n; i++) {
    // cout<<f[i][0]<<" "<<g[i][0]<<endl;
    ll num1, num2;
    num1 = num2 = inf;
    for (int j = 0; j <= 15; j++) {
      num1 = min(num1, f[i][j]);
      num2 = min(num2, g[i + 1][j]);
    }
    ans = min(ans, 1LL * num1 * 2 + 1LL * num2 * 2 + 1LL * i);
    // cout<<i<<" "<<num1<<" "<<num2<<endl;
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int read() {
  char ch = getchar();
  int f = 0, x = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      x = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    f = (f << 1) + (f << 3) + ch - '0';
    ch = getchar();
  }
  return f * x;
}
ll f[200005][19], g[200005][19];
int n;
ll a[200005], pow1[17];
const ll inf = 1000000000000000000LL;
int main() {
  // freopen("1.in","r",stdin);
  memset(f, 0x3f3f3f3f, sizeof(f));
  memset(g, 0x3f3f3f3f, sizeof(g));
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= 15; j++)
      f[i][j] = g[i][j] = inf;
  }
  n = read();
  pow1[0] = 1;
  for (int i = 1; i <= 15; i++)
    pow1[i] = pow1[i - 1] * 4;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (int i = 0; i <= 15; i++)
    f[1][i] = i;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= 15; j++) {
      for (int k = 15; k >= 0; k--) {
        if (a[i - 1] * pow1[k] >= a[i] * pow1[j]) {
          f[i][j] = min(f[i][j], f[i - 1][k] + j);
          // cout<<k<<endl;
        }
        // else break;
      }
      if (f[i][j] == 4557430888798830399LL)
        f[i][j] = f[i][j - 1] + i;
      // cout<<f[i][j]<<' ';
    }
    // cout<<endl;
  }
  for (int i = 0; i <= 15; i++)
    g[n][i] = i;
  for (int i = n - 1; i; i--) {
    for (int j = 0; j <= 15; j++) {
      for (int k = 15; k >= 0; k--) {
        if (a[i + 1] * pow1[k] >= a[i] * pow1[j]) {
          g[i][j] = min(g[i][j], g[i + 1][k] + j);
        }
        // else break;
      }
      if (g[i][j] == 4557430888798830399LL)
        g[i][j] = g[i][j - 1] + (n - i + 1);
    }
  }
  ll ans = inf;
  f[0][0] = g[0][0] = f[n + 1][0] = g[n + 1][0] = 0;
  for (int i = 0; i <= n; i++) {
    // cout<<f[i][0]<<" "<<g[i][0]<<endl;
    ll num1, num2;
    num1 = num2 = inf;
    for (int j = 0; j <= 15; j++) {
      num1 = min(num1, f[i][j]);
      num2 = min(num2, g[i + 1][j]);
    }
    ans = min(ans, 1LL * num1 * 2 + 1LL * num2 * 2 + 1LL * i);
    // cout<<i<<" "<<num1<<" "<<num2<<endl;
  }
  cout << ans;
}