accepted 506
aoj 508 solution