cho e solution bài này với

 

Bạn có thể tham khảo thuật toán, code tại: http://yeulaptrinh.pw/81/spoj-crec01/