My code is giving the desired output but submit is giving TLE please help me

#include
#include
using namespace std;

int main() {
// your code goes here
int T;
cin>>T;
int tmp;
vector p;
while(T–){
cin>>tmp;
p.push_back(tmp);
}
int q;
cin>>q;
int ans=0;
while(q–){
long long int x,k;
cin>>x>>k;
ans=0;
for(int i=x-1;i<p.size();i++){
if(p[i]==0){
continue;
}
if(k==0){
break;
}
if(k<=p[i]){
p[i]-=k;
k=0;
break;
}else{
k-=p[i];
p[i]=0;
}
ans+=k;
}
cout<<ans<<endl;
}
return 0;
}

It has complexity of O(n*q) that’s why it is giving TLE

3 Likes

so what changes i should make to correct it.