Problem p03173 (C++)

Buggy submission (status: Runtime Error):
/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE
PROHIBITED   **********************************
*/

/*#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/

#include <bits/stdc++.h>
#define int long long
#define double long double
#define pb emplace_back
#define pf emplace_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 998244353
#define PI 3.141592653589
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 2000000000000000000
#define M 1000000007
using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int power(int x, int n) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result % M;
}

int dp[405][405] = {0};
int tc[405][405] = {0};

signed main() {
  tezz

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  vi v(n);
  for (int &x : v)
    cin >> x;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      dp[i][j] = MAX, tc[i][j] = MAX;
    }

  for (int size = 0; size < n; size++) {
    for (int i = 0, j = size; j < n; i++, j++) {
      if (i == j) {
        dp[i][j] = v[i];
        tc[i][j] = 0;
      } else if (j - i == 1) {
        dp[i][j] = v[i] + v[j];
        tc[i][j] = v[i] + v[j];
      } else {
        for (int k = i; k < j; k++) {
          if (tc[i][j] > tc[i][k] + tc[k + 1][j] + dp[i][k] + dp[k + 1][j]) {
            dp[i][j] = dp[i][k] + dp[k + 1][j];
            tc[i][j] = tc[i][k] + tc[k + 1][j] + dp[i][j];
            // cout<<i<<' '<<k<<' '<<j<<" ------> "<<tc[i][j]<<endl;
          }

          // else cout<<i<<' '<<k<<' '<<j<<"____"<<tc[i][k] + tc[k+1][j] +
          // dp[i][k] + dp[k+1][j]<<endl;

          /*dp[i][j] = min(dp[i][j], tc[i][k] + tc[k+1][j] + dp[i][k] +
          dp[k+1][j]); tc[i][j] = dp[i][j];*/
        }
      }
    }
  }
  /*cout<<endl;

  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          if(dp[i][j] == MAX) cout<<0<<' ';
          else cout<<dp[i][j]<<' ';
      }
      cout<<endl;
  }
  cout<<endl;

  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          if(tc[i][j] == MAX) cout<<0<<' ';
          else cout<<tc[i][j]<<' ';
      }
      cout<<endl;
  }*/

  cout << tc[0][n - 1];
}

Fixed submission:
/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE
PROHIBITED   **********************************
*/

/*#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/

#include <bits/stdc++.h>
#define int long long
#define double long double
#define pb emplace_back
#define pf emplace_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 998244353
#define PI 3.141592653589
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 2000000000000000000
#define M 1000000007
using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int power(int x, int n) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result % M;
}

int dp[405][405] = {0};
int tc[405][405] = {0};

signed main() {
  tezz

      int n;
  cin >> n;
  vi v(n);
  for (int &x : v)
    cin >> x;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      dp[i][j] = MAX, tc[i][j] = MAX;
    }

  for (int size = 0; size < n; size++) {
    for (int i = 0, j = size; j < n; i++, j++) {
      if (i == j) {
        dp[i][j] = v[i];
        tc[i][j] = 0;
      } else if (j - i == 1) {
        dp[i][j] = v[i] + v[j];
        tc[i][j] = v[i] + v[j];
      } else {
        for (int k = i; k < j; k++) {
          if (tc[i][j] > tc[i][k] + tc[k + 1][j] + dp[i][k] + dp[k + 1][j]) {
            dp[i][j] = dp[i][k] + dp[k + 1][j];
            tc[i][j] = tc[i][k] + tc[k + 1][j] + dp[i][j];
            // cout<<i<<' '<<k<<' '<<j<<" ------> "<<tc[i][j]<<endl;
          }

          // else cout<<i<<' '<<k<<' '<<j<<"____"<<tc[i][k] + tc[k+1][j] +
          // dp[i][k] + dp[k+1][j]<<endl;

          /*dp[i][j] = min(dp[i][j], tc[i][k] + tc[k+1][j] + dp[i][k] +
          dp[k+1][j]); tc[i][j] = dp[i][j];*/
        }
      }
    }
  }
  /*cout<<endl;

  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          if(dp[i][j] == MAX) cout<<0<<' ';
          else cout<<dp[i][j]<<' ';
      }
      cout<<endl;
  }
  cout<<endl;

  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          if(tc[i][j] == MAX) cout<<0<<' ';
          else cout<<tc[i][j]<<' ';
      }
      cout<<endl;
  }*/

  cout << tc[0][n - 1];
}