Q:

Return the maximum from getting the left or right value of an array

{1,2,3,4,1,2,3,4,1}

I have to multiply the index by the value inside of that inside. I can only take a value from the left or right of the array.
For example:
If I take the number from the left ONLY, I'd have 1x1 + 2x2 + 3x3 + 4x4+ 1x5 + 2x6 + 3*7 + 4x8 + 1x9 = 109 points.
If I take the number from the right end each time, I'd have 1x1+4x2+3x3+2x4+1x5+4x6+3x7+2x8+1x9 = 101 points
If I alternated eating from the left and right ends (starting on the left), I'd score 1x1+1x2+2x3+4x4+3x5+3x6+4x7+2x8+1x9 = 111 points 
However, the correct solution is 
1x1+1x2+2x3+3x4+4x5+1x6+2x7+3x8+4x9 = 121 points 

My code is super ugly, but can anyone help me with it?
public class CandyRoll {
    static int total = 0;
    static int test (List<Integer> list, int index, int multiplier) {
        //Base Case
        if (list.size() < 1) { return -1;}

        if ((list.get(index) * multiplier) == (list.get(list.size() - 1) * multiplier)){
            total = total + list.get(index) * multiplier;
            multiplier++;
            //list.remove(index);
            index++;
            test(list, index, multiplier);
        } else if ((list.get(index) * multiplier) < (list.get(list.size() - 1) * multiplier)) {
            total = total + list.get(index) * multiplier;
            multiplier++;
            //list.remove(index);
            index++;
            test(list, index, multiplier);
        } else if ((list.get(index) * multiplier) > (list.get(list.size() - 1) * multiplier)) {
            total = total + list.get(list.size() - 1) * multiplier;
            multiplier++;
            //list.remove(list.size() - 1);
            index++;
            test(list, index, multiplier);
        }

        //Given example should be 121.
        return total;
    }
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(1);

        int index = 0;

        System.out.println(test(list, index, 1));
    }

}

A:

Here's a recursion in JavaScript:

function f(A, l=0, r=A.length-1, memo={}){
  if (memo.hasOwnProperty([l, r]))
    return memo[[l, r]];

  const i = A.length - (r - l);

  if (l == r)
    return memo[[l, r]] = i * A[l];
  
  return memo[[l, r]] = Math.max(
    i * A[l] + f(A, l + 1, r, memo),
    i * A[r] + f(A, l, r - 1, memo)
  );
}

let A = [1,2,3,4,1,2,3,4,1];
console.log(f(A));

