main.cpp: final solution as per algorithm.txt