Recursive Brute-Force
if it really has to be brute force solution, then you have to check all possible combinations of take first/take last item for both players

you know that there are 4 items, so every player takes two of them, combinations are (for each player):

take first (0), take first (0)
take first (0), take last  (1)
take last  (1), take first (0)
take last  (1), take last  (1)


as you can see, numbers are similar to binary numbers equal to 0-3(dec)

second player should do the same in an inner loop for every iteration of first player (outer loop)

pseudo code would be:

ArrayList &lt;Integer&gt; boxes = {5, 3, 7, 10}

boolean shouldTakeFirst(int combination, int iteration) {
    return (combination &amp; (1&lt;&lt;iteration)) &gt; 0; // checks if i-th bit of combination is "1"
}

int player1Score(int combinationP1, int combination P2) {
    int score = 0;
    for (int i=0; i&lt;boxes.size()/2; i++) {
        score += shouldTakeFirst(combinationP1, i) ? boxes.popFirst() : boxes.popLast(); // assuming that .popFirst() and .popLast() remove elements after taking them
        shouldTakeFirst(combinationP2, i) ? boxes.first() : boxes.last();
    }
    return score;
}

for (int i=0; i &lt; Math.pow(2, boxes.size()/2); i++) {
    for (int j=0; j &lt; Math.pow(2, boxes.size()/2); j++) {
        int p1Score = player1Score(i, j);
        int p2Score = sum(boxes) - p1Score;

        // check if scores better than max and remember/print it
    }
}