Problem p00202 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>

using namespace std;

const int MAX_N = 1000000;
bool prime[MAX_N + 1];

void make_prime(void) {
  fill(prime, prime + MAX_N, true);

  prime[0] = false;
  prime[1] = false;

  for (int i = 2; i < MAX_N + 1; ++i) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX_N + 1; j += i) {
        prime[j] = false;
      } // end for
    }   // end if
  }     // end for
}

bool dp[MAX_N + 1];

int main() {
  //	cut here before submit
  //	freopen ("testcase.be", "r", stdin );
  int n, x;

  make_prime();

  while (cin >> n >> x && n && x) {
    vector<int> cost(n, 0);
    for (int i = 0; i < n; ++i) {
      cin >> cost[i];
    } // end for
    memset(dp, false, sizeof(dp));

    dp[0] = true;
    for (int i = 0; i < n; ++i) {
      for (int j = cost[i]; j <= x; ++j) {
        dp[j] |= dp[j - cost[i]];
      } // end if
    }   // end for

    int res = 0;
    for (int j = MAX_N; j >= 0; --j) {
      if (dp[j] && prime[j]) {
        res = j;
        break;
      } // end if
    }   // end for

    if (res == 0) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    } // end if
  }   // end loop

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MAX_N = 1000000;
bool prime[MAX_N + 1];

void make_prime(void) {
  fill(prime, prime + MAX_N, true);

  prime[0] = false;
  prime[1] = false;

  for (int i = 2; i < MAX_N + 1; ++i) {
    if (prime[i]) {
      for (int j = 2 * i; j < MAX_N + 1; j += i) {
        prime[j] = false;
      } // end for
    }   // end if
  }     // end for
}

int dp[31][MAX_N + 1];

int main() {
  //	cut here before submit
  //	freopen ("testcase.be", "r", stdin );
  int n, x;

  make_prime();

  while (cin >> n >> x && n && x) {
    vector<int> cost(n, 0);
    for (int i = 0; i < n; ++i) {
      cin >> cost[i];
    } // end for
    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= x; ++j) {
        if (j < cost[i]) {
          dp[i + 1][j] = dp[i][j];
        } else {
          dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - cost[i]] + cost[i]);
        } // end if
      }   // end for
    }     // end for

    int res = 0;
    for (int j = 0; j <= MAX_N; ++j)
      if (prime[dp[n][j]])
        res = max(res, dp[n][j]);

    if (res == 0) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    } // end if
  }   // end loop

  return 0;
}