分治dfs
import java.util.*;
public class Main {
static int max;
static int n;
static int a[];
static int b[];
public static void main(String[] args) {
Scanner jin = new Scanner(System.in);
n = jin.nextInt();
a = new int[n+1];
b = new int[n];
for(int i=1;i<=n;i++) a[i] = jin.nextInt();
for(int i=1;i<n;i++) b[i] = jin.nextInt();
f(0, 0, 1);
System.out.println(max);
}
public static void f(int num, int cs, int bs){
if(num < 0){
System.out.println(-1);
System.exit(0);
}
if(bs == n){
if(cs < 3)
max = Math.max(max, num+a[n]);
else
max = Math.max(max, num);
return;
}
if(num >= b[bs]) f(num-b[bs], cs, bs+1);
if(cs < 3) f(num+a[bs]-b[bs], cs+1, bs+1);
}
}
查看14道真题和解析