• + 1 comment

    I used the same approach but I am getting a segmentation fault in test cases 7-12. Can someone please explain me why?

    #include<iostream>
    using namespace std;
    int main()
    {
        long n,m,a,b,k,sum=0,ans=0;
        cin>>n>>m;
        long int arr[n+1]={0};
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>k;
            arr[a]+=k;
            if(b!=n)
                arr[b+1]-=k;
        }
        for(int i=1;i<=n;i++)
        {
            sum+=arr[i];
            if(sum>ans)
                ans=sum;
        }
        cout<<ans<<endl;
    }