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

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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N * 2);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[N + i] = A[i];
  }
  vector<vector<ll>> dp(N * 2, vector<ll>(N * 2 + 1, -1e16));
  for (int i = 0; i < N * 2; i++) {
    dp[i][i + 1] = A[i];
  }
  ll res;
  for (int len = 2; len <= N; len++) {
    res = 0;
    if (len & 1) {
      for (int i = 0; i + len <= N * 2; i++) {
        if (dp[i + 1][i + len] > 0) {
          dp[i][i + len] = max(dp[i][i + len], dp[i + 1][i + len] + A[i]);
          res = max(res, dp[i + 1][i + len] + A[i]);
        }
        if (dp[i][i + len - 1] > 0) {
          dp[i][i + len] =
              max(dp[i][i + len], dp[i][i + len - 1] + A[i + len - 1]);
          res = max(res, dp[i][i + len - 1] + A[i + len - 1]);
        }
      }
    } else {
      for (int i = 0; i + len < N * 2; i++) {
        if (A[i] > A[i + len]) {
          dp[i][i + len] = max(dp[i][i + len], dp[i + 1][i + len]);
          res = max(res, dp[i + 1][i + len]);
        } else {
          dp[i + 1][i + len + 1] =
              max(dp[i + 1][i + len + 1], dp[i + 1][i + len]);
          res = max(res, dp[i + 1][i + len]);
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}


Error: MLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N * 3);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[N * 2 + i] = A[N + i] = A[i];
  }
  vector<vector<ll>> dp(N * 3, vector<ll>(N * 3 + 1, -1e16));
  for (int i = 0; i < N * 3; i++) {
    dp[i][i + 1] = A[i];
  }
  ll res;
  for (int len = 2; len <= N; len++) {
    res = 0;
    if (len & 1) {
      for (int i = 0; i + len <= N * 3; i++) {
        if (dp[i + 1][i + len] > 0) {
          dp[i][i + len] = max(dp[i][i + len], dp[i + 1][i + len] + A[i]);
          res = max(res, dp[i + 1][i + len] + A[i]);
        }
        if (dp[i][i + len - 1] > 0) {
          dp[i][i + len] =
              max(dp[i][i + len], dp[i][i + len - 1] + A[i + len - 1]);
          res = max(res, dp[i][i + len - 1] + A[i + len - 1]);
        }
      }
    } else {
      for (int i = 0; i + len < N * 3; i++) {
        if (A[i] > A[i + len]) {
          dp[i][i + len] = max(dp[i][i + len], dp[i + 1][i + len]);
          res = max(res, dp[i + 1][i + len]);
        } else {
          dp[i + 1][i + len + 1] =
              max(dp[i + 1][i + len + 1], dp[i + 1][i + len]);
          res = max(res, dp[i + 1][i + len]);
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

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

long long int DP[2000][2000];
long long int A[2000];

long long int INF = 100000000000000LL;

int main() {

  int N;
  cin >> N;

  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++) {
    cin >> A[i];
    DP[i][i] = A[i];
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i % 2 == 0) {
        DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
                                 DP[j][(j + i - 1) % N] + A[(j + i) % N]);
      } else {
        if (A[j] >= A[(j + i + 1) % N]) {
          DP[j][(j + i) % N] =
              max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
        }
        if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
          DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
        }
      }
      // cout << DP[j][(j + i) % N] << " ";
    }
    // cout << endl;
  }

  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, DP[i][(i - 1 + N) % N]);
  }

  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

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

long long int DP[2000][2000];
long long int A[2000];

long long int INF = 100000000000000LL;

int main() {

  int N;
  cin >> N;

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

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

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i % 2 == 0) {
        DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
                                 DP[j][(j + i - 1) % N] + A[(j + i) % N]);
      } else {
        if (A[j] >= A[(j + i + 1) % N]) {
          DP[j][(j + i) % N] =
              max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
        }
        if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
          DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
        }
      }
      // cout << DP[j][(j + i) % N] << " ";
    }
    // cout << endl;
  }

  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, DP[i][(i - 1 + N) % N]);
  }

  cout << ans << endl;

  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 2000000000

#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(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

