Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>

#define NIL -(long long)(1e9)
#define ll long long
#define satir cout << "\n"
#define ALL(v) (v).begin(), (v).end()
#define f first
#define s second
#define sps << " " <<
#define sp << " "
#define spl cout << "\n";
#define INF 0x3f3f3f3f3f3f3f3f
#define cntout                                                                 \
  cout << " " << rndcntbo;                                                     \
  ++rndcntbo;
const ll N = 1 * 1000 + 7;
ll n, n1, n2, n3, k, t, d1, d2, m, ans = -1, rndcntbo = 190;
ll arr[N];
ll dp[N][N];
ll pref[N];
void max_self(int &a, int b) {
  a = min(a, b);
  return;
}
ll dpa(int l, int r) {
  // cout<<l sps r<<" aadsd ";spl;
  if (l == r)
    return 0; // dp[l][r]=arr[l];
  // cout<<"test1";spl;
  // if(abs(l-r)==1) return (dp[l][r]=arr[l]+arr[r]);
  if (dp[l][r] != -1)
    return dp[l][r];
  // cout<<"test2";spl;

  ll nax = INF;

  for (int i = l; i < r; ++i) {
    // cout<<dpa(l,i)+dpa(i+1,r);spl;
    // cout<<"myl-myr"<<l sps r <<"calling"<<l sps i;spl;
    nax = min(nax, dpa(l, i) + dpa(i + 1, r) + pref[i] - pref[l - 1] + pref[r] -
                       pref[i]);
  }
  dp[l][r] = nax;
  // cout<<nax;spl;
  return nax;
}
inline void coz() {
  cin >> n;
  pref[0] = 0;
  for (ll i = 1; i <= n; ++i) {
    cin >> arr[i];
    // dp[i][i]=arr[i];
    pref[i] = pref[i - 1] + arr[i];
  }
  // cout<<"asd"<<n;spl;
  cout << dpa(1, n);
  // cout<<INF;
  // cout<<"bittiaq";spl;

  // cout<<dp[1][n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(dp, -1, sizeof(dp));

#ifndef ONLINE_JUDGE
  freopen("factory.in", "r", stdin);
  freopen("factory.out", "w", stdout);
#endif
  // cin>>t;
  // while(t--)
  coz();

  // cout<<ans<<" ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>

#define NIL -(long long)(1e9)
#define ll long long
#define satir cout << "\n"
#define ALL(v) (v).begin(), (v).end()
#define f first
#define s second
#define sps << " " <<
#define sp << " "
#define spl cout << "\n";
#define INF 0x3f3f3f3f3f3f3f3f
#define cntout                                                                 \
  cout << " " << rndcntbo;                                                     \
  ++rndcntbo;
const ll N = 1 * 1000 + 7;
ll n, n1, n2, n3, k, t, d1, d2, m, ans = -1, rndcntbo = 190;
ll arr[N];
ll dp[N][N];
ll pref[N];
void max_self(int &a, int b) {
  a = min(a, b);
  return;
}
ll dpa(int l, int r) {
  // cout<<l sps r<<" aadsd ";spl;
  if (l == r)
    return 0; // dp[l][r]=arr[l];
  // cout<<"test1";spl;
  // if(abs(l-r)==1) return (dp[l][r]=arr[l]+arr[r]);
  if (dp[l][r] != -1)
    return dp[l][r];
  // cout<<"test2";spl;

  ll nax = INF;

  for (int i = l; i < r; ++i) {
    // cout<<dpa(l,i)+dpa(i+1,r);spl;
    // cout<<"myl-myr"<<l sps r <<"calling"<<l sps i;spl;
    nax = min(nax, dpa(l, i) + dpa(i + 1, r) + pref[i] - pref[l - 1] + pref[r] -
                       pref[i]);
  }
  dp[l][r] = nax;
  // cout<<nax;spl;
  return nax;
}
inline void coz() {
  cin >> n;
  pref[0] = 0;
  for (ll i = 1; i <= n; ++i) {
    cin >> arr[i];
    // dp[i][i]=arr[i];
    pref[i] = pref[i - 1] + arr[i];
  }
  // cout<<"asd"<<n;spl;
  cout << dpa(1, n);
  // cout<<INF;
  // cout<<"bittiaq";spl;

  // cout<<dp[1][n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(dp, -1, sizeof(dp));

  // #ifndef ONLINE_JUDGE
  // 	freopen("factory.in","r",stdin);  freopen("factory.out","w",stdout);
  // #endif
  // cin>>t;
  // while(t--)
  coz();

  // cout<<ans<<" ";
  return 0;
}