Similar to a 01 backpack. HS [N] = 1 indicates whether it is reachable.
It can prove that if the meal card costs more than 5, it will inevitably order the most expensive food.
So the problem is to find M-5 within the order. The closer you get, the better. This is the 01 backpack ..
# Include <iostream> using namespace STD; const int n = 1105; int HS [N]; int price [N]; int main () {int n, m; while (scanf ("% d", & N), n) {memset (HS, 0, sizeof (HS); For (INT I = 0; I <N; I ++) scanf ("% d", & Price [I]); scanf ("% d", & M); If (M <5) {cout <m <Endl; // accidentally write the code as cout <5 <Endl; Wa took half a day to locate the problem continue;} int max =-1; int Index =-1; for (INT I = 0; I <n; I ++) // select the most expensive dish {If (price [I]> MAX) {max = price [I]; Index = I ;}} HS [0] = 1; for (INT I = 0; I <n; I ++) // Cai {if (I = index) continue; // For (Int J = 0; j <= m-5; j ++) // note this error for (Int J = M-5; j> = 0; j --) {If (HS [J] = 1) HS [J + price [I] = 1 ;}int money; For (money = M-5; money> = 0; money --) if (HS [money] = 1) break; cout <m-money-max <Endl;} return 0 ;}