classSolution{publiclonggridGame(int[][] grid){// Use long to prevent sum up overflowlong[] prefix_top =newlong[grid[0].length];long[] prefix_bottom =newlong[grid[0].length];// Copy top row and bottom rowfor(int i =0; i < grid[0].length;++i){
prefix_top[i]= grid[0][i];
prefix_bottom[i]= grid[1][i];}// Calculate prefix sumfor(int i =1; i < grid[0].length;++i){
prefix_top[i]+= prefix_top[i -1];
prefix_bottom[i]+= prefix_bottom[i -1];}long res =Long.MAX_VALUE;long robot2 =Long.MIN_VALUE;// Go through the grid, find the optimalfor(int i =0; i < grid[0].length;++i){// Stays on top or bottom// chose by robot1, robot2 only has the remaininglong top = prefix_top[grid[0].length -1]- prefix_top[i];long bottom =0;// In case out of boundif(i >0){
bottom = prefix_bottom[i -1];}// Robot2 tries to get as many points as possible
robot2 =Math.max(top, bottom);// Obtain robot2's lowest points
res =Math.min(res, robot2);}return res;}}