import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; public class kie { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; PrintStream printStream = System.out; new AllowanceCalculator(inputStream, System.getProperty("line.separator")).respond(printStream); } static class AllowanceCalculator { private BillsReader billsReader; public AllowanceCalculator(InputStream inputStream, String lineSeparator) throws IOException { billsReader = new BillsReader(inputStream, lineSeparator); } public int calculateMaxAllowaneSum() throws IOException { int maxAllowanceSum = 0; int[] bills = billsReader.readBills(); int minUneven = Integer.MAX_VALUE; for(int i = 0;i < bills.length;i++) { int bill = bills[i]; maxAllowanceSum += bill; if(isUneven(bill) && minUneven > bill) { minUneven = bill; } } if(isUneven(maxAllowanceSum)) { return maxAllowanceSum - minUneven; } return maxAllowanceSum; } public void respond(PrintStream printStream) throws IOException { int maxAllowanceSum = calculateMaxAllowaneSum(); if(maxAllowanceSum > 0) { printStream.print(maxAllowanceSum); } else { printStream.print("NIESTETY"); } } boolean isUneven(int number) { return (number & 1) == 1; } } static class BillsReader { private SimpleStringParser simpleStringParser; private SimpleNumber simpleNumber; public BillsReader(InputStream inputStream, String lineSeparator) throws IOException { simpleStringParser = new SimpleStringParser(10000, inputStream); simpleNumber = new SimplePositiveInteger(lineSeparator); } public int[] readBills() throws IOException { int n = simpleNumber.parseLastInt(simpleStringParser, true) - 1; int[] bills = new int[n + 1]; for(int i = 0;i < n;i++) { bills[i] = simpleNumber.parseInt(simpleStringParser); } bills[n] = simpleNumber.parseLastInt(simpleStringParser, false); return bills; } } private static abstract class SimpleNumber { protected static final int ZERO = '0'; protected static final int SPACE = ' '; protected final String lineSeparator; protected final char beginOfLineSeparator; public SimpleNumber(String lineSeparator) { this.lineSeparator = lineSeparator; beginOfLineSeparator = lineSeparator.charAt(0); } public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } private static class SimplePositiveInteger extends SimpleNumber { public SimplePositiveInteger(String lineSeparator) { super(lineSeparator); } @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } return integer; } @Override public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != beginOfLineSeparator) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += lineSeparator.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < lineSeparator.length();++i) { if(simpleStringParser.array[i] == lineSeparator.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } private static class SimpleStringParser { int index = 0; final byte[] array; int bytesReaded; final InputStream inputStream; public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException { array = new byte[arraySize]; this.inputStream = inputStream; bytesReaded = inputStream.read(array); } protected final void nextChunkOfData() throws IOException { bytesReaded = inputStream.read(array); index = 0; } protected final boolean nextChunkOfDataRequired() { return bytesReaded <= index; } } }
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; public class kie { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; PrintStream printStream = System.out; new AllowanceCalculator(inputStream, System.getProperty("line.separator")).respond(printStream); } static class AllowanceCalculator { private BillsReader billsReader; public AllowanceCalculator(InputStream inputStream, String lineSeparator) throws IOException { billsReader = new BillsReader(inputStream, lineSeparator); } public int calculateMaxAllowaneSum() throws IOException { int maxAllowanceSum = 0; int[] bills = billsReader.readBills(); int minUneven = Integer.MAX_VALUE; for(int i = 0;i < bills.length;i++) { int bill = bills[i]; maxAllowanceSum += bill; if(isUneven(bill) && minUneven > bill) { minUneven = bill; } } if(isUneven(maxAllowanceSum)) { return maxAllowanceSum - minUneven; } return maxAllowanceSum; } public void respond(PrintStream printStream) throws IOException { int maxAllowanceSum = calculateMaxAllowaneSum(); if(maxAllowanceSum > 0) { printStream.print(maxAllowanceSum); } else { printStream.print("NIESTETY"); } } boolean isUneven(int number) { return (number & 1) == 1; } } static class BillsReader { private SimpleStringParser simpleStringParser; private SimpleNumber simpleNumber; public BillsReader(InputStream inputStream, String lineSeparator) throws IOException { simpleStringParser = new SimpleStringParser(10000, inputStream); simpleNumber = new SimplePositiveInteger(lineSeparator); } public int[] readBills() throws IOException { int n = simpleNumber.parseLastInt(simpleStringParser, true) - 1; int[] bills = new int[n + 1]; for(int i = 0;i < n;i++) { bills[i] = simpleNumber.parseInt(simpleStringParser); } bills[n] = simpleNumber.parseLastInt(simpleStringParser, false); return bills; } } private static abstract class SimpleNumber { protected static final int ZERO = '0'; protected static final int SPACE = ' '; protected final String lineSeparator; protected final char beginOfLineSeparator; public SimpleNumber(String lineSeparator) { this.lineSeparator = lineSeparator; beginOfLineSeparator = lineSeparator.charAt(0); } public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } private static class SimplePositiveInteger extends SimpleNumber { public SimplePositiveInteger(String lineSeparator) { super(lineSeparator); } @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } return integer; } @Override public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != beginOfLineSeparator) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += lineSeparator.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < lineSeparator.length();++i) { if(simpleStringParser.array[i] == lineSeparator.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } private static class SimpleStringParser { int index = 0; final byte[] array; int bytesReaded; final InputStream inputStream; public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException { array = new byte[arraySize]; this.inputStream = inputStream; bytesReaded = inputStream.read(array); } protected final void nextChunkOfData() throws IOException { bytesReaded = inputStream.read(array); index = 0; } protected final boolean nextChunkOfDataRequired() { return bytesReaded <= index; } } } |