编程介的小学生 2017-05-07 06:33 采纳率: 20.5%
浏览 853
已采纳

Best Deal

Tom, a young explorer, visited an Indian tribe and fell in love with the Chief's daughter. Tom asked the Chief to marry his daughter to him. But the Chief would accept his proposal only if he could provide 10000 golden coins as the engagement gift. Tom couldn't make it, so he pled for a reduction. The Chief said:" OK, I would then request 8000 golden coins if you could get me the fur coat of the Pontiff. Or, if you could get me his crystal, I could drop to 5000 golden coins."

Tom then went to the Pontiff for his fur coat or crystal. The Pontiff too requested some amount of golden coins as the exchange, or a possible reduction of price if Tom could get him some other things. Tom continued to visit other people and found that all of them would either request some golden coins for exchange, or drop the price down for something else.

Now Tom needs your help on trading to marry the girl he loves.

An important rule Tom has to tell you is: the hierachical system is so strict in this tribe that two people will never contact each other in any way if the difference of the two levels they belong to is larger than a certain threshold. Tom is an outsider so he is beyond this restriction. However, if he trades with someone in a lower level, then others in a higher level will not trade with him anymore since otherwise they would be indirectly contacting the lower levels, and vice versa. Therefore your best suggestion to him must have all the situations considered.

For the sake of convenience, let us mark all the trading objects by numbers starting from 1. The Chief's approval is as well considered an object and is always marked 1. Each object has a price P, the owner's level L, and a sequence of substitutions Ti and the corresponding "voucher" price Vi. There must be no "indirect trading" if the difference between two levels is greater than M. You must calculate the least number of golden coins Tom needs to marry the daughter of the Chief.

Input

The input consists of several test cases. For each test case:

The 1st line contains two integers M and N (1<=N<=100) which represent the threshold of level difference and the total number of trading objects respectively.
Then the descriptions of N objects follow, in the increasing order with respect to their marks. Each description begins with three nonnegative integers P, L, and X (<N), representing the object's price, owner's level, and the number of substitutions. The following X lines each contains two integers T and V, which are the mark number of the substitution and its "voucher" price, respectively.

Output

For each test case, print in a single line the minimum number of golden coins needed.

Sample Input

1 4
10000 3 2 //The Chief's approval
2 8000
3 5000
1000 2 1 //The Pontiff's fur coat
4 200
3000 2 1 //The Pontiff's crystal
4 200
50 2 0 //Another object

Sample Output

5250

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?