#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef unsigned long long int uLL;
typedef unsigned int uint;
typedef long double ld;
const int N = 207;
const LL INF = 2000000000000000000;
unordered_map <uLL, LL> dp[N];
int n, nr;
uLL m;
LL tab[N];
uLL bigger(uLL l, int ile){
uLL p = 0;
for(int i = 0; i < ile; i++){
p |= (uLL(1)<<i);
}
int s = 0, t = ile;
while(p < l){
p <<= 1;
s++;
t++;
}
bool czy = false;
while(t >= s){
while(p >= l){
if(s == 0){
czy = true;
break;
}
p |= (uLL(1)<<(s-1));
p ^= (uLL(1)<<(t-1));
s--;
t--;
}
if(czy){
break;
}
p ^= (uLL(1)<< s);
p |= (uLL(1)<< t);
s++;
}
return p;
}
LL rek(int pos, uLL mask){
if(pos == n+1){
return 0;
}
if(dp[pos].find(mask) != dp[pos].end()){
return dp[pos][mask];
}
LL maks = -INF;
if(mask == 0){
maks = max(maks, rek(pos+1, 1));
}
for(int i = 1; i <= nr; i++){
uLL zz = bigger(mask, i);
if(zz > m){
continue;
}
maks = max(maks, tab[pos]*i+rek(pos+1, zz+1));
}
dp[pos][mask] = maks;
return maks;
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> tab[i];
}
while((uLL(1)<<nr) < m) nr++;
cout << rek(1, 0) << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; typedef unsigned long long int uLL; typedef unsigned int uint; typedef long double ld; const int N = 207; const LL INF = 2000000000000000000; unordered_map <uLL, LL> dp[N]; int n, nr; uLL m; LL tab[N]; uLL bigger(uLL l, int ile){ uLL p = 0; for(int i = 0; i < ile; i++){ p |= (uLL(1)<<i); } int s = 0, t = ile; while(p < l){ p <<= 1; s++; t++; } bool czy = false; while(t >= s){ while(p >= l){ if(s == 0){ czy = true; break; } p |= (uLL(1)<<(s-1)); p ^= (uLL(1)<<(t-1)); s--; t--; } if(czy){ break; } p ^= (uLL(1)<< s); p |= (uLL(1)<< t); s++; } return p; } LL rek(int pos, uLL mask){ if(pos == n+1){ return 0; } if(dp[pos].find(mask) != dp[pos].end()){ return dp[pos][mask]; } LL maks = -INF; if(mask == 0){ maks = max(maks, rek(pos+1, 1)); } for(int i = 1; i <= nr; i++){ uLL zz = bigger(mask, i); if(zz > m){ continue; } maks = max(maks, tab[pos]*i+rek(pos+1, zz+1)); } dp[pos][mask] = maks; return maks; } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> tab[i]; } while((uLL(1)<<nr) < m) nr++; cout << rek(1, 0) << '\n'; return 0; } |
English