#P9894. [ICPC2018 Qingdao R] Books

[ICPC2018 Qingdao R] Books

题目描述

DreamGrid went to the bookshop yesterday. There are nn books in the bookshop in total. Because DreamGrid is very rich, he bought the books according to the strategy below:

  • Check the nn books from the 1st one to the nn-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 nn books and the number of books DreamGrid bought in total, indicated by mm.

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n1051 \le n \le 10^5, 0mn0 \le m \le n), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.

The second line contains nn non-negative integers a1,a2,...,ana_1, a_2, ... , a_n (0ai1090 \le a_i \le 10^9), where aia_i indicates the price of the ii-th book checked by DreamGrid.

It's guaranteed that the sum of nn in all test cases will not exceed 10610^6.

输出格式

For each test case output one line.

If it's impossible to buy mm 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.

题目大意

共有 nn 本书,第 ii 本书的价格为 aia_i。买书的策略为:按编号顺序从小到大遍历每本书,若书的价格不超过剩余钱数,则购买,否则跳过。

问初始最多有多少钱能恰好买 mm 本书。若初始可以有无穷多的钱,则输出 Richman。若不能恰好买 mm 本数,则输出 Impossible

多组测试数据。

1n1051\leq n\leq 10 ^ 50mn0\leq m\leq n0ai1090\leq a_i\leq 10 ^ 9n106\sum n\leq 10 ^ 6

4
4 2
1 2 4 8
4 0
100 99 98 97
2 2
10000 10000
5 3
0 0 0 0 1
6
96
Richman
Impossible