After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.
This morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weight wi and cost ci. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than m, and total cost is maximum possible.
Help Petya to determine maximum possible total cost.
Input
The first line contains two integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 300000) — the number of Petya's souvenirs and total weight that he can carry to the market.
Then n lines follow. ith line contains two integers wi and ci (1 ≤ wi ≤ 3, 1 ≤ ci ≤ 109) — the weight and the cost of ith souvenir.
Output
Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.
Example
1 1
2 1
0
2 2
1 3
2 2
3
4 3
3 10
2 7
2 8
1 1
10
单独考虑仅由1、2 和仅由3组成各种和的最大情况,分别建立数组储存。之后整体扫一遍,记录len1、len2(len1+len2=m),len1、len2分别由上述两种方式组成的和的最大值即可。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <vector> 12 #include <stack> 13 #define mp make_pair 14 #define MIN(a,b) (a>b?b:a) 15 #define MAX(a,b) (a>b?a:b) 16 typedef long long ll; 17 typedef unsigned long long ull; 18 const int MAX=3e5+5; 19 const int INF=1e9+5; 20 const double M=4e18; 21 using namespace std; 22 const int MOD=1e9+7; 23 typedef pair<int,int> pii; 24 const double eps=0.000000001; 25 int n,m; 26 struct Node 27 { 28 ll cnt[4]; 29 ll num; 30 }; 31 Node dp1[MAX],dp2[MAX]; 32 Node tem; 33 ll c[4][MAX],an; 34 int cnt[4]; 35 int w,cost; 36 //int lo1,lo2,lo3; 37 int main() 38 { 39 scanf("%d%d",&n,&m); 40 memset(cnt,0,sizeof(cnt)); 41 for(int i=1;i<=n;i++) 42 { 43 scanf("%d%d",&w,&cost); 44 c[w][++cnt[w]]=cost; 45 } 46 // for(int i=1;i<=3;i++) 47 // cout<<cnt[i]<<endl; 48 for(int i=1;i<=3;i++) 49 sort(c[i]+1,c[i]+cnt[i]+1,greater<int>()); 50 // lo1=1;lo2=lo3=1; 51 tem.cnt[0]=tem.cnt[1]=tem.cnt[2]=tem.cnt[3]=1; 52 tem.num=0; 53 dp1[0]=tem; 54 if(1<=cnt[1]) 55 { 56 dp1[1]=tem; 57 dp1[1].num=c[1][1]; 58 dp1[1].cnt[1]=dp1[0].cnt[1]+1; 59 dp1[1].cnt[2]=dp1[0].cnt[2]; 60 } 61 else 62 dp1[1]=tem; 63 for(int i=2;i<=m;i++) 64 { 65 ll pos1,pos2; 66 pos1=dp1[i-1].num+(dp1[i-1].cnt[1]<=cnt[1]?c[1][dp1[i-1].cnt[1]]:0); 67 pos2=dp1[i-2].num+(dp1[i-2].cnt[2]<=cnt[2]?c[2][dp1[i-2].cnt[2]]:0); 68 // if(i==4) 69 // { 70 // cout<<"~~"<<pos1<<" "<<pos2<<endl; 71 // cout<<lo2<<" "<<cnt[2]<<endl; 72 // } 73 if(pos1>pos2) 74 { 75 dp1[i]=dp1[i-1]; 76 dp1[i].num=pos1; 77 dp1[i].cnt[1]=dp1[i-1].cnt[1]+1; 78 } 79 else 80 { 81 dp1[i]=dp1[i-2]; 82 dp1[i].num=pos2; 83 dp1[i].cnt[2]=dp1[i-2].cnt[2]+1; 84 } 85 // cout<<i<<" "<<dp1[i]<<endl; 86 } 87 dp2[0]=dp2[1]=dp2[2]=tem; 88 // lo3=1; 89 for(int i=3;i<=m;i++) 90 { 91 ll pos=dp2[i-3].num+(dp2[i-3].cnt[3]<=cnt[3]?c[3][dp2[i-3].cnt[3]]:0); 92 if(pos>dp2[i-1].num) 93 { 94 dp2[i]=dp2[i-3]; 95 dp2[i].num=pos; 96 dp2[i].cnt[3]=dp2[i-3].cnt[3]+1; 97 } 98 else 99 dp2[i]=dp2[i-1]; 100 // cout<<i<<" "<<dp2[i]<<endl; 101 } 102 an=0; 103 for(int i=0;i<=m;i++) 104 { 105 an=max(dp1[i].num+dp2[m-i].num,an); 106 } 107 printf("%I64d ",an); 108 return 0; 109 }