#include
<
iostream
>
#include < vector >
#include < set >
#include < algorithm >
using namespace std;
class CollectingMarbles
... {
public:
int mostMarbles(vector<int>, int, int);
int run(int, int, int);
} ;
int dp[ 8192 ][ 11 ][ 21 ];
vector < int > w;
int bC;
int CollectingMarbles::run( int mask, int left, int cur)
... {
if(left == 0) return 0;
if(dp[mask][left][cur] != -1) return dp[mask][left][cur];
dp[mask][left][cur] = 0;
int n = int(w.size());
for (int i=0;i<n;++i)...{
if((mask & (1 << i)) == 0 && w[i] <= cur)
dp[mask][left][cur] = max(dp[mask][left][cur], run(mask | (1 << i), left, cur - w[i]) + 1);
dp[mask][left][cur] = max(dp[mask][left][cur], run(mask, left - 1, bC));
}
return dp[mask][left][cur];
}
int CollectingMarbles::mostMarbles(vector < int > marblesWeights, int bagCapacity, int numberOfBags)
... {
w=marblesWeights;
bC=bagCapacity;
memset(dp, -1, sizeof(dp));
return run(0, numberOfBags, bagCapacity);
}
#include < vector >
#include < set >
#include < algorithm >
using namespace std;
class CollectingMarbles
... {
public:
int mostMarbles(vector<int>, int, int);
int run(int, int, int);
} ;
int dp[ 8192 ][ 11 ][ 21 ];
vector < int > w;
int bC;
int CollectingMarbles::run( int mask, int left, int cur)
... {
if(left == 0) return 0;
if(dp[mask][left][cur] != -1) return dp[mask][left][cur];
dp[mask][left][cur] = 0;
int n = int(w.size());
for (int i=0;i<n;++i)...{
if((mask & (1 << i)) == 0 && w[i] <= cur)
dp[mask][left][cur] = max(dp[mask][left][cur], run(mask | (1 << i), left, cur - w[i]) + 1);
dp[mask][left][cur] = max(dp[mask][left][cur], run(mask, left - 1, bC));
}
return dp[mask][left][cur];
}
int CollectingMarbles::mostMarbles(vector < int > marblesWeights, int bagCapacity, int numberOfBags)
... {
w=marblesWeights;
bC=bagCapacity;
memset(dp, -1, sizeof(dp));
return run(0, numberOfBags, bagCapacity);
}
Problem Statement | |||||||||||||
Maybe you don't know, but you are a fanatic of colored marbles. You want to collect them all, but you may not be able to carry them. You have numberOfBags bags, each of capacity bagCapacity grams. The i-th marble has a weight of marblesWeight[i] grams. You can go to the shop only once and you have enough money to buy all of the marbles. Return the largest number of marbles that you can carry home in the given bags. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | marblesWeights will contain between 1 and 13 elements, inclusive. | ||||||||||||
- | Each element of marblesWeights will be between 1 and 20, inclusive. | ||||||||||||
- | bagCapacity will be between 1 and 20, inclusive. | ||||||||||||
- | numberOfBags will be between 1 and 10, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.