Cod sursa(job #1795620)

Utilizator anisca22Ana Baltaretu anisca22 Data 2 noiembrie 2016 18:40:10
Problema Problema rucsacului Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n,g,mat[5][10005];
pair<int,int>p[5002];
int main()
{
    fin>>n>>g;
    for(int i=1;i<=n;i++)
        fin>>p[i].first>>p[i].second;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=g;j++)
        {
            if(i%2==0)
            {
                if(j>=p[i].first)
                    mat[2][j]=max(max(mat[2][j-1],mat[1][j]),mat[1][j-p[i].first]+p[i].second);
                else mat[2][j]=max(mat[2][j-1],mat[1][j]);
            }
            else
            {
                if(j>=p[i].first)
                    mat[1][j]=max(max(mat[1][j-1],mat[2][j]),mat[2][j-p[i].first]+p[i].second);
                else mat[1][j]=max(mat[1][j-1],mat[2][j]);
            }
        }
    if(n%2==0)
        fout<<mat[2][g]<<"\n";
    else fout<<mat[1][g]<<"\n";
    return 0;
}