算法 BISHI46 小红的魔法药剂 流程图 求解代码 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 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split("\\s+"); int[] redStr = new int[str.length]; for (int i = 0; i < str.length; i++) { redStr[i] = Integer.parseInt(str[i]); } int sum = 0; // 遍历每种药剂 for (int i = 0; i < n; i++) { // 读取合成第i种蓝色药剂所需的两种红色药剂的编号b和c String[] split = br.readLine().split("\\s+"); int b = Integer.parseInt(split[0]); int c = Integer.parseInt(split[1]); // 计算合成第i种蓝色药剂的花费,即所需两种红色药剂价格之和 int blue = redStr[b - 1] + redStr[c - 1]; // 获取直接购买第i种红色药剂的花费 int red = redStr[i]; // 选择合成蓝色药剂和直接购买红色药剂两者花费较小的值,累加到总花费sum中 sum += Math.min(blue, red); } out.println(sum); out.close(); br.close(); }