Code for problem 18D.
WA on test #5.
MLE on test #22.
MLE on test #22.
TLE on test #30.
Code for problem 17D