Problem p03170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_mset                                                           \
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \
       tree_order_statistics_node_update>
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); --i)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int, int>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<int>())
#define sz(a) (int)a.size()
#define endl '\n'
#define po(a, b) (ll)(pow(a, b) + 0.5)
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl;
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const ll M = 1e9 + 7;
const int MX = 1e5 + 5;

int n, k;
int dp[MX];
int val[MX];
int a[101];
int mn;
int solve(int num) {
  if (val[num])
    return 1;
  if (num < a[0])
    return 0;
  if (dp[k] != -1)
    return dp[k];
  int c = 0;
  fr(i, 0, n) {
    if (a[i] < num) {
      if (dp[num - a[i]] != -1 && c == 0)
        c = 1 - dp[num - a[i]];
      else if (solve(num - a[i]) == 0)
        c = 1;
    } else
      break;
  }
  return dp[num] = c;
}

int main() {
  Fast cin >> n >> k;
  memset(dp, -1, sizeof dp);
  memset(val, 0, sizeof val);

  fr(i, 0, n) {
    cin >> a[i];
    val[a[i]] = 1;
  }
  if (solve(k))
    cout << "First";
  else
    cout << "Second";
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_mset                                                           \
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \
       tree_order_statistics_node_update>
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); --i)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int, int>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<int>())
#define sz(a) (int)a.size()
#define endl '\n'
#define po(a, b) (ll)(pow(a, b) + 0.5)
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl;
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const ll M = 1e9 + 7;
const int MX = 1e5 + 5;

int n, k;
int dp[MX];
int val[MX];
int a[101];
int mn;
int solve(int num) {
  if (val[num])
    return 1;
  if (num < a[0])
    return 0;
  if (dp[k] != -1)
    return dp[k];
  int c = 0;
  fr(i, 0, n) {
    if (a[i] < num) {
      if (dp[num - a[i]] != -1) {
        if (c == 0)
          c = 1 - dp[num - a[i]];
      } else if (solve(num - a[i]) == 0)
        c = 1;
    } else
      break;
  }
  return dp[num] = c;
}

int main() {
  Fast cin >> n >> k;
  memset(dp, -1, sizeof dp);
  memset(val, 0, sizeof val);

  fr(i, 0, n) {
    cin >> a[i];
    val[a[i]] = 1;
  }
  if (solve(k))
    cout << "First";
  else
    cout << "Second";
}