import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class kug { public static void main(String[] args) throws IOException { Timer timer = new Timer(); final InputStream inputStream = System.in; final SimpleNumber simpleNumber = new SimplePositiveInteger(); final SimpleStringParser simpleStringParser = new SimpleStringParser(100000, inputStream); int n = simpleStringParser.parseLastInt(simpleNumber, true); int[][] array = new int[n][]; for(int i = 0;i < n;i++) { int length = n - i - 1; array[i] = new int[length + 1]; for(int j = 0;j < length;j++) { array[i][j] = simpleStringParser.parseInt(simpleNumber); } array[i][length] = simpleStringParser.parseLastInt(simpleNumber, length > 0); } long sum = 0; for(int i = 0;i < n;i++) { int min = Integer.MAX_VALUE; for(int j = i, k = 0;j > 0;j--, k++) { int value = array[k][j]; if(min > value) { min = value; } } int length = array[i].length; for(int j = 0;j < length;j++) { int value = array[i][j]; if(min > value) { min = value; } } sum += min; } System.out.println(sum); timer.printTimeInMillis(); } 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; } public int parseInt(final SimpleNumber simpleNumber) throws IOException { final int integer = simpleNumber.parseInt(this); ++index; return integer; } public int parseLastInt(final SimpleNumber simpleNumber, final boolean hasMore) throws IOException { return simpleNumber.parseLastInt(this, hasMore); } } static abstract class SimpleNumber { protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); protected static final char BEGIN_OF_LINE_SEPARATOR = LINE_SEPARATOR.charAt(0); protected static final int ZERO = '0'; protected static final int SPACE = ' '; public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } static class SimplePositiveInteger extends SimpleNumber { @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } 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(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != BEGIN_OF_LINE_SEPARATOR) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += LINE_SEPARATOR.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < LINE_SEPARATOR.length();++i) { if(simpleStringParser.array[i] == LINE_SEPARATOR.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } static public class Timer { private long time; public Timer() { startNextMeasure(); } public void startNextMeasure() { time = System.nanoTime(); } public void printTimeInMillis() { System.err.println(getTitmeInNanos() / 1000000); } public void printTimeInNanos() { System.err.println(getTitmeInNanos()); } public void printTimeInMicros() { System.err.println(getTitmeInNanos() / 1000); } public void printTimeInSeconds() { System.err.println(getTitmeInNanos() / 1000000000); } private long getTitmeInNanos() { return (System.nanoTime() - time); } } }
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 | import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class kug { public static void main(String[] args) throws IOException { Timer timer = new Timer(); final InputStream inputStream = System.in; final SimpleNumber simpleNumber = new SimplePositiveInteger(); final SimpleStringParser simpleStringParser = new SimpleStringParser(100000, inputStream); int n = simpleStringParser.parseLastInt(simpleNumber, true); int[][] array = new int[n][]; for(int i = 0;i < n;i++) { int length = n - i - 1; array[i] = new int[length + 1]; for(int j = 0;j < length;j++) { array[i][j] = simpleStringParser.parseInt(simpleNumber); } array[i][length] = simpleStringParser.parseLastInt(simpleNumber, length > 0); } long sum = 0; for(int i = 0;i < n;i++) { int min = Integer.MAX_VALUE; for(int j = i, k = 0;j > 0;j--, k++) { int value = array[k][j]; if(min > value) { min = value; } } int length = array[i].length; for(int j = 0;j < length;j++) { int value = array[i][j]; if(min > value) { min = value; } } sum += min; } System.out.println(sum); timer.printTimeInMillis(); } 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; } public int parseInt(final SimpleNumber simpleNumber) throws IOException { final int integer = simpleNumber.parseInt(this); ++index; return integer; } public int parseLastInt(final SimpleNumber simpleNumber, final boolean hasMore) throws IOException { return simpleNumber.parseLastInt(this, hasMore); } } static abstract class SimpleNumber { protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); protected static final char BEGIN_OF_LINE_SEPARATOR = LINE_SEPARATOR.charAt(0); protected static final int ZERO = '0'; protected static final int SPACE = ' '; public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } static class SimplePositiveInteger extends SimpleNumber { @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } 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(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != BEGIN_OF_LINE_SEPARATOR) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += LINE_SEPARATOR.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < LINE_SEPARATOR.length();++i) { if(simpleStringParser.array[i] == LINE_SEPARATOR.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } static public class Timer { private long time; public Timer() { startNextMeasure(); } public void startNextMeasure() { time = System.nanoTime(); } public void printTimeInMillis() { System.err.println(getTitmeInNanos() / 1000000); } public void printTimeInNanos() { System.err.println(getTitmeInNanos()); } public void printTimeInMicros() { System.err.println(getTitmeInNanos() / 1000); } public void printTimeInSeconds() { System.err.println(getTitmeInNanos() / 1000000000); } private long getTitmeInNanos() { return (System.nanoTime() - time); } } } |