What's wrong with this approach? (Own intuition)


#1
int Solution::solve(vector<int> &A, int B) {
    priority_queue<int> pq;
    if(A.size() == 0) return 0; // base
    
    for(int i = 0; i<A.size()-1; i++){
        int profit = 0;
        if(A[i+1] > A[i]){
            profit = A[i+1] - A[i];
            pq.push(profit);
        } 
    }
    
    int ans = 0;
    while(B > 0 && !pq.empty()){
        ans+=pq.top();
        pq.pop();
        B--;
    }
    return ans;
}

This was my own attempt to solve this sum…and all custom test cases are satisfied. Where am I going wrong?


#2

my friend consider vector A = {1,2,3}
B = 1
then your algo gives 1 as ans, while correct ans = 2;