Problem p03171 (C++)

Buggy submission (status: Runtime Error):
//
// Created by vishal on 7/7/20.
//

//
// Created by vishal on 6/14/20.
//

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w                                                                      \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define F10                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int mex = 3000;
int arr[mex][mex];
int32_t main() {
  int n;
  cin >> n;
  vi v(n + 2);
  v[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i][i];
    v[i] = v[i - 1] + arr[i][i];
  }
  for (int dist = 1; dist < n; ++dist) {
    for (int i = 1; i <= n - dist; ++i) {
      arr[i][i + dist] =
          max(arr[i][i] + (v[i + dist] - v[i]) - arr[i + 1][i + dist],
              arr[i + dist][i + dist] + (v[i + dist - 1] - v[i - 1]) -
                  arr[i][i + dist - 1]);
    }
  }
  cout << arr[1][n] * 2 - v[n];
}

Fixed submission:
//
// Created by vishal on 7/7/20.
//

//
// Created by vishal on 6/14/20.
//

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w                                                                      \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define F10                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int mex = 3005;
int arr[mex][mex];
int32_t main() {
  int n;
  cin >> n;
  vi v(n + 2);
  v[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i][i];
    v[i] = v[i - 1] + arr[i][i];
  }
  for (int dist = 1; dist < n; ++dist) {
    for (int i = 1; i <= n - dist; ++i) {
      arr[i][i + dist] =
          max(arr[i][i] + (v[i + dist] - v[i]) - arr[i + 1][i + dist],
              arr[i + dist][i + dist] + (v[i + dist - 1] - v[i - 1]) -
                  arr[i][i + dist - 1]);
    }
  }
  cout << arr[1][n] * 2 - v[n];
}