Cod sursa(job #3277899)

Utilizator tolickAnatol tolick Data 17 februarie 2025 21:00:53
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.44 kb
// ConsoleApplication27.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;



/*
int n, k;
vector < vector <long long int>> d(101, vector <long long int>(101));
void Stirling(int n, int k)
{
    d[1][1] = 1;
    for (int i = 2; i <= n; i++)
    {
        for (int j = 1; j <= min(i,k); j++)
        {
            d[i][j] = ( d[i - 1][j - 1] + (i - 1) * d[i - 1][j]) % 1000000007;
            cout << d[i][j] << endl;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= min(i, k); j++)
        {
            cout << d[i][j] << " ";
        }
        cout << endl;
    }
    cout << d[n][k];
}
*/







int main()
{
    /*
    cin >> n >> k;
    Stirling(n, k);
    */
    ifstream fin("rucsac.in");
    int n, W;
    fin >> n;
    fin >> W;

    vector <vector <short>> d(n + 1, vector<short>(W + 1));
    vector <short> w(n + 1);
    vector <short> v(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int greutate, cost;
        fin >> greutate >> cost;
        w[i] = greutate;
        v[i] = cost;
    }
    fin.close();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= W; j++)
        {
            if (w[i] > j)
            {
                d[i][j] = d[i - 1][j];
            }
            else
            {
                d[i][j] = max(d[i - 1][j], d[i - 1][j - w[i]] + v[i]);

            }
            
        }
    }
    /*
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= W; j++)
        {
            cout << d[i][j] << " ";
        }
        cout << endl;
    }
    */
    ofstream fout("rucsac.out");
    fout << d[n][W];
    fout.close();


}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file