# PAT「1002 Business (35分)」

## 1. 题目

### Description

As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given $3$ projects as the following:

• Project[1] takes $3$ days, it must be finished in $3$ days in order to gain $6$ units of profit.
• Project[2] takes $2$ days, it must be finished in $2$ days in order to gain $3$ units of profit.
• Project[3] takes $1$ day only, it must be finished in $3$ days in order to gain $4$ units of profit.

You may take Project[1] to gain $6$ units of profit. But if you take Project[2] first, then you will have $1$ day left to complete Project[3] just in time, and hence gain $7$ units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

### Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer $N$ ($\leq 50$), and then followed by $N$ lines of projects, each contains three numbers $P$, $L$, and $D$ where $P$ is the profit, $L$ the lasting days of the project, and $D$ the deadline. It is guaranteed that $L$ is never more than $D$, and all the numbers are non-negative integers.

### Output Specification:

For each test case, output in a line the maximum profit you can gain.

## 2. 题解

### 分析

• $c + p[j].lasting \leq p[j].ddl$
• $c + p[j].lasting + p[a_1].lasting \leq p[a_1].ddl$
• $\cdots$
• $c + p[j].lasting + p[a_1].lasting + \cdots + p[a_m].lasting \leq p[a_m].ddl$
• $c + p[j].lasting + p[a_1].lasting + \cdots + p[a_m].lasting + p[i].lasting \leq p[i].ddl$

• $c + p[i].lasting \leq p[i].ddl$
• $c + p[i].lasting + p[a_1].lasting \leq p[i].ddl \leq p[a_1].ddl$
• $\cdots$
• $c + p[i].lasting + p[a_1].lasting + \cdots + p[a_m].lasting \leq p[i].ddl \leq p[a_m].ddl$
• $c + p[i].lasting + p[a_1].lasting + \cdots + p[a_m].lasting + p[j].lasting \leq p[i].ddl \leq p[j].ddl$

$f[j][i] = \begin{cases} \max\{f[j][i+1], f[j+p[i+1].lasting][i+1]+p[i].profit\} & \text{ if } j+p[i].lasting \leq p[i].ddl, 0 \leq i < n \\ f[j][i] = f[j][i+1] & \text{ if } j+p[i].lasting > p[i].ddl, 0 \leq i < n \\ 0 & \text{ if } i = n \end{cases}$