-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkanpsack.cpp
More file actions
126 lines (117 loc) · 2.47 KB
/
Copy pathkanpsack.cpp
File metadata and controls
126 lines (117 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<utility>
#include<memory>
#include<cmath>
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const int MAX_N=202,MAX_X=200001;
int N;
ll v[MAX_N],w[MAX_N],dp[MAX_N][MAX_X];
ll W,sum_w,sum_v;
ll calc1(){
//Nが30以下 -> 半分全列挙
vector<PL> p1,p2;
int n1=N/2;
int n2=N-n1;
rep(i,1<<n1){
ll tw=0,tv=0;
rep(j,n1){
if(i>>j & 1){
tw+=w[j]; tv+=v[j];
}
}
p1.pb({tw,tv});
}
sort(ALL(p1));
int m=1;
p2.pb(p1[0]);
REP(i,1,1<<n1){
if(p2[m-1].second<p1[i].second){
//順序がおかしいものはスキップ,必要なpairのvalueより大きいものを取っていく
p2.pb(p1[i]);
m++;
}
}
ll res=0;
rep(i,1<<n2){
ll sw=0,sv=0;
rep(j,n2){
if(i>>j & 1){
sw+=w[n1+j]; sv+=v[n1+j];
}
}
if(sw>W) continue;
ll tv=(lower_bound(p2.begin(),p2.end(),PL(W-sw+1,0))-1)->second;
res=max(res,sv+tv);
}
return res;
}
ll calc2(){
//wが全て1000以下
if(W>=sum_w){
//全部入る
ll S=0;
rep(i,N) S+=v[i];
return S;
}
fill(dp[0],dp[MAX_N-1],0);
//dp[i][j]:=i番目までの荷物で重さjの荷物を持った時の価値の最大値
rep(i,N){
rep(j,W+1){
if(j-w[i]>=0) dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
else dp[i+1][j]=dp[i][j];
}
}
return dp[N][W];
}
ll calc3(){
//vが全て1000以下
fill(dp[0],dp[MAX_N-1],INF);
rep(i,N+1) dp[i][0]=0;
//dp[i][j]:=i番目までの荷物でjの価値を達成した時の重さの最小値
rep(i,N){
rep(j,sum_v){
dp[i+1][j]=min(dp[i][max(0LL,j-v[i])]+w[i],dp[i][j]);
}
}
ll res;
rep(j,sum_v){
if(dp[N][j+1]>W){
res=j;
break;
}
}
return res;
}
int main(){
cin >> N >> W ;
ll max_w=sum_w=sum_v=0;
rep(i,N){
scanf("%lld%lld",&v[i],&w[i]);
max_w=max(max_w,w[i]);
sum_w+=w[i];
sum_v+=v[i];
}
//cout << calc2() << endl;
if(N<=30) printf("%lld\n",calc1());
else if(max_w<=1000) printf("%lld\n",calc2());
else printf("%lld\n",calc3());
return 0;
}