Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int infI = 1e9 + 5;
const long long infL = 1e16;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

#define mp make_pair
#define in &
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pll pair<ll, ll>
#define vvl vector<vector<ll>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define eb emplace_back
#define forn(i, a, b) for (ll i = a; i < b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define endl '\n'
#define popb pop_back();
#define se second
#define fi first
#define debug(x) cerr << x << endl
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define fordebug(x) cerr << x << " "

ll power(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  ll temp = power(a, b / 2);
  temp = (temp * temp) % MOD;
  if (b % 2) {
    temp = (temp * a) % MOD;
  }
  return temp;
}

// only use if mod is prime
ll mod_inv(ll a, ll mod) {
  a %= mod;
  return power(a, mod - 2);
}

void extended_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return;
  }
  ll x1, y1;
  extended_gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - ((a / b) * y1);
  return;
}

// TLE can also occur if vectors are not passed by reference

ll dp[55][55][55 * 50], a[55], ans;

int main() {
  // dp[i][j][k] -> #of ways so that we select j elements from first i numbers
  // and get sum k
  int n, A, s = 0;
  cin >> n >> A;
  forn(i, 1, n + 1) {
    cin >> a[i];
    s += a[i];
  }
  dp[0][0][0] = 1;
  forn(i, 1, n + 1) {
    forn(j, 0, i + 1) {
      forn(k, 0, s + 1) {
        dp[i][j][k] = dp[i - 1][j][k]; // we do not take the i'th element
        if (k >= a[i] and j) {
          dp[i][j][k] += dp[i - 1][j - 1][k - a[i]]; // we take the i'th element
        }
      }
    }
  }
  forn(i, 1, 55 * 50) {
    if (A * i >= 55 * 50) {
      break;
    }
    ans += dp[n][i][A * i];
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int infI = 1e9 + 5;
const long long infL = 1e16;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

#define mp make_pair
#define in &
#define pb push_back
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pll pair<ll, ll>
#define vvl vector<vector<ll>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define eb emplace_back
#define forn(i, a, b) for (ll i = a; i < b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define endl '\n'
#define popb pop_back();
#define se second
#define fi first
#define debug(x) cerr << x << endl
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define fordebug(x) cerr << x << " "

ll power(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  ll temp = power(a, b / 2);
  temp = (temp * temp) % MOD;
  if (b % 2) {
    temp = (temp * a) % MOD;
  }
  return temp;
}

// only use if mod is prime
ll mod_inv(ll a, ll mod) {
  a %= mod;
  return power(a, mod - 2);
}

void extended_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return;
  }
  ll x1, y1;
  extended_gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - ((a / b) * y1);
  return;
}

// TLE can also occur if vectors are not passed by reference

ll dp[55][55][55 * 50], a[55], ans;

int main() {
  // dp[i][j][k] -> #of ways so that we select j elements from first i numbers
  // and get sum k
  int n, A, s = 0;
  cin >> n >> A;
  forn(i, 1, n + 1) {
    cin >> a[i];
    s += a[i];
  }
  dp[0][0][0] = 1;
  forn(i, 1, n + 1) {
    forn(j, 0, i + 1) {
      forn(k, 0, s + 1) {
        dp[i][j][k] = dp[i - 1][j][k]; // we do not take the i'th element
        if (k >= a[i] and j) {
          dp[i][j][k] += dp[i - 1][j - 1][k - a[i]]; // we take the i'th element
        }
      }
    }
  }
  forn(i, 1, n + 1) {
    if (A * i >= 55 * 50) {
      break;
    }
    ans += dp[n][i][A * i];
  }
  cout << ans;
  return 0;
}