Problem p02978 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PR pair<LL, LL>
const int N = 20;
LL a[N];
vector<PR> vp[N][N];
PR calpr(PR u, PR v, LL t) {
  LL num = u.sc + v.fr + t;
  return mp(u.fr + num, v.sc + num);
}
LL calval(PR t, LL u, LL v) { return t.fr * u + t.sc * v; }
void pus(int n, int l, int r, int u1, int v1, int u2, int v2) {
  PR m1, m2;
  LL val1 = 1e18, val2 = 1e18;
  int len = r - l + 1;
  for (int k = l; k <= r; k++) {
    int l1 = k - l, l2 = r - k;
    for (auto u : vp[l1][l])
      for (auto v : vp[l2][k + 1]) {
        PR t = calpr(u, v, a[k]);
        LL valt1 = calval(t, u1, v1);
        if (valt1 < val1)
          m1 = t, val1 = valt1;
        if (len > n - 2)
          continue;
        LL valt2 = calval(t, u2, v2);
        if (valt2 < val2)
          m2 = t, val2 = valt2;
      }
  }
  vp[len][l].pb(m1);
  if (len == n - 2)
    vp[len][l].pb(m2);
}
void work(int n) {
  pus(n, 1, n - 2, 1, 2, 1, 3);
  pus(n, 2, n - 1, 2, 3, 3, 2);
  pus(n, 3, n, 2, 1, 3, 1);

  pus(n, 1, n - 1, 1, 2, 0, 0);
  pus(n, 2, n, 2, 1, 0, 0);

  pus(n, 1, n, 1, 1, 0, 0);
}
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%lld", a + i);
  LL ans = a[0] + a[n - 1];
  n -= 2;

  for (int i = 1; i <= n; i++)
    vp[1][i].pb(mp(a[i], a[i])), vp[0][i].pb(mp(0, 0));
  vp[0][n + 1].pb(mp(0, 0));

  for (int p = 2; p <= n - 3; p++) {
    for (int i = 1; i + p - 1 <= n; i++) {
      int j = i + p - 1;
      for (int k = i; k <= j; k++) {
        int l1 = k - i, l2 = j - k;
        for (auto u : vp[l1][i])
          for (auto v : vp[l2][k + 1]) {
            vp[p][i].pb(calpr(u, v, a[k]));
          }
      }
    }
  }

  work(n);
  PR p = vp[n][1][0];
  ans += p.fr + p.sc;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PR pair<LL, LL>
const int N = 20;
LL a[N];
vector<PR> vp[N][N];
PR calpr(PR u, PR v, LL t) {
  LL num = u.sc + v.fr + t;
  return mp(u.fr + num, v.sc + num);
}
LL calval(PR t, LL u, LL v) { return t.fr * u + t.sc * v; }
void pus(int n, int l, int r, int u1, int v1, int u2, int v2) {
  if (l > r)
    return;
  PR m1, m2;
  LL val1 = 1e18, val2 = 1e18;
  int len = r - l + 1;
  for (int k = l; k <= r; k++) {
    int l1 = k - l, l2 = r - k;
    for (auto u : vp[l1][l])
      for (auto v : vp[l2][k + 1]) {
        PR t = calpr(u, v, a[k]);
        LL valt1 = calval(t, u1, v1);
        if (valt1 < val1)
          m1 = t, val1 = valt1;
        if (len > n - 2)
          continue;
        LL valt2 = calval(t, u2, v2);
        if (valt2 < val2)
          m2 = t, val2 = valt2;
      }
  }
  vp[len][l].pb(m1);
  if (len == n - 2)
    vp[len][l].pb(m2);
}
void work(int n) {
  pus(n, 1, n - 2, 1, 2, 1, 3);
  pus(n, 2, n - 1, 2, 3, 3, 2);
  pus(n, 3, n, 2, 1, 3, 1);

  pus(n, 1, n - 1, 1, 2, 0, 0);
  pus(n, 2, n, 2, 1, 0, 0);

  pus(n, 1, n, 1, 1, 0, 0);
}
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%lld", a + i);
  LL ans = a[0] + a[n - 1];
  n -= 2;

  for (int i = 1; i <= n; i++)
    vp[1][i].pb(mp(a[i], a[i])), vp[0][i].pb(mp(0, 0));
  vp[0][n + 1].pb(mp(0, 0));

  for (int p = 2; p <= n - 3; p++) {
    for (int i = 1; i + p - 1 <= n; i++) {
      int j = i + p - 1;
      for (int k = i; k <= j; k++) {
        int l1 = k - i, l2 = j - k;
        for (auto u : vp[l1][i])
          for (auto v : vp[l2][k + 1]) {
            vp[p][i].pb(calpr(u, v, a[k]));
          }
      }
    }
  }

  work(n);
  PR p = vp[n][1][0];
  ans += p.fr + p.sc;
  cout << ans << endl;

  return 0;
}