Doubt in memoization

Good Morning…
I am trying to solve this question from codeforces…
https://codeforces.com/problemset/problem/1285/B
I have writeen the recursive code.But i am having doubt in memoization. Like how should i memoize this code… Will it not give Memory limit exceeded as array size is 10**5
This is the code… i have written…
https://codeforces.com/contest/1285/submission/96487750