#include using namespace std; template Value unboundedKnapsack(Weight maxWeight, const vector& weight, const vector& value) { constexpr Value IMP = numeric_limits::min(); vector dp(maxWeight + Weight(1)); if (strict) fill(dp.begin() + 1, dp.end(), IMP); for (size_t i = 0; i < weight.size(); ++i) { for (int w = 0; w <= maxWeight; ++w) { if (strict && dp[w] == IMP) continue; Weight ww = Weight(w) + weight[i]; Value vv = dp[w] + value[i]; if (ww <= maxWeight && dp[ww] < vv) dp[ww] = vv; } } return dp[maxWeight]; } template vector knapsackCount(Weight maxWeight, const vector& weight) { vector dp(maxWeight + Weight(1)); dp[0] = 1; for (auto& w : weight) { for (int i = 0; i <= maxWeight; ++i) { Weight ww = Weight(i) + w; if (ww <= maxWeight) dp[ww] += dp[i]; } } return dp; } template vector unboundedKnapsackFill(Weight maxWeight, const vector& weight) { vector dp(maxWeight + Weight(1)); dp[0] = true; for (auto& w : weight) { for (int i = 0; i <= maxWeight; ++i) { Weight ww = Weight(i) + w; if (ww <= maxWeight && dp[i]) dp[ww] = true; } } return dp; } int main() { int c, n; cin >> c >> n; vector w(n), v(n, -1); for (int& i : w) cin >> i; auto res = unboundedKnapsack(c, w, v); cout << (res != numeric_limits::min() ? -res : -1) << endl; }