KMAP upto 3 variable in C++ using greedy approach