#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class CartInSupermarket { public:
int calcmin(vector <int> a, int b)
{
int L=0, R=*max_element(a.begin(), a.end()); // can do in (L,R] minutes
while(R-L>1) {
int C = (L+R)/2;
(can(a,b,C) ? R : L) = C;
}
return R;
}
bool can(vector<int> a, int b, int C) {
int sp = 0;
for(auto ai: a) {
int spx;
if(!need_split(ai, C, &spx))
return false;
sp += spx;
}
return sp <= b;
}
// Can we process A in C minutes? If so, return minimal necessary split to *spx.
bool need_split(int A, int C, int* spx) {
LL preM = 0;
for(int b=max(0,A/C-1) ;; ++b) {
LL M = cover(C, b);
if(M <= preM)
return false;
preM = M;
if(A<=M) {
*spx = b;
return true;
}
}
}
// C minutes, b splits. How many we can cover.
LL cover(int C, int b) {
vector<pair<int,int>> stock(1, make_pair(C,1));
while(b) {
if(b>=stock[0].second) {
b -= stock[0].second;
stock[0] = make_pair(stock[0].first-1, stock[0].second*2);
} else {
stock[0].second -= b;
stock.emplace_back(stock[0].first-1, b*2);
break;
}
}
LL ans = 0;
for(auto cn: stock) {
if(cn.first<=0)
return -1;
ans += LL(cn.first)*cn.second;
}
return ans;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, CartInSupermarket().calcmin(a, b));}
int main(){
CASE(0)
int a_[] = {8};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 3;
int _ = 4;
END
CASE(1)
int a_[] = {6,6,5};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 3;
int _ = 4;
END
CASE(2)
int a_[] = {12,5,6,2,6,8};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 4;
int _ = 6;
END
CASE(3)
int a_[] = {15,20,11,13,18,24,25,21,22,10,15,14,19};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 0;
int _ = 25;
END
CASE(4)
int a_[] = {671122748,846444748,84701624,608579554,672060899,967957440,31438849,734849843,376589643,904285209
,80693344,211737743,85405464,444633541,293184188,935462519,146753109,972886045,496631016,601669536
,257574086,958464570,6294570,546189534,668105964,601197313,784337929,921840143,70408284,722040626
,253400894,56411549,811940644,152086353,122638884,776352066,118932182,177589709,538122558,127914469
,523761221,290027568,734517444,819458123,699130727,784698122,810265337,283326309,593596316,368671876};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 6478;
int _ = 3805054;
END
CASE(5)
int a_[] = {977827843, 601538780, 476956916, 821358251, 646377015, 479421294, 631863623, 446866211, 147800371, 777376159, 997915782, 317155640, 611683333, 461303085, 473215018, 363166652, 39746841, 109934430, 391345044, 41220128, 379970887, 445787345, 801764390, 886145854, 494435459, 974163022, 126739284, 708338618, 775199509, 378383243, 928615727, 606884963};
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = 278304610;
int _ = -1;
END
/*
CASE(6)
int a_[] = ;
vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_));
int b = ;
int _ = ;
END
*/
}
// END CUT HERE