#include #include #include template std::pair copy_while(InputIt first, InputIt last, OutputIt out, Pred p); std::vector take_while_sum_less_than(std::vector &v, int n) { std::vector out; int acc = 0; while (true) { if (v.back() + acc > n) { int next = v.back(); out.push_back(n); v.pop_back(); } } } #if __cplusplus > 202002L // #include // #include std::vector take_while_sum_less_than(const std::vector &v, int n) { int sum = 0; auto result = v | std::ranges::views::take_while([&sum, n](int x) { if (sum + x < n) { sum += x; return true; } return false; }); return std::vector(result.begin(), result.end()); } #endif