在一个夜黑风高的晚上,有几个小朋友在桥的这边,现在他们需要过桥,但是由于桥很窄,每次只允许不超过两人通过,他们只有一个手电简,所以每次过桥后,需要有人把手电筒带回来,第:号小朋友过桥的时间为 a,两个人过桥的总时间为二者中时间长者。问所有小朋友过桥的总时间最短是多少。
输入格式
第一行输入一个整数n(1≤n≤1000),表示有 n 个小朋友。
第二行有 几 个整数 a: ,a: 表示第之个小朋友过河需要的时间(0 < ai ≤ 1000)
输出格式
输出一个整数,表示所有小朋友过河所需要的时间
import java.util.*; public class LANQIAO1 { public static int[] a = new int[1010]; // f表示前多少个人过去了,并且手电筒一定在对面 public static int[] f = new int[1010]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } Arrays.sort(a); // 0号过去等于a[0] f[0] = a[0]; f[1] = a[1]; for (int i = 2 ; i < n; i++) { // 让0号来送手电并且和i号和i-1号一起走 or 一号送手电并且和零号一起回去 f[i] = Math.min(f[i - 1] + a[0] + a[i],f[i - 2] + a[0] + 2 * a[1] + a[i]); } } }