Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define el_cucuy                                                               \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
#define pii pair<int, int>
#define f first
#define s second

const llong mxn = 4 * 1e5 + 3;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;

using namespace std;

// int dx[]= {1,0,-1,0}; int dy[]= {0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1}; int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2}; int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[]={-1,-1,+0,+1,+1,+0}; int dy[]={-1,+1,+2,+1,-1,-2};
// //Hexagonal Direction

llong n, m, a[mxn], dp[1001][1001], sum[mxn];

int main() {
  /// freopen("input.txt", "r", stdin);
  /// freopen("output.txt", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][i] = a[i];
  }
  for (int sz = 2; sz <= n; sz++) {
    for (int l = 1; l <= n - sz + 1; l++) {
      llong r = l + sz - 1;
      dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }
  cout << dp[1][n];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define el_cucuy                                                               \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount
#define pii pair<int, int>
#define f first
#define s second

const llong mxn = 4 * 1e5 + 3;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;

using namespace std;

// int dx[]= {1,0,-1,0}; int dy[]= {0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1}; int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2}; int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[]={-1,-1,+0,+1,+1,+0}; int dy[]={-1,+1,+2,+1,-1,-2};
// //Hexagonal Direction

llong n, m, a[mxn], dp[3001][3001], sum[mxn];

int main() {
  /// freopen("input.txt", "r", stdin);
  /// freopen("output.txt", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][i] = a[i];
  }
  for (int sz = 2; sz <= n; sz++) {
    for (int l = 1; l <= n - sz + 1; l++) {
      llong r = l + sz - 1;
      dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }
  cout << dp[1][n];
  return 0;
}