Cses Problem Set Weird Algorithm