This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct Item{
int profit, weight, copy;
Item() {
profit = 0;
weight = 0;
copy = 0;
}
Item(int profit, int weight, int copy){
this->profit=profit;
this->weight=weight;
this->copy=copy;
}
};
static bool cmp(struct Item a, struct Item b){
double r1=(double)a.profit/(double)a.weight;
double r2=(double)b.profit/(double)b.weight;
return r1>r2;
};
int knap(int W, struct Item arr[], int N){
sort(arr, arr+N, cmp);
int finalValue=0;
for(int i=0; i<N; i++){
while(arr[i].weight<=W&&arr[i].copy>0){
W-=arr[i].weight;
finalValue+=arr[i].profit;
arr[i].copy--;
}
}
return finalValue;
};
int main(){
int w,n;
cin>>w>>n;
Item arr[n];
for (int i=0; i<n; i++){
cin>>arr[i].profit>>arr[i].weight>>arr[i].copy;//input array arr dgn struct item
}
cout<<knap(w, arr, n);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |