#include <iostream> #include <cmath> #include <algorithm> using namespace std; long long a[200]; long long smallest_numbers[200][2]; long long biggest_sums[200][2]; long long limit_max_bits = 0; int n; long long m; bool cmp(pair<long long, int> a_, pair<long long, int> b) { return a_.first < b.first; } long long getFirstNumberWithBits(long long number, int bits) { long long max_bits = floor(log2(number)); long long current_bit = 1 << max_bits; int counted_bits = 0; long long result = 0; while (counted_bits < bits && current_bit > 0) { if (number & current_bit) { result += current_bit; counted_bits++; } current_bit >>= 1; } if (counted_bits == bits) { while ((result & 1) == 0) { result >>= 1; } result++; while (result <= number) { result <<= 1; } long long bit_counter = result; counted_bits = 0; while (bit_counter) { bit_counter &= (bit_counter - 1); counted_bits++; } } current_bit = 1; while (counted_bits < bits) { if ((result & current_bit) == 0) { result |= current_bit; counted_bits++; } current_bit <<= 1; } return result; } pair<long long, bool> getBestQuality(int index, long long current_sum, long long current_number) { if (index == n) { return pair<long long, bool>(current_sum, true); } if (current_number > m) { return pair<long long, bool>(0, false); } if (biggest_sums[index][0] >= current_sum && biggest_sums[index][1] <= current_number) { return pair<long long, bool>(0, false); } if (smallest_numbers[index][0] >= current_sum && smallest_numbers[index][1] <= current_number) { return pair<long long, bool>(0, false); } if (current_sum > biggest_sums[index][0]) { biggest_sums[index][0] = current_sum; biggest_sums[index][1] = current_number; } if (current_number < smallest_numbers[index][0]) { smallest_numbers[index][0] = current_sum; smallest_numbers[index][1] = current_number; } int i_start = 0; if (current_number != 0 || index != 0) { i_start = 1; } long long highest = 0; bool highest_set = false; pair<long long, int>* numbers = new pair<long long, int>[limit_max_bits + 1]; for (int i = i_start; i <= limit_max_bits; i++) { long long number = 0; if (i != 0) number = getFirstNumberWithBits(current_number, i); numbers[i].first = number; numbers[i].second = i; } sort(numbers, numbers + limit_max_bits + 1, cmp); long long extreme = (a[index] > 0 ? -1000 : 1000); for (int i = i_start; i <= limit_max_bits; i++) { if (numbers[i].first > m) continue; if (a[index] > 0 && numbers[i].second <= extreme) continue; if (a[index] < 0 && numbers[i].second >= extreme) continue; if (a[index] == 0 && i > i_start) break; extreme = numbers[i].second; long long second_quality = a[index] * numbers[i].second; pair<long long, bool> result = getBestQuality(index + 1, current_sum + second_quality, numbers[i].first); if ((result.first > highest || !highest_set) && result.second) { highest = result.first; highest_set = true; } } delete [] numbers; return pair<long long, bool>(highest, highest_set); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; limit_max_bits = floor(log2(m)) + 1; for (int i = 0; i < 200; i++) { biggest_sums[i][0] = smallest_numbers[i][0] = -9223372036854775807LL; biggest_sums[i][1] = smallest_numbers[i][1] = 9223372036854775807LL; } for (int i = 0; i < n; i++) { cin >> a[i]; } cout << getBestQuality(0, 0, 0).first << "\n"; 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <iostream> #include <cmath> #include <algorithm> using namespace std; long long a[200]; long long smallest_numbers[200][2]; long long biggest_sums[200][2]; long long limit_max_bits = 0; int n; long long m; bool cmp(pair<long long, int> a_, pair<long long, int> b) { return a_.first < b.first; } long long getFirstNumberWithBits(long long number, int bits) { long long max_bits = floor(log2(number)); long long current_bit = 1 << max_bits; int counted_bits = 0; long long result = 0; while (counted_bits < bits && current_bit > 0) { if (number & current_bit) { result += current_bit; counted_bits++; } current_bit >>= 1; } if (counted_bits == bits) { while ((result & 1) == 0) { result >>= 1; } result++; while (result <= number) { result <<= 1; } long long bit_counter = result; counted_bits = 0; while (bit_counter) { bit_counter &= (bit_counter - 1); counted_bits++; } } current_bit = 1; while (counted_bits < bits) { if ((result & current_bit) == 0) { result |= current_bit; counted_bits++; } current_bit <<= 1; } return result; } pair<long long, bool> getBestQuality(int index, long long current_sum, long long current_number) { if (index == n) { return pair<long long, bool>(current_sum, true); } if (current_number > m) { return pair<long long, bool>(0, false); } if (biggest_sums[index][0] >= current_sum && biggest_sums[index][1] <= current_number) { return pair<long long, bool>(0, false); } if (smallest_numbers[index][0] >= current_sum && smallest_numbers[index][1] <= current_number) { return pair<long long, bool>(0, false); } if (current_sum > biggest_sums[index][0]) { biggest_sums[index][0] = current_sum; biggest_sums[index][1] = current_number; } if (current_number < smallest_numbers[index][0]) { smallest_numbers[index][0] = current_sum; smallest_numbers[index][1] = current_number; } int i_start = 0; if (current_number != 0 || index != 0) { i_start = 1; } long long highest = 0; bool highest_set = false; pair<long long, int>* numbers = new pair<long long, int>[limit_max_bits + 1]; for (int i = i_start; i <= limit_max_bits; i++) { long long number = 0; if (i != 0) number = getFirstNumberWithBits(current_number, i); numbers[i].first = number; numbers[i].second = i; } sort(numbers, numbers + limit_max_bits + 1, cmp); long long extreme = (a[index] > 0 ? -1000 : 1000); for (int i = i_start; i <= limit_max_bits; i++) { if (numbers[i].first > m) continue; if (a[index] > 0 && numbers[i].second <= extreme) continue; if (a[index] < 0 && numbers[i].second >= extreme) continue; if (a[index] == 0 && i > i_start) break; extreme = numbers[i].second; long long second_quality = a[index] * numbers[i].second; pair<long long, bool> result = getBestQuality(index + 1, current_sum + second_quality, numbers[i].first); if ((result.first > highest || !highest_set) && result.second) { highest = result.first; highest_set = true; } } delete [] numbers; return pair<long long, bool>(highest, highest_set); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; limit_max_bits = floor(log2(m)) + 1; for (int i = 0; i < 200; i++) { biggest_sums[i][0] = smallest_numbers[i][0] = -9223372036854775807LL; biggest_sums[i][1] = smallest_numbers[i][1] = 9223372036854775807LL; } for (int i = 0; i < n; i++) { cin >> a[i]; } cout << getBestQuality(0, 0, 0).first << "\n"; return 0; } |