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