#include <cstdio> #include <cmath> #define MAX_LENGTH 200 int bitsSet(int num) { int count = 0; while(num > 0) { if(num & 1 == 1) { count++; } num >>= 1; } return count; } int hcw(int weightBit, int maxWeight) { int max = maxWeight; int set = bitsSet(maxWeight); int mask = 1; while(set > weightBit && (mask <= maxWeight || maxWeight >= 0)) { //if(maxWeight & mask == 0) { // mask <<= 1; // continue; //} //maxWeight ^= mask; //mask <<= 1; //set--; //printf("here\n"); maxWeight--; set = bitsSet(maxWeight); } while(set < weightBit && maxWeight >= 0) { maxWeight--; set = bitsSet(maxWeight); } if(set == weightBit && maxWeight <= max) { return maxWeight; } return -1; } int** memo; // memo int minBit = 0; int maxQuality(int song[MAX_LENGTH], int songLength, int maxWeight, int maxWeightBit, int i, int* res) { if(i < 0) { *res = 0; return 0; } int currentMax = -999999999; if(true || song[i] > 0) { for(int weightBit = maxWeightBit; weightBit >= minBit; weightBit--) { int highestCorrespondingWeight = hcw(weightBit, maxWeight); if(highestCorrespondingWeight == -1) { continue; } int nextVal; int next = 0; next = maxQuality(song, songLength, highestCorrespondingWeight - 1, log(highestCorrespondingWeight)/log(2), i - 1, &nextVal); if(next != 0) { *res = 0; return 1; } int current = song[i] * weightBit + nextVal; if(current > currentMax) { //printf("%d %d\n", i, weightBit); currentMax = current; } //break; } } else { for(int weightBit = 0; weightBit < maxWeightBit; weightBit++) { int highestCorrespondingWeight = hcw(weightBit, maxWeight); if(highestCorrespondingWeight == -1) { continue; } int nextVal; int next = maxQuality(song, songLength, highestCorrespondingWeight - 1, log(highestCorrespondingWeight)/log(2), i - 1, &nextVal); if(next != 0) { *res = 0; return 1; } int current = song[i] * weightBit + nextVal; if(current > currentMax) { //printf("%d %d\n", i, weightBit); currentMax = current; } break; } } *res = currentMax; return 0; } int main() { int n, max; scanf("%d", &n); scanf("%d", &max); int bitMax = log(max)/log(2); int song[MAX_LENGTH]; int weights[MAX_LENGTH]; for(int i=0;i<n;i++) { scanf("%d", &song[i]); } int res; int status = maxQuality(song, n, max, bitMax, n-1, &res); if(status != 0) { printf("Failure\n"); } printf("%d\n", res); return 0; }
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include <cstdio> #include <cmath> #define MAX_LENGTH 200 int bitsSet(int num) { int count = 0; while(num > 0) { if(num & 1 == 1) { count++; } num >>= 1; } return count; } int hcw(int weightBit, int maxWeight) { int max = maxWeight; int set = bitsSet(maxWeight); int mask = 1; while(set > weightBit && (mask <= maxWeight || maxWeight >= 0)) { //if(maxWeight & mask == 0) { // mask <<= 1; // continue; //} //maxWeight ^= mask; //mask <<= 1; //set--; //printf("here\n"); maxWeight--; set = bitsSet(maxWeight); } while(set < weightBit && maxWeight >= 0) { maxWeight--; set = bitsSet(maxWeight); } if(set == weightBit && maxWeight <= max) { return maxWeight; } return -1; } int** memo; // memo int minBit = 0; int maxQuality(int song[MAX_LENGTH], int songLength, int maxWeight, int maxWeightBit, int i, int* res) { if(i < 0) { *res = 0; return 0; } int currentMax = -999999999; if(true || song[i] > 0) { for(int weightBit = maxWeightBit; weightBit >= minBit; weightBit--) { int highestCorrespondingWeight = hcw(weightBit, maxWeight); if(highestCorrespondingWeight == -1) { continue; } int nextVal; int next = 0; next = maxQuality(song, songLength, highestCorrespondingWeight - 1, log(highestCorrespondingWeight)/log(2), i - 1, &nextVal); if(next != 0) { *res = 0; return 1; } int current = song[i] * weightBit + nextVal; if(current > currentMax) { //printf("%d %d\n", i, weightBit); currentMax = current; } //break; } } else { for(int weightBit = 0; weightBit < maxWeightBit; weightBit++) { int highestCorrespondingWeight = hcw(weightBit, maxWeight); if(highestCorrespondingWeight == -1) { continue; } int nextVal; int next = maxQuality(song, songLength, highestCorrespondingWeight - 1, log(highestCorrespondingWeight)/log(2), i - 1, &nextVal); if(next != 0) { *res = 0; return 1; } int current = song[i] * weightBit + nextVal; if(current > currentMax) { //printf("%d %d\n", i, weightBit); currentMax = current; } break; } } *res = currentMax; return 0; } int main() { int n, max; scanf("%d", &n); scanf("%d", &max); int bitMax = log(max)/log(2); int song[MAX_LENGTH]; int weights[MAX_LENGTH]; for(int i=0;i<n;i++) { scanf("%d", &song[i]); } int res; int status = maxQuality(song, n, max, bitMax, n-1, &res); if(status != 0) { printf("Failure\n"); } printf("%d\n", res); return 0; } |