AtCoder Beginner Contest 404(Promotion for Engineer Guild)
G
Specified Range Sums
AtCoder
⌀ 1894
原题
翻译中…

Problem Statement

翻译中…

You are given an integer $N$ and length-$M$ integer sequences $L=(L_1,L_2,\dots,L_M)$, $R=(R_1,R_2,\dots,R_M)$, and $S=(S_1,S_2,\dots,S_M)$.

翻译中…

Determine whether there exists a length-$N$ positive integer sequence $A$ satisfying the following condition. If such a sequence exists, find the minimum possible sum of $A$.

翻译中…
  • $\displaystyle \sum_{j=L_i}^{R_i} A_j = S_i$ for all $i$ ( $1 \le i \le M$ ).
翻译中…

Constraints

翻译中…
  • All input values are integers.
  • $1 \le N,M \le 4000$
  • $1 \le L_i \le R_i \le N$
  • $1 \le S_i \le 10^9$
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M
L_1 R_1 S_1
L_2 R_2 S_2
\vdots
L_M R_M S_M

翻译中…

Output

翻译中…

If there does not exist a length-$N$ positive integer sequence $A$ satisfying the condition, print -1. Otherwise, print the minimum possible sum of $A$ as an integer.

翻译中…

样例

Input #1
5 3
1 2 4
2 3 5
5 5 5
Output #1
12
For example, $A=(1,3,2,1,5)$ satisfies the condition. Its sum is $12$, which is the minimum possible.
Input #2
1 2
1 1 1
1 1 2
Output #2
-1
Sometimes no such $A$ exists.
Input #3
9 6
8 9 8
3 6 18
2 4 19
5 6 8
3 5 14
1 3 26
Output #3
44
题目信息
题号
abc404_g
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。