Passed half of the test cases, need debug help

Problem: Rental services (2018 JAN, Silver)
At first, I thought this is gonna be an easy problem, but I ran into some bugs and cannot fix it.

My code passed test cases 1,2,3,5, and 7, but not the rest.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
       return a.second<b.second;

int main(){

	//freopen("", "r", stdin);
	//freopen("rental.out", "w", stdout);

    int n,m,r;
    int cows[n];
    vector<pair<int,int>> stores;
    int farmers[r];
    int prefixsum[r+1];
    for(int i = 0;i<n;i++) cin>>cows[i];
    for(int i = 0;i<m;i++) {int a,b;cin>>a>>b;stores.push_back({a,b});}
    for(int i = 0;i<r;i++) cin>>farmers[i];
    sort(cows, cows+n,greater<int>());
    for(int i = 0;i<=r;i++) prefixsum[i] = 0;
    for(int i = 1;i<=r;i++) prefixsum[i] = prefixsum[i-1] + farmers[i-1];
    long long maxprofit = 0;
    long long curprofit = 0;
    for(int i = 0;i<n;i++){
        if(stores.size()==0) break;
            if(stores.size()==0) break;
            int last = stores.size()-1;
                stores[last].first -= cows[i];
                curprofit += stores[last].second*cows[i];
                cows[i] = 0;
                cows[i]-= stores[last].first;
                curprofit += stores[last].second * stores[last].first;
                stores[last].first = 0;
            if(stores[last].first==0) stores.pop_back();
        int prefixind = min(r,n-i-1);
        maxprofit = max(curprofit+prefixsum[prefixind],maxprofit);

Can you explain your algorithm/what you’ve done to try to debug it?

See the debugging section of How to ask for help on a problem.