ll dp[4040][6060];
int main(void) {
  int N;
  ll piece[6060];
  memset(piece, 0, sizeof piece);
  memset(dp, 0, sizeof dp);
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    piece[i] = a;
    piece[N * 1 + i] = a;
    piece[N * 2 + i] = a;
  }
  FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
  FOR(k, 1, N + 1) {
    REP(i, 2 * N) {
      int l = i, r = i + k;
      if (k % 2 == 0) {
        if (piece[l] >= piece[r]) {
          dp[l][r] = max(dp[l][r], dp[l + 1][r]);
        }
        if (l > 0 && piece[l - 1] <= piece[r - 1]) {
          dp[l][r] = max(dp[l][r], dp[l][r - 1]);
        }
      } else {
        dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
        dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
      }
    }
  }
  ll res = 0;
  REP(i, N) { res = max(res, dp[i][i + N]); }
  cout << res << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 2000000000

#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(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

ll dp[4040][6060];
int main(void) {
  int N;
  ll piece[6060];
  memset(piece, 0, sizeof piece);
  memset(dp, 0, sizeof dp);
  cin >> N;
  REP(i, N) {
    ll a;
    cin >> a;
    piece[i] = a;
    piece[N * 1 + i] = a;
    piece[N * 2 + i] = a;
  }
  FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
  FOR(k, 1, N + 1) {
    REP(i, min(4040, 3 * N - k)) {
      int l = i, r = i + k;
      if (k % 2 == 0) {
        if (piece[l] >= piece[r]) {
          dp[l][r] = max(dp[l][r], dp[l + 1][r]);
        }
        if (l > 0 && piece[l - 1] <= piece[r - 1]) {
          dp[l][r] = max(dp[l][r], dp[l][r - 1]);
        }
      } else {
        dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
        dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
      }
    }
  }
  ll res = 0;
  REP(i, N) { res = max(res, dp[i][i + N]); }
  cout << res << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ll;

ll n, A[4001], memo[4001][4001] = {}, ans = 0;
bool calced[2001][2001][2];
ll solve(int s, int e, bool JOI) {
  if (memo[s][e])
    return memo[s][e];
  if (s == e) {
    if (JOI) {
      return memo[s][e] = A[s];
    } else {
      return memo[s][e] = 0;
    }
  }

  if (JOI) {
    return memo[s][e] =
               max(solve(s + 1, e, 0) + A[s], solve(s, e - 1, 0) + A[e]);
  } else {
    if (A[s] > A[e]) {
      return memo[s][e] = solve(s + 1, e, 1);
    } else {
      return memo[s][e] = solve(s, e - 1, 1);
    }
  }
}

int main() {
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    ans = max(ans, solve(i, i + n - 1, 1));
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ll;
long long int n, A[4001], ans = 0;

ll solve(int s, int e, bool JOI) {
  if (s == e) {
    if (JOI) {
      return A[s];
    } else {
      return 0;
    }
  }

  if (JOI) {
    return max(solve(s + 1, e, 0) + A[s], solve(s, e - 1, 0) + A[e]);
  } else {
    if (A[s] > A[e]) {
      return solve(s + 1, e, 1);
    } else {
      return solve(s, e - 1, 1);
    }
  }
}

int main() {
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    ans = max(ans, solve(i, i + n - 1, 1));
  }

  cout << ans << endl;

  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n;
long long a[2000], memo[2][2000][2000];
long long sum(int j, int left, int right) {
  if (left == right) {
    if (j == 0)
      return 0;
    if (j == 1)
      return a[left];
  }
  if (memo[j][left][right] != -1)
    return memo[j][left][right];
  long long res = 0;
  if (j == 0) {
    if (a[left] > a[right]) {
      if (left == 0)
        res = max(res, sum(1, n - 1, right));
      else
        res = max(res, sum(1, left - 1, right));
    } else {
      if (right == n - 1)
        res = max(res, sum(1, left, 0));
      else
        res = max(res, sum(1, left, right + 1));
    }
  }
  if (j == 1) {
    if (left == 0)
      res = max(res, sum(0, n - 1, right) + a[left]);
    else
      res = max(res, sum(0, left - 1, right) + a[left]);
    if (right == n - 1)
      res = max(res, sum(0, left, 0) + a[right]);
    else
      res = max(res, sum(0, left, right + 1) + a[right]);
  }
  return memo[j][left][right] = res;
}
int main() {
  long long ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  fill_n(**memo, 2 * 2000 * 2000, -1);
  for (int i = 0; i < n; i++) {
    ans =
        max(ans, sum(0, i == 0 ? n - 1 : i - 1, i == n - 1 ? 0 : i + 1) + a[i]);
  }
  cout << ans << endl;
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
long long a[2000], memo[2][2000][2000];
long long sum(int j, int left, int right) {
  if (left == right) {
    if (j == 0)
      return 0;
    if (j == 1)
      return a[left];
  }
  if (memo[j][left][right] != -1)
    return memo[j][left][right];
  long long res = 0;
  if (j == 0) {
    if (a[left] > a[right]) {
      if (left == 0)
        res = max(res, sum(1, n - 1, right));
      else
        res = max(res, sum(1, left - 1, right));
    } else {
      if (right == n - 1)
        res = max(res, sum(1, left, 0));
      else
        res = max(res, sum(1, left, right + 1));
    }
  }
  if (j == 1) {
    if (left == 0)
      res = max(res, sum(0, n - 1, right) + a[left]);
    else
      res = max(res, sum(0, left - 1, right) + a[left]);
    if (right == n - 1)
      res = max(res, sum(0, left, 0) + a[right]);
    else
      res = max(res, sum(0, left, right + 1) + a[right]);
  }
  return memo[j][left][right] = res;
}
int main() {
  long long ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    fill_n(**memo, 2 * 2000 * 2000, -1);
    ans =
        max(ans, sum(0, i == 0 ? n - 1 : i - 1, i == n - 1 ? 0 : i + 1) + a[i]);
  }
  cout << ans << endl;
  return (0);
}
Problem p00538 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n;
int a[6000];
vector<vector<lint>> v;
lint &at(int l, int r) { return v[l][r - l]; }

lint calc(int l, int r) {
  int i = (r - l) - 1;
  if (i == n)
    return at(l, r) = 0;
  if (~at(l, r))
    return at(l, r);

  if (i & 1) {
    if (a[l] > a[r])
      return at(l, r) = calc(l - 1, r);
    else
      return at(l, r) = calc(l, r + 1);
  } else {
    return at(l, r) = max(a[l] + calc(l - 1, r), a[r] + calc(l, r + 1));
  }
}

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

  v.resize(n * 3);
  for (int i = 0; i < n * 3; i++) {
    v[i].assign(n * 3 - i, -1);
  }

  lint res = 0;
  for (int i = 0; i < n; i++) {
    lint r = a[i] + calc(i + n - 1, i + n + 1);
    res = max(res, r);
  }

  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

int n;
int a[6000];
lint dp[6000][6000];

lint calc(int l, int r) {
  int i = (r - l) - 1;
  if (i == n)
    return dp[l][r] = 0;
  if (~dp[l][r])
    return dp[l][r];

  if (i & 1) {
    if (a[l] > a[r])
      return dp[l][r] = calc(l - 1, r);
    else
      return dp[l][r] = calc(l, r + 1);
  } else {
    return dp[l][r] = max(a[l] + calc(l - 1, r), a[r] + calc(l, r + 1));
  }
}

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

  fill_n(*dp, 6000 * 6000, -1);

  lint res = 0;
  for (int i = 0; i < n; i++) {
    lint r = a[i] + calc(i + n - 1, i + n + 1);
    res = max(res, r);
  }

  cout << res << endl;

  return 0;
}
Problem p00538 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int N, A[2000], used[2001];
long long dp[2001][2001];

int prev(int idx) { return (idx - 1 + N) % N; }

int next(int idx) { return (idx + 1) % N; }

long long rec(int l, int r, bool turn) {

  if (l == r)
    return (turn ? A[l] : 0);
  if (~dp[l][r])
    return dp[l][r];

  long long ret = 0;
  if (!turn) {
    if (A[l] > A[r])
      ret = max(ret, rec(prev(l), r, true));
    else
      ret = max(ret, rec(l, next(r), true));
  } else {
    ret = max(ret, rec(prev(l), r, false) + A[l]);
    ret = max(ret, rec(l, next(r), false) + A[r]);
  }
  if (turn)
    dp[l][r] = ret;
  return ret;
}

int main() {

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

  long long ans = 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < N; i++) {
    ans = max(ans, rec(prev(i), next(i), false) + A[i]);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int N, A[2000], used[2001];
long long dp[2001][2001];

int prev(int idx) { return (idx - 1 + N) % N; }

int next(int idx) { return (idx + 1) % N; }

long long rec(int l, int r, bool turn) {

  if (l == r)
    return (turn ? A[l] : 0);
  if (~dp[l][r])
    return dp[l][r];

  long long ret = 0;
  if (!turn) {
    if (A[l] > A[r])
      ret = max(ret, rec(prev(l), r, true));
    else
      ret = max(ret, rec(l, next(r), true));
  } else {
    ret = max(ret, rec(prev(l), r, false) + A[l]);
    ret = max(ret, rec(l, next(r), false) + A[r]);
  }
  if (turn)
    dp[l][r] = ret;
  return ret;
}

int main() {

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

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    memset(dp, -1, sizeof(dp));
    ans = max(ans, rec(prev(i), next(i), false) + A[i]);
  }
  cout << ans << endl;
}
Problem p00538 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long

#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;

constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;

int n, a[MAX_N], mem[MAX_N][MAX_N];

int Dfs(int s, int t) {
  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return 0;

  if (a[s] < a[t]) {
    t++;
    t %= n;
  } else {
    s += n;
    s--;
    s %= n;
  }

  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return a[s];

  return mem[s][t] =
             max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(mem, -1, sizeof(mem));

  int ans = -1;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
  }

  cout << ans << endl;
}

