传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067
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:
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
题意:这个人很有钱,他想买书,会遵循三个规则,一他会看遍每一本书,二如果他的钱足够买下这一本书他一定会买,三如果买不起就跳过。现在另外一个人想知道这个有钱人可能带的最多的钱是多少,他知道有钱人买了多少书以及每本书的价格。
题解:题目里面关于0元的书有个坑就是一定会买,所以当零元书的数量大于m的时候一定是impossible的,否则就按顺序添加不为0的书直至数量为m,再加上剩余书的最小价值减一即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll maxn=1e6+1;
ll a[maxn];
int main()
{
int m,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ll z=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
if(a[i]==0)
{
z++;
}
}
if(z>m)
{
printf("Impossible\n");
}else if(n==m)
{
printf("Richman\n");
}else
{
ll mm=0,mi=1e9+7,m2=0;
ll ans=0;
for(int i=1;i<=n;i++)
{
if(m2+z<m)
{
if(a[i]==0)
{
mm++;
continue;
}
ans+=a[i];
m2++;
}else
{
if(a[i]==0)
{
continue;
}
mi=min(mi,a[i]);
}
}
ans+=mi-1;
printf("%lld\n",ans);
}
}
return 0;
}