Mai intai trebuie sa te autentifici.
Cod sursa(job #1800646)
Utilizator | Data | 7 noiembrie 2016 22:26:56 | |
---|---|---|---|
Problema | Problema rucsacului | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.67 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int maxg = 10005;
const int maxn = 5005;
//int dp[2][10005];
pair <int, int> v[maxn];
int dp[maxg];
int main()
{
int n, G;
in >> n >> G;
for(int i = 1; i <= n; i++)
in >> v[i].first >> v[i].second;
for(int i = 1; i <= n; i++)
for(int j = G; j >= v[i].first; j--)
dp[j] = max(dp[j], dp[j - v[i].first] + v[i].second);
int mx = -(1 << 30);
for(int i = 1; i <= G; i++)
{
mx = max(mx, dp[i]);
//mx = max(mx, dp[n % 2][i]);
}
out << mx << "\n";
return 0;
}