Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
ll mod = 1e9 + 7;

ll a[200005];
ll b[200005];

ll idx[100005][2];
// ll cnt[200005];
ll pre[200005];
// ll end[200005];
map<ll, ll> m;

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi - a.se) > (b.fi - b.se);
}

bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi) > (b.fi);
}

ll ceil11(ll a, ll b) {
  if (a % b != 0)
    return a / b + 1;
  else
    return a / b;
}
ll power(ll a, ll b, ll n) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % n;
    a = (a * a) % n;
    b /= 2;
  }
  return ans;
}

ll count1 = 0;

ll gcd(ll a, ll b) {
  // if(a<b)swap(a,b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll primepos(ll fact, ll p) {
  ll res = 0;
  while (fact > 0) {
    res += fact / p;
    fact /= p;
  }

  return res;
}

// void dfs(ll n, ll p){
//   if(vis[n]!=0){
//     return;
//   }
//   else{
//     vis[n]=1;
//     b[n]=p;
//     for(int i=0;i<v[n].size();i++){
//       dfs(v[n][i],p);
//     }
//   }
ll ceil1(ll a, ll b) { return a / b; }

int main() {

  ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ll n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long int dp[n][n], temp;
  for (int i = 0; i < n - 1; i++) {
    dp[i][i + 1] = a[i] + a[i + 1];
    dp[i][i] = 0;
  }
  dp[n - 1][n - 1] = 0;
  long long int sm[n][n];
  for (int i = 0; i < n; i++) {
    sm[i][i] = a[i];
    for (int j = i + 1; j < n; j++) {
      sm[i][j] = sm[i][j - 1] + a[j];
    }
  }
  for (int i = 2; i < n; i++) {
    for (int j = 0; j + i < n; j++) {
      temp = LONG_MAX;
      for (int k = 0; k < i; k++) {
        temp = min(temp, dp[j][j + k] + dp[j + k + 1][j + i]);
      }
      dp[j][j + i] = temp + sm[j][j + i];
    }
  }
  cout << dp[0][n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
ll mod = 1e9 + 7;

ll a[200005];
ll b[200005];

ll idx[100005][2];
// ll cnt[200005];
ll pre[200005];
// ll end[200005];
map<ll, ll> m;

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi - a.se) > (b.fi - b.se);
}

bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi) > (b.fi);
}

ll ceil11(ll a, ll b) {
  if (a % b != 0)
    return a / b + 1;
  else
    return a / b;
}
ll power(ll a, ll b, ll n) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % n;
    a = (a * a) % n;
    b /= 2;
  }
  return ans;
}

ll count1 = 0;

ll gcd(ll a, ll b) {
  // if(a<b)swap(a,b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll primepos(ll fact, ll p) {
  ll res = 0;
  while (fact > 0) {
    res += fact / p;
    fact /= p;
  }

  return res;
}

// void dfs(ll n, ll p){
//   if(vis[n]!=0){
//     return;
//   }
//   else{
//     vis[n]=1;
//     b[n]=p;
//     for(int i=0;i<v[n].size();i++){
//       dfs(v[n][i],p);
//     }
//   }
ll ceil1(ll a, ll b) { return a / b; }

int main() {

  ios::sync_with_stdio(0);
  ll n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long int dp[n][n], temp;
  for (int i = 0; i < n - 1; i++) {
    dp[i][i + 1] = a[i] + a[i + 1];
    dp[i][i] = 0;
  }
  dp[n - 1][n - 1] = 0;
  long long int sm[n][n];
  for (int i = 0; i < n; i++) {
    sm[i][i] = a[i];
    for (int j = i + 1; j < n; j++) {
      sm[i][j] = sm[i][j - 1] + a[j];
    }
  }
  for (int i = 2; i < n; i++) {
    for (int j = 0; j + i < n; j++) {
      temp = LONG_MAX;
      for (int k = 0; k < i; k++) {
        temp = min(temp, dp[j][j + k] + dp[j + k + 1][j + i]);
      }
      dp[j][j + i] = temp + sm[j][j + i];
    }
  }
  cout << dp[0][n - 1];

  return 0;
}