编程介的小学生 2017-02-13 09:47 采纳率: 20.5%
浏览 811
已采纳

Chemical Weighing

Description

Facer is the chemical keeper in a laboratory. Every day he receives M requests of a certain kind of chemical which is vital to a large variety of experiments. The ith request asks for Ri grams of the chemical and Facer has to weigh the chemicals in the exact order as the requests listed or he will mess up all the experiments.

The balance used in the laboratory has N different kinds of poises. The ith kind weighs Wi grams. The amount of poises of each kind is so large that can be considered as infinite. The poises are all in a flat shape. When Facer wants to weigh the chemical, he has to stack the poises on the left side of the balance one by one in such a way that the lighter ones are always above the heavier ones. For example, a 2-gram poise is never allowed to be on the top of a 1-gram poise.

To accelerate his work, Facer tries to use the minimum steps to finish the weighing. For each step he can put one poise on the top of the stack (as long as no poises in the stack is lighter than it) or remove the poise which is currently on the top.

Input

The first line of the input contains the number of the test cases.
Each test case follows the format below:

One line contains an integer N (1 ≤ N ≤ 15), the number of different kinds of poises
One line contains N integers, W1, W2 ... WN, ( 1 ≤ Wi ≤ 30)
One line contains an integer M (1 ≤ M ≤ 500), the number of requests
One line contains M integers, R1, R2, ... RM ( 1 ≤ Ri ≤ 30)
Output

For each test case, output a line containing the minimum number of steps Facer needed to finish all the requests. If any request can not be weighed out, output -1 instead.

Sample Input

2
3
1 2 3
3
4 5 6
2
3 4
3
1 6 8
Sample Output

4
-1

  • 写回答

1条回答 默认 最新

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题