Ancient go implementation on spoj :(