数据范围
1≤T≤20,
1≤m≤n≤100,
−1000≤ai≤1000
输入样例:
5
5 3
-6 0 35 -2 4
4 2
7 0 0 -7
6 6
756 -611 251 -66 572 -818
5 5
976 437 937 788 518
5 3
-2 -2 -2 -2 -2
输出样例:
8
7
1495
0
6
从小到大排个序,在前m个数中选所有负数,题目说最多选m个,可以不选满,正数就没有必要选了,扣分
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#define ll long long
#define ri register int
using namespace std;
const int maxn = 2e5 + 7;
int a[maxn];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
int ans = 0;
for (int i = 1; i <= k; ++i)
if(a[i]<0)ans -= a[i];
cout << ans << endl;
}
}