#include <algorithm> #include <iostream> #include <vector> #include <bitset> #include <climits> #include <set> #include <cassert> using namespace std; #define ll long long #define ull unsigned ll #define int2 pair<int, int> #define ll2 pair<ll, ll> #define ull2 pair<ull, ull> #define mp make_pair #define x first #define y second #define forEach(a) for(int i = 0; i < a.size(); i++) const int size = 1e5 + 9; vector<ll> tab; int N; ll M; int mlog; ll changeValue(ll v, int pos, bool one, bool increasing) { // if (pos == 0) { // if (increasing) // return v | 1; // return v - (v % 2); // } v >>= pos; if (increasing) v |= (ll)1; else v -= (v % 2); v <<= pos; if (one) v -= 1; if (increasing) v |= (ll)1 << pos; return v; } //__builtin_popcountll void nextSet(set<ll2> &values, set<ll2> &out, ll a, int ind, bool increasing) {// x = sum, y = val at b[i] // set<ll2> out; // cout << "Values size: " << values.size() << " " << a << "\n"; bool positive = a > 0; set<ll2>::iterator it = values.begin(); while (it != values.end()) { ll2 p = *it; ll val = p.y; for (int i = mlog; i >= 0; i--) { ll flag = (ll)1 << i; if (flag > M) continue; // cout << i << "Nex value: " << flag << "\n"; ll newVal; if ((val & flag) > 0 && !increasing) newVal = changeValue(val, i, positive, increasing); else if ((val & flag) == 0 && increasing) newVal = changeValue(val, i, positive, increasing); else continue; if (newVal <= M) { // bool addIt = newVal <= M; ll bits = __builtin_popcountll(newVal); //cout << "Bits " << bits << "\n"; ll newSum = p.x + a * bits; //cout << i << " New sum: " << newSum << "\n"; // if (!addIt && increasing) // break; set<ll2>::iterator it2 = out.upper_bound(mp(newSum, LLONG_MIN)); if (it2 != out.end()) { ll val2 = (*it2).y; //cout << "Val2: " << val2 << "\n"; if (newVal >= val2 && increasing) { // if (positive != increasing) // continue; break;//addIt = false; //cout << "Breaking: " << newSum << ", " << newVal << " a: " << a << " " << val2 << " " << p.x << "\n"; // continue; } else if (newVal <= val2 && !increasing) { // if (positive != increasing) // continue; break; } } // if (addIt) { //cout << "Adding: " << newSum << ", " << newVal << " a: " << a << "\n"; out.insert(mp(newSum, newVal)); // } // else //cout << "Not Adding: " << newSum << ", " << newVal << "\n"; } } if (ind == N - 1) { ll newVal = M; ll bits = __builtin_popcountll(newVal); ll newSum = p.x + a * bits; bool ok = true; set<ll2>::iterator it2 = out.upper_bound(mp(newSum, LLONG_MIN)); if (it2 != out.end()) { ll val2 = (*it2).y; if (newVal >= val2 && increasing) ok = false; else if (newVal <= val2 && !increasing) ok = false; } if (ok) out.insert(mp(newSum, newVal)); } it++; } } ll maxFromSet(set<ll2> &values) { set<ll2>::iterator it = values.begin(); ll out = LLONG_MIN; while (it != values.end()) { out = max(out, (*it).x); // cout << "Out: " << out << "\n"; it++; } return out; } void makeSet(int start, int end, set<ll2> &out, bool rev) { set<ll2> current[2]; if (!rev) current[start % 2].insert(mp((ll)0, (ll)0)); else current[(end - 1) % 2].insert(mp((ll)0, (ll)M + 1)); if (rev) { for(int i = end - 1; i >= start; i--) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, !rev); } swap(current[(start + 1) % 2], out); } else { for(int i = start; i < end; i++) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, !rev); } swap(current[end % 2], out); } } ll iterateMax(set<ll2> &av, set<ll2> &bv) { ll out = LLONG_MIN; if (bv.size() == 0) return out; set<ll2>::iterator it = av.begin(); while (it != av.end()) { ll2 val = *it; set<ll2>::iterator it2 = bv.end(); it2--; while (it2 != bv.end()) { ll2 val2 = *it2; if (val.y < val2.y) {//dol , w gore out = max(out, val.x + val2.x); // cout << "Vals: " << val.x << " + " << val2.x << "\n"; break; } if (it2 == bv.begin()) break; it2--; } // if (it2 == bv.begin()) // break; it++; } return out; } ll mim() { int MID = min((N + 1) / 2, N); set<ll2> sets[3]; makeSet(0, MID, sets[0], false); makeSet(1, MID, sets[1], false); makeSet(MID, N, sets[2], true); return max(iterateMax(sets[0], sets[2]), iterateMax(sets[1], sets[2])); } ll obtainMax(int start) { set<ll2> current[2]; current[start % 2].insert(mp((ll)0, (ll)0)); for(int i = start; i < N; i++) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, true); } // cout << current[N % 2].size() << "\n"; return maxFromSet(current[N % 2]); } void init() { cin >> N >> M; tab.resize(N); for(int i = 0; i < N; i++) cin >> tab[i]; } void solve() { mlog = 0; for (int i = 62; i >= 0; i--) if (((1 << i) & M) > 0) { mlog = i; break; } if (N <= 151) cout << max(obtainMax(0), obtainMax(1)) << "\n"; else cout << mim() << "\n"; } int main(int argc, const char * argv[]) { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); // cout << changeValue(10, 2, false, true) << "\n"; // assert(changeValue(10, 2, false, true) == 12); init(); solve(); return 0; } /* 100 1000000000000000 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ODP: 2203 */
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | #include <algorithm> #include <iostream> #include <vector> #include <bitset> #include <climits> #include <set> #include <cassert> using namespace std; #define ll long long #define ull unsigned ll #define int2 pair<int, int> #define ll2 pair<ll, ll> #define ull2 pair<ull, ull> #define mp make_pair #define x first #define y second #define forEach(a) for(int i = 0; i < a.size(); i++) const int size = 1e5 + 9; vector<ll> tab; int N; ll M; int mlog; ll changeValue(ll v, int pos, bool one, bool increasing) { // if (pos == 0) { // if (increasing) // return v | 1; // return v - (v % 2); // } v >>= pos; if (increasing) v |= (ll)1; else v -= (v % 2); v <<= pos; if (one) v -= 1; if (increasing) v |= (ll)1 << pos; return v; } //__builtin_popcountll void nextSet(set<ll2> &values, set<ll2> &out, ll a, int ind, bool increasing) {// x = sum, y = val at b[i] // set<ll2> out; // cout << "Values size: " << values.size() << " " << a << "\n"; bool positive = a > 0; set<ll2>::iterator it = values.begin(); while (it != values.end()) { ll2 p = *it; ll val = p.y; for (int i = mlog; i >= 0; i--) { ll flag = (ll)1 << i; if (flag > M) continue; // cout << i << "Nex value: " << flag << "\n"; ll newVal; if ((val & flag) > 0 && !increasing) newVal = changeValue(val, i, positive, increasing); else if ((val & flag) == 0 && increasing) newVal = changeValue(val, i, positive, increasing); else continue; if (newVal <= M) { // bool addIt = newVal <= M; ll bits = __builtin_popcountll(newVal); //cout << "Bits " << bits << "\n"; ll newSum = p.x + a * bits; //cout << i << " New sum: " << newSum << "\n"; // if (!addIt && increasing) // break; set<ll2>::iterator it2 = out.upper_bound(mp(newSum, LLONG_MIN)); if (it2 != out.end()) { ll val2 = (*it2).y; //cout << "Val2: " << val2 << "\n"; if (newVal >= val2 && increasing) { // if (positive != increasing) // continue; break;//addIt = false; //cout << "Breaking: " << newSum << ", " << newVal << " a: " << a << " " << val2 << " " << p.x << "\n"; // continue; } else if (newVal <= val2 && !increasing) { // if (positive != increasing) // continue; break; } } // if (addIt) { //cout << "Adding: " << newSum << ", " << newVal << " a: " << a << "\n"; out.insert(mp(newSum, newVal)); // } // else //cout << "Not Adding: " << newSum << ", " << newVal << "\n"; } } if (ind == N - 1) { ll newVal = M; ll bits = __builtin_popcountll(newVal); ll newSum = p.x + a * bits; bool ok = true; set<ll2>::iterator it2 = out.upper_bound(mp(newSum, LLONG_MIN)); if (it2 != out.end()) { ll val2 = (*it2).y; if (newVal >= val2 && increasing) ok = false; else if (newVal <= val2 && !increasing) ok = false; } if (ok) out.insert(mp(newSum, newVal)); } it++; } } ll maxFromSet(set<ll2> &values) { set<ll2>::iterator it = values.begin(); ll out = LLONG_MIN; while (it != values.end()) { out = max(out, (*it).x); // cout << "Out: " << out << "\n"; it++; } return out; } void makeSet(int start, int end, set<ll2> &out, bool rev) { set<ll2> current[2]; if (!rev) current[start % 2].insert(mp((ll)0, (ll)0)); else current[(end - 1) % 2].insert(mp((ll)0, (ll)M + 1)); if (rev) { for(int i = end - 1; i >= start; i--) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, !rev); } swap(current[(start + 1) % 2], out); } else { for(int i = start; i < end; i++) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, !rev); } swap(current[end % 2], out); } } ll iterateMax(set<ll2> &av, set<ll2> &bv) { ll out = LLONG_MIN; if (bv.size() == 0) return out; set<ll2>::iterator it = av.begin(); while (it != av.end()) { ll2 val = *it; set<ll2>::iterator it2 = bv.end(); it2--; while (it2 != bv.end()) { ll2 val2 = *it2; if (val.y < val2.y) {//dol , w gore out = max(out, val.x + val2.x); // cout << "Vals: " << val.x << " + " << val2.x << "\n"; break; } if (it2 == bv.begin()) break; it2--; } // if (it2 == bv.begin()) // break; it++; } return out; } ll mim() { int MID = min((N + 1) / 2, N); set<ll2> sets[3]; makeSet(0, MID, sets[0], false); makeSet(1, MID, sets[1], false); makeSet(MID, N, sets[2], true); return max(iterateMax(sets[0], sets[2]), iterateMax(sets[1], sets[2])); } ll obtainMax(int start) { set<ll2> current[2]; current[start % 2].insert(mp((ll)0, (ll)0)); for(int i = start; i < N; i++) { // cout << "N: " << i << "\n"; current[(i + 1) % 2].clear(); if (tab[i] == 0) { swap(current[0], current[1]); } else nextSet(current[i % 2], current[(i + 1) % 2], tab[i], i, true); } // cout << current[N % 2].size() << "\n"; return maxFromSet(current[N % 2]); } void init() { cin >> N >> M; tab.resize(N); for(int i = 0; i < N; i++) cin >> tab[i]; } void solve() { mlog = 0; for (int i = 62; i >= 0; i--) if (((1 << i) & M) > 0) { mlog = i; break; } if (N <= 151) cout << max(obtainMax(0), obtainMax(1)) << "\n"; else cout << mim() << "\n"; } int main(int argc, const char * argv[]) { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); // cout << changeValue(10, 2, false, true) << "\n"; // assert(changeValue(10, 2, false, true) == 12); init(); solve(); return 0; } /* 100 1000000000000000 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ODP: 2203 */ |