AC codeforce 162_2 E, spend the whole day, need think clear if within loop