#include <algorithm> #include <iostream> #include <map> #include <vector> int main() { int32_t n; std::cin >> n; std::vector<int32_t> packages(n); for (int32_t i = 0; i < n; ++i) { std::cin >> packages[i]; } std::sort(packages.begin(), packages.end()); std::map<int32_t, int32_t> options; options[0] = 1; constexpr int32_t big_prime = 1'000'000'007; int32_t result = 0; for (auto package: packages) { options.erase(options.begin(), options.lower_bound(package - 1)); if (options.empty()) { break; } for (auto it = options.rbegin(); it != options.rend(); ++it) { auto &target = options[it->first + package]; // docs say it's legal ¯\_(ツ)_/¯ result = (it->second + result) % big_prime; target = (it->second + target) % big_prime; } } std::cout << result << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <algorithm> #include <iostream> #include <map> #include <vector> int main() { int32_t n; std::cin >> n; std::vector<int32_t> packages(n); for (int32_t i = 0; i < n; ++i) { std::cin >> packages[i]; } std::sort(packages.begin(), packages.end()); std::map<int32_t, int32_t> options; options[0] = 1; constexpr int32_t big_prime = 1'000'000'007; int32_t result = 0; for (auto package: packages) { options.erase(options.begin(), options.lower_bound(package - 1)); if (options.empty()) { break; } for (auto it = options.rbegin(); it != options.rend(); ++it) { auto &target = options[it->first + package]; // docs say it's legal ¯\_(ツ)_/¯ result = (it->second + result) % big_prime; target = (it->second + target) % big_prime; } } std::cout << result << "\n"; } |