描述
输入n个整数,输出其中最小的K个数之和。例如输入4,5,1,1,6,2,7,3,3这9个数字,当k=4,则输出最小的4个数之和为7(1,1,2,3)。
输入
测试样例组数不超过10
每个测试案例包括2行:
第一行为2个整数n,k(1<=k<=n<=100000)
第二行包含n个整数,每个整数的范围为【1~2000】
输出
对应每个测试案例,输出最小的k个数之和。
样例输入
8 4
5 2 1 3 8 1 9 3
样例输出
7
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=sc.nextInt();
int[] i=new int[a];
int sum=0;
for(int b=0;b<a;b++){
i[b]=sc.nextInt();
}
Arrays.sort(i);
for(int b=0;b<c;b++){
sum=sum+i[b];
}
System.out.println(sum);
}