File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ class RemovalGame {
5
+ public static void main (String [] args ) throws IOException
6
+ {
7
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
8
+ int n = Integer .parseInt (br .readLine ());
9
+ String s [] = br .readLine ().split (" " );
10
+ int a [] = new int [n ];
11
+ for (int i = 0 ; i < n ; i ++) {
12
+ a [i ] = Integer .parseInt (s [i ]);
13
+ }
14
+ long dp [][] = new long [n ][n ];
15
+ for (int g = 0 ; g < n ; g ++) {
16
+ for (int i = 0 , j = g ; j < n ; i ++, j ++) {
17
+ if (g == 0 ) {
18
+ dp [i ][j ] = a [i ];
19
+ } else if (g == 1 ) {
20
+ dp [i ][j ] = Math .max (a [i ], a [j ]);
21
+ } else {
22
+ long val1 = Math .min (dp [i +1 ][j -1 ], dp [i +2 ][j ]) + a [i ];
23
+ long val2 = Math .min (dp [i ][j -2 ], dp [i +1 ][j -1 ]) + a [j ];
24
+ dp [i ][j ] = Math .max (val1 , val2 );
25
+ }
26
+ }
27
+ }
28
+ System .out .println (dp [0 ][n -1 ]);
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments