8664蛋糕的美味值
8664蛋糕的美味值
⭐️难度:中等
🌟考点:枚举
📖
📚
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in );
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = 0;
for(int i =0;i<=(1<<n)-1;i++){
long res = 0;
for(int j = 0;j<n;j++){
if((i>>j) % 2 == 1){
res += a[j];
}
}
if(res < k){
ans = Math.max(ans,res);
}
}
System.out.println(ans);
}
}
🍎笔记