Problem p03552 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define _D(p)                                                                  \
  std::cout << "L" << __LINE__ << " : " #p " = " << (p) << std::endl;
#define _D2(p, q)                                                              \
  std::cout << "L" << __LINE__ << " : " #p " = " << (p) << ", " #q " = "       \
            << (q) << std::endl;
#define _DN(v)                                                                 \
  std::cout << "L" << __LINE__ << " : " #v " = [";                             \
  rep(i, (v).size()) { std::cout << v[i] << (i == v.size() - 1 ? "" : ", "); } \
  std::cout << "]" << std::endl;
#define _DNN(v)                                                                \
  std::cout << "L" << __LINE__ << " : " #v " = [" << std::endl;                \
  rep(i, (v).size()) {                                                         \
    std::cout << "    [";                                                      \
    rep(j, (v)[0].size()) {                                                    \
      std::cout << v[i][j] << (j == v[0].size() - 1 ? "" : ", ");              \
    }                                                                          \
    std::cout << "]," << std::endl;                                            \
  }                                                                            \
  std::cout << "]" << std::endl;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 2e9;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dp[2001][2];
int flag[2001][2];
int N, Z, W;
vector<int> A;
int dfs(int i, int turn, int x, int y) {
  // cout << i << ", " << turn << ", " << x << ", " << y << endl;
  // if (flag[i][turn]) return dp[i][turn];
  if (i == N) {
    return abs(x - y);
  }
  int score = turn ? INF : 0;
  if (turn) {
    for (int j = i; j < N; ++j) {
      score = min(score, dfs(j + 1, 1 - turn, x, A[j]));
    }
  } else {
    for (int j = i; j < N; ++j) {
      score = max(score, dfs(j + 1, 1 - turn, A[j], y));
    }
  }
  dp[i][turn] = score;
  flag[i][turn] = 1;
  return score;
}

int main(int argc, char const *argv[]) {

  cin >> N >> Z >> W;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cout << dfs(0, 0, Z, W) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define _D(p)                                                                  \
  std::cout << "L" << __LINE__ << " : " #p " = " << (p) << std::endl;
#define _D2(p, q)                                                              \
  std::cout << "L" << __LINE__ << " : " #p " = " << (p) << ", " #q " = "       \
            << (q) << std::endl;
#define _DN(v)                                                                 \
  std::cout << "L" << __LINE__ << " : " #v " = [";                             \
  rep(i, (v).size()) { std::cout << v[i] << (i == v.size() - 1 ? "" : ", "); } \
  std::cout << "]" << std::endl;
#define _DNN(v)                                                                \
  std::cout << "L" << __LINE__ << " : " #v " = [" << std::endl;                \
  rep(i, (v).size()) {                                                         \
    std::cout << "    [";                                                      \
    rep(j, (v)[0].size()) {                                                    \
      std::cout << v[i][j] << (j == v[0].size() - 1 ? "" : ", ");              \
    }                                                                          \
    std::cout << "]," << std::endl;                                            \
  }                                                                            \
  std::cout << "]" << std::endl;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 2e9;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dp[2001][2];
int flag[2001][2];
int N, Z, W;
vector<int> A;
int dfs(int i, int turn, int x, int y) {
  if (flag[i][turn])
    return dp[i][turn];
  if (i == N) {
    return abs(x - y);
  }
  int score = turn ? INF : 0;
  if (turn) {
    for (int j = i; j < N; ++j) {
      score = min(score, dfs(j + 1, 1 - turn, x, A[j]));
    }
  } else {
    for (int j = i; j < N; ++j) {
      score = max(score, dfs(j + 1, 1 - turn, A[j], y));
    }
  }
  dp[i][turn] = score;
  flag[i][turn] = 1;
  return score;
}

int main(int argc, char const *argv[]) {

  cin >> N >> Z >> W;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cout << dfs(0, 0, Z, W) << endl;
  return 0;
}