Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
// vector push_back push front top empty pop make_pair long long insert begin
// end
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define RE rend()
#define Z size()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define L length()
#define show(a) cerr << " *** " << a << endl;
#define show1(a) cerr << " /// " << a << endl;
#define valid(a, b, c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int mod = (int)1e9 + 7;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll egcd(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
ll mbinp(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  ll ans = mbinp(a, b / 2);
  ll tmp = (ans * ans) % mod;
  if (b % 2)
    return ((tmp * a) % mod);
  return ((tmp) % mod);
}
ll binp(ll a, ll b) {
  if (b == 0)
    return 1;
  ll ans = binp(a, b / 2);
  ll tmp = (ans * ans);
  if (b % 2)
    return ((tmp * a));
  return ((tmp));
}

const int maax = 3050;
ll dp[maax][maax];
ll tab[maax];
int n;

ll solve(int i, int j) {
  if (j - i == 1)
    return (abs(tab[i] - tab[j]));
  if (dp[i][j] != -1)
    return dp[i][j];
  ll tmp = 0;
  tmp = tab[i] - solve(i + 1, j);
  tmp = max(tmp, tab[j] - solve(i, j - 1));
  return (dp[i][j] = tmp);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  memset(dp, -1, sizeof(dp));
  REP(i, 0, n)
  cin >> tab[i];
  ll res = solve(0, n - 1);
  cout << res;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
// vector push_back push front top empty pop make_pair long long insert begin
// end
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define RE rend()
#define Z size()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define L length()
#define show(a) cerr << " *** " << a << endl;
#define show1(a) cerr << " /// " << a << endl;
#define valid(a, b, c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int mod = (int)1e9 + 7;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll egcd(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
ll mbinp(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  ll ans = mbinp(a, b / 2);
  ll tmp = (ans * ans) % mod;
  if (b % 2)
    return ((tmp * a) % mod);
  return ((tmp) % mod);
}
ll binp(ll a, ll b) {
  if (b == 0)
    return 1;
  ll ans = binp(a, b / 2);
  ll tmp = (ans * ans);
  if (b % 2)
    return ((tmp * a));
  return ((tmp));
}

const int maax = 3050;
ll dp[maax][maax];
ll tab[maax];
int n;

ll solve(int i, int j) {
  if (j - i == 0)
    return tab[i];
  if (j - i == 1)
    return (abs(tab[i] - tab[j]));
  if (dp[i][j] != -1)
    return dp[i][j];
  ll tmp = 0;
  tmp = tab[i] - solve(i + 1, j);
  tmp = max(tmp, tab[j] - solve(i, j - 1));
  return (dp[i][j] = tmp);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  memset(dp, -1, sizeof(dp));
  REP(i, 0, n)
  cin >> tab[i];
  ll res = solve(0, n - 1);
  cout << res;
}