• + 0 comments

    A java version:

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
            
        int N = in.nextInt();
        int M = in.nextInt();
            
        int[] arr = new int[N];
            
        int a;
        int b;
        int k;
        while (M-- > 0)
        {
            a = in.nextInt() - 1;
            b = in.nextInt();
            k = in.nextInt();
                
            arr[a] += k;
            if (b < N) arr[b] -= k;
        }
        in.close();
            
        long max = Long.MIN_VALUE;
        long sum = 0;
        for (int value : arr)
        {
            sum += value;
            if (sum > max)
            {
                max = sum;
            }
        }
            
        System.out.println(max);
    }