代码如下:
#include <iostream>
using namespace std;
const int N = 1000010;
int r[N],d[N],s[N],t[N];
typedef long long LL;
LL b[N];
int n,m;
bool check(int mid)
{for (int i = 1;i<=n;i++) b[i] = r[i]-r[i-1];for (int i = 1;i<=mid;i++){b[s[i]]-=d[i];b[t[i]+1]+=d[i];}for (int i = 1;i<=n;i++){b[i]+=b[i-1];if (b[i] < 0) return true;}return false;
}int main()
{cin>>n>>m;for (int i = 1;i<=n;i++) cin>>r[i];for (int i = 1;i<=m;i++) cin>>d[i]>>s[i]>>t[i];int l = 1,r = m;if (!check(m)){cout<<"0"<<endl;return 0;}while(l < r){int mid = l+r>>1;if (check(mid)){r = mid;}else{l = mid+1; }}cout<<"-1"<<endl;cout<<r<<endl;return 0;
}