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

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;
bool dp[1000001];
bool pn[1000001];

void eratos(bool f[]) {
  for (int i = 0; i < 1000001; ++i) {
    f[i] = true;
  }
  pn[0] = false;
  pn[1] = false;
  for (int i = 2; i * i < 1000001; ++i) {
    if (f[i]) {
      for (int j = 2; i * j < 1000001; ++j) {
        f[i * j] = false;
      }
    }
  }
}
int main() {
  int n, x;
  eratos(pn);
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    vector<int> cost(n);
    for (int i = 0; i <= x; ++i) {
      dp[i] = false;
    }
    for (int i = 0; i < n; ++i) {
      cin >> cost[i];
      dp[cost[i]] = true;
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= x; ++j) {
        if (dp[j] && j + cost[i] <= x) {
          dp[j + cost[i]] = true;
        }
      }
    }
    bool flag = false;
    int ans;
    for (int i = x; i >= 0; --i) {
      if (dp[i] && pn[i]) {
        flag = true;
        ans = i;
        break;
      }
    }
    if (flag) {
      cout << ans << "\\n";
    } else {
      cout << "NA\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;
bool dp[30][1000001];
bool pn[1000001];

void eratos(bool f[]) {
  for (int i = 0; i < 1000001; ++i) {
    f[i] = true;
  }
  pn[0] = false;
  pn[1] = false;
  for (int i = 2; i * i < 1000001; ++i) {
    if (f[i]) {
      for (int j = 2; i * j < 1000001; ++j) {
        f[i * j] = false;
      }
    }
  }
}
int main() {
  int n, x;
  eratos(pn);
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;
    vector<int> cost(n);
    for (int i = 0; i < n; ++i) {
      cin >> cost[i];
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= x; ++j) {
        dp[i][j] = false;
      }
    }
    for (int i = 0; i * cost[0] <= x; ++i) {
      dp[0][cost[0] * i] = true;
    }
    for (int i = 1; i < n; ++i) {
      for (int j = 0; j <= x; ++j) {
        if (dp[i - 1][j]) {
          for (int k = 0; j + cost[i] * k <= x;) {
            dp[i][j + cost[i] * k] = true;
            ++k;
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
            if (j + cost[i] * k <= x) {
              dp[i][j + cost[i] * k] = true;
              ++k;
            }
          }
        }
      }
    }
    bool flag = false;
    int ans;
    for (int i = x; i >= 0; --i) {
      if (dp[n - 1][i] && pn[i]) {
        flag = true;
        ans = i;
        break;
      }
    }
    if (flag) {
      cout << ans << "\\n";
    } else {
      cout << "NA\\n";
    }
  }
  return 0;
}