#include<cstdio> #include<algorithm> #include<vector> #include<map> #define S 1000007 using namespace std; typedef long long ll; vector < ll > V; vector < pair < ll, int > > V2; vector < ll > V3[5]; map < ll, bool > mapa; ll dp[S]; ll dp2[S]; ll t[1000]; int b; ll m; ll pot[100]; int main(void){ int n; scanf("%d %lld",&n,&m); ll x; pot[0] = 1; int a = 1; while(pot[a-1] <= 1e18){ pot[a] = pot[a-1]*2; a++; } pot[a] = pot[a-1] * 2; if(m <= 1e5){ for(int i = 0; i <= m;i++){ V.push_back(__builtin_popcount(i)); } }else{ ll m2 = m; while(m2 > 0){ m2 /=2; b++; } V3[0].push_back(0); for(int i = 0 ; i < 4;i++){ for(int j = 0 ; j < V3[i].size();j++){ for(ll t = 1; t <= m;t*=2){ x = V3[i][j]; if(!(x & t)){ if(x + t <= m && !mapa[x+t]){ V3[i+1].push_back(x + t); mapa[x+t] = 1; } } } } } for(int i = 0 ; i < 5;i++){ for(int j = 0 ; j < V3[i].size();j++){ V2.push_back({V3[i][j],i}); if(pot[b]-1 - V3[i][j] <= m){ V2.push_back({pot[b]-1-V3[i][j],b-i}); } } } sort(V2.begin(),V2.end()); for(int i = 0; i < V2.size();i++){ V.push_back(V2[i].second); } } for(int i = 1; i <= n;i++){ scanf("%lld",&x); dp[i] = x * V[i-1] + dp[i-1]; for(int j = i+1; j <= V.size();j++){ dp[j] = max(dp[j-1], dp2[j-1] + V[j-1] * x); } for(int j = 1; j <= V.size()+6;j++){ dp2[j] = dp[j]; } } printf("%lld",dp[V.size()]); 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 | #include<cstdio> #include<algorithm> #include<vector> #include<map> #define S 1000007 using namespace std; typedef long long ll; vector < ll > V; vector < pair < ll, int > > V2; vector < ll > V3[5]; map < ll, bool > mapa; ll dp[S]; ll dp2[S]; ll t[1000]; int b; ll m; ll pot[100]; int main(void){ int n; scanf("%d %lld",&n,&m); ll x; pot[0] = 1; int a = 1; while(pot[a-1] <= 1e18){ pot[a] = pot[a-1]*2; a++; } pot[a] = pot[a-1] * 2; if(m <= 1e5){ for(int i = 0; i <= m;i++){ V.push_back(__builtin_popcount(i)); } }else{ ll m2 = m; while(m2 > 0){ m2 /=2; b++; } V3[0].push_back(0); for(int i = 0 ; i < 4;i++){ for(int j = 0 ; j < V3[i].size();j++){ for(ll t = 1; t <= m;t*=2){ x = V3[i][j]; if(!(x & t)){ if(x + t <= m && !mapa[x+t]){ V3[i+1].push_back(x + t); mapa[x+t] = 1; } } } } } for(int i = 0 ; i < 5;i++){ for(int j = 0 ; j < V3[i].size();j++){ V2.push_back({V3[i][j],i}); if(pot[b]-1 - V3[i][j] <= m){ V2.push_back({pot[b]-1-V3[i][j],b-i}); } } } sort(V2.begin(),V2.end()); for(int i = 0; i < V2.size();i++){ V.push_back(V2[i].second); } } for(int i = 1; i <= n;i++){ scanf("%lld",&x); dp[i] = x * V[i-1] + dp[i-1]; for(int j = i+1; j <= V.size();j++){ dp[j] = max(dp[j-1], dp2[j-1] + V[j-1] * x); } for(int j = 1; j <= V.size()+6;j++){ dp2[j] = dp[j]; } } printf("%lld",dp[V.size()]); return 0; } |