#include<bits/stdc++.h> using namespace std; const int N = 205; int n; long long m, a[N]; inline long long get(long long val, int bits){ if(val == -1){ return (1LL << bits) - 1; } long long res = 9e18, cur = 0; for(int i = 59; i >= 0; i--){ if((val >> i) & 1){ bits -= 1; if(bits < 0){ break; } cur ^= (1LL << i); } else if(bits > 0 && i >= bits - 1){ res = min(res, (cur ^ (1LL << i) ^ ((1LL << (bits - 1)) - 1))); } } return res; } long long rec(int pos = 1, long long prv = -1, bool firstt = true){ if(pos == n + 1){ return 0; } long long ans = -9e18; if(a[pos] <= 0){ for(int i = 0; i < 60; i++){ long long x = get(prv, i); if(prv < x && x <= m){ ///cout << "KEK JEDEN: " << pos << " " << i << " " << prv << " " << x << " " << rec(pos + 1, x) << " " << a[pos] * i << "\n"; ans = max(ans, rec(pos + 1, x, false) + a[pos] * i); } } } else{ for(int i = 59; i >= 0; i--){ long long x = get(prv, i); if(prv < x && x <= m){ ///cout << "KEK DWA: " << pos << " " << i << " " << prv << " " << x << " " << rec(pos + 1, x) << " " << a[pos] * i << "\n"; ans = max(ans, rec(pos + 1, x, false) + a[pos] * i); } } } return ans; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> a[i]; } cout << rec(1); }
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 | #include<bits/stdc++.h> using namespace std; const int N = 205; int n; long long m, a[N]; inline long long get(long long val, int bits){ if(val == -1){ return (1LL << bits) - 1; } long long res = 9e18, cur = 0; for(int i = 59; i >= 0; i--){ if((val >> i) & 1){ bits -= 1; if(bits < 0){ break; } cur ^= (1LL << i); } else if(bits > 0 && i >= bits - 1){ res = min(res, (cur ^ (1LL << i) ^ ((1LL << (bits - 1)) - 1))); } } return res; } long long rec(int pos = 1, long long prv = -1, bool firstt = true){ if(pos == n + 1){ return 0; } long long ans = -9e18; if(a[pos] <= 0){ for(int i = 0; i < 60; i++){ long long x = get(prv, i); if(prv < x && x <= m){ ///cout << "KEK JEDEN: " << pos << " " << i << " " << prv << " " << x << " " << rec(pos + 1, x) << " " << a[pos] * i << "\n"; ans = max(ans, rec(pos + 1, x, false) + a[pos] * i); } } } else{ for(int i = 59; i >= 0; i--){ long long x = get(prv, i); if(prv < x && x <= m){ ///cout << "KEK DWA: " << pos << " " << i << " " << prv << " " << x << " " << rec(pos + 1, x) << " " << a[pos] * i << "\n"; ans = max(ans, rec(pos + 1, x, false) + a[pos] * i); } } } return ans; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> a[i]; } cout << rec(1); } |