Problem p03552 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

int n;
lli dat[2005];
lli dp[2][2005];

lli Z, W;

lli inf = 10000000000LL;

lli dfs(int no, int d) {
  lli &x = dp[no][d];
  if (x >= 0) {
    return x;
  }
  lli res = abs(dat[d] - dat[n]);
  if (no == 0) {
    reg(j, d + 1, n - 1) { res = max(res, dfs(1 - no, j)); }
    return res;
  } else {
    reg(j, d + 1, n - 1) { res = min(res, dfs(1 - no, j)); }
  }
  x = res;
  // printf("%d %d %d %lld\n",no,d,bd,res);
  return res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%lld%lld", &n, &Z, &W);
  reg(i, 1, n) { scanf("%lld", &dat[i]); }
  dat[0] = W;
  printf("%lld\n", dfs(0, 0));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

int n;
lli dat[2005];
lli dp[2][2005];

lli Z, W;

lli inf = 10000000000LL;

lli dfs(int no, int d) {
  lli &x = dp[no][d];
  if (x >= 0) {
    return x;
  }
  lli res = abs(dat[d] - dat[n]);
  if (no == 0) {
    reg(j, d + 1, n - 1) { res = max(res, dfs(1 - no, j)); }
  } else {
    reg(j, d + 1, n - 1) { res = min(res, dfs(1 - no, j)); }
  }
  x = res;
  // printf("%d %d %d %lld\n",no,d,bd,res);
  return res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%lld%lld", &n, &Z, &W);
  reg(i, 1, n) { scanf("%lld", &dat[i]); }
  dat[0] = W;
  printf("%lld\n", dfs(0, 0));
  return 0;
}