``DreamGrid went to the bookshop yesterday. There are books in the bookshop in total. Because DreamGrid is very rich, he bought the books according to the strategy below:
•Check the books from the 1st one to the -th one in order.
•For each book being checked now, if DreamGrid has enough money (not less than the book price), he’ll buy the book and his money will be reduced by the price of the book.
•In case that his money is less than the price of the book being checked now, he will skip that book.
BaoBao is curious about how rich DreamGrid is. You are asked to tell him the maximum possible amount of money DreamGrid took before buying the books, which is a non-negative integer. All he knows are the prices of the books and the number of books DreamGrid bought in total, indicated by .
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.
The second line contains non-negative integers (), where indicates the price of the -th book checked by DreamGrid.
It’s guaranteed that the sum of in all test cases will not exceed .
Output
For each test case output one line.
If it’s impossible to buy books for any initial number of money, output “Impossible” (without quotes).
If DreamGrid may take an infinite amount of money, output “Richman” (without quotes).
In other cases, output a non-negative integer, indicating the maximum number of money he may take.
Sample Input
4
4 2
1 2 4 8
4 0
100 99 98 97
2 2
10000 10000
5 3
0 0 0 0 1
Sample Output
6
96
Richman
Impossible

思路;省赛热身题,一开始老是WA,后来经过实验室其他大佬提醒,发现忘了一个关键信息,标价为0的物品已经自动的算在已经获得的物品里了,为了解决这一问题,应该再开一个数组,存储非0元素。

#include<stdio.h>
#include<algorithm>
using namespace std;
long long a[100100],b[100100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,i;
int ans=0,cnt=0;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]!=0)
b[ans++]=a[i];
else cnt++;
}
long long minn,sum=0;
if(n==m)
printf("Richman\n");
else if(cnt>m)
printf("Impossible\n");
else if(m==0)
{
minn=b[0];
for(i=0;i<ans;i++)
minn=min(minn,b[i]);
printf("%lld\n",minn-1);
}
else{
for(i=0;i<m-cnt;i++)
sum+=b[i];
minn=b[m-cnt];
for(i=m-cnt+1;i<ans;i++)
minn=min(minn,b[i]);
printf("%lld\n",sum+minn-1);
}
}
return 0;
}

发现下面的代码好像更好懂一些。

#include<stdio.h>
#include<algorithm>
using namespace std;
long long a[100100],b[100100];
int main()
{
int t;
int tt;
int i;
int n;
int m;
scanf("%d",&t);
while(t--)
{
int cnt=0;
scanf("%d%d",&n,&m);
tt=m;
long long sum=0;
for(i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
if(a[i]==0)
{
m--;
continue;
}
b[++cnt]=a[i];
}
if(m<0)
{
printf("Impossible\n");
continue;
}
if(tt==n)
{
printf("Richman\n");
continue;
}
for(i=1; i<=cnt; i++)
{
sum+=b[i];
if(i>m)
{
sum-=b[i];
sort(b+i,b+cnt+1);
printf("%lld\n",sum+b[m+1]-1);
break;
}
}
}
return 0;
}

利用这种方式去0很巧妙啊,不用再另开数组储存了。

for (int i = 0; i < n; i++)
{
scanf("%d", &a[k]);
if (!a[k])
ans++;
else k++;
}