AtCoder Regular Contest 146
D
>=<
AtCoder
⌀ 2663
原题
← CE →
翻译中…

Problem Statement

翻译中…

A fantastic IS is an integer sequence of length $N$ whose every element is between $1$ and $M$ (inclusive) that satisfies the following condition.

翻译中…
  • For every integer $i$ such that $1 \le i \le K$, one of the following holds.
  • $A_{P_i} < X_i$ and $A_{Q_i} < Y_i$;
    • $A_{P_i} = X_i$ and $A_{Q_i} = Y_i$;
    • $A_{P_i} > X_i$ and $A_{Q_i} > Y_i$.
翻译中…

Determine whether a fantastic IS exists. If it does, find the minimum possible sum of the elements in a fantastic IS.

翻译中…

Constraints

翻译中…
  • $1 \le N,M,K \le 2 \times 10^5$
  • $1 \le P_i,Q_i \le N$
  • $1 \le X_i,Y_i \le M$
  • $P_i \neq Q_i$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N M K
P_1 X_1 Q_1 Y_1
P_2 X_2 Q_2 Y_2
\vdots
P_K X_K Q_K Y_K

翻译中…

Output

翻译中…

If a fantastic IS exists, print the minimum possible sum of the elements in a fantastic IS; otherwise, print $-1$.

翻译中…

样例

Input #1
3 4 3
3 1 1 2
1 1 2 2
3 4 1 4
Output #1
6
$A=(2,3,1)$ fully satisfies the condition and thus is a fantastic IS, whose sum of the elements is $6$. There is no fantastic IS whose sum of the elements is less than $6$, so the answer is $6$.
Input #2
2 2 2
1 1 2 2
2 1 1 2
Output #2
-1
There is no fantastic IS, so $-1$ should be printed.
Input #3
5 10 10
4 1 2 7
5 1 3 2
2 9 4 4
5 4 2 9
2 9 1 9
4 8 3 10
5 7 1 5
3 5 1 2
3 8 2 10
2 9 4 8
Output #3
12
题目信息
题号
arc146_d
时间限制
4 s
内存限制
1024 MB

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