#P11349. Maximizing Satisfaction in Contest Submissions

    ID: 13427 Type: Default 1000ms 256MiB

Maximizing Satisfaction in Contest Submissions

Maximizing Satisfaction in Contest Submissions

Stuart can submit his problems to \(c\) contests. When he submits a problem to the \(i\)-th contest, his satisfaction increases by \(s_i\). However, the \(i\)-th contest only accepts problems with quality at least \(m_i\). Each contest can accept multiple problems, and every submission provides an additional \(s_i\) satisfaction.

Stuart has prepared \(p\) problems. He estimates that the quality of the \(j\)-th problem is \(q_j\). However, due to the difficulty of problem preparation, submitting the \(j\)-th problem to any contest decreases his satisfaction by \(d_j\). Note that each problem can be submitted to at most one contest, or he may choose not to submit it.

Help Stuart find an optimal submission plan to maximize his total satisfaction. If all submission plans yield negative satisfaction, he can choose not to submit any problem, resulting in a total satisfaction of 0.

The net satisfaction gain from submitting problem \(j\) to contest \(i\) is \(s_i - d_j\) (provided that \(q_j \ge m_i\)). For each problem, he should submit it to the contest that gives the maximum \(s_i\) among those whose requirement \(m_i\) is at most \(q_j\), as long as \(s_i - d_j > 0\). Otherwise, he should not submit that problem.

inputFormat

The first line contains two integers \(c\) and \(p\) (\(1 \le c, p \le 10^5\)), representing the number of contests and the number of problems, respectively.

The following \(c\) lines each contain two integers \(m_i\) and \(s_i\) (\(1 \le m_i, s_i \le 10^9\)) where \(m_i\) is the minimum quality required by the \(i\)-th contest and \(s_i\) is the satisfaction gained for a submission in that contest.

The next \(p\) lines each contain two integers \(q_j\) and \(d_j\) (\(1 \le q_j, d_j \le 10^9\)) where \(q_j\) is the quality of the \(j\)-th problem and \(d_j\) is the penalty in satisfaction for submitting that problem.

outputFormat

Output a single integer representing the maximum total satisfaction that Stuart can achieve.

sample

2 3
10 5
15 10
12 3
16 6
11 2
9