1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #define maxn 1000000
5 #define mid (l+r)/2
6 using namespace std;
7
8 int n,m,a,b,c,arr[maxn],data[maxn][3],brr[maxn],x;
9
10 int check(int k){ //O(m+n)
11 memset(brr,0,sizeof(brr));
12
13 for(int i = 1;i <= k;i++){
14 brr[data[i][1]] -= data[i][0];
15 brr[data[i][2]+1] += data[i][0];
16 }
17
18 x = 0;
19
20 for(int i = 1;i <= n;i++){
21 x += arr[i]+brr[i];
22 // if(k == 2) printf("%d ",x);
23 if(x < 0) return 0;
24 }
25
26 // cout << endl;
27
28 return 1;
29 }
30
31 int main(){
32
33 scanf("%d%d",&n,&m);
34
35 for(int i = 1;i <= n;i++){
36 scanf("%d",&brr[i]);
37 arr[i] = brr[i] - brr[i-1];
38 }
39
40 // for(int i = 1;i <= n;i++){
41 // printf("%d ",arr[i]);
42 // }
43
44 // cout << endl;
45
46
47 for(int i = 1;i <= m;i++){
48 scanf("%d%d%d",&data[i][0],&data[i][1],&data[i][2]);
49 }
50
51 int l = 1,r = m;
52 while(l < r){
53 if(check(mid)) l = mid+1;
54 else r = mid;
55 }
56
57 if(!check(r)) printf("-1\n%d",r);
58 else if(!check(l)) printf("-1\n%d",l);
59 else printf("0");
60 // printf("B%d ",r);
61 // printf("C%d ",check(2));
62 return 0;
63 }