小招喵所在的國家正處於怪獸入侵的戰場中,這個國家一共有n+1個城市
阿新 • • 發佈:2019-09-16
import java.util.Arrays;
import java.util.Scanner;
public class test3 {
public static long max=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
long c[]=new long[n+1];
for(int i=0;i<n+1;i++)
c[i]=sc.nextInt();
long d[]=new long[n];
for(int i=0;i<n;i++)
d[i]=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=i;
f(a,0,c,d);
System.out.println(max);
}
public static void f(int a[],int k,long c[],long d[]){
if(k==a.length){
long n=g(a,c,d);
if(max<n)
max=n;
}
for(int i=k;i<a.length;i++){
int temp=a[k];
a[k]=a[i];
a[i]=temp;
f(a,k+1,c,d);
int temp1=a[k];
a[k]=a[i];
a[i]=temp1;
}
}
public static long g(int a[],long c[],long d[]){
long temp []= Arrays.copyOf(c,c.length);
long sum=0;
for(int i=0;i<a.length;i++){
//判斷該第i個勇士是否同時消滅掉第a[i],和a[i]+1兩城的怪獸, temp[a[i]]=0;
// temp[a[i]+1]=0;
if(d[i]>=temp[a[i]]+temp[a[i]+1]){
sum+=temp[a[i]]+temp[a[i]+1];
temp[a[i]]=0;
temp[a[i]+1]=0;
}else{
//判斷第i個勇士是否消滅掉的數大於第a[i]個城市的怪獸,如果大於,將 temp[a[i]]=0;
//第a[i]+1城市的怪獸 temp[a[i]+1]=temp[a[i]+1]-(d[i]-temp[a[i]]);
if(d[i]>temp[a[i]]){
sum+=d[i];
temp[a[i]+1]=temp[a[i]+1]-(d[i]-temp[a[i]]);
temp[a[i]]=0;
}else{
//這種情況是連第a[i]個城市的怪獸都消滅不了
sum+=d[i];
temp[a[i]]=temp[a[i]]-d[i];
}
}
}
return sum;