Problem p02978 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N;
ll A[20];
vector<l_l> dp[20][20];

void f(int start, int end) {
  vector<l_l> tmp;
  for (int eat = start; eat < end; eat++) {
    for (int i = 0; i < dp[start][eat].size(); i++) {
      for (int j = 0; j < dp[eat + 1][end].size(); j++) {
        ll val1 = dp[start][eat][i].first;
        val1 += dp[start][eat][i].second;
        val1 += dp[eat + 1][end][j].first;
        val1 += A[eat];
        ll val2 = dp[start][eat][i].second;
        val2 += dp[eat + 1][end][j].first;
        val2 += dp[eat + 1][end][j].second;
        val2 += A[eat];
        tmp.emplace_back(val1, val2);
      }
    }
  }
  sort(tmp.begin(), tmp.end());
  dp[start][end].push_back(tmp[0]);
  ll minimum = tmp[0].second;
  for (int i = 1; i < tmp.size(); i++) {
    // if(chmin(minimum, tmp[i].second)) {
    dp[start][end].push_back(tmp[i]);
    //}
  }
  /*
  cerr << "----" << start << "----" << end << "----" << endl;
  for(int i = 0; i < dp[start][end].size(); i++) {
      cerr << dp[start][end][i].first << " " << dp[start][end][i].second <<
  endl;
  }
  */
}

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= N; i++)
    dp[i][i].push_back({0, 0});
  for (int delta = 1; delta <= N - 2; delta++) {
    for (int start = 2; start + delta <= N; start++) {
      f(start, start + delta);
    }
  }
  ll ans = LONG_MAX;
  for (int i = 0; i < dp[2][N].size(); i++) {
    chmin(ans, A[1] + A[N] + dp[2][N][i].first + dp[2][N][i].second);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N;
ll A[20];
vector<l_l> dp[20][20];

void f(int start, int end) {
  vector<l_l> tmp;
  for (int eat = start; eat < end; eat++) {
    for (int i = 0; i < dp[start][eat].size(); i++) {
      for (int j = 0; j < dp[eat + 1][end].size(); j++) {
        ll val1 = dp[start][eat][i].first;
        val1 += dp[start][eat][i].second;
        val1 += dp[eat + 1][end][j].first;
        val1 += A[eat];
        ll val2 = dp[start][eat][i].second;
        val2 += dp[eat + 1][end][j].first;
        val2 += dp[eat + 1][end][j].second;
        val2 += A[eat];
        tmp.emplace_back(val1, val2);
      }
    }
  }
  sort(tmp.begin(), tmp.end());
  dp[start][end].push_back(tmp[0]);
  ll minimum = tmp[0].second;
  for (int i = 1; i < tmp.size(); i++) {
    if (chmin(minimum, tmp[i].second)) {
      dp[start][end].push_back(tmp[i]);
    }
  }
  /*
  cerr << "----" << start << "----" << end << "----" << endl;
  for(int i = 0; i < dp[start][end].size(); i++) {
      cerr << dp[start][end][i].first << " " << dp[start][end][i].second <<
  endl;
  }
  */
}

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= N; i++)
    dp[i][i].push_back({0, 0});
  for (int delta = 1; delta <= N - 2; delta++) {
    for (int start = 2; start + delta <= N; start++) {
      f(start, start + delta);
    }
  }
  ll ans = LONG_MAX;
  for (int i = 0; i < dp[2][N].size(); i++) {
    chmin(ans, A[1] + A[N] + dp[2][N][i].first + dp[2][N][i].second);
  }
  cout << ans << endl;
  return 0;
}