Problem p03198 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef long double lb;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

ll div_up(ll a, ll b) { return (a + b - 1) / b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, a[212345], dp1[1123][16], dp2[1123][16];

  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i <= 15; i++) {
    dp1[n - 1][i] = i;
    dp2[0][i] = i;
  }

  for (int i = n - 2; i >= 0; i--) {
    ll tmp = a[i];
    for (int j = 0; j <= 15; j++) {
      if (tmp <= a[i + 1])
        dp1[i][j] = j + dp1[i + 1][0];
      else {
        int x = ceil(log(div_up(tmp, a[i + 1])) / log(4));
        dp1[i][j] = j + dp1[i + 1][min(x, 15)];

        if (x >= 15) {
          dp1[i][j] += (x - 15) * (n - i - 1);
        }
      }

      tmp *= 4;
    }
  }

  for (int i = 1; i < n; i++) {
    ll tmp = a[i];
    for (int j = 0; j <= 15; j++) {
      if (tmp <= a[i - 1])
        dp2[i][j] = j + dp2[i - 1][0];
      else {
        int x = ceil(log(div_up(tmp, a[i - 1])) / log(4));
        dp2[i][j] = j + dp2[i - 1][min(x, 15)];

        if (x >= 15) {
          dp2[i][j] += (x - 15) * i;
        }
      }

      tmp *= 4;
    }
  }

  ll res = min(dp1[0][0] * 2, dp2[n - 1][0] * 2 + n);

  for (int i = 1; i < n; i++) {
    res = min(res, dp1[i][0] * 2 + dp2[i - 1][0] * 2 + i);
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef long double lb;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

ll div_up(ll a, ll b) { return (a + b - 1) / b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, a[212345], dp1[212345][16], dp2[212345][16];

  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i <= 15; i++) {
    dp1[n - 1][i] = i;
    dp2[0][i] = i;
  }

  for (int i = n - 2; i >= 0; i--) {
    ll tmp = a[i];
    for (int j = 0; j <= 15; j++) {
      if (tmp <= a[i + 1])
        dp1[i][j] = j + dp1[i + 1][0];
      else {
        int x = ceil(log(div_up(tmp, a[i + 1])) / log(4));
        dp1[i][j] = j + dp1[i + 1][min(x, 15)];

        if (x >= 15) {
          dp1[i][j] += (x - 15) * (n - i - 1);
        }
      }

      tmp *= 4;
    }
  }

  for (int i = 1; i < n; i++) {
    ll tmp = a[i];
    for (int j = 0; j <= 15; j++) {
      if (tmp <= a[i - 1])
        dp2[i][j] = j + dp2[i - 1][0];
      else {
        int x = ceil(log(div_up(tmp, a[i - 1])) / log(4));
        dp2[i][j] = j + dp2[i - 1][min(x, 15)];

        if (x >= 15) {
          dp2[i][j] += (x - 15) * i;
        }
      }

      tmp *= 4;
    }
  }

  ll res = min(dp1[0][0] * 2, dp2[n - 1][0] * 2 + n);

  for (int i = 1; i < n; i++) {
    res = min(res, dp1[i][0] * 2 + dp2[i - 1][0] * 2 + i);
  }

  cout << res << endl;

  return 0;
}