1- import java .io .BufferedReader ;
2- import java .io .InputStreamReader ;
3- import java .util .Arrays ;
1+ import java .io .*;
2+ import java .util .*;
43
54public class Main {
65
76 static int n , ans ;
8- static int [] grape ;
7+ static int [] wine ;
98 static int [][] dp ;
9+
1010 static BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
1111 public static void main (String [] args ) throws Exception {
12- input ( );
13- System . out . println ( recur ( 0 , 0 )) ;
14- }
12+ n = Integer . parseInt ( br . readLine () );
13+ wine = new int [ n ] ;
14+ dp = new int [ n ][ 3 ];
1515
16- public static int recur (int cnt , int seleted ){
17- if ( cnt == n ){
18- return 0 ;
16+ for (int i = 0 ; i < n ; i ++ ){
17+ wine [ i ] = Integer . parseInt ( br . readLine ());
18+ Arrays . fill ( dp [ i ], - 1 ) ;
1919 }
20- if (dp [cnt ][seleted ] != -1 ) return dp [cnt ][seleted ];
21- dp [cnt ][seleted ]= recur (cnt + 1 , 0 );
22- if (seleted < 2 ) dp [cnt ][seleted ] = Math .max (dp [cnt ][seleted ], recur (cnt + 1 , seleted + 1 ) + grape [cnt ]);
23- return dp [cnt ][seleted ];
20+ System .out .println (recur (0 , 0 ));
2421 }
2522
26- public static void input () throws Exception {
27- n = Integer .parseInt (br .readLine ());
28- grape = new int [n ];
29- dp = new int [n ][3 ];
30- for (int i = 0 ; i < n ; i ++) {
31- grape [i ] = Integer .parseInt (br .readLine ());
32- Arrays .fill (dp [i ], -1 );
23+ public static int recur (int cnt , int seq ){
24+ if (cnt == n )return 0 ;
25+ if (dp [cnt ][seq ] != -1 ) return dp [cnt ][seq ];
26+
27+ if (seq < 2 ){
28+ dp [cnt ][seq ] = Math .max (recur (cnt + 1 , seq + 1 ) + wine [cnt ], dp [cnt ][seq ]);
3329 }
30+ return dp [cnt ][seq ] = Math .max (recur (cnt + 1 , 0 ), dp [cnt ][seq ]);
3431 }
35- }
32+ }
0 commit comments