Phoenix and Berries
Problem Description
Phoenix is picking berries in his backyard. There are n shrubs, and each shrub has ai red berries and bi blue berries.
Each basket can contain k berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color.
For example, if there are two shrubs with 5 red and 2 blue berries in the first shrub and 2 red and 1 blue berries in the second shrub then Phoenix can fill 2 baskets of capacity 4 completely:
- the first basket will contain 3 red and 1 blue berries from the first shrub;
- the second basket will contain the 2 remaining red berries from the first
shrub and 2 red berries from the second shrub.
Help Phoenix determine the maximum number of baskets he can fill completely!
Input
The first line contains two integers n and k ( 1 ≤ n , k ≤ 500 1≤n,k≤500 1≤n,k≤500) — the number of shrubs and the basket capacity, respectively.
The i-th of the next n lines contain two integers a i a_i ai and b i b_i bi ( 0 ≤ a i , b i ≤ 1 0 9 0≤a_i,b_i≤10^9 0≤ai,bi≤109) — the number of red and blue berries in the i-th shrub, respectively.
Output
Output one integer — the maximum number of baskets that Phoenix can fill completely.
Sample Input
2 4
5 2
2 1
Sample Output
2
题意
n棵树,每棵树上有ai个红果实和bi个蓝果实。有可以装k个果实的篮子,一个篮子只能放同种颜色或同一棵树上的果实。求最多可以放满多少个篮子?
题解
显然大多数篮子内的果实都是同颜色的,最多只有n个篮子内的果实是不同色的(若同棵树有多个篮子装的不同色果实,可以将其转为为多个同色篮和一个不同色)。所以只需要考虑每棵树不同颜色的那个篮子的组成。
设 d p [ i ] [ j ] dp[i][j] dp</