http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2627
/*
3 100
1 102
2 104
100 300
输出:106
*/

#include
<iostream>
#include
<algorithm>
using namespace std;
struct Node
{
    
int normalPrice;
    
int magicPrice;
}
;
int main()
{
    
int n,cost;
    
while(cin>>n>>cost)
    
{
        
string str;
        Node infor[
1001];
        
int i,j,k,a,b,cc;
        
char ch;
        
for(cc=0,k=0,i=1;i<=n;i++)
        
{
            cin
>>a;
            ch
=getchar();
            
if(ch=='\n')//是换行符表示该行只有一个数
            {
                cc 
+= a;
                
continue;
            }

            
else
            
{                
                cin
>>b;
                
if(b-a>cost)//只能是有挣才会卖魔法价
                {
                    infor[k].normalPrice 
= a;
                    infor[k
++].magicPrice = b;
                }

                
else
                    cc 
+= a;
            }

        }

        
if(k==0 || cc >= cost)
        
{
            
for(i=0;i<k;i++)
                cc 
+= infor[i].magicPrice;
            cc 
-= cost*k;
        }

        
else
        
{
            
int temp;
            
int remain = cost-cc;//购买鉴定卷轴欠缺的钱
            int *dp = new int[remain+1];//dp[i]表示还差i元钱时损耗的最小价值
            for(i=0;i<=remain;i++)
                dp[i]
=100000000;
            dp[
0= 0;
            
for(j=0;j<k;j++)//物品
            {
                temp 
= infor[j].magicPrice - infor[j].normalPrice - cost;//记得减去cost
                
//temp表示亏损值
                if(infor[j].normalPrice >= remain && dp[remain] > temp )//正常价格够买鉴定卷轴
                    dp[remain] = temp;
                
else
                
{
                    
for( i=remain - infor[j].normalPrice ; i <= remain ; i++)
                    
//i从remain - infor[j].normalPrice
                        if(dp[remain] > dp[i] + temp) 
                            dp[remain] 
= dp[i] + temp;
                    
for( i=remain ; i >= infor[j].normalPrice ; i-- )
                        
if(dp[i] > dp[i-infor[j].normalPrice] + temp)
                            dp[i] 
= dp[i-infor[j].normalPrice] + temp;
                }

            }

            
if(dp[remain] == 100000000)//无法以魔法价格出售
            {
                
for(i=0;i<k;i++)
                    cc 
+= infor[i].normalPrice;
            }

            
else
            
{
                
for(i=0;i<k;i++)
                    cc 
+= infor[i].magicPrice;
                cc 
-= cost*+ dp[remain];//记得要扣掉买鉴定卷轴和损耗的钱
            }

        }

        cout
<<cc<<endl;
    }

    
return 0;
}