Insects
Time limit1sMemory limit128 MB
Each insect dies to one attractor, feed, and poison triple; choose ingredients to maximize p times insects killed minus the ingredient costs.
- Level
Medium6 of 10
- Topics
- Graph, Bit manipulation
- Solved
- No attempts yet
Problem
Sophie works at a company that produces insect baits. Each bait has three ingredients: an attractor, a feed, and a poison. For each of the insect types, there is exactly one triplet of ingredients that eliminates it. The ingredients are numbered.
Sophie is designing a new insect bait. The bait does not need to kill all the insects. Its goal is to maximize the company's profit.
The production cost is the sum of the unit costs of the distinct attractors, feeds, and poisons the bait contains. All attractors share one unit cost, and the same holds for feeds and poisons.
The price is the number of insect types the bait can kill, multiplied by , the bang-per-bug factor. The profit per unit of bait is its price minus its production cost.
Help Sophie design the most profitable bait. Write a program that reads the insect count, the bang-per-bug factor, and the unit costs, then determines the maximum possible profit per unit of bait.
Input
The first line contains the integers , , , , and (), separated by single spaces. They are the number of insect types, the bang-per-bug factor, and the unit costs of attractors, feeds, and poisons, respectively.
Each of the next lines contains three integers , , and (), separated by single spaces. The -th insect can be eliminated by combining attractor , feed , and poison . All triplets are pairwise different.
Output
Print a single integer: the maximum possible profit per unit of bait.
Hint
The bait uses attractors 127 and 255 (cost ), feed 127 (cost ), and poisons 0 and 127 (cost ), for a total production cost of 18. It kills insect types 1, 3, and 4, so its price is 30. The profit per unit is therefore 12.