#include #include #include void sort(std::vector &vector, int left, int right) { if (left < right) { auto l = left, r = right, pivot = vector.at((left + right) / 2); while (l < r) { while (vector.at(l) > pivot)++l; while (vector.at(r) < pivot)--r; if (l < r) { auto temp = vector.at(l); vector.at(l) = vector.at(r); vector.at(r) = temp; ++l; --r; } sort(vector, left, l), sort(vector, l + 1, right); } } } int main() { int c, n; std::cin >> c >> n; std::vector vector(c + 1, INT_MAX); std::vector arr(n, 0); for (auto &i : arr) { int a; std::cin >> a; i = a; } sort(arr, 0, arr.size() - 1); vector.at(0) = 0; int min = INT_MAX; for (const auto &a : arr) { for (auto i = 0; i < c + 1; ++i) { if (vector.at(i) < min && (i + a <= c) && (vector.at(i + a) > vector.at(i) + 1)) { vector.at(i + a) = vector.at(i) + 1; if ((i + a == c) && (min > vector.at(i + 1))) { min = vector.at(i + 1); } } } } if (vector.at(c) == INT_MAX) { std::cout << -1 << std::endl; } else { std::cout << vector.at(c) << std::endl; } return 0; }