Pagini recente » Cod sursa (job #1593866) | Cod sursa (job #1141692) | Cod sursa (job #1492094) | Cod sursa (job #2760921) | Cod sursa (job #2713273)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
unsigned short n, g;
int smax;
typedef struct obiect_t {
unsigned short val;
unsigned short greutate;
} obiect;
obiect obiecte[5000];
void recursiv(unsigned short k, unsigned short gc, int pc) {
unsigned short tgc=gc+obiecte[k].greutate;
unsigned short valk=obiecte[k].val;
bool nultim=k<n-1;
k++;
if(tgc<=g) {
int tpc=pc+valk;
if(tpc>smax)
smax=tpc;
if(tgc<g && nultim)
recursiv(k,tgc,tpc);
}
if(nultim)
recursiv(k,gc,pc);
}
int main() {
fin>>n>>g;
for(int i=0; i<n; i++)
fin>>obiecte[i].greutate>>obiecte[i].val;
recursiv(0,0,0);
fout<<smax;
return 0;
}