import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class kie { public static void debugPrint(List<Integer> in) { for(int e : in) { System.out.print(e + ","); } System.out.println(); } public static int sum(List<Integer> in) { int sum = 0; for (int e : in ) { sum += e; } return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> even = new ArrayList<Integer>(); List<Integer> odd = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { int k = sc.nextInt(); if (k % 2 == 0) { even.add(k); } else { odd.add(k); } } Collections.sort(even); Collections.sort(odd); Collections.reverse(odd); if (odd.size() == 1 && even.size() == 0) { System.out.println("NIESTETY"); return; } int sumEven = sum(even); if (odd.size() % 2 == 1) { odd.remove(odd.size() - 1); } int sumOdd = sum(odd); System.out.println(sumEven + sumOdd); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class kie { public static void debugPrint(List<Integer> in) { for(int e : in) { System.out.print(e + ","); } System.out.println(); } public static int sum(List<Integer> in) { int sum = 0; for (int e : in ) { sum += e; } return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> even = new ArrayList<Integer>(); List<Integer> odd = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { int k = sc.nextInt(); if (k % 2 == 0) { even.add(k); } else { odd.add(k); } } Collections.sort(even); Collections.sort(odd); Collections.reverse(odd); if (odd.size() == 1 && even.size() == 0) { System.out.println("NIESTETY"); return; } int sumEven = sum(even); if (odd.size() % 2 == 1) { odd.remove(odd.size() - 1); } int sumOdd = sum(odd); System.out.println(sumEven + sumOdd); } } |