Ouyang has 6 kinds of coins.
The number of the i-th coin is A[i] (0<=i<6).
Their value and weight are as follewed:
0. $0.01, 3g
1. $0.05, 5g
2. $0.10, 2g
3. $0.25, 6g
4. $0.50, 11g
5. $1, 8g
Ouyang want to run away from home with his coins.
But he is so weak that he can only carry M gram of coins.
Given the number of each coin he has, what is the maximal value of coins he can take?
There are multiple cases.
Each case has one line with 7 integers: M (1<=M<=10000), A[i], (0<=i<6, 0<=A[i]<=100000).
The maximal value of coins he can take.
1 1 1 1 1 1 1
38 3 1 10 4 2 1
75 8 5 23 4 2 4
$0.00
$2.40
$6.10
for (int w = 0; w <= W; w++)
{
M[0, w] = 0;
}
for (int i = 1; i <= n; i++)
{
for (w = 1; w <= W; w++)
{
if (wt[i] > w)
{
M[i, w] = M[i - 1, w];
}
else
{
M[i, w] = max(M[i - 1][w], v[i] + M[i - 1, w - wt[i]]);
}
}
}
M[i][w] = 0;
int nCount = min(A[i], w / wt[i]);
for(int k = 0; k <= nCount; k++)
{
M[i][w] = max(M[i][w], k * value[i] + M[i - 1][w - k * wt[i]]);
}
#include
#include
using namespace std;
int A[7];
double value[7] = {0, 0.01, 0.05, 0.10, 0.25, 0.50, 1.0};
int wt[7] = {0, 3, 5, 2, 6, 11, 8};
double M[10][11000] = {0};
int main()
{
int W;
while(cin >> W)
{
A[0] = 0;
for(int i = 1; i <= 6; i++)
cin >> A[i];
for(int w = 0; w <= W; w++)
M[0][w] = 0;
for(int i = 0; i <= 6; i++)
M[i][0] = 0;
for(int i = 1; i <= 6; i++)
{
for(int w = 1; w <= W; w++)
{
M[i][w] = 0;
int nCount = min(A[i], w / wt[i]);
for(int k = 0; k <= nCount; k++)
{
M[i][w] = max(M[i][w], k * value[i] + M[i - 1][w - k * wt[i]]); //注意这里是取的是M[i][w]和(k * value[i] + M[i - 1][w - k * wt[i]])中的最大值,而不是M[i - 1][w]和(k * value[i] + M[i - 1][w - k * wt[i]])中的最大值。
}
}
}
cout << "$" << fixed << setprecision(2) << M[6][W] << endl;
}
return 0;
}