Link to problem statement (Hackerrank):
I’m getting the naive approach i.e I must keep blasting those m balloons which have maximum value at that instant. Clearly this is giving TLE in most of the cases so, if anyone can guide in optimizing it then please guide.
Here’s my attempt:
#include<bits/stdc++.h>
using namespace std;
#include<unordered_set>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb(v,a) v.push_back(a);
#define INF 1000000009
#define MAX 100005
#define pi 3.14159265358979323846
int max(int a,int b){
return (a>b?a:b);
}
int min(int a,int b){
return (a<b?a:b);
}
//SOLVER
void solve(){
int n,m;cin>>n>>m;
multiset<ll> s;
ll u;
rep(i,0,n){
cin>>u;
s.insert(u);
}
bool t=1;ll res=0;
do{
int c=m;
while(c){
int x=*prev(s.end());
if(x!=0){
s.erase(prev(s.end()));
s.insert(x-1);
c--;}
else{t=0;break;}
}
if(t)res++;
}while(t);
cout<<res<<"\n";
}
//drive
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
//cin>>t;
t=1;
while(t--){
solve();
}
return 0;
}