C++#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=200009;
int n,m;
int a,b,c,d;
int v[N];
struct work{
int endtime,d;
bool operator<(const work &a) const{
return endtime>a.endtime;
}
};
priority_queue<work>q[N];
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v[i];
}
while(m--){
cin>>a>>b>>c>>d;
while(!q[b].empty()&&q[b].top().endtime<=a){
v[b]+=q[b].top().d;
q[b].pop();
}
if(v[b]<d){
cout<<"-1"<<endl;
}
else{
q[b].push({a+c,d});
v[b]-=d;
cout<<v[b]<<endl;
}
}
return 0;
}