Description Once Bob got to a sale of old TV sets. Therewere Input The first line contains two space-separatedintegers Output Output the only number — the maximum sum of money that Bob canearn, given that he can carry at most Sample Input
Input
5 3 -6 0 35 -2 4
Output
8
Input
4 2 7 0 0 -7
Output
7 |
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int n,m,a[100],i,sun;
while(scanf("%d %d",&n,&m)!=EOF)
{
sun=0;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(i=0;i<m;i++)
{
if(a[i]<0)
sun+=a[i];
}
cout<<abs(sun)<<endl;
}
return 0;
}