alexjercan/bugnet huggingface dataset:
Problem p00538 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N, A[114514];

int memo[4000][6000];

int dfs(int l, int r) {
  if (r - l - 1 == N)
    return 0;
  int &ret = memo[l][r];
  if (ret != -1)
    return ret;

  if ((r - l - 1) & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }
  return ret;
}

signed main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    A[i + N + N] = A[i + N] = A[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N, A[114514];

int memo[6666][6666];

int dfs(int l, int r) {
  if (r - l - 1 == N)
    return 0;
  int &ret = memo[l][r];
  if (ret != -1)
    return ret;

  if ((r - l - 1) & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }
  return ret;
}

signed main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    A[i + N + N] = A[i + N] = A[i];
  }
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
  cout << ans << endl;
  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF (1l << 55)

int N;
long long A[2010];
long long memo[2010][2010];

int l(int i) { return i == 0 ? N - 1 : i - 1; }
int r(int i) { return (i + 1) % N; }
long long max(long long a, long long b) { return a >= b ? a : b; }

long long f(int s, int t) {
  if (memo[s][t] >= 0)
    return memo[s][t];

  long long ret = 0;
  if (s == t) {
    if (A[l(s)] > A[r(s)])
      ret = max(ret, A[s] + f(l(s), s));
    else
      ret = max(ret, A[s] + f(s, r(s)));
    return memo[s][t] = ret;
  }
  for (int d = 0; d < 2; d++) {
    int ns, nt;
    long long v;
    if (d == 0)
      ns = l(s), nt = t, v = A[l(s)];
    else
      ns = s, nt = r(t), v = A[r(t)];
    if (ns == nt)
      return memo[s][t] = 0;
    if (l(ns) == nt)
      return memo[s][t] = v;

    if (A[l(ns)] >= A[r(nt)])
      ns = l(ns);
    else
      nt = r(nt);
    ret = max(ret, v + f(ns, nt));
  }

  return memo[s][t] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      memo[i][j] = -INF;
    }
  }

  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, f(i, i));
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF (1l << 55)

int N;
long long A[2010];
long long dp[2010][2010];

int l(int i) { return i == 0 ? N - 1 : i - 1; }
int l2(int i) { return l(l(i)); }
int r(int i) { return (i + 1) % N; }
int r2(int i) { return r(r(i)); }
long long max(long long a, long long b) { return a >= b ? a : b; }
void update(int s, int t, long long v) { dp[s][t] = max(dp[s][t], v); }

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = -INF;
    }
  }
  for (int i = 0; i < N; i++) {
    if (A[l(i)] > A[r(i)])
      update(l(i), i, A[i]);
    else
      update(i, r(i), A[i]);
  }
  for (int _ = 0; _ < 1000; _++)
    for (int s = 0; s < N; s++) {
      for (int t = 0; t < N; t++) {
        if (dp[s][t] == -INF)
          continue;

        for (int d = 0; d < 2; d++) {
          int ns, nt;
          long long v;
          if (d == 0)
            ns = l(s), nt = t, v = A[l(s)];
          else
            ns = s, nt = r(t), v = A[r(t)];
          if (ns == nt)
            continue;
          if (l(ns) != nt) {
            if (A[l(ns)] >= A[r(nt)])
              ns = l(ns);
            else
              nt = r(nt);
          }
          update(ns, nt, dp[s][t] + v);
        }
      }
    }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dp[i][l(i)]);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
ll memo[4000][4000];
int dat[4000];
int n;
ll rec(int a, int b, int turn) {
  if (memo[a][b] != -1)
    return memo[a][b];
  if (a == b) {
    if (turn)
      return memo[a][b] = dat[a];
    return memo[a][b] = 0;
  }
  if (turn) {
    return memo[a][b] = max(rec(a + 1, b, 0) + (ll)dat[a],
                            rec(a, b - 1, 0) + (ll)dat[b]);
  }
  if (dat[a] > dat[b])
    return memo[a][b] = rec(a + 1, b, 1);
  return memo[a][b] = rec(a, b - 1, 1);
}
int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n;
  REP(i, n) cin >> dat[i];
  REP(i, n) dat[i + n] = dat[i];
  ll ans = 0;
  REP(i, n) { ans = max(ans, rec(i + 1, i + n - 1, 0) + (ll)dat[i]); }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
ll memo[4000][4000];
int dat[4000];
int n;
ll rec(int a, int b, int turn) {
  if (memo[a][b] != -1)
    return memo[a][b];
  if (a == b) {
    if (turn)
      return dat[a % n];
    return 0;
  }
  if (turn) {
    return max(rec(a + 1, b, 0) + (ll)dat[a], rec(a, b - 1, 0) + (ll)dat[b]);
  }
  if (dat[a] > dat[b])
    return rec(a + 1, b, 1);
  return rec(a, b - 1, 1);
}
int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n;
  REP(i, n) cin >> dat[i];
  REP(i, n) dat[i + n] = dat[i];
  ll ans = 0;
  REP(i, n) { ans = max(ans, rec(i + 1, i + n - 1, 0) + (ll)dat[i]); }
  cout << ans << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll A[4010];
