Tap here to switch tabs
Problem
Submissions

Problem: Stone Game VIII

Stone Game VIII has Alice and Bob repeatedly merge at least two leftmost stones, scoring their sum, until one remains. Return the score difference under optimal play; stone values may be negative.

Statement

Alice and Bob play a game using an array of integers stones arranged in a line from left to right.

Alice plays first. On each turn, the active player chooses an integer x>1x > 1, removes the leftmost xx stones from the row, calculates the sum of their values, adds this sum to their score, and places a single new stone with a value equal to this sum at the leftmost position of the row.

The game concludes when only 11 stone remains in the row.

Alice seeks to maximize the difference between her score and Bob’s score, whereas Bob seeks to minimize this difference.

Assuming both players play optimally, return the resulting score difference between Alice and Bob.

Constraints:

  • n ==== stones.length

  • 2≤2 \leq n ≤105\leq 10^5

  • −104≤-10^4 \leq stones[i] ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Stone Game VIII

Stone Game VIII has Alice and Bob repeatedly merge at least two leftmost stones, scoring their sum, until one remains. Return the score difference under optimal play; stone values may be negative.

Statement

Alice and Bob play a game using an array of integers stones arranged in a line from left to right.

Alice plays first. On each turn, the active player chooses an integer x>1x > 1, removes the leftmost xx stones from the row, calculates the sum of their values, adds this sum to their score, and places a single new stone with a value equal to this sum at the leftmost position of the row.

The game concludes when only 11 stone remains in the row.

Alice seeks to maximize the difference between her score and Bob’s score, whereas Bob seeks to minimize this difference.

Assuming both players play optimally, return the resulting score difference between Alice and Bob.

Constraints:

  • n ==== stones.length

  • 2≤2 \leq n ≤105\leq 10^5

  • −104≤-10^4 \leq stones[i] ≤104\leq 10^4