Cod sursa(job #2691457)
| Utilizator | Data | 28 decembrie 2020 18:48:06 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
struct ex{
int profit, greutate;
}v[5005];
int n, g, sum, gr;
struct cmp{
bool operator()(ex a, ex b){
return a.profit < b.profit;
}
};
int main(){
in>>n>>g;
for(int i = 1; i <= n; i++)
in>>v[i].greutate>>v[i].profit;
sort(v + 1, v + n + 1, cmp());
int it = n;
while(gr + v[it].greutate <= g && it >= 1){
sum += v[it].profit;
gr += v[it--].greutate;
}
out<<sum;
}
