Problem Link
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 3000;
const int MAXPRICE = 1010;
long long dp[MAXN][MAXPRICE], albumGreatness[MAXN];
vector<pair<int, int> > album[MAXN];
int main()
{
int n, m, totalPrice, index;
cin >> n >> m >> totalPrice;
int greatness, price, a, albumPrice[m];
for(int i= 0; i < n; i++)
{
cin >> a >> price >> greatness;
//greatness and price of individual song
album[a].push_back(make_pair(price, greatness));
albumGreatness[a] += greatness;
}
for(int i = 1; i <= m; i++)
{
cin >> albumPrice[i];
}
index = 0;
for(int i = 1; i <= m; i++)
{
for(auto songs : album[i])
{
index++;
for(price = 1; price <= totalPrice; price++)
{
dp[index][price] = max(dp[index - 1][price],
(price >= songs.first) ? dp[index - 1][price - songs.first] + songs.second : 0);
}
}
index++;
for(price = 1; price <= totalPrice; price++)
{
dp[index][price] = max(dp[index - 1][price],
(price >= albumPrice[i]) ? dp[index - (int)album[i].size() - 1][price - albumPrice[i]] + albumGreatness[i] : 0);
}
}
cout << dp[index][totalPrice];
}
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 3000;
const int MAXPRICE = 1010;
long long dp[MAXN][MAXPRICE], albumGreatness[MAXN];
vector<pair<int, int> > album[MAXN];
int main()
{
int n, m, totalPrice, index;
cin >> n >> m >> totalPrice;
int greatness, price, a, albumPrice[m];
for(int i= 0; i < n; i++)
{
cin >> a >> price >> greatness;
//greatness and price of individual song
album[a].push_back(make_pair(price, greatness));
albumGreatness[a] += greatness;
}
for(int i = 1; i <= m; i++)
{
cin >> albumPrice[i];
}
index = 0;
for(int i = 1; i <= m; i++)
{
for(auto songs : album[i])
{
index++;
for(price = 1; price <= totalPrice; price++)
{
dp[index][price] = max(dp[index - 1][price],
(price >= songs.first) ? dp[index - 1][price - songs.first] + songs.second : 0);
}
}
index++;
for(price = 1; price <= totalPrice; price++)
{
dp[index][price] = max(dp[index - 1][price],
(price >= albumPrice[i]) ? dp[index - (int)album[i].size() - 1][price - albumPrice[i]] + albumGreatness[i] : 0);
}
}
cout << dp[index][totalPrice];
}
Comments
Post a Comment