ll dp[4010][4010];

ll f(int l, int r) {
  ll &d = dp[l][r];
  if (d != -1)
    return d;
  int len = r - l + 1;
  if (len <= 0)
    return d = 0;
  if (len == 1)
    return d = A[l];
  if (len == 2)
    return d = max(A[l], A[l + 1]);
  d = 0;
  int nl = l + 1, nr = r;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v1 = A[l] + f(nl, nr);
  nl = l, nr = r - 1;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v2 = A[r] + f(nl, nr);
  return d = max(v1, v2);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i + N] = A[i];
  }

  if (N == 1) {
    cout << *max_element(A, A + N) << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    int l = i + 1, r = i + N - 1;
    if (A[l] > A[r])
      l++;
    else
      r--;
    v += f(l, r);
    ans = max(ans, v);
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll A[4010];
ll dp[4010][4010];

ll f(int l, int r) {
  ll &d = dp[l][r];
  if (d != -1)
    return d;
  int len = r - l + 1;
  if (len == 1)
    return d = A[l];
  if (len == 2)
    return d = max(A[l], A[l + 1]);
  d = 0;
  int nl = l + 1, nr = r;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v1 = A[l] + f(nl, nr);
  nl = l, nr = r - 1;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v2 = A[r] + f(nl, nr);
  return d = max(v1, v2);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i + N] = A[i];
  }

  if (N == 1) {
    cout << *max_element(A, A + N) << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    int l = i + 1, r = i + N - 1;
    if (A[l] > A[r])
      l++;
    else
      r--;
    v += f(l, r);
    ans = max(ans, v);
  }
  cout << ans << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n;
long long ans = 0, sum[4001], a[4001], b[4001][4001], x[4001][4001];

void read() {
  scanf("%d", &n);
  FOR(i, 1, n)
  scanf("%lld", &a[i]);
  FOR(i, 1, n)
  a[n + i] = a[i];
}

void solve() {
  FOR(i, 1, 2 * n)
  sum[i] = sum[i - 1] + a[i];
  FOR(j, 1, 2 * n)
  FORD(i, j, 1) {
    if (i == j) {
      b[i][j] = a[i];
      x[i][j] = a[i];
    } else {
      b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
      if (a[i] > a[j])
        x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
      else
        x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
    }
  }
}

void process() {
  solve();
  FOR(i, 1, n) {
    ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
  }
  cout << ans << '\\n';
}

