Cod sursa(job #2000245)

Utilizator zanugMatyas Gergely zanug Data 13 iulie 2017 10:56:01
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

const int N = 1e5 + 10;

int wid[N];
int val[N];
int x[N], y[N];
int* xx = x;
int* yy = y;
int n, k;

int main()
{
    fin >> n >> k;

    for(int i = 1; i <= n; ++i)
        fin >> wid[i] >> val[i];

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= k; ++j)
        {
            if(j < wid[i])
            {
                yy[j] = xx[j];
            }
            else
            {
                yy[j] = max(xx[j], xx[j-wid[i]] + val[i]);
            }
//            cout << yy[j] << " ";
        }
//        cout << "\n";

        int* ures = yy;
        yy = xx;
        xx = ures;
    }

    fout << xx[k];

    return 0;
}