Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define Max (int)1e5 + 10
#define MAX_INT (int)1e9 + 2
#define MOD ((int)1e9 + 7)
using namespace std;

int dp[Max];

void init() {
  int i;
  for (i = 0; i < Max; i++)
    dp[i] = -1;
}

int canWin(int k, int min, int n, int a[]) {
  if (k == 0)
    return 0;
  if (dp[k] != -1)
    return dp[k];
  int status = 0, i;
  for (i = 0; i < n; i++) {
    if (canWin(k - a[i], min, n, a) == 0 && k - a[i] >= 0)
      status = 1;
  }
  dp[k] = status;
  return dp[k];
}

int main() {
  int n, k, a[Max], min = MAX_INT, i;

  init();
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (min > a[i])
      min = a[i];
  }

  if (canWin(k, min, n, a))
    printf("First\n");
  else
    printf("Second\n");
}

/*
Atcoder Dp G-Longest Path iterative solution

vector<int>vec[Max];
queue<int>q;

int dp[Max];
int vis[Max];

void init(){
        int i;
        for(i=0;i<Max;i++){
                dp[i] = 0;
                vis[i] = 0;
        }
}

int getMax(int a,int b){
        return (a<b)?b:a;
}

void solve(int x){
        int i,u;
        q.push(x);

        while(!q.empty()){
                u = q.front();
                q.pop();

                if(vis[u])
                        continue;
                vis[u] = 1;

                for(i=0;i<vec[u].size();i++){
                        dp[vec[u][i]] = getMax(dp[vec[u][i]],dp[u]+1);
                        q.push(vec[u][i]);
                }
        }

}

int main(){
        int n,m,incoming[Max],x,y,i;
        for(i=0;i<Max;i++)
                incoming[i] = 0;

        init();

        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++){
                scanf("%d%d",&x,&y);
                vec[y].push_back(x);
                incoming[x]++;
        }

        for(i=1;i<=n;i++)
                solve(i);

        int max = 0;
        for(i=0;i<Max;i++)
                max = getMax(max,dp[i]);

        printf("%d\n",max);
        return 0;
}
*/

Fixed submission:
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define Max (int)1e5 + 10
#define MAX_INT (int)1e9 + 2
#define MOD ((int)1e9 + 7)
using namespace std;

int dp[Max];

void init() {
  int i;
  for (i = 0; i < Max; i++)
    dp[i] = -1;
}

int canWin(int k, int min, int n, int a[]) {
  if (k == 0)
    return 0;
  if (dp[k] != -1)
    return dp[k];
  int status = 0, i;
  for (i = 0; i < n; i++) {
    if (k - a[i] >= 0) {
      if (canWin(k - a[i], min, n, a) == 0)
        status = 1;
    }
  }
  dp[k] = status;
  return dp[k];
}

int main() {
  int n, k, a[Max], min = MAX_INT, i;

  init();
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (min > a[i])
      min = a[i];
  }

  if (canWin(k, min, n, a))
    printf("First\n");
  else
    printf("Second\n");
}

/*
Atcoder Dp G-Longest Path iterative solution

vector<int>vec[Max];
queue<int>q;

int dp[Max];
int vis[Max];

void init(){
        int i;
        for(i=0;i<Max;i++){
                dp[i] = 0;
                vis[i] = 0;
        }
}

int getMax(int a,int b){
        return (a<b)?b:a;
}

void solve(int x){
        int i,u;
        q.push(x);

        while(!q.empty()){
                u = q.front();
                q.pop();

                if(vis[u])
                        continue;
                vis[u] = 1;

                for(i=0;i<vec[u].size();i++){
                        dp[vec[u][i]] = getMax(dp[vec[u][i]],dp[u]+1);
                        q.push(vec[u][i]);
                }
        }

}

int main(){
        int n,m,incoming[Max],x,y,i;
        for(i=0;i<Max;i++)
                incoming[i] = 0;

        init();

        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++){
                scanf("%d%d",&x,&y);
                vec[y].push_back(x);
                incoming[x]++;
        }

        for(i=1;i<=n;i++)
                solve(i);

        int max = 0;
        for(i=0;i<Max;i++)
                max = getMax(max,dp[i]);

        printf("%d\n",max);
        return 0;
}
*/