import java.util.Arrays; import java.util.Scanner; public class kie { public static void main(String... args) { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int noOfNotes = Integer.parseInt(scanner.nextLine()); int[] notes = new int[noOfNotes]; long sum = 0; String secondLine = scanner.nextLine(); String[] notesString = secondLine.split(" "); for (int i = 0; i < notesString.length; i++) { int note = Integer.parseInt(notesString[i]); notes[i] = note; sum = sum + note; } Arrays.sort(notes); long result = 0; while (result == 0) { result = findNewSum(notes, sum, -1); if (result == -1) { System.out.println("NIESTETY"); return; } } System.out.println(result); } private static int findIgnoredIdx(int ignoredIdx, int[] notes) { for (int i = ignoredIdx + 1; i < notes.length; i++) { if (notes[i] % 2 != 0) { ignoredIdx = i; break; } } return ignoredIdx; } private static long findNewSum(int[] notes, long sum, int ignoredIdx) { long newSum = 0; if (sum % 2 != 0) { ignoredIdx = findIgnoredIdx(ignoredIdx, notes); // System.out.println("Ignored value: " + notes[ignoredIdx]); newSum = (sum - notes[ignoredIdx]); // System.out.println("New sum: " + newSum); if (newSum == 0) { return -1; } } else { newSum = sum; } return newSum > 0 ? newSum : -1; } }
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 66 67 68 | import java.util.Arrays; import java.util.Scanner; public class kie { public static void main(String... args) { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int noOfNotes = Integer.parseInt(scanner.nextLine()); int[] notes = new int[noOfNotes]; long sum = 0; String secondLine = scanner.nextLine(); String[] notesString = secondLine.split(" "); for (int i = 0; i < notesString.length; i++) { int note = Integer.parseInt(notesString[i]); notes[i] = note; sum = sum + note; } Arrays.sort(notes); long result = 0; while (result == 0) { result = findNewSum(notes, sum, -1); if (result == -1) { System.out.println("NIESTETY"); return; } } System.out.println(result); } private static int findIgnoredIdx(int ignoredIdx, int[] notes) { for (int i = ignoredIdx + 1; i < notes.length; i++) { if (notes[i] % 2 != 0) { ignoredIdx = i; break; } } return ignoredIdx; } private static long findNewSum(int[] notes, long sum, int ignoredIdx) { long newSum = 0; if (sum % 2 != 0) { ignoredIdx = findIgnoredIdx(ignoredIdx, notes); // System.out.println("Ignored value: " + notes[ignoredIdx]); newSum = (sum - notes[ignoredIdx]); // System.out.println("New sum: " + newSum); if (newSum == 0) { return -1; } } else { newSum = sum; } return newSum > 0 ? newSum : -1; } } |