Hi Everyone,
Here is the solution of #Day1 of #30daysOfCode by Newton School & Everyday Coders🚀
Problem name: Mall and Coupons
Link: https://lnkd.in/dpPk546m
See my solution using a heap.
✅Approach: Take the maximum price and apply a coupon on it if available.
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;
#define ll long long
int main() {
ll n,k,x;
cin >> n >> k >> x;
priority_queue<ll>pq;
for(ll i=0; i<n; i++){
ll a;
cin >> a;
pq.push(a);
}
ll ans=0;
while(1){
ll cur=pq.top();
if(k <=0)
break;
if(cur){
pq.pop();
int times = cur/x;
if(times){
if(times > k)
times=k;
cur-= x*times;
k-=times;
}else{
if(k >0){
cur=0;
--k;}
else break;
}
pq.push(cur);
}else
break;
}
while(pq.size()){
ans += pq.top();
pq.pop();
}
cout << ans;
return 0;
}
If you are a tech enthusiast and want to be part of this program👩🏻💻
registrations are open✨
Register Here: https://30daysofcode.netlify.app/
How to Submit Code: https://youtu.be/lhtgBI9wMCY
Special Thanks to Vaneela Khatri for giving the solution.