int main() {
  read();
  process();
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n;
long long ans = 0, sum[2001], a[4001], b[4001][4001], x[4001][4001];

void read() {
  scanf("%d", &n);
  FOR(i, 1, n)
  scanf("%lld", &a[i]);
  FOR(i, 1, n)
  a[n + i] = a[i];
}

void solve() {
  FOR(i, 1, 2 * n)
  sum[i] = sum[i - 1] + a[i];
  FOR(j, 1, 2 * n)
  FORD(i, j, 1) {
    if (i == j) {
      b[i][j] = a[i];
      x[i][j] = a[i];
    } else {
      b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
      if (a[i] > a[j])
        x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
      else
        x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
    }
  }
}

void process() {
  solve();
  FOR(i, 1, n) {
    ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
  }
  cout << ans << '\\n';
}

int main() {
  read();
  process();
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define int long long
using namespace std;

signed main() {
  int n, a[2000];
  static int dp[2000][2000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (n % 2)
      dp[i][i] = a[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((n - i) % 2)
        dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
                                 dp[(j + n + 1) % n][(j + i) % n] + a[j]);
      else if (a[j] > a[(j + i) % n]) {
        dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
      } else {
        dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < n; i++)
    ma = max(dp[i][(i + n - 1) % n], ma);
  cout << ma << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define int long long
using namespace std;

signed main() {
  int n, a[2000];
  int dp[2000][2000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (n % 2)
      dp[i][i] = a[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((n - i) % 2)
        dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
                                 dp[(j + n + 1) % n][(j + i) % n] + a[j]);
      else if (a[j] > a[(j + i) % n]) {
        dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
      } else {
        dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < n; i++)
    ma = max(dp[i][(i + n - 1) % n], ma);
  cout << ma << endl;
  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[4000];
int memo[4000][4000];
int saiki(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  if (a > b)
    return 0;
  if ((b - a) & 1) { //??????
    int S = 0;
    if (::a[a] >= ::a[b + 1] && ::a[b + 1] != 0)
      S = saiki(a + 1, b);
    if (::a[a - 1] <= ::a[b] && ::a[a - 1] != 0)
      S = max(S, saiki(a, b - 1));
    return memo[a][b] = S;
  } else { //??????
    return memo[a][b] = max(saiki(a + 1, b) + ::a[a], saiki(a, b - 1) + ::a[b]);
  }
  return 0;
}
signed main() {
  memset(memo, -1, sizeof(memo));
  int b;
  cin >> b;
  for (int c = 0; c < b; c++) {
    scanf("%lld", &a[c]);
    a[b + c] = a[c];
  }
  int MAX = 0;
  for (int d = 1; d <= b; d++) {
    MAX = max(MAX, saiki(d, d + b - 1));
  }
  cout << MAX << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int memo[6000][6000], a[6000], b;
int saiki(int c, int d) { // c??????d?????§????????§???
  if (c > d)
    return 0;
  if (memo[c % b][d % b] != -1)
    return memo[c][d];
  int S = 0;
  if (((d - c) & 1) == 0) { //??????
    S = max(saiki(c + 1, d) + a[c], saiki(c, d - 1) + a[d]);
  } else { //??????
    if (a[c + 1] >= a[d]) {
      S = saiki(c + 1, d);
    }
    if (a[c] <= a[d - 1]) {
      S = max(S, saiki(c, d - 1));
    }
  }
  return memo[c][d] = S;
}
signed main() {
  memset(memo, -1, sizeof(memo));
  cin >> b;
  for (int c = 0; c < b; c++) {
    int d;
    scanf("%lld", &d);
    a[c] = a[b + c] = a[b + b + c] = d;
  }
  int MAX = 0;
  for (int i = b; i <= b + b; i++) {
    MAX = max(MAX, saiki(i, i + b - 1));
  }
  cout << MAX << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n;
int a[2020];

int dp[2020][2020];

int dfs(int l, int r) {
  l = (l + n) % n;
  r = (r + n) % n;

  int &ret = dp[l][r];
  if (ret != -1)
    return ret;
  if (l == r)
    return 0;
  if (a[l] > a[r]) {
    l--;
  } else if (a[r] > a[l]) {
    r++;
  }

  l = (l + n) % n;
  r = (r + n) % n;

  if (l == r)
    return a[l];

  ret = max(dfs(l - 1, r) + a[l], dfs(l, r + 1) + a[r]);

  return ret;
}

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int maxv = 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i += 2) {
    maxv = max(maxv, dfs(i - 1, i + 1) + a[i]);
  }
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i < n; i += 2) {
    maxv = max(maxv, dfs(i - 1, i + 1) + a[i]);
  }
  cout << maxv << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n;
int a[2020];

int dp[2020][2020];

int dfs(int l, int r) {
  l = (l + n) % n;
  r = (r + n) % n;

  int &ret = dp[l][r];
  if (ret != -1)
    return ret;
  if (l == r)
    return 0;
  if (a[l] > a[r]) {
    l--;
  } else if (a[r] > a[l]) {
    r++;
  }

  l = (l + n) % n;
  r = (r + n) % n;

  if (l == r)
    return a[l];

  ret = max(dfs(l - 1, r) + a[l], dfs(l, r + 1) + a[r]);

  return ret;
}

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int maxv = 0;
  for (int i = 0; i < n; i++) {
    memset(dp, -1, sizeof(dp));
    maxv = max(maxv, dfs(i - 1, i + 1) + a[i]);
  }

  cout << maxv << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  if (left == right)
    return player * a[left];
  if (dp[left][right][player] != -1)
    return dp[left][right][player];

  ll maxi = -1;
  if ((!player && a[left] > a[right]) || player)
    maxi =
        max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
  if ((!player && a[left] < a[right]) || player)
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
                             player * a[right]);

  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
  cout << maxi << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  if (left == right)
    return player * a[left];

  ll maxi = -1;
  if ((!player && a[left] > a[right]) || player)
    maxi =
        max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
  if ((!player && a[left] < a[right]) || player)
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
                             player * a[right]);

  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
  cout << maxi << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  //    cout<<left<<" "<<right<<" "<<player<<endl;
  if (left == right) {
    if (player == JOI)
      return a[left];
    else
      return 0;
  }
  if (dp[left][right][player] != -1)
    return dp[left][right][player];

  ll maxi = -1;
  if (player == IOI) {
    if (a[left] > a[right])
      maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
    if (a[left] < a[right])
      maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
  } else {
    maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
  }
  //    cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) {
    int left = (i + 1) % n;
    int right = (i - 1 + n) % n;

    ll res = solve(left, right, IOI);
    //        cout<<i<<" "<<res<<endl;
    maxi = max(maxi, res + a[i]);
  }
  cout << maxi << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0

int n;
vector<int> a;

ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
  //    cout<<left<<" "<<right<<" "<<player<<endl;
  if (left == right) {
    if (player == JOI)
      return a[left];
    else
      return 0;
  }
  //    if(dp[left][right][player]!=-1)return dp[left][right][player];

  ll maxi = -1;
  if (player == IOI) {
    if (a[left] > a[right])
      maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
    if (a[left] < a[right])
      maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
  } else {
    maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
    maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
  }
  //    cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
  return dp[left][right][player] = maxi;
}

int main() {
  rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];

  ll maxi = -1;
  rep(i, n) {
    int left = (i + 1) % n;
    int right = (i - 1 + n) % n;

    ll res = solve(left, right, IOI);
    //        cout<<i<<" "<<res<<endl;
    maxi = max(maxi, res + a[i]);
  }
  cout << maxi << endl;
}