##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1563.html

# 1563. Stone Game V (Hard)

There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

In each round of the game, Alice divides the row into two non-empty rows (i.e. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice's score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away. The next round starts with the remaining row.

The game ends when there is only one stone remaining. Alice's is initially zero.

Return the maximum score that Alice can obtain.

Example 1:

Input: stoneValue = [6,2,3,4,5,5]
Output: 18
Explanation: In the first round, Alice divides the row to [6,2,3], [4,5,5]. The left row has the value 11 and the right row has value 14. Bob throws away the right row and Alice's score is now 11.
In the second round Alice divides the row to [6], [2,3]. This time Bob throws away the left row and Alice's score becomes 16 (11 + 5).
The last round Alice has only one choice to divide the row which is [2], [3]. Bob throws away the right row and Alice's score is now 18 (16 + 2). The game ends because only one stone is remaining in the row.


Example 2:

Input: stoneValue = [7,7,7,7,7,7,7]
Output: 28


Example 3:

Input: stoneValue = [4]
Output: 0


Constraints:

• 1 <= stoneValue.length <= 500
• 1 <= stoneValue[i] <= 10^6

Related Topics:
Dynamic Programming

Similar Questions:

## TLE version: Bottom-up DP

The bottom-up version DP will get TLE because it go through every cases, while the top-down DP skip some cases.

// OJ: https://leetcode.com/problems/stone-game-v/
// Time: O(N^3)
// Space: O(N^2)
class Solution {
public:
int stoneGameV(vector<int>& A) {
int N = A.size();
vector<vector<int>> dp(N + 1, vector<int>(N + 1));
vector<int> sum(N + 1);
for (int i = 0; i < N; ++i) sum[i + 1] = sum[i] + A[i];
for (int len = 2; len <= N; ++len)  {
for (int i = 0; i <= N - len; ++i) {
int j = i + len - 1, val = 0;
for (int k = i; k < j; ++k) {
int left = sum[k + 1] - sum[i], right = sum[j + 1] - sum[k + 1];
int L = dp[i][k] + left, R = dp[k + 1][j] + right;
if (left < right) val = max(val, L);
else if (right < left) val = max(val, R);
else val = max(val, max(L, R));
}
dp[i][j] = val;
}
}
return dp[0][N - 1];
}
};


## Solution 1. Top-down DP

// OJ: https://leetcode.com/problems/stone-game-v/
// Time: O(N^3)
// Space: O(N^2)
class Solution {
int dp[501][501] = {}, N;
vector<int> sum;
int dfs(vector<int> &A, int i, int j){
if (i == j) return 0;
if (dp[i][j]) return dp[i][j];
int val = 0;
for (int k = i; k < j; ++k) {
int left = sum[k + 1] - sum[i], right = sum[j + 1] - sum[k + 1];
if (left < right) val = max(val, dfs(A, i, k) + left);
else if (right < left) val = max(val, dfs(A, k + 1, j) + right);
else val = max({ val, dfs(A, i, k) + left, dfs(A, k + 1, j) + right });
}
return dp[i][j] = val;
}
public:
int stoneGameV(vector<int>& A) {
N = A.size();
sum.assign(N + 1, 0);
for (int i = 0; i < N; ++i) sum[i + 1] = sum[i] + A[i];
return dfs(A, 0, N - 1);
}
};

• class Solution {
public int stoneGameV(int[] stoneValue) {
int length = stoneValue.length;
int[] prefix = new int[length];
prefix[0] = stoneValue[0];
for (int i = 1; i < length; i++)
prefix[i] = prefix[i - 1] + stoneValue[i];
int[][] dp = new int[length][length];
for (int i = length - 2; i >= 0; i--) {
for (int j = i + 1; j < length; j++) {
for (int k = i; k < j; k++) {
int score1 = getSubarraySum(prefix, i, k);
int score2 = getSubarraySum(prefix, k + 1, j);
int curScore = 0;
if (score1 <= score2)
curScore = Math.max(curScore, score1 + dp[i][k]);
if (score1 >= score2)
curScore = Math.max(curScore, score2 + dp[k + 1][j]);
dp[i][j] = Math.max(dp[i][j], curScore);
}
}
}
return dp[0][length - 1];
}

public int getSubarraySum(int[] prefix, int start, int end) {
if (start == 0)
return prefix[end];
else
return prefix[end] - prefix[start - 1];
}
}

############

class Solution {
private int n;
private int[] s;
private int[] stoneValue;
private Integer[][] f;

public int stoneGameV(int[] stoneValue) {
n = stoneValue.length;
this.stoneValue = stoneValue;
s = new int[n + 1];
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + stoneValue[i - 1];
}
f = new Integer[n][n];
return dfs(0, n - 1);
}

private int dfs(int i, int j) {
if (i == j) {
return 0;
}
if (f[i][j] != null) {
return f[i][j];
}
int ans = 0;
int a = 0;
for (int k = i; k < j; ++k) {
a += stoneValue[k];
int b = s[j + 1] - s[i] - a;
if (a < b) {
if (ans >= a * 2) {
continue;
}
ans = Math.max(ans, a + dfs(i, k));
} else if (a > b) {
if (ans >= b * 2) {
break;
}
ans = Math.max(ans, b + dfs(k + 1, j));
} else {
ans = Math.max(ans, Math.max(a + dfs(i, k), b + dfs(k + 1, j)));
}
}
return f[i][j] = ans;
}
}

