Try to understand that starting case for j>i case is i=0 . You can run the loop till 2*n and do %n for making one solution work for both cases. Now all we have to worry about is a[i],b[i], and the sum so far.

Elaborated below:

We know the sum so far consists of an element from array a and maybe some from b (after the index of the element of array a) , so first we can check max(sumsofar+a[i],maxsofar) and then we can simply check best case for next element in array b or a that is either we take a[i] and set sumsofar to a[i] or add b[i] in sumsofar ,so we add a[i] if a[i] is greater than sumsofar+b[i] else we just add b[i].

These are the basic cases I thought of for i<n which will also work for the wrapping around case only with one catch. Try to figure the issue yourself.

See the next hint only if you've tried and failed.

Check length of sumsofar, maintain starting index. Make sure it doesn't exceed the length of array.