Problem p03173 (C++)

Buggy submission (status: Runtime Error):
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define ld long double
#define beg(i, n) for (ll i = 0; i < n; i++)
#define beg1(i, n) for (ll i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second
#define inf 1e18 + 10

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 1e9 + 7;
const ll MAX = 1e5 + 7;

//---------------------------------------------------TEMPLATES-------------------------------------------------------

void add_self(ll &a, ll b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(ll &a, ll b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

void merge_self(vector<ll> &a, vector<ll> b) {
  ll s = a.size();
  beg(i, s) a[i] += b[i];
}

void set_self(vector<ll> &a, ll n, ll s) { beg(i, s) a[i] = n; }

ll factors() {}

//---------------------------------------------------FUNCTIONS-------------------------------------------------------

ll dp[401][401], sum[401], ar[401];

void rec(ll s, ll e) {
  if (s == e) {
    dp[s][e] = 0;
    return;
  }
  if (dp[s][e] != -1) {
    return;
  }
  dp[s][e] = inf;
  for (int i = s; i <= e; i++) {
    rec(s, i);
    rec(i + 1, e);
    dp[s][e] = min(dp[s][e], dp[s][i] + dp[i + 1][e] + sum[e] - sum[s - 1]);
    // cout << s << "  " << e << "  " << sum[e] - sum[s-1] << nl ;
  }
  return;
}

void solve() {
  memset(dp, -1, sizeof(dp));
  ll n;
  cin >> n;
  beg1(i, n + 1) cin >> ar[i];
  sum[0] = 0;
  beg1(i, n + 1) sum[i] = sum[i - 1] + ar[i];
  rec(1, n);
  cout << dp[1][n];
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define ld long double
#define beg(i, n) for (ll i = 0; i < n; i++)
#define beg1(i, n) for (ll i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second
#define inf 1e18 + 10

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 1e9 + 7;
const ll MAX = 1e5 + 7;

//---------------------------------------------------TEMPLATES-------------------------------------------------------

void add_self(ll &a, ll b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(ll &a, ll b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

void merge_self(vector<ll> &a, vector<ll> b) {
  ll s = a.size();
  beg(i, s) a[i] += b[i];
}

void set_self(vector<ll> &a, ll n, ll s) { beg(i, s) a[i] = n; }

ll factors() {}

//---------------------------------------------------FUNCTIONS-------------------------------------------------------

ll dp[401][401], sum[401], ar[401];

void rec(ll s, ll e) {
  if (s == e) {
    dp[s][e] = 0;
    return;
  }
  if (dp[s][e] != -1) {
    return;
  }
  dp[s][e] = inf;
  for (int i = s; i < e; i++) {
    rec(s, i);
    rec(i + 1, e);
    dp[s][e] = min(dp[s][e], dp[s][i] + dp[i + 1][e] + sum[e] - sum[s - 1]);
    // cout << s << "  " << e << "  " << sum[e] - sum[s-1] << nl ;
  }
  return;
}

void solve() {
  memset(dp, -1, sizeof(dp));
  ll n;
  cin >> n;
  beg1(i, n + 1) cin >> ar[i];
  sum[0] = 0;
  beg1(i, n + 1) sum[i] = sum[i - 1] + ar[i];
  rec(1, n);
  cout << dp[1][n];
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}