/* * Solution Template for Atlantis III: Twin Rivers * * Australian Informatics Olympiad 2024 * * This file is provided to assist with reading and writing of the input * files for the problem. You may modify this file however you wish, or * you may choose not to use this file at all. */ import java.io.*; class Solution { /* N is the number of bridges. */ private static int N; /* L is the length of the city. */ private static int L; /* * B and R describe the bridges. Note that the arrays start from 0, and so * the bridges are (B[0], R[0]) to (B[N-1], R[N-1]). */ private static long B[] = new long[200005]; private static int R[] = new int[200005]; /* T is the number of trips. */ private static int T; /* * X and S describe the trips. Note that the arrays start from 0, and so * the trips are (X[0], S[0]) to (X[T-1], S[T-1]). */ private static long X[] = new long[200005]; private static int S[] = new int[200005]; private static long answer; /* * Read the next token from the input file. * Tokens are separated by whitespace, i.e., spaces, tabs and newlines. * If end-of-file is reached then an empty string is returned. */ private static String readToken(BufferedReader in) throws IOException { StringBuffer ans = new StringBuffer(); int next; /* Skip any initial whitespace. */ next = in.read(); while (next >= 0 && Character.isWhitespace((char)next)) next = in.read(); /* Read the following token. */ while (next >= 0 && ! Character.isWhitespace((char)next)) { ans.append((char)next); next = in.read(); } return ans.toString(); } public static void main(String[] args) throws IOException { /* Open the input and output files. */ BufferedReader input_file = new BufferedReader(new FileReader( "twinin.txt")); BufferedWriter output_file = new BufferedWriter(new FileWriter( "twinout.txt")); /* Read the values of N, L, B, R, T, X, and S from input file. */ N = Integer.parseInt(readToken(input_file)); L = Integer.parseInt(readToken(input_file)); for (int i = 0; i < N; i++) { B[i] = Long.parseLong(readToken(input_file)); R[i] = Integer.parseInt(readToken(input_file)); } T = Integer.parseInt(readToken(input_file)); for (int i = 0; i < T; i++) { X[i] = Long.parseLong(readToken(input_file)); S[i] = Integer.parseInt(readToken(input_file)); } /* * TODO: This is where you should compute your solution. Store the * smallest possible sum of trip distances you can achieve after * building one more bridge into the variable answer. */ /* * Please note that the answer may exceed the maximum value * that can be stored in an "int" integer type. * Because of this, you should use the "long" integer type * instead of "int" when computing your solution. */ /* Write the answer to the output file. */ output_file.write(answer + "\n"); /* Finally, close the input/output files. */ input_file.close(); output_file.close(); } }