Cod sursa(job #1590534)

Utilizator Vele_GeorgeVele George Vele_George Data 5 februarie 2016 11:41:20
Problema Problema rucsacului Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#define nmax 10005
using namespace std;

int n, g, w, p, a[nmax], b[nmax];
int *first =a, *second =b, *aux;

int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");

    fin >> n >> g;

    for(int j=0; j<=g; j++)
    {
        *(first+j) = 0;
     }

    for(int i=1; i<=n; i++)
    {
        fin >> w >> p;
        for(int j=0; j<=g; j++)
        {
            if (j-w <0) *(second+j) = *(first+j);
                  else  *(second+j) = max( *(first+j-w)+p, *(first+j) );
        }
        aux = first;
        first = second;
        second = aux;
    }

    fout << *(second + g);
    fin.close();
    fout.close();
    return 0;
}