Problem p02978 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

int n;

int main() {
  cin >> n;
  n -= 2;
  long long res;
  cin >> res;
  vector<int> a(n);
  range(i, 0, n) cin >> a[i];
  int foo;
  cin >> foo;
  res += foo;
  vector<pair<long long, long long>> cur;
  vector<pair<int, int>> to((1 << (n + 1)) - 1, make_pair(-1, -1));
  function<void(int, int, int)> dfs = [&](int u, int v, int dep) {
    cur.emplace_back(u, v);
    if (dep == n)
      return;
    int pos = cur.size() - 1;
    to[pos].first = cur.size();
    dfs(u, u + v, dep + 1);
    to[pos].second = cur.size();
    dfs(u + v, v, dep + 1);
  };
  dfs(1, 1, 0);
  vector<vector<vector<long long>>> dp(
      n, vector<vector<long long>>(
             n, vector<long long>((1 << (n + 1)) - 1, LLONG_MAX / 3)));
  range(s, 0, n) {
    range(x, 0, n - s) {
      int y = x + s;
      if (x == y) {
        range(z, 0, (1 << (n + 1)) - 1) dp[x][y][z] =
            cur[z].first * a[x] + cur[z].second * a[y];
        continue;
      }
      range(z, 0, (1 << (n + 1)) - 1) {
        int u = to[z].first, v = to[z].second;
        if (u == -1)
          continue;
        range(d, x, y + 1) {
          long long l = (d == x ? 0 : dp[x][d - 1][u]),
                    r = (d == y ? 0 : dp[d + 1][y][v]);
          dp[x][y][z] =
              min(dp[x][y][z], l + r + (cur[z].first + cur[z].second) * a[d]);
        }
      }
    }
  }
  res += dp[0][n - 1][0];
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

int n;

int main() {
  cin >> n;
  n -= 2;
  long long res;
  cin >> res;
  vector<int> a(n);
  range(i, 0, n) cin >> a[i];
  int foo;
  cin >> foo;
  res += foo;
  if (n == 0) {
    cout << res;
    return 0;
  }
  vector<pair<long long, long long>> cur;
  vector<pair<int, int>> to((1 << (n + 1)) - 1, make_pair(-1, -1));
  function<void(int, int, int)> dfs = [&](int u, int v, int dep) {
    cur.emplace_back(u, v);
    if (dep == n)
      return;
    int pos = cur.size() - 1;
    to[pos].first = cur.size();
    dfs(u, u + v, dep + 1);
    to[pos].second = cur.size();
    dfs(u + v, v, dep + 1);
  };
  dfs(1, 1, 0);
  vector<vector<vector<long long>>> dp(
      n, vector<vector<long long>>(
             n, vector<long long>((1 << (n + 1)) - 1, LLONG_MAX / 3)));
  range(s, 0, n) {
    range(x, 0, n - s) {
      int y = x + s;
      if (x == y) {
        range(z, 0, (1 << (n + 1)) - 1) dp[x][y][z] =
            cur[z].first * a[x] + cur[z].second * a[y];
        continue;
      }
      range(z, 0, (1 << (n + 1)) - 1) {
        int u = to[z].first, v = to[z].second;
        if (u == -1)
          continue;
        range(d, x, y + 1) {
          long long l = (d == x ? 0 : dp[x][d - 1][u]),
                    r = (d == y ? 0 : dp[d + 1][y][v]);
          dp[x][y][z] =
              min(dp[x][y][z], l + r + (cur[z].first + cur[z].second) * a[d]);
        }
      }
    }
  }
  res += dp[0][n - 1][0];
  cout << res;
  return 0;
}