SRM 314 DIV 2 hard

memoization dynamic