Error: 0
4 1
3 2
2 1
0 2
4 3
3 2
1 3
1 0
0 4
2 4
2 1
1 0
3 0


Fixed submission:

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

#define int long long

#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;

constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;

int n, a[MAX_N], mem[MAX_N][MAX_N];

int Dfs(int s, int t) {
  cerr << s << " " << t << endl;
  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return 0;

  if (a[s] < a[t]) {
    t++;
    t %= n;
  } else {
    s += n;
    s--;
    s %= n;
  }

  if (mem[s][t] != -1)
    return mem[s][t];
  if (s == t)
    return a[s];

  return mem[s][t] =
             max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(mem, -1, sizeof(mem));

  int ans = -1;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
  }

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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
long long memo[2001][2001], A[2001];
long long dp(int left, int right) {
  if (left < 0)
    left = n - 1;
  if (right >= n)
    right = 0;
  int next_left = left, next_right = right;
  long long ans = 0;

  if (memo[left][right] != -1)
    return memo[left][right];
  if (left == right)
    return memo[left][right] = ans;

  // IOI?????????
  if (A[left] > A[right])
    next_left--;
  else
    next_right++;

  //?????°??????
  if (next_left < 0)
    next_left = n - 1;
  if (next_right >= n)
    next_right = 0;
  if (next_left == next_right)
    return memo[next_left][next_right] = ans + A[next_left];

  // JOI??????
  ans = max(dp(next_left - 1, next_right) + A[next_left],
            dp(next_left, next_right + 1) + A[next_right]);

  return memo[left][right] = ans;
}
int main() {
  long long ans = -1;
  memset(memo, -1, sizeof memo);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < n; i++) {
    ans = max(dp(i - 1, i + 1) + A[i], ans);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
long long memo[2001][2001], A[2001];
long long dp(int left, int right) {
  if (left < 0)
    left = n - 1;
  if (right >= n)
    right = 0;
  if (memo[left][right] != -1)
    return memo[left][right];

  long long ans = 0;

  //?????°??????
  if (left < 0)
    left = n - 1;
  if (right >= n)
    right = 0;
  if (left == right)
    return memo[left][right] = ans;

  // IOI?????????
  if (A[left] > A[right])
    left--;
  else
    right++;

  //?????°??????
  if (left < 0)
    left = n - 1;
  if (right >= n)
    right = 0;
  if (left == right)
    return memo[left][right] = ans + A[left];

  // JOI??????
  ans = max(dp(left - 1, right) + A[left], dp(left, right + 1) + A[right]);

  return memo[left][right] = ans;
}
int main() {
  long long ans = -1;
  memset(memo, -1, sizeof memo);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < n; i++) {
    ans = max(dp(i - 1, i + 1) + A[i], ans);
  }
  cout << ans << endl;
}