/** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-12 Runda 1A. Kuglarz. * * Pamiec: 128MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public final class kug { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 1..2 000 int[] min1 = new int[n + 1]; // min w kolumnie int[] min2 = new int[n + 1]; // druga wartosc min w kolmnie int[] prz = new int[n + 1]; // koszt dla jednego kubka (1-1, 2-2, ..) for (int i = 1; i <= n; i++) { min1[i] = Integer.MAX_VALUE; min2[i] = Integer.MAX_VALUE; } for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { int x = nextInteger(); // 1..1 000 000 000 if (i == j) { prz[i] = x; } if (min1[j] > x) { min2[j] = min1[j]; min1[j] = x; } else if (min2[j] > x) { min2[j] = x; } } } int max = min1[1], maxj = 1; long r = min1[1]; for (int j = 2; j <= n; j++) { r += min1[j]; if (max < min1[j]) { max = min1[j]; maxj = j; } } int x = 0; // liczba przedzialow jednoelementowych (1-1, 2-2, ..) for (int j = 1; j <= n; j++) { if (j != maxj && min1[j] == prz[j]) { x += 1; } } int min = Integer.MAX_VALUE; for (int j = 1; j <= n; j++) { if (x >= 2) { if (min > min2[j]) { min = min2[j]; } } else { // dla x = 0 bedzie problem:( if (min > prz[j] && prz[j] >= min2[j]) { min = prz[j]; } } } if (max > min) { r = r - max + min; } System.out.println(r); } }
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 | /** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-12 Runda 1A. Kuglarz. * * Pamiec: 128MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public final class kug { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 1..2 000 int[] min1 = new int[n + 1]; // min w kolumnie int[] min2 = new int[n + 1]; // druga wartosc min w kolmnie int[] prz = new int[n + 1]; // koszt dla jednego kubka (1-1, 2-2, ..) for (int i = 1; i <= n; i++) { min1[i] = Integer.MAX_VALUE; min2[i] = Integer.MAX_VALUE; } for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { int x = nextInteger(); // 1..1 000 000 000 if (i == j) { prz[i] = x; } if (min1[j] > x) { min2[j] = min1[j]; min1[j] = x; } else if (min2[j] > x) { min2[j] = x; } } } int max = min1[1], maxj = 1; long r = min1[1]; for (int j = 2; j <= n; j++) { r += min1[j]; if (max < min1[j]) { max = min1[j]; maxj = j; } } int x = 0; // liczba przedzialow jednoelementowych (1-1, 2-2, ..) for (int j = 1; j <= n; j++) { if (j != maxj && min1[j] == prz[j]) { x += 1; } } int min = Integer.MAX_VALUE; for (int j = 1; j <= n; j++) { if (x >= 2) { if (min > min2[j]) { min = min2[j]; } } else { // dla x = 0 bedzie problem:( if (min > prz[j] && prz[j] >= min2[j]) { min = prz[j]; } } } if (max > min) { r = r - max + min; } System.out.println(r); } } |