Stock Maximize

qstn: Stock Maximize | HackerRank

I am new to DP and recursion.Can anyone help me explaining how to solve this qstn by recursion(so that i can approach with DP).plz explain and give the pseudo code as i am new to this topic

Here is my solution:
#include<bits/stdc++.h>

using namespace std;

int main()
{
int t,n,i;
cin>>t;
while(t–)
{cin>>n;
vector a(n);
for(i=0;i<n;i++)
{cin>>a[i];
}
long long ans=0, max=0;
for(i=n-1;i>=0;i–)
{
if(a[i]>max)
max=a[i];
else
ans+=max-a[i];
}
cout<<ans<<endl;

 }

}

Logic:
Start from the rear. If current value is smaller than current maximum, then sell at that point(add diff to answer). Otherwise make this the current maximum.