Mai intai trebuie sa te autentifici.
Cod sursa(job #2337856)
Utilizator | Data | 6 februarie 2019 19:14:10 | |
---|---|---|---|
Problema | Problema rucsacului | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.78 kb |
#include <iostream>
#include <utility>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int m[2][10002];
pair <int, int> date[5001];
int main()
{
int gmax, n, x, y, i, j;
fin >> n >> gmax;
for(i = 0; i < n; i++)
{
fin >> x >> y;
if (x > gmax)
i--, n--;
else
date[i].first = x, date[i].second = y;
}
for(i = 0; i < n; i++)
{
for (j = 0; j <= gmax; j++)
m[0][j] = m[1][j], m[1][j] = 0;
j = 1;
while(j < date[i].first)
m[1][j] = m[0][j], j++;
while(j <= gmax)
m[1][j] = max(m[0][j-date[i].first] + date[i].second, m[0][j]), j++;
}
fout << m[1][gmax];
return 0;
}