import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class kie { public static void main(String[] args) { try (BufferedReader Console = new BufferedReader(new InputStreamReader(System.in))) { int Length = Integer.parseInt(Console.readLine()); int[] Cash = ReadIntArrayFromConsole(Console, Length); CheckCoins(Cash); } catch (IOException e) { e.printStackTrace(); } } private static void CheckCoins(int[] Cash) { long CashTotal = 0; for (int CurrentIndex = 0; CurrentIndex < Cash.length; CurrentIndex++) { CashTotal = CashTotal + Cash[CurrentIndex]; } long CashMax; for (CashMax = CashTotal; CashMax > 0; CashMax--) { if (IsEvenLong(CashMax) && IsContainTotalTwice(Cash, CashMax / 2)) { break; } } if (!(CashMax > 0)) { System.out.println("NIESTETY"); } else { System.out.println(CashMax); } } private static boolean IsEvenLong(long InputLong) { long HalfLong = (InputLong / 2); return (InputLong == (HalfLong * 2)); } private static boolean IsContainTotalTwice(int[] Cash, long CashCount) { long CashStateOne = 0; long CashStateTwo = 0; for (int CurrentIndex = 0; CurrentIndex < Cash.length; CurrentIndex++) { if (CashStateOne < CashCount) { CashStateOne = CashStateOne + Cash[CurrentIndex]; } else { if (CashStateOne != CashStateTwo) { CashStateTwo = CashStateTwo + Cash[CurrentIndex]; } else { return true; } } } return false; } private static int[] ReadIntArrayFromConsole(BufferedReader ConsoleReader, int Length) throws IOException { String[] Input = ConsoleReader.readLine().split(" "); int[] ResultArray = new int[Length]; for (int Index = 0; Index < Length; Index++) { ResultArray[Index] = Integer.parseInt(Input[Index]); } return ResultArray; } }
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class kie { public static void main(String[] args) { try (BufferedReader Console = new BufferedReader(new InputStreamReader(System.in))) { int Length = Integer.parseInt(Console.readLine()); int[] Cash = ReadIntArrayFromConsole(Console, Length); CheckCoins(Cash); } catch (IOException e) { e.printStackTrace(); } } private static void CheckCoins(int[] Cash) { long CashTotal = 0; for (int CurrentIndex = 0; CurrentIndex < Cash.length; CurrentIndex++) { CashTotal = CashTotal + Cash[CurrentIndex]; } long CashMax; for (CashMax = CashTotal; CashMax > 0; CashMax--) { if (IsEvenLong(CashMax) && IsContainTotalTwice(Cash, CashMax / 2)) { break; } } if (!(CashMax > 0)) { System.out.println("NIESTETY"); } else { System.out.println(CashMax); } } private static boolean IsEvenLong(long InputLong) { long HalfLong = (InputLong / 2); return (InputLong == (HalfLong * 2)); } private static boolean IsContainTotalTwice(int[] Cash, long CashCount) { long CashStateOne = 0; long CashStateTwo = 0; for (int CurrentIndex = 0; CurrentIndex < Cash.length; CurrentIndex++) { if (CashStateOne < CashCount) { CashStateOne = CashStateOne + Cash[CurrentIndex]; } else { if (CashStateOne != CashStateTwo) { CashStateTwo = CashStateTwo + Cash[CurrentIndex]; } else { return true; } } } return false; } private static int[] ReadIntArrayFromConsole(BufferedReader ConsoleReader, int Length) throws IOException { String[] Input = ConsoleReader.readLine().split(" "); int[] ResultArray = new int[Length]; for (int Index = 0; Index < Length; Index++) { ResultArray[Index] = Integer.parseInt(Input[Index]); } return ResultArray; } } |