cswork / OAOpen practice workspace

AmazonOA #160

Find Maximum Money (Fungible SDE 1 :)

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersPrefix & RangesBinary Search

Bags are numbered starting from 1. Within each of the non-overlapping closed intervals [l,r], every bag holds amount v, and bags not listed hold 0. Choose exactly k consecutive bags, first maximize the real amount, then return that maximum modulo 1000000007. The source examples contain intervals that violate the "non-overlapping" guarantee; this site replaces them with non-overlapping examples and does not adopt an additive interpretation.

Input

The first line contains n k, followed by n lines l r v. This site sets 1≤n≤100000, 1≤l≤r≤10^9, 1≤k≤10^9, 1≤v≤10^9; the intervals do not overlap, and the input may be unordered.

Output

Output a single integer answer.

Example 1

Input:

4 5
1 4 2
6 6 5
7 7 7
9 10 1

Output:

16

Example 2

Input:

1 10
2 3 4

Output:

8

Example 3

Input:

2 2
1 2 500000004
4 4 2

Output:

1

Example notes

Example 1: choose 3..7 for an amount of 2+2+0+5+7=16. Example 2: a length of 10 can cover two bags of 4 each, giving 8. Example 3: choose the first two bags; their real amount 1000000008 is greater than any other window, so choose it first and then take the modulo, giving 1.

Editorial

Loading solution…