#include <bits/stdc++.h> using namespace std; #define FOR(i,x,y) for(int i=(int)(x);i<(int)(y);++i) #define FORE(i,x,y) for(int i=(int)(x);i<=(int)(y);++i) #define FORD(i,x,y) for(int i=(int)(x);i>=(int)(y);--i) #define PB push_back #define MP make_pair #define ST first #define ND second typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> p2ll; const int MAXN=2e2+6; ll sum=0; ll n,m; ll a[MAXN]; map<ll,ll> mp[2]; ll bt; ll prv1(ll v,ll d){ /*ll res=(1LL<<v); --res; ll pos=62; --v; while(pos>v&&v>=0){ ll tr=((res|(1LL<<pos))&(~(1LL<<v))); if(tr<=d){ res=tr; --v; } --pos; } return res;*/ ll res=d; if(bt<v){ ll i=0; while(bt<v){ if(!(d&1)){ res|=(1LL<<i); --v; } ++i; d/=2; } while((d&1))d/=2,++i; if(d==0)return res; while(!(d&1)){ ++i; d/=2; } res|=(1LL<<(i-1)); res&=(~(1LL<<(i))); }else if(bt>v){ ll i=0; while(bt>v){ if(d&1){ res&=(~(1LL<<i)); ++v; } ++i; d/=2; } } return res; } ll lg2(ll v){ ll res=-1; while(v>0)v/=2,++res; return res; } ll Bts(ll v){ ll res=0; while(v>0){ if(v&1)++res; v/=2; } return res; } int main() { ios_base::sync_with_stdio(false);cin.tie(0); cin>>n>>m; FOR(i,0,n)cin>>a[i]; int sile; if(n>70)sile=35; if(n>80)sile=30; if(n>90)sile=25; if(n>100)sile=20; if(n>120)sile=15; if(n>150)sile=10; if(n>170)sile=8; int t=0,p=1; mp[p].insert(MP(0,m+1)); FORD(i,n-1,0){ mp[t].clear(); map<ll,ll>::iterator it,it2; it=mp[p].end(); --it; int cos=0; for(;true;--it){ ++cos; ll c=it->ST; ll d=it->ND; ll x=lg2(d); c+=a[i]*x; bt=Bts(d-1); int ile=sile; if(a[i]<=0)ile=100; for(;x>=0;--x){ --ile; ll pos=prv1(x,d-1); if(pos>d)continue; it2=mp[t].lower_bound(c); if(it2==mp[t].end()){ mp[t].insert(it2,MP(c,pos)); --it2; }else if(it2->ND<pos){ if(it2->ST==c){ it2->ND=pos; }else{ mp[t].insert(it2,MP(c,pos)); --it2; } } if(it2!=mp[t].begin()){ --it2; while(true){ if(it2->ND<=pos){ it2=mp[t].erase(it2); }else{ break; } if(it2==mp[t].begin())break; --it2; } } c-=a[i]; if(ile<0||cos>10000)break; } if(it==mp[p].begin())break; } swap(t,p); } map<ll,ll>::iterator it=mp[p].end(); --it; cout<<it->ST<<"\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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <bits/stdc++.h> using namespace std; #define FOR(i,x,y) for(int i=(int)(x);i<(int)(y);++i) #define FORE(i,x,y) for(int i=(int)(x);i<=(int)(y);++i) #define FORD(i,x,y) for(int i=(int)(x);i>=(int)(y);--i) #define PB push_back #define MP make_pair #define ST first #define ND second typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> p2ll; const int MAXN=2e2+6; ll sum=0; ll n,m; ll a[MAXN]; map<ll,ll> mp[2]; ll bt; ll prv1(ll v,ll d){ /*ll res=(1LL<<v); --res; ll pos=62; --v; while(pos>v&&v>=0){ ll tr=((res|(1LL<<pos))&(~(1LL<<v))); if(tr<=d){ res=tr; --v; } --pos; } return res;*/ ll res=d; if(bt<v){ ll i=0; while(bt<v){ if(!(d&1)){ res|=(1LL<<i); --v; } ++i; d/=2; } while((d&1))d/=2,++i; if(d==0)return res; while(!(d&1)){ ++i; d/=2; } res|=(1LL<<(i-1)); res&=(~(1LL<<(i))); }else if(bt>v){ ll i=0; while(bt>v){ if(d&1){ res&=(~(1LL<<i)); ++v; } ++i; d/=2; } } return res; } ll lg2(ll v){ ll res=-1; while(v>0)v/=2,++res; return res; } ll Bts(ll v){ ll res=0; while(v>0){ if(v&1)++res; v/=2; } return res; } int main() { ios_base::sync_with_stdio(false);cin.tie(0); cin>>n>>m; FOR(i,0,n)cin>>a[i]; int sile; if(n>70)sile=35; if(n>80)sile=30; if(n>90)sile=25; if(n>100)sile=20; if(n>120)sile=15; if(n>150)sile=10; if(n>170)sile=8; int t=0,p=1; mp[p].insert(MP(0,m+1)); FORD(i,n-1,0){ mp[t].clear(); map<ll,ll>::iterator it,it2; it=mp[p].end(); --it; int cos=0; for(;true;--it){ ++cos; ll c=it->ST; ll d=it->ND; ll x=lg2(d); c+=a[i]*x; bt=Bts(d-1); int ile=sile; if(a[i]<=0)ile=100; for(;x>=0;--x){ --ile; ll pos=prv1(x,d-1); if(pos>d)continue; it2=mp[t].lower_bound(c); if(it2==mp[t].end()){ mp[t].insert(it2,MP(c,pos)); --it2; }else if(it2->ND<pos){ if(it2->ST==c){ it2->ND=pos; }else{ mp[t].insert(it2,MP(c,pos)); --it2; } } if(it2!=mp[t].begin()){ --it2; while(true){ if(it2->ND<=pos){ it2=mp[t].erase(it2); }else{ break; } if(it2==mp[t].begin())break; --it2; } } c-=a[i]; if(ile<0||cos>10000)break; } if(it==mp[p].begin())break; } swap(t,p); } map<ll,ll>::iterator it=mp[p].end(); --it; cout<<it->ST<<"\n"; return 0; } |