Under construction

Still being built. Things may change

I Don't Want to Work

Time limit1sMemory limit1024 MB

Summary
Given N farm cells, M days, and K fruit types with first-harvest time, regrow time, and sale price, find the maximum money earned by day M.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

Farmer Jay owns a farm with NN cells and wants to grow various fruits from day 00 to day MM.

Each day, Jay can do the following at any cell.

  • Plant a seed. If the cell already has a fruit, remove it and plant a new seed.
  • Harvest a fruit.

Jay can grow KK kinds of fruit. The information for the ii-th kind is as follows.

  • It takes AiA_i days for a seed to grow and bear its first fruit.
  • After a harvest, the fruit grows back after BiB_i days.
  • Each harvest yields one fruit, and selling a fruit earns CiC_i money.

Jay wants to earn as much money as possible.

Jay can do as much work as desired each day, and has plenty of seeds of each fruit.

Input

The first line contains NN, MM, and KK separated by spaces. (1≤N≤1000,1≤M≤50,1≤K≤100)(1\le N \le 1000, 1\le M \le 50, 1 \le K \le 100)

The next KK lines contain AiA_i, BiB_i, and CiC_i for the ii-th fruit, separated by spaces. (1≤Ai,Bi≤50,1≤Ci≤1000)(1 \le A_i, B_i \le 50, 1 \le C_i \le 1000)

Output

Print the maximum money Jay can earn by day MM.

Examples1

  1. Example 1

    Input
    1 15 2
    3 2 8
    4 3 15
    
    Expected output
    61