hdu 1024. DP. good question. the optimization for AC is a little tricky.