两份一模一样的代码,第一遍交时在#3WA(输出-84)了,多交几遍就AC了?!
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e2 + 5;
void read(int &x) {
int f = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
for (x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= f;
}
//bool cmp(int x, int y) { return x > y; }
int a[N];
int main() {
int n, m;
read(n), read(m);
int now = 0, ans = 0;
for (int i = 1; i <= n; i++) {
read(a[i]);
//if (now <= m && a[i] < 0) ans -= a[i], now++;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= m; i++) {
if (a[i] < 0) ans -= a[i];
else break;
}
printf("%d\n", ans);
return 0;
}
兼WA又AC的代码……