Under construction

Still being built. Things may change

Gas and Minerals

Interview

Time limit1sMemory limit1024 MB

Summary
Given minerals and gas budgets and n building types with costs and defense values, unbounded knapsack with two resource constraints finds the maximum total defense.
Level

Medium4 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

An artifact that could turn the tide of the war was discovered in one of the distant Terran colonies. Meanwhile, the intelligence service reports that a Zerg swarm is moving toward the colony. The artifact must be protected at all costs before reinforcements arrive.

You have mm units of minerals and gg units of Vespen gas. There are nn types of defensive buildings available for construction. A building of type ii requires aia_i units of minerals and bib_i units of gas to construct, and increases the defenses of the base by cic_i units. You may construct any number of buildings of any type, including zero, as long as the total minerals and total gas spent do not exceed mm and gg, respectively.

Determine the maximum total defense capability that can be achieved under these constraints.

Input

The first line contains three integers mm, gg, and nn, the units of minerals and gas available and the number of building types (0≤m≤10000 \le m \le 1000, 0≤g≤10000 \le g \le 1000, 1≤n≤101 \le n \le 10).

The ii-th of the following nn lines contains three integers aia_i, bib_i, and cic_i, the units of minerals and gas needed to construct a building of type ii, and its defense value (1≤ai≤1001 \le a_i \le 100, 0≤bi≤1000 \le b_i \le 100, 0≤ci≤1000 \le c_i \le 100).

Output

Print a single integer, the maximum total defense capability that can be achieved.

Examples2

  1. Example 1

    Input
    10 10 3
    7 0 6
    6 2 7
    2 5 5
    
    Expected output
    12
    
  2. Example 2

    Input
    11 10 3
    7 0 6
    6 2 7
    2 5 5
    
    Expected output
    16