#include <bits/stdc++.h> using namespace std; const int N = 201; const int M = 1e4; int n; long long m; long long a[N]; long long dp[N][M]; long long next_with_bits(long long num,int bits){ if(num < 0){ if((1<<bits)-1 <= m) return (1<<bits)-1; else return -1; } int numbits = __builtin_popcount(num); vector<bool>binary; long long num2 = num; while(num2 > 0){ binary.push_back(num2%2); num2 /= 2; } binary.push_back(0); if(numbits < bits){ size_t j=0; while(numbits < bits){ if(j >= binary.size()){ binary.push_back(1); numbits++; } else if(binary[j] == 0){ binary[j] = 1; numbits++; } j++; } }else{ int s = 0; int i = binary.size(); while(s < bits){ i--; s += binary[i]; } for(int j=i-1;j>=0;j--){ numbits -= binary[j]; binary[j] = 0; } while(binary[i]){ binary[i] = 0; numbits--; i++; } binary[i] = 1; numbits++; size_t j=0; while(numbits < bits){ if(j >= binary.size()){ binary.push_back(1); numbits++; } else if(binary[j] == 0){ binary[j] = 1; numbits++; } j++; } } long long p2=1; long long ret=0; for(size_t i=0;i<binary.size();i++){ ret += binary[i]*p2; p2 *= 2; } if(ret <= num || ret > m) return -1; return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) for(int j=0;j<=m;j++) dp[i][j] = LLONG_MIN/2; dp[1][0] = 0; for(int i=1;i<=n;i++){ for(int j=0;j<=m;j++){ for(int k=0;k<j;k++){ dp[i][j] = max(dp[i][j],dp[i-1][k]+(__builtin_popcount(j)*a[i])); } } } /* for(int i=1;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<dp[i][j]<<" "; } cout<<endl; } */ long long res = LLONG_MIN; for(int i=0;i<=m;i++) res = max(res,dp[n][i]); cout<<res<<endl; 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 | #include <bits/stdc++.h> using namespace std; const int N = 201; const int M = 1e4; int n; long long m; long long a[N]; long long dp[N][M]; long long next_with_bits(long long num,int bits){ if(num < 0){ if((1<<bits)-1 <= m) return (1<<bits)-1; else return -1; } int numbits = __builtin_popcount(num); vector<bool>binary; long long num2 = num; while(num2 > 0){ binary.push_back(num2%2); num2 /= 2; } binary.push_back(0); if(numbits < bits){ size_t j=0; while(numbits < bits){ if(j >= binary.size()){ binary.push_back(1); numbits++; } else if(binary[j] == 0){ binary[j] = 1; numbits++; } j++; } }else{ int s = 0; int i = binary.size(); while(s < bits){ i--; s += binary[i]; } for(int j=i-1;j>=0;j--){ numbits -= binary[j]; binary[j] = 0; } while(binary[i]){ binary[i] = 0; numbits--; i++; } binary[i] = 1; numbits++; size_t j=0; while(numbits < bits){ if(j >= binary.size()){ binary.push_back(1); numbits++; } else if(binary[j] == 0){ binary[j] = 1; numbits++; } j++; } } long long p2=1; long long ret=0; for(size_t i=0;i<binary.size();i++){ ret += binary[i]*p2; p2 *= 2; } if(ret <= num || ret > m) return -1; return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) for(int j=0;j<=m;j++) dp[i][j] = LLONG_MIN/2; dp[1][0] = 0; for(int i=1;i<=n;i++){ for(int j=0;j<=m;j++){ for(int k=0;k<j;k++){ dp[i][j] = max(dp[i][j],dp[i-1][k]+(__builtin_popcount(j)*a[i])); } } } /* for(int i=1;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<dp[i][j]<<" "; } cout<<endl; } */ long long res = LLONG_MIN; for(int i=0;i<=m;i++) res = max(res,dp[n][i]); cout<<res<<endl; return 0; } |