Cses Weird Algorithm Solution