2018 ACM-ICPC Asia Qingdao Regional J BOOKS

1 篇文章 0 订阅

                                                Books

传送门: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:

  • 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

题意:这个人很有钱,他想买书,会遵循三个规则,一他会看遍每一本书,二如果他的钱足够买下这一本书他一定会买,三如果买不起就跳过。现在另外一个人想知道这个有钱人可能带的最多的钱是多少,他知道有钱人买了多少书以及每本书的价格。

题解:题目里面关于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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值