spoj/EDIST solutions in C++ and Ruby