File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ public static void main (String [] args ) throws IOException {
7
+
8
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
9
+ int N = Integer .parseInt (br .readLine ());
10
+ List <Integer > positiveNum = new ArrayList <>();
11
+ List <Integer > negativeNum = new ArrayList <>();
12
+ for (int i =0 ; i <N ; i ++) {
13
+ int num = Integer .parseInt (br .readLine ());
14
+ if (num > 0 ) {
15
+ positiveNum .add (num );
16
+ }else {
17
+ negativeNum .add (num );
18
+ }
19
+ }
20
+ Collections .sort (positiveNum );
21
+ Collections .sort (negativeNum , Collections .reverseOrder ());
22
+ int answer = getMaxBindSum (positiveNum ) + getMaxBindSum (negativeNum );
23
+ System .out .println (answer );
24
+ }
25
+
26
+ public static int getMaxBindSum (List <Integer > list ) {
27
+ int max = 0 ;
28
+ for (int i = list .size ()-1 ; i >= 0 ; ) {
29
+ if (i == 0 ) {
30
+ max += list .get (i );
31
+ break ;
32
+ }
33
+ int multiply = list .get (i ) * list .get (i -1 );
34
+ int plus = list .get (i ) + list .get (i -1 );
35
+ if (multiply < list .get (i )) {
36
+ max += list .get (i --);
37
+ } else if (multiply < plus ) {
38
+ max += plus ;
39
+ i -= 2 ;
40
+ } else {
41
+ max += multiply ;
42
+ i -= 2 ;
43
+ }
44
+ }
45
+ return max ;
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments