import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String str = sc.nextLine();
int n = Integer.parseInt(str);
String[] chm = sc.nextLine().split(" ");
String[] chx = sc.nextLine().split(" ");
ArrayList<integer> all = new ArrayList<>();
for(int i=0; i<n; i++){
int m = Integer.parseInt(chm[i]);
int x = Integer.parseInt(chx[i]);
for(int j=0; j<x; j++){
all.add(m);
}
}
HashSet<integer> set = new HashSet<>();
set.add(0);
for(int i=0; i<all.size(); i++){
int a = all.get(i);
HashSet<integer> set1 = new HashSet<>(set);
for(int j:set1){
set.add(a+j);
}
}
System.out.println(set.size());
}
}
}
java解法:先用一个数组把每一个“砝码”装起来,如输入用例{1,1,2},再用集合Set记录每一个组合重量值。
首先集合里只有{0},将上面的每一个砝码一个一个地往里装,同时与前面的重量值分别相加形成新的重量值:
放m=1的砝码进去:放之前{0};放之后{0,0+1}={0,1}
放m=1的砝码进去:放之前{0,1};放之后{0,1,0+1,1+1}={0,1,2}
放m=2的砝码进去:放之前{0,1,2};放之后{0,1,2,0+2,1+2,2+2}={0,1,2,3,4}</integer></integer></integer>