function [Total_MECCost_nearby,beta_MEC_nearby]= MEC_Algo_nearby(B_n,D_n, V_n_size, t_max, V2V_stay, fV_i,R_V2V_final)
P_nearby= 2.5 % The price for local computation is 2.5 Units/s
C_nearby= 1 % The cost of transmission for source to nearby vehicle
Time_initial=0;
beta_MEC=0;
beta_MEC_nearby=0;
t_n_up=0;
t_exe=0;
Time_nearby=0;
Cost_nearby=0;
Cost_initial=0;
Total_RRV=0;            % That will count the total no. of resource rich vehicles
[row_V_n,col_nearby]=size(V2V_stay)
Total_Cost_nearby=zeros(1, V_n_size);    % A matrix for Total system cost

    for i=1:row_V_n
        for j=1:col_nearby
            if(V2V_stay(i,j)~=-1 && t_max(i)>=V2V_stay(i,j))
                %fV_i(i,j)= randi([10 10^2],1,1) % Nearby available computational capacity
                t_n_up(i,j)=D_n(i)/ R_V2V_final(i,j)    %Transmission time from source to all nearby vehicles
                t_exe(i,j)=B_n(i)/fV_i(i,j)             %execution time 
                beta_MEC(i,j)= V2V_stay(i,j)/(t_n_up(i,j)+t_exe(i,j))           % This is the value of beta according to stay time
                Time_initial(i,j)= beta_MEC(i,j)*t_n_up(i,j) + beta_MEC(i,j)* t_exe(i,j)
                Cost_initial(i,j)= (C_nearby*beta_MEC(i,j)*t_n_up(i,j))+ (P_nearby*((beta_MEC(i,j)* t_exe(i,j))))
                %Total_RRV(i,j)=Total_RRV+1;
            else
                Time_initial(i,j)=-950000       % Just to fill the remaining values of matrix
                Cost_initial(i,j)=-950000       % Just to fill the remaining values of matrix
                beta_MEC(i,j)=0      % Just to fill the remaining values of matrix
                fV_i(i,j)=-1                   % Those vehicles which are in range but do not have sufficient resources 
            end
            [minValues, Indices] = max(Time_initial,[],2)    % Selection of vehicle for each source vehicle according to minimum time
            if (minValues(i)~=950000)
                Time_nearby(i)=minValues(i);                 % Minimum Time in a row i.e. minimum value for V_n from all candidate vehicles%                 fV_i(i,temp)=fV_i(i,temp)-B_n(i)       % The selected vehicle will process the task, so the computation power will be used  

            end
            [minCost, Index] = max(Cost_initial,[],2)
            if (minCost(i) ~=-950000)
                Cost_nearby(i)= minCost(i)                       % Minimum cost in a row i.e. minimum value for V_n from all candidate vehicles
                
            end                                    
            [maxbeta, Index] = max(beta_MEC,[],2)
%             if (maxbeta(i)~=-950000)
                beta_MEC_nearby(i)= maxbeta(i)
%             end
               
        end
            temp=Indices(i)
            fV_i(i,temp)=fV_i(i,temp)-B_n(i)

    end
    Total_MECCost_nearby= Cost_nearby          %For taking the Total system cost 
    for i=2:length(Cost_nearby);
        Total_MECCost_nearby(i) = Total_MECCost_nearby(i-1) + Cost_nearby(i);
    end
    
end