Problem p03772 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MOD = (int)1e9 + 7;

const int MAXN = 50;

int n;

int a[MAXN * 2 + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= 2 * n - 1; ++i)
    a[i] = read<int>();
}

inline void solve() {
  static int f[MAXN + 5][MAXN * 2 + 5][MAXN * 2 + 5];

  sort(a + 1, a + 2 * n);

  f[n][1][0] = 1;
  for (int i = n - 1; i >= 1; --i) {
    int pl = a[i] != a[i + 1], pr = a[2 * n - i - 1] != a[2 * n - i];
    for (int l = 0; l <= 2 * n - 1; ++l)
      for (int r = 0; l + r <= 2 * n - 1; ++r)
        if (f[i + 1][l][r]) {
          for (int k = 1; k <= l + pl; ++k)
            (f[i][l + pl - k + 1][r + pr + (k > 1)] += f[i + 1][l][r]) %= MOD;
          for (int k = 1; k <= r + pr; ++k)
            (f[i][l + pl + 1][r + pr - k] += f[i + 1][l][r]) %= MOD;
        }
  }

  int ans = 0;
  for (int l = 0; l <= 2 * n - 1; ++l)
    for (int r = 0; l + r <= 2 * n - 1; ++r)
      (ans += f[1][l][r]) %= MOD;
  printf("%d\n", (ans + MOD) % MOD);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("F.in", "r", stdin);
  freopen("F.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MOD = (int)1e9 + 7;

const int MAXN = 50;

int n;

int a[MAXN * 2 + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= 2 * n - 1; ++i)
    a[i] = read<int>();
}

inline void solve() {
  static int f[MAXN + 5][MAXN * 2 + 5][MAXN * 2 + 5];

  sort(a + 1, a + 2 * n);

  f[n][1][0] = 1;
  for (int i = n - 1; i >= 1; --i) {
    int pl = a[i] != a[i + 1], pr = a[2 * n - i - 1] != a[2 * n - i];
    for (int l = 0; l <= 2 * n - 1; ++l)
      for (int r = 0; l + r <= 2 * n - 1; ++r)
        if (f[i + 1][l][r]) {
          for (int k = 1; k <= l + pl; ++k)
            (f[i][l + pl - k + 1][r + pr + (k > 1)] += f[i + 1][l][r]) %= MOD;
          for (int k = 1; k <= r + pr; ++k)
            (f[i][l + pl + 1][r + pr - k] += f[i + 1][l][r]) %= MOD;
        }
  }

  int ans = 0;
  for (int l = 0; l <= 2 * n - 1; ++l)
    for (int r = 0; l + r <= 2 * n - 1; ++r)
      (ans += f[1][l][r]) %= MOD;
  printf("%d\n", (ans + MOD) % MOD);
}

int main() {

  input();
  solve();

  return 0;
}