#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #undef max #undef min #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #endif #ifdef _DEBUG #define ASSERT(x) if(!(x)) __debugbreak() #else char *crash_please=(char *)42; #define ASSERT(x) //if(!(x)) { printf("%s failed",#x); *crash_please=33; } #endif #include class cLogPerformance_Guard { std::chrono::time_point mStartTime=std::chrono::high_resolution_clock::now(); const char *mName; public: cLogPerformance_Guard(const char *Name): mName(Name) {} ~cLogPerformance_Guard() { auto EndTime=std::chrono::high_resolution_clock::now(); auto Elapsed=std::chrono::duration_cast(EndTime-mStartTime); // printf("--- Elapsed %llu ms in %s ---\n", (unsigned long long)Elapsed.count(), mName); } }; using namespace std; using namespace std::string_literals; using ull=unsigned long long; using ll=long long; constexpr ll mod=1'000'000'007; template auto rev(I i) { return std::reverse_iterator(i); } #define RI(var_name) int var_name; scanf("%d", &var_name); #define RIV(var_name, size) vector var_name(size); for(auto &item_of_##var_name: var_name) scanf("%d", &item_of_##var_name); #define RII(var_name1, var_name2) int var_name1, var_name2; scanf("%d %d", &var_name1, &var_name2); #define RIII(var_name1, var_name2, var_name3) int var_name1, var_name2, var_name3; scanf("%d %d %d", &var_name1, &var_name2, &var_name3); #define RIIII(var_name1, var_name2, var_name3, var_name4) int var_name1, var_name2, var_name3, var_name4; scanf("%d %d %d %d", &var_name1, &var_name2, &var_name3, &var_name4); #define RL(var_name) ll var_name; scanf("%lld", &var_name); #define RLV(var_name, size) vector var_name(size); for(auto &item_of_##var_name: var_name) scanf("%lld", &item_of_##var_name); #define RLL(var_name1, var_name2) ll var_name1, var_name2; scanf("%lld %lld", &var_name1, &var_name2); #define RLLL(var_name1, var_name2, var_name3) ll var_name1, var_name2, var_name3; scanf("%lld %lld %lld", &var_name1, &var_name2, &var_name3); #define RLLLL(var_name1, var_name2, var_name3, var_name4) ll var_name1, var_name2, var_name3, var_name4; scanf("%lld %lld %lld %lld", &var_name1, &var_name2, &var_name3, &var_name4); #define RD(var_name) double var_name; scanf("%lf", &var_name); #define RDV(var_name, size) vector var_name(size); for(auto &item_of_##var_name: var_name) scanf("%d", &item_of_##var_name); #define RDD(var_name1, var_name2) double var_name1, var_name2; scanf("%lf %lf", &var_name1, &var_name2); #define RDDD(var_name1, var_name2, var_name3) double var_name1, var_name2, var_name3; scanf("%lf %lf %lf", &var_name1, &var_name2, &var_name3); #define ALL(cont) cont.begin(), cont.end() #define FOR(var, max_value) for(int var=0;var a; void Solve() { RII(n, k); a.resize(n); int all_and=~0; int all_or=0, all_max=numeric_limits::min(), all_min=numeric_limits::max(); for(auto &x: a) { scanf("%d", &x); all_and&=x; all_or|=x; all_max=max(all_max, x); all_min=min(all_min, x); } int best_or_and=all_or-all_and; vector best(n, 0); for(int from=0; from=k) last_place_cost_above=i; int how_much_it_can_grow=best_or_and-(r_or-r_and); if(cost+how_much_it_can_grow