Practice: The number of such elements is small, and the status is related to the order. You can consider compressing the status.
# Include <cstdio> # include <cstring> const int LMT = 1 <20; int DP [LMT], C [25], R [25]; int max (int A, int B) {return A> B? A: B;} int min (int A, int B) {return a <B? A: B;} void Init (void) {memset (DP,-1, sizeof (DP);} int main (void) {int N, I, j, Lim, boss; while (~ Scanf ("% d", & N) {Init (); for (I = 1; I <n; I ++) scanf ("% d ", & C [I], & R [I]); scanf ("% d", & boss); Lim = 1 <(n-1); DP [0] = 100; for (I = 0; I <Lim; I ++) if (DP [I]! =-1) {for (j = 1; j <n; j ++) if (! (I & (1 <(J-1) & DP [I]> = C [J]) {DP [I | (1 <(J-1)] = max (DP [I | (1 <(J-1)], DP [I] + R [J]-C [J]); DP [I | (1 <(J-1)] = min (DP [I | (1 <(J-1)], 100 );}} if (DP [lim-1] <boss | DP [lim-1] =-1) printf ("Try again \ n"); else printf ("Clear !!! \ N ") ;}return 0 ;}