Pagini recente » Cod sursa (job #2304872) | Cod sursa (job #1413861) | Cod sursa (job #1631153) | Cod sursa (job #2701529) | Cod sursa (job #782712)
Cod sursa(job #782712)
#include <iostream>
#include <fstream>
#include <vector>
#define MAX(a, b) a > b ? a : b
using namespace std;
int n, g;
vector< pair<int, int> > objects;
void function(vector<int> &line, int index, int * result)
{
vector<int> newLine(g + 1, 0);
for(int i = 1; i <= g; i++)
{
newLine[i] = line[i];
int tmp = line[i - objects[index - 1].first] + objects[index - 1].second;
if(line[i] < tmp && i - objects[index - 1].first >= 0)
newLine[i] = tmp;
}
if(index == n)
*result = newLine[g];
else
function(newLine, index + 1, result);
}
int main()
{
ifstream in ("rucsac.in");
ofstream out ("rucsac.out");
int result;
in >> n >> g;
for(int i = 0; i < n; i++)
{
int og, ov;
in >> og >> ov;
objects.push_back(pair<int, int> (og, ov));
}
vector<int> line (g + 1, 0);
function(line, 1, &result);
out << result;
in.close();
out.close();
return 0;
}