Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2100000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

ll n;
vl a(3005);
ll dp[3005][3005];
bool calc[3005][3005];

ll rec(ll l, ll r) {
  if (l == r)
    return a[l];
  if (calc[l][r])
    return dp[l][r];
  dp[l][r] = max(a[l] - rec(l + 1, r), a[r] - rec(l, r - 1));
  return dp[l][r];
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  cout << rec(0, n - 1) << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2100000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

ll n;
vl a(3005);
ll dp[3005][3005];
bool calc[3005][3005];

ll rec(ll l, ll r) {
  if (l == r)
    return a[l];
  if (calc[l][r])
    return dp[l][r];
  dp[l][r] = max(a[l] - rec(l + 1, r), a[r] - rec(l, r - 1));
  calc[l][r] = true;
  return dp[l][r];
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  cout << rec(0, n - 1) << endl;
}