MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / main

Function main

CPP/Problems/3_FRACTIONAL_KNAPSACK.CPP:13–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11}
12
13int main()
14{
15 int n;
16 cin>>n;
17
18 vector<pair<int,int>> v(n);
19
20 for(int i=0;i<n;i++)
21 {
22 cin>>v[i].first>>v[i].second;
23 }
24
25 int w;
26 cin>>w;
27
28 sort(v.begin(),v.end(),compare);
29
30 int ans=0;
31
32 for(int i=0;i<n;i++)
33 {
34 if(w>=v[i].second)
35 {
36 ans+=v[i].first;
37 w-=v[i].second;
38 }
39 else
40 {
41 double vw=((double)v[i].first/v[i].second);
42 ans+= vw*w;
43 w=0;
44 break;
45 }
46 }
47
48 cout<<ans<<endl;
49
50}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected