PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000239. Thao tác trên mảng (Array Manipulation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Thao tác trên mảng (Array Manipulation)

Array Manipulation

Source: HackerRank

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, an array AA has nn elements, indexed from 11 through nn, and every element is 00.

Process mm operations in the given order. An operation consists of three integers aa, bb, kk and increases every element at positions a,a+1,…,ba,a+1,\ldots,b by kk. Both endpoints are included; positions outside the interval are unchanged.

After all operations have been performed, find the largest value among the nn array elements.

Input

  • The first line contains integers n,mn,m: the array length and the operation count.
  • Each of the next mm lines contains integers a,b,ka,b,k, denoting the inclusive update interval and the amount added to every element in that interval. Array indexing starts at 11.

Output

Print one integer: the maximum array value after all operations.

Subtasks

  • Subtask 1 (20 points): 3≤n≤203\le n\le 20; 1≤m≤201\le m\le 20; 1≤a≤b≤n1\le a\le b\le n and 0≤k≤1090\le k\le10^9 for every operation.
  • Subtask 2 (30 points): 3≤n≤20003\le n\le 2000; 1≤m≤20001\le m\le 2000; 1≤a≤b≤n1\le a\le b\le n and 0≤k≤1090\le k\le10^9 for every operation.
  • Subtask 3 (50 points): 3≤n≤100000003\le n\le 10000000; 1≤m≤2000001\le m\le 200000; 1≤a≤b≤n1\le a\le b\le n and 0≤k≤1090\le k\le10^9 for every operation.

Examples

Input

5 3
1 2 100
2 5 100
3 4 100

Output

200

Explanation

The initial array is [0,0,0,0,0][0,0,0,0,0].

  • After adding 100100 on [1,2][1,2]: [100,100,0,0,0][100,100,0,0,0].
  • After adding 100100 on [2,5][2,5]: [100,200,100,100,100][100,200,100,100,100].
  • After adding 100100 on [3,4][3,4]: [100,200,200,200,100][100,200,200,200,100].

The final maximum is 200200, occurring at positions 2,3,42,3,4, so the output is 200.