#include <iostream>
#include <algorithm>
#include <functional>

template<class RandIt, class Compare>
bool next_k_permutation(RandIt first, RandIt mid, RandIt last, Compare comp)
{
    std::sort(mid, last, [=](auto a, auto b){ return comp(b,a); });
    return std::next_permutation(first, last, comp);
}

template<class RandIt>
bool next_k_permutation(RandIt first, RandIt mid, RandIt last)
{
    return next_k_permutation(first, mid, last,std::less<>());
}

int main()
{
    //         B M W Z A O T
    int v[] = {0,1,2,3,4,5,6,7,8,9};
    do {
        if (((v[0]+v[3]-v[5])*10+v[1]+v[4]-v[6])*10+v[2]+v[3]-v[5] == 1000*v[1])
            std::cout
                << v[0] << v[1] << v[2] << "+"
                << v[3] << v[4] << v[3] << "="
                << v[1] << v[5] << v[6] << v[5] << "\n";
    } while(next_k_permutation(v,v+7,v+10));
}
