#include <iostream>
#include <cstdint>
#include <vector>
#include <utility>
#include <algorithm>
#include <bitset>
#include <limits>
using namespace std;
int bitcount(int64_t n){
return std::bitset<64>(n).count();
}
/*int bitcount(
* int64_t n){
int acc=0;
while (n>0){
acc += (n&1);
n/=2;
}
return acc;
}*/
void go(){
int n;
int64_t m;
cin>>n>>m;
if (m==0){
cout<<0<<endl;
return;
}
vector<int64_t> a(n,0);
for (auto& x : a)
cin>>x;
vector<pair< int64_t,int64_t> > J, J2;
int sekunda =0;
if (a[0]>=0)
J.push_back( make_pair(-1,0));// pioseka konczaca sie z bitem b i na łaczną warttość v
else{
sekunda=1;
J.push_back( make_pair(0,0));
}
for (; sekunda < n; sekunda++){
for (const auto & eJ: J ){
int64_t b = eJ.first+1;
int64_t val = eJ.second;
int64_t inc = 1;
if (a[sekunda]>=0){
int bitc = bitcount(b);
while (b<=m){
J2.push_back( make_pair(b, val + a[sekunda]*bitc ) );
while ((b & inc) >0) inc*=2;
b+=inc;
bitc++;
}
}else{
while (b<=m){
int bitc = bitcount(b);
J2.push_back( make_pair(b, val + a[sekunda]*bitc ) );
do{
while ((b & inc) == 0) inc*=2;
b+=inc;
}while ( (b & (2*inc)) == 1 );
bitc++;
}
}
}
sort(begin(J2),end(J2),
[](const pair< int64_t,int64_t>& a, const pair< int64_t,int64_t>& b ){
//return a.first<b.first || (!(a.first<b.first) && a.second>b.second);
return a.first<b.first || ((a.first==b.first) && a.second>b.second);
}
);
J.clear();
int64_t max_val=numeric_limits<int64_t>::min();
for (auto eJ : J2){
if (eJ.second > max_val){
max_val = eJ.second;
J.push_back(eJ);
}
}
// cout<<sekunda<<" "<<J.size()<<" "<<J2.size()<<endl;
J2.clear();
}
cout<<J.back().second<<endl;
}
int main(){
std::ios::sync_with_stdio(false);
go();
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 | #include <iostream> #include <cstdint> #include <vector> #include <utility> #include <algorithm> #include <bitset> #include <limits> using namespace std; int bitcount(int64_t n){ return std::bitset<64>(n).count(); } /*int bitcount( * int64_t n){ int acc=0; while (n>0){ acc += (n&1); n/=2; } return acc; }*/ void go(){ int n; int64_t m; cin>>n>>m; if (m==0){ cout<<0<<endl; return; } vector<int64_t> a(n,0); for (auto& x : a) cin>>x; vector<pair< int64_t,int64_t> > J, J2; int sekunda =0; if (a[0]>=0) J.push_back( make_pair(-1,0));// pioseka konczaca sie z bitem b i na łaczną warttość v else{ sekunda=1; J.push_back( make_pair(0,0)); } for (; sekunda < n; sekunda++){ for (const auto & eJ: J ){ int64_t b = eJ.first+1; int64_t val = eJ.second; int64_t inc = 1; if (a[sekunda]>=0){ int bitc = bitcount(b); while (b<=m){ J2.push_back( make_pair(b, val + a[sekunda]*bitc ) ); while ((b & inc) >0) inc*=2; b+=inc; bitc++; } }else{ while (b<=m){ int bitc = bitcount(b); J2.push_back( make_pair(b, val + a[sekunda]*bitc ) ); do{ while ((b & inc) == 0) inc*=2; b+=inc; }while ( (b & (2*inc)) == 1 ); bitc++; } } } sort(begin(J2),end(J2), [](const pair< int64_t,int64_t>& a, const pair< int64_t,int64_t>& b ){ //return a.first<b.first || (!(a.first<b.first) && a.second>b.second); return a.first<b.first || ((a.first==b.first) && a.second>b.second); } ); J.clear(); int64_t max_val=numeric_limits<int64_t>::min(); for (auto eJ : J2){ if (eJ.second > max_val){ max_val = eJ.second; J.push_back(eJ); } } // cout<<sekunda<<" "<<J.size()<<" "<<J2.size()<<endl; J2.clear(); } cout<<J.back().second<<endl; } int main(){ std::ios::sync_with_stdio(false); go(); return 0; } |
English