1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #define gc() getchar_unlocked() #define pc(x) putchar_unlocked(x) #include <stdio.h> #include <memory.h> int had[1<<20+1], N, L, answer=0, cals[20], happys[20]; int readINT() { int N = gc(), ret = 0; do { ret = (ret << 3) + (ret << 1) + (N & 0b1111); N = gc(); } while ('0' <= N); return ret; } void writeANSWER(int n) { int rev = 0, count = 0; pc(0x23); while (!(n % 10)) { count++; n /= 10; } while (n) { rev = (rev << 3) + (rev << 1) + n % 10; n /= 10; } while (rev) { pc(rev % 10 | 0b110000); rev /= 10; } while (count--) pc(0x30); count = 0; pc(0x20); while (!(answer % 10)) { count++; answer /= 10; } while (answer) { rev = (rev << 3) + (rev << 1) + answer % 10; answer /= 10; } while (rev) { pc(rev % 10 | 0b110000); rev /= 10; } while (count--) pc(0x30); pc(0x0A); return; } void simulation(int cal, int picked, int happiness) { if (had[picked]) return; had[picked] = cal; answer = answer > happiness ? answer : happiness; for (int i = 0; i < N; i++) { if (picked&(1 << i)) continue; if (cal + cals[i] < L) simulation(cal + cals[i], picked + (1 << i), happiness + happys[i]); } } int main(int argc, char** argv) { int tc, T = readINT(); for (tc = 1; tc <= T; tc++) { N = readINT(), L = readINT(); answer = 0; memset(had, 0, sizeof(had)); for (int i = 0; i < N; i++) { happys[i] = readINT(); cals[i] = readINT(); } simulation(0, 0, 0); writeANSWER(tc); } }
|