Problem p04013 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define ll long long
#define endl '\n'
#define pi 3.14159265
ll n, a;
vector<ll> v(51);
ll dp[51][30000][25];
ll f(ll i, ll sum, ll len) {
  if (i == n)
    return (len * a == sum);
  if (dp[i][sum][len] != -1)
    return dp[i][sum][len];
  dp[i][sum][len] = f(i + 1, sum, len) + f(i + 1, sum + v[i], len + 1);
  return dp[i][sum][len];
}
int main() {
  cin >> n >> a;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  cout << f(0, 0, 0) - 1;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define ll long long
#define endl '\n'
#define pi 3.14159265
ll n, a;
vector<ll> v(51);
ll dp[51][3000][25];
ll f(ll i, ll sum, ll len) {
  if (i == n)
    return (len * a == sum);
  if (dp[i][sum][len] != -1)
    return dp[i][sum][len];
  dp[i][sum][len] = f(i + 1, sum, len) + f(i + 1, sum + v[i], len + 1);
  return dp[i][sum][len];
}
int main() {
  cin >> n >> a;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  cout << f(0, 0, 0) - 1;
}