Somewhat long but detailed coding solution ( C++ )


#1

int minval( vector& strength){

int minsum = strength[0];

for( int i=1; i<strength.size(); i++ ){
    
    if( strength[i] < minsum )
        minsum = strength[i];
}

return minsum;

}

vector Solution::solve(int A, vector &B) {

vector<int> svac;
vector<int> ans;
int val = minval( B );

int kick = A/val;

for( int i=0; i<B.size(); i++ ){
    
    int ivar = val*(kick-1) + B[i];
    
    if( ivar <= A )
        svac.push_back(i);
        
    if( B[i] == val )
        break;
}

int i=0;
int c=0;
int sumvar = 0;
int sum=0;

while( c<kick && i<svac.size() ){
    
    sum = sum + val*( kick  - c - 1 ) + B[ svac[i] ] ;

    if( sum <= A ){
        
        ans.push_back( svac[i] );
        sumvar += B[ svac[i] ];
        ++c;
    }
    else
        ++i;
        
    sum = sumvar;
}

if( i == svac.size() && ans.size() < kick ){
    
    for( int temp=0; temp<ans.size()-kick; temp++ ){
        
        ans.push_back( svac[i-1] );
    }
}

return ans;

}