#GYM104707A2. Project Allocation (Full)
Project Allocation (Full)
Description
Marta has two employees named Arda and Bimala. For each of the next $n$ days, she will receive a project and must choose which employee to assign it to. Arda and Bimala will complete the $i$th project with quality $a_i$ and $b_i$ respectively. However, if at any point either employee is assigned more than $k$ projects more than their colleague, they will complain that the allocation is unfair.
Help Marta decide the maximum total quality that her team can achieve without complaints.
The first line of input consists of two space-separated integers $n$ ($1 \le n \le 1000$) and $k$ ($\textcolor{red}{1 \le k \le n}$), representing the number of projects and the largest tolerable difference in the number of projects assigned to each employee.
$n$ lines follow, the $i$th of which consists of two space-separated integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le {10}^6$), representing the quality of Arda's and Bimala's work respectively for task $i$.
Output a single integer, the maximum total quality that can be achieved without any complaints.
Input
The first line of input consists of two space-separated integers $n$ ($1 \le n \le 1000$) and $k$ ($\textcolor{red}{1 \le k \le n}$), representing the number of projects and the largest tolerable difference in the number of projects assigned to each employee.
$n$ lines follow, the $i$th of which consists of two space-separated integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le {10}^6$), representing the quality of Arda's and Bimala's work respectively for task $i$.
Output
Output a single integer, the maximum total quality that can be achieved without any complaints.
2 1
2 1
3 1
5 1
2 6
7 1
1 4
1 10
3 5
5 2
2 6
7 1
1 4
1 10
3 5
4
29
30
Note
In the first sample case Arda would produce better quality for either project, but he will complain if he is assigned the first two projects at which point Bimala has none. Therefore Bimala should be assigned project 1 and Arda project 2, for a total quality of 4.
In the second sample case Arda can be given projects 2 and 3, and Bimala projects 1, 4 and 5, for a total quality of $6 + 7 + 1 + 10 + 5 = 29$. At no point does either employee have more than one project more than their colleague.
In the third sample case Arda can be given projects 2 and 5, and Bimala projects 1, 3 and 4, for a total quality of $6 + 7 + 4 + 10 + 3 = 30$. Note that while the overall allocation gives Bimala only one more project than Arda, she would still complain if $k$ was 1 rather than 2, since after the first four projects she has three while Arda only has one.
Note that the third sample case is not a valid input for the subtask, as $k > 1$.