Description
大家都知道zz特别喜欢金鱼,一天zz去买金鱼,但是他只带了n元,他想买k种金鱼。(每种一条)如果钱不够他会向wxa借。已知每条金鱼的价格,你能告诉他最少他需要向wxa借多少吗?
Input
第一行输入一个T,代表有T组测试数据。
每组测试数据第一行输入n,k,m(0 < m < 1000)。接下来有一行,有m组数据,代表总共有m种金鱼。
Output
zz向wxa借的钱。
Samples
input Copy
1 10 5 10 1 2 3 4 5 6 7 8 9 10
output Copy
5
Hint
注意:金鱼可以卖0元,就是免费送。
#include<stdio.h>
void bubble_sort(int arr[], int len) {
int i, j, temp;
for (i = 0; i < len - 1; i++)
for (j = 0; j < len - 1 - i; j++)
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int main(){
int t,n,k,i,m,j;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&k,&m);
int a[m];
for(i=0;i<m;i++){
scanf("%d",&a[i]);
}
bubble_sort(a,m);
int sum=0;
for(i=0;i<k;i++){
sum+=a[i];
}
if(sum>=n)
printf("%d\n",sum-n);
else printf("0\n");
}
return 0;
}