• // OJ: https://leetcode.com/problems/stone-game-v/
// Time: O(N^3)
// Space: O(N^2)
class Solution {
public:
int stoneGameV(vector<int>& A) {
int N = A.size();
vector<vector<int>> dp(N + 1, vector<int>(N + 1));
vector<int> sum(N + 1);
for (int i = 0; i < N; ++i) sum[i + 1] = sum[i] + A[i];
for (int len = 2; len <= N; ++len)  {
for (int i = 0; i <= N - len; ++i) {
int j = i + len - 1, val = 0;
for (int k = i; k < j; ++k) {
int left = sum[k + 1] - sum[i], right = sum[j + 1] - sum[k + 1];
int L = dp[i][k] + left, R = dp[k + 1][j] + right;
if (left < right) val = max(val, L);
else if (right < left) val = max(val, R);
else val = max(val, max(L, R));
}
dp[i][j] = val;
}
}
return dp[0][N - 1];
}
};

• class Solution:
def stoneGameV(self, stoneValue: List[int]) -> int:
@cache
def dfs(i, j):
if i == j:
return 0
ans = a = 0
for k in range(i, j):
a += stoneValue[k]
b = s[j + 1] - s[i] - a
if a < b:
if ans >= a * 2:
continue
ans = max(ans, a + dfs(i, k))
elif a > b:
if ans >= b * 2:
break
ans = max(ans, b + dfs(k + 1, j))
else:
ans = max(ans, a + dfs(i, k), b + dfs(k + 1, j))
return ans

s = list(accumulate(stoneValue, initial=0))
return dfs(0, len(stoneValue) - 1)


• func stoneGameV(stoneValue []int) int {
n := len(stoneValue)
s := make([]int, n+1)
for i, x := range stoneValue {
s[i+1] = s[i] + x
}
f := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
}
var dfs func(i, j int) int
dfs = func(i, j int) int {
if i == j {
return 0
}
if f[i][j] != 0 {
return f[i][j]
}
ans, a := 0, 0
for k := i; k < j; k++ {
a += stoneValue[k]
b := s[j+1] - s[i] - a
if a < b {
if ans >= a*2 {
continue
}
ans = max(ans, a+dfs(i, k))
} else if a > b {
if ans >= b*2 {
break
}
ans = max(ans, b+dfs(k+1, j))
} else {
ans = max(ans, max(a+dfs(i, k), b+dfs(k+1, j)))
}
}
f[i][j] = ans
return ans
}
return dfs(0, n-1)
}

func max(a, b int) int {
if a > b {
return a
}
return b
}