Problem p01153 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int memo[100][100];
int n;
int card[100];

#define ABS(x) ((x) < 0 ? -(x) : (x))

int solve(int a, int b) {
  if (a == b)
    return 0;
  if (b == a + 1 || (b == n - 1 && a == 0))
    return ABS(card[b] - card[a]);
  if (memo[a][b] != -1)
    return memo[a][b];

  int ret = 0;
  int bb = b;
  if (a > b)
    bb = n + b;
  for (int c = a; c < bb; c++)
    ret = max(ret, solve(a, c % n) + solve((c + 1) % n, b) +
                       ABS(card[a] - card[(c + 1) % n]));

  return memo[a][b] = ret;
}

int main() {
  int cc = getInt();
  while (cc-- > 0) {
    int ans = 0;
    n = getInt();
    REP(i, n) card[i] = getInt();
    memset(memo, -1, sizeof(memo));
    REP(i, n) ans = max(ans, solve(i, (i + n - 1) % n));
    print(ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int memo[100][100];
int n;
int _card[100];
struct __card {
  int base;
  int operator[](int i) { return _card[(i + base) % n]; }
} card;

#define ABS(x) ((x) < 0 ? -(x) : (x))

int solve(int a, int b) {
  if (a == b)
    return 0;
  if (b == a + 1)
    return ABS(card[b] - card[a]);
  if (memo[a][b] != -1)
    return memo[a][b];

  int ret = 0;
  for (int c = a; c < b; c++)
    ret = max(ret, solve(a, c) + solve(c + 1, b) + ABS(card[a] - card[c + 1]));

  return memo[a][b] = ret;
}

int main() {
  int cc = getInt();
  while (cc-- > 0) {
    int ans = 0;
    n = getInt();
    REP(i, n) _card[i] = getInt();
    for (card.base = 0; card.base < n; card.base++) {
      memset(memo, -1, sizeof(memo));
      ans = max(ans, solve(0, n - 1));
    }
    print(ans);
  }
  return 0;
}