Http://poj.org/problem? Id = 1276
There is a cash machine containing T currencies with a nominal value of N [I], each with a V [I. The maximum amount that can be exchanged if the exchange amount does not exceed the cash RMB.
Multiple backpacks are converted into 01 backpacks, which require binary optimization. 9. The answer to this story is quite good. Add a coefficient before the value of each item and ensure that the combination of these coefficients includes all the numbers of 1. N and the sum is N.
Code:
# Include <cstdio>
# Include <cstring>
# Define max (A, B) A> B? A: B
Int DP [100005];
Int Val [105];
Int main (){
Int cash, N, V, G, I, J, count;
While (~ Scanf ("% d", & cash, & N )){
If (! Cash |! N ){
While (n --)
Scanf ("% d", & G, & V );
Printf ("0 \ n ");
Continue;
}
Count = 0;
Memset (DP, 0, sizeof (DP ));
While (n --){
Scanf ("% d", & G, & V );
// Binary Optimization
I = 1;
While (G> = I ){
Val [count ++] = I * V;
G-= I;
I * = 2;
}
If (g) Val [count ++] = V * g;
}
// 01 solve a backpack
For (I = 0; I <count; I ++ ){
For (j = cash; j> = Val [I]; j --){
DP [J] = max (DP [J], DP [J-Val [I] + val [I]);
}
}
Printf ("% d \ n", DP [Cash]);
}
Return 0;
}