class Main {
static final int N = 5;
static final int W = 10;
static int []w = {6,5,4,2,1};
static int []v = {5,3,5,3,2};
static int curValue;
static int curWeight;
static int bestValue;
static int []x = new int[5];
static int []bestX = new int[5];
public static void main(String[] args) {
backtracking(0);
System.out.println("最大的价值: " + bestValue);
System.out.print("选择的物品: ");
for (int i = 0; i < N; i++) {
System.out.print(bestX[i]+" ");
}
}
public static void backtracking(int k) {
if (k > N-1) {
if (bestValue < curValue) {
bestValue = curValue;
for (int i = 0; i < N; i++) {
bestX[i] = x[i];
}
}
}
else {
for (int i = 0; i <= 1; i++) {
x[k] = i;
if (i == 0) {
backtracking(k+1);
}
else {
if (curWeight + w[k] < W) {
curWeight += w[k];
curValue += v[k];
backtracking(k+1);
curWeight -= w[k];
curValue -= v[k];
}
}
}
}
}
}