In a stock market, there is a product with its infinite stocks. The stock prices are given for i th days, where arr[i] denotes the price of the stock on the i th day. There is a rule that a customer can buy at most i stock on the i th day. If the customer has an amount of k dollars initially, find out the maximum number of stocks they can buy?For example, for 3 days the price of a stock is given as 7,10,4 . You can buy  stock worth $7 on day 1,  2 stocks worth $10 each on day 2 and 3 stocks worth  each $ 4 on day 3. If k=$100 , you can buy all the stocks (total 6 ) for $39.Constraints:(i) 1<=n<=100000(ii)1<=arr[i]<=100(iii)1<=k<=100Below is my code:long buyMaximumProducts(long n, long long k, vector <long> a) {    long numofstocks;    numofstocks=0;    for (long i=0;i<n;i++)    {        if (k>=a[i])  //still can buy at least one        {               if (k%a[i]>=i+1)            {numofstocks= numofstocks+i+1;                 }            else            {numofstocks=numofstocks+k%a[i];}        }        k=k-numofstocks*a[i];    }    return numofstocks;}int main() {    long n;    cin >> n;    vector<long> arr(n);    for(long arr_i = 0; arr_i < n; arr_i++){       cin >> arr[arr_i];    }    long long k;    cin >> k;    long result = buyMaximumProducts(n, k, arr);    cout << result << endl;    return 0;}
Want to know where the logic goes wrong
c++