编程介的小学生 2017-02-22 16:21 采纳率: 20.5%
浏览 803
已采纳

Supermarket

Mr. Jones is an exemplary husband. Every Saturday morning Mrs. Jones gives him a list of goods to be bought from the supermarket and he buys exactly what he has been asked for, always choosing the brands with lowest prices. But Mr. Jones hates going to the supermarket on a Saturday, since its aisles are packed with shoppers. He wants to change the way he does his shopping. Instead of going to and fro to buy the products on his wife's list, he will try to get the goods on the list going through each aisle only once, picking up the products in the exact order given in the list. So he asked you to write a program to help him with his new style of shopping.

Given the information about products available in the supermarket together with their prices in the order in which they appear in Mr. Jones' way and the list of products given by his wife, your program must determine the least cost that he would pay.

Mr. Jones buys the products in the order in which they appear in Mrs. Jones' list and he never goes back as he walks down the aisles. Therefore, if he buys the i-th product on his way as the j-th item on the list, the next product to be bought is the (j+1)-th item of the list - and it must be bought from the products that come after i in his path. The figure below shows an example where products are identified by integers. Note that different brands of the same product may appear separately. In the example Mr. Jones must buy products 1,1,2,20 (notice that product 1 appears twice in the list). For the example, the least cost for Mr. Jones following his constraints is 21.30. Notice that with this new way of shopping it may be impossible for Mr. Jones to buy all the goods on Mrs. Jones list; in that case, your program should warn Mr. Jones.

Input

Your program should process data for several shopping sessions. The first line in the description of a shopping session contains two integers M and N; M indicates the number of items in Mrs. Jones' list (1 <= M <= 100) and N represents the total number of products available in the supermarket (1 <= N <= 100,000). The next line contains M integers Xi representing the list of products in Mrs. Jones' list (1 <= Xi <= 100,000, 1 <= i <= M). Then N lines follow, representing the supermarket products in the order in which they appear in Mr. Jones' way. Each of those lines contains an integer K and a real P which represent respectively a product identifier and its price (1 <= K <= 100,000). The end of input is indicated by M = N = 0.

Output

For each shopping session in the input, your program should produce one line of output, containing the least cost that Mr. Jones would pay. If it is not possible to buy all the goods for the session, print the word 'Impossible'. The cost must be printed as a real number with two-digit precision, and the last decimal digit must be rounded. The input will not contain test cases where differences in rounding are significant.

Sample Input

4 8
1 1 2 20
2 0.29
1 0.30
20 0.15
1 1.00
5 0.05
2 10.00
20 20.00
20 10.00
2 5
1 2
3 1.00
4 1.00
2 0.01
1 1.00
2 1.50
2 3
1 2
2 0.05
1 10.00
1 3.00
0 0

Sample Output

21.30
2.50
Impossible

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-22 16:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