simple DP O(n) f(i)=max(f(i+1)+a[i+1]-a[i], 0)