Make Money Problem of CodeChef

 This was the 2nd problem of the day and somehow I solved this in the very first attempt, but even when you solve the problem correctly in paper and also get the correct answer for most of the cases, the only thing you don't wanna do is to forget to print the output. And yes, I did the exact same thing, and was thinking that maybe it's a bad day, cause this would be the 2nd problem with a hidden logical error, but gratefully after reading the code 3rd time, I saw this mistake. After coping with this the code passed all the test cases.

The code is provided below:-

#include <iostream>
#include<vector>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a<b)?a:b
using namespace std;

int main() {
    // your code goes here
    int T;
    cin>>T;
    while(T>0){
        int n,x,c,ans=0;
        cin>>n>>x>>c;
        vector<int> vec(n);
        for(int i=0;i<n;i++){
            cin>>vec[i];
            // When the difference b/w 
            // max possible num and the curr_num is 
            // > Cost then only do something otherwise not
            if((x-vec[i])>c){
                // We will increase this number uptill x and then spend 
                // c cost so we 
                // get this as--> x-c
                ans+=x-c;
            }
            else{
                // Means no change will be made and this num
                // will be as it is bcz there is no profit in increasing this
                ans+=vec[i];
            }
        }
        cout<<ans<<endl;
        T--;
    }
    return 0;
}

Comments

Popular Posts