GOTHAM (contest : SPYBITS IIT BHU )

hey guys!!

I am unable to figure out why i am getting TLE for GOTHAM in the contest SPYBITS.

MY CODE

It would be great help if anyone can help me out _/_

I think you are unnecessarily removing and adding the value to the set again and again. Try by changing your code so that value is removed only when its 0. so you donâ€™t have to remove and add it again and again.

Why take pair , when u can do without pairs -

``````    lli n;
cin>>n;
lli a[n+1];
seti st;
FOR(i,1,n+1){
cin>>a[i];
st.insert(i);
}
st.insert(n+1);
lli q;
cin>>q;
while(q--)
{
lli x,people;
cin>>x>>people;
lli i  = *st.lower_bound(x);
lli ans = 0;
while(people!=0 and i!=(n+1) )
{
lli prev = i;
if(a[i]>0) //means some capacity is remaining
{
if(a[i]>people){
a[i]-=people;
ans += (i-x)*people;
people = 0;
break;
}
else
{
people-=a[i];
ans+=(i-x)*a[i];
a[i] = 0;
st.erase(i); //bcz if the capacity of i'th box is zero then we have to remove
}
}
i  = *st.lower_bound(prev);
}
print(ans);
}
``````